Search in sources :

Example 1 with PulsarWorkerService

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

the class AbstractPulsarE2ETest method createPulsarFunctionWorker.

private PulsarWorkerService createPulsarFunctionWorker(ServiceConfiguration config) throws IOException {
    System.setProperty(JAVA_INSTANCE_JAR_PROPERTY, FutureUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    workerConfig = new WorkerConfig();
    tempDirectory = PulsarFunctionTestTemporaryDirectory.create(getClass().getSimpleName());
    tempDirectory.useTemporaryDirectoriesForWorkerConfig(workerConfig);
    workerConfig.setPulsarFunctionsNamespace(pulsarFunctionsNamespace);
    workerConfig.setSchedulerClassName(org.apache.pulsar.functions.worker.scheduler.RoundRobinScheduler.class.getName());
    workerConfig.setFunctionRuntimeFactoryClassName(ThreadRuntimeFactory.class.getName());
    workerConfig.setFunctionRuntimeFactoryConfigs(// worker talks to local broker
    ObjectMapperFactory.getThreadLocal().convertValue(new ThreadRuntimeFactoryConfig().setThreadGroupName("use"), Map.class));
    workerConfig.setFailureCheckFreqMs(100);
    workerConfig.setNumFunctionPackageReplicas(1);
    workerConfig.setClusterCoordinationTopicName("coordinate");
    workerConfig.setFunctionAssignmentTopicName("assignment");
    workerConfig.setFunctionMetadataTopicName("metadata");
    workerConfig.setInstanceLivenessCheckFreqMs(100);
    workerConfig.setWorkerPort(0);
    workerConfig.setPulsarFunctionsCluster(config.getClusterName());
    String hostname = ServiceConfigurationUtils.getDefaultOrConfiguredAddress(config.getAdvertisedAddress());
    this.workerId = "c-" + config.getClusterName() + "-fw-" + hostname + "-" + workerConfig.getWorkerPort();
    workerConfig.setWorkerHostname(hostname);
    workerConfig.setWorkerId(workerId);
    workerConfig.setBrokerClientAuthenticationPlugin(AuthenticationTls.class.getName());
    workerConfig.setBrokerClientAuthenticationParameters(String.format("tlsCertFile:%s,tlsKeyFile:%s", TLS_CLIENT_CERT_FILE_PATH, TLS_CLIENT_KEY_FILE_PATH));
    workerConfig.setUseTls(true);
    workerConfig.setTlsAllowInsecureConnection(true);
    workerConfig.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
    workerConfig.setAuthenticationEnabled(true);
    workerConfig.setAuthorizationEnabled(true);
    PulsarWorkerService workerService = new PulsarWorkerService();
    workerService.init(workerConfig, null, false);
    return workerService;
}
Also used : ThreadRuntimeFactoryConfig(org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactoryConfig) AuthenticationTls(org.apache.pulsar.client.impl.auth.AuthenticationTls) PulsarWorkerService(org.apache.pulsar.functions.worker.PulsarWorkerService) WorkerConfig(org.apache.pulsar.functions.worker.WorkerConfig) ThreadRuntimeFactory(org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactory) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with PulsarWorkerService

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

the class PulsarFunctionAdminTest method createPulsarFunctionWorker.

private PulsarWorkerService createPulsarFunctionWorker(ServiceConfiguration config) {
    workerConfig = new WorkerConfig();
    workerConfig.setPulsarFunctionsNamespace(pulsarFunctionsNamespace);
    workerConfig.setSchedulerClassName(org.apache.pulsar.functions.worker.scheduler.RoundRobinScheduler.class.getName());
    workerConfig.setFunctionRuntimeFactoryClassName(ThreadRuntimeFactory.class.getName());
    workerConfig.setFunctionRuntimeFactoryConfigs(ObjectMapperFactory.getThreadLocal().convertValue(new ThreadRuntimeFactoryConfig().setThreadGroupName("use"), Map.class));
    // worker talks to local broker
    workerConfig.setPulsarServiceUrl("pulsar://127.0.0.1:" + config.getBrokerServicePortTls().get());
    workerConfig.setPulsarWebServiceUrl("https://127.0.0.1:" + config.getWebServicePortTls().get());
    workerConfig.setFailureCheckFreqMs(100);
    workerConfig.setNumFunctionPackageReplicas(1);
    workerConfig.setClusterCoordinationTopicName("coordinate");
    workerConfig.setFunctionAssignmentTopicName("assignment");
    workerConfig.setFunctionMetadataTopicName("metadata");
    workerConfig.setInstanceLivenessCheckFreqMs(100);
    workerConfig.setWorkerPort(0);
    workerConfig.setPulsarFunctionsCluster(config.getClusterName());
    String hostname = ServiceConfigurationUtils.getDefaultOrConfiguredAddress(config.getAdvertisedAddress());
    workerConfig.setWorkerHostname(hostname);
    workerConfig.setWorkerId("c-" + config.getClusterName() + "-fw-" + hostname + "-" + workerConfig.getWorkerPort());
    workerConfig.setBrokerClientAuthenticationPlugin(AuthenticationTls.class.getName());
    workerConfig.setBrokerClientAuthenticationParameters(String.format("tlsCertFile:%s,tlsKeyFile:%s", TLS_CLIENT_CERT_FILE_PATH, TLS_CLIENT_KEY_FILE_PATH));
    workerConfig.setUseTls(true);
    workerConfig.setTlsAllowInsecureConnection(true);
    workerConfig.setTlsTrustCertsFilePath(TLS_CLIENT_CERT_FILE_PATH);
    PulsarWorkerService workerService = new PulsarWorkerService();
    workerService.init(workerConfig, null, false);
    return workerService;
}
Also used : ThreadRuntimeFactoryConfig(org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactoryConfig) AuthenticationTls(org.apache.pulsar.client.impl.auth.AuthenticationTls) PulsarWorkerService(org.apache.pulsar.functions.worker.PulsarWorkerService) WorkerConfig(org.apache.pulsar.functions.worker.WorkerConfig) ThreadRuntimeFactory(org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with PulsarWorkerService

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

the class FunctionApiV3ResourceTest method testDownloadFunctionHttpUrl.

@Test
public void testDownloadFunctionHttpUrl() throws Exception {
    String jarHttpUrl = "https://repo1.maven.org/maven2/org/apache/pulsar/pulsar-common/2.4.2/pulsar-common-2.4.2.jar";
    String testDir = FunctionApiV3ResourceTest.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    PulsarWorkerService worker = mock(PulsarWorkerService.class);
    doReturn(true).when(worker).isInitialized();
    WorkerConfig config = mock(WorkerConfig.class);
    when(config.isAuthorizationEnabled()).thenReturn(false);
    when(worker.getWorkerConfig()).thenReturn(config);
    FunctionsImpl function = new FunctionsImpl(() -> worker);
    StreamingOutput streamOutput = function.downloadFunction(jarHttpUrl, null, null);
    File pkgFile = new File(testDir, UUID.randomUUID().toString());
    OutputStream output = new FileOutputStream(pkgFile);
    streamOutput.write(output);
    Assert.assertTrue(pkgFile.exists());
    if (pkgFile.exists()) {
        pkgFile.delete();
    }
}
Also used : PulsarWorkerService(org.apache.pulsar.functions.worker.PulsarWorkerService) FunctionsImpl(org.apache.pulsar.functions.worker.rest.api.FunctionsImpl) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) WorkerConfig(org.apache.pulsar.functions.worker.WorkerConfig) StreamingOutput(javax.ws.rs.core.StreamingOutput) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) Test(org.testng.annotations.Test)

