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.heliosapm.streams.metrics.aggregation.StreamedMetricAggregation.java

License:Apache License

/**
 * Returns this aggregation as a byte array
 * @return a byte array/*w  w w .  j  ava2s.  c  om*/
 */
public byte[] toByteArray() {
    final ByteBuf b = BufferManager.getInstance().buffer(size == -1 ? 128 : size);
    try {
        b.writeByte(sticky ? 1 : 0);
        b.writeByte(doubleType ? 1 : 0);
        b.writeLong(createTime);
        b.writeLong(period);
        b.writeByte(periodUnit.ordinal());
        values.position(0);
        b.writeBytes(values);
        b.writeByte(tags.size());
        BufferManager.writeUTF(metricName, b);
        for (Map.Entry<String, String> entry : tags.entrySet()) {
            BufferManager.writeUTF(entry.getKey(), b);
            BufferManager.writeUTF(entry.getValue(), b);
        }
        return ByteBufUtil.getBytes(b);
    } finally {
        try {
            b.release();
        } catch (Exception x) {
            /* No Op */}
    }
}

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. ja v  a2 s  .c o m
 */
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

/**
 * Reads a streamed metric from the passed byte array
 * @param bytes the byte array to read the streamed metric from
 * @return the appropriate type of StreamedMetric
 *//*w ww .  j ava2  s  .co m*/
public static StreamedMetric read(final byte[] bytes) {
    final ByteBuf buff = BufferManager.getInstance().directBuffer(bytes.length).writeBytes(bytes);
    try {
        final byte type = buff.readByte();
        switch (type) {
        case 0:
            return StreamedMetric.fromBuff(buff);
        case 1:
            return StreamedMetricValue.fromBuff(buff);
        default:
            throw new RuntimeException("Unrecognized metric type code [" + type + "]");
        }
    } finally {
        try {
            buff.release();
        } catch (Exception x) {
            /* No Op */}
    }
}

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

License:Open Source License

/**
 * Returns an interator over the StreamMetrics in the passed buffer
 * @param buf The buffer to read from//from  w w  w .jav a  2  s  . co  m
 * @param releaseOnDone true to release the buffer on iterator end
 * @return the iterator
 */
public static Iterable<StreamedMetric> streamedMetrics(final ByteBuf buf, final boolean releaseOnDone) {
    return new Iterable<StreamedMetric>() {
        @Override
        public Iterator<StreamedMetric> iterator() {
            return new Iterator<StreamedMetric>() {
                @Override
                public boolean hasNext() {
                    final boolean hasNext = buf.isReadable(MIN_READABLE_BYTES);
                    if (releaseOnDone)
                        buf.release();
                    return hasNext;
                }

                @Override
                public StreamedMetric next() {
                    return read(buf);
                }
            };
        }
    };
}

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//www. jav  a  2  s  .c  o 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 //from  ww  w.j a v a  2s .  c om
 */
@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.StreamValueTest.java

License:Open Source License

protected void testStreamedMetricValueIterator(final boolean single, final boolean release) {
    ByteBuf inBuffer = null;
    try {//from   ww  w. ja v a 2  s .  co  m
        final int metricCount = 10000;
        inBuffer = BufferManager.getInstance().buffer(metricCount * 128);
        final Set<StreamedMetricValue> originals = new LinkedHashSet<StreamedMetricValue>(metricCount);
        for (int i = 0; i < metricCount; i++) {
            StreamedMetricValue smv = new StreamedMetricValue(System.currentTimeMillis(), nextPosDouble(),
                    getRandomFragment(), randomTags(3));
            originals.add(smv);
            smv.intoByteBuf(inBuffer);
        }
        Assert.assertEquals("Invalid number of samples", metricCount, originals.size());
        final Iterator<StreamedMetricValue> originalsIter = originals.iterator();
        final Iterator<StreamedMetricValue> iter = StreamedMetricValue
                .streamedMetricValues(single, inBuffer, release).iterator();
        int loops = 0;
        while (originalsIter.hasNext()) {
            final StreamedMetricValue smv1 = originalsIter.next();
            Assert.assertTrue("Buffered iterator had no next metric", iter.hasNext());
            final StreamedMetricValue smv2 = iter.next();
            assertEquals(smv1, smv2);
            //            log(smv1);
            loops++;
        }
        Assert.assertFalse("Buffer iter should have no more metrics", iter.hasNext());
        Assert.assertEquals("Invalid number of loops", metricCount, loops);
        if (release) {
            Assert.assertEquals("Invalid refCount on released buffer", 0, inBuffer.refCnt());
        } else {
            Assert.assertEquals("Invalid refCount on released buffer", 1, inBuffer.refCnt());
        }
    } finally {
        if (!release)
            inBuffer.release();
    }
}

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

License:Apache License

/**
 * {@inheritDoc}//w ww .  j a  v  a  2s. com
 * @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 */}
    }
}

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

License:Apache License

protected void send(final Set<StreamedMetricValue> metrics) {
    final ByteBuf buff = BufferManager.getInstance().buffer(metrics.size() * 128);

    try {//  w w  w  .j  a  va 2s  . co  m

        for (StreamedMetricValue smv : metrics) {
            smv.intoByteBuf(buff);
        }
        log("Sent Buff Size:" + buff.readableBytes());
        final int vsize = producer.send(new ProducerRecord<String, ByteBuf>("tsdb.metrics.binary", buff)).get()
                .serializedValueSize();
        log("Sent Value Size:" + vsize);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            buff.release();
        } catch (Exception x) {
            /* No Op */}
    }
}

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

License:Apache License

/**
 * {@inheritDoc}//from w w  w  .  j a  v a2 s. c o m
 * @see com.heliosapm.streams.chronicle.MessageListener#onMetric(io.netty.buffer.ByteBuf)
 */
@Override
public int onMetric(final ByteBuf buf) {
    log.debug("OnMetric Buffer: {} bytes", buf.readableBytes());
    try {
        int totalCount = 0;
        try {
            final Iterator<StreamedMetricValue> iter = StreamedMetricValue.streamedMetricValues(true, buf, true)
                    .iterator();
            while (iter.hasNext()) {
                final StreamedMetricValue smv = iter.next();
                kafkaSender
                        .send(new ProducerRecord<String, StreamedMetricValue>(topicName, smv.metricKey(), smv));
                totalCount++;
            }
        } catch (Exception ex) {
            log.error("OnMetric Error", ex);
        }
        return totalCount;
    } finally {
        try {
            buf.release();
        } catch (Exception ex) {
            /* No Op */}
    }
}