public void clearBuffers() { for (RecordSerializer<?> serializer : serializers) { synchronized (serializer) { try { Buffer buffer = serializer.getCurrentBuffer(); if (buffer != null) { buffer.recycle(); } } finally { serializer.clear(); } } } }
public void flush() throws IOException { for (int targetChannel = 0; targetChannel < numChannels; targetChannel++) { RecordSerializer<T> serializer = serializers[targetChannel]; synchronized (serializer) { try { Buffer buffer = serializer.getCurrentBuffer(); if (buffer != null) { numBytesOut.inc(buffer.getSize()); targetPartition.writeBuffer(buffer, targetChannel); } } finally { serializer.clear(); } } } }
public void broadcastEvent(AbstractEvent event) throws IOException, InterruptedException { final Buffer eventBuffer = EventSerializer.toBuffer(event); try { for (int targetChannel = 0; targetChannel < numChannels; targetChannel++) { RecordSerializer<T> serializer = serializers[targetChannel]; synchronized (serializer) { Buffer buffer = serializer.getCurrentBuffer(); if (buffer != null) { numBytesOut.inc(buffer.getSize()); writeAndClearBuffer(buffer, targetChannel, serializer); } else if (serializer.hasData()) { // sanity check throw new IllegalStateException("No buffer, but serializer has buffered data."); } // retain the buffer so that it can be recycled by each channel of targetPartition eventBuffer.retain(); targetPartition.writeBuffer(eventBuffer, targetChannel); } } } finally { // we do not need to further retain the eventBuffer // (it will be recycled after the last channel stops using it) eventBuffer.recycle(); } }
private void sendToTarget(T record, int targetChannel) throws IOException, InterruptedException { RecordSerializer<T> serializer = serializers[targetChannel]; synchronized (serializer) { SerializationResult result = serializer.addRecord(record); while (result.isFullBuffer()) { Buffer buffer = serializer.getCurrentBuffer(); if (buffer != null) { numBytesOut.inc(buffer.getSize()); writeAndClearBuffer(buffer, targetChannel, serializer); // If this was a full record, we are done. Not breaking // out of the loop at this point will lead to another // buffer request before breaking out (that would not be // a problem per se, but it can lead to stalls in the // pipeline). if (result.isFullRecord()) { break; } } else { buffer = targetPartition.getBufferProvider().requestBufferBlocking(); result = serializer.setNextBuffer(buffer); } } } }