Example usage for io.netty.buffer ByteBuf readableBytes

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

Introduction

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

Prototype

public abstract int readableBytes();

Source Link

Document

Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex) .

Usage

From source file:com.heliosapm.streams.metrichub.HubManager.java

License:Apache License

protected HttpRequest buildHttpRequest(final ByteBuf jsonRequest) {
    final String[] endpoints = tsdbEndpoint.getUpServers();
    final URL postUrl = URLHelper.toURL(endpoints[0] + "/query/");
    log.debug("Http Post to [{}]", postUrl);
    final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            postUrl.getPath(), jsonRequest);
    request.headers().set(HttpHeaderNames.HOST, postUrl.getHost());
    request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
    //      request.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
    request.headers().set(HttpHeaderNames.CONTENT_LENGTH, jsonRequest.readableBytes());
    request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
    return request;
}

From source file:com.heliosapm.streams.metrics.StreamedMetric.java

License:Open Source License

/**
 * Returns a byte array containing the serialized streammetric
 * @return a byte array //from  w  w  w  .j a  v  a2 s .c om
 */
public byte[] toByteArray() {
    final ByteBuf buff = BufferManager.getInstance().directBuffer(byteSize);
    try {
        buff.writeByte(TYPE_CODE);
        writeByteArray(buff);
        return ByteBufUtil.getBytes(buff, 0, buff.readableBytes());

    } finally {
        try {
            buff.release();
        } catch (Exception x) {
            /* No Op */}
    }
}

From source file:com.heliosapm.streams.metrics.StreamedMetric.java

License:Open Source License

/**
 * Creates a StreamedMetric from the passed buffer
 * @param buff The buffer to read the StreamedMetric from
 * @return the created StreamedMetric/*from  w w w  . ja va2  s  .c  o m*/
 */
static StreamedMetric fromBuff(final ByteBuf buff) {
    final StreamedMetric sm = new StreamedMetric();
    sm.byteSize = buff.readableBytes() + 1;
    sm.readFromBuff(buff);
    return sm;
}

From source file:com.heliosapm.streams.metrics.StreamedMetricValue.java

License:Apache License

/**
 * Returns an interator over the StreamMetricValues in the passed buffer
 * @param singleInstance If true, the StreamMetricValue returns from the iterator will be the same actual instance, updated on each loop of the iterator.
 * As such, the returned StreamMetricValue should be used before the next iterator loop since the values of that instance will change.
 * In other words, attempting to stash all the returned StreamMetricValues in a collection, or the like, will void the warranty. 
 * @param buf The buffer to read from//from   www  . j  av a2 s  . co m
 * @param releaseOnDone true to release the buffer on iterator end
 * @return the iterator
 * FIXME: all this stuff needs to return SM or SMV
 */
public static Iterable<StreamedMetricValue> streamedMetricValues(final boolean singleInstance,
        final ByteBuf buf, final boolean releaseOnDone) {
    final StreamedMetricValue single = singleInstance ? new StreamedMetricValue() : null;
    return new Iterable<StreamedMetricValue>() {
        @Override
        public Iterator<StreamedMetricValue> iterator() {
            return new Iterator<StreamedMetricValue>() {
                @Override
                public boolean hasNext() {
                    final boolean hasNext = buf.readableBytes() > MIN_READABLE_BYTES;
                    if (!hasNext && releaseOnDone)
                        buf.release();
                    return hasNext;
                }

                @Override
                public StreamedMetricValue next() {
                    if (singleInstance) {
                        buf.readByte();
                        return single.update(buf);
                    }
                    return read(buf).forValue(1L);
                }
            };
        }
    };
}

From source file:com.heliosapm.streams.metrics.StreamedMetricValue.java

License:Apache License

/**
 * Returns a byte array containing the serialized streammetric
 * @return a byte array // w w w  .  j  a v a 2 s.  co  m
 */
@Override
public byte[] toByteArray() {
    final ByteBuf buff = BufferManager.getInstance().directBuffer(byteSize);
    try {
        buff.writeByte(TYPE_CODE);
        writeByteArray(buff);
        if (isDoubleValue) {
            buff.writeByte(0);
            buff.writeDouble(doubleValue);
        } else {
            buff.writeByte(1);
            buff.writeLong(longValue);
        }
        return ByteBufUtil.getBytes(buff, 0, buff.readableBytes());
    } finally {
        try {
            buff.release();
        } catch (Exception x) {
            /* No Op */}
    }
}

From source file:com.heliosapm.streams.metrics.StreamedMetricValue.java

License:Apache License

/**
 * Creates a StreamedMetricValue from the passed buffer
 * @param bytes The byte to read the StreamedMetric from
 * @return the created StreamedMetric//  ww  w . j av  a  2 s  .  c o  m
 */
