Search in sources :

Example 6 with WriteResponse

use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.

the class TestDistributedLogService method testStreamOpChecksumBadChecksum.

@Test(timeout = 60000)
public void testStreamOpChecksumBadChecksum() throws Exception {
    DistributedLogServiceImpl localService = createConfiguredLocalService();
    WriteContext ctx = new WriteContext().setCrc32(999);
    Future<WriteResponse> result = localService.heartbeat("test", ctx);
    WriteResponse resp = Await.result(result);
    assertEquals(StatusCode.CHECKSUM_FAILED, resp.getHeader().getCode());
    result = localService.release("test", ctx);
    resp = Await.result(result);
    assertEquals(StatusCode.CHECKSUM_FAILED, resp.getHeader().getCode());
    result = localService.delete("test", ctx);
    resp = Await.result(result);
    assertEquals(StatusCode.CHECKSUM_FAILED, resp.getHeader().getCode());
    localService.shutdown();
}
Also used : WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) WriteContext(com.twitter.distributedlog.thrift.service.WriteContext) Test(org.junit.Test)

Example 7 with WriteResponse

use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.

the class TestDistributedLogService method testAcquireStreams.

@Test(timeout = 60000)
public void testAcquireStreams() throws Exception {
    String streamName = testName.getMethodName();
    StreamImpl s0 = createUnstartedStream(service, streamName);
    s0.suspendAcquiring();
    DistributedLogServiceImpl service1 = createService(serverConf, dlConf);
    StreamImpl s1 = createUnstartedStream(service1, streamName);
    s1.suspendAcquiring();
    // create write ops
    WriteOp op0 = createWriteOp(service, streamName, 0L);
    s0.submit(op0);
    WriteOp op1 = createWriteOp(service1, streamName, 1L);
    s1.submit(op1);
    // check pending size
    assertEquals("Write Op 0 should be pending in service 0", 1, s0.numPendingOps());
    assertEquals("Write Op 1 should be pending in service 1", 1, s1.numPendingOps());
    // start acquiring s0
    s0.resumeAcquiring().start();
    WriteResponse wr0 = Await.result(op0.result());
    assertEquals("Op 0 should succeed", StatusCode.SUCCESS, wr0.getHeader().getCode());
    assertEquals("Service 0 should acquire stream", StreamStatus.INITIALIZED, s0.getStatus());
    assertNotNull(s0.getManager());
    assertNotNull(s0.getWriter());
    assertNull(s0.getLastException());
    // start acquiring s1
    s1.resumeAcquiring().start();
    WriteResponse wr1 = Await.result(op1.result());
    assertEquals("Op 1 should fail", StatusCode.FOUND, wr1.getHeader().getCode());
    assertEquals("Service 1 should be in BACKOFF state", StreamStatus.BACKOFF, s1.getStatus());
    assertNotNull(s1.getManager());
    assertNull(s1.getWriter());
    assertNotNull(s1.getLastException());
    assertTrue(s1.getLastException() instanceof OwnershipAcquireFailedException);
    service1.shutdown();
}
Also used : OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) WriteOp(com.twitter.distributedlog.service.stream.WriteOp) StreamImpl(com.twitter.distributedlog.service.stream.StreamImpl) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) Test(org.junit.Test)

Example 8 with WriteResponse

use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.

the class TestDistributedLogService method testAcquireStreamsWhenExceedMaxCachedPartitions.

