com.twitter.util.Promise Java Examples

The following examples show how to use com.twitter.util.Promise. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FutureUtils.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public static <T> Future<List<Future<T>>> newTFutureList(
        CompletableFuture<List<CompletableFuture<T>>> jFutureList) {
    Promise<List<Future<T>>> promise = new Promise<>();
    jFutureList.whenComplete((value, cause) -> {
        if (null != cause) {
            if (cause instanceof CompletionException) {
                promise.setException(cause.getCause());
            } else {
                promise.setException(cause);
            }
        } else {
            promise.setValue(Lists.transform(
                value,
                future -> newTFuture(future)));
        }
    });
    return promise;
}
 
Example #2
Source File: StreamManagerImpl.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
public Future<Void> createStreamAsync(final String stream) {
    final Promise<Void> createPromise = new Promise<Void>();
    java.util.concurrent.Future<?> scheduleFuture = schedule(new Runnable() {
        @Override
        public void run() {
            try {
                dlNamespace.createLog(stream);
                createPromise.setValue(null);
            } catch (Exception e) {
                createPromise.setException(e);
            }
        }
    }, 0);
    if (null == scheduleFuture) {
        return Future.exception(
            new ServiceUnavailableException("Couldn't schedule a create task."));
    }
    return createPromise;
}
 
Example #3
Source File: StreamImpl.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
Future<Boolean> acquireStream() {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    final Promise<Boolean> acquirePromise = new Promise<Boolean>();
    manager.openAsyncLogWriter().whenCompleteAsync(
        new org.apache.distributedlog.common.concurrent.FutureEventListener<AsyncLogWriter>() {

        @Override
        public void onSuccess(AsyncLogWriter w) {
            onAcquireStreamSuccess(w, stopwatch, acquirePromise);
        }

        @Override
        public void onFailure(Throwable cause) {
            onAcquireStreamFailure(cause, stopwatch, acquirePromise);
        }

    }, scheduler.chooseExecutor(getStreamName()));
    return acquirePromise;
}
 
Example #4
Source File: BKAbstractLogWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private Future<BKLogSegmentWriter> completeOldSegmentAndCacheNewLogSegmentWriter(
        BKLogSegmentWriter oldSegmentWriter,
        final BKLogSegmentWriter newSegmentWriter) {
    final Promise<BKLogSegmentWriter> completePromise = new Promise<BKLogSegmentWriter>();
    // complete the old log segment
    writeHandler.completeAndCloseLogSegment(oldSegmentWriter)
            .addEventListener(new FutureEventListener<LogSegmentMetadata>() {

                @Override
                public void onSuccess(LogSegmentMetadata value) {
                    cacheLogWriter(newSegmentWriter);
                    removeAllocatedLogWriter();
                    FutureUtils.setValue(completePromise, newSegmentWriter);
                }

                @Override
                public void onFailure(Throwable cause) {
                    FutureUtils.setException(completePromise, cause);
                }
            });
    return completePromise;
}
 
Example #5
Source File: BookKeeperClient.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
public Future<LedgerHandle> createLedger(int ensembleSize,
                                         int writeQuorumSize,
                                         int ackQuorumSize) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return Future.exception(ioe);
    }
    final Promise<LedgerHandle> promise = new Promise<LedgerHandle>();
    bk.asyncCreateLedger(ensembleSize, writeQuorumSize, ackQuorumSize,
            BookKeeper.DigestType.CRC32, passwd, new AsyncCallback.CreateCallback() {
                @Override
                public void createComplete(int rc, LedgerHandle lh, Object ctx) {
                    if (BKException.Code.OK == rc) {
                        promise.updateIfEmpty(new Return<LedgerHandle>(lh));
                    } else {
                        promise.updateIfEmpty(new Throw<LedgerHandle>(BKException.create(rc)));
                    }
                }
            }, null);
    return promise;
}
 
