Search in sources :

Example 46 with Promise

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

the class FederatedZKLogMetadataStore method createSubNamespace.

Future<URI> createSubNamespace() {
    final Promise<URI> promise = new Promise<URI>();
    final String nsPath = namespace.getPath() + "/" + ZNODE_SUB_NAMESPACES + "/" + SUB_NAMESPACE_PREFIX;
    try {
        zkc.get().create(nsPath, new byte[0], zkc.getDefaultACL(), CreateMode.PERSISTENT_SEQUENTIAL, new AsyncCallback.StringCallback() {

            @Override
            public void processResult(int rc, String path, Object ctx, String name) {
                if (Code.OK.intValue() == rc) {
                    try {
                        URI newUri = getSubNamespaceURI(getNamespaceFromZkPath(name));
                        logger.info("Created sub namespace {}", newUri);
                        promise.setValue(newUri);
                    } catch (UnexpectedException ue) {
                        promise.setException(ue);
                    } catch (URISyntaxException e) {
                        promise.setException(new UnexpectedException("Invalid namespace " + name + " is created."));
                    }
                } else {
                    promise.setException(KeeperException.create(Code.get(rc)));
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    } catch (InterruptedException e) {
        promise.setException(e);
    }
    return promise;
}
Also used : UnexpectedException(com.twitter.distributedlog.exceptions.UnexpectedException) AsyncCallback(org.apache.zookeeper.AsyncCallback) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Promise(com.twitter.util.Promise) ZooKeeperClient(com.twitter.distributedlog.ZooKeeperClient)

Example 47 with Promise

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

the class SimpleLedgerAllocator method createAllocationData.

private static Future<Versioned<byte[]>> createAllocationData(final String allocatePath, final ZooKeeperClient zkc) {
    try {
        final Promise<Versioned<byte[]>> promise = new Promise<Versioned<byte[]>>();
        zkc.get().create(allocatePath, DistributedLogConstants.EMPTY_BYTES, zkc.getDefaultACL(), CreateMode.PERSISTENT, new org.apache.zookeeper.AsyncCallback.Create2Callback() {

            @Override
            public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
                if (KeeperException.Code.OK.intValue() == rc) {
                    promise.setValue(new Versioned<byte[]>(DistributedLogConstants.EMPTY_BYTES, new ZkVersion(stat.getVersion())));
                } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                    Utils.zkGetData(zkc, allocatePath, false).proxyTo(promise);
                } else {
                    promise.setException(FutureUtils.zkException(KeeperException.create(KeeperException.Code.get(rc)), allocatePath));
                }
            }
        }, null);
        return promise;
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        return Future.exception(FutureUtils.zkException(e, allocatePath));
    } catch (InterruptedException e) {
        return Future.exception(FutureUtils.zkException(e, allocatePath));
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) Promise(com.twitter.util.Promise) Stat(org.apache.zookeeper.data.Stat) ZooKeeperClient(com.twitter.distributedlog.ZooKeeperClient) ZkVersion(org.apache.bookkeeper.meta.ZkVersion)

Example 48 with Promise

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

the class ReadUtils method asyncReadRecord.

private static Future<LogRecordWithDLSN> asyncReadRecord(final String streamName, final LogSegmentMetadata l, final boolean fence, final boolean includeControl, final boolean includeEndOfStream, final int scanStartBatchSize, final int scanMaxBatchSize, final AtomicInteger numRecordsScanned, final ExecutorService executorService, final LedgerHandleCache handleCache, final LogRecordSelector selector, final boolean backward, final long startEntryId) {
    final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>();
    FutureEventListener<LedgerDescriptor> openLedgerListener = new FutureEventListener<LedgerDescriptor>() {

        @Override
        public void onSuccess(final LedgerDescriptor ledgerDescriptor) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("{} Opened logsegment {} for reading record", streamName, l);
            }
            promise.ensure(new AbstractFunction0<BoxedUnit>() {

                @Override
                public BoxedUnit apply() {
                    handleCache.asyncCloseLedger(ledgerDescriptor);
                    return BoxedUnit.UNIT;
                }
            });
            if (LOG.isDebugEnabled()) {
                LOG.debug("{} {} scanning {}.", new Object[] { (backward ? "backward" : "forward"), streamName, l });
            }
            asyncReadRecordFromLogSegment(streamName, ledgerDescriptor, handleCache, l, executorService, scanStartBatchSize, scanMaxBatchSize, includeControl, includeEndOfStream, promise, numRecordsScanned, selector, backward, startEntryId);
        }

        @Override
        public void onFailure(final Throwable cause) {
            String errMsg = "Error opening log segment [" + l + "] for reading record of " + streamName;
            promise.setException(new IOException(errMsg, BKException.create(FutureUtils.bkResultCode(cause))));
        }
    };
    handleCache.asyncOpenLedger(l, fence).addEventListener(FutureEventListenerRunnable.of(openLedgerListener, executorService));
    return promise;
}
Also used : Promise(com.twitter.util.Promise) FutureEventListener(com.twitter.util.FutureEventListener) BoxedUnit(scala.runtime.BoxedUnit) IOException(java.io.IOException)

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