com.twitter.util.FutureEventListener Java Examples

The following examples show how to use com.twitter.util.FutureEventListener. 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: DistributedTranslog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void truncateLogBeforeDLSN(final DLSN dlsn) {
    if (dlsn.getLogSegmentSequenceNo() <= truncatedDlsn.getLogSegmentSequenceNo()) {
        logger.info("previous truncate dlsn is [{}], current dlsn is [{}], segment no not change not call truncate", truncatedDlsn, dlsn);
        return;
    }
    if (this.logWriter == null) {
        logger.error("log writer is closed, maybe not primary any more, skip truncate");
        return;
    }
    this.logWriter.truncate(dlsn).addEventListener(new FutureEventListener<Boolean>() {

        @Override
        public void onFailure(Throwable t) {
            logger.error("errors while truncate log after DLSN [{}]", t, dlsn);
        }

        @Override
        public void onSuccess(Boolean isSuccess) {
            if (isSuccess) {
                truncatedDlsn = dlsn;
                logger.info("truncate log before [{}] successfully", dlsn);
            }
        }
    });
}
 
Example #2
Source File: ZKDistributedLock.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
void closeWaiter(final LockWaiter waiter,
                 final Promise<Void> closePromise) {
    if (null == waiter) {
        interruptTryLock(tryLockFuture, closePromise);
    } else {
        waiter.getAcquireFuture().addEventListener(OrderedFutureEventListener.of(
                new FutureEventListener<Boolean>() {
                    @Override
                    public void onSuccess(Boolean value) {
                        unlockInternalLock(closePromise);
                    }
                    @Override
                    public void onFailure(Throwable cause) {
                        unlockInternalLock(closePromise);
                    }
                }, lockStateExecutor, lockPath));
        FutureUtils.cancel(waiter.getAcquireFuture());
    }
}
 