Example #6
Source File: BKLogHandler.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
protected Future<List<LogSegmentMetadata>> asyncGetLedgerList(final boolean forceFetch,
                                                                    final boolean fetchFullList,
                                                                    final Comparator<LogSegmentMetadata> comparator,
                                                                    final boolean throwOnEmpty) {
    final Promise<List<LogSegmentMetadata>> promise = new Promise<List<LogSegmentMetadata>>();
    final Stopwatch stopwatch = Stopwatch.createStarted();
    final OpStatsLogger statsLogger = fetchFullList ? getFullListStat : getFilteredListStat;
    asyncDoGetLedgerList(forceFetch, fetchFullList, comparator, throwOnEmpty)
            .addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {
                @Override
                public void onSuccess(List<LogSegmentMetadata> value) {
                    statsLogger.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
                    promise.setValue(value);
                }

                @Override
                public void onFailure(Throwable cause) {
                    statsLogger.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
                    promise.setException(cause);
                }
            });
    return promise;
}
 
Example #7
Source File: EnvelopedRecordSetWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
EnvelopedRecordSetWriter(int initialBufferSize,
                         CompressionCodec.Type codec) {
    this.buffer = new Buffer(Math.max(initialBufferSize, HEADER_LEN));
    this.promiseList = new LinkedList<Promise<DLSN>>();
    this.codec = codec;
    switch (codec) {
        case LZ4:
            this.codecCode = COMPRESSION_CODEC_LZ4;
            break;
        default:
            this.codecCode = COMPRESSION_CODEC_NONE;
            break;
    }
    this.writer = new DataOutputStream(buffer);
    try {
        this.writer.writeInt((VERSION & METADATA_VERSION_MASK)
                | (codecCode & METADATA_COMPRESSION_MASK));
        this.writer.writeInt(0); // count
        this.writer.writeInt(0); // original len
        this.writer.writeInt(0); // actual len
    } catch (IOException e) {
        logger.warn("Failed to serialize the header to an enveloped record set", e);
    }
    this.writeChannel = Channels.newChannel(writer);
}
 
Example #8
Source File: FederatedZKLogMetadataStore.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private <T> Future<T> postStateCheck(Future<T> future) {
    final Promise<T> postCheckedPromise = new Promise<T>();
    future.addEventListener(new FutureEventListener<T>() {
        @Override
        public void onSuccess(T value) {
            if (duplicatedLogFound.get()) {
                postCheckedPromise.setException(new UnexpectedException("Duplicate log found under " + namespace));
            } else {
                postCheckedPromise.setValue(value);
            }
        }

        @Override
        public void onFailure(Throwable cause) {
            postCheckedPromise.setException(cause);
        }
    });
    return postCheckedPromise;
}
 
Example #9
Source File: TestZKLogSegmentMetadataStore.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testStoreMaxLogSegmentSequenceNumber() 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.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);
        }
    });
    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.deserializeLogSegmentSequenceNumber(data));
    assertEquals(1, stat.getVersion());
}
 
Example #10
Source File: FederatedZKLogMetadataStore.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
void doCreateLog(final String logName, final Promise<URI> createPromise) {
    getLogLocation(logName).addEventListener(new FutureEventListener<Optional<URI>>() {
        @Override
        public void onSuccess(Optional<URI> uriOptional) {
            if (uriOptional.isPresent()) {
                createPromise.setException(new LogExistsException("Log " + logName + " already exists in " + uriOptional.get()));
            } else {
                getCachedSubNamespacesAndCreateLog(logName, createPromise);
            }
        }

        @Override
        public void onFailure(Throwable cause) {
            createPromise.setException(cause);
        }
    });
}
 
Example #11
Source File: ZKSessionLock.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Execute a lock action of a given <i>lockEpoch</i> in ordered safe way. If the lock action couln't be
 * executed due to epoch changed, fail the given <i>promise</i> with
 * {@link EpochChangedException}
 *
 * @param lockEpoch
 *          lock epoch
 * @param func
 *          function to execute a lock action
 * @param promise
 *          promise
 */
protected <T> void executeLockAction(final int lockEpoch, final LockAction func, final Promise<T> promise) {
    lockStateExecutor.submit(lockPath, new SafeRunnable() {
        @Override
        public void safeRun() {
            int currentEpoch = ZKSessionLock.this.epoch.get();
            if (currentEpoch == lockEpoch) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("{} executed lock action '{}' under epoch {} for lock {}",
                            new Object[]{lockId, func.getActionName(), lockEpoch, lockPath});
                }
                func.execute();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("{} executed lock action '{}' under epoch {} for lock {}",
                            new Object[]{lockId, func.getActionName(), lockEpoch, lockPath});
                }
            } else {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("{} skipped executing lock action '{}' for lock {}, since epoch is changed from {} to {}.",
                            new Object[]{lockId, func.getActionName(), lockPath, lockEpoch, currentEpoch});
                }
                promise.setException(new EpochChangedException(lockPath, lockEpoch, currentEpoch));
            }
        }
    });
}
 
