View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21  
22  package org.miloss.fgsms.dependency;
23  
24  import java.sql.Connection;
25  import java.sql.PreparedStatement;
26  import java.sql.ResultSet;
27  import java.sql.SQLException;
28  
29  import org.miloss.fgsms.common.Utility;
30  import org.apache.log4j.Level;
31  import org.miloss.fgsms.common.Logger;;
32  import org.apache.log4j.PropertyConfigurator;
33  import org.miloss.fgsms.common.DBUtils;
34  
35  /**
36   * yet another quartz job that scans through the database looking for
37   * correlations/transaction threads with more than 2 transactions associated
38   * with it
39   *
40   * @author AO
41   */
42  public class DependencyScanner {
43  
44      static final Logger log = Logger.getLogger("fgsms.DependencyScanner");
45  
46      public static void main(String args[]) {
47            PropertyConfigurator.configure("log4j.properties");
48          new DependencyScanner().go(false, 0);
49      }
50  
51      public void go(boolean pooled, long timestampAtLastRunTime) {
52          //loop through the last 10 minutes worth of traffic, looking for dependencies
53          Connection con = null;
54          if (pooled) {
55              con = Utility.getPerformanceDBConnection();
56          } else {
57              con = Utility.getPerformanceDB_NONPOOLED_Connection();
58          }
59          long timer = System.currentTimeMillis();
60          long threadsSearched=0;
61          long dependenciesFound=0;
62          PreparedStatement cmd=null;
63          PreparedStatement c2=null;
64          ResultSet rs=null;
65          ResultSet r2=null;
66          try {
67              cmd = con.prepareStatement("select * from (select  threadid,count(threadid) as t from rawdata where utcdatetime >= ? group by threadid ) as foo where foo.t > 3;");
68              cmd.setLong(1, timestampAtLastRunTime - (30 * 1000));
69               rs = cmd.executeQuery();
70              while (rs.next()) {
71                  threadsSearched++;
72                  try{
73                  c2 = con.prepareCall("select url,monitorsource,soapaction  from rawdata where threadid=? order by utcdatetime asc;");
74                  c2.setString(1, rs.getString("threadid"));
75                  //first hit is the initial request
76                  r2 = c2.executeQuery();
77                  record first = null;
78                  record second = null;
79                  while (r2.next()) {
80                      if (first == null) {
81                          //first run through this thread
82                          first = new record();
83                          first.url = r2.getString("url");
84                          first.action = rs.getString("soapaction");
85                          first.hostname = rs.getString("monitorsource");
86                          if (r2.next()) {
87                              if (second==null)
88                                  second=new record();
89                              second.url = r2.getString("url");
90                              second.action = rs.getString("soapaction");
91                              second.hostname = rs.getString("monitorsource");
92                          }
93                      } else {
94                          if (second==null)
95                                  second=new record();
96                          second.url = r2.getString("url");
97                          second.action = rs.getString("soapaction");
98                          second.hostname = rs.getString("monitorsource");
99                      }
100 
101 
102 
103                     //do some work
104                     if (first == null || second == null) {
105                         continue;
106                     }
107                     if ((!first.url.equalsIgnoreCase(second.url) || !(first.action.equalsIgnoreCase(second.action))) && first.hostname.equalsIgnoreCase(second.hostname)) {
108                         dependenciesFound++;
109                         RecordDependency(first.url, first.action, second.url, second.action, con);
110                     }
111 
112                     //setup for the next iteration
113                     first = second;
114                     second = null;
115                 }
116                 }catch (Exception ex){
117                     log.log(Level.ERROR, null, ex);
118                 } finally {
119                     DBUtils.safeClose(r2);
120                     DBUtils.safeClose(c2);
121                 }
122 
123             }
124         } catch (Exception ex) {
125             log.log(Level.ERROR, null, ex);
126         }
127         finally{
128             DBUtils.safeClose(rs);
129             DBUtils.safeClose(cmd);
130             DBUtils.safeClose(con);
131         }
132         
133         log.log(Level.INFO, "Web Service Threads Searched: " + threadsSearched + " Dependencies Found: " + dependenciesFound + " took " + (System.currentTimeMillis()-timer) + " ms to run");
134     }
135 
136     private void RecordDependency(String url, String action, String url0, String action0, Connection con) {
137         PreparedStatement cmd=null;
138         try {
139             cmd = con.prepareStatement("INSERT INTO dependencies(sourceurl, sourcesoapaction, destintationurl, destinationsoapaction)    VALUES (?, ?, ?, ?);");
140             cmd.setString(1, url);
141             cmd.setString(2, action);
142             cmd.setString(3, url0);
143             cmd.setString(4, action0);
144             cmd.executeUpdate();
145         } catch (Exception ex) {
146             log.log(Level.ERROR, null, ex);
147         } finally {
148             DBUtils.safeClose(cmd);
149         }
150     }
151 
152     /**
153      * internal class used for temporary record storage
154      */
155     class record {
156 
157         public String url;
158         public String action;
159         public String hostname;
160     }
161 }
162 /*
163  * from notes
164  * select * from rawdata
165  *       group by theadid
166  *      order by timestamp asc
167  * for each record pair
168  *      if threadid matches and 
169  *  (url is different or action is different ) && (hostname matches)
170  *      record dependency
171  */