Search in sources :

Example 1 with FunctionInstanceStatsImpl

use of org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl in project pulsar by apache.

the class FunctionRuntimeManager method getFunctionStats.

/**
 * Get stats of all function instances.
 *
 * @param tenant       the tenant the function belongs to
 * @param namespace    the namespace the function belongs to
 * @param functionName the function name
 * @return a list of function statuses
 * @throws PulsarAdminException
 */
public FunctionStatsImpl getFunctionStats(String tenant, String namespace, String functionName, URI uri) throws PulsarAdminException {
    Collection<Assignment> assignments = this.findFunctionAssignments(tenant, namespace, functionName);
    FunctionStatsImpl functionStats = new FunctionStatsImpl();
    if (assignments.isEmpty()) {
        return functionStats;
    }
    if (runtimeFactory.externallyManaged()) {
        Assignment assignment = assignments.iterator().next();
        boolean isOwner = this.workerConfig.getWorkerId().equals(assignment.getWorkerId());
        if (isOwner) {
            int parallelism = assignment.getInstance().getFunctionMetaData().getFunctionDetails().getParallelism();
            for (int i = 0; i < parallelism; ++i) {
                FunctionInstanceStatsDataImpl functionInstanceStatsData = getFunctionInstanceStats(tenant, namespace, functionName, i, null);
                FunctionInstanceStatsImpl functionInstanceStats = new FunctionInstanceStatsImpl();
                functionInstanceStats.setInstanceId(i);
                functionInstanceStats.setMetrics(functionInstanceStatsData);
                functionStats.addInstance(functionInstanceStats);
            }
        } else {
            // find the hostname/port of the worker who is the owner
            List<WorkerInfo> workerInfoList = this.membershipManager.getCurrentMembership();
            WorkerInfo workerInfo = null;
            for (WorkerInfo entry : workerInfoList) {
                if (assignment.getWorkerId().equals(entry.getWorkerId())) {
                    workerInfo = entry;
                }
            }
            if (workerInfo == null) {
                return functionStats;
            }
            if (uri == null) {
                throw new WebApplicationException(Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build());
            } else {
                URI redirect = UriBuilder.fromUri(uri).host(workerInfo.getWorkerHostname()).port(workerInfo.getPort()).build();
                throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
            }
        }
    } else {
        for (Assignment assignment : assignments) {
            boolean isOwner = this.workerConfig.getWorkerId().equals(assignment.getWorkerId());
            FunctionInstanceStatsDataImpl functionInstanceStatsData;
            if (isOwner) {
                functionInstanceStatsData = getFunctionInstanceStats(tenant, namespace, functionName, assignment.getInstance().getInstanceId(), null);
            } else {
                functionInstanceStatsData = (FunctionInstanceStatsDataImpl) this.functionAdmin.functions().getFunctionStats(assignment.getInstance().getFunctionMetaData().getFunctionDetails().getTenant(), assignment.getInstance().getFunctionMetaData().getFunctionDetails().getNamespace(), assignment.getInstance().getFunctionMetaData().getFunctionDetails().getName(), assignment.getInstance().getInstanceId());
            }
            FunctionInstanceStatsImpl functionInstanceStats = new FunctionInstanceStatsImpl();
            functionInstanceStats.setInstanceId(assignment.getInstance().getInstanceId());
            functionInstanceStats.setMetrics(functionInstanceStatsData);
            functionStats.addInstance(functionInstanceStats);
        }
    }
    return functionStats.calculateOverall();
}
Also used : Assignment(org.apache.pulsar.functions.proto.Function.Assignment) FunctionInstanceStatsImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl) WebApplicationException(javax.ws.rs.WebApplicationException) WorkerInfo(org.apache.pulsar.common.functions.WorkerInfo) FunctionStatsImpl(org.apache.pulsar.common.policies.data.FunctionStatsImpl) FunctionInstanceStatsDataImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsDataImpl) URI(java.net.URI)

Example 2 with FunctionInstanceStatsImpl

use of org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl in project pulsar by apache.

the class FunctionsImplTest method testMetricsEmpty.