Example #3
Source File: ReaderWorker.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    final DLSN dlsnToTruncate = prevDLSN;
    if (null == dlsnToTruncate) {
        return;
    }
    final Stopwatch stopwatch = Stopwatch.createStarted();
    dlc.truncate(streamName, dlsnToTruncate).addEventListener(
            new FutureEventListener<Boolean>() {
                @Override
                public void onSuccess(Boolean value) {
                    truncationStat.registerSuccessfulEvent(
                      stopwatch.stop().elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
                }

                @Override
                public void onFailure(Throwable cause) {
                    truncationStat.registerFailedEvent(
                      stopwatch.stop().elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
                    LOG.error("Failed to truncate stream {} to {} : ",
                            new Object[]{streamName, dlsnToTruncate, cause});
                }
            });
}
 
Example #4
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 #5
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 #6
Source File: ZKDistributedLock.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
void waitForAcquire(final LockWaiter waiter,
                    final Promise<ZKDistributedLock> acquirePromise) {
    waiter.getAcquireFuture().addEventListener(OrderedFutureEventListener.of(
            new FutureEventListener<Boolean>() {
                @Override
                public void onSuccess(Boolean acquired) {
                    LOG.info("{} acquired lock {}", waiter, lockPath);
                    if (acquired) {
                        FutureUtils.setValue(acquirePromise, ZKDistributedLock.this);
                    } else {
                        FutureUtils.setException(acquirePromise,
                                new OwnershipAcquireFailedException(lockPath, waiter.getCurrentOwner()));
                    }
                }

                @Override
                public void onFailure(Throwable cause) {
                    FutureUtils.setException(acquirePromise, cause);
                }
            }, lockStateExecutor, lockPath));
}
 
Example #7
Source File: ProxyClientManager.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Handshake with a given proxy
 *
 * @param address
 *          proxy address
 * @param sc
 *          proxy client
 * @param listener
 *          listener on handshake result
 */
private void handshake(SocketAddress address,
                       ProxyClient sc,
                       FutureEventListener<ServerInfo> listener,
                       boolean logging,
                       boolean getOwnerships) {
    if (clientConfig.getHandshakeWithClientInfo()) {
        ClientInfo clientInfo = new ClientInfo();
        clientInfo.setGetOwnerships(getOwnerships);
        clientInfo.setStreamNameRegex(clientConfig.getStreamNameRegex());
        if (logging) {
            logger.info("Handshaking with {} : {}", address, clientInfo);
        }
        sc.getService().handshakeWithClientInfo(clientInfo)
                .addEventListener(listener);
    } else {
        if (logging) {
            logger.info("Handshaking with {}", address);
        }
        sc.getService().handshake().addEventListener(listener);
    }
}
 
Example #8
Source File: MultiReader.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private static void readLoop(final AsyncLogReader reader,
                             final CountDownLatch keepAliveLatch) {
    final FutureEventListener<LogRecordWithDLSN> readListener = new FutureEventListener<LogRecordWithDLSN>() {
        @Override
        public void onFailure(Throwable cause) {
            System.err.println("Encountered error on reading records from stream " + reader.getStreamName());
            cause.printStackTrace(System.err);
            keepAliveLatch.countDown();
        }

        @Override
        public void onSuccess(LogRecordWithDLSN record) {
            System.out.println("Received record " + record.getDlsn() + " from stream " + reader.getStreamName());
            System.out.println("\"\"\"");
            System.out.println(new String(record.getPayload(), UTF_8));
            System.out.println("\"\"\"");
            reader.readNext().addEventListener(this);
        }
    };
    reader.readNext().addEventListener(readListener);
}
 
Example #9
Source File: DLFutureRecordMetadata.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
DLFutureRecordMetadata(final String topic,
                       com.twitter.util.Future<DLSN> dlsnFuture,
                       final Callback callback) {
    this.topic = topic;
    this.dlsnFuture = dlsnFuture;
    this.callback = callback;

    this.dlsnFuture.addEventListener(new FutureEventListener<DLSN>() {
        @Override
        public void onFailure(Throwable cause) {
            callback.onCompletion(null, new IOException(cause));
        }

        @Override
        public void onSuccess(DLSN value) {
            callback.onCompletion(new RecordMetadata(new TopicPartition(topic, 0), -1L, -1L), null);
        }
    });
}
 
Example #10
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 #11
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 #12
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 #13
Source File: DistributedLogClientImpl.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Send the stream operation by routing service, excluding previous address if it is not null.
 *
 * @param op
 *          stream operation.
 * @param previousAddr
 *          previous tried address.
 */
private void doSend(final StreamOp op, final SocketAddress previousAddr) {
    if (null != previousAddr) {
        op.routingContext.addTriedHost(previousAddr, StatusCode.WRITE_EXCEPTION);
    }
    // Get host first
    final SocketAddress address = ownershipCache.getOwner(op.stream);
    if (null == address || op.routingContext.isTriedHost(address)) {
        getOwner(op).addEventListener(new FutureEventListener<SocketAddress>() {
            @Override
            public void onFailure(Throwable cause) {
                op.fail(null, cause);
            }

            @Override
            public void onSuccess(SocketAddress ownerAddr) {
                op.send(ownerAddr);
            }
        });
    } else {
        op.send(address);
    }
}
 
Example #14
Source File: ReaderWorker.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    final DLSN dlsnToTruncate = prevDLSN;
    if (null == dlsnToTruncate) {
        return;
    }
    final Stopwatch stopwatch = Stopwatch.createStarted();
    dlc.truncate(streamName, dlsnToTruncate).addEventListener(
            new FutureEventListener<Boolean>() {
                @Override
                public void onSuccess(Boolean value) {
                    truncationStat.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
                }

                @Override
                public void onFailure(Throwable cause) {
                    truncationStat.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
                    LOG.error("Failed to truncate stream {} to {} : ",
                            new Object[]{streamName, dlsnToTruncate, cause});
                }
            });
}
 
Example #15
Source File: ProxyClientManager.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Create a client to proxy <code>address</code>.
 *
 * @param address
 *          proxy address
 * @return proxy client
 */