Example 4 with PulsarWorkerService

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

the class WorkerServiceLoader method load.

/**
 * Load the worker services for the given <tt>protocol</tt> list.
 *
 * @param wsNarPackage worker service nar package
 * @param narExtractionDirectory the directory to extract nar directory
 * @return the worker service
 */
static WorkerService load(String wsNarPackage, String narExtractionDirectory) {
    if (isEmpty(wsNarPackage)) {
        return new PulsarWorkerService();
    }
    WorkerServiceDefinition definition;
    try {
        definition = getWorkerServiceDefinition(wsNarPackage, narExtractionDirectory);
    } catch (IOException ioe) {
        log.error("Failed to get the worker service definition from {}", wsNarPackage, ioe);
        throw new RuntimeException("Failed to get the worker service definition from " + wsNarPackage, ioe);
    }
    WorkerServiceMetadata metadata = new WorkerServiceMetadata();
    Path narPath = Paths.get(wsNarPackage);
    metadata.setArchivePath(narPath);
    metadata.setDefinition(definition);
    WorkerServiceWithClassLoader service;
    try {
        service = load(metadata, narExtractionDirectory);
    } catch (IOException e) {
        log.error("Failed to load the worker service {}", metadata, e);
        throw new RuntimeException("Failed to load the worker service " + metadata, e);
    }
    log.info("Successfully loaded worker service {}", metadata);
    return service;
}
Also used : Path(java.nio.file.Path) PulsarWorkerService(org.apache.pulsar.functions.worker.PulsarWorkerService) IOException(java.io.IOException)