@Test
public void testMetricsEmpty() throws PulsarClientException {
    Function.FunctionDetails.Builder functionDetailsBuilder = createDefaultFunctionDetails().toBuilder();
    InstanceConfig instanceConfig = new InstanceConfig();
    instanceConfig.setFunctionDetails(functionDetailsBuilder.build());
    instanceConfig.setMaxBufferedTuples(1024);
    JavaInstanceRunnable javaInstanceRunnable = new JavaInstanceRunnable(instanceConfig, null, null, null, null, null, null, null, null);
    CompletableFuture<InstanceCommunication.MetricsData> completableFuture = new CompletableFuture<InstanceCommunication.MetricsData>();
    completableFuture.complete(javaInstanceRunnable.getMetrics());
    Runtime runtime = mock(Runtime.class);
    doReturn(completableFuture).when(runtime).getMetrics(anyInt());
    RuntimeSpawner runtimeSpawner = mock(RuntimeSpawner.class);
    doReturn(runtime).when(runtimeSpawner).getRuntime();
    FunctionRuntimeInfo functionRuntimeInfo = mock(FunctionRuntimeInfo.class);
    doReturn(runtimeSpawner).when(functionRuntimeInfo).getRuntimeSpawner();
    FunctionInstanceStatsImpl instanceStats1 = WorkerUtils.getFunctionInstanceStats("public/default/test", functionRuntimeInfo, 0);
    FunctionInstanceStatsImpl instanceStats2 = WorkerUtils.getFunctionInstanceStats("public/default/test", functionRuntimeInfo, 1);
    FunctionStatsImpl functionStats = new FunctionStatsImpl();
    functionStats.addInstance(instanceStats1);
    functionStats.addInstance(instanceStats2);
    assertNotNull(functionStats.calculateOverall());
}
Also used : JavaInstanceRunnable(org.apache.pulsar.functions.instance.JavaInstanceRunnable) FunctionInstanceStatsImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Runtime(org.apache.pulsar.functions.runtime.Runtime) InstanceConfig(org.apache.pulsar.functions.instance.InstanceConfig) InstanceCommunication(org.apache.pulsar.functions.proto.InstanceCommunication) FunctionRuntimeInfo(org.apache.pulsar.functions.worker.FunctionRuntimeInfo) FunctionStatsImpl(org.apache.pulsar.common.policies.data.FunctionStatsImpl) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner) Test(org.testng.annotations.Test)

Example 3 with FunctionInstanceStatsImpl

use of org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl in project pulsar by yahoo.

the class WorkerImpl method getFunctionsMetrics.

@Override
public List<WorkerFunctionInstanceStats> getFunctionsMetrics(String clientRole) throws IOException {
    if (!isWorkerServiceAvailable()) {
        throwUnavailableException();
    }
    if (worker().getWorkerConfig().isAuthorizationEnabled() && !isSuperUser(clientRole)) {
        log.error("Client [{}] is not authorized to get function stats", clientRole);
        throw new RestException(Status.UNAUTHORIZED, "Client is not authorized to perform operation");
    }
    Map<String, FunctionRuntimeInfo> functionRuntimes = worker().getFunctionRuntimeManager().getFunctionRuntimeInfos();
    List<WorkerFunctionInstanceStats> metricsList = new ArrayList<>(functionRuntimes.size());
    for (Map.Entry<String, FunctionRuntimeInfo> entry : functionRuntimes.entrySet()) {
        String fullyQualifiedInstanceName = entry.getKey();
        FunctionRuntimeInfo functionRuntimeInfo = entry.getValue();
        if (worker().getFunctionRuntimeManager().getRuntimeFactory().externallyManaged()) {
            Function.FunctionDetails functionDetails = functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().getFunctionDetails();
            int parallelism = functionDetails.getParallelism();
            for (int i = 0; i < parallelism; ++i) {
                FunctionInstanceStatsImpl functionInstanceStats = WorkerUtils.getFunctionInstanceStats(fullyQualifiedInstanceName, functionRuntimeInfo, i);
                WorkerFunctionInstanceStats workerFunctionInstanceStats = new WorkerFunctionInstanceStats();
                workerFunctionInstanceStats.setName(FunctionCommon.getFullyQualifiedInstanceId(functionDetails.getTenant(), functionDetails.getNamespace(), functionDetails.getName(), i));
                workerFunctionInstanceStats.setMetrics(functionInstanceStats.getMetrics());
                metricsList.add(workerFunctionInstanceStats);
            }
        } else {
            FunctionInstanceStatsImpl functionInstanceStats = WorkerUtils.getFunctionInstanceStats(fullyQualifiedInstanceName, functionRuntimeInfo, functionRuntimeInfo.getFunctionInstance().getInstanceId());
            WorkerFunctionInstanceStats workerFunctionInstanceStats = new WorkerFunctionInstanceStats();
            workerFunctionInstanceStats.setName(fullyQualifiedInstanceName);
            workerFunctionInstanceStats.setMetrics(functionInstanceStats.getMetrics());
            metricsList.add(workerFunctionInstanceStats);
        }
    }
    return metricsList;
}
Also used : RestException(org.apache.pulsar.common.util.RestException) ArrayList(java.util.ArrayList) WorkerFunctionInstanceStats(org.apache.pulsar.common.policies.data.WorkerFunctionInstanceStats) Function(org.apache.pulsar.functions.proto.Function) FunctionInstanceStatsImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl) FunctionRuntimeInfo(org.apache.pulsar.functions.worker.FunctionRuntimeInfo) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with FunctionInstanceStatsImpl