public ProxyClient createClient(final SocketAddress address) {
    final ProxyClient sc = clientBuilder.build(address);
    ProxyClient oldSC = address2Services.putIfAbsent(address, sc);
    if (null != oldSC) {
        sc.close();
        return oldSC;
    } else {
        final Stopwatch stopwatch = Stopwatch.createStarted();
        FutureEventListener<ServerInfo> listener = new FutureEventListener<ServerInfo>() {
            @Override
            public void onSuccess(ServerInfo serverInfo) {
                notifyHandshakeSuccess(address, sc, serverInfo, true, stopwatch);
            }
            @Override
            public void onFailure(Throwable cause) {
                notifyHandshakeFailure(address, sc, cause, stopwatch);
            }
        };
        // send a ping messaging after creating connections.
        handshake(address, sc, listener, true, true);
        return sc;
    }
}
 
Example #16
Source File: ZKAccessControlManager.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private void fetchDefaultAccessControlEntry(final Promise<ZKAccessControl> promise) {
    ZKAccessControl.read(zkc, zkRootPath, this)
        .addEventListener(new FutureEventListener<ZKAccessControl>() {
            @Override
            public void onSuccess(ZKAccessControl accessControl) {
                logger.info("Default Access Control will be changed from {} to {}",
                            ZKAccessControlManager.this.defaultAccessControl,
                            accessControl);
                ZKAccessControlManager.this.defaultAccessControl = accessControl;
                promise.setValue(accessControl);
            }

            @Override
            public void onFailure(Throwable cause) {
                if (cause instanceof KeeperException.NoNodeException) {
                    logger.info("Default Access Control is missing, creating one for {} ...", zkRootPath);
                    createDefaultAccessControlEntryIfNeeded(promise);
                } else {
                    promise.setException(cause);
                }
            }
        });
}
 
Example #17
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 #18
Source File: MultiReader.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private static void readLoop(final DistributedLogManager dlm,
                             final DLSN dlsn,
                             final CountDownLatch keepAliveLatch) {
    System.out.println("Wait for records from " + dlm.getStreamName() + " starting from " + dlsn);
    dlm.openAsyncLogReader(dlsn).addEventListener(new FutureEventListener<AsyncLogReader>() {
        @Override
        public void onFailure(Throwable cause) {
            System.err.println("Encountered error on reading records from stream " + dlm.getStreamName());
            cause.printStackTrace(System.err);
            keepAliveLatch.countDown();
        }

        @Override
        public void onSuccess(AsyncLogReader reader) {
            System.out.println("Open reader to read records from stream " + reader.getStreamName());
            readLoop(reader, keepAliveLatch);
        }
    });
}
 
Example #19
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 #20
Source File: StreamImpl.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
    // acquire the stream
    acquireStream().addEventListener(new FutureEventListener<Boolean>() {
            @Override
            public void onSuccess(Boolean success) {
                if (!success) {
                    // failed to acquire the stream. set the stream in error status and close it.
                    setStreamInErrorStatus();
                    requestClose("Failed to acquire the ownership");
                }
            }

            @Override
            public void onFailure(Throwable cause) {
                // unhandled exceptions
                logger.error("Stream {} threw unhandled exception : ", name, cause);
                // failed to acquire the stream. set the stream in error status and close it.
                setStreamInErrorStatus();
                requestClose("Unhandled exception");
            }
        });
}
 
