Search in sources :

Example 36 with Promise

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

the class FederatedZKLogMetadataStore method fetchLogLocation.

private Future<Optional<URI>> fetchLogLocation(final String logName) {
    final Promise<Optional<URI>> fetchPromise = new Promise<Optional<URI>>();
    Set<URI> uris = subNamespaces.keySet();
    List<Future<Optional<URI>>> fetchFutures = Lists.newArrayListWithExpectedSize(uris.size());
    for (URI uri : uris) {
        fetchFutures.add(fetchLogLocation(uri, logName));
    }
    Future.collect(fetchFutures).addEventListener(new FutureEventListener<List<Optional<URI>>>() {

        @Override
        public void onSuccess(List<Optional<URI>> fetchResults) {
            Optional<URI> result = Optional.absent();
            for (Optional<URI> fetchResult : fetchResults) {
                if (result.isPresent()) {
                    if (fetchResult.isPresent()) {
                        logger.error("Log {} is found in multiple sub namespaces : {} & {}.", new Object[] { logName, result.get(), fetchResult.get() });
                        duplicatedLogName.compareAndSet(null, logName);
                        duplicatedLogFound.set(true);
                        fetchPromise.setException(new UnexpectedException("Log " + logName + " is found in multiple sub namespaces : " + result.get() + " & " + fetchResult.get()));
                        return;
                    }
                } else {
                    result = fetchResult;
                }
            }
            fetchPromise.setValue(result);
        }

        @Override
        public void onFailure(Throwable cause) {
            fetchPromise.setException(cause);
        }
    });
    return fetchPromise;
}
Also used : UnexpectedException(com.twitter.distributedlog.exceptions.UnexpectedException) Optional(com.google.common.base.Optional) URI(java.net.URI) Promise(com.twitter.util.Promise) Future(com.twitter.util.Future) List(java.util.List)

Example 37 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 38 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 39 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)

Example 40 with Promise

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

the class DistributedLogMultiStreamWriter method write.

public synchronized Future<DLSN> write(ByteBuffer buffer) {
    int logRecordSize = buffer.remaining();
    if (logRecordSize > MAX_LOGRECORD_SIZE) {
        return Future.exception(new LogRecordTooLongException("Log record of size " + logRecordSize + " written when only " + MAX_LOGRECORD_SIZE + " is allowed"));
    }
    // if exceed max number of bytes
    if ((recordSetWriter.getNumBytes() + logRecordSize) > MAX_LOGRECORDSET_SIZE) {
        flush();
    }
    Promise<DLSN> writePromise = new Promise<DLSN>();
    try {
        recordSetWriter.writeRecord(buffer, writePromise);
    } catch (LogRecordTooLongException e) {
        return Future.exception(e);
    } catch (WriteException e) {
        recordSetWriter.abortTransmit(e);
        recordSetWriter = newRecordSetWriter();
        return Future.exception(e);
    }
    if (recordSetWriter.getNumBytes() >= bufferSize) {
        flush();
    }
    return writePromise;
}
Also used : Promise(com.twitter.util.Promise) WriteException(com.twitter.distributedlog.exceptions.WriteException) DLSN(com.twitter.distributedlog.DLSN) LogRecordTooLongException(com.twitter.distributedlog.exceptions.LogRecordTooLongException)

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