Example usage for io.netty.buffer ByteBuf release

List of usage examples for io.netty.buffer ByteBuf release

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf release.

Prototype

boolean release();

Source Link

Document

Decreases the reference count by 1 and deallocates this object if the reference count reaches at 0 .

Usage

From source file:com.addthis.hydra.task.source.Mark.java

License:Apache License

@Override
public void bytesDecode(byte[] b, long version) {
    ByteBuf buffer = Unpooled.wrappedBuffer(b);
    try {//from  w  w w  . j a v  a 2 s .com
        int valLength = Varint.readUnsignedVarInt(buffer);
        byte[] valBytes = new byte[valLength];
        buffer.readBytes(valBytes);
        setValue(new String(valBytes));
        setIndex(Varint.readUnsignedVarLong(buffer));
        setEnd(buffer.readByte() == 1);
        setError(Varint.readUnsignedVarInt(buffer));
    } finally {
        buffer.release();
    }
}

From source file:com.addthis.hydra.task.source.SimpleMark.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] retBytes = null;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
    try {//  w  w  w  .j av  a2s.c o  m
        byte[] valBytes = val.getBytes();
        Varint.writeUnsignedVarInt(valBytes.length, buffer);
        buffer.writeBytes(valBytes);
        Varint.writeUnsignedVarLong(index, buffer);
        buffer.writeByte(end ? 1 : 0);
        retBytes = new byte[buffer.readableBytes()];
        buffer.readBytes(retBytes);
    } finally {
        buffer.release();
    }
    return retBytes;
}

From source file:com.addthis.hydra.task.source.SimpleMark.java

License:Apache License

@Override
public void bytesDecode(byte[] b, long version) {
    ByteBuf buffer = Unpooled.wrappedBuffer(b);
    try {/*from w w  w  .j  a  va2 s .  com*/
        int valLength = Varint.readUnsignedVarInt(buffer);
        byte[] valBytes = new byte[valLength];
        buffer.readBytes(valBytes);
        val = new String(valBytes);
        index = Varint.readUnsignedVarLong(buffer);
        end = buffer.readByte() == 1;
    } finally {
        buffer.release();
    }
}

From source file:com.addthis.meshy.ChannelState.java

License:Apache License

public boolean send(final ByteBuf sendBuffer, final SendWatcher watcher, final int reportBytes) {
    int attempts = 1;
    while (channel.isActive()) {
        final boolean inEventLoop = channel.eventLoop().inEventLoop();
        // if this thread is the io loop for this channel: make sure it is allowed to do writes
        if (inEventLoop && !channel.isWritable()) {
            channel.unsafe().forceFlush();
        }//  www.ja  v  a2 s  .c om
        if (channel.isWritable() || (attempts >= 500)
        // if this thread is the io loop for this channel: block writes only based on the actual socket queue;
        // channel writability factors in queued writes from other threads that we can't get to until we finish.
                || (inEventLoop
                        && (channel.unsafe().outboundBuffer().size() < estimateMaxQueued(sendBuffer)))) {
            if (attempts >= 500) {
                log.warn(
                        "Forcing write despite channel being backed up to prevent possible distributed deadlock");
            }
            ChannelFuture future = channel.writeAndFlush(sendBuffer);
            future.addListener(ignored -> {
                meshy.sentBytes(reportBytes);
                if (watcher != null) {
                    watcher.sendFinished(reportBytes);
                }
            });
            return true;
        } else {
            if ((attempts % 100) == 0) {
                ChannelOutboundBuffer outboundBuffer = channel.unsafe().outboundBuffer();
                if (outboundBuffer != null) {
                    log.info(
                            "Sleeping write because channel is unwritable. Attempt: {}, Pending Bytes: {}, State: {}",
                            attempts, outboundBuffer.totalPendingWriteBytes(), this);
                }
            }
            sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
            writeSleeps.incrementAndGet();
            sleepMeter.mark();
        }
        attempts += 1;
    }
    if (reportBytes > 0) {
        /**
         * if bytes == 0 then it's a sendComplete() and
         * there are plenty of legit cases when a client would
         * disconnect before sendComplete() makes it back. best
         * example is StreamService when EOF framing tells the
         * client we're done before sendComplete() framing does.
         */
        log.debug("{} writing [{}] to dead channel", this, reportBytes);
        if (watcher != null) {
            /**
             * for accounting, rate limiting reasons, we have to report these as sent.
             * no need to report 0 bytes since it's an accounting no-op.
             */
            watcher.sendFinished(reportBytes);
        }
    }
    sendBuffer.release();
    return false;
}

