use of com.linkedin.databus.client.DatabusRelayConnectionFactory in project databus by linkedin.
the class RelayEventProducer method createDatabusSourcesConnection.
public static DatabusSourcesConnection createDatabusSourcesConnection(String producerName, int id, String serverName, String subscriptionString, DatabusCombinedConsumer consumer, long internalBufferMaxSize, int largestEventSize, long consumerTimeoutMs, long pollIntervalMs, long connTimeoutMs, int consumerParallelism, boolean blockingBuffer, DatabusClientNettyThreadPools nettyThreadPools, int noEventsTimeoutSec, int maxEventVersion, int initReadBufferSize) throws InvalidConfigException {
// the assumption here is that the list of subscriptions will become the
// list of sources hosted by the relay
Set<ServerInfo> relayServices = createServerInfo(serverName, subscriptionString);
// null bootstrapService
Set<ServerInfo> bootstrapServices = null;
// create subscription objects based on what is required by subscription
String[] subscriptionList = subscriptionString.split(",");
List<DatabusSubscription> subsList = DatabusSubscription.createSubscriptionList(Arrays.asList(subscriptionList));
List<String> sourcesStrList = DatabusSubscription.getStrList(subsList);
LOG.info("The sourcesList is " + sourcesStrList);
// create registration objects with consumers
List<DatabusV2ConsumerRegistration> relayConsumers = createDatabusV2ConsumerRegistration(consumer, sourcesStrList);
List<DatabusV2ConsumerRegistration> bstConsumers = null;
// setup sources connection config
DatabusSourcesConnection.Config confBuilder = new DatabusSourcesConnection.Config();
confBuilder.setId(id);
// consume whatever is in relay
confBuilder.setConsumeCurrent(true);
//this is set to false as the behaviour is to read the latest SCN when SCN is not found, the buffer isn't cleared
//as such , so a possibility of gaps in events arises. What we want ideally is to clear existing buffer and then consume from latest SCN
confBuilder.setReadLatestScnOnError(false);
// 10s max consumer timeout
confBuilder.setConsumerTimeBudgetMs(consumerTimeoutMs);
// poll interval in ms and infinite retry;
confBuilder.getPullerRetries().setMaxRetryNum(-1);
confBuilder.getPullerRetries().setInitSleep(pollIntervalMs);
confBuilder.setConsumerParallelism(consumerParallelism);
confBuilder.getDispatcherRetries().setMaxRetryNum(1);
// internal buffer conf
DbusEventBuffer.Config bufferConf = new DbusEventBuffer.Config();
bufferConf.setMaxSize(internalBufferMaxSize);
bufferConf.setAllocationPolicy("DIRECT_MEMORY");
if (initReadBufferSize > 0) {
bufferConf.setAverageEventSize(initReadBufferSize);
}
bufferConf.setMaxEventSize(largestEventSize);
//client buffer's scn index- not used
bufferConf.setScnIndexSize(64 * 1024);
String queuePolicy = blockingBuffer ? "BLOCK_ON_WRITE" : "OVERWRITE_ON_WRITE";
bufferConf.setQueuePolicy(queuePolicy);
//get appropriate checkpointThresholdPct
double newCkptPct = confBuilder.computeSafeCheckpointThresholdPct(bufferConf);
if (newCkptPct < 5.0 || newCkptPct > 95.0) {
LOG.warn("Not setting required checkpointThresholdPct : " + newCkptPct + "to accommodate largestEventSize= " + largestEventSize + " in buffer of size " + bufferConf.getMaxSize());
if (newCkptPct <= 0.0) {
//unlikely to happen: if it does retain default
newCkptPct = confBuilder.getCheckpointThresholdPct();
}
if (newCkptPct < 5.0) {
newCkptPct = 5.0;
} else if (newCkptPct > 95.0) {
newCkptPct = 95.0;
}
}
LOG.info("Setting checkpointThresholdPct:" + newCkptPct);
confBuilder.setCheckpointThresholdPct(newCkptPct);
confBuilder.setEventBuffer(bufferConf);
confBuilder.setNoEventsConnectionResetTimeSec(noEventsTimeoutSec);
DatabusSourcesConnection.StaticConfig connConfig = confBuilder.build();
// internal buffers of databus client library
DbusEventBuffer buffer = new DbusEventBuffer(connConfig.getEventBuffer());
buffer.start(0);
DbusEventBuffer bootstrapBuffer = null;
// Create threadpools and netty managers
// read - write timeout in ms
long readTimeoutMs = connTimeoutMs;
long writeTimeoutMs = connTimeoutMs;
long bstReadTimeoutMs = connTimeoutMs;
int protocolVersion = 2;
// connection factory
NettyHttpConnectionFactory defaultConnFactory = new NettyHttpConnectionFactory(nettyThreadPools.getBossExecutorService(), nettyThreadPools.getIoExecutorService(), null, nettyThreadPools.getTimer(), writeTimeoutMs, readTimeoutMs, bstReadTimeoutMs, protocolVersion, maxEventVersion, nettyThreadPools.getChannelGroup());
// Create Thread pool for consumer threads
int maxThreadsNum = 1;
int keepAliveMs = 1000;
ThreadPoolExecutor defaultExecutorService = new OrderedMemoryAwareThreadPoolExecutor(maxThreadsNum, 0, 0, keepAliveMs, TimeUnit.MILLISECONDS);
ConsumerCallbackStats relayConsumerStats = new ConsumerCallbackStats(id, producerName + ".inbound.cons", producerName + ".inbound.cons", true, false, null, ManagementFactory.getPlatformMBeanServer());
ConsumerCallbackStats bootstrapConsumerStats = new ConsumerCallbackStats(id, producerName + ".inbound.bs.cons", producerName + ".inbound.bs.cons", true, false, null, ManagementFactory.getPlatformMBeanServer());
UnifiedClientStats unifiedClientStats = new UnifiedClientStats(id, producerName + ".inbound.unified.cons", producerName + ".inbound.unified.cons", true, false, UnifiedClientStats.DEFAULT_DEADNESS_THRESHOLD_MS, null, ManagementFactory.getPlatformMBeanServer());
DatabusRelayConnectionFactory relayConnFactory = defaultConnFactory;
DatabusBootstrapConnectionFactory bootstrapConnFactory = defaultConnFactory;
ConnectionStateFactory connStateFactory = new ConnectionStateFactory(sourcesStrList);
DatabusSourcesConnection conn = new DatabusSourcesConnection(connConfig, subsList, relayServices, bootstrapServices, relayConsumers, bstConsumers, buffer, bootstrapBuffer, defaultExecutorService, // getContainerStatsCollector(),
null, // getInboundEventStatisticsCollector(),
null, // getBootstrapEventsStatsCollector(),
null, // relay callback stats
relayConsumerStats, // bootstrap callback stats
bootstrapConsumerStats, // combined relay/bootstrap callback stats
unifiedClientStats, // getCheckpointPersistenceProvider(),
null, relayConnFactory, bootstrapConnFactory, // getHttpStatsCollector(),
null, // RegistrationId
null, null, new DbusEventV2Factory(), // TODO Get the ref to factory from HttpRelay.
connStateFactory);
return conn;
}
Aggregations