static StreamedMetricValue fromBuff(final ByteBuf buff) {
    final StreamedMetricValue sm = new StreamedMetricValue();
    sm.byteSize = buff.readableBytes() + 1;
    sm.readFromBuff(buff);
    final byte type = buff.readByte();
    if (type == 0) {
        sm.isDoubleValue = true;
        sm.doubleValue = buff.readDouble();
    } else {
        sm.isDoubleValue = false;
        sm.longValue = buff.readLong();
    }
    return sm;
}

From source file:com.heliosapm.streams.onramp.GZipDetector.java

License:Apache License

/**
 * {@inheritDoc}// ww  w  .  j av a 2  s .  c  o  m
 * @see io.netty.handler.codec.ByteToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)
 */
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf buff, final List<Object> out)
        throws Exception {
    if (buff.readableBytes() > 4) {
        final int magic1 = buff.getUnsignedByte(buff.readerIndex());
        final int magic2 = buff.getUnsignedByte(buff.readerIndex() + 1);
        if (isGzip(magic1, magic2)) {
            enableGzip(ctx);
        } else {
            ctx.pipeline().remove(this);
        }
        out.add(buff.retain());
    }
}

From source file:com.heliosapm.streams.onramp.HttpJsonRpcHandler.java

License:Apache License

/**
 * {@inheritDoc}/*  w  w  w.j  a v  a  2s. c  o m*/
 * @see io.netty.handler.codec.MessageToMessageDecoder#decode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List)
 */
@Override
protected void decode(final ChannelHandlerContext ctx, final FullHttpRequest msg, final List<Object> out)
        throws Exception {
    int forwarded = 0;
    int nodes = 0;
    final ElapsedTime et = SystemClock.startClock();
    final ByteBuf buff = msg.content();
    if (buff.readableBytes() < 2) {
        log.info("Request from [{}] had no content: {}", ctx.channel().remoteAddress(), buff);
        // send response
        return;
    }

    final ArrayList<ObjectNode> metricNodes = new ArrayList<ObjectNode>(256);
    final JsonNode rootNode = JSONOps.parseToNode(buff);
    if (rootNode.isArray()) {
        for (JsonNode node : rootNode) {
            if (node.has("metric")) {
                metricNodes.add((ObjectNode) node);
                nodes++;
                if (nodes == batchSize) {
                    try {
                        nodes = 0;
                        forwarded += forwardMetrics(metricNodes);
                    } finally {
                        metricNodes.clear();
                    }
                }
            }
        }
        if (!metricNodes.isEmpty())
            try {
                nodes += metricNodes.size();
                forwarded += forwardMetrics(metricNodes);
            } finally {
                metricNodes.clear();
            }
    } else {
        if (rootNode.has("metric")) {
            forwarded += forwardMetrics(Collections.singletonList((ObjectNode) rootNode));
        }
    }
    ctx.channel().pipeline()
            .writeAndFlush(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NO_CONTENT));
    log.info("Wrote [{}] metrics: {}", forwarded, et.printAvg("Metrics", forwarded));
}

From source file:com.heliosapm.streams.opentsdb.KafkaRPC.java

License:Apache License

/**
 * Handles the kafka consumer/*from   w w w  .j av a  2s. com*/
 */
@Override
public void run() {
    log.info("Kafka Subscription Thread Started");
    try {
        consumer.subscribe(Arrays.asList(topics), this);
        while (!closed.get()) {
            try {
                final ConsumerRecords<String, ByteBuf> records;
                try {
                    records = consumer.poll(pollTimeout);
                    final Context ctx = perMessageTimer.time();
                    final int recordCount = records.count();
                    if (recordCount > 0) {
                        log.info("Polled {} messages:", records.count());
                        int i = 0;

                        for (final Iterator<ConsumerRecord<String, ByteBuf>> iter = records.iterator(); iter
                                .hasNext();) {
                            i++;
                            final ConsumerRecord<String, ByteBuf> record = iter.next();
                            final ByteBuf b = record.value();
                            log.debug("Polled Record #{}: {} bytes", i, b.readableBytes());
                            messageQueue.writeEntry(b);

                        }
                        final long st = System.currentTimeMillis();

                        log.info("Wrote [{}] records to MessageQueue in [{}] ms.", recordCount,
                                System.currentTimeMillis() - st);
                    }
                    if (syncAdd) {
                        consumer.commitAsync();
                    } else {
                        consumer.commitAsync(); // FIXME:  this will break at some point
                    }
                    ctx.stop();
                } catch (Exception ex) {
                    deserErrors.inc();
                    continue;
                }
            } catch (Exception ex) {
                log.error("Unexpected Exception1", ex);
            }
        }
    } catch (Exception ex) {
        log.error("Unexpected Exception2", ex);
    }
}