@Test(timeout = 60000)
public void testAcquireStreamsWhenExceedMaxCachedPartitions() throws Exception {
    String streamName = testName.getMethodName() + "_0000";
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(dlConf);
    confLocal.setMaxCachedPartitionsPerProxy(1);
    ServerConfiguration serverConfLocal = new ServerConfiguration();
    serverConfLocal.addConfiguration(serverConf);
    serverConfLocal.setStreamPartitionConverterClass(DelimiterStreamPartitionConverter.class);
    DistributedLogServiceImpl serviceLocal = createService(serverConfLocal, confLocal);
    Stream stream = serviceLocal.getLogWriter(streamName);
    // stream is cached
    assertNotNull(stream);
    assertEquals(1, serviceLocal.getStreamManager().numCached());
    // create write ops
    WriteOp op0 = createWriteOp(service, streamName, 0L);
    stream.submit(op0);
    WriteResponse wr0 = Await.result(op0.result());
    assertEquals("Op 0 should succeed", StatusCode.SUCCESS, wr0.getHeader().getCode());
    assertEquals(1, serviceLocal.getStreamManager().numAcquired());
    // should fail to acquire another partition
    try {
        serviceLocal.getLogWriter(testName.getMethodName() + "_0001");
        fail("Should fail to acquire new streams");
    } catch (StreamUnavailableException sue) {
    // expected
    }
    assertEquals(1, serviceLocal.getStreamManager().numCached());
    assertEquals(1, serviceLocal.getStreamManager().numAcquired());
    // should be able to acquire partitions from other streams
    String anotherStreamName = testName.getMethodName() + "-another_0001";
    Stream anotherStream = serviceLocal.getLogWriter(anotherStreamName);
    assertNotNull(anotherStream);
    assertEquals(2, serviceLocal.getStreamManager().numCached());
    // create write ops
    WriteOp op1 = createWriteOp(service, anotherStreamName, 0L);
    anotherStream.submit(op1);
    WriteResponse wr1 = Await.result(op1.result());
    assertEquals("Op 1 should succeed", StatusCode.SUCCESS, wr1.getHeader().getCode());
    assertEquals(2, serviceLocal.getStreamManager().numAcquired());
}
Also used : StreamUnavailableException(com.twitter.distributedlog.exceptions.StreamUnavailableException) DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) WriteOp(com.twitter.distributedlog.service.stream.WriteOp) ServerConfiguration(com.twitter.distributedlog.service.config.ServerConfiguration) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) Stream(com.twitter.distributedlog.service.stream.Stream) Test(org.junit.Test)

Example 9 with WriteResponse

use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.

the class TestDistributedLogService method testCloseStreamsShouldFlush.

@Test(timeout = 60000)
public void testCloseStreamsShouldFlush() 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);
    }
    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 succeed or be rejected : " + response.getHeader().getCode(), StatusCode.SUCCESS == response.getHeader().getCode() || StatusCode.WRITE_EXCEPTION == response.getHeader().getCode() || StatusCode.STREAM_UNAVAILABLE == response.getHeader().getCode());
    }
    assertTrue("There should be no streams in the cache", streamManager.getCachedStreams().isEmpty());
    assertTrue("There should be no streams in the acquired cache", streamManager.getAcquiredStreams().isEmpty());
    localService.shutdown();
}
Also used : WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) StreamManagerImpl(com.twitter.distributedlog.service.stream.StreamManagerImpl) WriteContext(com.twitter.distributedlog.thrift.service.WriteContext) DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) HeartbeatOptions(com.twitter.distributedlog.thrift.service.HeartbeatOptions) Future(com.twitter.util.Future) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 10 with WriteResponse

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();
}
Also used : DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) ServerConfiguration(com.twitter.distributedlog.service.config.ServerConfiguration) ArrayList(java.util.ArrayList) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) Future(com.twitter.util.Future) StreamManagerImpl(com.twitter.distributedlog.service.stream.StreamManagerImpl) Test(org.junit.Test)

Aggregations

WriteResponse (com.twitter.distributedlog.thrift.service.WriteResponse)23 Test (org.junit.Test)21 WriteContext (com.twitter.distributedlog.thrift.service.WriteContext)10 WriteOp (com.twitter.distributedlog.service.stream.WriteOp)8 Future (com.twitter.util.Future)7 ArrayList (java.util.ArrayList)7 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)6 StreamImpl (com.twitter.distributedlog.service.stream.StreamImpl)6 DLSN (com.twitter.distributedlog.DLSN)5 StreamManagerImpl (com.twitter.distributedlog.service.stream.StreamManagerImpl)5 ServerConfiguration (com.twitter.distributedlog.service.config.ServerConfiguration)4 Stream (com.twitter.distributedlog.service.stream.Stream)3 List (java.util.List)3 LogRecord (com.twitter.distributedlog.LogRecord)2 InternalServerException (com.twitter.distributedlog.exceptions.InternalServerException)2 OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)2 HeartbeatOptions (com.twitter.distributedlog.thrift.service.HeartbeatOptions)2 AsyncLogWriter (com.twitter.distributedlog.AsyncLogWriter)1 AlreadyClosedException (com.twitter.distributedlog.exceptions.AlreadyClosedException)1 DLException (com.twitter.distributedlog.exceptions.DLException)1