Example usage for io.netty.buffer ByteBufAllocator DEFAULT

List of usage examples for io.netty.buffer ByteBufAllocator DEFAULT

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufAllocator DEFAULT.

Prototype

ByteBufAllocator DEFAULT

To view the source code for io.netty.buffer ByteBufAllocator DEFAULT.

Click Source Link

Usage

From source file:org.apache.helix.ipc.benchmark.BenchmarkDriver.java

License:Apache License

private void startTraffic(final String remoteHost, final int remotePort) {
    if (isShutdown.getAndSet(false)) {
        System.out.println("Starting " + trafficThreads.length + " threads to generate traffic");
        for (int i = 0; i < trafficThreads.length; i++) {
            Thread t = new Thread() {
                @Override/*from   w  w  w.  j  a va  2  s  .c  om*/
                public void run() {
                    ByteBuf m = ByteBufAllocator.DEFAULT.buffer(messageBytes.length);
                    m.writeBytes(messageBytes);
                    while (!isShutdown.get()) {
                        for (int i = 0; i < numPartitions; i++) {
                            HelixMessageScope scope = new HelixMessageScope.Builder().cluster("CLUSTER")
                                    .resource("RESOURCE").partition("PARTITION_" + i)
                                    .sourceInstance(localhost + "_" + port).build();

                            Set<HelixAddress> destinations = ImmutableSet
                                    .of(new HelixAddress(scope, remoteHost + "_" + remotePort,
                                            new InetSocketAddress(remoteHost, remotePort)));

                            UUID uuid = UUID.randomUUID();

                            try {
                                for (HelixAddress destination : destinations) {
                                    m.retain();
                                    ipcService.send(destination, MESSAGE_TYPE, uuid, m);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            };
            t.start();
            trafficThreads[i] = t;
        }
        System.out.println("Started traffic to " + remoteHost + ":" + remotePort);
    }
}

From source file:org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsGenerator.java

License:Apache License

public static void generate(PulsarService pulsar, OutputStream out) throws IOException {
    ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer();
    try {// ww  w .j a v a  2s  . c o  m
        SimpleTextOutputStream stream = new SimpleTextOutputStream(buf);

        generateSystemMetrics(stream, pulsar.getConfiguration().getClusterName());

        NamespaceStatsAggregator.generate(pulsar, stream);

        out.write(buf.array(), buf.arrayOffset(), buf.readableBytes());
    } finally {
        buf.release();
    }
}

From source file:org.apache.pulsar.functions.worker.FunctionStatsGeneratorTest.java

License:Apache License

@Test
public void testFunctionsStatsGenerate() {
    FunctionRuntimeManager functionRuntimeManager = mock(FunctionRuntimeManager.class);
    Map<String, FunctionRuntimeInfo> functionRuntimeInfoMap = new HashMap<>();

    WorkerService workerService = mock(WorkerService.class);
    doReturn(functionRuntimeManager).when(workerService).getFunctionRuntimeManager();

    CompletableFuture<InstanceCommunication.MetricsData> metricsDataCompletableFuture = new CompletableFuture<>();
    InstanceCommunication.MetricsData metricsData = InstanceCommunication.MetricsData.newBuilder()
            .putMetrics("__total_processed__",
                    InstanceCommunication.MetricsData.DataDigest.newBuilder().setCount(100.0).setMax(200.0)
                            .setSum(300.0).setMin(0.0).build())
            .putMetrics("__avg_latency_ms__", InstanceCommunication.MetricsData.DataDigest.newBuilder()
                    .setCount(10.0).setMax(20.0).setSum(30.0).setMin(0.0).build())
            .build();/*from  w w  w  .  java2 s . c  om*/

    metricsDataCompletableFuture.complete(metricsData);
    Runtime runtime = mock(Runtime.class);
    doReturn(metricsDataCompletableFuture).when(runtime).getAndResetMetrics();

    RuntimeSpawner runtimeSpawner = mock(RuntimeSpawner.class);
    doReturn(runtime).when(runtimeSpawner).getRuntime();

    Function.FunctionMetaData function1 = Function.FunctionMetaData.newBuilder()
            .setFunctionDetails(Function.FunctionDetails.newBuilder().setTenant("test-tenant")
                    .setNamespace("test-namespace").setName("func-1"))
            .build();

    Function.Instance instance = Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0)
            .build();

    FunctionRuntimeInfo functionRuntimeInfo = mock(FunctionRuntimeInfo.class);
    doReturn(runtimeSpawner).when(functionRuntimeInfo).getRuntimeSpawner();
    doReturn(instance).when(functionRuntimeInfo).getFunctionInstance();

    functionRuntimeInfoMap.put(Utils.getFullyQualifiedInstanceId(instance), functionRuntimeInfo);
    doReturn(functionRuntimeInfoMap).when(functionRuntimeManager).getFunctionRuntimeInfos();

    ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer();
    SimpleTextOutputStream statsOut = new SimpleTextOutputStream(buf);
    FunctionsStatsGenerator.generate(workerService, "default", statsOut);

    String str = buf.toString(Charset.defaultCharset());
    buf.release();
    Map<String, Metric> metrics = parseMetrics(str);

    Assert.assertEquals(metrics.size(), 8);

    Metric m = metrics.get("pulsar_function__total_processed__count");
    assertEquals(m.tags.get("cluster"), "default");
    assertEquals(m.tags.get("instanceId"), "0");
    assertEquals(m.tags.get("name"), "func-1");
    assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace");
    assertEquals(m.value, 100.0);

    m = metrics.get("pulsar_function__total_processed__max");
    assertEquals(m.tags.get("cluster"), "default");
    assertEquals(m.tags.get("instanceId"), "0");
    assertEquals(m.tags.get("name"), "func-1");
    assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace");
    assertEquals(m.value, 200.0);

    m = metrics.get("pulsar_function__total_processed__sum");
    assertEquals(m.tags.get("cluster"), "default");
    assertEquals(m.tags.get("instanceId"), "0");
    assertEquals(m.tags.get("name"), "func-1");
    assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace");
    assertEquals(m.value, 300.0);

    m = metrics.get("pulsar_function__total_processed__min");
    assertEquals(m.tags.get("cluster"), "default");
    assertEquals(m.tags.get("instanceId"), "0");
    assertEquals(m.tags.get("name"), "func-1");
    assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace");
    assertEquals(m.value, 0.0);

    m = metrics.get("pulsar_function__avg_latency_ms__count");
    assertEquals(m.tags.get("cluster"), "default");
    assertEquals(m.tags.get("instanceId"), "0");
    assertEquals(m.tags.get("name"), "func-1");
    assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace");
    assertEquals(m.value, 10.0);

    m = metrics.get("pulsar_function__avg_latency_ms__max");
    assertEquals(m.tags.get("cluster"), "default");
    assertEquals(m.tags.get("instanceId"), "0");
    assertEquals(m.tags.get("name"), "func-1");
    assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace");
    assertEquals(m.value, 20.0);

    m = metrics.get("pulsar_function__avg_latency_ms__sum");
    assertEquals(m.tags.get("cluster"), "default");
    assertEquals(m.tags.get("instanceId"), "0");
    assertEquals(m.tags.get("name"), "func-1");
    assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace");
    assertEquals(m.value, 30.0);

    m = metrics.get("pulsar_function__avg_latency_ms__min");
    assertEquals(m.tags.get("cluster"), "default");
    assertEquals(m.tags.get("instanceId"), "0");
    assertEquals(m.tags.get("name"), "func-1");
    assertEquals(m.tags.get("namespace"), "test-tenant/test-namespace");
    assertEquals(m.value, 0.0);
}

From source file:org.apache.pulsar.functions.worker.rest.api.FunctionsMetricsResource.java

License:Apache License

@Path("metrics")
@GET/* w w w . j a v  a 2 s.c  o m*/
@Produces(MediaType.TEXT_PLAIN)
public Response getMetrics() {

    WorkerService workerService = get();

    ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer();
    try {
        SimpleTextOutputStream stream = new SimpleTextOutputStream(buf);
        FunctionsStatsGenerator.generate(workerService, "default", stream);
        byte[] payload = buf.array();
        int arrayOffset = buf.arrayOffset();
        int readableBytes = buf.readableBytes();
        StreamingOutput streamOut = out -> {
            out.write(payload, arrayOffset, readableBytes);
            out.flush();
        };
        return Response.ok(streamOut).type(MediaType.TEXT_PLAIN_TYPE).build();
    } finally {
        buf.release();
    }
}

From source file:org.asynchttpclient.netty.ssl.DefaultSslEngineFactory.java

License:Open Source License

@Override
public SSLEngine newSslEngine(AsyncHttpClientConfig config, String peerHost, int peerPort) {
    // FIXME should be using ctx allocator
    SSLEngine sslEngine = sslContext.newEngine(ByteBufAllocator.DEFAULT, peerHost, peerPort);
    configureSslEngine(sslEngine, config);
    return sslEngine;
}

From source file:org.asynchttpclient.request.body.multipart.part.MessageEndMultipartPart.java

License:Open Source License

private ByteBuf lazyLoadContentBuffer() {
    if (contentBuffer == null) {
        contentBuffer = ByteBufAllocator.DEFAULT.buffer((int) getContentLength());
        contentBuffer.writeBytes(EXTRA_BYTES).writeBytes(boundary).writeBytes(EXTRA_BYTES)
                .writeBytes(CRLF_BYTES);
    }//from   w  w  w .ja va  2  s  .  c  om
    return contentBuffer;
}

From source file:org.ballerinalang.stdlib.utils.MultipartUtils.java

License:Open Source License

/**
 * Read http content chunk by chunk from netty encoder and add it to carbon message.
 *
 * @param httpRequestMsg Represent carbon message that the content should be added to
 * @param nettyEncoder   Represent netty encoder that holds the actual http content
 * @throws Exception In case content cannot be read from netty encoder
 */// w ww  .  j  a  v  a 2s . com
private static void addMultipartsToCarbonMessage(HttpCarbonMessage httpRequestMsg,
        HttpPostRequestEncoder nettyEncoder) throws Exception {
    while (!nettyEncoder.isEndOfInput()) {
        httpRequestMsg.addHttpContent(nettyEncoder.readChunk(ByteBufAllocator.DEFAULT));
    }
    nettyEncoder.cleanFiles();
}

From source file:org.ebayopensource.scc.cache.CacheResultVerifierTest.java

License:Apache License

@Test
public void testFetchResult() {
    FullHttpResponse cache = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            ByteBufAllocator.DEFAULT.buffer());
    FullHttpResponse actual = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            ByteBufAllocator.DEFAULT.buffer());
    CacheResultVerifier crv = new CacheResultVerifier("key", null, cache);

    HttpHeaders.setHeader(cache, "ETag", "etagvalue");
    HttpHeaders.setHeader(actual, "ETag", "etagvalue");

    CacheVerifiedResult result = crv.fetchResult(actual);
    Assert.assertTrue(result.result);//from   ww w. j  a v  a  2s .c o  m

    HttpHeaders.setHeader(actual, "ETag", "etagvalue2");
    result = crv.fetchResult(actual);
    Assert.assertFalse(result.result);

    cache = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            ByteBufAllocator.DEFAULT.buffer());
    actual = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            ByteBufAllocator.DEFAULT.buffer());
    crv = new CacheResultVerifier("key", null, cache);

    actual.setStatus(HttpResponseStatus.SERVICE_UNAVAILABLE);
    result = crv.fetchResult(actual);
    Assert.assertFalse(result.result);
    actual.setStatus(HttpResponseStatus.OK);

    HttpHeaders.setHeader(cache, "h1", "value");
    HttpHeaders.setHeader(actual, "h1", "value");
    result = crv.fetchResult(actual);
    Assert.assertTrue(result.result);

    HttpHeaders.setHeader(cache, "h2", "value");
    HttpHeaders.setHeader(actual, "h2", "value2");
    result = crv.fetchResult(actual);
    Assert.assertFalse(result.result);
    HttpHeaders.setHeader(actual, "h2", "value");

    ByteBuf cc = cache.content();
    ByteBuf ac = actual.content();

    cc.writeBytes("abc".getBytes());
    result = crv.fetchResult(actual);
    Assert.assertFalse(result.result);

    ac.writeBytes("abc".getBytes());
    result = crv.fetchResult(actual);
    Assert.assertTrue(result.result);

    cc.writeBytes("eft".getBytes());
    ac.writeBytes("xyz".getBytes());
    result = crv.fetchResult(actual);
    Assert.assertFalse(result.result);

}

