Search in sources :

Example 21 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class TestZKLogSegmentMetadataStore method testStoreMaxTxnId.

@Test(timeout = 60000)
public void testStoreMaxTxnId() throws Exception {
    Transaction<Object> updateTxn = lsmStore.transaction();
    Versioned<Long> value = new Versioned<Long>(999L, new ZkVersion(0));
    final Promise<Version> result = new Promise<Version>();
    lsmStore.storeMaxTxnId(updateTxn, rootZkPath, value, new Transaction.OpListener<Version>() {

        @Override
        public void onCommit(Version r) {
            result.setValue(r);
        }

        @Override
        public void onAbort(Throwable t) {
            result.setException(t);
        }
    });
    FutureUtils.result(updateTxn.execute());
    assertEquals(1, ((ZkVersion) FutureUtils.result(result)).getZnodeVersion());
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(rootZkPath, false, stat);
    assertEquals(999L, DLUtils.deserializeTransactionId(data));
    assertEquals(1, stat.getVersion());
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) Promise(com.twitter.util.Promise) Stat(org.apache.zookeeper.data.Stat) Transaction(com.twitter.distributedlog.util.Transaction) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) Version(org.apache.bookkeeper.versioning.Version) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) Test(org.junit.Test)

Example 22 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class TestZKLogSegmentMetadataStore method testStoreMaxLogSegmentSequenceNumberBadVersion.

@Test(timeout = 60000)
public void testStoreMaxLogSegmentSequenceNumberBadVersion() throws Exception {
    Transaction<Object> updateTxn = lsmStore.transaction();
    Versioned<Long> value = new Versioned<Long>(999L, new ZkVersion(10));
    final Promise<Version> result = new Promise<Version>();
    lsmStore.storeMaxLogSegmentSequenceNumber(updateTxn, rootZkPath, value, new Transaction.OpListener<Version>() {

        @Override
        public void onCommit(Version r) {
            result.setValue(r);
        }

        @Override
        public void onAbort(Throwable t) {
            result.setException(t);
        }
    });
    try {
        FutureUtils.result(updateTxn.execute());
        fail("Should fail on storing log segment sequence number if providing bad version");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    try {
        Await.result(result);
        fail("Should fail on storing log segment sequence number if providing bad version");
    } catch (KeeperException ke) {
        assertEquals(KeeperException.Code.BADVERSION, ke.code());
    }
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(rootZkPath, false, stat);
    assertEquals(0, stat.getVersion());
    assertEquals(0, data.length);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) Promise(com.twitter.util.Promise) ZKException(com.twitter.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) Transaction(com.twitter.distributedlog.util.Transaction) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) Version(org.apache.bookkeeper.versioning.Version) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 23 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class TestZKLogSegmentMetadataStore method testStoreMaxTxnIdOnNonExistentPath.

@Test(timeout = 60000)
public void testStoreMaxTxnIdOnNonExistentPath() throws Exception {
    Transaction<Object> updateTxn = lsmStore.transaction();
    Versioned<Long> value = new Versioned<Long>(999L, new ZkVersion(10));
    final Promise<Version> result = new Promise<Version>();
    String nonExistentPath = rootZkPath + "/non-existent";
    lsmStore.storeMaxLogSegmentSequenceNumber(updateTxn, nonExistentPath, value, new Transaction.OpListener<Version>() {

        @Override
        public void onCommit(Version r) {
            result.setValue(r);
        }

        @Override
        public void onAbort(Throwable t) {
            result.setException(t);
        }
    });
    try {
        FutureUtils.result(updateTxn.execute());
        fail("Should fail on storing log record transaction id if path doesn't exist");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.NONODE, zke.getKeeperExceptionCode());
    }
    try {
        Await.result(result);
        fail("Should fail on storing log record transaction id if path doesn't exist");
    } catch (KeeperException ke) {
        assertEquals(KeeperException.Code.NONODE, ke.code());
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) Promise(com.twitter.util.Promise) ZKException(com.twitter.distributedlog.exceptions.ZKException) Transaction(com.twitter.distributedlog.util.Transaction) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) Version(org.apache.bookkeeper.versioning.Version) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 24 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class BKAsyncLogReaderDLSN method asyncClose.

@Override
public Future<Void> asyncClose() {
    // Cancel the idle reader timeout task, interrupting if necessary
    ReadCancelledException exception;
    Promise<Void> closePromise;
    synchronized (this) {
        if (null != closeFuture) {
            return closeFuture;
        }
        closePromise = closeFuture = new Promise<Void>();
        exception = new ReadCancelledException(bkLedgerManager.getFullyQualifiedName(), "Reader was closed");
        setLastException(exception);
    }
    // Do this after we have checked that the reader was not previously closed
    try {
        if (null != idleReaderTimeoutTask) {
            idleReaderTimeoutTask.cancel(true);
        }
    } catch (Exception exc) {
        LOG.info("{}: Failed to cancel the background idle reader timeout task", bkLedgerManager.getFullyQualifiedName());
    }
    synchronized (scheduleLock) {
        if (null != backgroundScheduleTask) {
            backgroundScheduleTask.cancel(true);
        }
    }
    cancelAllPendingReads(exception);
    bkLedgerManager.unregister(sessionExpireWatcher);
    FutureUtils.ignore(bkLedgerManager.asyncClose()).proxyTo(closePromise);
    return closePromise;
}
Also used : Promise(com.twitter.util.Promise) ReadCancelledException(com.twitter.distributedlog.exceptions.ReadCancelledException) ReadCancelledException(com.twitter.distributedlog.exceptions.ReadCancelledException) UnexpectedException(com.twitter.distributedlog.exceptions.UnexpectedException) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) EndOfStreamException(com.twitter.distributedlog.exceptions.EndOfStreamException) IdleReaderException(com.twitter.distributedlog.exceptions.IdleReaderException) DLIllegalStateException(com.twitter.distributedlog.exceptions.DLIllegalStateException) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException) IOException(java.io.IOException)