Example 5 with PulsarWorkerService

use of org.apache.pulsar.functions.worker.PulsarWorkerService in project pulsar by yahoo.

the class WorkerServiceLoader method load.

/**
 * Load the worker services for the given <tt>protocol</tt> list.
 *
 * @param wsNarPackage worker service nar package
 * @param narExtractionDirectory the directory to extract nar directory
 * @return the worker service
 */
static WorkerService load(String wsNarPackage, String narExtractionDirectory) {
    if (isEmpty(wsNarPackage)) {
        return new PulsarWorkerService();
    }
    WorkerServiceDefinition definition;
    try {
        definition = getWorkerServiceDefinition(wsNarPackage, narExtractionDirectory);
    } catch (IOException ioe) {
        log.error("Failed to get the worker service definition from {}", wsNarPackage, ioe);
        throw new RuntimeException("Failed to get the worker service definition from " + wsNarPackage, ioe);
    }
    WorkerServiceMetadata metadata = new WorkerServiceMetadata();
    Path narPath = Paths.get(wsNarPackage);
    metadata.setArchivePath(narPath);
    metadata.setDefinition(definition);
    WorkerServiceWithClassLoader service;
    try {
        service = load(metadata, narExtractionDirectory);
    } catch (IOException e) {
        log.error("Failed to load the worker service {}", metadata, e);
        throw new RuntimeException("Failed to load the worker service " + metadata, e);
    }
    log.info("Successfully loaded worker service {}", metadata);
    return service;
}
Also used : Path(java.nio.file.Path) PulsarWorkerService(org.apache.pulsar.functions.worker.PulsarWorkerService) IOException(java.io.IOException)

Aggregations

PulsarWorkerService (org.apache.pulsar.functions.worker.PulsarWorkerService)21 WorkerConfig (org.apache.pulsar.functions.worker.WorkerConfig)18 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)9 OutputStream (java.io.OutputStream)9 HashMap (java.util.HashMap)9 Map (java.util.Map)9 StreamingOutput (javax.ws.rs.core.StreamingOutput)9 AuthenticationTls (org.apache.pulsar.client.impl.auth.AuthenticationTls)9 ThreadRuntimeFactory (org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactory)9 ThreadRuntimeFactoryConfig (org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactoryConfig)9 FunctionsImpl (org.apache.pulsar.functions.worker.rest.api.FunctionsImpl)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 Test (org.testng.annotations.Test)9 URL (java.net.URL)6 Path (java.nio.file.Path)6 IOException (java.io.IOException)3 TreeMap (java.util.TreeMap)3 Namespace (org.apache.distributedlog.api.namespace.Namespace)3 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)3