Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threads must wait to be ready #10

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -408,7 +409,9 @@ public void run( int transactionNumber )
{
HttpClientTransport httpClientTransport = getHttpClientTransport();

List<Future<?>> futures = new ArrayList<Future<?>>( getUsers() );


List<Callable<Void>> callables = new ArrayList<>( getUsers() );

for ( int i = getUsers(); i > 0; i-- )
{
Expand All @@ -427,7 +430,7 @@ public void run( int transactionNumber )
new LoadGeneratorRunner( httpClient, this, _loadGeneratorResultHandler, //
transactionNumber);

futures.add( this.runnersExecutorService.submit( loadGeneratorRunner ));
callables.add( loadGeneratorRunner );

}
catch ( Throwable e )
Expand All @@ -437,6 +440,8 @@ public void run( int transactionNumber )
}
}

List<Future<Void>> futures = this.runnersExecutorService.invokeAll( callables );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not good, invokeAll() blocks until all tasks are complete.


while ( !LoadGenerator.this.stop.get() && !futures.stream().allMatch( future -> future.isDone() ))
{
// wait until stopped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@
import org.mortbay.jetty.load.generator.profile.Resource;

import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
*
*/
public class LoadGeneratorRunner
implements Runnable
implements Callable<Void>
{

private static final Logger LOGGER = Log.getLogger( LoadGeneratorRunner.class );
Expand Down Expand Up @@ -67,8 +70,9 @@ public LoadGeneratorRunner( HttpClient httpClient, LoadGenerator loadGenerator,
this.transactionNumber = transactionNumber;
}


@Override
public void run()
public Void call()
{
LOGGER.debug( "loadGenerator#run" );
try
Expand Down Expand Up @@ -118,6 +122,7 @@ public void run()
LOGGER.warn( "ignoring exception:" + e.getMessage(), e );
// TODO record error in generator report
}
return null;
}

private void handleResource( Resource resource )
Expand All @@ -139,25 +144,28 @@ private void handleResource( Resource resource )
// it's a group so we can request in parallel but wait all responses before next step
ExecutorService executorService = Executors.newWorkStealingPool();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we want to create a new ExecutorService here ? Can't we just use the existing one ?


List<Callable<Void>> callables = new ArrayList<>( resource.getResources().size() );


for ( Resource children : resource.getResources() )
{
executorService.execute( () ->
{
try
{
handleResource( children );
}
catch ( Exception e )
{
LOGGER.debug( e.getMessage(), e );
}
} );
callables.add( () ->
{
try
{
handleResource( children );
}
catch ( Exception e )
{
LOGGER.debug( e.getMessage(), e );
}
return null;
} );
}

executorService.shutdown();
List<Future<Void>> futures = executorService.invokeAll( callables, resource.getChildrenTimeout(), TimeUnit.MILLISECONDS );
boolean finished = futures.stream().allMatch( voidFuture -> voidFuture.isDone() );

// TODO make this configurable??
boolean finished = executorService.awaitTermination( resource.getChildrenTimeout(), TimeUnit.MILLISECONDS );
if ( !finished )
{
LOGGER.warn( "resourceGroup request not all completed for timeout " + resource.getChildrenTimeout() );
Expand Down