Search in sources :

Example 1 with JavaInstanceRunnable

use of org.apache.pulsar.functions.instance.JavaInstanceRunnable in project pulsar by apache.

the class ThreadRuntime method start.

/**
 * The core logic that initialize the thread container and executes the function.
 */
@Override
public void start() throws Exception {
    // extract class loader for function
    ClassLoader functionClassLoader = getFunctionClassLoader(instanceConfig, jarFile, narExtractionDirectory, fnCache, connectorsManager);
    // re-initialize JavaInstanceRunnable so that variables in constructor can be re-initialized
    this.javaInstanceRunnable = new JavaInstanceRunnable(instanceConfig, clientBuilder, pulsarClient, pulsarAdmin, stateStorageImplClass, stateStorageServiceUrl, secretsProvider, collectorRegistry, functionClassLoader);
    log.info("ThreadContainer starting function with instanceId {} functionId {} namespace {}", instanceConfig.getInstanceId(), instanceConfig.getFunctionId(), instanceConfig.getFunctionDetails().getNamespace());
    this.fnThread = new Thread(threadGroup, javaInstanceRunnable, String.format("%s-%s", FunctionCommon.getFullyQualifiedName(instanceConfig.getFunctionDetails()), instanceConfig.getInstanceId()));
    this.fnThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            log.error("Uncaught exception in thread {}", t, e);
        }
    });
    this.fnThread.start();
}
Also used : JavaInstanceRunnable(org.apache.pulsar.functions.instance.JavaInstanceRunnable)

Example 2 with JavaInstanceRunnable

use of org.apache.pulsar.functions.instance.JavaInstanceRunnable 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 3 with JavaInstanceRunnable

use of org.apache.pulsar.functions.instance.JavaInstanceRunnable 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 4 with JavaInstanceRunnable

use of org.apache.pulsar.functions.instance.JavaInstanceRunnable in project incubator-pulsar by apache.

the class ThreadRuntime method start.

/**
 * The core logic that initialize the thread container and executes the function.
 */
@Override
public void start() throws Exception {
    // extract class loader for function
    ClassLoader functionClassLoader = getFunctionClassLoader(instanceConfig, jarFile, narExtractionDirectory, fnCache, connectorsManager);
    // re-initialize JavaInstanceRunnable so that variables in constructor can be re-initialized
    this.javaInstanceRunnable = new JavaInstanceRunnable(instanceConfig, clientBuilder, pulsarClient, pulsarAdmin, stateStorageImplClass, stateStorageServiceUrl, secretsProvider, collectorRegistry, functionClassLoader);
    log.info("ThreadContainer starting function with instanceId {} functionId {} namespace {}", instanceConfig.getInstanceId(), instanceConfig.getFunctionId(), instanceConfig.getFunctionDetails().getNamespace());
    this.fnThread = new Thread(threadGroup, javaInstanceRunnable, String.format("%s-%s", FunctionCommon.getFullyQualifiedName(instanceConfig.getFunctionDetails()), instanceConfig.getInstanceId()));
    this.fnThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            log.error("Uncaught exception in thread {}", t, e);
        }
    });
    this.fnThread.start();
}
Also used : JavaInstanceRunnable(org.apache.pulsar.functions.instance.JavaInstanceRunnable)

Example 5 with JavaInstanceRunnable

use of org.apache.pulsar.functions.instance.JavaInstanceRunnable 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)

Aggregations

JavaInstanceRunnable (org.apache.pulsar.functions.instance.JavaInstanceRunnable)9 CompletableFuture (java.util.concurrent.CompletableFuture)6 InstanceConfig (org.apache.pulsar.functions.instance.InstanceConfig)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 InputStream (java.io.InputStream)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 FunctionInstanceStatsImpl (org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl)3 FunctionStatsImpl (org.apache.pulsar.common.policies.data.FunctionStatsImpl)3 TenantInfo (org.apache.pulsar.common.policies.data.TenantInfo)3 RuntimeFactory (org.apache.pulsar.functions.runtime.RuntimeFactory)3 FunctionMetaDataManager (org.apache.pulsar.functions.worker.FunctionMetaDataManager)3 FunctionRuntimeManager (org.apache.pulsar.functions.worker.FunctionRuntimeManager)3 PulsarWorkerService (org.apache.pulsar.functions.worker.PulsarWorkerService)3 WorkerConfig (org.apache.pulsar.functions.worker.WorkerConfig)3