Search in sources :

Example 1 with Future

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);
        }
    });
}
Also used : MessageDroppedException(com.metamx.tranquility.tranquilizer.MessageDroppedException) Values(org.apache.storm.tuple.Values) Future(com.twitter.util.Future) FutureEventListener(com.twitter.util.FutureEventListener)

Example 2 with Future

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;
}
Also used : ArrayList(java.util.ArrayList) SendResult(com.metamx.tranquility.beam.SendResult) Future(com.twitter.util.Future) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 3 with Future

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;
}
Also used : Promise(com.twitter.util.Promise) AsyncCloseable(com.twitter.distributedlog.io.AsyncCloseable) Future(com.twitter.util.Future)

Example 4 with Future

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;
}
Also used : DistributedLock(com.twitter.distributedlog.lock.DistributedLock) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock) ExceptionalFunction0(com.twitter.util.ExceptionalFunction0) Future(com.twitter.util.Future) IOException(java.io.IOException) ExceptionalFunction(com.twitter.util.ExceptionalFunction) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock)

Example 5 with Future

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());
    }
}
Also used : ArrayList(java.util.ArrayList) Future(com.twitter.util.Future) FutureEventListener(com.twitter.util.FutureEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Future (com.twitter.util.Future)40 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)24 ByteBuffer (java.nio.ByteBuffer)9 Promise (com.twitter.util.Promise)8 ZKDistributedLock (com.twitter.distributedlog.lock.ZKDistributedLock)7 WriteResponse (com.twitter.distributedlog.thrift.service.WriteResponse)7 List (java.util.List)6 DLSN (com.twitter.distributedlog.DLSN)5 FutureEventListener (com.twitter.util.FutureEventListener)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)5 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)4 StreamImpl (com.twitter.distributedlog.service.stream.StreamImpl)4 StreamManagerImpl (com.twitter.distributedlog.service.stream.StreamManagerImpl)4 IOException (java.io.IOException)4 LogRecordWithDLSN (com.twitter.distributedlog.LogRecordWithDLSN)3 DynamicDistributedLogConfiguration (com.twitter.distributedlog.config.DynamicDistributedLogConfiguration)3 BKTransmitException (com.twitter.distributedlog.exceptions.BKTransmitException)3 WriteCancelledException (com.twitter.distributedlog.exceptions.WriteCancelledException)3