use of com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector in project databus by linkedin.
the class DatabusHttpClientImpl method initializeRelayConnections.
private synchronized void initializeRelayConnections() {
for (List<DatabusSubscription> subsList : _relayGroups.keySet()) {
List<String> sourcesStrList = DatabusSubscription.getStrList(subsList);
List<DatabusV2ConsumerRegistration> relayConsumers = getRelayGroupStreamConsumers().get(subsList);
//nothing to do
if (null == relayConsumers || 0 == relayConsumers.size())
continue;
try {
DatabusSourcesConnection.StaticConfig connConfig = getClientStaticConfig().getConnection(sourcesStrList);
if (null == connConfig) {
connConfig = getClientStaticConfig().getConnectionDefaults();
}
// make sure we have the right policy.
if (!connConfig.getEventBuffer().isEnableScnIndex() && connConfig.getEventBuffer().getQueuePolicy() != DbusEventBuffer.QueuePolicy.BLOCK_ON_WRITE) {
throw new InvalidConfigException("If SCN index is disabled, queue policy must be BLOCK_ON_WRITE");
}
CheckpointPersistenceProvider cpPersistenceProvder = getCheckpointPersistenceProvider();
if (null != cpPersistenceProvder && getClientStaticConfig().getCheckpointPersistence().isClearBeforeUse()) {
cpPersistenceProvder.removeCheckpoint(sourcesStrList);
}
ServerInfo server0 = _relayGroups.get(subsList).iterator().next();
ArrayList<DatabusV2ConsumerRegistration> bstConsumersRegs = new ArrayList<DatabusV2ConsumerRegistration>();
for (List<DatabusSubscription> bstSubSourcesList : getRelayGroupBootstrapConsumers().keySet()) {
List<DatabusV2ConsumerRegistration> bstRegsistrations = getRelayGroupBootstrapConsumers().get(bstSubSourcesList);
for (DatabusV2ConsumerRegistration bstConsumerReg : bstRegsistrations) {
if (server0.supportsSources(bstConsumerReg.getSources())) {
bstConsumersRegs.add(bstConsumerReg);
}
}
}
DbusEventBuffer eventBuffer = connConfig.getEventBuffer().getOrCreateEventBuffer(_eventFactory);
eventBuffer.setDropOldEvents(true);
eventBuffer.start(0);
DbusEventBuffer bootstrapBuffer = null;
// create bootstrap only if it is enabled
if (_clientStaticConfig.getRuntime().getBootstrap().isEnabled()) {
bootstrapBuffer = new DbusEventBuffer(connConfig.getEventBuffer());
bootstrapBuffer.setDropOldEvents(false);
bootstrapBuffer.start(0);
}
LOG.info("The sourcesList is " + sourcesStrList);
LOG.info("The relayGroupStreamConsumers is " + getRelayGroupStreamConsumers().get(subsList));
Set<ServerInfo> relays = _relayGroups.get(subsList);
Set<ServerInfo> bootstrapServices = _bootstrapGroups.get(subsList);
String statsCollectorName = generateSubsStatsName(sourcesStrList);
int ownerId = getContainerStaticConfig().getId();
_bootstrapEventsStats.addStatsCollector(statsCollectorName, new DbusEventsStatisticsCollector(ownerId, statsCollectorName + ".inbound.bs", true, false, getMbeanServer()));
_inBoundStatsCollectors.addStatsCollector(statsCollectorName, new DbusEventsStatisticsCollector(ownerId, statsCollectorName + ".inbound", true, false, getMbeanServer()));
_outBoundStatsCollectors.addStatsCollector(statsCollectorName, new DbusEventsStatisticsCollector(ownerId, statsCollectorName + ".outbound", true, false, getMbeanServer()));
_consumerStatsCollectors.addStatsCollector(statsCollectorName, new ConsumerCallbackStats(ownerId, statsCollectorName + ".inbound.cons", statsCollectorName + ".inbound.cons", true, false, null, getMbeanServer()));
_bsConsumerStatsCollectors.addStatsCollector(statsCollectorName, new ConsumerCallbackStats(ownerId, statsCollectorName + ".inbound.bs.cons", statsCollectorName + ".inbound.bs.cons", true, false, null, getMbeanServer()));
_unifiedClientStatsCollectors.addStatsCollector(statsCollectorName, new UnifiedClientStats(ownerId, statsCollectorName + ".inbound.unified.cons", statsCollectorName + ".inbound.unified.cons", true, false, _clientStaticConfig.getPullerThreadDeadnessThresholdMs(), null, getMbeanServer()));
ConnectionStateFactory connStateFactory = new ConnectionStateFactory(DatabusSubscription.getStrList(subsList));
DatabusSourcesConnection newConn = new DatabusSourcesConnection(connConfig, subsList, relays, bootstrapServices, relayConsumers, //_relayGroupBootstrapConsumers.get(sourcesList),
bstConsumersRegs, eventBuffer, bootstrapBuffer, getDefaultExecutorService(), getContainerStatsCollector(), _inBoundStatsCollectors.getStatsCollector(statsCollectorName), _bootstrapEventsStats.getStatsCollector(statsCollectorName), _consumerStatsCollectors.getStatsCollector(statsCollectorName), _bsConsumerStatsCollectors.getStatsCollector(statsCollectorName), _unifiedClientStatsCollectors.getStatsCollector(statsCollectorName), getCheckpointPersistenceProvider(), getRelayConnFactory(), getBootstrapConnFactory(), getHttpStatsCollector(), null, this, _eventFactory, connStateFactory);
newConn.start();
_relayConnections.add(newConn);
} catch (Exception e) {
LOG.error("connection initialization issue for source(s):" + subsList + "; please check your configuration", e);
}
}
if (0 == _relayConnections.size()) {
LOG.warn("No connections specified");
}
}
use of com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector in project databus by linkedin.
the class RelayPullThread method doReadDataEvents.
protected void doReadDataEvents(ConnectionState curState) {
boolean debugEnabled = _log.isDebugEnabled();
boolean enqueueMessage = true;
try {
ChunkedBodyReadableByteChannel readChannel = curState.getReadChannel();
Checkpoint cp = curState.getCheckpoint();
curState.setRelayFellOff(false);
String remoteErrorName = RemoteExceptionHandler.getExceptionName(readChannel);
Throwable knownRemoteError = _remoteExceptionHandler.getException(readChannel);
if (null != knownRemoteError && knownRemoteError instanceof ScnNotFoundException) {
if (toTearConnAfterHandlingResponse()) {
tearConnectionAndEnqueuePickServer();
enqueueMessage = false;
} else {
curState.setRelayFellOff(true);
if (_retriesOnFallOff.getRemainingRetriesNum() > 0) {
_log.error("Got SCNNotFoundException. Retry (" + _retriesOnFallOff.getRetriesNum() + ") out of " + _retriesOnFallOff.getConfig().getMaxRetryNum());
curState.switchToPickServer();
} else {
enqueueMessage = onRelayFellOff(curState, cp, knownRemoteError);
}
}
} else if (null != remoteErrorName) {
if (toTearConnAfterHandlingResponse()) {
tearConnectionAndEnqueuePickServer();
enqueueMessage = false;
} else {
//remote processing error
_log.error("read events error: " + RemoteExceptionHandler.getExceptionMessage(readChannel));
curState.switchToStreamResponseError();
}
} else {
/*DispatcherState dispatchState = curState.getDispatcherState();
dispatchState.switchToDispatchEvents();
_dispatcherThread.addNewStateBlocking(dispatchState);*/
if (debugEnabled)
_log.debug("Sending events to buffer");
DbusEventsStatisticsCollector connCollector = _sourcesConn.getInboundEventsStatsCollector();
if (curState.isSCNRegress()) {
_log.info("SCN Regress requested !! Sending a SCN Regress Message to dispatcher. Curr Ckpt :" + curState.getCheckpoint());
DbusEvent regressEvent = getEventFactory().createSCNRegressEvent(new SCNRegressMessage(curState.getCheckpoint()));
writeEventToRelayDispatcher(curState, regressEvent, "SCN Regress Event from ckpt :" + curState.getCheckpoint());
curState.setSCNRegress(false);
}
UnifiedClientStats unifiedClientStats = _sourcesConn.getUnifiedClientStats();
if (unifiedClientStats != null) {
// failsafe: we're definitely not bootstrapping here
unifiedClientStats.setBootstrappingState(false);
sendHeartbeat(unifiedClientStats);
}
int eventsNum = curState.getDataEventsBuffer().readEvents(readChannel, curState.getListeners(), connCollector);
boolean resetConnection = false;
if (eventsNum > 0) {
_timeSinceEventsSec = System.currentTimeMillis();
cp.checkPoint();
} else {
// check how long it has been since we got some events
if (_remoteExceptionHandler.getPendingEventSize(readChannel) > curState.getDataEventsBuffer().getMaxReadBufferCapacity()) {
// The relay had a pending event that we can never accommodate. This is fatal error.
String err = "ReadBuffer max capacity(" + curState.getDataEventsBuffer().getMaxReadBufferCapacity() + ") is less than event size(" + _remoteExceptionHandler.getPendingEventSize(readChannel) + "). Increase databus.client.connectionDefaults.eventBuffer.maxEventSize and restart.";
_log.fatal(err);
enqueueMessage(LifecycleMessage.createSuspendOnErroMessage(new PendingEventTooLargeException(err)));
return;
} else {
if (_noEventsConnectionResetTimeSec > 0) {
// unless the feature is disabled (<=0)
resetConnection = (System.currentTimeMillis() - _timeSinceEventsSec) / 1000 > _noEventsConnectionResetTimeSec;
if (resetConnection) {
_timeSinceEventsSec = System.currentTimeMillis();
_log.warn("about to reset connection to relay " + curState.getServerInetAddress() + ", because there were no events for " + _noEventsConnectionResetTimeSec + "secs");
}
}
}
}
if (debugEnabled)
_log.debug("Events read: " + eventsNum);
// if it has been too long since we got non-empty responses - the relay may be stuck, try to reconnect
if (toTearConnAfterHandlingResponse() || resetConnection) {
tearConnectionAndEnqueuePickServer();
enqueueMessage = false;
} else {
curState.switchToStreamResponseDone();
resetServerRetries();
}
}
if (enqueueMessage)
enqueueMessage(curState);
} catch (InterruptedException ie) {
_log.warn("interrupted", ie);
curState.switchToStreamResponseError();
enqueueMessage(curState);
} catch (InvalidEventException e) {
_log.error("error reading events from server:" + e, e);
curState.switchToStreamResponseError();
enqueueMessage(curState);
} catch (RuntimeException e) {
_log.error("runtime error reading events from server: " + e, e);
curState.switchToStreamResponseError();
enqueueMessage(curState);
}
}
use of com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector in project databus by linkedin.
the class MockBootstrapConnection method testBootstrapPendingEvent.
// Make sure that we suspend on error when we get the x-dbus-pending-size header with a size that is
// larger than our dbusevent size.
@Test
public void testBootstrapPendingEvent() throws Exception {
List<String> sources = Arrays.asList("source1");
Properties clientProps = new Properties();
clientProps.setProperty("client.container.httpPort", "0");
clientProps.setProperty("client.container.jmx.rmiEnabled", "false");
clientProps.setProperty("client.runtime.bootstrap.enabled", "true");
clientProps.setProperty("client.runtime.bootstrap.service(1).name", "bs1");
clientProps.setProperty("client.runtime.bootstrap.service(1).host", "localhost");
clientProps.setProperty("client.runtime.bootstrap.service(1).port", "10001");
clientProps.setProperty("client.runtime.bootstrap.service(1).sources", "source1");
clientProps.setProperty("client.runtime.relay(1).name", "relay1");
clientProps.setProperty("client.runtime.relay(1).port", "10001");
clientProps.setProperty("client.runtime.relay(1).sources", "source1");
clientProps.setProperty("client.connectionDefaults.eventBuffer.maxSize", "100000");
clientProps.setProperty("client.connectionDefaults.pullerRetries.maxRetryNum", "3");
DatabusHttpClientImpl.Config clientConfBuilder = new DatabusHttpClientImpl.Config();
ConfigLoader<DatabusHttpClientImpl.StaticConfig> configLoader = new ConfigLoader<DatabusHttpClientImpl.StaticConfig>("client.", clientConfBuilder);
configLoader.loadConfig(clientProps);
DatabusHttpClientImpl.StaticConfig clientConf = clientConfBuilder.build();
DatabusSourcesConnection.StaticConfig srcConnConf = clientConf.getConnectionDefaults();
DatabusHttpClientImpl client = new DatabusHttpClientImpl(clientConf);
client.registerDatabusBootstrapListener(new LoggingConsumer(), null, "source1");
Assert.assertNotNull(client, "client instantiation failed");
DatabusHttpClientImpl.RuntimeConfig clientRtConf = clientConf.getRuntime().build();
//we keep the index of the next server we expect to see
AtomicInteger serverIdx = new AtomicInteger(-1);
List<IdNamePair> sourcesResponse = new ArrayList<IdNamePair>();
sourcesResponse.add(new IdNamePair(1L, "source1"));
Map<Long, List<RegisterResponseEntry>> registerResponse = new HashMap<Long, List<RegisterResponseEntry>>();
List<RegisterResponseEntry> regResponse = new ArrayList<RegisterResponseEntry>();
regResponse.add(new RegisterResponseEntry(1L, (short) 1, SCHEMA$.toString()));
registerResponse.put(1L, regResponse);
ChunkedBodyReadableByteChannel channel = EasyMock.createMock(ChunkedBodyReadableByteChannel.class);
// getting the pending-event-size header is called twice, once for checking and once for logging.
EasyMock.expect(channel.getMetadata(DatabusHttpHeaders.DATABUS_PENDING_EVENT_SIZE)).andReturn("1000000").times(2);
EasyMock.expect(channel.getMetadata("x-dbus-error-cause")).andReturn(null).times(2);
EasyMock.expect(channel.getMetadata("x-dbus-error")).andReturn(null).times(2);
EasyMock.replay(channel);
DbusEventBuffer dbusBuffer = EasyMock.createMock(DbusEventBuffer.class);
dbusBuffer.endEvents(false, -1, false, false, null);
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(dbusBuffer.injectEvent(EasyMock.<DbusEventInternalReadable>notNull())).andReturn(true).anyTimes();
EasyMock.expect(dbusBuffer.getEventSerializationVersion()).andReturn(DbusEventFactory.DBUS_EVENT_V1).anyTimes();
EasyMock.expect(dbusBuffer.getMaxReadBufferCapacity()).andReturn(600).times(2);
EasyMock.expect(dbusBuffer.getBufferFreeReadSpace()).andReturn(600000).times(2);
EasyMock.expect(dbusBuffer.readEvents(EasyMock.<ReadableByteChannel>notNull())).andReturn(1).times(1);
EasyMock.expect(dbusBuffer.readEvents(EasyMock.<ReadableByteChannel>notNull(), EasyMock.<List<InternalDatabusEventsListener>>notNull(), EasyMock.<DbusEventsStatisticsCollector>isNull())).andReturn(0).times(1);
EasyMock.replay(dbusBuffer);
ConnectionStateFactory connStateFactory = new ConnectionStateFactory(sources);
//This guy succeeds on /sources but fails on /register
MockBootstrapConnection mockSuccessConn = new MockBootstrapConnection(10, 10, channel, serverIdx, false);
DatabusBootstrapConnectionFactory mockConnFactory = org.easymock.EasyMock.createMock("mockRelayFactory", DatabusBootstrapConnectionFactory.class);
//each server should be tried MAX_RETRIES time until all retries are exhausted
EasyMock.expect(mockConnFactory.createConnection(EasyMock.<ServerInfo>notNull(), EasyMock.<ActorMessageQueue>notNull(), EasyMock.<RemoteExceptionHandler>notNull())).andReturn(mockSuccessConn).anyTimes();
List<DatabusSubscription> sourcesSubList = DatabusSubscription.createSubscriptionList(sources);
DatabusSourcesConnection sourcesConn2 = EasyMock.createMock(DatabusSourcesConnection.class);
EasyMock.expect(sourcesConn2.getSourcesNames()).andReturn(Arrays.asList("source1")).anyTimes();
EasyMock.expect(sourcesConn2.getSubscriptions()).andReturn(sourcesSubList).anyTimes();
EasyMock.expect(sourcesConn2.getConnectionConfig()).andReturn(srcConnConf).anyTimes();
EasyMock.expect(sourcesConn2.getConnectionStatus()).andReturn(new DatabusComponentStatus("dummy")).anyTimes();
EasyMock.expect(sourcesConn2.getLocalRelayCallsStatsCollector()).andReturn(null).anyTimes();
EasyMock.expect(sourcesConn2.getRelayCallsStatsCollector()).andReturn(null).anyTimes();
EasyMock.expect(sourcesConn2.getUnifiedClientStats()).andReturn(null).anyTimes();
EasyMock.expect(sourcesConn2.getBootstrapConnFactory()).andReturn(mockConnFactory).anyTimes();
EasyMock.expect(sourcesConn2.loadPersistentCheckpoint()).andReturn(null).anyTimes();
EasyMock.expect(sourcesConn2.getDataEventsBuffer()).andReturn(dbusBuffer).anyTimes();
EasyMock.expect(sourcesConn2.isBootstrapEnabled()).andReturn(true).anyTimes();
EasyMock.expect(sourcesConn2.getBootstrapRegistrations()).andReturn(null).anyTimes();
EasyMock.expect(sourcesConn2.getBootstrapServices()).andReturn(null).anyTimes();
EasyMock.expect(sourcesConn2.getBootstrapEventsStatsCollector()).andReturn(null).anyTimes();
EasyMock.makeThreadSafe(mockConnFactory, true);
EasyMock.makeThreadSafe(sourcesConn2, true);
EasyMock.replay(mockConnFactory);
EasyMock.replay(sourcesConn2);
BootstrapPullThread bsPuller = new BootstrapPullThread("RelayPuller", sourcesConn2, dbusBuffer, connStateFactory, clientRtConf.getBootstrap().getServicesSet(), new ArrayList<DbusKeyCompositeFilterConfig>(), clientConf.getPullerBufferUtilizationPct(), ManagementFactory.getPlatformMBeanServer(), new DbusEventV2Factory(), null, null);
mockSuccessConn.setCallback(bsPuller);
bsPuller.getComponentStatus().start();
Checkpoint cp = _ckptHandlerSource1.createInitialBootstrapCheckpoint(null, 0L);
//TODO remove
//cp.setSnapshotSource("source1");
//cp.setCatchupSource("source1");
//cp.setConsumptionMode(DbusClientMode.BOOTSTRAP_SNAPSHOT);
ConnectionState connState = bsPuller.getConnectionState();
connState.switchToBootstrap(cp);
testTransitionCase(bsPuller, StateId.BOOTSTRAP, StateId.REQUEST_START_SCN, cp);
bsPuller.getMessageQueue().clear();
testTransitionCase(bsPuller, StateId.REQUEST_START_SCN, StateId.START_SCN_RESPONSE_SUCCESS, null);
bsPuller.getMessageQueue().clear();
Map<Long, List<RegisterResponseEntry>> entries = new HashMap<Long, List<RegisterResponseEntry>>();
entries.put(1L, new ArrayList<RegisterResponseEntry>());
connState.setSourcesSchemas(entries);
connState.setCurrentBSServerInfo(bsPuller.getCurentServer());
testTransitionCase(bsPuller, StateId.START_SCN_RESPONSE_SUCCESS, StateId.REQUEST_STREAM, null);
bsPuller.getMessageQueue().clear();
connState.getSourcesNameMap().put("source1", new IdNamePair(1L, "source1"));
connState.getSourceIdMap().put(1L, new IdNamePair(1L, "source1"));
testTransitionCase(bsPuller, StateId.REQUEST_STREAM, StateId.STREAM_REQUEST_SUCCESS, null);
bsPuller.getMessageQueue().clear();
testTransitionCase(bsPuller, StateId.STREAM_REQUEST_SUCCESS, StateId.STREAM_REQUEST_SUCCESS, "SUSPEND_ON_ERROR", null);
EasyMock.verify(channel);
EasyMock.verify(sourcesConn2);
EasyMock.verify(dbusBuffer);
EasyMock.verify(channel);
EasyMock.verify(mockConnFactory);
}
use of com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector in project databus by linkedin.
the class DatabusV2ClusterRegistrationImpl method initializeStatsCollectors.
protected synchronized void initializeStatsCollectors() {
//some safety against null pointers coming from unit tests
MBeanServer mbeanServer = null;
int ownerId = -1;
long pullerThreadDeadnessThresholdMs = UnifiedClientStats.DEFAULT_DEADNESS_THRESHOLD_MS;
if (null != _client) {
mbeanServer = _client.getMbeanServer();
ownerId = _client.getContainerStaticConfig().getId();
pullerThreadDeadnessThresholdMs = _client.getClientStaticConfig().getPullerThreadDeadnessThresholdMs();
}
String regId = null != _id ? _id.getId() : "unknownReg";
ConsumerCallbackStats relayConsumerStats = new ConsumerCallbackStats(ownerId, regId + ".callback.relay", regId, true, false, new ConsumerCallbackStatsEvent());
ConsumerCallbackStats bootstrapConsumerStats = new ConsumerCallbackStats(ownerId, regId + ".callback.bootstrap", regId, true, false, new ConsumerCallbackStatsEvent());
UnifiedClientStats unifiedClientStats = new UnifiedClientStats(ownerId, regId + ".callback.unified", regId, true, false, pullerThreadDeadnessThresholdMs, new UnifiedClientStatsEvent());
_relayCallbackStatsMerger = new StatsCollectors<ConsumerCallbackStats>(relayConsumerStats);
_bootstrapCallbackStatsMerger = new StatsCollectors<ConsumerCallbackStats>(bootstrapConsumerStats);
_unifiedClientStatsMerger = new StatsCollectors<UnifiedClientStats>(unifiedClientStats);
_relayEventStatsMerger = new StatsCollectors<DbusEventsStatisticsCollector>(new AggregatedDbusEventsStatisticsCollector(ownerId, regId + ".inbound", true, false, mbeanServer));
_bootstrapEventStatsMerger = new StatsCollectors<DbusEventsStatisticsCollector>(new AggregatedDbusEventsStatisticsCollector(ownerId, regId + ".inbound.bs", true, false, mbeanServer));
if (null != _client) {
_client.getBootstrapEventsStats().addStatsCollector(regId, _bootstrapEventStatsMerger.getStatsCollector());
_client.getInBoundStatsCollectors().addStatsCollector(regId, _relayEventStatsMerger.getStatsCollector());
_client.getRelayConsumerStatsCollectors().addStatsCollector(regId, _relayCallbackStatsMerger.getStatsCollector());
_client.getBootstrapConsumerStatsCollectors().addStatsCollector(regId, _bootstrapCallbackStatsMerger.getStatsCollector());
_client.getUnifiedClientStatsCollectors().addStatsCollector(regId, _unifiedClientStatsMerger.getStatsCollector());
_client.getGlobalStatsMerger().registerStatsCollector(_relayEventStatsMerger);
_client.getGlobalStatsMerger().registerStatsCollector(_bootstrapEventStatsMerger);
_client.getGlobalStatsMerger().registerStatsCollector(_relayCallbackStatsMerger);
_client.getGlobalStatsMerger().registerStatsCollector(_bootstrapCallbackStatsMerger);
_client.getGlobalStatsMerger().registerStatsCollector(_unifiedClientStatsMerger);
}
}
use of com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector in project databus by linkedin.
the class DummySuccessfulErrorCountingConsumer method testInStreamTimeOut2.
/**
* Tests the logic of the client to handle Timeout that comes while processing stream request.
* the script:
* setup client and connect to one of the servers
* wait for /sources and register call and replay
* save the 'future' of the write operation for the /stream call. Replace this future down the stream with the fake one,
* so the notification of write completion will never come
* make server send only headers info first
* make server send data, but intercept the message before it reaches the client. At this moment fire WriteTimeout
* exception from a separate thread.
* Make sure PullerThread doesn't get two error messages (and as a result tries to setup up two new connections)
*/
@Test
public void testInStreamTimeOut2() throws Exception {
final Logger log = Logger.getLogger("TestDatabusHttpClient.testInStreamTimeout2");
MockServerChannelHandler.LOG.setLevel(Level.DEBUG);
//log.setLevel(Level.);
final int eventsNum = 20;
DbusEventInfo[] eventInfos = createSampleSchema1Events(eventsNum);
//simulate relay buffers
DbusEventBuffer relayBuffer = new DbusEventBuffer(_bufCfg);
relayBuffer.start(0);
writeEventsToBuffer(relayBuffer, eventInfos, 4);
//prepare stream response
Checkpoint cp = Checkpoint.createFlexibleCheckpoint();
final DbusEventsStatisticsCollector stats = new DbusEventsStatisticsCollector(1, "test1", true, false, null);
// create ChunnelBuffer and fill it with events from relayBuffer
ChannelBuffer streamResPrefix = NettyTestUtils.streamToChannelBuffer(relayBuffer, cp, 20000, stats);
//create client
_stdClientCfgBuilder.getContainer().setReadTimeoutMs(DEFAULT_READ_TIMEOUT_MS);
final DatabusHttpClientImpl client = new DatabusHttpClientImpl(_stdClientCfgBuilder.build());
final TestConsumer consumer = new TestConsumer();
client.registerDatabusStreamListener(consumer, null, SOURCE1_NAME);
// connect to a relay created in SetupClass (one out of three)
client.start();
// wait until a connection made
try {
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return client._relayConnections.size() == 1;
}
}, "sources connection present", 100, log);
//get the connection
final DatabusSourcesConnection clientConn = client._relayConnections.get(0);
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != clientConn.getRelayPullThread().getLastOpenConnection();
}
}, "relay connection present", 100, log);
// figure out connection details
final NettyHttpDatabusRelayConnection relayConn = (NettyHttpDatabusRelayConnection) clientConn.getRelayPullThread().getLastOpenConnection();
final NettyHttpDatabusRelayConnectionInspector relayConnInsp = new NettyHttpDatabusRelayConnectionInspector(relayConn);
// wait until client is connected
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != relayConnInsp.getChannel() && relayConnInsp.getChannel().isConnected();
}
}, "client connected", 200, log);
//figure out which port we got connected to on the server side
Channel clientChannel = relayConnInsp.getChannel();
InetSocketAddress relayAddr = (InetSocketAddress) clientChannel.getRemoteAddress();
int relayPort = relayAddr.getPort();
log.info("relay selected: " + relayPort);
// add our handler to the client's pipeline which will generate the timeout
MockServerChannelHandler mock = new MockServerChannelHandler();
clientChannel.getPipeline().addBefore("inflater", "mockServer", mock);
Map<String, ChannelHandler> map = clientChannel.getPipeline().toMap();
boolean handlerFound = false;
for (Map.Entry<String, ChannelHandler> m : map.entrySet()) {
if (LOG.isDebugEnabled())
LOG.debug(m.getKey() + "=>" + m.getValue());
if (m.getKey().equals("mockServer"))
handlerFound = true;
}
Assert.assertTrue(handlerFound, "handler added");
SimpleTestServerConnection relay = null;
// Find the relay's object
for (int i = 0; i < RELAY_PORT.length; ++i) {
if (relayPort == RELAY_PORT[i])
relay = _dummyServer[i];
}
assertTrue(null != relay);
SocketAddress clientAddr = clientChannel.getLocalAddress();
final SocketAddress testClientAddr = clientAddr;
final SimpleTestServerConnection testRelay = relay;
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != testRelay.getChildChannel(testClientAddr);
}
}, "relay detects new connection", 1000, log);
Channel serverChannel = relay.getChildChannel(clientAddr);
assertTrue(null != serverChannel);
ChannelPipeline serverPipeline = serverChannel.getPipeline();
SimpleObjectCaptureHandler objCapture = (SimpleObjectCaptureHandler) serverPipeline.get("3");
//process the /sources request
NettyTestUtils.waitForHttpRequest(objCapture, SOURCES_REQUEST_REGEX, 1000);
objCapture.clear();
//send back the /sources response
HttpResponse sourcesResp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
sourcesResp.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
sourcesResp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
HttpChunk body = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer(("[{\"id\":1,\"name\":\"" + SOURCE1_NAME + "\"}]").getBytes(Charset.defaultCharset())));
NettyTestUtils.sendServerResponses(relay, clientAddr, sourcesResp, body);
//make sure the client processes the response correctly
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
String idListString = clientConn.getRelayPullThread()._currentState.getSourcesIdListString();
return "1".equals(idListString);
}
}, "client processes /sources response", 100, log);
log.info("process the /register request");
NettyTestUtils.waitForHttpRequest(objCapture, "/register.*", 1000);
objCapture.clear();
String msgHistory = clientConn.getRelayPullThread().getMessageHistoryLog();
log.info("MSG HISTORY before: " + msgHistory);
// make sure our handler will save the 'future' of the next write operation - 'stream'
mock.enableSaveTheFuture(true);
log.info("send back the /register response");
RegisterResponseEntry entry = new RegisterResponseEntry(1L, (short) 1, SOURCE1_SCHEMA_STR);
String responseStr = NettyTestUtils.generateRegisterResponse(entry);
body = new DefaultHttpChunk(ChannelBuffers.wrappedBuffer(responseStr.getBytes(Charset.defaultCharset())));
NettyTestUtils.sendServerResponses(relay, clientAddr, sourcesResp, body);
log.info("make sure the client processes the response /register correctly");
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
DispatcherState dispState = clientConn.getRelayDispatcher().getDispatcherState();
return null != dispState.getSchemaMap() && 1 == dispState.getSchemaMap().size();
}
}, "client processes /register response", 100, log);
mock.disableWriteComplete(true);
log.info("process /stream call and return a response");
NettyTestUtils.waitForHttpRequest(objCapture, "/stream.*", 1000);
objCapture.clear();
log.info("***1");
//disable save future as it should be saved by now
mock.enableSaveTheFuture(false);
log.info("***2");
final HttpResponse streamResp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
streamResp.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
streamResp.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
log.info("***3");
// timeout for local netty calls (in test only)
int timeout = 1000;
// send header info
relay.sendServerResponse(clientAddr, sourcesResp, timeout);
TestUtil.sleep(1000);
log.info("***4");
// when write data arrives from the server - we want to simulate/throw WriteTimeoutException
mock.enableThrowWTOException(true);
// send data
relay.sendServerResponse(clientAddr, new DefaultHttpChunk(streamResPrefix), timeout);
relay.sendServerResponse(clientAddr, HttpChunk.LAST_CHUNK, timeout);
log.info("***5");
// make sure close channel event and future failure are propagated
TestUtil.sleep(3000);
// get the history and validate it
String expectedHistory = "[START, PICK_SERVER, REQUEST_SOURCES, SOURCES_RESPONSE_SUCCESS, REQUEST_REGISTER, REGISTER_RESPONSE_SUCCESS, REQUEST_STREAM, STREAM_REQUEST_SUCCESS, STREAM_RESPONSE_DONE, REQUEST_STREAM, STREAM_REQUEST_ERROR, PICK_SERVER, REQUEST_SOURCES]".trim();
msgHistory = clientConn.getRelayPullThread().getMessageHistoryLog().trim();
log.info("***6");
LOG.info("MSG HISTORY: " + msgHistory);
Assert.assertEquals(msgHistory, expectedHistory, "Puller thread message history doesn't match");
log.info("***7");
} catch (Exception e2) {
log.info("Got exception" + e2);
} finally {
client.shutdown();
}
}
Aggregations