From source file:com.heliosapm.streams.opentsdb.KafkaRPC.java

License:Apache License

/**
 * {@inheritDoc}/*ww w.ja  v a 2 s.c o m*/
 * @see com.heliosapm.streams.chronicle.MessageListener#onMetric(io.netty.buffer.ByteBuf)
 */
@Override
public int onMetric(final ByteBuf buf) {
    log.info("OnMetric Buffer: {} bytes", buf.readableBytes());
    try {
        final List<Deferred<Object>> addPointDeferreds = new ArrayList<Deferred<Object>>();
        int recordCount = 0;
        int totalCount = 0;
        int totalBlacklisted = 0;
        final long startTimeNanos = System.nanoTime();
        try {
            final Iterator<StreamedMetricValue> iter = StreamedMetricValue.streamedMetricValues(true, buf, true)
                    .iterator();
            final long now = System.currentTimeMillis();
            while (iter.hasNext()) {
                final StreamedMetricValue smv = iter.next();
                final long elapsed = now - smv.getTimestamp();
                if (elapsed > 0)
                    metricLatency.update(now - smv.getTimestamp());
                totalCount++;
                try {
                    if (blacklist.isBlackListed(smv.metricKey())) {
                        totalBlacklisted++;
                        continue;
                    }
                    Deferred<Object> thisDeferred = null;
                    if (smv.isDoubleValue()) {
                        final double d = smv.getDoubleValue();
                        if (d == Double.NaN || d == Double.NEGATIVE_INFINITY || d == Double.POSITIVE_INFINITY)
                            continue;
                        thisDeferred = tsdb.addPoint(smv.getMetricName(), smv.getTimestamp(),
                                smv.getDoubleValue(), smv.getTags());
                    } else {
                        thisDeferred = tsdb.addPoint(smv.getMetricName(), smv.getTimestamp(),
                                smv.getLongValue(), smv.getTags());
                    }
                    pendingDataPointAdds.increment();
                    recordCount++;
                    addPointDeferreds.add(thisDeferred); // keep all the deferreds so we can wait on them
                } catch (Exception adpe) {
                    if (smv != null) {
                        log.error("Failed to add data point for invalid metric name: {}, cause: {}",
                                smv.metricKey(), adpe.getMessage());
                        blacklist.blackList(smv.metricKey());
                    }
                    log.error("Failed to process StreamedMetricValue", adpe);
                }
            }
            log.info("Async Writes Complete. total-reads: {}, total-writes: {}, blacklisted: {}", totalCount,
                    recordCount, totalBlacklisted);
        } catch (Exception ex) {
            log.error("BufferIteration Failure on read #" + totalCount, ex);
        }
        final long readAndWriteTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTimeNanos);
        log.debug("Read [{}] total metrics and wrote [{}] to OpenTSDB in [{}] ms.", totalCount, recordCount,
                readAndWriteTime);
        Deferred<ArrayList<Object>> d = Deferred.group(addPointDeferreds);
        final int rcount = recordCount;
        d.addCallback(new Callback<Void, ArrayList<Object>>() {
            @Override
            public Void call(final ArrayList<Object> arg) throws Exception {
                pendingDataPointAdds.add(-1 * rcount);
                final long elapsed = System.nanoTime() - startTimeNanos;
                perMessageTimer.update(nanosPerMessage(elapsed, rcount), TimeUnit.NANOSECONDS);
                pointsAddedMeter.mark(rcount);
                if (syncAdd)
                    log.info("Sync Processed {} records in {} ms. Pending: {}", rcount,
                            TimeUnit.NANOSECONDS.toMillis(elapsed), pendingDataPointAdds.longValue());
                return null;
            }
        });
        if (syncAdd) {
            try {
                d.joinUninterruptibly(syncAddTimeout);
            } catch (Exception ex) {
                putTimeouts.inc(rcount);
                log.warn("Datapoints Write Timed Out");
            }
        } else {
            final long elapsed = System.nanoTime() - startTimeNanos;
            perMessageTimer.update(nanosPerMessage(elapsed, recordCount), TimeUnit.NANOSECONDS);
            pointsAddedMeter.mark(recordCount);
            log.info("Async Processed {} records in {} ms. Pending: {}", recordCount,
                    TimeUnit.NANOSECONDS.toMillis(elapsed), pendingDataPointAdds.longValue());
        }
        return recordCount;
    } finally {
        try {
            buf.release();
        } catch (Exception ex) {
            /* No Op */}
    }
}