Search in sources :

Example 1 with Runtime

use of org.apache.pulsar.functions.runtime.Runtime in project incubator-pulsar by apache.

the class FunctionsStatsGenerator method generate.

public static void generate(WorkerService workerService, String cluster, SimpleTextOutputStream out) {
    if (workerService != null) {
        Map<String, FunctionRuntimeInfo> functionRuntimes = workerService.getFunctionRuntimeManager().getFunctionRuntimeInfos();
        for (Map.Entry<String, FunctionRuntimeInfo> entry : functionRuntimes.entrySet()) {
            String fullyQualifiedInstanceName = entry.getKey();
            FunctionRuntimeInfo functionRuntimeInfo = entry.getValue();
            RuntimeSpawner functionRuntimeSpawner = functionRuntimeInfo.getRuntimeSpawner();
            if (functionRuntimeSpawner != null) {
                Runtime functionRuntime = functionRuntimeSpawner.getRuntime();
                if (functionRuntime != null) {
                    try {
                        InstanceCommunication.MetricsData metrics = functionRuntime.getAndResetMetrics().get();
                        for (Map.Entry<String, InstanceCommunication.MetricsData.DataDigest> metricsEntry : metrics.getMetricsMap().entrySet()) {
                            String metricName = metricsEntry.getKey();
                            InstanceCommunication.MetricsData.DataDigest dataDigest = metricsEntry.getValue();
                            String tenant = functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().getFunctionConfig().getTenant();
                            String namespace = functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().getFunctionConfig().getNamespace();
                            String name = functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().getFunctionConfig().getName();
                            int instanceId = functionRuntimeInfo.getFunctionInstance().getInstanceId();
                            String qualifiedNamespace = String.format("%s/%s", tenant, namespace);
                            metric(out, cluster, qualifiedNamespace, name, String.format("pulsar_function%scount", metricName), instanceId, dataDigest.getCount());
                            metric(out, cluster, qualifiedNamespace, name, String.format("pulsar_function%smax", metricName), instanceId, dataDigest.getMax());
                            metric(out, cluster, qualifiedNamespace, name, String.format("pulsar_function%smin", metricName), instanceId, dataDigest.getMin());
                            metric(out, cluster, qualifiedNamespace, name, String.format("pulsar_function%ssum", metricName), instanceId, dataDigest.getSum());
                        }
                    } catch (InterruptedException | ExecutionException e) {
                        log.warn("Failed to collect metrics for function instance {}", fullyQualifiedInstanceName, e);
                    }
                }
            }
        }
    }
}
Also used : Runtime(org.apache.pulsar.functions.runtime.Runtime) InstanceCommunication(org.apache.pulsar.functions.proto.InstanceCommunication) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner)

Example 2 with Runtime

use of org.apache.pulsar.functions.runtime.Runtime in project incubator-pulsar by apache.

the class FunctionStatsGeneratorTest method testFunctionsStatsGenerate.

@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();
    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().setFunctionConfig(Function.FunctionConfig.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);
}
Also used : SimpleTextOutputStream(org.apache.pulsar.common.util.SimpleTextOutputStream) HashMap(java.util.HashMap) ToString(lombok.ToString) ByteBuf(io.netty.buffer.ByteBuf) Function(org.apache.pulsar.functions.proto.Function) CompletableFuture(java.util.concurrent.CompletableFuture) Runtime(org.apache.pulsar.functions.runtime.Runtime) InstanceCommunication(org.apache.pulsar.functions.proto.InstanceCommunication) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner) Test(org.testng.annotations.Test)

Aggregations

InstanceCommunication (org.apache.pulsar.functions.proto.InstanceCommunication)2 Runtime (org.apache.pulsar.functions.runtime.Runtime)2 RuntimeSpawner (org.apache.pulsar.functions.runtime.RuntimeSpawner)2 ByteBuf (io.netty.buffer.ByteBuf)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ToString (lombok.ToString)1 SimpleTextOutputStream (org.apache.pulsar.common.util.SimpleTextOutputStream)1 Function (org.apache.pulsar.functions.proto.Function)1 Test (org.testng.annotations.Test)1