From source file:com.addthis.meshy.ChannelState.java

License:Apache License

public void messageReceived(ByteBuf in) {
    log.trace("{} recv msg={}", this, in);
    meshy.recvBytes(in.readableBytes());
    buffer.writeBytes(in);// www  .j  a  v a  2  s .co m
    in.release();
    loop: while (true) {
        switch (mode) {
        // zero signifies a reply to a source
        case ReadType:
            if (buffer.readableBytes() < 4) {
                break loop;
            }
            type = buffer.readInt();
            mode = READMODE.ReadSession;
            continue;
        case ReadSession:
            if (buffer.readableBytes() < 4) {
                break loop;
            }
            session = buffer.readInt();
            mode = READMODE.ReadLength;
            continue;
        // zero length signifies end of session
        case ReadLength:
            if (buffer.readableBytes() < 4) {
                break loop;
            }
            length = buffer.readInt();
            mode = READMODE.ReadData;
            continue;
        case ReadData:
            int readable = buffer.readableBytes();
            if (readable < length) {
                break loop;
            }
            SessionHandler handler = null;
            if (type == MeshyConstants.KEY_RESPONSE) {
                handler = sourceHandlers.get(session);
            } else {
                handler = targetHandlers.get(session);
                if ((handler == null) && (meshy instanceof MeshyServer)) {
                    if (type != MeshyConstants.KEY_EXISTING) {
                        handler = meshy.createHandler(type);
                        ((TargetHandler) handler).setContext((MeshyServer) meshy, this, session);
                        log.debug("{} createHandler {} session={}", this, handler, session);
                        if (targetHandlers.put(session, handler) != null) {
                            log.debug("clobbered session {} with {}", session, handler);
                        }
                        if (targetHandlers.size() >= excessiveTargets) {
                            log.debug("excessive targets reached, current targetHandlers = {}",
                                    targetHandlers.size());
                            if (log.isTraceEnabled()) {
                                debugSessions();
                            }
                        }
                    } else {
                        log.debug("Ignoring bad handler creation request for session {} type {}", session,
                                type); // happens with fast streams and send-mores
                    }
                }
            }
            if (handler != null) {
                if (length == 0) {
                    sessionComplete(handler, type, session);
                } else {
                    try {
                        handler.receive(this, session, length, buffer);
                    } catch (Exception ex) {
                        log.error("suppressing handler exception during receive; trying receiveComplete", ex);
                        sessionComplete(handler, type, session);
                    }
                }
            }
            int read = readable - buffer.readableBytes();
            if (read < length) {
                if ((handler != null) || log.isDebugEnabled()) {
                    log.debug("{} recv type={} handler={} ssn={} did not consume all bytes (read={} of {})",
                            this, type, handler, session, read, length);
                }
                buffer.skipBytes(length - read);
            }
            mode = READMODE.ReadType;
            continue;
        default:
            throw new RuntimeException("invalid state");
        }
    }
    buffer.discardReadBytes();
}

From source file:com.addthis.meshy.SourceHandler.java

License:Apache License

