use of com.twitter.util.Future in project distributedlog by twitter.
the class TestDistributedLogServer method testBulkWriteEmptyBuffer.
@Test(timeout = 60000)
public void testBulkWriteEmptyBuffer() throws Exception {
String name = String.format("dlserver-bulk-write-%s", "empty");
dlClient.routingService.addHost(name, dlServer.getAddress());
List<ByteBuffer> writes = new ArrayList<ByteBuffer>();
writes.add(ByteBuffer.wrap(("").getBytes()));
writes.add(ByteBuffer.wrap(("").getBytes()));
List<Future<DLSN>> futures = dlClient.dlClient.writeBulk(name, writes);
assertEquals(2, futures.size());
for (Future<DLSN> future : futures) {
// No throw == pass
DLSN dlsn = Await.result(future, Duration.fromSeconds(10));
}
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class TestDistributedLogService method testServiceTimeout.
@Test(timeout = 60000)
public void testServiceTimeout() throws Exception {
DistributedLogConfiguration confLocal = newLocalConf();
confLocal.setOutputBufferSize(Integer.MAX_VALUE).setImmediateFlushEnabled(false).setPeriodicFlushFrequencyMilliSeconds(0);
ServerConfiguration serverConfLocal = newLocalServerConf();
serverConfLocal.addConfiguration(serverConf);
serverConfLocal.setServiceTimeoutMs(200).setStreamProbationTimeoutMs(100);
String streamName = testName.getMethodName();
// create a new service with 200ms timeout
DistributedLogServiceImpl localService = createService(serverConfLocal, confLocal);
StreamManagerImpl streamManager = (StreamManagerImpl) localService.getStreamManager();
int numWrites = 10;
List<Future<WriteResponse>> futureList = new ArrayList<Future<WriteResponse>>(numWrites);
for (int i = 0; i < numWrites; i++) {
futureList.add(localService.write(streamName, createRecord(i)));
}
assertTrue("Stream " + streamName + " should be cached", streamManager.getCachedStreams().containsKey(streamName));
StreamImpl s = (StreamImpl) streamManager.getCachedStreams().get(streamName);
// the stream should be set CLOSING
while (StreamStatus.CLOSING != s.getStatus() && StreamStatus.CLOSED != s.getStatus()) {
TimeUnit.MILLISECONDS.sleep(20);
}
assertNotNull("Writer should be initialized", s.getWriter());
assertNull("No exception should be thrown", s.getLastException());
Future<Void> closeFuture = s.getCloseFuture();
Await.result(closeFuture);
for (int i = 0; i < numWrites; i++) {
assertTrue("Write should not fail before closing", futureList.get(i).isDefined());
WriteResponse response = Await.result(futureList.get(i));
assertTrue("Op should fail with " + StatusCode.WRITE_CANCELLED_EXCEPTION, StatusCode.BK_TRANSMIT_ERROR == response.getHeader().getCode() || StatusCode.WRITE_EXCEPTION == response.getHeader().getCode() || StatusCode.WRITE_CANCELLED_EXCEPTION == response.getHeader().getCode());
}
while (streamManager.getCachedStreams().containsKey(streamName)) {
TimeUnit.MILLISECONDS.sleep(20);
}
assertFalse("Stream should be removed from cache", streamManager.getCachedStreams().containsKey(streamName));
assertFalse("Stream should be removed from acquired cache", streamManager.getAcquiredStreams().containsKey(streamName));
localService.shutdown();
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class ZKLogMetadataForWriter method checkLogMetadataPaths.
static Future<List<Versioned<byte[]>>> checkLogMetadataPaths(ZooKeeper zk, String logRootPath, boolean ownAllocator) {
// Note re. persistent lock state initialization: the read lock persistent state (path) is
// initialized here but only used in the read handler. The reason is its more convenient and
// less error prone to manage all stream structure in one place.
final String logRootParentPath = new File(logRootPath).getParent();
final String logSegmentsPath = logRootPath + LOGSEGMENTS_PATH;
final String maxTxIdPath = logRootPath + MAX_TXID_PATH;
final String lockPath = logRootPath + LOCK_PATH;
final String readLockPath = logRootPath + READ_LOCK_PATH;
final String versionPath = logRootPath + VERSION_PATH;
final String allocationPath = logRootPath + ALLOCATION_PATH;
int numPaths = ownAllocator ? MetadataIndex.ALLOCATION + 1 : MetadataIndex.LOGSEGMENTS + 1;
List<Future<Versioned<byte[]>>> checkFutures = Lists.newArrayListWithExpectedSize(numPaths);
checkFutures.add(Utils.zkGetData(zk, logRootParentPath, false));
checkFutures.add(Utils.zkGetData(zk, logRootPath, false));
checkFutures.add(Utils.zkGetData(zk, maxTxIdPath, false));
checkFutures.add(Utils.zkGetData(zk, versionPath, false));
checkFutures.add(Utils.zkGetData(zk, lockPath, false));
checkFutures.add(Utils.zkGetData(zk, readLockPath, false));
checkFutures.add(Utils.zkGetData(zk, logSegmentsPath, false));
if (ownAllocator) {
checkFutures.add(Utils.zkGetData(zk, allocationPath, false));
}
return Future.collect(checkFutures);
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class TestDistributedLock method testAsyncAcquireBasics.
@Test(timeout = 60000)
public void testAsyncAcquireBasics() throws Exception {
TestLockFactory locks = new TestLockFactory(runtime.getMethodName(), zkc, lockStateExecutor);
int count = 3;
ArrayList<Future<ZKDistributedLock>> results = new ArrayList<Future<ZKDistributedLock>>(count);
ZKDistributedLock[] lockArray = new ZKDistributedLock[count];
final CountDownLatch[] latches = new CountDownLatch[count];
// the future.
for (int i = 0; i < count; i++) {
latches[i] = new CountDownLatch(1);
lockArray[i] = locks.createLock(i, zkc);
final int index = i;
results.add(lockArray[i].asyncAcquire().addEventListener(new FutureEventListener<ZKDistributedLock>() {
@Override
public void onSuccess(ZKDistributedLock lock) {
latches[index].countDown();
}
@Override
public void onFailure(Throwable cause) {
fail("unexpected failure " + cause);
}
}));
}
// acquired).
for (int i = 0; i < count; i++) {
latches[i].await();
assertLatchesSet(latches, i + 1);
Await.result(results.get(i));
FutureUtils.result(lockArray[i].asyncClose());
}
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class DistributedLogAdmin method checkStream.
private static StreamCandidate checkStream(final com.twitter.distributedlog.DistributedLogManagerFactory factory, final String streamName, final ExecutorService executorService, final BookKeeperClient bkc, String digestpw) throws IOException {
DistributedLogManager dlm = factory.createDistributedLogManagerWithSharedClients(streamName);
try {
List<LogSegmentMetadata> segments = dlm.getLogSegments();
if (segments.isEmpty()) {
return null;
}
List<Future<LogSegmentCandidate>> futures = new ArrayList<Future<LogSegmentCandidate>>(segments.size());
for (LogSegmentMetadata segment : segments) {
futures.add(checkLogSegment(streamName, segment, executorService, bkc, digestpw));
}
List<LogSegmentCandidate> segmentCandidates;
try {
segmentCandidates = Await.result(Future.collect(futures));
} catch (Exception e) {
throw new IOException("Failed on checking stream " + streamName, e);
}
StreamCandidate streamCandidate = new StreamCandidate(streamName);
for (LogSegmentCandidate segmentCandidate : segmentCandidates) {
if (null != segmentCandidate) {
streamCandidate.addLogSegmentCandidate(segmentCandidate);
}
}
if (streamCandidate.segmentCandidates.isEmpty()) {
return null;
}
return streamCandidate;
} finally {
dlm.close();
}
}
Aggregations