use of org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl in project incubator-pulsar by apache.

the class WorkerUtils method getFunctionInstanceStats.

public static FunctionInstanceStatsImpl getFunctionInstanceStats(String fullyQualifiedInstanceName, FunctionRuntimeInfo functionRuntimeInfo, int instanceId) {
    RuntimeSpawner functionRuntimeSpawner = functionRuntimeInfo.getRuntimeSpawner();
    FunctionInstanceStatsImpl functionInstanceStats = new FunctionInstanceStatsImpl();
    if (functionRuntimeSpawner != null) {
        Runtime functionRuntime = functionRuntimeSpawner.getRuntime();
        if (functionRuntime != null) {
            try {
                InstanceCommunication.MetricsData metricsData = functionRuntime.getMetrics(instanceId).get();
                functionInstanceStats.setInstanceId(instanceId);
                FunctionInstanceStatsDataImpl functionInstanceStatsData = new FunctionInstanceStatsDataImpl();
                functionInstanceStatsData.setReceivedTotal(metricsData.getReceivedTotal());
                functionInstanceStatsData.setProcessedSuccessfullyTotal(metricsData.getProcessedSuccessfullyTotal());
                functionInstanceStatsData.setSystemExceptionsTotal(metricsData.getSystemExceptionsTotal());
                functionInstanceStatsData.setUserExceptionsTotal(metricsData.getUserExceptionsTotal());
                functionInstanceStatsData.setAvgProcessLatency(metricsData.getAvgProcessLatency() == 0.0 ? null : metricsData.getAvgProcessLatency());
                functionInstanceStatsData.setLastInvocation(metricsData.getLastInvocation() == 0 ? null : metricsData.getLastInvocation());
                functionInstanceStatsData.oneMin.setReceivedTotal(metricsData.getReceivedTotal1Min());
                functionInstanceStatsData.oneMin.setProcessedSuccessfullyTotal(metricsData.getProcessedSuccessfullyTotal1Min());
                functionInstanceStatsData.oneMin.setSystemExceptionsTotal(metricsData.getSystemExceptionsTotal1Min());
                functionInstanceStatsData.oneMin.setUserExceptionsTotal(metricsData.getUserExceptionsTotal1Min());
                functionInstanceStatsData.oneMin.setAvgProcessLatency(metricsData.getAvgProcessLatency1Min() == 0.0 ? null : metricsData.getAvgProcessLatency1Min());
                // Filter out values that are NaN
                Map<String, Double> statsDataMap = metricsData.getUserMetricsMap().entrySet().stream().filter(stringDoubleEntry -> !stringDoubleEntry.getValue().isNaN()).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
                functionInstanceStatsData.setUserMetrics(statsDataMap);
                functionInstanceStats.setMetrics(functionInstanceStatsData);
            } catch (InterruptedException | ExecutionException e) {
                log.warn("Failed to collect metrics for function instance {}", fullyQualifiedInstanceName, e);
            }
        }
    }
    return functionInstanceStats;
}
Also used : AppendOnlyStreamWriter(org.apache.distributedlog.AppendOnlyStreamWriter) Producer(org.apache.pulsar.client.api.Producer) DLMetadata(org.apache.distributedlog.metadata.DLMetadata) StringUtils(org.apache.commons.lang3.StringUtils) Map(java.util.Map) URI(java.net.URI) WorkerInfo(org.apache.pulsar.common.functions.WorkerInfo) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ZKException(org.apache.distributedlog.exceptions.ZKException) Runtime(org.apache.pulsar.functions.runtime.Runtime) CompressionType(org.apache.pulsar.client.api.CompressionType) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) SizeUnit(org.apache.pulsar.client.api.SizeUnit) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) Code(org.apache.zookeeper.KeeperException.Code) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) InstanceCommunication(org.apache.pulsar.functions.proto.InstanceCommunication) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) Supplier(java.util.function.Supplier) FunctionInstanceStatsImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl) PulsarAdminBuilder(org.apache.pulsar.client.admin.PulsarAdminBuilder) DLOutputStream(org.apache.pulsar.functions.worker.dlog.DLOutputStream) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) PropertiesUtils(org.apache.pulsar.client.internal.PropertiesUtils) FunctionInstanceStatsDataImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsDataImpl) PulsarClient(org.apache.pulsar.client.api.PulsarClient) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) OutputStream(java.io.OutputStream) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) Reader(org.apache.pulsar.client.api.Reader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Namespace(org.apache.distributedlog.api.namespace.Namespace) BKDLConfig(org.apache.distributedlog.impl.metadata.BKDLConfig) MessageId(org.apache.pulsar.client.api.MessageId) FunctionCommon(org.apache.pulsar.functions.utils.FunctionCommon) ProducerAccessMode(org.apache.pulsar.client.api.ProducerAccessMode) MetadataStoreFactoryImpl.removeIdentifierFromMetadataURL(org.apache.pulsar.metadata.impl.MetadataStoreFactoryImpl.removeIdentifierFromMetadataURL) InternalConfigurationData(org.apache.pulsar.common.conf.InternalConfigurationData) ReaderBuilder(org.apache.pulsar.client.api.ReaderBuilder) DLInputStream(org.apache.pulsar.functions.worker.dlog.DLInputStream) InputStream(java.io.InputStream) FunctionInstanceStatsDataImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsDataImpl) FunctionInstanceStatsImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl) Runtime(org.apache.pulsar.functions.runtime.Runtime) InstanceCommunication(org.apache.pulsar.functions.proto.InstanceCommunication) ExecutionException(java.util.concurrent.ExecutionException) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner)