Example #12
Source File: TestEntry.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testWriteTooLongRecord() throws Exception {
    Writer writer = Entry.newEntry(
            "test-write-too-long-record",
            1024,
            false,
            CompressionCodec.Type.NONE,
            NullStatsLogger.INSTANCE);
    assertEquals("zero bytes", 0, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());

    LogRecord largeRecord = new LogRecord(1L, new byte[MAX_LOGRECORD_SIZE + 1]);
    try {
        writer.writeRecord(largeRecord, new Promise<DLSN>());
        Assert.fail("Should fail on writing large record");
    } catch (LogRecordTooLongException lrtle) {
        // expected
    }
    assertEquals("zero bytes", 0, writer.getNumBytes());
    assertEquals("zero records", 0, writer.getNumRecords());

    Buffer buffer = writer.getBuffer();
    Assert.assertEquals("zero bytes", 0, buffer.size());
}
 
Example #13
Source File: FutureUtils.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Ignore exception from the <i>future</i> and log <i>errorMsg</i> on exceptions
 *
 * @param future the original future
 * @param errorMsg the error message to log on exceptions
 * @return a transformed future ignores exceptions
 */
public static <T> Promise<Void> ignore(Future<T> future, final String errorMsg) {
    final Promise<Void> promise = new Promise<Void>();
    future.addEventListener(new FutureEventListener<T>() {
        @Override
        public void onSuccess(T value) {
            setValue(promise, null);
        }

        @Override
        public void onFailure(Throwable cause) {
            if (null != errorMsg) {
                logger.error(errorMsg, cause);
            }
            setValue(promise, null);
        }
    });
    return promise;
}
 
Example #14
Source File: Utils.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Asynchronously create zookeeper path recursively and optimistically
 *
 * @param zkc Zookeeper client
 * @param pathToCreate  Zookeeper full path
 * @param parentPathShouldNotCreate zookeeper parent path should not be created
 * @param data Zookeeper data
 * @param acl Acl of the zk path
 * @param createMode Create mode of zk path
 */
public static Future<BoxedUnit> zkAsyncCreateFullPathOptimistic(
    final ZooKeeperClient zkc,
    final String pathToCreate,
    final Optional<String> parentPathShouldNotCreate,
    final byte[] data,
    final List<ACL> acl,
    final CreateMode createMode) {
    final Promise<BoxedUnit> result = new Promise<BoxedUnit>();

    zkAsyncCreateFullPathOptimisticRecursive(zkc, pathToCreate, parentPathShouldNotCreate,
            data, acl, createMode, new AsyncCallback.StringCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, String name) {
            handleKeeperExceptionCode(rc, path, result);
        }
    }, result);

    return result;
}
 
Example #15
Source File: BKLogHandler.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
protected Future<List<LogSegmentMetadata>> asyncForceGetLedgerList(final Comparator<LogSegmentMetadata> comparator,
                                                                   final LogSegmentFilter segmentFilter,
                                                                   final boolean throwOnEmpty) {
    final Promise<List<LogSegmentMetadata>> promise = new Promise<List<LogSegmentMetadata>>();
    final Stopwatch stopwatch = Stopwatch.createStarted();
    asyncGetLedgerListWithRetries(comparator, segmentFilter, null)
        .addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {

            @Override
            public void onSuccess(List<LogSegmentMetadata> ledgers) {
                forceGetListStat.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
                if (ledgers.isEmpty() && throwOnEmpty) {
                    promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " is empty"));
                } else {
                    promise.setValue(ledgers);
                }
            }

            @Override
            public void onFailure(Throwable cause) {
                forceGetListStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS));
                promise.setException(cause);
            }
        });
    return promise;
}
 
Example #16
Source File: LedgerHandleCache.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Async try read last confirmed.
 *
 * @param ledgerDesc
 *          ledger descriptor
 * @return future presenting read last confirmed result.
 */