Example #21
Source File: AbstractStreamOp.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
public Future<Void> execute(AsyncLogWriter writer, Sequencer sequencer, Object txnLock) {
    stopwatch.reset().start();
    return executeOp(writer, sequencer, txnLock)
            .addEventListener(new FutureEventListener<Response>() {
        @Override
        public void onSuccess(Response response) {
            opStatsLogger.registerSuccessfulEvent(
              stopwatch.elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
            setResponse(response);
        }
        @Override
        public void onFailure(Throwable cause) {
        }
    }).voided();
}
 
Example #22
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 #23
Source File: FederatedZKLogMetadataStore.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private void getCachedSubNamespacesAndCreateLog(final String logName,
                                                final Promise<URI> createPromise) {
    getCachedSubNamespaces().addEventListener(new FutureEventListener<Set<URI>>() {
        @Override
        public void onSuccess(Set<URI> uris) {
            findSubNamespaceToCreateLog(logName, uris, createPromise);
        }

        @Override
        public void onFailure(Throwable cause) {
            createPromise.setException(cause);
        }
    });
}
 
Example #24
Source File: StreamTransformer.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private static void transform(final AsyncLogWriter writer,
                              LogRecordWithDLSN record,
                              Transformer<byte[], byte[]> replicationTransformer,
                              final CountDownLatch keepAliveLatch)
        throws Exception {
    DLSN srcDLSN = record.getDlsn();
    byte[] payload = record.getPayload();
    byte[] transformedPayload = replicationTransformer.transform(payload);
    TransformedRecord transformedRecord =
            new TransformedRecord(ByteBuffer.wrap(transformedPayload));
    transformedRecord.setSrcDlsn(srcDLSN.serializeBytes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
    transformedRecord.write(protocolFactory.getProtocol(new TIOStreamTransport(baos)));
    byte[] data = baos.toByteArray();
    writer.write(new LogRecord(record.getSequenceId(), data))
            .addEventListener(new FutureEventListener<DLSN>() {
        @Override
        public void onFailure(Throwable cause) {
            System.err.println("Encountered error on writing records to stream " + writer.getStreamName());
            cause.printStackTrace(System.err);
            keepAliveLatch.countDown();
        }

        @Override
        public void onSuccess(DLSN dlsn) {
            System.out.println("Write transformed record " + dlsn);
        }
    });
}
 
Example #25
Source File: FederatedZKLogMetadataStore.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #26
Source File: ReadAheadWorker.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private void issueReadLastConfirmedAndEntry(final boolean parallel,
                                            final long lastAddConfirmed) {
    final String ctx = String.format("ReadLastConfirmedAndEntry(%s, %d)", parallel? "Parallel":"Sequential", lastAddConfirmed);
    final ReadLastConfirmedAndEntryCallbackWithNotification callback =
        new ReadLastConfirmedAndEntryCallbackWithNotification(lastAddConfirmed, this, ctx);
    boolean callbackImmediately = setMetadataNotification(callback);
    handleCache.asyncReadLastConfirmedAndEntry(
            currentLH,
            nextReadAheadPosition.getEntryId(),
            conf.getReadLACLongPollTimeout(),
            parallel
    ).addEventListener(new FutureEventListener<Pair<Long, LedgerEntry>>() {
        @Override
        public void onSuccess(Pair<Long, LedgerEntry> lacAndEntry) {
            callback.readLastConfirmedAndEntryComplete(
                    BKException.Code.OK,
                    lacAndEntry.getLeft(),
                    lacAndEntry.getRight(),
                    ctx);
        }

        @Override
        public void onFailure(Throwable cause) {
            callback.readLastConfirmedAndEntryComplete(
                    FutureUtils.bkResultCode(cause),
                    lastAddConfirmed,
                    null,
                    ctx);
        }
    });
    callback.callbackImmediately(callbackImmediately);
    readAheadReadLACAndEntryCounter.inc();
}
 
Example #27
Source File: ZKDistributedLock.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
void doAsyncAcquire(final Promise<ZKDistributedLock> acquirePromise,
                    final long lockTimeout) {
    LOG.trace("Async Lock Acquire {}", lockPath);
    try {
        checkLockState();
    } catch (IOException ioe) {
        FutureUtils.setException(acquirePromise, ioe);
        return;
    }

    if (haveLock()) {
        // it already hold the lock
        FutureUtils.setValue(acquirePromise, this);
        return;
    }

    lockFactory.createLock(lockPath, lockContext).addEventListener(OrderedFutureEventListener.of(
            new FutureEventListener<SessionLock>() {
        @Override
        public void onSuccess(SessionLock lock) {
            synchronized (ZKDistributedLock.this) {
                if (closed) {
                    LOG.info("Skipping tryLocking lock {} since it is already closed", lockPath);
                    FutureUtils.setException(acquirePromise, newLockClosedException());
                    return;
                }
            }
            synchronized (ZKDistributedLock.this) {
                internalLock = lock;
                internalLock.setLockListener(ZKDistributedLock.this);
            }
            asyncTryLock(lock, acquirePromise, lockTimeout);
        }

        @Override
        public void onFailure(Throwable cause) {
            FutureUtils.setException(acquirePromise, cause);
        }
    }, lockStateExecutor, lockPath));
}
 
Example #28
Source File: BKLogSegmentWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private void abortTransmitPacketOnClose(final boolean abort,
                                        final AtomicReference<Throwable> throwExc,
                                        final Promise<Void> closePromise) {
    LOG.info("Closing BKPerStreamLogWriter (abort={}) for {} :" +
                    " lastDLSN = {} outstandingTransmits = {} writesPendingTransmit = {} addCompletesPending = {}",
            new Object[]{abort, fullyQualifiedLogSegment, getLastDLSN(),
                    outstandingTransmits.get(), getWritesPendingTransmit(), getPendingAddCompleteCount()});

    // Save the current packet to reset, leave a new empty packet to avoid a race with
    // addCompleteDeferredProcessing.
    final BKTransmitPacket packetPreviousSaved;
    final BKTransmitPacket packetCurrentSaved;
    synchronized (this) {
        packetPreviousSaved = packetPrevious;
        packetCurrentSaved = new BKTransmitPacket(recordSetWriter);
        recordSetWriter = newRecordSetWriter();
    }

    // Once the last packet been transmitted, apply any remaining promises asynchronously
    // to avoid blocking close if bk client is slow for some reason.
    if (null != packetPreviousSaved) {
        packetPreviousSaved.addTransmitCompleteListener(new FutureEventListener<Integer>() {
            @Override
            public void onSuccess(Integer transmitResult) {
                flushAddCompletes();
                abortPacket(packetCurrentSaved);
            }
            @Override
            public void onFailure(Throwable cause) {
                LOG.error("Unexpected error on transmit completion ", cause);
            }
        });
    } else {
        // In this case there are no pending add completes, but we still need to abort the
        // current packet.
        abortPacket(packetCurrentSaved);
    }
    closeLedgerOnClose(abort, throwExc, closePromise);
}
 
Example #29
Source File: BKLogSegmentWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private void closeInternal(final boolean abort,
                           final AtomicReference<Throwable> throwExc,
                           final Promise<Void> closePromise) {
    // Cancel the periodic flush schedule first
    // The task is allowed to exit gracefully
    if (null != periodicFlushSchedule) {
        // we don't need to care about the cancel result here. if the periodicl flush task couldn't
        // be cancelled, it means that it is doing flushing. So following flushes would be synchronized
        // to wait until background flush completed.
        if (!periodicFlushSchedule.cancel(false)) {
            LOG.info("Periodic flush for log segment {} isn't cancelled.", getFullyQualifiedLogSegment());
        }
    }

    // If it is a normal close and the stream isn't in an error state, we attempt to flush any buffered data
    if (!abort && !isLogSegmentInError()) {
        this.enforceLock = false;
        LOG.info("Flushing before closing log segment {}", getFullyQualifiedLogSegment());
        flushAndCommit().addEventListener(new FutureEventListener<Long>() {
            @Override
            public void onSuccess(Long value) {
                abortTransmitPacketOnClose(abort, throwExc, closePromise);
            }

            @Override
            public void onFailure(Throwable cause) {
                throwExc.set(cause);
                abortTransmitPacketOnClose(abort, throwExc, closePromise);
            }
        });
    } else {
        abortTransmitPacketOnClose(abort, throwExc, closePromise);
    }

}
 
Example #30
Source File: TestFutureUtils.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testWithin() throws Exception {
    OrderedScheduler scheduler = OrderedScheduler.newBuilder()
            .corePoolSize(1)
            .name("test-within")
            .build();
    final Promise<Void> promiseToTimeout = new Promise<Void>();
    final Promise<Void> finalPromise = new Promise<Void>();
    FutureUtils.within(
            promiseToTimeout,
            10,
            TimeUnit.MILLISECONDS,
            new TestException(),
            scheduler,
            "test-within"
    ).addEventListener(new FutureEventListener<Void>() {
        @Override
        public void onFailure(Throwable cause) {
            FutureUtils.setException(finalPromise, cause);
        }

        @Override
        public void onSuccess(Void value) {
            FutureUtils.setValue(finalPromise, value);
        }
    });
    try {
        FutureUtils.result(finalPromise);
        fail("Should fail with TestException");
    } catch (TestException te) {
        // expected
    }
}