Example 25 with Promise

use of com.twitter.util.Promise in project distributedlog by twitter.

the class BKDistributedLogManager method getAsyncLogReaderWithLock.

protected Future<AsyncLogReader> getAsyncLogReaderWithLock(final Optional<DLSN> fromDLSN, final Optional<String> subscriberId) {
    if (!fromDLSN.isPresent() && !subscriberId.isPresent()) {
        return Future.exception(new UnexpectedException("Neither from dlsn nor subscriber id is provided."));
    }
    final BKAsyncLogReaderDLSN reader = new BKAsyncLogReaderDLSN(BKDistributedLogManager.this, scheduler, getLockStateExecutor(true), fromDLSN.isPresent() ? fromDLSN.get() : DLSN.InitialDLSN, subscriberId, false, dynConf.getDeserializeRecordSetOnReads(), statsLogger);
    pendingReaders.add(reader);
    final Future<Void> lockFuture = reader.lockStream();
    final Promise<AsyncLogReader> createPromise = new Promise<AsyncLogReader>(new Function<Throwable, BoxedUnit>() {

        @Override
        public BoxedUnit apply(Throwable cause) {
            // cancel the lock when the creation future is cancelled
            lockFuture.cancel();
            return BoxedUnit.UNIT;
        }
    });
    // lock the stream - fetch the last commit position on success
    lockFuture.flatMap(new Function<Void, Future<AsyncLogReader>>() {

        @Override
        public Future<AsyncLogReader> apply(Void complete) {
            if (fromDLSN.isPresent()) {
                return Future.value((AsyncLogReader) reader);
            }
            LOG.info("Reader {} @ {} reading last commit position from subscription store after acquired lock.", subscriberId.get(), name);
            // we acquired lock
            final SubscriptionStateStore stateStore = getSubscriptionStateStore(subscriberId.get());
            return stateStore.getLastCommitPosition().map(new ExceptionalFunction<DLSN, AsyncLogReader>() {

                @Override
                public AsyncLogReader applyE(DLSN lastCommitPosition) throws UnexpectedException {
                    LOG.info("Reader {} @ {} positioned to last commit position {}.", new Object[] { subscriberId.get(), name, lastCommitPosition });
                    reader.setStartDLSN(lastCommitPosition);
                    return reader;
                }
            });
        }
    }).addEventListener(new FutureEventListener<AsyncLogReader>() {

        @Override
        public void onSuccess(AsyncLogReader r) {
            pendingReaders.remove(reader);
            FutureUtils.setValue(createPromise, r);
        }

        @Override
        public void onFailure(final Throwable cause) {
            pendingReaders.remove(reader);
            reader.asyncClose().ensure(new AbstractFunction0<BoxedUnit>() {

                @Override
                public BoxedUnit apply() {
                    FutureUtils.setException(createPromise, cause);
                    return BoxedUnit.UNIT;
                }
            });
        }
    });
    return createPromise;
}
Also used : UnexpectedException(com.twitter.distributedlog.exceptions.UnexpectedException) SubscriptionStateStore(com.twitter.distributedlog.subscription.SubscriptionStateStore) ZKSubscriptionStateStore(com.twitter.distributedlog.subscription.ZKSubscriptionStateStore) AbstractFunction0(scala.runtime.AbstractFunction0) Promise(com.twitter.util.Promise) ExceptionalFunction(com.twitter.util.ExceptionalFunction) Function(com.twitter.util.Function) BoxedUnit(scala.runtime.BoxedUnit)

Aggregations

Promise (com.twitter.util.Promise)48 IOException (java.io.IOException)11 Stat (org.apache.zookeeper.data.Stat)11 Test (org.junit.Test)10 FutureEventListener (com.twitter.util.FutureEventListener)9 ZkVersion (org.apache.bookkeeper.meta.ZkVersion)9 BoxedUnit (scala.runtime.BoxedUnit)9 ZKException (com.twitter.distributedlog.exceptions.ZKException)8 Future (com.twitter.util.Future)8 Versioned (org.apache.bookkeeper.versioning.Versioned)8 Stopwatch (com.google.common.base.Stopwatch)7 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)7 List (java.util.List)7 AsyncCallback (org.apache.zookeeper.AsyncCallback)7 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)6 Transaction (com.twitter.distributedlog.util.Transaction)6 Version (org.apache.bookkeeper.versioning.Version)6 KeeperException (org.apache.zookeeper.KeeperException)6 ByteBuffer (java.nio.ByteBuffer)5 ArrayList (java.util.ArrayList)5