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(); } }