From source file:org.ebayopensource.scc.cache.NettyResponseSerializerTest.java

License:Apache License

@Test
public void testSerialize() {
    NettyResponseSerializer serializer = new NettyResponseSerializer();

    DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK);
    CacheResponse cacheRes = serializer.serialize(response);
    assertNotNull(cacheRes);/*w  w  w  .  j  av a2  s  .c  o m*/
    assertEquals(HttpVersion.HTTP_1_0.toString(), cacheRes.getProtocalVersion());
    assertEquals(HttpResponseStatus.OK.code(), cacheRes.getCode());
    assertEquals(0, cacheRes.getContent().length);
    assertTrue(cacheRes.getHeaders().get(0).getKey().equals("Content-Length"));
    assertTrue(cacheRes.getTrailingHeaders().isEmpty());

    ByteBuf content = new EmptyByteBuf(ByteBufAllocator.DEFAULT);
    response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, content);
    cacheRes = serializer.serialize(response);
    assertEquals(0, cacheRes.getContent().length);

    content = UnpooledByteBufAllocator.DEFAULT.buffer();
    content.writeBytes("Hello, World!".getBytes());
    response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, content);
    cacheRes = serializer.serialize(response);
    assertEquals("Hello, World!", new String(cacheRes.getContent()));

    HttpHeaders headers = response.headers();
    headers.add("header1", "value1");

    HttpHeaders trailingHeaders = response.trailingHeaders();
    trailingHeaders.add("tHeader2", "value2");

    cacheRes = serializer.serialize(response);
    Entry<String, String> header = cacheRes.getHeaders().get(0);
    assertEquals("header1", header.getKey());
    assertEquals("value1", header.getValue());

    Entry<String, String> tHeader = cacheRes.getTrailingHeaders().get(0);
    assertEquals("tHeader2", tHeader.getKey());
    assertEquals("value2", tHeader.getValue());
}

