com.twitter.util.Function0 Java Examples
The following examples show how to use
com.twitter.util.Function0.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 6 votes |
@Test public void testNoBackupFutureExecutedEarlyFailureForTimeBasedPolicy() { // First future fails soon enough. Future<Integer> originalFuture = getFuture(50, 1, false); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithSuccess(500, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX, BackupFutureRetryPolicy.TIMEOUT_RETRY_POLICY); Exception e = null; try { speculativeFuture.get(); } catch (RuntimeException re) { e = re; } assertNotNull(e); assertEquals(EXCEPTION_MSG, e.getMessage()); assertFalse(isBackupFutureInvoked); checkStats(0, 0, 0); }
Example #2
Source File: SafeQueueingFuturePool.java From distributedlog with Apache License 2.0 | 6 votes |
public synchronized Future<T> apply(final Function0<T> fn) { Preconditions.checkNotNull(fn); if (closed) { return Future.exception(new RejectedExecutionException("Operation submitted to closed SafeQueueingFuturePool")); } ++outstanding; queue.add(fn); Future<T> result = orderedFuturePool.apply(new Function0<T>() { @Override public T apply() { return queue.poll().apply(); } @Override public String toString() { return fn.toString(); } }).ensure(new Function0<BoxedUnit>() { public BoxedUnit apply() { if (decrOutstandingAndCheckDone()) { applyAll(); } return null; } }); return result; }
Example #3
Source File: TestSafeQueueingFuturePool.java From distributedlog with Apache License 2.0 | 6 votes |
@Test public void testFailedDueToClosed() throws Exception { TestFuturePool<Void> pool = new TestFuturePool<Void>(); pool.wrapper.close(); Future<Void> future = pool.wrapper.apply(new Function0<Void>() { public Void apply() { throw new RuntimeException("failed"); } }); try { Await.result(future); fail("should have thrown"); } catch (RejectedExecutionException ex) { } pool.shutdown(); }
Example #4
Source File: TestSafeQueueingFuturePool.java From distributedlog with Apache License 2.0 | 6 votes |
@Test public void testRejectedFailure() throws Exception { TestFuturePool<Void> pool = new TestFuturePool<Void>(); final AtomicBoolean result = new AtomicBoolean(false); pool.executor.shutdown(); final CountDownLatch latch = new CountDownLatch(1); Future<Void> future = pool.wrapper.apply(new Function0<Void>() { public Void apply() { result.set(true); latch.countDown(); return null; } }); try { Await.result(future); fail("should have thrown"); } catch (RejectedExecutionException ex) { } assertFalse(result.get()); pool.wrapper.close(); latch.await(); assertTrue(result.get()); pool.shutdown(); }
Example #5
Source File: ReplicatedTerrapinClient.java From terrapin with Apache License 2.0 | 6 votes |
public Future<TerrapinSingleResponse> getOne( final String fileSet, final ByteBuffer key, RequestOptions options) { Pair<TerrapinClient, TerrapinClient> clientPair = getClientTuple(options); final TerrapinClient firstClient = clientPair.getLeft(); final TerrapinClient secondClient = clientPair.getRight(); // We perform the request on the primary cluster with retries to second replica. if (secondClient == null) { return firstClient.getOne(fileSet, key); } return FutureUtil.getSpeculativeFuture(firstClient.getOne(fileSet, key), new Function0<Future<TerrapinSingleResponse>>() { @Override public Future<TerrapinSingleResponse> apply() { return secondClient.getOneNoRetries(fileSet, key); } }, options.speculativeTimeoutMillis, "terrapin-get-one"); }
Example #6
Source File: ReplicatedTerrapinClient.java From terrapin with Apache License 2.0 | 6 votes |
public Future<TerrapinResponse> getMany( final String fileSet, final Set<ByteBuffer> keys, RequestOptions options) { Pair<TerrapinClient, TerrapinClient> clientPair = getClientTuple(options); final TerrapinClient firstClient = clientPair.getLeft(); final TerrapinClient secondClient = clientPair.getRight(); // We perform the request on the primary cluster with retries to second replica. if (secondClient == null) { return firstClient.getMany(fileSet, keys); } return FutureUtil.getSpeculativeFuture(firstClient.getMany(fileSet, keys), new Function0<Future<TerrapinResponse>>() { @Override public Future<TerrapinResponse> apply() { return secondClient.getManyNoRetries(fileSet, keys); } }, options.speculativeTimeoutMillis, "terrapin-get-many"); }
Example #7
Source File: DistributedLogServiceImpl.java From distributedlog with Apache License 2.0 | 6 votes |
@Override public Future<BulkWriteResponse> writeBulkWithContext(final String stream, List<ByteBuffer> data, WriteContext ctx) { bulkWritePendingStat.inc(); receivedRecordCounter.add(data.size()); BulkWriteOp op = new BulkWriteOp(stream, data, statsLogger, perStreamStatsLogger, streamPartitionConverter, getChecksum(ctx), featureChecksumDisabled, accessControlManager); executeStreamOp(op); return op.result().ensure(new Function0<BoxedUnit>() { public BoxedUnit apply() { bulkWritePendingStat.dec(); return null; } }); }
Example #8
Source File: CoreLindenServiceImpl.java From linden with Apache License 2.0 | 6 votes |
@Override public Future<LindenServiceInfo> getServiceInfo() { return instanceExecutorPool.apply(new Function0<LindenServiceInfo>() { @Override public LindenServiceInfo apply() { LindenServiceInfo serviceInfo; try { serviceInfo = lindenCore.getServiceInfo(); CacheInfo cacheInfo = lindenCluster.getCacheInfo(); serviceInfo.setCacheInfo(cacheInfo); } catch (Exception e) { serviceInfo = new LindenServiceInfo(); LOGGER.error("get service info failed : {}", Throwables.getStackTraceAsString(e)); } return serviceInfo; } }); }
Example #9
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 6 votes |
@Test public void testBothFutureFailure() { Future<Integer> originalFuture = getFuture(200, 1, false); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithFailure(200, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX); Exception e = null; try { speculativeFuture.get(); } catch (RuntimeException re) { e = re; } assertNotNull(e); assertEquals(EXCEPTION_MSG, e.getMessage()); assertTrue(isBackupFutureInvoked); checkStats(1, 0, 0); }
Example #10
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
@Test public void testBothFutureSuccessAndBackupFinishesFirst() { Future<Integer> originalFuture = getFuture(1000, 1, true); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithSuccess(200, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX); int value = speculativeFuture.get().intValue(); assertTrue(isBackupFutureInvoked); assertEquals(2, value); checkStats(1, 0, 1); }
Example #11
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
/** * Following 2 test cases deal with the case when both futures succeed. */ @Test public void testBothFutureSuccessAndOriginalFinishesFirst() { Future<Integer> originalFuture = getFuture(200, 1, true); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithSuccess(1000, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX); assertEquals(1, speculativeFuture.get().intValue()); assertTrue(isBackupFutureInvoked); checkStats(1, 1, 0); }
Example #12
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
@Test public void testBackupFutureExecutedEarlyFailure() { // First future fails before timeout and the backup future gets executed Future<Integer> originalFuture = getFuture(50, 1, false); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithSuccess(500, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX, BackupFutureRetryPolicy.UN_CONDITIONAL_RETRY_POLICY); speculativeFuture.get(); assertTrue(isBackupFutureInvoked); checkStats(1, 0, 1); }
Example #13
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
/** * Following 2 test cases deal with the case when the first future that finishes throws an * exception. */ @Test public void testOriginalFailsAndBackupFinishesSecond() { Future<Integer> originalFuture = getFuture(300, 1, false); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithSuccess(800, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX); assertEquals(2, speculativeFuture.get().intValue()); assertTrue(isBackupFutureInvoked); checkStats(1, 0, 1); }
Example #14
Source File: TestSafeQueueingFuturePool.java From distributedlog with Apache License 2.0 | 5 votes |
@Test public void testSimpleFailure() throws Exception { TestFuturePool<Void> pool = new TestFuturePool<Void>(); Future<Void> future = pool.wrapper.apply(new Function0<Void>() { public Void apply() { throw new RuntimeException("failed"); } }); try { Await.result(future); fail("should have thrown"); } catch (Exception ex) { } pool.shutdown(); }
Example #15
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
@Test public void testNoBackupFutureExecutedSuccess() { // First future succeeds soon enough. Future<Integer> originalFuture = getFuture(50, 1, true); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithSuccess(500, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX); assertEquals(1, speculativeFuture.get().intValue()); assertFalse(isBackupFutureInvoked); checkStats(0, 0, 0); }
Example #16
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
private Function0<Future<Integer>> getBackupFutureFunctionWithFailure( final long futureExecutionTimeMs, final Integer futureValue) { return new Function0<Future<Integer>>() { public Future<Integer> apply() { isBackupFutureInvoked = true; return getFuture(futureExecutionTimeMs, futureValue, false); } }; }
Example #17
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
private Function0<Future<Integer>> getBackupFutureFunctionWithSuccess( final long futureExecutionTimeMs, final Integer futureValue) { return new Function0<Future<Integer>>() { public Future<Integer> apply() { isBackupFutureInvoked = true; return getFuture(futureExecutionTimeMs, futureValue, true); } }; }
Example #18
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
private Future<Integer> getFuture(final long futureExecutionTimeMs, final Integer futureValue, final boolean isFutureSuccessful) { return timer.doLater( Duration.fromMilliseconds(futureExecutionTimeMs), new Function0<Integer>() { public Integer apply() { if (isFutureSuccessful) { return futureValue; } else { throw new RuntimeException(EXCEPTION_MSG); } } }); }
Example #19
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
@Test public void testBackupFailsAndOriginalFinishesSecond() { Future<Integer> originalFuture = getFuture(1000, 1, true); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithFailure(400, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX); assertEquals(1, speculativeFuture.get().intValue()); assertTrue(isBackupFutureInvoked); checkStats(1, 1, 0); }
Example #20
Source File: DistributedLogServiceImpl.java From distributedlog with Apache License 2.0 | 5 votes |
private Future<WriteResponse> doWrite(final String name, ByteBuffer data, Long checksum, boolean isRecordSet) { writePendingStat.inc(); receivedRecordCounter.inc(); WriteOp op = newWriteOp(name, data, checksum, isRecordSet); executeStreamOp(op); return op.result().ensure(new Function0<BoxedUnit>() { public BoxedUnit apply() { writePendingStat.dec(); return null; } }); }
Example #21
Source File: DistributedLogServiceImpl.java From distributedlog with Apache License 2.0 | 5 votes |
@Override public Future<BulkWriteResponse> writeBulkWithContext(final String stream, List<ByteBuffer> data, WriteContext ctx) { bulkWritePendingStat.inc(); receivedRecordCounter.add(data.size()); BulkWriteOp op = new BulkWriteOp(stream, data, statsLogger, perStreamStatsLogger, getChecksum(ctx), featureChecksumDisabled, accessControlManager); executeStreamOp(op); return op.result().ensure(new Function0<BoxedUnit>() { public BoxedUnit apply() { bulkWritePendingStat.dec(); return null; } }); }
Example #22
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
@Test public void testOriginalFailsAndBackupFinishesFirst() { Future<Integer> originalFuture = getFuture(500, 1, false); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithSuccess(10, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX); assertEquals(2, speculativeFuture.get().intValue()); assertTrue(isBackupFutureInvoked); checkStats(1, 0, 1); }
Example #23
Source File: FutureUtilTest.java From terrapin with Apache License 2.0 | 5 votes |
@Test public void testBackupFailsAndOriginalFinishesFirst() { Future<Integer> originalFuture = getFuture(300, 1, true); Function0<Future<Integer>> backupFunctionFuture = getBackupFutureFunctionWithFailure(800, 2); Future<Integer> speculativeFuture = FutureUtil.getSpeculativeFuture( originalFuture, backupFunctionFuture, 100, STATS_PREFIX); assertEquals(1, speculativeFuture.get().intValue()); assertTrue(isBackupFutureInvoked); checkStats(1, 1, 0); }
Example #24
Source File: ConsistentHashRoutingService.java From distributedlog with Apache License 2.0 | 5 votes |
ConsistentHashRoutingService(ServerSetWatcher serverSetWatcher, int numReplicas, int blackoutSeconds, StatsReceiver statsReceiver) { super(serverSetWatcher); this.circle = new ConsistentHash(hashFunction, numReplicas, statsReceiver.scope("ring")); this.hashedWheelTimer = new HashedWheelTimer(new ThreadFactoryBuilder() .setNameFormat("ConsistentHashRoutingService-Timer-%d").build()); this.blackoutSeconds = blackoutSeconds; // stats this.statsReceiver = statsReceiver; this.numBlackoutHosts = new AtomicInteger(0); this.numBlackoutHostsGauge = this.statsReceiver.addGauge(gaugeName("num_blackout_hosts"), new Function0<Object>() { @Override public Object apply() { return (float) numBlackoutHosts.get(); } }); this.numHostsGauge = this.statsReceiver.addGauge(gaugeName("num_hosts"), new Function0<Object>() { @Override public Object apply() { return (float) address2ShardId.size(); } }); }
Example #25
Source File: CoreLindenServiceImpl.java From linden with Apache License 2.0 | 5 votes |
@Override public Future<Response> delete(final LindenDeleteRequest request) { final Stopwatch sw = Stopwatch.createStarted(); return instanceExecutorPool.apply(new Function0<Response>() { @Override public Response apply() { Response response = null; try { long eps = sw.elapsed(TimeUnit.MILLISECONDS); if (eps > 10) { LOGGER.warn("Warning: instanceExecutorPool took " + eps + "ms to start delete."); if (eps > instanceFuturePoolWaitTimeout) { response = ResponseUtils.buildFailedResponse("Waiting time is too long, " + eps + "ms in instance future pool"); return response; } } response = lindenCore.delete(request); } catch (Exception e) { String errorStackInfo = Throwables.getStackTraceAsString(e); response = ResponseUtils.buildFailedResponse(errorStackInfo); } finally { if (response.isSuccess()) { LOGGER.info("Instance delete request succeeded, request: {}, cost: {} ms.", request, sw.elapsed(TimeUnit.MILLISECONDS)); } else { LOGGER.error("Instance delete request failed, request: {}, cost: {} ms.", request, sw.elapsed(TimeUnit.MILLISECONDS)); } return response; } } }); }
Example #26
Source File: CoreLindenServiceImpl.java From linden with Apache License 2.0 | 5 votes |
@Override public Future<Response> index(final String content) { final Stopwatch sw = Stopwatch.createStarted(); return instanceExecutorPool.apply(new Function0<Response>() { @Override public Response apply() { Response response = null; try { long eps = sw.elapsed(TimeUnit.MILLISECONDS); if (eps > 10) { LOGGER.warn("Warning: instanceExecutorPool took " + eps + "ms to start index."); if (eps > instanceFuturePoolWaitTimeout) { response = ResponseUtils.buildFailedResponse("Waiting time is too long, " + eps + "ms in instance future pool"); return response; } } LindenIndexRequest indexRequest = LindenIndexRequestParser.parse(config.getSchema(), content); response = lindenCore.index(indexRequest); } catch (Exception e) { String errorStackInfo = Throwables.getStackTraceAsString(e); response = ResponseUtils.buildFailedResponse(errorStackInfo); } finally { if (response.isSuccess()) { LOGGER.info("Instance index request succeeded, content: {}, cost: {} ms.", content, sw.elapsed(TimeUnit.MILLISECONDS)); } else { LOGGER.error("Instance index request failed, content: {}, cost: {} ms.", content, sw.elapsed(TimeUnit.MILLISECONDS)); } return response; } } }); }
Example #27
Source File: CoreLindenServiceImpl.java From linden with Apache License 2.0 | 5 votes |
@Override public Future<Response> executeCommand(final String command) { final Stopwatch sw = Stopwatch.createStarted(); return instanceExecutorPool.apply(new Function0<Response>() { @Override public Response apply() { LOGGER.info("Receive command {}", command); Response response = null; try { long eps = sw.elapsed(TimeUnit.MILLISECONDS); if (eps > 10) { LOGGER.warn("Warning: instanceExecutorPool took " + eps + "ms to start executeCommand."); if (eps > instanceFuturePoolWaitTimeout) { response = ResponseUtils.buildFailedResponse("Waiting time is too long, " + eps + "ms in instance future pool"); return response; } } response = lindenCore.executeCommand(command); } catch (Exception e) { String errorStackInfo = Throwables.getStackTraceAsString(e); response = ResponseUtils.buildFailedResponse(errorStackInfo); } finally { if (response.isSuccess()) { LOGGER.info("executeCommand succeeded, command: {}, cost: {} ms.", command, sw.elapsed(TimeUnit.MILLISECONDS)); } else { LOGGER.error("executeCommand failed, content: {}, cost: {} ms.", command, sw.elapsed(TimeUnit.MILLISECONDS)); } return response; } } }); }
Example #28
Source File: DistributedLogServiceImpl.java From distributedlog with Apache License 2.0 | 5 votes |
private Future<WriteResponse> doWrite(final String name, ByteBuffer data, Long checksum, boolean isRecordSet) { writePendingStat.inc(); receivedRecordCounter.inc(); WriteOp op = newWriteOp(name, data, checksum, isRecordSet); executeStreamOp(op); return op.result().ensure(new Function0<BoxedUnit>() { public BoxedUnit apply() { writePendingStat.dec(); return null; } }); }
Example #29
Source File: PlacementPolicy.java From distributedlog with Apache License 2.0 | 5 votes |
public void start(boolean leader) { logger.info("Starting placement policy"); TreeSet<ServerLoad> emptyServerLoads = new TreeSet<ServerLoad>(); for (String server : getServers()) { emptyServerLoads.add(new ServerLoad(server)); } load(emptyServerLoads); //Pre-Load so streams don't NPE if (leader) { //this is the leader shard logger.info("Shard is leader. Scheduling timed refresh."); placementRefreshTimer = new ScheduledThreadPoolTimer(1, "timer", true); placementRefreshTimer.schedule(Time.now(), refreshInterval, new Function0<BoxedUnit>() { @Override public BoxedUnit apply() { refresh(); return BoxedUnit.UNIT; } }); } else { logger.info("Shard is not leader. Watching for server load changes."); placementStateManager.watch(new PlacementStateManager.PlacementCallback() { @Override public void callback(TreeSet<ServerLoad> serverLoads) { if (!serverLoads.isEmpty()) { load(serverLoads); } } }); } }
Example #30
Source File: ConsistentHashRoutingService.java From distributedlog with Apache License 2.0 | 5 votes |
ConsistentHashRoutingService(ServerSetWatcher serverSetWatcher, int numReplicas, int blackoutSeconds, StatsReceiver statsReceiver) { super(serverSetWatcher); this.circle = new ConsistentHash(hashFunction, numReplicas, statsReceiver.scope("ring")); this.hashedWheelTimer = new HashedWheelTimer(new ThreadFactoryBuilder() .setNameFormat("ConsistentHashRoutingService-Timer-%d").build()); this.blackoutSeconds = blackoutSeconds; // stats this.statsReceiver = statsReceiver; this.numBlackoutHosts = new AtomicInteger(0); this.numBlackoutHostsGauge = this.statsReceiver.addGauge(gaugeName("num_blackout_hosts"), new Function0<Object>() { @Override public Object apply() { return (float) numBlackoutHosts.get(); } }); this.numHostsGauge = this.statsReceiver.addGauge(gaugeName("num_hosts"), new Function0<Object>() { @Override public Object apply() { return (float) address2ShardId.size(); } }); }