List of usage examples for io.netty.buffer ByteBuf retain
@Override public abstract ByteBuf retain();
From source file:org.apache.bookkeeper.mledger.impl.OpAddEntry.java
License:Apache License
public static OpAddEntry create(ManagedLedgerImpl ml, ByteBuf data, AddEntryCallback callback, Object ctx) { OpAddEntry op = RECYCLER.get();// w ww. j a va2 s. co m op.ml = ml; op.ledger = null; op.data = data.retain(); op.dataLength = data.readableBytes(); op.callback = callback; op.ctx = ctx; op.closeWhenDone = false; op.entryId = -1; op.startTime = System.nanoTime(); ml.mbean.addAddEntrySample(op.dataLength); if (log.isDebugEnabled()) { log.debug("Created new OpAddEntry {}", op); } return op; }
From source file:org.apache.camel.component.netty4.codec.DatagramPacketByteArrayEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception { if (msg.content() instanceof byte[]) { delegateEncoder.encode(ctx, (byte[]) msg.content(), out); ByteBuf buf = (ByteBuf) out.remove(out.size() - 1); AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>( buf.retain(), msg.recipient(), msg.sender()); out.add(addressedEnvelop);// www .ja v a2 s. c o m } }
From source file:org.apache.camel.component.netty4.codec.DatagramPacketEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception { if (msg.content() instanceof ByteBuf) { ByteBuf payload = (ByteBuf) msg.content(); // Just wrap the message as DatagramPacket, need to make sure the message content is ByteBuf DatagramPacket dp = new DatagramPacket(payload.retain(), msg.recipient()); out.add(dp);//from ww w . j a va 2s. co m } }
From source file:org.apache.camel.component.netty4.codec.DatagramPacketObjectEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception { if (msg.content() instanceof Serializable) { Serializable payload = (Serializable) msg.content(); ByteBuf buf = ctx.alloc().heapBuffer(); delegateObjectEncoder.encode(ctx, payload, buf); AddressedEnvelope<Object, InetSocketAddress> addressedEnvelop = new DefaultAddressedEnvelope<Object, InetSocketAddress>( buf.retain(), msg.recipient(), msg.sender()); out.add(addressedEnvelop);/*from w ww. j av a 2 s . co m*/ } }
From source file:org.apache.distributedlog.io.IdentityCompressionCodec.java
License:Apache License
@Override public ByteBuf compress(ByteBuf uncompressed, int headerLen) { checkNotNull(uncompressed);/*w w w . ja v a 2 s . co m*/ checkArgument(uncompressed.readableBytes() >= 0); if (headerLen == 0) { return uncompressed.retain(); } else { CompositeByteBuf composited = PooledByteBufAllocator.DEFAULT.compositeBuffer(2); composited.addComponent(PooledByteBufAllocator.DEFAULT.buffer(headerLen, headerLen)); composited.addComponent(uncompressed.retain()); return composited; } }
From source file:org.apache.distributedlog.io.IdentityCompressionCodec.java
License:Apache License
@Override public ByteBuf decompress(ByteBuf compressed, int decompressedSize) { checkNotNull(compressed);// ww w . j av a 2s .c o m checkArgument(compressed.readableBytes() >= 0); checkArgument(decompressedSize >= 0); return compressed.retain(); }
From source file:org.apache.distributedlog.TestEntry.java
License:Apache License
@Test(timeout = 20000) public void testWriteRecords() throws Exception { Writer writer = Entry.newEntry("test-write-records", 1024, true, CompressionCodec.Type.NONE); assertEquals("zero bytes", HEADER_LENGTH, writer.getNumBytes()); assertEquals("zero records", 0, writer.getNumRecords()); List<CompletableFuture<DLSN>> writePromiseList = Lists.newArrayList(); // write first 5 records for (int i = 0; i < 5; i++) { LogRecord record = new LogRecord(i, ("record-" + i).getBytes(UTF_8)); record.setPositionWithinLogSegment(i); CompletableFuture<DLSN> writePromise = new CompletableFuture<DLSN>(); writer.writeRecord(record, writePromise); writePromiseList.add(writePromise); assertEquals((i + 1) + " records", (i + 1), writer.getNumRecords()); }/*from w ww . j a v a 2 s. c o m*/ // write large record LogRecord largeRecord = new LogRecord(1L, new byte[MAX_LOGRECORD_SIZE + 1]); try { writer.writeRecord(largeRecord, new CompletableFuture<DLSN>()); Assert.fail("Should fail on writing large record"); } catch (LogRecordTooLongException lrtle) { // expected } assertEquals("5 records", 5, writer.getNumRecords()); // write another 5 records for (int i = 0; i < 5; i++) { LogRecord record = new LogRecord(i + 5, ("record-" + (i + 5)).getBytes(UTF_8)); record.setPositionWithinLogSegment(i + 5); CompletableFuture<DLSN> writePromise = new CompletableFuture<DLSN>(); writer.writeRecord(record, writePromise); writePromiseList.add(writePromise); assertEquals((i + 6) + " records", (i + 6), writer.getNumRecords()); } ByteBuf buffer = writer.getBuffer(); buffer.retain(); // Test transmit complete writer.completeTransmit(1L, 1L); List<DLSN> writeResults = Utils.ioResult(FutureUtils.collect(writePromiseList)); for (int i = 0; i < 10; i++) { assertEquals(new DLSN(1L, 1L, i), writeResults.get(i)); } // Test reading from buffer Reader reader = Entry.newBuilder().setEntry(buffer).setLogSegmentInfo(1L, 1L).setEntryId(0L) .setEnvelopeEntry(true).buildReader(); buffer.release(); LogRecordWithDLSN record = reader.nextRecord(); int numReads = 0; long expectedTxid = 0L; while (null != record) { assertEquals(expectedTxid, record.getTransactionId()); assertEquals(expectedTxid, record.getSequenceId()); assertEquals(new DLSN(1L, 0L, expectedTxid), record.getDlsn()); ++numReads; ++expectedTxid; record = reader.nextRecord(); } assertEquals(10, numReads); reader.release(); }
From source file:org.apache.distributedlog.TestEntry.java
License:Apache License
@Test(timeout = 20000) public void testWriteRecordSet() throws Exception { Writer writer = Entry.newEntry("test-write-recordset", 1024, true, CompressionCodec.Type.NONE); assertEquals("zero bytes", HEADER_LENGTH, writer.getNumBytes()); assertEquals("zero records", 0, writer.getNumRecords()); List<CompletableFuture<DLSN>> writePromiseList = Lists.newArrayList(); // write first 5 records for (int i = 0; i < 5; i++) { LogRecord record = new LogRecord(i, ("record-" + i).getBytes(UTF_8)); record.setPositionWithinLogSegment(i); CompletableFuture<DLSN> writePromise = new CompletableFuture<DLSN>(); writer.writeRecord(record, writePromise); writePromiseList.add(writePromise); assertEquals((i + 1) + " records", (i + 1), writer.getNumRecords()); }/*from w w w . j a v a2 s . c o m*/ final LogRecordSet.Writer recordSetWriter = LogRecordSet.newWriter(1024, CompressionCodec.Type.NONE); List<CompletableFuture<DLSN>> recordSetPromiseList = Lists.newArrayList(); // write another 5 records as a batch for (int i = 0; i < 5; i++) { ByteBuffer record = ByteBuffer.wrap(("record-" + (i + 5)).getBytes(UTF_8)); CompletableFuture<DLSN> writePromise = new CompletableFuture<DLSN>(); recordSetWriter.writeRecord(record, writePromise); recordSetPromiseList.add(writePromise); assertEquals((i + 1) + " records", (i + 1), recordSetWriter.getNumRecords()); } final ByteBuf recordSetBuffer = recordSetWriter.getBuffer(); LogRecord setRecord = new LogRecord(5L, recordSetBuffer); setRecord.setPositionWithinLogSegment(5); setRecord.setRecordSet(); CompletableFuture<DLSN> writePromise = new CompletableFuture<DLSN>(); writePromise.whenComplete(new FutureEventListener<DLSN>() { @Override public void onSuccess(DLSN dlsn) { recordSetWriter.completeTransmit(dlsn.getLogSegmentSequenceNo(), dlsn.getEntryId(), dlsn.getSlotId()); } @Override public void onFailure(Throwable cause) { recordSetWriter.abortTransmit(cause); } }); writer.writeRecord(setRecord, writePromise); writePromiseList.add(writePromise); // write last 5 records for (int i = 0; i < 5; i++) { LogRecord record = new LogRecord(i + 10, ("record-" + (i + 10)).getBytes(UTF_8)); record.setPositionWithinLogSegment(i + 10); writePromise = new CompletableFuture<DLSN>(); writer.writeRecord(record, writePromise); writePromiseList.add(writePromise); assertEquals((i + 11) + " records", (i + 11), writer.getNumRecords()); } ByteBuf buffer = writer.getBuffer(); buffer.retain(); // Test transmit complete writer.completeTransmit(1L, 1L); List<DLSN> writeResults = Utils.ioResult(FutureUtils.collect(writePromiseList)); for (int i = 0; i < 5; i++) { assertEquals(new DLSN(1L, 1L, i), writeResults.get(i)); } assertEquals(new DLSN(1L, 1L, 5), writeResults.get(5)); for (int i = 0; i < 5; i++) { assertEquals(new DLSN(1L, 1L, (10 + i)), writeResults.get(6 + i)); } List<DLSN> recordSetWriteResults = Utils.ioResult(FutureUtils.collect(recordSetPromiseList)); for (int i = 0; i < 5; i++) { assertEquals(new DLSN(1L, 1L, (5 + i)), recordSetWriteResults.get(i)); } // Test reading from buffer verifyReadResult(buffer, 1L, 1L, 1L, true, new DLSN(1L, 1L, 2L), 3, 5, 5, new DLSN(1L, 1L, 2L), 2L); verifyReadResult(buffer, 1L, 1L, 1L, true, new DLSN(1L, 1L, 7L), 0, 3, 5, new DLSN(1L, 1L, 7L), 7L); verifyReadResult(buffer, 1L, 1L, 1L, true, new DLSN(1L, 1L, 12L), 0, 0, 3, new DLSN(1L, 1L, 12L), 12L); verifyReadResult(buffer, 1L, 1L, 1L, false, new DLSN(1L, 1L, 2L), 3, 5, 5, new DLSN(1L, 1L, 2L), 2L); verifyReadResult(buffer, 1L, 1L, 1L, false, new DLSN(1L, 1L, 7L), 0, 3, 5, new DLSN(1L, 1L, 7L), 7L); verifyReadResult(buffer, 1L, 1L, 1L, false, new DLSN(1L, 1L, 12L), 0, 0, 3, new DLSN(1L, 1L, 12L), 12L); buffer.release(); }
From source file:org.apache.drill.exec.rpc.ChunkCreationHandler.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { if (RpcConstants.EXTRA_DEBUGGING) { logger.debug("ChunkCreationHandler called with msg {} of size {} with chunkSize {}", msg, msg.readableBytes(), chunkSize); }//w ww .j a v a2s . c om if (!ctx.channel().isOpen()) { logger.debug("Channel closed, skipping encode inside {}.", RpcConstants.CHUNK_CREATION_HANDLER); msg.release(); return; } // Calculate the number of chunks based on configured chunk size and input msg size int numChunks = (int) Math.ceil((double) msg.readableBytes() / chunkSize); // Initialize a composite buffer to hold numChunks chunk. final CompositeByteBuf cbb = ctx.alloc().compositeBuffer(numChunks); int cbbWriteIndex = 0; int currentChunkLen = min(msg.readableBytes(), chunkSize); // Create slices of chunkSize from input msg and add it to the composite buffer. while (numChunks > 0) { final ByteBuf chunkBuf = msg.slice(msg.readerIndex(), currentChunkLen); chunkBuf.retain(); cbb.addComponent(chunkBuf); cbbWriteIndex += currentChunkLen; msg.skipBytes(currentChunkLen); --numChunks; currentChunkLen = min(msg.readableBytes(), chunkSize); } // Update the writerIndex of composite byte buffer. Netty doesn't do it automatically. cbb.writerIndex(cbbWriteIndex); // Add the final composite bytebuf into output buffer. out.add(cbb); }
From source file:org.apache.drill.exec.rpc.RpcCheckedFuture.java
License:Apache License
public void setBuffer(ByteBuf buffer) { if (buffer != null) { buffer.retain(); this.buffer = buffer; } }