From source file:org.graylog2.plugin.inputs.transports.AbstractTcpTransport.java

License:Open Source License

private Callable<ChannelHandler> buildSslHandlerCallable(SslProvider tlsProvider, File certFile, File keyFile,
        String password, ClientAuth clientAuth, File clientAuthCertFile) {
    return new Callable<ChannelHandler>() {
        @Override/*from  ww w.  jav  a 2  s . co  m*/
        public ChannelHandler call() throws Exception {
            try {
                return new SslHandler(createSslEngine());
            } catch (SSLException e) {
                LOG.error(
                        "Error creating SSL context. Make sure the certificate and key are in the correct format: cert=X.509 key=PKCS#8");
                throw e;
            }
        }

        private SSLEngine createSslEngine() throws IOException, CertificateException {
            final X509Certificate[] clientAuthCerts;
            if (EnumSet.of(ClientAuth.OPTIONAL, ClientAuth.REQUIRE).contains(clientAuth)) {
                if (clientAuthCertFile.exists()) {
                    clientAuthCerts = KeyUtil.loadCertificates(clientAuthCertFile.toPath()).stream()
                            .filter(certificate -> certificate instanceof X509Certificate)
                            .map(certificate -> (X509Certificate) certificate).toArray(X509Certificate[]::new);
                } else {
                    LOG.warn(
                            "Client auth configured, but no authorized certificates / certificate authorities configured");
                    clientAuthCerts = null;
                }
            } else {
                clientAuthCerts = null;
            }

            final SslContext sslContext = SslContextBuilder
                    .forServer(certFile, keyFile, Strings.emptyToNull(password)).sslProvider(tlsProvider)
                    .clientAuth(clientAuth).trustManager(clientAuthCerts).build();

            // TODO: Use byte buffer allocator of channel
            return sslContext.newEngine(ByteBufAllocator.DEFAULT);
        }
    };
}