Background: (brief)
Pinpoint is a set of APM (Application performance Management) tools designed to help analyze the overall structure of the system and how components are called each other, as well as to track performance issues on the line, and to easily locate the point where the problem occurs.
Pinpoint mainly has the following components:
- Pinpoint Agent: By bytecode enhancement technology, attached to the user's Java application to do the sampling, the program starts with the designation of Javaagent and Agentid,pplicationname.
- HBase: Data that is used to store agent samples.
- Pinpoint Collector: Information collector, deployed in Tomcat, due to collect data from agent samples and deposit into hbase.
- Pinpoint Web: Provides a WEB_UI interface, deployed in Tomcat, provides a visual page, and provides a monitoring alarm function. (You need to implement it yourself)
Blog main content and main purpose: Pinpoint alarm function is needed to expand the implementation of the network, there are many ways to achieve, but there is no source for this alarm mechanism analysis, the purpose of this article is to fill the gap here, so that readers have some basic understanding, so debugging the alarm can be handy. Background knowledge: (The details can be Baidu a bit, the following will also introduce relevant content)
Spring task:spring built-in Scheduled tasks.
Spring Batch: A parallel processing framework for large data volumes.
Overview:
Through the scheduled task of spring task, a batch check through Springbatch is done every minute.
The module GitHub Source address:
Https://github.com/naver/pinpoint/tree/master/web/src/main/java/com/navercorp/pinpoint/web/alarm
Source Code Analysis: (The focus of this article) 1. Timed Task Entry:
Src/main/resources/batch/applicationcontext-batch-schedule.xml
<Task:scheduled-tasksScheduler= "Scheduler"> <task:scheduledref= "Batchjoblauncher"Method= "Alarmjob"Cron= "0 0/1 * * * *" /> <!--omitted -</Task:scheduled-tasks><Task:schedulerID= "Scheduler"pool-size= "5"/>
This code tag uses the springtask tag, presumably meaning that a scheduler scheduler with a thread pool size of 5 is defined.
Scheduler perform the task of three, Batchjoblauncher Alarmjob method, every minute to perform once, Alarmjob, as the name implies, alarm task.
2. Batch Task Portal: Src/main/resources/batch/applicationcontext-alarmjob.xml
The preceding instructions are explained in the-springbatch batch framework in this context:
In the case of batch processing, there is a natural batch task (corresponding to the job tag below), and each task naturally has one or more steps (corresponding to the step label below).
Each step has three operations, reads the data (corresponding to reader), processes the data (corresponding to the processor), and writes back the data (corresponding to writer). The parameters in these three are passed in order.
You might think, what is the relationship between the alarm mechanism and reading data, processing data, and writing back data? Let me explain the corresponding business relation of pinpoint:
Reader: Read data-provides checker, the exception validator, through user-configured rules.
Processor: processing data and checking with checker to mark abnormal state.
Writer: Write back data and judge whether there are abnormal checker, there is the alarm.
The following configuration of the source code:
<!--defines a alramjob batch task--<batch:job id= "Alarmjob" > <batch:step id= "Alarmpartitionstep" > <!--This alarmjob has only one step--> <batch:partition step= "Alarmstep" partitioner= "Alarmpartitioner" > <!--set the thread pool for execution--<batch:handler task-executor= "alarmpooltaskexecutorforpartition"/> &L t;/batch:partition> </batch:step> <batch:listeners> <batch:listener ref= "Jobfaillistener"/&G T </batch:listeners></batch:job><batch:step id= "Alarmstep" > <!--represents a processing strategy for step--<batch: Tasklet> <!--Batch Process-<!--sequential Execution reader: Read the data and provide checker, the exception checker, see the following Bean Proces SOR: processing data and check with checker, see the following bean Writer: write back data and determine whether there are anomalies checker, there is the alarm, see the following bean--<BATC H:chunk reader= "Reader" processor= "Processor" writer= "writer" commit-interval= "1"/> </batch:tasklet></ Batch:step><bean id="Alarmpartitioner" class= "Com.navercorp.pinpoint.web.alarm.AlarmPartitioner"/><bean id= "reader" class= " Com.navercorp.pinpoint.web.alarm.AlarmReader "scope=" step "/><bean id=" Processor "class=" Com.navercorp.pinpoint.web.alarm.AlarmProcessor "scope=" step "/><bean id=" writer "class=" Com.navercorp.pinpoint.web.alarm.AlarmWriter "scope=" Step "/>
3. Through the analysis of Alarmreader, Alarmprocessor, Alarmwriter, combing the alarm principle (1) Com.navercorp.pinpoint.web.alarm.AlarmReader: Implements Itemreader interface
Stepexecutionlistener Listener, you can define the operation before and after the step start public class Alarmreader implements ITEMREADER<ALARMCHECKER> Stepexecutionlistener {...//alarm checker in memory private final queue<alarmchecker> checkers = new Concurrentl Inkeddeque<> (); ...//checker out of the team for processor use, @Override public alarmchecker read () {return checkers.poll (); }//batch, add the applied alarm rule to checker @Override public void Beforestep (Stepexecution stepexecution) {//query all Apps list<application> applicationlist = Applicationindexdao.selectallapplicationnames (); Add Checker to queue for (application application:applicationlist) {addchecker (application) According to the application user-configured rules; }} private void Addchecker (Application application) {/////To get all the rules according to the application name, the application name is when configuring the agent, the specified applicationn Ame list<rule> rules = Alarmservice.selectrulebyapplicationid (Application.getname ()); Long timeslotendtime = System.currenttimemillis (); MAp<datacollectorcategory, datacollector> collectormap = new hashmap<> (); Traversal rule for (rule rule:rules) {///Checkercategory is an enumeration class that presets all the alarm rule templates, such as the number of failed requests, the number of slow requests, and so on Checkerca Tegory checkercategory = Checkercategory.getvalue (Rule.getcheckername ()); The data collector is prepared for the inspection rule, such as rule is the number of failed requests, but the number of times from which the datacollector collector = Collectormap.get (checkercategory) is from this collector. Getdatacollectorcategory ()); Here is a map-based cache if (collector = = null) {collector = Datacollectorfactory.createdatacollector (c Heckercategory, application, timeslotendtime); Collectormap.put (Collector.getdatacollectorcategory (), collector); }//Create checker, interested readers can look at Checkercategroy source code, design is very good. Alaramchecker is an abstract method in which specific functions are implemented by subclasses Alarmchecker Checker = Checkercategory.createchecker (collector, rule); Join Queue Checkers.add (checker); } } ...}
(2) Com.navercorp.pinpoint.web.alarm.AlarmProcessor: Implements the Itemprocessor interface
public class Alarmprocessor implements Itemprocessor<alarmchecker, alarmchecker> {//Alarmchecker here is the above read () Method passed over the
@Override public alarmchecker process (Alarmchecker checker) { //check, as the name implies, test, mark if there is an exception, check () method see below Checker. Check (); return checker;} }
Com.navercorp.pinpoint.web.alarm.checker. Alarmprocessor
protected Abstract Boolean Decideresult (T value); Public void Check () { // Collect Data datacollector.collect (); // the // detected field will be checked in subsequent writter = Decideresult (Getdetectedvalue () ); }
(3) Com.navercorp.pinpoint.web.alarm.AlarmWriter: Implements the Itemwriter interface
public class Alarmwriter implements itemwriter<alarmchecker> {//Alarmmessagesender that require user-defined configuration in spring, if not configured, is an empty implementation @Autowired (required = false) Private Alarmmessagesender Alarmmessagesender = new Emptymessagesender (); @Autowired private Alarmservice Alarmservice;
The method of implementing the interface, the main content @Override public void write (list<? extends Alarmchecker> checkers) throws Exception { map<string, checkerresult> beforecheckerresults = alarmservice.selectbeforecheckerresults (checkers.get (0). Getrule (). Getapplicationid ()); Traverse the above passed checker for (Alarmchecker checker:checkers) {Checkerresult Beforecheckerresult = Beforechec Kerresults.get (Checker.getrule (). Getcheckername ()); if (Beforecheckerresult = = null) {Beforecheckerresult = new Checkerresult (Checker.getrule (). getapplication Id (), Checker.getrule (). Getcheckername (), False, 0, 1); }//Check the detected value of the processor tag above if (checker.isdetected ()) {Sendalarmmessage (befor Echeckerresult, checker); }//Record alarm history alarmservice.updatebeforecheckerresult (Beforecheckerresult, checker); }} private void Sendalarmmessage (Checkerresult beforecheckerresult, ALARMCHecker checker) {if (Isturntosendalarm (Beforecheckerresult)) {//is configured to send alarm SMS if (checker.is Smssend ()) {alarmmessagesender.sendsms (checker, Beforecheckerresult.getsequencecount () + 1); }//is configured to send an alert message if (Checker.isemailsend ()) {Alarmmessagesender.sendemail (checker, B Eforecheckerresult.getsequencecount () + 1); } } } // ...}
Pinpoint web alarm mechanism source code Analysis