use of com.linkedin.databus.core.DbusEventInternalWritable 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.DbusEventInternalWritable in project databus by linkedin.
the class BootstrapProducerCallback method onDataEvent.
@Override
public ConsumerCallbackResult onDataEvent(DbusEvent e, DbusEventDecoder eventDecoder) {
if (e.sequence() < _newWindowScn) {
LOG.warn("Seeing an Old event. Dropping it !! Current SCN : " + _newWindowScn + ". Event :" + e.toString());
return ConsumerCallbackResult.SUCCESS;
}
_numEvents++;
_newWindowScn = e.sequence();
try {
// TODO (DDSDBUS-776) : remove erstwhile windowscn column
_stmt.setLong(1, _newWindowScn);
_stmt.setLong(2, _newWindowScn);
String keyStr = null;
if (e.isKeyNumber()) {
keyStr = Long.toString(e.key());
} else if (e.isKeyString()) {
keyStr = StringUtils.bytesToString(e.keyBytes());
} else if (e.isKeySchema()) {
LOG.error("schema key type not supported: " + e);
return ConsumerCallbackResult.ERROR;
} else {
LOG.error("unknown event key type: " + e);
return ConsumerCallbackResult.ERROR;
}
_stmt.setString(3, keyStr);
if (!(e instanceof DbusEventInternalWritable)) {
throw new UnsupportedClassVersionError("Cannot get raw bytes out of DbusEvent");
}
ByteBuffer bytebuff = ((DbusEventInternalWritable) e).getRawBytes();
byte[] val = new byte[bytebuff.remaining()];
bytebuff.get(val);
_stmt.setBytes(4, val);
_stmt.executeUpdate();
} catch (SQLException e1) {
if (null != _statsCollector)
_statsCollector.registerSQLException();
LOG.error("Got SQLException in dataEvent Hanlder!! Connections will be reset !!", e1);
try {
reset();
} catch (DatabusException e2) {
DbusPrettyLogUtils.logExceptionAtError("Unable to reset connection", e2, LOG);
} catch (SQLException sqlEx) {
LOG.error("Got exception while resetting connections. Stopping Client !!", sqlEx);
return ConsumerCallbackResult.ERROR_FATAL;
}
return ConsumerCallbackResult.ERROR;
}
return ConsumerCallbackResult.SUCCESS;
}
use of com.linkedin.databus.core.DbusEventInternalWritable in project databus by linkedin.
the class DbusEventCorrupter method toggleEventCorruption.
/**
* Toggles corrupted state of event. Final event state is determined by
# incoming event and specified subfield of event.
*
* @param type part of event in which to inject corruption
* @param event DbusEvent that will be modified
*/
public static void toggleEventCorruption(EventCorruptionType type, DbusEvent event) throws InvalidEventException {
// Regardless of the underlying event type (e.g., readable), we're writing to its
// internal state, so "convert" it to a writable event.
// bruuuuuuutal...
DbusEventInternalWritable writableEvent = makeWritable(event);
switch(type) {
case LENGTH:
int newSize = writableEvent.size() ^ CORRUPTION_PATTERN;
writableEvent.setSize(newSize);
break;
case HEADERCRC:
long headerCrc = writableEvent.headerCrc() ^ CORRUPTION_PATTERN;
writableEvent.setHeaderCrc(headerCrc);
break;
case PAYLOAD:
if (writableEvent.payloadLength() > 0) {
byte[] payload = new byte[writableEvent.payloadLength()];
writableEvent.value().get(payload);
payload[0] ^= CORRUPTION_PATTERN;
writableEvent.setValue(payload);
}
break;
case PAYLOADCRC:
long payloadCrc = writableEvent.bodyCrc() ^ CORRUPTION_PATTERN;
writableEvent.setValueCrc(payloadCrc);
break;
}
}
use of com.linkedin.databus.core.DbusEventInternalWritable in project databus by linkedin.
the class DbusEventAppender method tarnishEventsInBuffer.
/**
* Alter event fields by XORing against a known fixed value; each invocation toggles between
* 'good' state and the 'tarnished' (corrupted) state.
*
* @param type : event field that will be tarnished
* @param positions : list of event positions in *sorted order* that will be tarnished
* @return number of events tarnished
*/
public int tarnishEventsInBuffer(int[] positions, EventCorruptionType type) throws InvalidEventException {
int tarnishedEvents = 0;
int count = 0;
int posIndex = 0;
boolean onlyDataEvents = (type == EventCorruptionType.PAYLOAD);
for (Iterator<DbusEventInternalWritable> di = _buffer.iterator(); (posIndex < positions.length) && di.hasNext(); ) {
DbusEventInternalWritable ev = di.next();
if (!onlyDataEvents || !ev.isControlMessage()) {
if (count == positions[posIndex]) {
DbusEventCorrupter.toggleEventCorruption(type, ev);
++tarnishedEvents;
++posIndex;
}
++count;
}
}
return tarnishedEvents;
}
use of com.linkedin.databus.core.DbusEventInternalWritable in project databus by linkedin.
the class DummySuccessfulErrorCountingConsumer method writeEventsToBuffer.
static WriteEventsResult writeEventsToBuffer(DbusEventBuffer buf, DbusEventInfo[] eventInfos, int winSize) throws UnsupportedEncodingException {
Random rng = new Random(100);
final List<Integer> eventOfs = new ArrayList<Integer>(eventInfos.length);
final List<DbusEventKey> eventKeys = new ArrayList<DbusEventKey>(eventInfos.length);
InternalDatabusEventsListener ofsTracker = new InternalDatabusEventsListener() {
@Override
public void close() throws IOException {
}
@Override
public void onEvent(DbusEvent event, long offset, int size) {
DbusEventInternalWritable e = (DbusEventInternalWritable) event;
assertTrue("endEvents() gave us an invalid event! (offset = " + offset + ", size = " + size + ")", e.isValid(true));
// FIXME: is this correct? position() == offset; why subtract size?
eventOfs.add(e.getRawBytes().position() - e.size());
}
};
buf.addInternalListener(ofsTracker);
for (int i = 0; i < eventInfos.length; ++i) {
if (i % winSize == 0) {
if (i > 0)
buf.endEvents(i);
buf.startEvents();
}
DbusEventKey key = new DbusEventKey(RngUtils.randomString(rng, rng.nextInt(50)).getBytes("UTF-8"));
buf.appendEvent(key, eventInfos[i], null);
eventKeys.add(key);
}
buf.endEvents(eventInfos.length);
buf.removeInternalListener(ofsTracker);
return new WriteEventsResult(eventOfs, eventKeys);
}
Aggregations