Search in sources :

Example 1 with FunctionRuntimeInfo

use of org.apache.pulsar.functions.worker.FunctionRuntimeInfo in project pulsar by apache.

the class FunctionsImplTest method setup.

@BeforeMethod
public void setup() throws Exception {
    this.mockedManager = mock(FunctionMetaDataManager.class);
    this.mockedFunctionRunTimeManager = mock(FunctionRuntimeManager.class);
    this.mockedTenantInfo = mock(TenantInfo.class);
    this.mockedRuntimeFactory = mock(RuntimeFactory.class);
    this.mockedInputStream = mock(InputStream.class);
    this.mockedNamespace = mock(Namespace.class);
    this.mockedFormData = mock(FormDataContentDisposition.class);
    when(mockedFormData.getFileName()).thenReturn("test");
    this.mockedPulsarAdmin = mock(PulsarAdmin.class);
    this.mockedTenants = mock(Tenants.class);
    this.mockedNamespaces = mock(Namespaces.class);
    this.mockedFunctionMetadata = Function.FunctionMetaData.newBuilder().setFunctionDetails(createDefaultFunctionDetails()).build();
    namespaceList.add(tenant + "/" + namespace);
    this.mockedWorkerService = mock(PulsarWorkerService.class);
    when(mockedWorkerService.getFunctionMetaDataManager()).thenReturn(mockedManager);
    when(mockedWorkerService.getFunctionRuntimeManager()).thenReturn(mockedFunctionRunTimeManager);
    when(mockedFunctionRunTimeManager.getRuntimeFactory()).thenReturn(mockedRuntimeFactory);
    when(mockedWorkerService.getDlogNamespace()).thenReturn(mockedNamespace);
    when(mockedWorkerService.isInitialized()).thenReturn(true);
    when(mockedWorkerService.getBrokerAdmin()).thenReturn(mockedPulsarAdmin);
    when(mockedPulsarAdmin.tenants()).thenReturn(mockedTenants);
    when(mockedPulsarAdmin.namespaces()).thenReturn(mockedNamespaces);
    when(mockedTenants.getTenantInfo(any())).thenReturn(mockedTenantInfo);
    when(mockedNamespaces.getNamespaces(any())).thenReturn(namespaceList);
    when(mockedManager.getFunctionMetaData(any(), any(), any())).thenReturn(mockedFunctionMetadata);
    when(mockedManager.containsFunction(tenant, namespace, function)).thenReturn(true);
    when(mockedFunctionRunTimeManager.findFunctionAssignment(eq(tenant), eq(namespace), eq(function), anyInt())).thenReturn(Function.Assignment.newBuilder().setWorkerId(workerId).build());
    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> metricsDataCompletableFuture = new CompletableFuture<InstanceCommunication.MetricsData>();
    metricsDataCompletableFuture.complete(javaInstanceRunnable.getMetrics());
    Runtime runtime = mock(Runtime.class);
    doReturn(metricsDataCompletableFuture).when(runtime).getMetrics(anyInt());
    CompletableFuture<InstanceCommunication.FunctionStatus> functionStatusCompletableFuture = new CompletableFuture<>();
    functionStatusCompletableFuture.complete(javaInstanceRunnable.getFunctionStatus().build());
    RuntimeSpawner runtimeSpawner = mock(RuntimeSpawner.class);
    when(runtimeSpawner.getFunctionStatus(anyInt())).thenReturn(functionStatusCompletableFuture);
    doReturn(runtime).when(runtimeSpawner).getRuntime();
    FunctionRuntimeInfo functionRuntimeInfo = mock(FunctionRuntimeInfo.class);
    doReturn(runtimeSpawner).when(functionRuntimeInfo).getRuntimeSpawner();
    when(mockedFunctionRunTimeManager.getFunctionRuntimeInfo(any())).thenReturn(functionRuntimeInfo);
    // worker config
    WorkerConfig workerConfig = new WorkerConfig().setWorkerId(workerId).setWorkerPort(8080).setFunctionMetadataTopicName("pulsar/functions").setNumFunctionPackageReplicas(3).setPulsarServiceUrl("pulsar://localhost:6650/");
    tempDirectory = PulsarFunctionTestTemporaryDirectory.create(getClass().getSimpleName());
    tempDirectory.useTemporaryDirectoriesForWorkerConfig(workerConfig);
    when(mockedWorkerService.getWorkerConfig()).thenReturn(workerConfig);
    this.resource = spy(new FunctionsImpl(() -> mockedWorkerService));
}
Also used : Namespaces(org.apache.pulsar.client.admin.Namespaces) TenantInfo(org.apache.pulsar.common.policies.data.TenantInfo) Tenants(org.apache.pulsar.client.admin.Tenants) CompletableFuture(java.util.concurrent.CompletableFuture) Runtime(org.apache.pulsar.functions.runtime.Runtime) InstanceConfig(org.apache.pulsar.functions.instance.InstanceConfig) WorkerConfig(org.apache.pulsar.functions.worker.WorkerConfig) FunctionRuntimeInfo(org.apache.pulsar.functions.worker.FunctionRuntimeInfo) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) InputStream(java.io.InputStream) PulsarWorkerService(org.apache.pulsar.functions.worker.PulsarWorkerService) JavaInstanceRunnable(org.apache.pulsar.functions.instance.JavaInstanceRunnable) RuntimeFactory(org.apache.pulsar.functions.runtime.RuntimeFactory) Namespace(org.apache.distributedlog.api.namespace.Namespace) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) InstanceCommunication(org.apache.pulsar.functions.proto.InstanceCommunication) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) FunctionRuntimeManager(org.apache.pulsar.functions.worker.FunctionRuntimeManager) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with FunctionRuntimeInfo

