com.twitter.util.Try Java Examples
The following examples show how to use
com.twitter.util.Try.
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: FinagleSender.java From zipkin-finagle with Apache License 2.0 | 6 votes |
@Override protected void doEnqueue(Callback<Void> callback) { try { client.apply(makeRequest(spans)).respond(new AbstractFunction1<Try<Rep>, BoxedUnit>() { @Override public BoxedUnit apply(Try<Rep> result) { if (result.isReturn()) { callback.onSuccess(null); } else { callback.onError(result.throwable()); } return BoxedUnit.UNIT; } }); } catch (Exception e) { callback.onError(e); } }
Example #2
Source File: TerrapinServerInternalImplTest.java From terrapin with Apache License 2.0 | 6 votes |
@Test public void testGetErrorMultipleResources() throws Exception { TerrapinInternalGetRequest request = new TerrapinInternalGetRequest(); MultiKey multiKey1 = new MultiKey().setResource("resource1").setPartition("1"); multiKey1.addToKey(ByteBuffer.wrap("k1".getBytes())); MultiKey multiKey2 = new MultiKey().setResource("resource2").setPartition("1"); multiKey2.addToKey(ByteBuffer.wrap("k2".getBytes())); request.addToKeyList(multiKey1); request.addToKeyList(multiKey2); Reader mockReader = mock(Reader.class); when(mockResourcePartitionMap.getReader(eq("resource1"), eq("1"))).thenReturn(mockReader); Try<TerrapinResponse> responseTry = serverImpl.get(request).get(Duration.forever()); TerrapinGetException e = (TerrapinGetException)((Throw)responseTry).e(); assertEquals(TerrapinGetErrorCode.INVALID_REQUEST, e.getErrorCode()); }
Example #3
Source File: BKAsyncLogWriter.java From distributedlog with Apache License 2.0 | 6 votes |
private List<Future<DLSN>> asyncWriteBulk(List<LogRecord> records) { final ArrayList<Future<DLSN>> results = new ArrayList<Future<DLSN>>(records.size()); Iterator<LogRecord> iterator = records.iterator(); while (iterator.hasNext()) { LogRecord record = iterator.next(); Future<DLSN> future = asyncWrite(record, !iterator.hasNext()); results.add(future); // Abort early if an individual write has already failed. Option<Try<DLSN>> result = future.poll(); if (result.isDefined() && result.get().isThrow()) { break; } } if (records.size() > results.size()) { appendCancelledFutures(results, records.size() - results.size()); } return results; }
Example #4
Source File: TerrapinServiceImplTest.java From terrapin with Apache License 2.0 | 6 votes |
@Test public void testMultiGetError() { TerrapinMultiGetRequest request = prepareMultiGetRequest(); Set<ByteBuffer> keys = Sets.newHashSet(ByteBuffer.wrap("key1".getBytes()), ByteBuffer.wrap("key2".getBytes()), ByteBuffer.wrap("key3".getBytes())); when (mockClient1.getMany(eq(FILESET), eq(keys))).thenReturn( Future.<TerrapinResponse>exception(new TerrapinGetException("Failed", TerrapinGetErrorCode.FILE_SET_NOT_FOUND))); Try<TerrapinResponse> responseTry = serviceIface.multiGet(request).get(Duration.forever()); assertTrue(responseTry.isThrow()); assertEquals(TerrapinGetErrorCode.FILE_SET_NOT_FOUND, ((TerrapinGetException)((Throw)responseTry).e()).getErrorCode()); when(mockClient1.getMany(eq(FILESET), eq(keys))).thenThrow( new RuntimeException(new NullPointerException())); responseTry = serviceIface.multiGet(request).get(Duration.forever()); assertTrue(responseTry.isThrow()); assertEquals(TerrapinGetErrorCode.OTHER, ((TerrapinGetException)((Throw)responseTry).e()).getErrorCode()); }
Example #5
Source File: TerrapinServiceImplTest.java From terrapin with Apache License 2.0 | 6 votes |
@Test public void testMultiGetEmptyClusters() { TerrapinMultiGetRequest request = prepareMultiGetRequest(); RequestOptions options = new RequestOptions(); options.setSelectionPolicy(SelectionPolicy.PRIMARY_FIRST); request.setOptions(options); request.setClusterList(ImmutableList.copyOf(new String[]{})); TerrapinResponse response = new TerrapinResponse().setResponseMap(ImmutableMap.of( ByteBuffer.wrap("key1".getBytes()), new TerrapinSingleResponse().setValue("value1".getBytes()), ByteBuffer.wrap("key2".getBytes()), new TerrapinSingleResponse().setErrorCode(TerrapinGetErrorCode.READ_ERROR))); Set<ByteBuffer> keys = Sets.newHashSet(ByteBuffer.wrap("key1".getBytes()), ByteBuffer.wrap("key2".getBytes()), ByteBuffer.wrap("key3".getBytes())); when(mockClient1.getMany(eq(FILESET), eq(keys))).thenReturn(Future.value(response)); when(mockClient2.getManyNoRetries(eq(FILESET), eq(keys))).thenReturn(Future.value(response)); Try<TerrapinResponse> returnResponseTry = serviceIface.multiGet(request).get(Duration.forever()); assertTrue(returnResponseTry.isThrow()); assertEquals(TerrapinGetErrorCode.INVALID_REQUEST, ((TerrapinGetException)((Throw)returnResponseTry).e()).getErrorCode()); }
Example #6
Source File: TerrapinServiceImplTest.java From terrapin with Apache License 2.0 | 6 votes |
@Test public void testGetEmptyClusters() { ByteBuffer key = ByteBuffer.wrap(KEY); TerrapinGetRequest request = prepareGetRequest(); RequestOptions options = new RequestOptions(); options.setSelectionPolicy(SelectionPolicy.PRIMARY_FIRST); request.setOptions(options); request.setClusterList(ImmutableList.copyOf(new String[]{})); TerrapinResponse response = new TerrapinResponse().setResponseMap(ImmutableMap.of( key, new TerrapinSingleResponse().setValue(ByteBuffer.wrap("value".getBytes())))); when(mockClient1.getMany(eq(FILESET), eq(Sets.newHashSet(key)))).thenReturn( Future.value(response)); when(mockClient2.getManyNoRetries(eq(FILESET), eq(Sets.newHashSet(key)))).thenReturn( Future.value(response)); Try<TerrapinSingleResponse> singleResponseTry = serviceIface.get(request).get(Duration.forever()); assertTrue(singleResponseTry.isThrow()); assertEquals(TerrapinGetErrorCode.INVALID_REQUEST, ((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode()); }
Example #7
Source File: BulkWriteOp.java From distributedlog with Apache License 2.0 | 5 votes |
private boolean isDefiniteFailure(Try<DLSN> result) { boolean def = false; try { result.get(); } catch (Exception ex) { if (ex instanceof OwnershipAcquireFailedException || ex instanceof AlreadyClosedException || ex instanceof LockingException) { def = true; } } return def; }
Example #8
Source File: PinLaterQueryIssuer.java From pinlater with Apache License 2.0 | 5 votes |
private void issueEnqueueRequests(PinLater.ServiceIface iface) throws InterruptedException { Preconditions.checkNotNull(queueName, "Queue was not specified."); final AtomicLong queriesIssued = new AtomicLong(0); final Semaphore permits = new Semaphore(concurrency); while (numQueries == -1 || queriesIssued.get() < numQueries) { final PinLaterEnqueueRequest request = new PinLaterEnqueueRequest(); request.setQueueName(queueName); for (int i = 0; i < batchSize; i++) { PinLaterJob job = new PinLaterJob(ByteBuffer.wrap( new String("task_" + random.nextInt(Integer.MAX_VALUE)).getBytes())); job.setPriority(priority); request.addToJobs(job); } final long startTimeNanos = System.nanoTime(); queriesIssued.incrementAndGet(); permits.acquire(); iface.enqueueJobs(REQUEST_CONTEXT, request).respond( new Function<Try<PinLaterEnqueueResponse>, BoxedUnit>() { @Override public BoxedUnit apply(Try<PinLaterEnqueueResponse> responseTry) { permits.release(); statsLogger.requestComplete( Duration.fromNanoseconds(System.nanoTime() - startTimeNanos)); if (responseTry.isThrow()) { LOG.info("Exception for request: " + request + " : " + ((Throw) responseTry).e()); } return BoxedUnit.UNIT; } }); } permits.acquire(concurrency); LOG.info("Enqueue queries issued: " + queriesIssued); }
Example #9
Source File: PinLaterBackendBase.java From pinlater with Apache License 2.0 | 5 votes |
public Future<PinLaterDequeueResponse> dequeueJobs(final String source, final PinLaterDequeueRequest request) { Future<PinLaterDequeueResponse> dequeueFuture; try { dequeueFuture = dequeueSemaphoreMap.get(request.getQueueName()).acquire().flatMap( new Function<Permit, Future<PinLaterDequeueResponse>>() { @Override public Future<PinLaterDequeueResponse> apply(final Permit permit) { return futurePool.apply(new ExceptionalFunction0<PinLaterDequeueResponse>() { @Override public PinLaterDequeueResponse applyE() throws Throwable { return dequeueJobsImpl(source, request, numAutoRetries); } }).respond(new Function<Try<PinLaterDequeueResponse>, BoxedUnit>() { @Override public BoxedUnit apply(Try<PinLaterDequeueResponse> responseTry) { permit.release(); return BoxedUnit.UNIT; } }); } }); } catch (ExecutionException e) { // The dequeueSemaphoreMap's get() can in theory throw an ExecutionException, but we // never expect it in practice since our load method is simply new'ing up an AsyncSemaphore. dequeueFuture = Future.exception(e); } // Dequeue requests can contain ack requests as payloads. If so, we execute both in parallel. Future<Void> ackFuture = request.isSetJobAckRequest() ? ackDequeuedJobsImpl(request.getJobAckRequest()) : Future.Void(); return dequeueFuture.join(ackFuture).map( new Function<Tuple2<PinLaterDequeueResponse, Void>, PinLaterDequeueResponse>() { @Override public PinLaterDequeueResponse apply(Tuple2<PinLaterDequeueResponse, Void> tuple) { return tuple._1(); } }); }
Example #10
Source File: FutureUtil.java From terrapin with Apache License 2.0 | 5 votes |
public static <T> Future<Try<T>> lifeToTry(Future<T> future) { return future.map(new Function<T, Try<T>>() { @Override public Try<T> apply(T o) { return new Return(o); } }).handle(new Function<Throwable, Try<T>>() { @Override public Try<T> apply(Throwable throwable) { return new Throw(throwable); } }); }
Example #11
Source File: TerrapinServiceImplTest.java From terrapin with Apache License 2.0 | 5 votes |
@Test public void testMultiGetClusterNotFound() { TerrapinMultiGetRequest request = prepareMultiGetRequest().setClusterList(ImmutableList.of( "random-cluster")); Try<TerrapinResponse> responseTry = serviceIface.multiGet(request).get(Duration.forever()); assertTrue(responseTry.isThrow()); assertEquals(TerrapinGetErrorCode.CLUSTER_NOT_FOUND, ((TerrapinGetException)((Throw)responseTry).e()).getErrorCode()); }
Example #12
Source File: TerrapinServiceImplTest.java From terrapin with Apache License 2.0 | 5 votes |
@Test public void testGetError() { // Test the case where We get back an error set through an error code set in // TerrapinSingleResponse. ByteBuffer key = ByteBuffer.wrap(KEY); TerrapinGetRequest request = prepareGetRequest(); TerrapinResponse response = new TerrapinResponse().setResponseMap(ImmutableMap.of( key, new TerrapinSingleResponse().setErrorCode(TerrapinGetErrorCode.OTHER))); when(mockClient1.getMany(eq(FILESET), eq(Sets.newHashSet(key)))).thenReturn( Future.value(response)); Try<TerrapinSingleResponse> singleResponseTry = serviceIface.get(request).get( Duration.forever()); assertTrue(singleResponseTry.isThrow()); assertEquals(TerrapinGetErrorCode.OTHER, ((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode()); // Test the case where the call to the client library itself bails out due to a // legit error. when(mockClient1.getMany(eq(FILESET), eq(Sets.newHashSet(key)))).thenReturn( Future.<TerrapinResponse>exception(new TerrapinGetException("Failed.", TerrapinGetErrorCode.FILE_SET_NOT_FOUND))); singleResponseTry = serviceIface.get(request).get(Duration.forever()); assertTrue(singleResponseTry.isThrow()); assertEquals(TerrapinGetErrorCode.FILE_SET_NOT_FOUND, ((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode()); // Test the case where the call to the client library bails out due to a runtime // exception. when(mockClient1.getMany(eq(FILESET), eq(Sets.newHashSet(key)))).thenThrow( new RuntimeException(new NullPointerException())); singleResponseTry = serviceIface.get(request).get(Duration.forever()); assertTrue(singleResponseTry.isThrow()); assertEquals(TerrapinGetErrorCode.OTHER, ((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode()); }
Example #13
Source File: TerrapinServiceImplTest.java From terrapin with Apache License 2.0 | 5 votes |
@Test public void testGetClusterNotFound() { TerrapinGetRequest request = prepareGetRequest().setClusterList(ImmutableList.of( "random-cluster")); Try<TerrapinSingleResponse> singleResponseTry = serviceIface.get(request).get(Duration.forever()); assertTrue(singleResponseTry.isThrow()); assertEquals(TerrapinGetErrorCode.CLUSTER_NOT_FOUND, ((TerrapinGetException)((Throw)singleResponseTry).e()).getErrorCode()); }
Example #14
Source File: AbstractStreamOp.java From distributedlog with Apache License 2.0 | 5 votes |
protected void setResponse(Response response) { Return<Response> responseTry = new Return(response); boolean isEmpty = result.updateIfEmpty(responseTry); if (!isEmpty) { Option<Try<Response>> resultTry = result.poll(); logger.error("Result set multiple times. Value='{}', New='{}'", resultTry, responseTry); } }
Example #15
Source File: BulkWriteOp.java From distributedlog with Apache License 2.0 | 5 votes |
private Future<List<Try<DLSN>>> asTryList(Future<List<Future<DLSN>>> futureList) { return futureList.flatMap(new AbstractFunction1<List<Future<DLSN>>, Future<List<Try<DLSN>>>>() { @Override public Future<List<Try<DLSN>>> apply(List<Future<DLSN>> results) { return Future$.MODULE$.collectToTry(results); } }); }
Example #16
Source File: BulkWriteOp.java From distributedlog with Apache License 2.0 | 5 votes |
private boolean isDefiniteFailure(Try<DLSN> result) { boolean def = false; try { result.get(); } catch (Exception ex) { if (ex instanceof OwnershipAcquireFailedException || ex instanceof AlreadyClosedException || ex instanceof LockingException) { def = true; } } return def; }
Example #17
Source File: BKLogReadHandler.java From distributedlog with Apache License 2.0 | 5 votes |
<T> void satisfyPromiseAsync(final Promise<T> promise, final Try<T> result) { scheduler.submit(new SafeRunnable() { @Override public void safeRun() { promise.update(result); } }); }
Example #18
Source File: FutureUtils.java From distributedlog with Apache License 2.0 | 5 votes |
/** * Raise an exception to the <i>promise</i> within a given <i>timeout</i> period. * If the promise has been satisfied before raising, it won't change the state of the promise. * * @param promise promise to raise exception * @param timeout timeout period * @param unit timeout period unit * @param cause cause to raise * @param scheduler scheduler to execute raising exception * @param key the submit key used by the scheduler * @return the promise applied with the raise logic */ public static <T> Promise<T> within(final Promise<T> promise, final long timeout, final TimeUnit unit, final Throwable cause, final OrderedScheduler scheduler, final Object key) { if (timeout < DistributedLogConstants.FUTURE_TIMEOUT_IMMEDIATE || promise.isDefined()) { return promise; } // schedule a timeout to raise timeout exception final java.util.concurrent.ScheduledFuture<?> task = scheduler.schedule(key, new Runnable() { @Override public void run() { if (!promise.isDefined() && FutureUtils.setException(promise, cause)) { logger.info("Raise exception", cause); } } }, timeout, unit); // when the promise is satisfied, cancel the timeout task promise.respond(new AbstractFunction1<Try<T>, BoxedUnit>() { @Override public BoxedUnit apply(Try<T> value) { if (!task.cancel(true)) { logger.debug("Failed to cancel the timeout task"); } return BoxedUnit.UNIT; } }); return promise; }
Example #19
Source File: AbstractStreamOp.java From distributedlog with Apache License 2.0 | 5 votes |
protected void setResponse(Response response) { Return<Response> responseTry = new Return(response); boolean isEmpty = result.updateIfEmpty(responseTry); if (!isEmpty) { Option<Try<Response>> resultTry = result.poll(); logger.error("Result set multiple times. Value='{}', New='{}'", resultTry, responseTry); } }
Example #20
Source File: BulkWriteOp.java From distributedlog with Apache License 2.0 | 5 votes |
private Future<List<Try<DLSN>>> asTryList(Future<List<Future<DLSN>>> futureList) { return futureList.flatMap(new AbstractFunction1<List<Future<DLSN>>, Future<List<Try<DLSN>>>>() { @Override public Future<List<Try<DLSN>>> apply(List<Future<DLSN>> results) { return Future$.MODULE$.collectToTry(results); } }); }
Example #21
Source File: PinLaterQueryIssuer.java From pinlater with Apache License 2.0 | 4 votes |
private void issueDequeueAckRequests(final PinLater.ServiceIface iface) throws InterruptedException { Preconditions.checkNotNull(queueName, "Queue was not specified."); final AtomicLong queriesIssued = new AtomicLong(0); final Semaphore permits = new Semaphore(concurrency); while (numQueries == -1 || queriesIssued.get() < numQueries) { final PinLaterDequeueRequest request = new PinLaterDequeueRequest(); request.setQueueName(queueName); request.setLimit(batchSize); final long startTimeNanos = System.nanoTime(); queriesIssued.incrementAndGet(); permits.acquire(); iface.dequeueJobs(REQUEST_CONTEXT, request).flatMap( new Function<PinLaterDequeueResponse, Future<Void>>() { @Override public Future<Void> apply(PinLaterDequeueResponse response) { if (response.getJobsSize() == 0) { return Future.Void(); } PinLaterJobAckRequest jobAckRequest = new PinLaterJobAckRequest(queueName); for (String job : response.getJobs().keySet()) { if (random.nextInt(100) < dequeueSuccessPercent) { jobAckRequest.addToJobsSucceeded(new PinLaterJobAckInfo(job)); } else { jobAckRequest.addToJobsFailed(new PinLaterJobAckInfo(job)); } } return iface.ackDequeuedJobs(REQUEST_CONTEXT, jobAckRequest); } }).respond(new Function<Try<Void>, BoxedUnit>() { @Override public BoxedUnit apply(Try<Void> voidTry) { permits.release(); statsLogger.requestComplete( Duration.fromNanoseconds(System.nanoTime() - startTimeNanos)); if (voidTry.isThrow()) { LOG.info("Exception for request: " + request + " : " + ((Throw) voidTry).e()); } return BoxedUnit.UNIT; } }); } permits.acquire(concurrency); LOG.info("Dequeue/ack queries issued: " + queriesIssued); }