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