use of org.apache.pulsar.functions.worker.FunctionRuntimeInfo 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 FunctionRuntimeInfo

use of org.apache.pulsar.functions.worker.FunctionRuntimeInfo 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 FunctionRuntimeInfo

use of org.apache.pulsar.functions.worker.FunctionRuntimeInfo in project incubator-pulsar by apache.

the class FunctionsImplTest method setup.

@BeforeMethod
public void setup() throws Exception {
    this.mockedManager = mock(FunctionMetaDataManager.class);
    this.mockedFunctionRunTimeManager = mock(FunctionRuntimeManager.class);
    this.mockedTenantInfo = mock(TenantInfo.class);
    this.mockedRuntimeFactory = mock(RuntimeFactory.class);
    this.mockedInputStream = mock(InputStream.class);
    this.mockedNamespace = mock(Namespace.class);
    this.mockedFormData = mock(FormDataContentDisposition.class);
    when(mockedFormData.getFileName()).thenReturn("test");
    this.mockedPulsarAdmin = mock(PulsarAdmin.class);
    this.mockedTenants = mock(Tenants.class);
    this.mockedNamespaces = mock(Namespaces.class);
    this.mockedFunctionMetadata = Function.FunctionMetaData.newBuilder().setFunctionDetails(createDefaultFunctionDetails()).build();
    namespaceList.add(tenant + "/" + namespace);
    this.mockedWorkerService = mock(PulsarWorkerService.class);
    when(mockedWorkerService.getFunctionMetaDataManager()).thenReturn(mockedManager);
    when(mockedWorkerService.getFunctionRuntimeManager()).thenReturn(mockedFunctionRunTimeManager);
    when(mockedFunctionRunTimeManager.getRuntimeFactory()).thenReturn(mockedRuntimeFactory);
    when(mockedWorkerService.getDlogNamespace()).thenReturn(mockedNamespace);
    when(mockedWorkerService.isInitialized()).thenReturn(true);
    when(mockedWorkerService.getBrokerAdmin()).thenReturn(mockedPulsarAdmin);
    when(mockedPulsarAdmin.tenants()).thenReturn(mockedTenants);
    when(mockedPulsarAdmin.namespaces()).thenReturn(mockedNamespaces);
    when(mockedTenants.getTenantInfo(any())).thenReturn(mockedTenantInfo);
    when(mockedNamespaces.getNamespaces(any())).thenReturn(namespaceList);
    when(mockedManager.getFunctionMetaData(any(), any(), any())).thenReturn(mockedFunctionMetadata);
    when(mockedManager.containsFunction(tenant, namespace, function)).thenReturn(true);
    when(mockedFunctionRunTimeManager.findFunctionAssignment(eq(tenant), eq(namespace), eq(function), anyInt())).thenReturn(Function.Assignment.newBuilder().setWorkerId(workerId).build());
    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> metricsDataCompletableFuture = new CompletableFuture<InstanceCommunication.MetricsData>();
    metricsDataCompletableFuture.complete(javaInstanceRunnable.getMetrics());
    Runtime runtime = mock(Runtime.class);
    doReturn(metricsDataCompletableFuture).when(runtime).getMetrics(anyInt());
    CompletableFuture<InstanceCommunication.FunctionStatus> functionStatusCompletableFuture = new CompletableFuture<>();
    functionStatusCompletableFuture.complete(javaInstanceRunnable.getFunctionStatus().build());
    RuntimeSpawner runtimeSpawner = mock(RuntimeSpawner.class);
    when(runtimeSpawner.getFunctionStatus(anyInt())).thenReturn(functionStatusCompletableFuture);
    doReturn(runtime).when(runtimeSpawner).getRuntime();
    FunctionRuntimeInfo functionRuntimeInfo = mock(FunctionRuntimeInfo.class);
    doReturn(runtimeSpawner).when(functionRuntimeInfo).getRuntimeSpawner();
    when(mockedFunctionRunTimeManager.getFunctionRuntimeInfo(any())).thenReturn(functionRuntimeInfo);
    // worker config
    WorkerConfig workerConfig = new WorkerConfig().setWorkerId(workerId).setWorkerPort(8080).setFunctionMetadataTopicName("pulsar/functions").setNumFunctionPackageReplicas(3).setPulsarServiceUrl("pulsar://localhost:6650/");
    tempDirectory = PulsarFunctionTestTemporaryDirectory.create(getClass().getSimpleName());
    tempDirectory.useTemporaryDirectoriesForWorkerConfig(workerConfig);
    when(mockedWorkerService.getWorkerConfig()).thenReturn(workerConfig);
    this.resource = spy(new FunctionsImpl(() -> mockedWorkerService));
}
Also used : Namespaces(org.apache.pulsar.client.admin.Namespaces) TenantInfo(org.apache.pulsar.common.policies.data.TenantInfo) Tenants(org.apache.pulsar.client.admin.Tenants) CompletableFuture(java.util.concurrent.CompletableFuture) Runtime(org.apache.pulsar.functions.runtime.Runtime) InstanceConfig(org.apache.pulsar.functions.instance.InstanceConfig) WorkerConfig(org.apache.pulsar.functions.worker.WorkerConfig) FunctionRuntimeInfo(org.apache.pulsar.functions.worker.FunctionRuntimeInfo) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) InputStream(java.io.InputStream) PulsarWorkerService(org.apache.pulsar.functions.worker.PulsarWorkerService) JavaInstanceRunnable(org.apache.pulsar.functions.instance.JavaInstanceRunnable) RuntimeFactory(org.apache.pulsar.functions.runtime.RuntimeFactory) Namespace(org.apache.distributedlog.api.namespace.Namespace) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) InstanceCommunication(org.apache.pulsar.functions.proto.InstanceCommunication) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) FunctionRuntimeManager(org.apache.pulsar.functions.worker.FunctionRuntimeManager) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 5 with FunctionRuntimeInfo

use of org.apache.pulsar.functions.worker.FunctionRuntimeInfo 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

FunctionRuntimeInfo (org.apache.pulsar.functions.worker.FunctionRuntimeInfo)9 CompletableFuture (java.util.concurrent.CompletableFuture)6 FunctionInstanceStatsImpl (org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl)6 InstanceConfig (org.apache.pulsar.functions.instance.InstanceConfig)6 JavaInstanceRunnable (org.apache.pulsar.functions.instance.JavaInstanceRunnable)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 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Namespace (org.apache.distributedlog.api.namespace.Namespace)3 Namespaces (org.apache.pulsar.client.admin.Namespaces)3 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)3 Tenants (org.apache.pulsar.client.admin.Tenants)3 FunctionStatsImpl (org.apache.pulsar.common.policies.data.FunctionStatsImpl)3 TenantInfo (org.apache.pulsar.common.policies.data.TenantInfo)3 WorkerFunctionInstanceStats (org.apache.pulsar.common.policies.data.WorkerFunctionInstanceStats)3 RestException (org.apache.pulsar.common.util.RestException)3