Apache LRU algorithm Problem Analysis and Solution

Source: Internet
Author: User

Apache LRU algorithm Problem Analysis and Solution

Udl (United data load) is a general data loading tool developed by the company. It supports extraction settings, job definition, page monitoring, and other functions, A udl job scheduling problem occurs many times in a project. Because the problem is hidden, it takes a lot of effort to analyze and solve it. Now, we share our experiences with you.
1. symptom
The problem generally occurs when the udl runs for 3-5 days and the error is fixed. The following exception is thrown in the background (the red part is the starting point of the analysis below ):

1 01:05:00, 078 [] Error-job: e_f_table_all_workid an unpredictable error occurs during the running process. The current job stops running in this scheduling and an unpredictable error occurs, this is usually caused by incorrect basic information configuration or abnormal running environment. Check the Cause according to the error message [uruntimejob: runjob]
2 Java. lang. illegalstateexception: entry. next = NULL, data [removeindex] = resoft. udl. schedule. jobkey @ 600a5e7d = false previous = resoft. udl. schedule. jobkey @ 74a3b4ef = false key = resoft. udl. schedule. jobkey @ fb09e543 value = true size = 150 maxsize = 150 please check that your keys are immutable, and that you have used synchronization properly. if so, then please report this to commons-dev@jakarta.apache.org as a bug.
3 At org. Apache. commons. Collections. Map. lrumap. reusemapping (lrumap. Java: 300)
4 At org. Apache. commons. Collections. Map. lrumap. addmapping (lrumap. Java: 266)
5 At org. Apache. commons. Collections. Map. Fig. Put (fig. Java: 283)
6 At resoft. udl. jetlvalidate. logicvalidate. validator. validatorfactory. setjobover (validatorfactory. Java: 57)
7 At resoft. udl. Job. uruntimejob. runjob (uruntimejob. Java: 389)
8 At resoft.udl.job.uruntimejob.exe cute (uruntimejob. Java: 132)
9 at org. quartz.core. jobrunshell. Run (jobrunshell. Java: 202)
10 at edu. Emory. mathcs. backport. java. util. Concurrent. threadpoolexecutor. runworker (threadpoolexecutor. Java: 987)
11 at edu. Emory. mathcs. backport. java. util. Concurrent. threadpoolexecutor $ worker. Run (threadpoolexecutor. Java: 528)
12 at java. Lang. thread. Run (thread. Java: 595)

 

2. Problem Analysis
Tracked the setjobover method in udl code validatorfactory. java.

1 Private Static map isjobovermap = new lrumap (100 );
2 public static void setjobover (string jobid, string date, Boolean istaskjobover ){
3 isjobovermap. Put (New jobkey (jobid, date), Boolean. valueof (istaskjobover ));
4}

The Code called in lrumap. Java is as follows (red indicates that an exception is thrown ):

1 protected void reusemapping (linkentry entry, int hashindex, int hashcode, object key, object Value ){
2 try {
3 int removeindex = hashindex (entry. hashcode, Data. Length );
4 hashentry [] TMP = data; // may protect against some sync issues
5 hashentry loop = TMP [removeindex];
6 hashentry previous = NULL;
7 while (loop! = Entry & loop! = NULL ){
8 Previous = loop;
9 loop = loop. Next;
10}
11 if (loop = NULL ){
12 throw new illegalstateexception (
13 ......
14}

An error may occur in the following aspects:

  • Network ProblemsThe database connection is interrupted due to network interruption. However, this problem occurs regularly, and the actual situation may be very small. Therefore, it is preliminarily ruled out;
  • Database Connection timeoutIf the database connection time is not set enough, the connection will be canceled. If the database connection timeout is set later, this problem is not caused and can be ruled out;
  • Error Using LRU AlgorithmLRU is the least recently used algorithm of least recently used. It is mainly used to prevent memory overflow caused by excessive memory usage. in programming, LRU is mainly used to store discarded objects. Whether the udl stores the completed state of the job. It is possible that the objects in the stored state are squeezed out of the memory, which cannot be found when the status is obtained. This issue is highly likely and will be the focus of subsequent analysis;
  • Thread synchronization problemsSince the LRU Algorithm in Apache is non-thread-safe, the LRU Object Storage reaches the maximum number of calls to the LRU algorithm reuse memory space. Because of the non-thread security, errors may occur when two processes simultaneously operate on one memory area, very likely;

For more information, see more than 100 jobs defined at the customer's site. Set the maximum number of LRU jobs to 150, so that the maximum number of jobs set by LRU is reached after a period of operation, in this case, the LRU algorithm is called to clean up and replace the memory. The above problem occurs.

3. Problem Solving
Through problem analysis, we roughly understand that the LRU algorithm and thread synchronization are the solutions to the problem, and propose two solutions:

  • Manually manage the job lifecycle without using the LRU Algorithm
  • Add the code for thread synchronization to the code

Compared with the two solutions, the first solution is more thorough, but the change is large. The second solution is relatively simple, so the second solution is adopted first, and the red part is modified.

 

1 public static void setjobover (string jobid, string date,
2 Boolean istaskjobover ){
3
4 synchronized (isjobovermap ){
5 isjobovermap. Put (New jobkey (jobid, date), Boolean. valueof (istaskjobover ));
6}
7}

The problem is solved through the above modification.

 

4. Note that
Through the above problems and code analysis, a hidden bug is solved. The following experiences can be used for reference:

  • Exercise caution when using the LRU algorithm. It is best to store and use it to discard objects. It is best not to use it to store objects to be maintained, and there is a risk that sleep objects need to be cleared;
  • Pay special attention to thread security issues and need to be synchronized.

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.