@Override
public final boolean send(byte[] data, final SendWatcher watcher) {
    synchronized (channels) {
        if (channels.isEmpty()) {
            return false;
        }/*from   w ww  . j  av  a 2  s  . c  o m*/

        int sendType = MeshyConstants.KEY_EXISTING;
        if (sent.compareAndSet(false, true) || DISABLE_CREATION_FRAMES) {
            sendType = targetHandler;
        }

        final ByteBufAllocator alloc = channels.iterator().next().alloc();
        final ByteBuf buffer = allocateSendBuffer(alloc, sendType, session, data);
        final int reportBytes = data.length;
        final int peerCount = channels.size();

        log.trace("{} send {} to {}", this, buffer.capacity(), peerCount);
        List<ChannelFuture> futures = new ArrayList<>(peerCount);
        for (Channel c : channels) {
            futures.add(c.writeAndFlush(buffer.duplicate().retain()));
        }
        AggregateChannelFuture aggregateFuture = new AggregateChannelFuture(futures);
        aggregateFuture.addListener(ignored -> {
            master.sentBytes(reportBytes * peerCount);
            if (watcher != null) {
                watcher.sendFinished(reportBytes);
            }
        });
        buffer.release();
        return true;
    }
}

From source file:com.addthis.meshy.SourceHandler.java

License:Apache License

protected boolean sendToSingleTarget(Channel channel, byte[] data) {
    int sendType = MeshyConstants.KEY_EXISTING;
    final ByteBufAllocator alloc = channel.alloc();
    final ByteBuf buffer = allocateSendBuffer(alloc, sendType, session, data);

    final int reportBytes = data.length;
    log.trace("{} send {} to {}", this, buffer.capacity(), channel);
    channel.writeAndFlush(buffer.duplicate().retain()).addListener(ignored -> {
        master.sentBytes(reportBytes);//w  w  w . j a  v a 2 s. c  o m
    });
    buffer.release();
    return true;
}

From source file:com.addthis.muxy.MuxStreamDirectory.java

License:Apache License

protected long writeStreamsToBlock() throws IOException {
    long writtenBytes = 0;
    openWritesLock.lock();//from w  w w .j  a v  a 2s.c  om
    try {
        /* yes, this could be optimized for concurrency by writing after lock is released, etc */
        if (openWriteBytes.get() == 0) {
            return 0;
        }
        List<TempData> streamsWithData = new ArrayList<>(openStreamWrites.size());
        for (StreamOut out : openStreamWrites.values()) {
            synchronized (out) {
                StreamOut pendingOut = pendingStreamCloses.get(out.meta.streamId);
                if (pendingOut != null) {
                    pendingOut.outputBuffer.writeBytes(out.outputBuffer);
                    assert out.outputBuffer.readableBytes() == 0;
                    out.outputBuffer.discardSomeReadBytes();
                } else if (out.output.buffer().readableBytes() > 0) {
                    streamsWithData.add(new TempData(out));
                    out.outputBuffer.retain();
                }
            }
        }
        for (StreamOut out : pendingStreamCloses.values()) { // guarded by openStreamWrites
            streamsWithData.add(new TempData(out));
        }
        pendingStreamCloses.clear();
        if (streamsWithData.isEmpty()) {
            return 0;
        }
        for (TempData td : streamsWithData) {
            writtenBytes += td.snapshotLength;
        }
        int streams = streamsWithData.size();
        publishEvent(MuxyStreamEvent.BLOCK_FILE_WRITE, streams);
        int currentFileOffset = (int) openWriteFile.size();
        /* write out IDs in this block */
        ByteBuf metaBuffer = PooledByteBufAllocator.DEFAULT.directBuffer(2 + 4 * streams + 4 + 8 * streams);
        metaBuffer.writeShort(streams);
        int bodyOutputSize = 0;
        for (TempData out : streamsWithData) {
            metaBuffer.writeInt(out.meta.streamId);
            /* (4) chunk body offset (4) chunk length (n) chunk bytes */
            bodyOutputSize += 8 + out.snapshotLength;
        }
        /* write remainder size for rest of block data so that readers can skip if desired ID isn't present */
        metaBuffer.writeInt(bodyOutputSize);
        /* write offsets and lengths for each stream id */
        int bodyOffset = streamsWithData.size() * 8;
        for (TempData out : streamsWithData) {
            metaBuffer.writeInt(bodyOffset); //TODO - reconsider how frequently this shortcut is placed on disk
            metaBuffer.writeInt(out.snapshotLength);
            bodyOffset += out.snapshotLength;
        }
        while (metaBuffer.readableBytes() > 0) {
            metaBuffer.readBytes(openWriteFile, metaBuffer.readableBytes());
        }
        metaBuffer.release();
        /* write bytes for each stream id */
        for (TempData out : streamsWithData) {
            synchronized (out.stream) { // need less confusing variable names for concurrency
                int toWrite = out.snapshotLength;
                while (toWrite > 0) {
                    int numBytesRead = out.data.readBytes(openWriteFile, toWrite);
                    assert numBytesRead > 0;
                    toWrite -= numBytesRead;
                }
                openWriteBytes.addAndGet((long) -out.snapshotLength);
                eventListener.reportWrite((long) -out.snapshotLength);
                out.meta.endFile = streamDirectoryConfig.currentFile.get();
                out.meta.endFileBlockOffset = currentFileOffset;
                if (out.meta.startFile == 0) {
                    out.meta.startFile = out.meta.endFile;
                    out.meta.startFileBlockOffset = out.meta.endFileBlockOffset;
                }
                if (!out.data.release()) { // release the pending writes that did not get an extra retain
                    out.data.discardSomeReadBytes();
                }
            }
        }
        /* check for rolling current file on size threshold */
        if (openWriteFile.size() > streamDirectoryConfig.maxFileSize) {
            openWriteFile.close();
            openWriteFile = FileChannel.open(getFileByID(bumpCurrentFile()), APPEND, CREATE);
            publishEvent(MuxyStreamEvent.BLOCK_FILE_WRITE_ROLL, streamDirectoryConfig.currentFile);
        }
    } finally {
        openWritesLock.unlock();
    }
    return writtenBytes;
}