public Future<Long> asyncTryReadLastConfirmed(LedgerDescriptor ledgerDesc) {
    RefCountedLedgerHandle refHandle = handlesMap.get(ledgerDesc);
    if (null == refHandle) {
        LOG.error("Accessing ledger {} without opening.", ledgerDesc);
        return Future.exception(BKException.create(BKException.Code.UnexpectedConditionException));
    }
    final Promise<Long> promise = new Promise<Long>();
    refHandle.handle.asyncTryReadLastConfirmed(new AsyncCallback.ReadLastConfirmedCallback() {
        @Override
        public void readLastConfirmedComplete(int rc, long lastAddConfirmed, Object ctx) {
            if (BKException.Code.OK == rc) {
                promise.setValue(lastAddConfirmed);
            } else {
                promise.setException(BKException.create(rc));
            }
        }
    }, null);
    return promise;
}
 
Example #17
Source File: FutureUtils.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Ignore exception from the <i>future</i> and log <i>errorMsg</i> on exceptions.
 *
 * @param future the original future
 * @param errorMsg the error message to log on exceptions
 * @return a transformed future ignores exceptions
 */
public static <T> Promise<Void> ignore(Future<T> future, final String errorMsg) {
    final Promise<Void> promise = new Promise<Void>();
    future.addEventListener(new FutureEventListener<T>() {
        @Override
        public void onSuccess(T value) {
            setValue(promise, null);
        }

        @Override
        public void onFailure(Throwable cause) {
            if (null != errorMsg) {
                logger.error(errorMsg, cause);
            }
            setValue(promise, null);
        }
    });
    return promise;
}
 
Example #18
Source File: StreamManagerImpl.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Must be enqueued to an executor to avoid deadlocks (close and execute-op both
 * try to acquire the same read-write lock).
 */
@Override
public Future<Void> deleteAndRemoveAsync(final String stream) {
    final Promise<Void> result = new Promise<Void>();
    java.util.concurrent.Future<?> scheduleFuture = schedule(new Runnable() {
        @Override
        public void run() {
            result.become(doDeleteAndRemoveAsync(stream));
        }
    }, 0);
    if (null == scheduleFuture) {
        return Future.exception(
            new ServiceUnavailableException("Couldn't schedule a delete task."));
    }
    return result;
}
 
Example #19
Source File: FutureUtils.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Raise an exception to the <i>promise</i> within a given <i>timeout</i> period.
 * If the promise has been satisfied before raising, it won't change the state of the promise.
 *
 * @param promise promise to raise exception
 * @param timeout timeout period
 * @param unit timeout period unit
 * @param cause cause to raise
 * @param scheduler scheduler to execute raising exception
 * @param key the submit key used by the scheduler
 * @return the promise applied with the raise logic
 */
public static <T> Promise<T> within(final Promise<T> promise,
                                    final long timeout,
                                    final TimeUnit unit,
                                    final Throwable cause,
                                    final OrderedScheduler scheduler,
                                    final Object key) {
    if (timeout < DistributedLogConstants.FUTURE_TIMEOUT_IMMEDIATE || promise.isDefined()) {
        return promise;
    }
    scheduler.schedule(key, new Runnable() {
        @Override
        public void run() {
            logger.info("Raise exception", cause);
            // satisfy the promise
            FutureUtils.setException(promise, cause);
        }
    }, timeout, unit);
    return promise;
}
 
Example #20
Source File: ZKSessionLockFactory.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
public Future<SessionLock> createLock(String lockPath,
                                      DistributedLockContext context) {
    AtomicInteger numRetries = new AtomicInteger(lockCreationRetries);
    final AtomicReference<Throwable> interruptedException = new AtomicReference<Throwable>(null);
    Promise<SessionLock> createPromise =
            new Promise<SessionLock>(new com.twitter.util.Function<Throwable, BoxedUnit>() {
        @Override
        public BoxedUnit apply(Throwable t) {
            interruptedException.set(t);
            return BoxedUnit.UNIT;
        }
    });
    createLock(
            lockPath,
            context,
            interruptedException,
            numRetries,
            createPromise,
            0L);
    return createPromise;
}
 
Example #21
Source File: ZKDistributedLock.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
void interruptTryLock(final Future<LockWaiter> tryLockFuture,
                      final Promise<Void> closePromise) {
    if (null == tryLockFuture) {
        unlockInternalLock(closePromise);
    } else {
        tryLockFuture.addEventListener(OrderedFutureEventListener.of(
                new FutureEventListener<LockWaiter>() {
                    @Override
                    public void onSuccess(LockWaiter waiter) {
                        closeWaiter(waiter, closePromise);
                    }
                    @Override
                    public void onFailure(Throwable cause) {
                        unlockInternalLock(closePromise);
                    }
                }, lockStateExecutor, lockPath));
        FutureUtils.cancel(tryLockFuture);
    }
}
 
