use of com.twitter.util.Future in project storm by apache.
the class DruidBeamBolt method process.
@Override
protected void process(final Tuple tuple) {
Future future = tranquilizer.send((druidEventMapper.getEvent(tuple)));
LOG.debug("Sent tuple : [{}]", tuple);
future.addEventListener(new FutureEventListener() {
@Override
public void onFailure(Throwable cause) {
if (cause instanceof MessageDroppedException) {
collector.ack(tuple);
LOG.debug("Tuple Dropped due to MessageDroppedException : [{}]", tuple);
if (druidConfig.getDiscardStreamId() != null)
collector.emit(druidConfig.getDiscardStreamId(), new Values(tuple, System.currentTimeMillis()));
} else {
collector.fail(tuple);
LOG.debug("Tuple Processing Failed : [{}]", tuple);
}
}
@Override
public void onSuccess(Object value) {
collector.ack(tuple);
LOG.debug("Tuple Processing Success : [{}]", tuple);
}
});
}
use of com.twitter.util.Future in project storm by apache.
the class DruidBeamState method update.
public List<E> update(List<TridentTuple> tuples, TridentCollector collector) {
List<E> events = new ArrayList<>(tuples.size());
for (TridentTuple tuple : tuples) {
events.add(druidEventMapper.getEvent(tuple));
}
LOG.info("Sending [{}] events", events.size());
scala.collection.immutable.List<E> scalaList = scala.collection.JavaConversions.collectionAsScalaIterable(events).toList();
Collection<Future<SendResult>> futureList = scala.collection.JavaConversions.asJavaCollection(beam.sendAll(scalaList));
List<E> discardedEvents = new ArrayList<>();
int index = 0;
for (Future<SendResult> future : futureList) {
try {
SendResult result = Await.result(future);
if (!result.sent()) {
discardedEvents.add(events.get(index));
}
} catch (Exception e) {
LOG.error("Failed in writing messages to Druid", e);
}
index++;
}
return discardedEvents;
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class BKDistributedLogManager method asyncClose.
/**
* Close the distributed log manager, freeing any resources it may hold.
*/
@Override
public Future<Void> asyncClose() {
Promise<Void> closeFuture;
BKLogReadHandler readHandlerToClose;
synchronized (this) {
if (null != closePromise) {
return closePromise;
}
closeFuture = closePromise = new Promise<Void>();
readHandlerToClose = readHandlerForListener;
}
// NOTE: the resources {scheduler, writerBKC, readerBKC} are mostly from namespace instance.
// so they are not blocking call except tests.
AsyncCloseable resourcesCloseable = new AsyncCloseable() {
@Override
public Future<Void> asyncClose() {
int schedTimeout = conf.getSchedulerShutdownTimeoutMs();
// Clean up executor state.
if (ownExecutor) {
SchedulerUtils.shutdownScheduler(scheduler, schedTimeout, TimeUnit.MILLISECONDS);
LOG.info("Stopped BKDL executor service for {}.", name);
if (scheduler != readAheadScheduler) {
SchedulerUtils.shutdownScheduler(readAheadScheduler, schedTimeout, TimeUnit.MILLISECONDS);
LOG.info("Stopped BKDL ReadAhead Executor Service for {}.", name);
}
SchedulerUtils.shutdownScheduler(getLockStateExecutor(false), schedTimeout, TimeUnit.MILLISECONDS);
LOG.info("Stopped BKDL Lock State Executor for {}.", name);
}
if (ownWriterBKC) {
writerBKC.close();
}
if (ownReaderBKC) {
readerBKC.close();
}
return Future.Void();
}
};
Future<Void> closeResult = Utils.closeSequence(null, true, readHandlerToClose, pendingReaders, resourcesCloseable, new AsyncCloseable() {
@Override
public Future<Void> asyncClose() {
return BKDistributedLogManager.super.asyncClose();
}
});
closeResult.proxyTo(closeFuture);
return closeFuture;
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class BKLogReadHandler method lockStream.
/**
* Elective stream lock--readers are not required to acquire the lock before using the stream.
*/
synchronized Future<Void> lockStream() {
if (null == lockAcquireFuture) {
final Function0<DistributedLock> lockFunction = new ExceptionalFunction0<DistributedLock>() {
@Override
public DistributedLock applyE() throws IOException {
// Unfortunately this has a blocking call which we should not execute on the
// ZK completion thread
BKLogReadHandler.this.readLock = new ZKDistributedLock(lockStateExecutor, lockFactory, readLockPath, conf.getLockTimeoutMilliSeconds(), statsLogger.scope("read_lock"));
LOG.info("acquiring readlock {} at {}", getLockClientId(), readLockPath);
return BKLogReadHandler.this.readLock;
}
};
lockAcquireFuture = ensureReadLockPathExist().flatMap(new ExceptionalFunction<Void, Future<Void>>() {
@Override
public Future<Void> applyE(Void in) throws Throwable {
return scheduler.apply(lockFunction).flatMap(new ExceptionalFunction<DistributedLock, Future<Void>>() {
@Override
public Future<Void> applyE(DistributedLock lock) throws IOException {
return acquireLockOnExecutorThread(lock);
}
});
}
});
}
return lockAcquireFuture;
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class TestDistributedLock method testAsyncAcquireBasics.
@Test(timeout = 60000)
public void testAsyncAcquireBasics() throws Exception {
TestLockFactory locks = new TestLockFactory(runtime.getMethodName(), zkc, lockStateExecutor);
int count = 3;
ArrayList<Future<ZKDistributedLock>> results = new ArrayList<Future<ZKDistributedLock>>(count);
ZKDistributedLock[] lockArray = new ZKDistributedLock[count];
final CountDownLatch[] latches = new CountDownLatch[count];
// the future.
for (int i = 0; i < count; i++) {
latches[i] = new CountDownLatch(1);
lockArray[i] = locks.createLock(i, zkc);
final int index = i;
results.add(lockArray[i].asyncAcquire().addEventListener(new FutureEventListener<ZKDistributedLock>() {
@Override
public void onSuccess(ZKDistributedLock lock) {
latches[index].countDown();
}
@Override
public void onFailure(Throwable cause) {
fail("unexpected failure " + cause);
}
}));
}
// acquired).
for (int i = 0; i < count; i++) {
latches[i].await();
assertLatchesSet(latches, i + 1);
Await.result(results.get(i));
FutureUtils.result(lockArray[i].asyncClose());
}
}
Aggregations