List of usage examples for java.util.concurrent ExecutorCompletionService poll
public Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException
From source file:org.apache.solr.client.solrj.impl.BackupRequestLBHttpSolrServer.java
private RequestTaskState getResponseIfReady(ExecutorCompletionService<RequestTaskState> executer, boolean waitUntilTaskComplete) throws SolrException { Future<RequestTaskState> taskInProgress = null; try {/*from ww w . j a v a 2s.c o m*/ if (waitUntilTaskComplete) { taskInProgress = executer.take(); } else { taskInProgress = executer.poll(backUpRequestDelay, TimeUnit.MILLISECONDS); } // could be null if poll time exceeded in which case return null. if (taskInProgress != null && !taskInProgress.isCancelled()) { RequestTaskState resp = taskInProgress.get(); return resp; } } catch (InterruptedException e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } catch (ExecutionException e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } return null; }
From source file:org.apache.solr.client.solrj.impl.BackupRequestLBHttpSolrClient.java
private RequestTaskState getResponseIfReady(ExecutorCompletionService<RequestTaskState> executer, int backupDelay) throws SolrException { Future<RequestTaskState> taskInProgress = null; try {//from www . j av a2 s . c o m if (backupDelay < 0) { taskInProgress = executer.take(); } else { taskInProgress = executer.poll(backupDelay, TimeUnit.MILLISECONDS); } // could be null if poll time exceeded in which case return null. if (taskInProgress != null && !taskInProgress.isCancelled()) { RequestTaskState resp = taskInProgress.get(); return resp; } } catch (InterruptedException e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } catch (ExecutionException e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } return null; }
From source file:com.liferay.sync.engine.lan.session.LanSession.java
protected SyncLanClientQueryResult findSyncLanClient(SyncFile syncFile) throws Exception { SyncAccount syncAccount = SyncAccountService.fetchSyncAccount(syncFile.getSyncAccountId()); List<String> syncLanClientUuids = SyncLanEndpointService .findSyncLanClientUuids(syncAccount.getLanServerUuid(), syncFile.getRepositoryId()); if (syncLanClientUuids.isEmpty()) { return null; }/*w w w . ja v a2 s .com*/ final List<Callable<SyncLanClientQueryResult>> syncLanClientQueryResultCallables = Collections .synchronizedList(new ArrayList<Callable<SyncLanClientQueryResult>>(syncLanClientUuids.size())); for (String syncLanClientUuid : syncLanClientUuids) { SyncLanClient syncLanClient = SyncLanClientService.fetchSyncLanClient(syncLanClientUuid); syncLanClientQueryResultCallables.add(createSyncLanClientQueryResultCallable(syncLanClient, syncFile)); } int queryPoolSize = Math.min(syncLanClientUuids.size(), PropsValues.SYNC_LAN_SESSION_QUERY_POOL_MAX_SIZE); List<Future<SyncLanClientQueryResult>> pendingSyncLanClientQueryResults = new ArrayList<>(queryPoolSize); ExecutorCompletionService<SyncLanClientQueryResult> executorCompletionService = new ExecutorCompletionService<>( getExecutorService()); for (int i = 0; i < queryPoolSize; i++) { Callable<SyncLanClientQueryResult> callable = new Callable<SyncLanClientQueryResult>() { @Override public synchronized SyncLanClientQueryResult call() throws Exception { if (syncLanClientQueryResultCallables.isEmpty()) { return null; } Callable<SyncLanClientQueryResult> syncLanClientQueryResultCallable = syncLanClientQueryResultCallables .remove(0); try { return syncLanClientQueryResultCallable.call(); } catch (Exception e) { return call(); } } }; pendingSyncLanClientQueryResults.add(executorCompletionService.submit(callable)); } List<Future<SyncLanClientQueryResult>> completedSyncLanClientQueryResult = new ArrayList<>(queryPoolSize); long timeout = PropsValues.SYNC_LAN_SESSION_QUERY_TOTAL_TIMEOUT; long endTime = System.currentTimeMillis() + timeout; for (int i = 0; i < queryPoolSize; i++) { Future<SyncLanClientQueryResult> future = executorCompletionService.poll(timeout, TimeUnit.MILLISECONDS); if (future == null) { for (Future<SyncLanClientQueryResult> pendingSyncLanClientQueryResult : pendingSyncLanClientQueryResults) { if (!pendingSyncLanClientQueryResult.isDone()) { pendingSyncLanClientQueryResult.cancel(true); } } break; } completedSyncLanClientQueryResult.add(future); timeout = endTime - System.currentTimeMillis(); } SyncLanClientQueryResult candidateSyncLanClientQueryResult = null; int candidateDownloadRatePerConnection = 0; for (Future<SyncLanClientQueryResult> completedFuture : completedSyncLanClientQueryResult) { SyncLanClientQueryResult syncLanClientQueryResult = null; try { syncLanClientQueryResult = completedFuture.get(); } catch (Exception e) { continue; } if (syncLanClientQueryResult == null) { continue; } if (syncLanClientQueryResult.getConnectionsCount() >= syncLanClientQueryResult.getMaxConnections()) { if (candidateSyncLanClientQueryResult == null) { candidateSyncLanClientQueryResult = syncLanClientQueryResult; } continue; } if (syncLanClientQueryResult.getConnectionsCount() == 0) { return syncLanClientQueryResult; } int downloadRatePerConnection = syncLanClientQueryResult.getDownloadRate() / (syncLanClientQueryResult.getConnectionsCount() + 1); if (downloadRatePerConnection >= candidateDownloadRatePerConnection) { candidateDownloadRatePerConnection = downloadRatePerConnection; candidateSyncLanClientQueryResult = syncLanClientQueryResult; } } return candidateSyncLanClientQueryResult; }
From source file:org.apache.hadoop.hbase.regionserver.HFileReadWriteTest.java
public boolean runRandomReadWorkload() throws IOException { if (inputFileNames.size() != 1) { throw new IOException("Need exactly one input file for random reads: " + inputFileNames); }// w ww .ja va2s . c om Path inputPath = new Path(inputFileNames.get(0)); // Make sure we are using caching. StoreFile storeFile = openStoreFile(inputPath, true); StoreFile.Reader reader = storeFile.createReader(); LOG.info("First key: " + Bytes.toStringBinary(reader.getFirstKey())); LOG.info("Last key: " + Bytes.toStringBinary(reader.getLastKey())); KeyValue firstKV = KeyValue.createKeyValueFromKey(reader.getFirstKey()); firstRow = firstKV.getRow(); KeyValue lastKV = KeyValue.createKeyValueFromKey(reader.getLastKey()); lastRow = lastKV.getRow(); byte[] family = firstKV.getFamily(); if (!Bytes.equals(family, lastKV.getFamily())) { LOG.error("First and last key have different families: " + Bytes.toStringBinary(family) + " and " + Bytes.toStringBinary(lastKV.getFamily())); return false; } if (Bytes.equals(firstRow, lastRow)) { LOG.error("First and last row are the same, cannot run read workload: " + "firstRow=" + Bytes.toStringBinary(firstRow) + ", " + "lastRow=" + Bytes.toStringBinary(lastRow)); return false; } ExecutorService exec = Executors.newFixedThreadPool(numReadThreads + 1); int numCompleted = 0; int numFailed = 0; try { ExecutorCompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(exec); endTime = System.currentTimeMillis() + 1000 * durationSec; boolean pread = true; for (int i = 0; i < numReadThreads; ++i) ecs.submit(new RandomReader(i, reader, pread)); ecs.submit(new StatisticsPrinter()); Future<Boolean> result; while (true) { try { result = ecs.poll(endTime + 1000 - System.currentTimeMillis(), TimeUnit.MILLISECONDS); if (result == null) break; try { if (result.get()) { ++numCompleted; } else { ++numFailed; } } catch (ExecutionException e) { LOG.error("Worker thread failure", e.getCause()); ++numFailed; } } catch (InterruptedException ex) { LOG.error("Interrupted after " + numCompleted + " workers completed"); Thread.currentThread().interrupt(); continue; } } } finally { storeFile.closeReader(true); exec.shutdown(); BlockCache c = cacheConf.getBlockCache(); if (c != null) { c.shutdown(); } } LOG.info("Worker threads completed: " + numCompleted); LOG.info("Worker threads failed: " + numFailed); return true; }
From source file:org.tallison.cc.WReGetter.java
private void execute(String[] args) throws IOException { if (args.length != 3) { usage();// w ww .jav a 2 s. c o m System.exit(1); } if (args[0].contains("-h")) { usage(); System.exit(0); } int numThreads = Integer.parseInt(args[0]); BufferedReader r = Files.newBufferedReader(Paths.get(args[1])); ArrayBlockingQueue<DigestURLPair> queue = new ArrayBlockingQueue<DigestURLPair>(1000); QueueFiller filler = new QueueFiller(r, queue, numThreads); new Thread(filler).start(); rootDir = Paths.get(args[2]); System.out.println("creating thread pool"); ExecutorService executorService = Executors.newFixedThreadPool(numThreads); ExecutorCompletionService<Integer> executorCompletionService = new ExecutorCompletionService<Integer>( executorService); System.out.println("about to start"); for (int i = 0; i < numThreads; i++) { System.out.println("submitted " + i); executorCompletionService.submit(new WGetter(queue)); } int completed = 0; while (completed < numThreads) { try { Future<Integer> future = executorCompletionService.poll(1, TimeUnit.SECONDS); if (future != null) { completed++; } } catch (InterruptedException e) { } } executorService.shutdown(); executorService.shutdownNow(); System.exit(0); }