-
Notifications
You must be signed in to change notification settings - Fork 105
/
CygnusApplication.java
549 lines (468 loc) · 23.7 KB
/
CygnusApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/**
* Copyright 2015-2017 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of fiware-cygnus (FIWARE project).
*
* fiware-cygnus is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
* General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
* fiware-cygnus is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License along with fiware-cygnus. If not, see
* http://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License please contact with iot_support at tid dot es
*/
package com.telefonica.iot.cygnus.nodes;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.flume.Channel;
import org.apache.flume.Constants;
import org.apache.flume.SinkRunner;
import org.apache.flume.SourceRunner;
import org.apache.flume.lifecycle.LifecycleAware;
import org.apache.flume.lifecycle.LifecycleState;
import org.apache.flume.lifecycle.LifecycleSupervisor;
import org.apache.flume.node.Application;
import org.apache.flume.node.MaterializedConfiguration;
import org.apache.flume.node.PollingPropertiesFileConfigurationProvider;
import org.apache.flume.node.PropertiesFileConfigurationProvider;
import org.apache.logging.log4j.ThreadContext;
import java.util.UUID;
import java.util.Map;
//import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.telefonica.iot.cygnus.channels.CygnusChannel;
import com.telefonica.iot.cygnus.channels.CygnusFileChannel;
import com.telefonica.iot.cygnus.channels.CygnusMemoryChannel;
import com.telefonica.iot.cygnus.http.JettyServer;
import com.telefonica.iot.cygnus.log.CygnusLogger;
import com.telefonica.iot.cygnus.management.ManagementInterface;
import com.telefonica.iot.cygnus.utils.CommonConstants;
import com.telefonica.iot.cygnus.utils.CommonUtils;
/**
* CygnusApplication is an extension of the already existing org.apache.flume.node.Application. CygnusApplication
* is closed in an ordered way, first the sources in order to no not receiving further notifications, then the
* application waits until the channels are emptied by the sinks, finally the sinks are closed.
*
* Java Reflection has been used in order to access the LifecycleSupervisor supervisor private variable since this
* object allows to effectively stop the Cygnus agent components (if directly stoped from the components referecnes
* then the lifecycle supervisor starts them again).
*
* Cygnus agent components references are obtained only once, at handleConfigurationEvent method since it already
* receives as an argument a MaterializedConfiguration object (if a new MaterializedConfiguration is gotten then new
* instances of the components are started).
*
* @author frb
*/
public class CygnusApplication extends Application {
private static final CygnusLogger LOGGER = new CygnusLogger(CygnusApplication.class);
private static JettyServer mgmtIfServer;
private static Map<String, SourceRunner> sourcesRef;
private static Map<String, Channel> channelsRef;
private static Map<String, SinkRunner> sinksRef;
private static LifecycleSupervisor supervisorRef;
private static final int CHANNEL_CHECKING_INTERVAL = 5000;
private static final int YAFS_CHECKING_INTERVAL = 1000;
private static final int DEF_MGMT_IF_PORT = 8081;
private static final int DEF_GUI_PORT = 8082;
private static final int DEF_POLLING_INTERVAL = 30;
private boolean firstTime = true;
private static YAFS yafs = null;
/**
* Constructor.
*/
public CygnusApplication() {
super();
} // CygnusApplication
/**
* Constructor.
* @param components
*/
public CygnusApplication(List<LifecycleAware> components) {
super(components);
try {
// get a reference to the supervisor, if not possible then Cygnus application cannot start
getSupervisorRef();
} catch (NoSuchFieldException | IllegalAccessException e) {
LOGGER.debug(e.getMessage());
supervisorRef = null;
} // try catch
} // CygnusApplication
/**
* Gets a reference to the private variable "supervisor" within the super class "Application". This is achieved by
* using Java Reflection.
*/
private void getSupervisorRef() throws NoSuchFieldException, IllegalAccessException {
// get a reference to the supervisor object, this will be needed when shutting down Cygnus in a certain order
Field privateField = Application.class.getDeclaredField("supervisor");
privateField.setAccessible(true);
supervisorRef = (LifecycleSupervisor) privateField.get(this);
privateField.setAccessible(false);
} // getSupervisorRef
/**
* Stops and starts all the components when a configuration change event is generated. It also gets a reference to
* all the Flume components.
*
* @param conf
*/
@Override
@Subscribe
public synchronized void handleConfigurationEvent(MaterializedConfiguration conf) {
LOGGER.debug("handleConfigurationEvent with conf: " + conf.toString());
// Stop checking threads until configuration is loaded
if (yafs != null) {
yafs.setReloading(true);
LOGGER.debug("Pausing YAFS.");
}
super.handleConfigurationEvent(conf);
if (firstTime) {
// get references to the different elements of the agent, this will be needed when shutting down Cygnus in a
// certain order
sourcesRef = conf.getSourceRunners();
channelsRef = conf.getChannels();
sinksRef = conf.getSinkRunners();
LOGGER.debug("References to Flume components have been taken for fist time");
firstTime = false;
return;
} // if
// get references to the different elements of the agent, this will be needed when shutting down Cygnus in a
// certain order
sourcesRef = conf.getSourceRunners();
channelsRef = conf.getChannels();
sinksRef = conf.getSinkRunners();
LOGGER.debug("References to Flume components have been taken");
// Resume Thread checking
if (yafs != null) {
yafs.setReloading(false);
LOGGER.debug("Resuming YAFS.");
}
} // handleConfigurationEvent
/**
* Main application to be run when this CygnusApplication is invoked. The only differences with the original one
* are the CygnusApplication is used instead of the Application one, and the Management Interface port option in
* the command line.
* @param args
*/
public static void main(String[] args) {
try {
// Set some MDC logging fields to 'N/A' for this thread
// Later in this method the component field will be given a value
ThreadContext.put("id", UUID.randomUUID().toString());
ThreadContext.put(CommonConstants.LOG4J_CORR, CommonConstants.NA);
ThreadContext.put(CommonConstants.LOG4J_TRANS, CommonConstants.NA);
ThreadContext.put(CommonConstants.LOG4J_SVC, CommonConstants.NA);
ThreadContext.put(CommonConstants.LOG4J_SUBSVC, CommonConstants.NA);
ThreadContext.put(CommonConstants.LOG4J_COMP, CommonConstants.NA);
// Print Cygnus starting trace including version
LOGGER.info("Starting Cygnus, version " + CommonUtils.getCygnusVersion() + "."
+ CommonUtils.getLastCommit());
CommonUtils.printLoadedJars();
// Define the options to be read
Options options = new Options();
Option option = new Option("n", "name", true, "the name of this agent");
option.setRequired(true);
options.addOption(option);
option = new Option("f", "conf-file", true, "specify a conf file");
option.setRequired(true);
options.addOption(option);
option = new Option(null, "no-reload-conf", false, "do not reload conf file if changed");
options.addOption(option);
option = new Option("h", "help", false, "display help text");
options.addOption(option);
option = new Option("p", "mgmt-if-port", true, "the management interface port");
option.setRequired(false);
options.addOption(option);
option = new Option("g", "gui-port", true, "the GUI port");
option.setRequired(false);
options.addOption(option);
option = new Option("t", "polling-interval", true, "polling interval");
option.setRequired(false);
options.addOption(option);
option = new Option("6", "ipv6", false, "use ipv6 instead ipv4");
options.addOption(option);
option = new Option(null, "no-yafs", false, "do not use YAFS");
options.addOption(option);
// Read the options
CommandLineParser parser = new GnuParser();
CommandLine commandLine = parser.parse(options, args);
File configurationFile = new File(commandLine.getOptionValue('f'));
String agentName = CommonConstants.DEF_AGENT_NAME;
if (commandLine.hasOption('n')) {
agentName = commandLine.getOptionValue('n');
} // if
boolean reload = !commandLine.hasOption("no-reload-conf");
String configurationPath = configurationFile.getParent();
if (commandLine.hasOption('h')) {
new HelpFormatter().printHelp("cygnus-flume-ng agent", options, true);
return;
} // if
int apiPort = DEF_MGMT_IF_PORT;
if (commandLine.hasOption('p')) {
apiPort = new Integer(commandLine.getOptionValue('p'));
} // if
int guiPort = DEF_GUI_PORT;
if (commandLine.hasOption('g')) {
guiPort = new Integer(commandLine.getOptionValue('g'));
} else {
guiPort = 0; // this disables the GUI for the time being
} // if else
int pollingInterval = DEF_POLLING_INTERVAL;
if (commandLine.hasOption('t')) {
pollingInterval = new Integer(commandLine.getOptionValue('t'));
} // if
boolean ipv6 = false;
if (commandLine.hasOption('6')) {
ipv6 = true;
} // if
boolean noYAFS = false;
if (commandLine.hasOption("no-yafs")) {
noYAFS = true;
} // if
// the following is to ensure that by default the agent will fail on startup if the file does not exist
if (!configurationFile.exists()) {
// if command line invocation, then need to fail fast
if (System.getProperty(Constants.SYSPROP_CALLED_FROM_SERVICE) == null) {
String path = configurationFile.getPath();
try {
path = configurationFile.getCanonicalPath();
} catch (IOException e) {
LOGGER.error("Failed to read canonical path for file: " + path + ". Details="
+ e.getMessage());
} // try catch
throw new ParseException("The specified configuration file does not exist: " + path);
} // if
} // if
List<LifecycleAware> components = Lists.newArrayList();
CygnusApplication application;
if (reload) {
LOGGER.debug("no-reload-conf was not set, thus the configuration file will be polled each 30 seconds");
EventBus eventBus = new EventBus(agentName + "-event-bus");
PollingPropertiesFileConfigurationProvider configurationProvider =
new PollingPropertiesFileConfigurationProvider(agentName, configurationFile, eventBus,
pollingInterval);
components.add(configurationProvider);
application = new CygnusApplication(components);
application.handleConfigurationEvent(configurationProvider.getConfiguration());
eventBus.register(application);
} else {
LOGGER.debug("no-reload-conf was set, thus the configuration file will only be read this time");
PropertiesFileConfigurationProvider configurationProvider =
new PropertiesFileConfigurationProvider(agentName, configurationFile);
application = new CygnusApplication();
application.handleConfigurationEvent(configurationProvider.getConfiguration());
} // if else
// Set MDC logging field value for component
ThreadContext.put(CommonConstants.LOG4J_COMP, agentName);
// start the Cygnus application
application.start();
// wait until the references to Flume components are not null
try {
while (sourcesRef == null || channelsRef == null || sinksRef == null) {
LOGGER.info("Waiting for valid Flume components references...");
Thread.sleep(1000);
} // while
} catch (InterruptedException e) {
LOGGER.error("There was an error while waiting for Flume components references. Details: "
+ e.getMessage());
} // try catch
// start the Management Interface, passing references to Flume components
if (ipv6) {
LOGGER.info("Starting a Jetty server listening on ::0:" + apiPort + " (Management Interface)");
} else {
LOGGER.info("Starting a Jetty server listening on 0.0.0.0:" + apiPort + " (Management Interface)");
} // if else
mgmtIfServer = new JettyServer(apiPort, guiPort, new ManagementInterface(configurationPath,
configurationFile, sourcesRef, channelsRef, sinksRef, apiPort, guiPort), ipv6);
mgmtIfServer.start();
if (!noYAFS) {
// create a hook "listening" for shutdown interrupts (runtime.exit(int), crtl+c, etc)
Runtime.getRuntime().addShutdownHook(new AgentShutdownHook("agent-shutdown-hook", supervisorRef));
// start YAFS
yafs = new YAFS();
yafs.start();
} // if
} catch (IllegalArgumentException | ParseException e) {
LOGGER.error("A fatal error occurred while running. Exception follows. Details=" + e.getMessage());
} // try catch
} // main
/**
* Implements a thread that starts when the Cygnus applications exits (runtime.exit(int), ctrl+c, etc).
*/
private static class AgentShutdownHook extends Thread {
private final LifecycleSupervisor supervisorRef;
/**
* Constructor.
* @param name
*/
public AgentShutdownHook(String name, LifecycleSupervisor supervisorRef) {
super(name);
this.supervisorRef = supervisorRef;
} // AgentShutdownHook
@Override
public void run() {
if (supervisorRef == null) {
System.err.println("Cygnus cannot be shutdown in an ordered way since the supervisor variable at "
+ "super class org.apache.flume.node.Application could not be accessed");
return;
} // if
try {
System.out.println("Starting an ordered shutdown of Cygnus");
// stop the sources
System.out.println("Stopping sources");
stopSources();
// wait until the channels are empty; if at least one of them has a single event, Cygnus cannot stop
while (true) {
Iterator it = channelsRef.keySet().iterator();
boolean emptyChannels = true;
while (it.hasNext()) {
String channelName = (String) it.next();
Channel channel = channelsRef.get(channelName);
CygnusChannel cygnusChannel;
if (channel instanceof CygnusMemoryChannel) {
cygnusChannel = (CygnusMemoryChannel) channel;
} else if (channel instanceof CygnusFileChannel) {
cygnusChannel = (CygnusFileChannel) channel;
} else {
continue;
} // if else
long numEvents = cygnusChannel.getNumEvents();
if (numEvents != 0) {
System.out.println("There are " + numEvents + " events within " + channelName
+ ", Cygnus cannnot shutdown yet");
emptyChannels = false;
break;
} // if
} // while
if (emptyChannels) {
System.out.println("All the channels are empty");
break;
} else {
System.out.println("Waiting 5 seconds");
Thread.sleep(CHANNEL_CHECKING_INTERVAL);
} // if else
} // while
// stop the channels
System.out.println("Stopping channels");
stopChannels();
// stop the sinks
System.out.println("Stopping sinks");
stopSinks();
} catch (InterruptedException e) {
System.err.println("There was some problem while shutting down Cygnus. Details=" + e.getMessage());
} // try catch
} // run
/**
* Stops the sources.
*/
private void stopSources() {
for (String sourceName : sourcesRef.keySet()) {
SourceRunner source = sourcesRef.get(sourceName);
LifecycleState state = source.getLifecycleState();
System.out.println("Stopping " + sourceName + " (lyfecycle state=" + state.toString() + ")");
supervisorRef.unsupervise(source);
} // for
} // stopSources
/**
* Stops the channels.
*/
private void stopChannels() {
for (String channelName : channelsRef.keySet()) {
Channel channel = channelsRef.get(channelName);
LifecycleState state = channel.getLifecycleState();
System.out.println("Stopping " + channelName + " (lyfecycle state=" + state.toString() + ")");
supervisorRef.unsupervise(channel);
} // for
} // stopChannels
/**
* Stops the sinks.
*/
private void stopSinks() {
for (String sinkName : sinksRef.keySet()) {
SinkRunner sink = sinksRef.get(sinkName);
LifecycleState state = sink.getLifecycleState();
System.out.println("Stopping " + sinkName + " (lyfecycle state=" + state.toString() + ")");
supervisorRef.unsupervise(sink);
} // for
} // stopSinks
} // AgentShutdownHook
/**
* Yet Another Flume Supervisor. FIXME: this is a shortcut avoiding to extend the original LifecycleSupervisor
* class from Apache Flume, which can be be hard to do. Nevertheless, a tech debt issue has been created regarding
* this: https://github.com/telefonicaid/fiware-cygnus/issues/354
*/
protected static class YAFS extends Thread {
private AtomicBoolean reloading = new AtomicBoolean(false); //Cysgnus is reloading configuration
private AtomicBoolean waiting = new AtomicBoolean(false); //Waiting for Cygnus to finish new configuration load
/**
* @return if it is reloading
*/
public boolean isReloading() {
return reloading.get();
}
/**
* @param reloading value to set
*/
public void setReloading(boolean reloading) {
if (reloading) {
waiting.set(true);
}
this.reloading.set(reloading);
}
@Override
public void run() {
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
// Regex matching Jetty thread names, like: qtp586434923-27 or 106024875@qtp-1176250385-1
String jettyThreadNamePattern = "^(\\d+@)?qtp-?\\d{2,}-\\d+";
while (true) {
if (waiting.get()) {
if (!reloading.get()) {
LOGGER.debug("YAFS reloading Thread List.");
threadSet = Thread.getAllStackTraces().keySet();
threadArray = threadSet.toArray(new Thread[threadSet.size()]);
waiting.set(false);
} else {
try {
Thread.sleep(YAFS_CHECKING_INTERVAL);
} catch (InterruptedException ex) {
LOGGER.error("YAFS: Error trying to sleep thread.");
}
}
} else {
for (Thread t: threadArray) {
// exit Cygnus if some thread (except for the main one and threads from the Jetty
// QueuedThreadPool (qtp)) is found to be not alive or in a terminated state
if ((t.getState() == State.TERMINATED || !t.isAlive())
&& !t.getName().equals("main") && !t.getName().matches(jettyThreadNamePattern)) {
LOGGER.error("Thread found not alive, exiting Cygnus. ID=" + t.getId()
+ ", name=" + t.getName());
System.exit(-1);
} // if
} // for
try {
Thread.sleep(YAFS_CHECKING_INTERVAL);
} catch (InterruptedException ex) {
System.exit(-1);
}
} // if waiting
} // while
} // run
} // YAFS
} // CygnusApplication