Search in sources :

Example 1 with ServerConfiguration

use of com.twitter.distributedlog.service.config.ServerConfiguration in project distributedlog by twitter.

the class DistributedLogServer method runServer.

public void runServer() throws ConfigurationException, IllegalArgumentException, IOException {
    if (!uri.isPresent()) {
        throw new IllegalArgumentException("No distributedlog uri provided.");
    }
    URI dlUri = URI.create(uri.get());
    DistributedLogConfiguration dlConf = new DistributedLogConfiguration();
    if (conf.isPresent()) {
        String configFile = conf.get();
        try {
            dlConf.loadConf(new File(configFile).toURI().toURL());
        } catch (ConfigurationException e) {
            throw new IllegalArgumentException("Failed to load distributedlog configuration from " + configFile + ".");
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Failed to load distributedlog configuration from malformed " + configFile + ".");
        }
    }
    this.configExecutorService = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder().setNameFormat("DistributedLogService-Dyncfg-%d").setDaemon(true).build());
    // server configuration and dynamic configuration
    ServerConfiguration serverConf = new ServerConfiguration();
    serverConf.loadConf(dlConf);
    // overwrite the shard id if it is provided in the args
    if (shardId.isPresent()) {
        serverConf.setServerShardId(shardId.get());
    }
    serverConf.validate();
    DynamicDistributedLogConfiguration dynDlConf = getServiceDynConf(dlConf);
    logger.info("Starting stats provider : {}", statsProvider.getClass());
    statsProvider.start(dlConf);
    if (announceServerSet.isPresent() && announceServerSet.get()) {
        announcer = new ServerSetAnnouncer(dlUri, port.or(0), statsPort.or(0), shardId.or(0));
    } else {
        announcer = new NOPAnnouncer();
    }
    // Build the stream partition converter
    StreamPartitionConverter converter;
    try {
        converter = ReflectionUtils.newInstance(serverConf.getStreamPartitionConverterClass());
    } catch (ConfigurationException e) {
        logger.warn("Failed to load configured stream-to-partition converter. Fallback to use {}", IdentityStreamPartitionConverter.class.getName());
        converter = new IdentityStreamPartitionConverter();
    }
    StreamConfigProvider streamConfProvider = getStreamConfigProvider(dlConf, converter);
    // pre-run
    preRun(dlConf, serverConf);
    Pair<DistributedLogServiceImpl, Server> serverPair = runServer(serverConf, dlConf, dynDlConf, dlUri, converter, statsProvider, port.or(0), keepAliveLatch, statsReceiver, thriftmux.isPresent(), streamConfProvider);
    this.dlService = serverPair.getLeft();
    this.server = serverPair.getRight();
    // announce the service
    announcer.announce();
}
Also used : MalformedURLException(java.net.MalformedURLException) Server(com.twitter.finagle.builder.Server) ServerConfiguration(com.twitter.distributedlog.service.config.ServerConfiguration) URI(java.net.URI) DynamicDistributedLogConfiguration(com.twitter.distributedlog.config.DynamicDistributedLogConfiguration) IdentityStreamPartitionConverter(com.twitter.distributedlog.service.streamset.IdentityStreamPartitionConverter) StreamPartitionConverter(com.twitter.distributedlog.service.streamset.StreamPartitionConverter) DynamicDistributedLogConfiguration(com.twitter.distributedlog.config.DynamicDistributedLogConfiguration) DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) ServerSetAnnouncer(com.twitter.distributedlog.service.announcer.ServerSetAnnouncer) ConfigurationException(org.apache.commons.configuration.ConfigurationException) IdentityStreamPartitionConverter(com.twitter.distributedlog.service.streamset.IdentityStreamPartitionConverter) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) NOPAnnouncer(com.twitter.distributedlog.service.announcer.NOPAnnouncer) DefaultStreamConfigProvider(com.twitter.distributedlog.service.config.DefaultStreamConfigProvider) ServiceStreamConfigProvider(com.twitter.distributedlog.service.config.ServiceStreamConfigProvider) NullStreamConfigProvider(com.twitter.distributedlog.service.config.NullStreamConfigProvider) StreamConfigProvider(com.twitter.distributedlog.service.config.StreamConfigProvider) File(java.io.File)

Example 2 with ServerConfiguration

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

Example 3 with ServerConfiguration

use of com.twitter.distributedlog.service.config.ServerConfiguration 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 4 with ServerConfiguration

use of com.twitter.distributedlog.service.config.ServerConfiguration 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)

Example 5 with ServerConfiguration

use of com.twitter.distributedlog.service.config.ServerConfiguration in project distributedlog by twitter.

the class TestDistributedLogService method newLocalServerConf.

private ServerConfiguration newLocalServerConf() {
    ServerConfiguration serverConf = new ServerConfiguration();
    serverConf.loadConf(dlConf);
    serverConf.setServerThreads(1);
    return serverConf;
}
Also used : ServerConfiguration(com.twitter.distributedlog.service.config.ServerConfiguration)

Aggregations

ServerConfiguration (com.twitter.distributedlog.service.config.ServerConfiguration)6 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)5 WriteResponse (com.twitter.distributedlog.thrift.service.WriteResponse)4 Test (org.junit.Test)4 Stream (com.twitter.distributedlog.service.stream.Stream)2 StreamManagerImpl (com.twitter.distributedlog.service.stream.StreamManagerImpl)2 WriteOp (com.twitter.distributedlog.service.stream.WriteOp)2 Future (com.twitter.util.Future)2 ArrayList (java.util.ArrayList)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 DynamicDistributedLogConfiguration (com.twitter.distributedlog.config.DynamicDistributedLogConfiguration)1 StreamUnavailableException (com.twitter.distributedlog.exceptions.StreamUnavailableException)1 NOPAnnouncer (com.twitter.distributedlog.service.announcer.NOPAnnouncer)1 ServerSetAnnouncer (com.twitter.distributedlog.service.announcer.ServerSetAnnouncer)1 DefaultStreamConfigProvider (com.twitter.distributedlog.service.config.DefaultStreamConfigProvider)1 NullStreamConfigProvider (com.twitter.distributedlog.service.config.NullStreamConfigProvider)1 ServiceStreamConfigProvider (com.twitter.distributedlog.service.config.ServiceStreamConfigProvider)1 StreamConfigProvider (com.twitter.distributedlog.service.config.StreamConfigProvider)1 StreamImpl (com.twitter.distributedlog.service.stream.StreamImpl)1 IdentityStreamPartitionConverter (com.twitter.distributedlog.service.streamset.IdentityStreamPartitionConverter)1