Example #22
Source File: TestZKLogSegmentMetadataStore.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #23
Source File: LedgerHandleCache.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Async Read Entries
 *
 * @param ledgerDesc
 *          ledger descriptor
 * @param first
 *          first entry
 * @param last
 *          second entry
 */
public Future<Enumeration<LedgerEntry>> asyncReadEntries(
        LedgerDescriptor ledgerDesc, long first, long last) {
    RefCountedLedgerHandle refHandle = handlesMap.get(ledgerDesc);
    if (null == refHandle) {
        LOG.error("Accessing ledger {} without opening.", ledgerDesc);
        return Future.exception(BKException.create(BKException.Code.UnexpectedConditionException));
    }
    final Promise<Enumeration<LedgerEntry>> promise = new Promise<Enumeration<LedgerEntry>>();
    refHandle.handle.asyncReadEntries(first, last, new AsyncCallback.ReadCallback() {
        @Override
        public void readComplete(int rc, LedgerHandle lh, Enumeration<LedgerEntry> entries, Object ctx) {
            if (BKException.Code.OK == rc) {
                promise.setValue(entries);
            } else {
                promise.setException(BKException.create(rc));
            }
        }
    }, null);
    return promise;
}
 
Example #24
Source File: ReaderWorker.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
void scheduleReinitStream(final int idx, final Promise<Void> promise) {
    executorService.schedule(new Runnable() {
        @Override
        public void run() {
            reinitStream(idx, promise);
        }
    }, BACKOFF_MS, TimeUnit.MILLISECONDS);
}
 
Example #25
Source File: BKLogWriteHandler.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private Future<LogSegmentMetadata> completeLogSegment(LogSegmentMetadata l,
                                                      LogRecordWithDLSN lastRecord) {
    LOG.info("Recovered last record in log segment {} for {}.", l, getFullyQualifiedName());

    long endTxId = DistributedLogConstants.EMPTY_LOGSEGMENT_TX_ID;
    int recordCount = 0;
    long lastEntryId = -1;
    long lastSlotId = -1;

    if (null != lastRecord) {
        endTxId = lastRecord.getTransactionId();
        recordCount = lastRecord.getLastPositionWithinLogSegment();
        lastEntryId = lastRecord.getDlsn().getEntryId();
        lastSlotId = lastRecord.getDlsn().getSlotId();
    }

    if (endTxId == DistributedLogConstants.INVALID_TXID) {
        LOG.error("Unrecoverable corruption has occurred in segment "
            + l.toString() + " at path " + l.getZkPath()
            + ". Unable to continue recovery.");
        return Future.exception(new IOException("Unrecoverable corruption,"
            + " please check logs."));
    } else if (endTxId == DistributedLogConstants.EMPTY_LOGSEGMENT_TX_ID) {
        // TODO: Empty ledger - Ideally we should just remove it?
        endTxId = l.getFirstTxId();
    }

    Promise<LogSegmentMetadata> promise = new Promise<LogSegmentMetadata>();
    doCompleteAndCloseLogSegment(
            l.getZNodeName(),
            l.getLogSegmentSequenceNumber(),
            l.getLedgerId(),
            l.getFirstTxId(),
            endTxId,
            recordCount,
            lastEntryId,
            lastSlotId,
            promise);
    return promise;
}
 
Example #26
Source File: EnvelopedEntryWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void writeRecord(LogRecord record,
                                     Promise<DLSN> transmitPromise)
        throws LogRecordTooLongException, WriteException {
    int logRecordSize = record.getPersistentSize();
    if (logRecordSize > MAX_LOGRECORD_SIZE) {
        throw new LogRecordTooLongException(
                "Log Record of size " + logRecordSize + " written when only "
                        + MAX_LOGRECORD_SIZE + " is allowed");
    }

    try {
        this.writer.writeOp(record);
        int numRecords = 1;
        if (!record.isControl()) {
            hasUserData = true;
        }
        if (record.isRecordSet()) {
            numRecords = LogRecordSet.numRecords(record);
        }
        count += numRecords;
        writeRequests.add(new WriteRequest(numRecords, transmitPromise));
        maxTxId = Math.max(maxTxId, record.getTransactionId());
    } catch (IOException e) {
        logger.error("Failed to append record to record set of {} : ",
                logName, e);
        throw new WriteException(logName, "Failed to append record to record set of "
                + logName);
    }
}
 
