public static DefaultValueSerializer create(final ByteBuffer buffer, final IRegistry<String> stringRegistry) { return new DefaultValueSerializer(buffer, stringRegistry); }
@Override public void putBoolean(final boolean value) { final byte data; if (value) { data = TRUE_VALUE; } else { data = FALSE_VALUE; } this.putByte(data); }
@Override public void putString(final String value) { final int stringId = this.stringRegistry.get(value); this.putInt(stringId); }
private int encodeRecords(final Collection<IMonitoringRecord> records, final ByteBuffer buffer, final IRegistry<String> stringRegistry) { final int offsetBefore = buffer.position(); for (final IMonitoringRecord record : records) { // Since writeBytes does not contain the type name and the logging timestamp, // these two fields must be serialized separately final String typeName = record.getClass().getName(); final int typeNameId = stringRegistry.get(typeName); buffer.putInt(typeNameId); buffer.putLong(record.getLoggingTimestamp()); record.serialize(DefaultValueSerializer.create(buffer, stringRegistry)); } final int offsetAfter = buffer.position(); return (offsetAfter - offsetBefore); }
public SingleSocketTcpWriter(final Configuration configuration) throws IOException { super(configuration); final String hostname = configuration.getStringProperty(CONFIG_HOSTNAME); final int port = configuration.getIntProperty(CONFIG_PORT); // buffer size is available by byteBuffer.capacity() this.socketChannel = SocketChannel.open(new InetSocketAddress(hostname, port)); // TODO should we check for buffers too small for a single record? final int bufferSize = this.configuration.getIntProperty(CONFIG_BUFFERSIZE); this.buffer = ByteBuffer.allocateDirect(bufferSize); this.registryBuffer = ByteBuffer.allocateDirect(bufferSize); this.flush = configuration.getBooleanProperty(CONFIG_FLUSH); final WriterRegistry writerRegistry = new WriterRegistry(this); this.serializer = DefaultValueSerializer.create(this.buffer, new GetIdAdapter<>(writerRegistry)); }
@Override public void writeMonitoringRecord(final IMonitoringRecord monitoringRecord) { monitoringRecord.registerStrings(this.registerStringsAdapter); final ByteBuffer recordBuffer = this.buffer; final int requiredBufferSize = SIZE_OF_ENVELOPE + 4 + 8 + monitoringRecord.getSize(); if (recordBuffer.capacity() < requiredBufferSize) { throw new IllegalStateException("Insufficient buffer capacity for string registry data"); } // register monitoringRecord class name final String recordClassName = monitoringRecord.getClass().getName(); this.writerRegistry.register(recordClassName); // Prepend envelope data recordBuffer.put(REGULAR_RECORD_ID); recordBuffer.putLong(this.writerRegistry.getId()); // serialized monitoringRecord recordBuffer.putInt(this.writerRegistry.getId(recordClassName)); recordBuffer.putLong(monitoringRecord.getLoggingTimestamp()); monitoringRecord.serialize(DefaultValueSerializer.create(recordBuffer, this.writeBytesAdapter)); this.publishBuffer(recordBuffer); }
public TCPWriter(final Configuration configuration) throws IOException { super(configuration); final String hostname = configuration.getStringProperty(CONFIG_HOSTNAME); final int port1 = configuration.getIntProperty(CONFIG_PORT1); final int port2 = configuration.getIntProperty(CONFIG_PORT2); // TODO should be check for buffers too small for a single record? final int bufferSize = configuration.getIntProperty(CONFIG_BUFFERSIZE); int stringRegistryBufferSize = configuration.getIntProperty(CONFIG_STRING_REGISTRY_BUFFERSIZE); if (stringRegistryBufferSize <= 0) { LOG.warn("Invalid buffer size passed for string registry records: " + stringRegistryBufferSize + ". Defaults to " + DEFAULT_STRING_REGISTRY_BUFFER_SIZE); stringRegistryBufferSize = DEFAULT_STRING_REGISTRY_BUFFER_SIZE; } this.flush = configuration.getBooleanProperty(CONFIG_FLUSH); this.recordBuffer = ByteBuffer.allocateDirect(bufferSize); this.stringRegistryBuffer = ByteBuffer.allocateDirect(stringRegistryBufferSize); // buffer size is available by byteBuffer.capacity() this.monitoringRecordChannel = SocketChannel.open(new InetSocketAddress(hostname, port1)); this.registryRecordChannel = SocketChannel.open(new InetSocketAddress(hostname, port2)); final WriterRegistry writerRegistry = new WriterRegistry(this); this.serializer = DefaultValueSerializer.create(this.recordBuffer, new GetIdAdapter<>(writerRegistry)); }
@Override public void writeMonitoringRecord(final IMonitoringRecord monitoringRecord) { final PooledFileChannel channel = this.fileWriterPool.getFileWriter(this.buffer); monitoringRecord.registerStrings(this.registerStringsAdapter); final ByteBuffer recordBuffer = this.buffer; if ((4 + 8 + monitoringRecord.getSize()) > recordBuffer.remaining()) { channel.flush(recordBuffer, LOG); } final String recordClassName = monitoringRecord.getClass().getName(); this.writerRegistry.register(recordClassName); recordBuffer.putInt(this.writerRegistry.getId(recordClassName)); recordBuffer.putLong(monitoringRecord.getLoggingTimestamp()); monitoringRecord.serialize(DefaultValueSerializer.create(recordBuffer, this.writeBytesAdapter)); if (this.flush) { channel.flush(recordBuffer, LOG); } }
public DualSocketTcpWriter(final Configuration configuration) throws IOException { super(configuration); final String hostname = configuration.getStringProperty(CONFIG_HOSTNAME); final int monitoringPort = configuration.getIntProperty(CONFIG_PORT1); final int registryPort = configuration.getIntProperty(CONFIG_PORT2); // TODO should be check for buffers too small for a single record? final int bufferSize = configuration.getIntProperty(CONFIG_BUFFERSIZE); int stringRegistryBufferSize = configuration.getIntProperty(CONFIG_STRING_REGISTRY_BUFFERSIZE); if (stringRegistryBufferSize <= 0) { LOG.warn("Invalid buffer size passed for string registry records: " + stringRegistryBufferSize + ". Defaults to " + DEFAULT_STRING_REGISTRY_BUFFER_SIZE); stringRegistryBufferSize = DEFAULT_STRING_REGISTRY_BUFFER_SIZE; } this.flush = configuration.getBooleanProperty(CONFIG_FLUSH); this.recordBuffer = ByteBuffer.allocateDirect(bufferSize); this.stringRegistryBuffer = ByteBuffer.allocateDirect(stringRegistryBufferSize); // buffer size is available by byteBuffer.capacity() this.monitoringRecordChannel = SocketChannel.open(new InetSocketAddress(hostname, monitoringPort)); this.registryRecordChannel = SocketChannel.open(new InetSocketAddress(hostname, registryPort)); final WriterRegistry writerRegistry = new WriterRegistry(this); final GetIdAdapter<String> writeBytesAdapter = new GetIdAdapter<>(writerRegistry); this.serializer = DefaultValueSerializer.create(this.recordBuffer, writeBytesAdapter); // this.encoder = StandardCharsets.UTF_8.newEncoder(); // remove RegisterAdapter }