Search in sources :

Example 1 with SubscriptionStateStore

use of com.twitter.distributedlog.subscription.SubscriptionStateStore 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)

Example 2 with SubscriptionStateStore

use of com.twitter.distributedlog.subscription.SubscriptionStateStore in project distributedlog by twitter.

the class TestBKDistributedLogManager method testSubscriptionStateStore.

@Test(timeout = 60000)
@Deprecated
public void testSubscriptionStateStore() throws Exception {
    String name = "distrlog-subscription-state";
    String subscriberId = "defaultSubscriber";
    DLSN commitPosition0 = new DLSN(4, 33, 5);
    DLSN commitPosition1 = new DLSN(4, 34, 5);
    DLSN commitPosition2 = new DLSN(5, 34, 5);
    DistributedLogManager dlm = createNewDLM(conf, name);
    SubscriptionStateStore store = dlm.getSubscriptionStateStore(subscriberId);
    assertEquals(Await.result(store.getLastCommitPosition()), DLSN.NonInclusiveLowerBound);
    Await.result(store.advanceCommitPosition(commitPosition1));
    assertEquals(Await.result(store.getLastCommitPosition()), commitPosition1);
    Await.result(store.advanceCommitPosition(commitPosition0));
    assertEquals(Await.result(store.getLastCommitPosition()), commitPosition1);
    Await.result(store.advanceCommitPosition(commitPosition2));
    assertEquals(Await.result(store.getLastCommitPosition()), commitPosition2);
    SubscriptionStateStore store1 = dlm.getSubscriptionStateStore(subscriberId);
    assertEquals(Await.result(store1.getLastCommitPosition()), commitPosition2);
}
Also used : SubscriptionStateStore(com.twitter.distributedlog.subscription.SubscriptionStateStore) Test(org.junit.Test)

Aggregations

SubscriptionStateStore (com.twitter.distributedlog.subscription.SubscriptionStateStore)2 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)1 ZKSubscriptionStateStore (com.twitter.distributedlog.subscription.ZKSubscriptionStateStore)1 ExceptionalFunction (com.twitter.util.ExceptionalFunction)1 Function (com.twitter.util.Function)1 Promise (com.twitter.util.Promise)1 Test (org.junit.Test)1 AbstractFunction0 (scala.runtime.AbstractFunction0)1 BoxedUnit (scala.runtime.BoxedUnit)1