use of com.twitter.distributedlog.thrift.service.WriteResponse 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.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.
the class TestDistributedLogService method testNonDurableWrite.
@Test(timeout = 60000)
public void testNonDurableWrite() throws Exception {
DistributedLogConfiguration confLocal = newLocalConf();
confLocal.setOutputBufferSize(Integer.MAX_VALUE).setImmediateFlushEnabled(false).setPeriodicFlushFrequencyMilliSeconds(0).setDurableWriteEnabled(false);
ServerConfiguration serverConfLocal = new ServerConfiguration();
serverConfLocal.addConfiguration(serverConf);
serverConfLocal.enableDurableWrite(false);
serverConfLocal.setServiceTimeoutMs(Integer.MAX_VALUE).setStreamProbationTimeoutMs(Integer.MAX_VALUE);
String streamName = testName.getMethodName();
DistributedLogServiceImpl localService = createService(serverConfLocal, confLocal);
StreamManagerImpl streamManager = (StreamManagerImpl) localService.getStreamManager();
int numWrites = 10;
List<Future<WriteResponse>> futureList = new ArrayList<Future<WriteResponse>>();
for (int i = 0; i < numWrites; i++) {
futureList.add(localService.write(streamName, createRecord(i)));
}
assertTrue("Stream " + streamName + " should be cached", streamManager.getCachedStreams().containsKey(streamName));
List<WriteResponse> resultList = FutureUtils.result(Future.collect(futureList));
for (WriteResponse wr : resultList) {
assertEquals(DLSN.InvalidDLSN, DLSN.deserialize(wr.getDlsn()));
}
localService.shutdown();
}
use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.
the class TestDistributedLogService method testCloseStreamsShouldAbort.
@Test(timeout = 60000)
public void testCloseStreamsShouldAbort() throws Exception {
DistributedLogConfiguration confLocal = newLocalConf();
confLocal.setOutputBufferSize(Integer.MAX_VALUE).setImmediateFlushEnabled(false).setPeriodicFlushFrequencyMilliSeconds(0);
String streamNamePrefix = testName.getMethodName();
DistributedLogServiceImpl localService = createService(serverConf, confLocal);
StreamManagerImpl streamManager = (StreamManagerImpl) localService.getStreamManager();
int numStreams = 10;
int numWrites = 10;
List<Future<WriteResponse>> futureList = Lists.newArrayListWithExpectedSize(numStreams * numWrites);
for (int i = 0; i < numStreams; i++) {
String streamName = streamNamePrefix + "-" + i;
HeartbeatOptions hbOptions = new HeartbeatOptions();
hbOptions.setSendHeartBeatToReader(true);
// make sure the first log segment of each stream created
FutureUtils.result(localService.heartbeatWithOptions(streamName, new WriteContext(), hbOptions));
for (int j = 0; j < numWrites; j++) {
futureList.add(localService.write(streamName, createRecord(i * numWrites + j)));
}
}
assertEquals("There should be " + numStreams + " streams in cache", numStreams, streamManager.getCachedStreams().size());
while (streamManager.getAcquiredStreams().size() < numStreams) {
TimeUnit.MILLISECONDS.sleep(20);
}
for (Stream s : streamManager.getAcquiredStreams().values()) {
StreamImpl stream = (StreamImpl) s;
stream.setStatus(StreamStatus.FAILED);
}
Future<List<Void>> closeResult = localService.closeStreams();
List<Void> closedStreams = Await.result(closeResult);
assertEquals("There should be " + numStreams + " streams closed", numStreams, closedStreams.size());
// all writes should be flushed
for (Future<WriteResponse> future : futureList) {
WriteResponse response = Await.result(future);
assertTrue("Op should fail with " + StatusCode.BK_TRANSMIT_ERROR + " or be rejected : " + response.getHeader().getCode(), StatusCode.BK_TRANSMIT_ERROR == response.getHeader().getCode() || StatusCode.WRITE_EXCEPTION == response.getHeader().getCode() || StatusCode.WRITE_CANCELLED_EXCEPTION == response.getHeader().getCode());
}
// acquired streams should all been removed after we close them
assertTrue("There should be no streams in the acquired cache", streamManager.getAcquiredStreams().isEmpty());
localService.shutdown();
// cached streams wouldn't be removed immediately after streams are closed
// but they should be removed after we shutdown the service
assertTrue("There should be no streams in the cache after shutting down the service", streamManager.getCachedStreams().isEmpty());
}
use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.
the class TestDistributedLogService method testWriteOpChecksumBadData.
@Test(timeout = 60000)
public void testWriteOpChecksumBadData() throws Exception {
DistributedLogServiceImpl localService = createConfiguredLocalService();
ByteBuffer buffer = getTestDataBuffer();
WriteContext ctx = new WriteContext().setCrc32(ProtocolUtils.writeOpCRC32("test", buffer.array()));
// Overwrite 1 byte to corrupt data.
buffer.put(1, (byte) 0xab);
Future<WriteResponse> result = localService.writeWithContext("test", buffer, ctx);
WriteResponse resp = Await.result(result);
assertEquals(StatusCode.CHECKSUM_FAILED, resp.getHeader().getCode());
localService.shutdown();
}
use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.
the class TestDistributedLogService method testShutdown.
@Test(timeout = 60000)
public void testShutdown() throws Exception {
service.shutdown();
StreamManagerImpl streamManager = (StreamManagerImpl) service.getStreamManager();
WriteResponse response = Await.result(service.write(testName.getMethodName(), createRecord(0L)));
assertEquals("Write should fail with " + StatusCode.SERVICE_UNAVAILABLE, StatusCode.SERVICE_UNAVAILABLE, response.getHeader().getCode());
assertTrue("There should be no streams created after shutdown", streamManager.getCachedStreams().isEmpty());
assertTrue("There should be no streams acquired after shutdown", streamManager.getAcquiredStreams().isEmpty());
}
Aggregations