Skip to content
Browse files

Modified the code to now stop the workload which is then used by the

terminator thread. Using thread interrupt as in the previous version
had issues due to interrupts being lost.
  • Loading branch information...
1 parent 23eee39 commit 0580776b64f6ba9932f113ffa88c0de2a1a79e5b @sudiptodas committed
Showing with 45 additions and 21 deletions.
  1. +6 −12 src/com/yahoo/ycsb/Client.java
  2. +20 −9 src/com/yahoo/ycsb/TerminatorThread.java
  3. +19 −0 src/com/yahoo/ycsb/Workload.java
View
18 src/com/yahoo/ycsb/Client.java
@@ -220,9 +220,7 @@ public void run()
}
catch (InterruptedException e)
{
- // Changed from doing nothing to returning to support interruption
- // of the client threads.
- return;
+ // do nothing.
}
try
@@ -231,7 +229,7 @@ public void run()
{
long st=System.currentTimeMillis();
- while ( (_opcount==0) || (_opsdone<_opcount) )
+ while (((_opcount == 0) || (_opsdone < _opcount)) && !_workload.isStopRequested())
{
if (!_workload.doTransaction(_db,_workloadstate))
@@ -256,9 +254,7 @@ public void run()
}
catch (InterruptedException e)
{
- // Changed from doing nothing to returning to support interruption
- // of the client threads.
- return;
+ // do nothing.
}
}
@@ -269,7 +265,7 @@ public void run()
{
long st=System.currentTimeMillis();
- while ( (_opcount==0) || (_opsdone<_opcount) )
+ while (((_opcount == 0) || (_opsdone < _opcount)) && !_workload.isStopRequested())
{
if (!_workload.doInsert(_db,_workloadstate))
@@ -294,9 +290,7 @@ public void run()
}
catch (InterruptedException e)
{
- // Changed from doing nothing to returning to support interruption
- // of the client threads.
- return;
+ // do nothing.
}
}
}
@@ -755,7 +749,7 @@ public void run()
Thread terminator = null;
if (maxExecutionTime > 0) {
- terminator = new TerminatorThread(maxExecutionTime, threads);
+ terminator = new TerminatorThread(maxExecutionTime, threads, workload);
terminator.start();
}
View
29 src/com/yahoo/ycsb/TerminatorThread.java
@@ -31,10 +31,15 @@
private Vector<Thread> threads;
private long maxExecutionTime;
+ private Workload workload;
+ private long waitTimeOutInMS;
- public TerminatorThread(long maxExecutionTime, Vector<Thread> threads) {
+ public TerminatorThread(long maxExecutionTime, Vector<Thread> threads,
+ Workload workload) {
this.maxExecutionTime = maxExecutionTime;
this.threads = threads;
+ this.workload = workload;
+ waitTimeOutInMS = 2000;
System.err.println("Maximum execution time specified as: " + maxExecutionTime + " secs");
}
@@ -42,17 +47,23 @@ public void run() {
try {
Thread.sleep(maxExecutionTime * 1000);
} catch (InterruptedException e) {
- System.err.println("Could not wait until max specified time, thread interrupted.");
+ System.err.println("Could not wait until max specified time, TerminatorThread interrupted.");
return;
}
- System.err.println("Maximum time elapsed. Interrupting the benchmark threads.");
+ System.err.println("Maximum time elapsed. Requesting stop for the workload.");
+ workload.requestStop();
+ System.err.println("Stop requested for workload. Now Joining!");
for (Thread t : threads) {
- if (!t.isInterrupted()) {
- t.interrupt();
- }
- try {
- t.join();
- } catch (InterruptedException e) {
+ while (t.isAlive()) {
+ try {
+ t.join(waitTimeOutInMS);
+ if (t.isAlive()) {
+ System.err.println("Still waiting for thread " + t.getName() + " to complete. " +
+ "Workload status: " + workload.isStopRequested());
+ }
+ } catch (InterruptedException e) {
+ // Do nothing. Don't know why I was interrupted.
+ }
}
}
}
View
19 src/com/yahoo/ycsb/Workload.java
@@ -18,6 +18,7 @@
package com.yahoo.ycsb;
import java.util.Properties;
+import java.util.concurrent.atomic.AtomicBoolean;
/**
* One experiment scenario. One object of this type will
@@ -39,6 +40,8 @@
public static final String INSERT_START_PROPERTY_DEFAULT="0";
+ private volatile AtomicBoolean stopRequested = new AtomicBoolean(false);
+
/**
* Initialize the scenario. Create any generators and other shared objects here.
* Called once, in the main client thread, before any operations are started.
@@ -90,4 +93,20 @@ public void cleanup() throws WorkloadException
* @return false if the workload knows it is done for this thread. Client will terminate the thread. Return true otherwise. Return true for workloads that rely on operationcount. For workloads that read traces from a file, return true when there are more to do, false when you are done.
*/
public abstract boolean doTransaction(DB db, Object threadstate);
+
+ /**
+ * Allows scheduling a request to stop the workload.
+ */
+ public void requestStop() {
+ stopRequested.set(true);
+ }
+
+ /**
+ * Check the status of the stop request flag.
+ * @return true if stop was requested, false otherwise.
+ */
+ public boolean isStopRequested() {
+ if (stopRequested.get() == true) return true;
+ else return false;
+ }
}

0 comments on commit 0580776

Please sign in to comment.
Something went wrong with that request. Please try again.