From source file:com.adobe.acs.livereload.impl.WebSocketServerHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    if (res.getStatus().code() != HttpServletResponse.SC_OK) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);//  w w  w . ja v  a  2  s  . co  m
        buf.release();
        setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!isKeepAlive(req) || res.getStatus().code() != HttpServletResponse.SC_OK) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.aerofs.baseline.http.EntityInputStream.java

License:Apache License

@Override
public synchronized int read(@Nonnull byte[] b, int off, int len) throws IOException {
    // basic argument checks
    // pulled straight from InputStream
    if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }/*  w  ww .  ja  v a2 s  .  c o  m*/

    // if the sender already closed
    // the stream then we're going to
    // abort, *even if* the sender
    // has already sent the bytes we
    // care about
    throwIfClosed();

    // we may have to send a 100 CONTINUE
    // if the client specifically requests
    // it. to prevent the server from buffering
    // bytes we only send it the moment the
    // app thread starts reading from the
    // stream. this can be changed easily
    sendContinueIfRequested();

    int initialOffset = off;
    int bufferRemaining = len;

    while (true) {
        // get bytes from the network
        getNetworkBytes();

        // how many bytes can we read?
        int remaining = inputCompleted ? (int) Math.min(readable, bufferRemaining) : bufferRemaining;
        if (remaining == 0) {
            break;
        }

        Preconditions.checkState(!buffers.isEmpty(), "readable bytes, but no buffers");

        // read as much as required
        // from the top buffer
        ByteBuf head = buffers.get(0);
        int chunkReadable = Math.min(head.readableBytes(), remaining);
        head.readBytes(b, off, chunkReadable);

        // bookkeeping
        readable -= chunkReadable;
        off += chunkReadable;
        bufferRemaining -= chunkReadable;

        // remove the top buffer if it's exhausted
        if (head.readableBytes() == 0) {
            buffers.remove(0);
            head.release();
        }
    }

    return off == initialOffset ? -1 : off - initialOffset;
}