Example 5 with FunctionInstanceStatsImpl

use of org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl in project incubator-pulsar by apache.

the class FunctionsImplTest method testMetricsEmpty.

@Test
public void testMetricsEmpty() throws PulsarClientException {
    Function.FunctionDetails.Builder functionDetailsBuilder = createDefaultFunctionDetails().toBuilder();
    InstanceConfig instanceConfig = new InstanceConfig();
    instanceConfig.setFunctionDetails(functionDetailsBuilder.build());
    instanceConfig.setMaxBufferedTuples(1024);
    JavaInstanceRunnable javaInstanceRunnable = new JavaInstanceRunnable(instanceConfig, null, null, null, null, null, null, null, null);
    CompletableFuture<InstanceCommunication.MetricsData> completableFuture = new CompletableFuture<InstanceCommunication.MetricsData>();
    completableFuture.complete(javaInstanceRunnable.getMetrics());
    Runtime runtime = mock(Runtime.class);
    doReturn(completableFuture).when(runtime).getMetrics(anyInt());
    RuntimeSpawner runtimeSpawner = mock(RuntimeSpawner.class);
    doReturn(runtime).when(runtimeSpawner).getRuntime();
    FunctionRuntimeInfo functionRuntimeInfo = mock(FunctionRuntimeInfo.class);
    doReturn(runtimeSpawner).when(functionRuntimeInfo).getRuntimeSpawner();
    FunctionInstanceStatsImpl instanceStats1 = WorkerUtils.getFunctionInstanceStats("public/default/test", functionRuntimeInfo, 0);
    FunctionInstanceStatsImpl instanceStats2 = WorkerUtils.getFunctionInstanceStats("public/default/test", functionRuntimeInfo, 1);
    FunctionStatsImpl functionStats = new FunctionStatsImpl();
    functionStats.addInstance(instanceStats1);
    functionStats.addInstance(instanceStats2);
    assertNotNull(functionStats.calculateOverall());
}
Also used : JavaInstanceRunnable(org.apache.pulsar.functions.instance.JavaInstanceRunnable) FunctionInstanceStatsImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl) CompletableFuture(java.util.concurrent.CompletableFuture) Runtime(org.apache.pulsar.functions.runtime.Runtime) InstanceConfig(org.apache.pulsar.functions.instance.InstanceConfig) InstanceCommunication(org.apache.pulsar.functions.proto.InstanceCommunication) FunctionRuntimeInfo(org.apache.pulsar.functions.worker.FunctionRuntimeInfo) FunctionStatsImpl(org.apache.pulsar.common.policies.data.FunctionStatsImpl) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner) Test(org.testng.annotations.Test)

Aggregations

FunctionInstanceStatsImpl (org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl)12 URI (java.net.URI)6 Map (java.util.Map)6 FunctionInstanceStatsDataImpl (org.apache.pulsar.common.policies.data.FunctionInstanceStatsDataImpl)6 FunctionStatsImpl (org.apache.pulsar.common.policies.data.FunctionStatsImpl)6 InstanceCommunication (org.apache.pulsar.functions.proto.InstanceCommunication)6 Runtime (org.apache.pulsar.functions.runtime.Runtime)6 RuntimeSpawner (org.apache.pulsar.functions.runtime.RuntimeSpawner)6 FunctionRuntimeInfo (org.apache.pulsar.functions.worker.FunctionRuntimeInfo)6 WorkerInfo (org.apache.pulsar.common.functions.WorkerInfo)5 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 Files (java.nio.file.Files)3 REPLACE_EXISTING (java.nio.file.StandardCopyOption.REPLACE_EXISTING)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3