"Java" "Flume" flume-ng boot Process source code Analysis (i)

Source: Internet
Author: User

From Bin/flume this shell script can see Flume starting from the Org.apache.flume.node.Application class, which is where the main function of Flume is.

The Main method first parses the shell command, assuming that the specified configuration file does not exist and then dumps the exception.

According to the command contains the "no-reload-conf" parameters, decided to use the kind of loading configuration file mode: First, no this parameter. The configuration file is loaded dynamically, and the configuration file is loaded once every 30 seconds, so the configuration file can be changed dynamically. In this case, only one configuration file is loaded at startup.

Implementing the dynamic Onboarding feature uses the advertised subscription model, which is implemented using the Eventbus in guava.

Eventbus Eventbus =NewEventbus (AgentName + "-event-bus"); Pollingpropertiesfileconfigurationprovider Configurationprovider =NewPollingpropertiesfileconfigurationprovider (AgentName, ConfigurationFile, Eventbus,30);        Here is the class that publishes the event, here 30 is the dynamic load the configuration file time interval, the unit is S Components.add (Configurationprovider); Application=Newapplication (components);    Eventbus.register (application); Register the subscription class with the bus

subscription class is APPLICATION  = new  application ( Release code in .

In this is just the first to build a pollingpropertiesfileconfigurationprovider object, Pollingpropertiesfileconfigurationprovider extends  Propertiesfileconfigurationprovider Implements Lifecycleaware, continue tracking Propertiesfileconfigurationprovider extends Abstractconfigurationprovider. Tracking Abstractconfigurationprovider implements  Configurationprovider To see how these classes are constructed is initialized. abstractconfigurationprovid's construction method initializes the sink, channel, The factory class for source.

application . Handleconfigurationevent (materializedconfiguration conf) has @subscribe annotations, is the subscription method, when Eventbus.post (materializedconfiguration conf) The runtime triggers the run

  New Application (components) , an object is built supervisor = new Lifecyclesupervisor () initiates 10 threads to execute each component in the configuration file. and monitor the entire execution of the component.

The Application.start () method initiates the loading process of the configuration file Supervisor.supervise (component, New Supervisorpolicy.alwaysrestartpolicy (), Lifecyclestate.start); Lifecyclestate.start starts execution. Here's theComponentThat's the top.PollingpropertiesfileconfigurationproviderObject.supervisemethod is used toComponentCreate amonitorrunnableProcess. And put a default of 10 threadsMonitorserviceTo perform

Supervisoree process =NewSupervisoree (); Process.status=NewStatus (); Process.policy=policy; Process.status.desiredState=desiredstate; Process.status.error=false; Monitorrunnable monitorrunnable=Newmonitorrunnable (); Monitorrunnable.lifecycleaware= Lifecycleaware;//ComponentsMonitorrunnable.supervisoree =process; Monitorrunnable.monitorservice=Monitorservice;    Supervisedprocesses.put (lifecycleaware, process); //creates and runs a recurring action that is first enabled after a given initial delay. Then. There is a given delay between each run termination and the next run start.

Assuming any run of the task encounters an exception, the run may be canceled. scheduledfuture<?

> future = monitorservice.schedulewithfixeddelay (monitorrunnable, 0, 3, timeunit.seconds); // start monitorrunnable, 3 seconds after the end to start again, can be used to retry monitorfutures.put (Lifecycleaware, future);

Look at the monitorrunnable class. The Run method mainly runs the corresponding operation according to the value of supervisoree.status.desiredState.

The lifecycleaware here is the componentin the supervise method above,Lifecycleaware at the beginning of the construction will Lifecyclestate=idle, The Application.start () method uses the Supervisor.supervise method to Supervisoree.status.desiredstate=start. So the Run method runs Lifecycleaware.start (), which is the Pollingpropertiesfileconfigurationprovider. Start () method.

< Span style= "margin:0px; padding:0px; line-height:1.5 ">pollingpropertiesfileconfigurationprovider . Start () Method starts a single-threaded filewatcherrunnable every 30s to load the configuration file (assuming the configuration file has changed): Eventbus.post (GetConfiguration ()). getCo Nfiguration () is Abstractconfigurationprovider. getCo Nfiguration () This method resolves the configuration file to get all the components and their configuration properties. This method is more complex. Put it on perhaps another explanation.

eventbus.post (GetConfiguration ()) will trigger application . Handleconfigurationevent method:

  @Subscribe  publicsynchronizedvoid  handleconfigurationevent ( Materializedconfiguration conf) {    stopallcomponents ();    Startallcomponents (conf);  }

  The stopallcomponents () method sequentially stops the execution of individual components. The order is: source, sink, channel.

The order is because: first, the source is kept reading data into the channel; second, Sink is constantly from the channel to take data. Both ends of the channel should stop at the end, stop sending data to the channel and sink stop before the data is lost. Stop is completed by the Supervisor.unsupervise method.

  startallcomponents (conf) is the start of each component, in the same order as the stopallcomponents () stop order. I believe everyone is very easy to understand. is to start the component through Supervisor.supervise. It is also important to note that a certain amount of time is required after the channel component is started in order for all channel to be started.

Besides, why stop and start first? Due to the fact that you want to load the configuration file dynamically. Once you have loaded the configuration file, you need to start all the components again. So stop all of it first, and start all over again.

The last other hook function of the Main method Runtime.getruntime (). Addshutdownhook, mainly used for memory cleanup, object destruction and other operations.

"Java" "Flume" flume-ng boot Process source code Analysis (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.