use of java.util.concurrent.SynchronousQueue in project polymap4-core by Polymap4.
the class UnboundPoolExecutor method newInstance.
public static ExecutorService newInstance() {
// thread factory
ThreadFactory threadFactory = new ThreadFactory() {
volatile int threadNumber = 0;
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "polymap-worker-" + threadNumber++);
t.setDaemon(false);
t.setPriority(DEFAULT_THREAD_PRIORITY);
return t;
}
};
// thread pool
final int procs = Runtime.getRuntime().availableProcessors();
final int maxThreads = procs * MAX_THREADS_PER_PROC;
ThreadPoolExecutor executor = new ThreadPoolExecutor(procs, maxThreads, // 180L, TimeUnit.SECONDS, new LinkedBlockingQueue( procs ), threadFactory );
180L, TimeUnit.SECONDS, new SynchronousQueue(), threadFactory);
executor.allowCoreThreadTimeOut(false);
// rejected? -> wait and try again
executor.setRejectedExecutionHandler(new RejectedExecutionHandlers.Blocking());
return executor;
}
use of java.util.concurrent.SynchronousQueue in project Orekit by CS-SI.
the class PropagatorsParallelizer method propagate.
/**
* Propagate from a start date towards a target date.
* @param start start date from which orbit state should be propagated
* @param target target date to which orbit state should be propagated
* @return propagated states
* @exception OrekitException if state cannot be propagated
*/
public List<SpacecraftState> propagate(final AbsoluteDate start, final AbsoluteDate target) throws OrekitException {
if (propagators.size() == 1) {
// special handling when only one propagator is used
propagators.get(0).setMasterMode(new SinglePropagatorHandler(globalHandler));
return Collections.singletonList(propagators.get(0).propagate(start, target));
}
final double sign = FastMath.copySign(1.0, target.durationFrom(start));
final int n = propagators.size();
// set up queues for propagators synchronization
// the main thread will let underlying propagators go forward
// by consuming the step handling parameters they will put at each step
final List<SynchronousQueue<SpacecraftState>> initQueues = new ArrayList<>(n);
final List<SynchronousQueue<StepHandlingParameters>> shpQueues = new ArrayList<>(n);
for (final Propagator propagator : propagators) {
final SynchronousQueue<SpacecraftState> initQueue = new SynchronousQueue<>();
initQueues.add(initQueue);
final SynchronousQueue<StepHandlingParameters> shpQueue = new SynchronousQueue<>();
shpQueues.add(shpQueue);
propagator.setMasterMode(new MultiplePropagatorsHandler(initQueue, shpQueue));
}
// concurrently run all propagators
final ExecutorService executorService = Executors.newFixedThreadPool(n);
final List<Future<SpacecraftState>> futures = new ArrayList<>(n);
final List<SpacecraftState> initialStates = new ArrayList<>(n);
final List<StepHandlingParameters> stepHandlingParameters = new ArrayList<>(n);
final List<OrekitStepInterpolator> restricted = new ArrayList<>(n);
final List<SpacecraftState> finalStates = new ArrayList<>(n);
for (int i = 0; i < n; ++i) {
final Propagator propagator = propagators.get(i);
final Future<SpacecraftState> future = executorService.submit(() -> propagator.propagate(start, target));
futures.add(future);
initialStates.add(getParameters(i, future, initQueues.get(i)));
stepHandlingParameters.add(getParameters(i, future, shpQueues.get(i)));
restricted.add(null);
finalStates.add(null);
}
// main loop
AbsoluteDate previousDate = start;
globalHandler.init(initialStates, target);
for (boolean isLast = false; !isLast; ) {
// select the earliest ending propagator, according to propagation direction
int selected = -1;
AbsoluteDate selectedStepEnd = null;
for (int i = 0; i < n; ++i) {
final AbsoluteDate stepEnd = stepHandlingParameters.get(i).getDate();
if (selected < 0 || sign * selectedStepEnd.durationFrom(stepEnd) > 0) {
selected = i;
selectedStepEnd = stepEnd;
}
}
// restrict steps to a common time range
for (int i = 0; i < n; ++i) {
final OrekitStepInterpolator interpolator = stepHandlingParameters.get(i).interpolator;
final SpacecraftState previousState = interpolator.getInterpolatedState(previousDate);
final SpacecraftState currentState = interpolator.getInterpolatedState(selectedStepEnd);
restricted.set(i, interpolator.restrictStep(previousState, currentState));
}
// will this be the last step?
isLast = stepHandlingParameters.get(selected).isLast;
// handle all states at once
globalHandler.handleStep(restricted, isLast);
if (!isLast) {
// advance one step
stepHandlingParameters.set(selected, getParameters(selected, futures.get(selected), shpQueues.get(selected)));
}
previousDate = selectedStepEnd;
}
// stop all remaining propagators
executorService.shutdownNow();
// extract the final states
for (int i = 0; i < n; ++i) {
try {
finalStates.set(i, futures.get(i).get());
} catch (InterruptedException | ExecutionException e) {
// sort out if exception was intentional or not
manageException(e);
// this propagator was intentionally stopped,
// we retrieve the final state from the last available interpolator
finalStates.set(i, stepHandlingParameters.get(i).interpolator.getInterpolatedState(previousDate));
}
}
return finalStates;
}
use of java.util.concurrent.SynchronousQueue in project drill by apache.
the class DrillClient method connect.
/**
* Start's a connection from client to server
* @param connect - Zookeeper connection string provided at connection URL
* @param props - not null {@link Properties} filled with connection url parameters
* @throws RpcException
*/
public synchronized void connect(String connect, Properties props) throws RpcException {
if (connected) {
return;
}
properties = DrillProperties.createFromProperties(props);
final List<DrillbitEndpoint> endpoints = new ArrayList<>();
if (isDirectConnection) {
// Populate the endpoints list with all the drillbit information provided in the connection string
endpoints.addAll(parseAndVerifyEndpoints(properties.getProperty(DrillProperties.DRILLBIT_CONNECTION), config.getString(ExecConstants.INITIAL_USER_PORT)));
} else {
if (ownsZkConnection) {
try {
this.clusterCoordinator = new ZKClusterCoordinator(this.config, connect);
this.clusterCoordinator.start(10000);
} catch (Exception e) {
throw new RpcException("Failure setting up ZK for client.", e);
}
}
// Gets the drillbit endpoints that are ONLINE and excludes the drillbits that are
// in QUIESCENT state. This avoids the clients connecting to drillbits that are
// shutting down thereby avoiding reducing the chances of query failures.
endpoints.addAll(clusterCoordinator.getOnlineEndPoints());
// Make sure we have at least one endpoint in the list
checkState(!endpoints.isEmpty(), "No active Drillbit endpoint found from ZooKeeper. Check connection parameters?");
}
// shuffle the collection then get the first endpoint
Collections.shuffle(endpoints);
eventLoopGroup = createEventLoop(config.getInt(ExecConstants.CLIENT_RPC_THREADS), "Client-");
executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new NamedThreadFactory("drill-client-executor-")) {
@Override
protected void afterExecute(final Runnable r, final Throwable t) {
if (t != null) {
logger.error("{}.run() leaked an exception.", r.getClass().getName(), t);
}
super.afterExecute(r, t);
}
};
final String connectTriesConf = properties.getProperty(DrillProperties.TRIES, "5");
int connectTriesVal;
try {
connectTriesVal = Math.min(endpoints.size(), Integer.parseInt(connectTriesConf));
} catch (NumberFormatException e) {
throw new InvalidConnectionInfoException("Invalid tries value: " + connectTriesConf + " specified in " + "connection string");
}
// If the value provided in the connection string is <=0 then override with 1 since we want to try connecting
// at least once
connectTriesVal = Math.max(1, connectTriesVal);
int triedEndpointIndex = 0;
DrillbitEndpoint endpoint;
while (triedEndpointIndex < connectTriesVal) {
endpoint = endpoints.get(triedEndpointIndex);
// version
if (!properties.containsKey(DrillProperties.SERVICE_HOST)) {
properties.setProperty(DrillProperties.SERVICE_HOST, endpoint.getAddress());
props.setProperty(DrillProperties.SERVICE_HOST, endpoint.getAddress());
}
// Note: the properties member is a DrillProperties instance which lower cases names of
// properties. That does not work too well with properties that are mixed case.
// For user client severla properties are mixed case so we do not use the properties member
// but instead pass the props parameter.
client = new UserClient(clientName, config, props, supportComplexTypes, allocator, eventLoopGroup, executor, endpoint);
logger.debug("Connecting to server {}:{}", endpoint.getAddress(), endpoint.getUserPort());
try {
connect(endpoint);
connected = true;
logger.info("Successfully connected to server {}:{}", endpoint.getAddress(), endpoint.getUserPort());
break;
} catch (NonTransientRpcException ex) {
logger.error("Connection to {}:{} failed with error {}. Not retrying anymore", endpoint.getAddress(), endpoint.getUserPort(), ex.getMessage());
throw ex;
} catch (RpcException ex) {
++triedEndpointIndex;
logger.error("Attempt {}: Failed to connect to server {}:{}", triedEndpointIndex, endpoint.getAddress(), endpoint.getUserPort());
// Throw exception when we have exhausted all the tries without having a successful connection
if (triedEndpointIndex == connectTriesVal) {
throw ex;
}
// Close the connection here to avoid calling close twice in case when all tries are exhausted.
// Since DrillClient.close is also calling client.close
client.close();
}
}
}
use of java.util.concurrent.SynchronousQueue in project eureka by Netflix.
the class TimedSupervisorTaskTest method setUp.
@Before
public void setUp() {
scheduler = Executors.newScheduledThreadPool(4, new ThreadFactoryBuilder().setNameFormat("DiscoveryClient-%d").setDaemon(true).build());
helperExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
executor = new ThreadPoolExecutor(// corePoolSize
1, // maxPoolSize
3, // keepAliveTime
0, TimeUnit.SECONDS, // use direct handoff
new SynchronousQueue<Runnable>());
testTaskStartCounter = new AtomicLong(0);
testTaskSuccessfulCounter = new AtomicLong(0);
testTaskInterruptedCounter = new AtomicLong(0);
maxConcurrentTestTasks = new AtomicLong(0);
testTaskCounter = new AtomicLong(0);
}
use of java.util.concurrent.SynchronousQueue in project storm by apache.
the class DRPCSpout method open.
@Override
public void open(Map<String, Object> conf, TopologyContext context, SpoutOutputCollector collector) {
this.collector = collector;
if (localDrpcId == null) {
background = new ExtendedThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
futuresMap = new HashMap<>();
int numTasks = context.getComponentTasks(context.getThisComponentId()).size();
int index = context.getThisTaskIndex();
int port = ObjectReader.getInt(conf.get(Config.DRPC_INVOCATIONS_PORT));
List<String> servers = (List<String>) conf.get(Config.DRPC_SERVERS);
if (servers == null || servers.isEmpty()) {
throw new RuntimeException("No DRPC servers configured for topology");
}
List<DRPCClientBuilder> clientBuilders = new ArrayList<>();
if (numTasks < servers.size()) {
for (String s : servers) {
clientBuilders.add(new DRPCClientBuilder(s, port, conf));
}
} else {
int i = index % servers.size();
clientBuilders.add(new DRPCClientBuilder(servers.get(i), port, conf));
}
establishConnections(clientBuilders);
}
}
Aggregations