use of com.linkedin.databus.core.DbusEventFactory in project databus by linkedin.
the class SimpleClientPipelineFactoryWithSleep method testClientSimpleRequestResponse.
@Test
public void testClientSimpleRequestResponse() {
DbusEventFactory eventFactory = new DbusEventV1Factory();
SimpleTestServerConnection srvConn = new SimpleTestServerConnection(eventFactory.getByteOrder());
srvConn.setPipelineFactory(new SimpleServerPipelineFactory());
boolean serverStarted = srvConn.startSynchronously(101, CONNECT_TIMEOUT_MS);
Assert.assertTrue(serverStarted, "server started");
final SimpleTestClientConnection clientConn = new SimpleTestClientConnection(eventFactory.getByteOrder());
clientConn.setPipelineFactory(new SimpleClientPipelineFactoryWithSleep(200));
boolean clientConnected = clientConn.startSynchronously(101, CONNECT_TIMEOUT_MS);
Assert.assertTrue(clientConnected, "client connected");
// hook in to key places in the server pipeline
ChannelPipeline lastSrvConnPipeline = srvConn.getLastConnChannel().getPipeline();
SimpleTestMessageReader srvMsgReader = (SimpleTestMessageReader) lastSrvConnPipeline.get(SimpleTestMessageReader.class.getSimpleName());
ExceptionListenerTestHandler srvExceptionListener = (ExceptionListenerTestHandler) lastSrvConnPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
// hook in to key places in the client pipeline
ChannelPipeline clientPipeline = clientConn.getChannel().getPipeline();
final ExceptionListenerTestHandler clientExceptionListener = (ExceptionListenerTestHandler) clientPipeline.get(ExceptionListenerTestHandler.class.getSimpleName());
// System.err.println("Current thread: " + Thread.currentThread());
// send a request in a separate thread because the client will intentionally block to simulate
// a timeout
final ChannelBuffer msg = ChannelBuffers.wrappedBuffer("hello".getBytes(Charset.defaultCharset()));
Thread sendThread1 = new Thread(new Runnable() {
@Override
public void run() {
// System.err.println(Thread.currentThread().toString() + ": sending message");
clientConn.getChannel().write(msg);
}
}, "send msg thread");
sendThread1.start();
// System.err.println(Thread.currentThread().toString() + ": waiting for 10");
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
}
;
// System.err.println(Thread.currentThread().toString() + ": done Waiting for 10");
// make sure the server has not received the message
Assert.assertNull(srvExceptionListener.getLastException(), "no server errors");
Assert.assertNull(clientExceptionListener.getLastException(), "no errors yet");
Assert.assertTrue(!"hello".equals(srvMsgReader.getMsg()), "message not read yet");
// wait for the write timeout
try {
Thread.sleep(300);
} catch (InterruptedException ie) {
}
;
// System.err.println("Done Waiting for 300");
// the client should have timed out and closed the connection
TestUtil.assertWithBackoff(new ConditionCheck() {
@Override
public boolean check() {
return null != clientExceptionListener.getLastException();
}
}, "client error", 1000, null);
Assert.assertTrue(null != clientExceptionListener.getLastException(), "client error");
Assert.assertTrue(clientExceptionListener.getLastException() instanceof ClosedChannelException || clientExceptionListener.getLastException() instanceof WriteTimeoutException, "client error test");
Assert.assertTrue(!lastSrvConnPipeline.getChannel().isConnected(), "client has disconnected");
Assert.assertTrue(!clientPipeline.getChannel().isConnected(), "closed connection to server");
}
use of com.linkedin.databus.core.DbusEventFactory in project databus by linkedin.
the class DbusEventFactoryForTesting method createReadOnlyDbusEventFromBuffer.
/**
* Creates a read-only DbusEvent out of an already initialized (serialized) buffer,
* except with the specified SCN. The event's header CRC is updated appropriately.
*
* @param buf buffer containing the serialized event
* @param position byte-offset of the serialized event in the buffer
* @param seq sequence number (SCN) of the new event
* @param version desired version of the new event
* @return a read-only DbusEvent
*/
public static DbusEventInternalReadable createReadOnlyDbusEventFromBuffer(ByteBuffer buf, int position, long seq, byte version) {
DbusEventFactory eventFactory;
if (version == DbusEventFactory.DBUS_EVENT_V1) {
eventFactory = new DbusEventV1Factory();
} else if (version == DbusEventFactory.DBUS_EVENT_V2) {
eventFactory = new DbusEventV2Factory();
} else {
throw new UnsupportedDbusEventVersionRuntimeException(version);
}
DbusEventInternalWritable event = eventFactory.createWritableDbusEventFromBuffer(buf, position);
event.setSequence(seq);
event.applyCrc();
return event;
}
use of com.linkedin.databus.core.DbusEventFactory in project databus by linkedin.
the class TestDbusEventBufferNG method testSameBufferTraceOn.
@Test
public void testSameBufferTraceOn() throws IOException, InvalidConfigException {
File buf1Trace = null;
File buf2Trace = null;
File buf3Trace = null;
try {
File tmpFile = File.createTempFile("TestDbusEventBufferNG-buffer", ".trace");
// make sure the file was created, i.e. we have write permissions
Assert.assertTrue(tmpFile.exists());
// we just need the name
Assert.assertTrue(tmpFile.delete());
DbusEventBuffer.Config cfgBuilder = new DbusEventBuffer.Config();
cfgBuilder.setMaxSize(10000);
cfgBuilder.setScnIndexSize(1024);
cfgBuilder.setAverageEventSize(1024);
cfgBuilder.setAllocationPolicy(AllocationPolicy.HEAP_MEMORY.toString());
cfgBuilder.getTrace().setOption(RelayEventTraceOption.Option.file.toString());
cfgBuilder.getTrace().setFilename(tmpFile.getAbsolutePath());
cfgBuilder.getTrace().setNeedFileSuffix(true);
// test that the first gets created
PhysicalPartition pp = new PhysicalPartition(1, "TestPart");
DbusEventFactory eventFactory = new DbusEventV2Factory();
DbusEventBuffer buf1 = new DbusEventBuffer(cfgBuilder.build(), pp, eventFactory);
buf1Trace = new File(tmpFile.getAbsolutePath() + "." + pp.getName() + "_" + pp.getId());
Assert.assertTrue(buf1Trace.exists());
buf1.start(1);
for (int i = 1; i <= 10; ++i) {
buf1.startEvents();
buf1.appendEvent(new DbusEventKey(i), (short) 0, (short) 0, 0L, (short) 0, new byte[16], new byte[10], false, null);
buf1.endEvents(10 * i);
}
long trace1len = buf1Trace.length();
Assert.assertTrue(trace1len > 0);
// create the second buffer and check its trace is different
DbusEventBuffer buf2 = new DbusEventBuffer(cfgBuilder.build(), pp, eventFactory);
buf2Trace = new File(tmpFile.getAbsolutePath() + "." + pp.getName() + "_" + pp.getId() + ".1");
Assert.assertTrue(buf2Trace.exists());
buf2.start(1);
for (int i = 1; i <= 5; ++i) {
buf2.startEvents();
buf2.appendEvent(new DbusEventKey(i), (short) 0, (short) 0, 0L, (short) 0, new byte[16], new byte[10], false, null);
buf2.endEvents(10 * i);
}
long trace2len = buf2Trace.length();
Assert.assertTrue(trace2len > 0);
Assert.assertTrue(buf1Trace.length() == trace1len);
// create a third buffer and check its trace is different
DbusEventBuffer buf3 = new DbusEventBuffer(cfgBuilder.build(), pp, eventFactory);
buf3Trace = new File(tmpFile.getAbsolutePath() + "." + pp.getName() + "_" + pp.getId() + ".2");
Assert.assertTrue(buf3Trace.exists());
buf3.start(1);
for (int i = 1; i <= 6; ++i) {
buf3.startEvents();
buf3.appendEvent(new DbusEventKey(i), (short) 0, (short) 0, 0L, (short) 0, new byte[16], new byte[10], false, null);
buf3.endEvents(10 * i);
}
long trace3len = buf3Trace.length();
Assert.assertTrue(trace3len > 0);
Assert.assertTrue(buf1Trace.length() == trace1len);
Assert.assertTrue(buf2Trace.length() == trace2len);
} finally {
Assert.assertTrue(null == buf1Trace || buf1Trace.delete());
Assert.assertTrue(null == buf2Trace || buf2Trace.delete());
Assert.assertTrue(null == buf3Trace || buf3Trace.delete());
}
}
use of com.linkedin.databus.core.DbusEventFactory in project databus by linkedin.
the class TestDbusEvent method convertV2toV1LongKey.
public void convertV2toV1LongKey(DbusEventKey dbusKey) {
// both versions share same _byteOrder var
final DbusEventFactory eventV2Factory = new DbusEventV2Factory();
String randomValue = RngUtils.randomString(100);
ByteBuffer serializationBuffer = ByteBuffer.allocate(1000).order(eventV2Factory.getByteOrder());
try {
DbusEventInfo eventInfo = new DbusEventInfo(DbusOpcode.UPSERT, 0L, (short) 0, partitionId, timeStamp, srcId, schemaId, randomValue.getBytes(Charset.defaultCharset()), false, false);
// create v2 event
eventInfo.setEventSerializationVersion(DbusEventFactory.DBUS_EVENT_V2);
DbusEventFactory.serializeEvent(dbusKey, serializationBuffer, eventInfo);
} catch (KeyTypeNotImplementedException e2) {
fail(e2.getLocalizedMessage());
}
DbusEventInternalReadable e = null;
try {
e = DbusEventFactoryForTesting.createReadOnlyDbusEventFromBuffer(serializationBuffer, 0, 200L, DbusEventFactory.DBUS_EVENT_V2);
} catch (Exception e1) {
fail("Not supposed to throw exception");
}
assertTrue("event V2 is invalid", e.isValid());
// now let's convert the event
DbusEventV2 eV2 = (DbusEventV2) e;
DbusEventV1 eV1 = null;
try {
eV1 = (DbusEventV1) eV2.convertToV1();
} catch (KeyTypeNotImplementedException e1) {
fail(e1.getLocalizedMessage());
}
LOG.info("ev1 =" + eV1);
// let's compare the fields
assertTrue("event v1 is invalid", eV1.isValid(true));
assertEquals(eV1.getVersion(), DbusEventFactory.DBUS_EVENT_V1);
assertEquals(eV2.getVersion(), DbusEventFactory.DBUS_EVENT_V2);
if (dbusKey.getKeyType() == KeyType.LONG)
assertEquals(eV2.key(), eV1.key());
else if (dbusKey.getKeyType() == KeyType.STRING) {
assertTrue(Arrays.equals(eV2.keyBytes(), eV1.keyBytes()));
assertEquals(eV2.keyBytesLength(), eV1.keyBytesLength());
}
assertEquals(eV2.getPartitionId(), eV1.getPartitionId());
assertEquals(eV2.srcId(), eV1.srcId());
assertEquals(eV2.schemaId(), eV1.schemaId());
assertEquals(eV2.isTraceEnabled(), eV1.isTraceEnabled());
assertEquals(eV2.timestampInNanos(), eV1.timestampInNanos());
assertEquals(eV2.valueLength(), eV1.valueLength());
assertTrue(Arrays.equals(Utils.byteBufferToBytes(eV2.value()), Utils.byteBufferToBytes(eV1.value())));
// and these fields should differ
assertFalse(eV2.bodyCrc() == eV1.bodyCrc());
assertFalse(eV2.headerCrc() == eV1.headerCrc());
assertFalse(eV2.size() == eV1.size());
}
use of com.linkedin.databus.core.DbusEventFactory in project databus by linkedin.
the class TestInternalMetadata method createEvent.
private DbusEvent createEvent(DbusEventPart metadataPart) throws Exception {
DbusEventInfo eventInfo = new DbusEventInfo(DbusOpcode.UPSERT, SCN, PARTITION_ID, PARTITION_ID, TIMESTAMP_IN_NANOS, SOURCE_ID, PAYLOAD_SCHEMA_CHECKSUM, PAYLOAD, // enable tracing
false, // auto-commit
true, DbusEventFactory.DBUS_EVENT_V2, PAYLOAD_SCHEMA_VERSION, metadataPart);
DbusEventFactory eventFactory = new DbusEventV2Factory();
DbusEventKey eventKey = new DbusEventKey(LONG_KEY);
ByteBuffer serialBuf = ByteBuffer.allocate(maxEventLen).order(eventFactory.getByteOrder());
DbusEventFactory.serializeEvent(eventKey, serialBuf, eventInfo);
return eventFactory.createReadOnlyDbusEventFromBuffer(serialBuf, 0);
}
Aggregations