use of com.twitter.distributedlog.exceptions.LogEmptyException in project distributedlog by twitter.
the class BKLogHandler method asyncGetFirstLogRecord.
public Future<LogRecordWithDLSN> asyncGetFirstLogRecord() {
final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>();
checkLogStreamExistsAsync().addEventListener(new FutureEventListener<Void>() {
@Override
public void onSuccess(Void value) {
asyncGetFullLedgerList(true, true).addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {
@Override
public void onSuccess(List<LogSegmentMetadata> ledgerList) {
if (ledgerList.isEmpty()) {
promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
return;
}
Future<LogRecordWithDLSN> firstRecord = null;
for (LogSegmentMetadata ledger : ledgerList) {
if (!ledger.isTruncated() && (ledger.getRecordCount() > 0 || ledger.isInProgress())) {
firstRecord = asyncReadFirstUserRecord(ledger, DLSN.InitialDLSN);
break;
}
}
if (null != firstRecord) {
promise.become(firstRecord);
} else {
promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
}
}
@Override
public void onFailure(Throwable cause) {
promise.setException(cause);
}
});
}
@Override
public void onFailure(Throwable cause) {
promise.setException(cause);
}
});
return promise;
}
use of com.twitter.distributedlog.exceptions.LogEmptyException in project distributedlog by twitter.
the class BKLogHandler method getLastLogRecordAsync.
public Future<LogRecordWithDLSN> getLastLogRecordAsync(final boolean recover, final boolean includeEndOfStream) {
final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>();
checkLogStreamExistsAsync().addEventListener(new FutureEventListener<Void>() {
@Override
public void onSuccess(Void value) {
asyncGetFullLedgerListDesc(true, true).addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {
@Override
public void onSuccess(List<LogSegmentMetadata> ledgerList) {
if (ledgerList.isEmpty()) {
promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
return;
}
asyncGetLastLogRecord(ledgerList.iterator(), promise, recover, false, includeEndOfStream);
}
@Override
public void onFailure(Throwable cause) {
promise.setException(cause);
}
});
}
@Override
public void onFailure(Throwable cause) {
promise.setException(cause);
}
});
return promise;
}
use of com.twitter.distributedlog.exceptions.LogEmptyException in project distributedlog by twitter.
the class StreamTransformer method main.
public static void main(String[] args) throws Exception {
if (3 != args.length) {
System.out.println(HELP);
return;
}
String dlUriStr = args[0];
final String srcStreamName = args[1];
final String targetStreamName = args[2];
URI uri = URI.create(dlUriStr);
DistributedLogConfiguration conf = new DistributedLogConfiguration();
// 16KB
conf.setOutputBufferSize(16 * 1024);
// 5ms
conf.setPeriodicFlushFrequencyMilliSeconds(5);
DistributedLogNamespace namespace = DistributedLogNamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
// open the dlm
System.out.println("Opening log stream " + srcStreamName);
DistributedLogManager srcDlm = namespace.openLog(srcStreamName);
System.out.println("Opening log stream " + targetStreamName);
DistributedLogManager targetDlm = namespace.openLog(targetStreamName);
Transformer<byte[], byte[]> replicationTransformer = new IdenticalTransformer<byte[]>();
LogRecordWithDLSN lastTargetRecord;
DLSN srcDlsn;
try {
lastTargetRecord = targetDlm.getLastLogRecord();
TransformedRecord lastTransformedRecord = new TransformedRecord();
try {
lastTransformedRecord.read(protocolFactory.getProtocol(new TIOStreamTransport(new ByteArrayInputStream(lastTargetRecord.getPayload()))));
srcDlsn = DLSN.deserializeBytes(lastTransformedRecord.getSrcDlsn());
System.out.println("Last transformed record is " + srcDlsn);
} catch (TException e) {
System.err.println("Error on reading last transformed record");
e.printStackTrace(System.err);
srcDlsn = DLSN.InitialDLSN;
}
} catch (LogNotFoundException lnfe) {
srcDlsn = DLSN.InitialDLSN;
} catch (LogEmptyException lee) {
srcDlsn = DLSN.InitialDLSN;
}
AsyncLogWriter targetWriter = FutureUtils.result(targetDlm.openAsyncLogWriter());
try {
readLoop(srcDlm, srcDlsn, targetWriter, replicationTransformer);
} finally {
FutureUtils.result(targetWriter.asyncClose(), Duration.apply(5, TimeUnit.SECONDS));
targetDlm.close();
srcDlm.close();
namespace.close();
}
}
use of com.twitter.distributedlog.exceptions.LogEmptyException in project distributedlog by twitter.
the class MultiReader method main.
public static void main(String[] args) throws Exception {
if (2 != args.length) {
System.out.println(HELP);
return;
}
String dlUriStr = args[0];
final String streamList = args[1];
URI uri = URI.create(dlUriStr);
DistributedLogConfiguration conf = new DistributedLogConfiguration();
DistributedLogNamespace namespace = DistributedLogNamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
String[] streamNameList = StringUtils.split(streamList, ',');
DistributedLogManager[] managers = new DistributedLogManager[streamNameList.length];
for (int i = 0; i < managers.length; i++) {
String streamName = streamNameList[i];
// open the dlm
System.out.println("Opening log stream " + streamName);
managers[i] = namespace.openLog(streamName);
}
final CountDownLatch keepAliveLatch = new CountDownLatch(1);
for (DistributedLogManager dlm : managers) {
final DistributedLogManager manager = dlm;
dlm.getLastLogRecordAsync().addEventListener(new FutureEventListener<LogRecordWithDLSN>() {
@Override
public void onFailure(Throwable cause) {
if (cause instanceof LogNotFoundException) {
System.err.println("Log stream " + manager.getStreamName() + " is not found. Please create it first.");
keepAliveLatch.countDown();
} else if (cause instanceof LogEmptyException) {
System.err.println("Log stream " + manager.getStreamName() + " is empty.");
readLoop(manager, DLSN.InitialDLSN, keepAliveLatch);
} else {
System.err.println("Encountered exception on process stream " + manager.getStreamName());
keepAliveLatch.countDown();
}
}
@Override
public void onSuccess(LogRecordWithDLSN record) {
readLoop(manager, record.getDlsn(), keepAliveLatch);
}
});
}
keepAliveLatch.await();
for (DistributedLogManager dlm : managers) {
dlm.close();
}
namespace.close();
}
use of com.twitter.distributedlog.exceptions.LogEmptyException in project distributedlog by twitter.
the class TailReader method main.
public static void main(String[] args) throws Exception {
if (2 != args.length) {
System.out.println(HELP);
return;
}
String dlUriStr = args[0];
final String streamName = args[1];
URI uri = URI.create(dlUriStr);
DistributedLogConfiguration conf = new DistributedLogConfiguration();
DistributedLogNamespace namespace = DistributedLogNamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
// open the dlm
System.out.println("Opening log stream " + streamName);
DistributedLogManager dlm = namespace.openLog(streamName);
// get the last record
LogRecordWithDLSN lastRecord;
DLSN dlsn;
try {
lastRecord = dlm.getLastLogRecord();
dlsn = lastRecord.getDlsn();
readLoop(dlm, dlsn);
} catch (LogNotFoundException lnfe) {
System.err.println("Log stream " + streamName + " is not found. Please create it first.");
return;
} catch (LogEmptyException lee) {
System.err.println("Log stream " + streamName + " is empty.");
dlsn = DLSN.InitialDLSN;
readLoop(dlm, dlsn);
} finally {
dlm.close();
namespace.close();
}
}
Aggregations