Example #27
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String mapPromise() throws Exception {
  Promise<String> p = new Promise<String>();
  Future<String> f = p.map(mapF);
  p.setValue(string);
  return Await.result(f);
}
 
Example #28
Source File: ZKDistributedLock.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
void doInternalReacquireLock(final AtomicInteger numRetries,
                             final long lockTimeout,
                             final Promise<ZKDistributedLock> reacquirePromise) {
    internalTryRetries.inc();
    Promise<ZKDistributedLock> tryPromise = new Promise<ZKDistributedLock>();
    tryPromise.addEventListener(new FutureEventListener<ZKDistributedLock>() {
        @Override
        public void onSuccess(ZKDistributedLock lock) {
            FutureUtils.setValue(reacquirePromise, lock);
        }

        @Override
        public void onFailure(Throwable cause) {
            if (cause instanceof OwnershipAcquireFailedException) {
                // the lock has been acquired by others
                FutureUtils.setException(reacquirePromise, cause);
            } else {
                if (numRetries.getAndDecrement() > 0 && !closed) {
                    internalReacquireLock(numRetries, lockTimeout, reacquirePromise);
                } else {
                    FutureUtils.setException(reacquirePromise, cause);
                }
            }
        }
    });
    doAsyncAcquireWithSemaphore(tryPromise, 0);
}
 
Example #29
Source File: BKLogSegmentWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
synchronized public Future<DLSN> writeInternal(LogRecord record)
        throws LogRecordTooLongException, LockingException, BKTransmitException,
               WriteException, InvalidEnvelopedEntryException {
    int logRecordSize = record.getPersistentSize();

    if (logRecordSize > MAX_LOGRECORD_SIZE) {
        throw new LogRecordTooLongException(String.format(
                "Log Record of size %d written when only %d is allowed",
                logRecordSize, MAX_LOGRECORD_SIZE));
    }

    // If we will exceed the max number of bytes allowed per entry
    // initiate a transmit before accepting the new log record
    if ((recordSetWriter.getNumBytes() + logRecordSize) > MAX_LOGRECORDSET_SIZE) {
        checkStateAndTransmit();
    }

    checkWriteLock();

    if (enableRecordCounts) {
        // Set the count here. The caller would appropriately increment it
        // if this log record is to be counted
        record.setPositionWithinLogSegment(positionWithinLogSegment);
    }

    Promise<DLSN> writePromise = new Promise<DLSN>();
    writePromise.addEventListener(new OpStatsListener<DLSN>(writeTime));
    recordSetWriter.writeRecord(record, writePromise);

    if (record.getTransactionId() < lastTxId) {
        LOG.info("Log Segment {} TxId decreased Last: {} Record: {}",
                new Object[] {fullyQualifiedLogSegment, lastTxId, record.getTransactionId()});
    }
    if (!record.isControl()) {
        // only update last tx id for user records
        lastTxId = record.getTransactionId();
        outstandingBytes += (20 + record.getPayload().length);
    }
    return writePromise;
}
 
Example #30
Source File: BookKeeperClient.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
public Future<Void> deleteLedger(long lid,
                                 final boolean ignoreNonExistentLedger) {
    BookKeeper bk;
    try {
        bk = get();
    } catch (IOException ioe) {
        return Future.exception(ioe);
    }
    final Promise<Void> promise = new Promise<Void>();
    bk.asyncDeleteLedger(lid, new AsyncCallback.DeleteCallback() {
        @Override
        public void deleteComplete(int rc, Object ctx) {
            if (BKException.Code.OK == rc) {
                promise.updateIfEmpty(new Return<Void>(null));
            } else if (BKException.Code.NoSuchLedgerExistsException == rc) {
                if (ignoreNonExistentLedger) {
                    promise.updateIfEmpty(new Return<Void>(null));
                } else {
                    promise.updateIfEmpty(new Throw<Void>(BKException.create(rc)));
                }
            } else {
                promise.updateIfEmpty(new Throw<Void>(BKException.create(rc)));
            }
        }
    }, null);
    return promise;
}