@Override public void onTerminating() { // Always flush the registryBuffer before flushing the recordBuffer. // Otherwise the monitoring records could arrive before their string // records WriterUtil.flushBuffer(this.registryBuffer, this.socketChannel, LOG); WriterUtil.flushBuffer(this.buffer, this.socketChannel, LOG); WriterUtil.close(this.socketChannel, LOG); } }
public void flush(final ByteBuffer buffer, final Log log) { this.bytesWritten += WriterUtil.flushBuffer(buffer, this.channel, log); }
public void close() { WriterUtil.close(this.currentFileWriter, this.writerLog); }
@Override public void onNewRegistryEntry(final String value, final int id) { final ByteBuffer localRegistryBuffer = this.registryBuffer; final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); // logging timestamp + class id + RegistryRecord.SIZE + bytes.length final int requiredBufferSize = (2 * AbstractMonitoringRecord.TYPE_SIZE_INT) + RegistryRecord.SIZE + bytes.length; if (localRegistryBuffer.remaining() < requiredBufferSize) { WriterUtil.flushBuffer(localRegistryBuffer, this.socketChannel, LOG); } localRegistryBuffer.putInt(RegistryRecord.CLASS_ID); localRegistryBuffer.putInt(id); localRegistryBuffer.putInt(value.length()); localRegistryBuffer.put(bytes); }
/** * @return the number of bytes written from the buffer to the channel */ public static long flushBuffer(final ByteBuffer buffer, final WritableByteChannel writableChannel, final Logger logger) { long bytesWritten = 0; buffer.flip(); try { while (buffer.hasRemaining()) { bytesWritten += writableChannel.write(buffer); } buffer.clear(); } catch (final IOException e) { logger.error("Caught exception while writing to the channel.", e); WriterUtil.close(writableChannel, logger); } return bytesWritten; }
@Override public void onTerminating() { WriterUtil.flushBuffer(this.stringRegistryBuffer, this.registryRecordChannel, LOG); WriterUtil.flushBuffer(this.recordBuffer, this.monitoringRecordChannel, LOG); WriterUtil.close(this.registryRecordChannel, LOG); WriterUtil.close(this.monitoringRecordChannel, LOG); } }
@Override public void onNewRegistryEntry(final String value, final int id) { final ByteBuffer localRegistryBuffer = this.registryBuffer; final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); // logging timestamp + class id + RegistryRecord.SIZE + bytes.length final int requiredBufferSize = (2 * AbstractMonitoringRecord.TYPE_SIZE_INT) + RegistryRecord.SIZE + bytes.length; if (localRegistryBuffer.remaining() < requiredBufferSize) { WriterUtil.flushBuffer(localRegistryBuffer, this.socketChannel, LOGGER); } localRegistryBuffer.putInt(RegistryRecord.CLASS_ID); localRegistryBuffer.putInt(id); localRegistryBuffer.putInt(value.length()); localRegistryBuffer.put(bytes); }
@Override public void close() throws IOException { this.buffer.flip(); try { while (this.buffer.hasRemaining()) { this.numOfBytes += this.outputChannel.write(this.buffer); } this.buffer.clear(); } catch (final IOException e) { LOGGER.error("Caught exception while writing to the channel.", e); WriterUtil.close(this.outputChannel, LOGGER); } this.serializedStream.flush(); super.close(); }
@Override public void onTerminating() { WriterUtil.flushBuffer(this.stringRegistryBuffer, this.registryRecordChannel, LOGGER); WriterUtil.flushBuffer(this.recordBuffer, this.monitoringRecordChannel, LOGGER); WriterUtil.close(this.registryRecordChannel, LOGGER); WriterUtil.close(this.monitoringRecordChannel, LOGGER); } }
@Override public void onNewRegistryEntry(final String value, final int id) { final ByteBuffer buffer = this.stringRegistryBuffer; final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); // final ByteBuffer valueInByteBuffer = StandardCharsets.UTF_8.encode(value); final int requiredBufferSize = RegistryRecord.SIZE + bytes.length; if (buffer.capacity() < requiredBufferSize) { // stringRegistryBuffer = ByteBuffer.allocateDirect(RegistryRecord.SIZE + bytes.length); throw new IllegalStateException("Insufficient capacity for string registry buffer"); } // loggingTimestamp not transmitted by dual socket communication // class id not used by dual socket communication buffer.putInt(id); buffer.putInt(value.length()); buffer.put(bytes); // always flush so that on the reader side the records can be reconstructed WriterUtil.flushBuffer(buffer, this.registryRecordChannel, LOG); }
/** * @return the number of bytes written from the buffer to the channel */ public static long flushBuffer(final ByteBuffer buffer, final WritableByteChannel writableChannel, final Log log) { long bytesWritten = 0; buffer.flip(); try { while (buffer.hasRemaining()) { bytesWritten += writableChannel.write(buffer); } buffer.clear(); } catch (final IOException e) { log.error("Caught exception while writing to the channel.", e); WriterUtil.close(writableChannel, log); } return bytesWritten; }
@Override public void onTerminating() { // Always flush the registryBuffer before flushing the recordBuffer. Otherwise // the monitoring records could arrive before their string records WriterUtil.flushBuffer(this.registryBuffer, this.socketChannel, LOGGER); WriterUtil.flushBuffer(this.buffer, this.socketChannel, LOGGER); WriterUtil.close(this.socketChannel, LOGGER); } }
@Override public void onNewRegistryEntry(final String value, final int id) { final ByteBuffer buffer = this.stringRegistryBuffer; final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); // final ByteBuffer valueInByteBuffer = StandardCharsets.UTF_8.encode(value); final int requiredBufferSize = RegistryRecord.SIZE + bytes.length; if (buffer.capacity() < requiredBufferSize) { // stringRegistryBuffer = ByteBuffer.allocateDirect(RegistryRecord.SIZE + bytes.length); throw new IllegalStateException("Insufficient capacity for string registry buffer"); } // loggingTimestamp not transmitted by dual socket communication // class id not used by dual socket communication buffer.putInt(id); buffer.putInt(value.length()); buffer.put(bytes); // always flush so that the stringRegistryBuffer is transmitted before the record buffer WriterUtil.flushBuffer(buffer, this.registryRecordChannel, LOG); }
/** * Request space in the buffer, if necessary flush the buffer. * * @param bufferSpace * requested size * @param log * @throws IOException */ private void requestBufferSpace(final int bufferSpace) throws IOException { if (bufferSpace > this.buffer.remaining()) { this.buffer.flip(); try { while (this.buffer.hasRemaining()) { this.numOfBytes += this.outputChannel.write(this.buffer); } this.buffer.clear(); } catch (final IOException e) { LOGGER.error("Caught exception while writing to the channel.", e); WriterUtil.close(this.outputChannel, LOGGER); } if (this.flushLogFile) { this.serializedStream.flush(); } } } }
/** * Flushes the buffer and closes the channel afterwards. */ public void close(final ByteBuffer buffer, final Log log) { this.bytesWritten += WriterUtil.flushBuffer(buffer, this.channel, log); WriterUtil.close(this.channel, log); } }
@Override public void onNewRegistryEntry(final String value, final int id) { final ByteBuffer buffer = this.stringRegistryBuffer; final byte[] bytes = value.getBytes(StandardCharsets.UTF_8); // final ByteBuffer valueInByteBuffer = StandardCharsets.UTF_8.encode(value); final int requiredBufferSize = RegistryRecord.SIZE + bytes.length; if (buffer.capacity() < requiredBufferSize) { // stringRegistryBuffer = ByteBuffer.allocateDirect(RegistryRecord.SIZE + bytes.length); throw new IllegalStateException("Insufficient capacity for string registry buffer"); } // loggingTimestamp not transmitted by dual socket communication // class id not used by dual socket communication buffer.putInt(id); buffer.putInt(value.length()); buffer.put(bytes); // always flush so that the stringRegistryBuffer is transmitted before the record buffer WriterUtil.flushBuffer(buffer, this.registryRecordChannel, LOGGER); }
@Override public void serialize(final IMonitoringRecord record, final int id) throws IOException { final String header = String.format("$%d;%d", id, record.getLoggingTimestamp()); this.buffer.put(header); record.serialize(this.serializer); this.buffer.put(LINE_SEPARATOR); this.buffer.flip(); try { while (this.buffer.hasRemaining()) { this.numOfBytes += this.outputChannel.write(this.charset.encode(this.buffer)); } this.numOfEntries++; this.buffer.clear(); if (this.flushLogFile) { this.serializedStream.flush(); } } catch (final IOException e) { LOGGER.error("Caught exception while writing to the channel.", e); WriterUtil.close(this.outputChannel, LOGGER); } }
@Override public void onTerminating() { WriterUtil.flushBuffer(this.stringRegistryBuffer, this.registryRecordChannel, LOG); WriterUtil.flushBuffer(this.recordBuffer, this.monitoringRecordChannel, LOG); WriterUtil.close(this.registryRecordChannel, LOG); WriterUtil.close(this.monitoringRecordChannel, LOG); }
@Override public void writeMonitoringRecord(final IMonitoringRecord monitoringRecord) { final ByteBuffer recordBuffer = this.buffer; if ((4 + 8 + monitoringRecord.getSize()) > recordBuffer.remaining()) { // Always flush the registryBuffer before flushing the recordBuffer. Otherwise // the monitoring records could arrive before their string records WriterUtil.flushBuffer(this.registryBuffer, this.socketChannel, LOGGER); WriterUtil.flushBuffer(recordBuffer, this.socketChannel, LOGGER); } final String recordClassName = monitoringRecord.getClass().getName(); this.serializer.putString(recordClassName); this.serializer.putLong(monitoringRecord.getLoggingTimestamp()); monitoringRecord.serialize(this.serializer); if (this.flush) { // Always flush the registryBuffer before flushing the recordBuffer. Otherwise // the monitoring records could arrive before their string records WriterUtil.flushBuffer(this.registryBuffer, this.socketChannel, LOGGER); WriterUtil.flushBuffer(recordBuffer, this.socketChannel, LOGGER); } }
private void onThresholdExceeded() { WriterUtil.close(this.currentFileWriter, this.writerLog); this.currentFileNumber++; final Path newFile = this.getNextFileName(this.currentFileNumber, this.fileExtensionWithDot); try { Files.createDirectories(this.folder); // use CREATE_NEW to fail if the file already exists OutputStream outputStream = Files.newOutputStream(newFile, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); if (this.shouldCompress) { // final GZIPOutputStream compressedOutputStream = new GZIPOutputStream(outputStream); final ZipOutputStream compressedOutputStream = new ZipOutputStream(outputStream); final ZipEntry newZipEntry = new ZipEntry(newFile.toString() + FSUtil.NORMAL_FILE_EXTENSION); compressedOutputStream.putNextEntry(newZipEntry); outputStream = compressedOutputStream; } // this.currentFileWriter = Channels.newChannel(outputStream); Writer writer = new OutputStreamWriter(outputStream, this.charset); writer = new BufferedWriter(writer); this.currentMeasuringWriter = new MeasuringWriter(writer); this.currentFileWriter = new PrintWriter(this.currentMeasuringWriter); } catch (final IOException e) { throw new IllegalStateException("This exception should not have been thrown.", e); } this.numEntriesInCurrentFile = 1; }