use of com.twitter.util.FutureEventListener in project distributedlog by twitter.
the class AtomicWriter method main.
public static void main(String[] args) throws Exception {
if (args.length < 3) {
System.out.println(HELP);
return;
}
String finagleNameStr = args[0];
String streamName = args[1];
String[] messages = new String[args.length - 2];
System.arraycopy(args, 2, messages, 0, messages.length);
DistributedLogClient client = DistributedLogClientBuilder.newBuilder().clientId(ClientId$.MODULE$.apply("atomic-writer")).name("atomic-writer").thriftmux(true).finagleNameStr(finagleNameStr).build();
final LogRecordSet.Writer recordSetWriter = LogRecordSet.newWriter(16 * 1024, Type.NONE);
List<Future<DLSN>> writeFutures = Lists.newArrayListWithExpectedSize(messages.length);
for (String msg : messages) {
final String message = msg;
ByteBuffer msgBuf = ByteBuffer.wrap(msg.getBytes(UTF_8));
Promise<DLSN> writeFuture = new Promise<DLSN>();
writeFuture.addEventListener(new FutureEventListener<DLSN>() {
@Override
public void onFailure(Throwable cause) {
System.out.println("Encountered error on writing data");
cause.printStackTrace(System.err);
Runtime.getRuntime().exit(0);
}
@Override
public void onSuccess(DLSN dlsn) {
System.out.println("Write '" + message + "' as record " + dlsn);
}
});
recordSetWriter.writeRecord(msgBuf, writeFuture);
writeFutures.add(writeFuture);
}
FutureUtils.result(client.writeRecordSet(streamName, recordSetWriter).addEventListener(new FutureEventListener<DLSN>() {
@Override
public void onFailure(Throwable cause) {
recordSetWriter.abortTransmit(cause);
System.out.println("Encountered error on writing data");
cause.printStackTrace(System.err);
Runtime.getRuntime().exit(0);
}
@Override
public void onSuccess(DLSN dlsn) {
recordSetWriter.completeTransmit(dlsn.getLogSegmentSequenceNo(), dlsn.getEntryId(), dlsn.getSlotId());
}
}));
FutureUtils.result(Future.collect(writeFutures));
client.close();
}
use of com.twitter.util.FutureEventListener in project distributedlog by twitter.
the class StreamRewinder method readLoop.
private static void readLoop(final DistributedLogManager dlm, final int rewindSeconds) throws Exception {
final CountDownLatch keepAliveLatch = new CountDownLatch(1);
long rewindToTxId = System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(rewindSeconds, TimeUnit.SECONDS);
System.out.println("Record records starting from " + rewindToTxId + " which is " + rewindSeconds + " seconds ago");
final AsyncLogReader reader = FutureUtils.result(dlm.openAsyncLogReader(rewindToTxId));
final AtomicBoolean caughtup = new AtomicBoolean(false);
final FutureEventListener<LogRecordWithDLSN> readListener = new FutureEventListener<LogRecordWithDLSN>() {
@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(LogRecordWithDLSN record) {
System.out.println("Received record " + record.getDlsn());
System.out.println("\"\"\"");
System.out.println(new String(record.getPayload(), UTF_8));
System.out.println("\"\"\"");
long diffInMilliseconds = System.currentTimeMillis() - record.getTransactionId();
if (!caughtup.get() && diffInMilliseconds < 2000) {
System.out.println("Reader caught with latest data");
caughtup.set(true);
}
reader.readNext().addEventListener(this);
}
};
reader.readNext().addEventListener(readListener);
keepAliveLatch.await();
FutureUtils.result(reader.asyncClose(), Duration.apply(5, TimeUnit.SECONDS));
}
use of com.twitter.util.FutureEventListener in project distributedlog by twitter.
the class TailReader method readLoop.
private static void readLoop(final DistributedLogManager dlm, final DLSN dlsn) throws Exception {
final CountDownLatch keepAliveLatch = new CountDownLatch(1);
System.out.println("Wait for records starting from " + dlsn);
final AsyncLogReader reader = FutureUtils.result(dlm.openAsyncLogReader(dlsn));
final FutureEventListener<LogRecordWithDLSN> readListener = new FutureEventListener<LogRecordWithDLSN>() {
@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(LogRecordWithDLSN record) {
System.out.println("Received record " + record.getDlsn());
System.out.println("\"\"\"");
System.out.println(new String(record.getPayload(), UTF_8));
System.out.println("\"\"\"");
reader.readNext().addEventListener(this);
}
};
reader.readNext().addEventListener(readListener);
keepAliveLatch.await();
FutureUtils.result(reader.asyncClose(), Duration.apply(5, TimeUnit.SECONDS));
}
use of com.twitter.util.FutureEventListener in project distributedlog by twitter.
the class StreamTransformer method readLoop.
private static void readLoop(final DistributedLogManager dlm, final DLSN fromDLSN, final AsyncLogWriter targetWriter, final Transformer<byte[], byte[]> replicationTransformer) throws Exception {
final CountDownLatch keepAliveLatch = new CountDownLatch(1);
System.out.println("Wait for records starting from " + fromDLSN);
final AsyncLogReader reader = FutureUtils.result(dlm.openAsyncLogReader(fromDLSN));
final FutureEventListener<LogRecordWithDLSN> readListener = new FutureEventListener<LogRecordWithDLSN>() {
@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(LogRecordWithDLSN record) {
if (record.getDlsn().compareTo(fromDLSN) <= 0) {
reader.readNext().addEventListener(this);
return;
}
System.out.println("Received record " + record.getDlsn());
System.out.println("\"\"\"");
System.out.println(new String(record.getPayload(), UTF_8));
System.out.println("\"\"\"");
try {
transform(targetWriter, record, replicationTransformer, keepAliveLatch);
} catch (Exception e) {
System.err.println("Encountered error on transforming record " + record.getDlsn() + " from stream " + dlm.getStreamName());
e.printStackTrace(System.err);
keepAliveLatch.countDown();
}
reader.readNext().addEventListener(this);
}
};
reader.readNext().addEventListener(readListener);
keepAliveLatch.await();
FutureUtils.result(reader.asyncClose(), Duration.apply(5, TimeUnit.SECONDS));
}
use of com.twitter.util.FutureEventListener in project distributedlog by twitter.
the class StreamImpl method acquireStream.
Future<Boolean> acquireStream() {
// Reset this flag so the acquire thread knows whether re-acquire is needed.
writeSinceLastAcquire = false;
final Stopwatch stopwatch = Stopwatch.createStarted();
final Promise<Boolean> acquirePromise = new Promise<Boolean>();
manager.openAsyncLogWriter().addEventListener(FutureUtils.OrderedFutureEventListener.of(new FutureEventListener<AsyncLogWriter>() {
@Override
public void onSuccess(AsyncLogWriter w) {
synchronized (txnLock) {
sequencer.setLastId(w.getLastTxId());
}
AsyncLogWriter oldWriter;
Queue<StreamOp> oldPendingOps;
boolean success;
synchronized (StreamImpl.this) {
oldWriter = setStreamStatus(StreamStatus.INITIALIZED, StreamStatus.INITIALIZING, w, null, null);
oldPendingOps = pendingOps;
pendingOps = new ArrayDeque<StreamOp>();
success = true;
}
// check if the stream is allowed to be acquired
if (!streamManager.allowAcquire(StreamImpl.this)) {
if (null != oldWriter) {
Abortables.asyncAbort(oldWriter, true);
}
int maxAcquiredPartitions = dynConf.getMaxAcquiredPartitionsPerProxy();
StreamUnavailableException sue = new StreamUnavailableException("Stream " + partition.getStream() + " is not allowed to acquire more than " + maxAcquiredPartitions + " partitions");
countException(sue, exceptionStatLogger);
logger.error("Failed to acquire stream {} because it is unavailable : {}", name, sue.getMessage());
synchronized (this) {
oldWriter = setStreamStatus(StreamStatus.ERROR, StreamStatus.INITIALIZED, null, null, sue);
// we don't switch the pending ops since they are already switched
// when setting the status to initialized
success = false;
}
}
processPendingRequestsAfterOpen(success, oldWriter, oldPendingOps);
}
@Override
public void onFailure(Throwable cause) {
AsyncLogWriter oldWriter;
Queue<StreamOp> oldPendingOps;
boolean success;
if (cause instanceof AlreadyClosedException) {
countException(cause, streamExceptionStatLogger);
handleAlreadyClosedException((AlreadyClosedException) cause);
return;
} else if (cause instanceof OwnershipAcquireFailedException) {
OwnershipAcquireFailedException oafe = (OwnershipAcquireFailedException) cause;
logger.warn("Failed to acquire stream ownership for {}, current owner is {} : {}", new Object[] { name, oafe.getCurrentOwner(), oafe.getMessage() });
synchronized (StreamImpl.this) {
oldWriter = setStreamStatus(StreamStatus.BACKOFF, StreamStatus.INITIALIZING, null, oafe.getCurrentOwner(), oafe);
oldPendingOps = pendingOps;
pendingOps = new ArrayDeque<StreamOp>();
success = false;
}
} else if (cause instanceof InvalidStreamNameException) {
InvalidStreamNameException isne = (InvalidStreamNameException) cause;
countException(isne, streamExceptionStatLogger);
logger.error("Failed to acquire stream {} due to its name is invalid", name);
synchronized (StreamImpl.this) {
oldWriter = setStreamStatus(StreamStatus.ERROR, StreamStatus.INITIALIZING, null, null, isne);
oldPendingOps = pendingOps;
pendingOps = new ArrayDeque<StreamOp>();
success = false;
}
} else {
countException(cause, streamExceptionStatLogger);
logger.error("Failed to initialize stream {} : ", name, cause);
synchronized (StreamImpl.this) {
oldWriter = setStreamStatus(StreamStatus.FAILED, StreamStatus.INITIALIZING, null, null, cause);
oldPendingOps = pendingOps;
pendingOps = new ArrayDeque<StreamOp>();
success = false;
}
}
processPendingRequestsAfterOpen(success, oldWriter, oldPendingOps);
}
void processPendingRequestsAfterOpen(boolean success, AsyncLogWriter oldWriter, Queue<StreamOp> oldPendingOps) {
if (success) {
streamAcquireStat.registerSuccessfulEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS));
} else {
streamAcquireStat.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS));
}
for (StreamOp op : oldPendingOps) {
executeOp(op, success);
pendingOpsCounter.dec();
}
Abortables.asyncAbort(oldWriter, true);
FutureUtils.setValue(acquirePromise, success);
}
}, scheduler, getStreamName()));
return acquirePromise;
}
Aggregations