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;
}
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;
}
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));
}
}
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;
}
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;
}
Aggregations