KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > slamd > example > LatencyCheckMasterThread


1 /*
2  * Sun Public License
3  *
4  * The contents of this file are subject to the Sun Public License Version
5  * 1.0 (the "License"). You may not use this file except in compliance with
6  * the License. A copy of the License is available at http://www.sun.com/
7  *
8  * The Original Code is the SLAMD Distributed Load Generation Engine.
9  * The Initial Developer of the Original Code is Neil A. Wilson.
10  * Portions created by Neil A. Wilson are Copyright (C) 2004.
11  * Some preexisting portions Copyright (C) 2002-2004 Sun Microsystems, Inc.
12  * All Rights Reserved.
13  *
14  * Contributor(s): Neil A. Wilson
15  */

16 package com.sun.slamd.example;
17
18
19
20 import netscape.ldap.*;
21
22
23
24 /**
25  * This class defines a thread that will periodically make changes to an LDAP
26  * directory server so that another thread can watch for those changes to appear
27  * on a replica. This makes it possible to measure the replication latency
28  * between the master and the replica.
29  *
30  *
31  * @author Neil A. Wilson
32  */

33 public class LatencyCheckMasterThread
34        extends Thread JavaDoc
35 {
36   // Indicates whether this thread should be actively making changes to the
37
// master server.
38
boolean active;
39
40   // Indicates whether this thread has stopped running.
41
boolean stopCompleted;
42
43   // Indicates whether a request has been made to stop this thread.
44
boolean stopRequested;
45
46   // The minimum length of time that should pass between modifications.
47
int latencyDelay;
48
49   // The reference to the thread itself (to use to interrupt it if necessary).
50
LatencyCheckMasterThread masterThread;
51
52   // The connection to the master directory server.
53
LDAPConnection connection;
54
55   // The time that the last modification was performed.
56
long lastModTime;
57
58   // The job thread with which this check thread is associated.
59
ReplicaLatencyCheckJobClass jobThread;
60
61   // The name of the attribute to modify.
62
String JavaDoc attributeName;
63
64   // The DN of the entry to be modified.
65
String JavaDoc entryDN;
66
67
68   /**
69    * Creates a new latency check master thread based on the provided
70    * information.
71    *
72    * @param jobThread The job thread with which this check thread is
73    * associated.
74    * @param masterHost The address of the directory server.
75    * @param masterPort The port of the directory server.
76    * @param bindDN The DN to use to bind to the directory.
77    * @param bindPW The password to use to bind to the directory.
78    * @param entryDN The DN of the entry to be modified.
79    * @param attributeName The name of the attribute to modify.
80    * @param latencyDelay The minimum length of time that should pass between
81    * modifications.
82    *
83    * @throws LDAPException If a problem occurs while establishing the
84    * connection to the directory server.
85    */

86   public LatencyCheckMasterThread(ReplicaLatencyCheckJobClass jobThread,
87                                   String JavaDoc masterHost, int masterPort,
88                                   String JavaDoc bindDN, String JavaDoc bindPW, String JavaDoc entryDN,
89                                   String JavaDoc attributeName, int latencyDelay)
90          throws LDAPException
91   {
92     setName("Latency Check Master Thread");
93
94     // Initialize the instance variables.
95
this.jobThread = jobThread;
96     this.entryDN = entryDN;
97     this.latencyDelay = latencyDelay;
98     this.attributeName = attributeName;
99     stopCompleted = false;
100     stopRequested = false;
101     lastModTime = 0;
102     masterThread = null;
103
104     // Establish the connection to the master server.
105
connection = new LDAPConnection();
106     connection.connect(3, masterHost, masterPort, bindDN, bindPW);
107   }
108
109
110
111   /**
112    * Indicates that this thread should start performing modifications against
113    * the directory server.
114    */

115   public void startChecking()
116   {
117     active = true;
118   }
119
120
121
122   /**
123    * Periodically makes changes to the specified entry in the directory.
124    */

125   public void run()
126   {
127     masterThread = this;
128
129     // First, wait until the thread is actually activated.
130
while ((! active) && (! stopRequested))
131     {
132       try
133       {
134         Thread.sleep(10);
135       } catch (InterruptedException JavaDoc ie) {}
136     }
137
138     // Loop until the thread has been requested to stop.
139
while (! stopRequested)
140     {
141       // Make the update to the directory server.
142
LDAPAttribute attr = new LDAPAttribute(attributeName,
143                                              String.valueOf(lastModTime));
144       LDAPModification mod =
145            new LDAPModification(LDAPModification.REPLACE, attr);
146
147       try
148       {
149         synchronized (jobThread.latencyCheckMutex)
150         {
151           connection.modify(entryDN, mod);
152           jobThread.latencyTime.startTimer();
153           lastModTime = System.currentTimeMillis();
154
155           try
156           {
157             jobThread.latencyCheckMutex.wait();
158             long modStopTime = System.currentTimeMillis();
159             int latencySeconds = ((int) (modStopTime - lastModTime)) / 1000;
160             jobThread.latencyCategories.increment(latencySeconds + "-" +
161                                                   (latencySeconds+1) + " s");
162           } catch (InterruptedException JavaDoc ie) {}
163         }
164       }
165       catch (LDAPException le)
166       {
167         jobThread.writeVerbose("Unable to modify replica entry " + entryDN +
168                                ": " + le);
169         break;
170       }
171
172       // Sleep if necessary before making the next change.
173
if (! stopRequested)
174       {
175         long now = System.currentTimeMillis();
176         long sleepTime = latencyDelay - (now - lastModTime);
177         if (sleepTime > 0)
178         {
179           try
180           {
181             Thread.sleep(sleepTime);
182           }
183           catch (InterruptedException JavaDoc ie) {}
184         }
185       }
186     }
187
188     // Indicate that this thread is no longer running.
189
stopCompleted = true;
190     masterThread = null;
191   }
192
193
194
195   /**
196    * Indicates that the check thread should stop running and waits for it to do
197    * so.
198    */

199   public void stopAndWait()
200   {
201     if (stopCompleted)
202     {
203       return;
204     }
205
206     stopRequested = true;
207
208     try
209     {
210       if (masterThread != null)
211       {
212         masterThread.interrupt();
213       }
214     } catch (Exception JavaDoc e) {}
215
216     while (! stopCompleted)
217     {
218       try
219       {
220         Thread.sleep(10);
221       } catch (InterruptedException JavaDoc ie) {}
222     }
223
224     try
225     {
226       connection.disconnect();
227     } catch (LDAPException le) {}
228   }
229 }
230
231
Popular Tags