Search in sources :

Example 1 with SecretsProvider

use of org.apache.pulsar.functions.secretsprovider.SecretsProvider in project pulsar by apache.

the class ThreadRuntimeFactory method createContainer.

@Override
public ThreadRuntime createContainer(InstanceConfig instanceConfig, String jarFile, String originalCodeFileName, Long expectedHealthCheckInterval) {
    SecretsProvider secretsProvider = defaultSecretsProvider;
    if (secretsProvider == null) {
        String secretsProviderClassName = secretsProviderConfigurator.getSecretsProviderClassName(instanceConfig.getFunctionDetails());
        secretsProvider = (SecretsProvider) Reflections.createInstance(secretsProviderClassName, this.rootClassLoader);
        log.info("Initializing secrets provider {} with configs: {}", secretsProvider.getClass().getName(), secretsProviderConfigurator.getSecretsProviderConfig(instanceConfig.getFunctionDetails()));
        secretsProvider.init(secretsProviderConfigurator.getSecretsProviderConfig(instanceConfig.getFunctionDetails()));
    }
    return new ThreadRuntime(instanceConfig, fnCache, threadGroup, jarFile, pulsarClient, clientBuilder, pulsarAdmin, stateStorageImplClass, storageServiceUrl, secretsProvider, collectorRegistry, narExtractionDirectory, connectorsManager);
}
Also used : SecretsProvider(org.apache.pulsar.functions.secretsprovider.SecretsProvider)

Example 2 with SecretsProvider

use of org.apache.pulsar.functions.secretsprovider.SecretsProvider in project pulsar by apache.

the class JavaInstanceStarter method start.

public void start(String[] args, ClassLoader functionInstanceClassLoader, ClassLoader rootClassLoader) throws Exception {
    Thread.currentThread().setContextClassLoader(functionInstanceClassLoader);
    JCommander jcommander = new JCommander(this);
    // parse args by JCommander
    jcommander.parse(args);
    InstanceConfig instanceConfig = new InstanceConfig();
    instanceConfig.setFunctionId(functionId);
    instanceConfig.setFunctionVersion(functionVersion);
    instanceConfig.setInstanceId(instanceId);
    instanceConfig.setMaxBufferedTuples(maxBufferedTuples);
    instanceConfig.setClusterName(clusterName);
    instanceConfig.setMaxPendingAsyncRequests(maxPendingAsyncRequests);
    instanceConfig.setExposePulsarAdminClientEnabled(exposePulsarAdminClientEnabled);
    Function.FunctionDetails.Builder functionDetailsBuilder = Function.FunctionDetails.newBuilder();
    if (functionDetailsJsonString.charAt(0) == '\'') {
        functionDetailsJsonString = functionDetailsJsonString.substring(1);
    }
    if (functionDetailsJsonString.charAt(functionDetailsJsonString.length() - 1) == '\'') {
        functionDetailsJsonString = functionDetailsJsonString.substring(0, functionDetailsJsonString.length() - 1);
    }
    JsonFormat.parser().merge(functionDetailsJsonString, functionDetailsBuilder);
    Function.FunctionDetails functionDetails = functionDetailsBuilder.build();
    instanceConfig.setFunctionDetails(functionDetails);
    instanceConfig.setPort(port);
    instanceConfig.setMetricsPort(metricsPort);
    Map<String, String> secretsProviderConfigMap = null;
    if (!StringUtils.isEmpty(secretsProviderConfig)) {
        if (secretsProviderConfig.charAt(0) == '\'') {
            secretsProviderConfig = secretsProviderConfig.substring(1);
        }
        if (secretsProviderConfig.charAt(secretsProviderConfig.length() - 1) == '\'') {
            secretsProviderConfig = secretsProviderConfig.substring(0, secretsProviderConfig.length() - 1);
        }
        Type type = new TypeToken<Map<String, String>>() {
        }.getType();
        secretsProviderConfigMap = new Gson().fromJson(secretsProviderConfig, type);
    }
    if (StringUtils.isEmpty(secretsProviderClassName)) {
        secretsProviderClassName = ClearTextSecretsProvider.class.getName();
    }
    SecretsProvider secretsProvider;
    try {
        secretsProvider = (SecretsProvider) Reflections.createInstance(secretsProviderClassName, functionInstanceClassLoader);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    secretsProvider.init(secretsProviderConfigMap);
    // Collector Registry for prometheus metrics
    FunctionCollectorRegistry collectorRegistry = FunctionCollectorRegistry.getDefaultImplementation();
    RuntimeUtils.registerDefaultCollectors(collectorRegistry);
    containerFactory = new ThreadRuntimeFactory("LocalRunnerThreadGroup", pulsarServiceUrl, stateStorageImplClass, stateStorageServiceUrl, AuthenticationConfig.builder().clientAuthenticationPlugin(clientAuthenticationPlugin).clientAuthenticationParameters(clientAuthenticationParameters).useTls(isTrue(useTls)).tlsAllowInsecureConnection(isTrue(tlsAllowInsecureConnection)).tlsHostnameVerificationEnable(isTrue(tlsHostNameVerificationEnabled)).tlsTrustCertsFilePath(tlsTrustCertFilePath).build(), secretsProvider, collectorRegistry, narExtractionDirectory, rootClassLoader, exposePulsarAdminClientEnabled, webServiceUrl);
    runtimeSpawner = new RuntimeSpawner(instanceConfig, jarFile, // we really dont use this in thread container
    null, containerFactory, expectedHealthCheckInterval * 1000);
    server = ServerBuilder.forPort(port).addService(new InstanceControlImpl(runtimeSpawner)).build().start();
    log.info("JavaInstance Server started, listening on " + port);
    java.lang.Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            // Use stderr here since the logger may have been reset by its JVM shutdown hook.
            try {
                close();
            } catch (Exception ex) {
                System.err.println(ex);
            }
        }
    });
    log.info("Starting runtimeSpawner");
    runtimeSpawner.start();
    // starting metrics server
    log.info("Starting metrics server on port {}", metricsPort);
    metricsServer = new HTTPServer(new InetSocketAddress(metricsPort), collectorRegistry, true);
    if (expectedHealthCheckInterval > 0) {
        healthCheckTimer = InstanceCache.getInstanceCache().getScheduledExecutorService().scheduleAtFixedRate(() -> {
            try {
                if (System.currentTimeMillis() - lastHealthCheckTs > 3 * expectedHealthCheckInterval * 1000) {
                    log.info("Haven't received health check from spawner in a while. Stopping instance...");
                    close();
                }
            } catch (Exception e) {
                log.error("Error occurred when checking for latest health check", e);
            }
        }, expectedHealthCheckInterval * 1000, expectedHealthCheckInterval * 1000, TimeUnit.MILLISECONDS);
    }
    runtimeSpawner.join();
    log.info("RuntimeSpawner quit, shutting down JavaInstance");
    close();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FunctionCollectorRegistry(org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry) Gson(com.google.gson.Gson) ClearTextSecretsProvider(org.apache.pulsar.functions.secretsprovider.ClearTextSecretsProvider) ExecutionException(java.util.concurrent.ExecutionException) Function(org.apache.pulsar.functions.proto.Function) Type(java.lang.reflect.Type) HTTPServer(io.prometheus.client.exporter.HTTPServer) InstanceConfig(org.apache.pulsar.functions.instance.InstanceConfig) JCommander(com.beust.jcommander.JCommander) ThreadRuntimeFactory(org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactory) Map(java.util.Map) SecretsProvider(org.apache.pulsar.functions.secretsprovider.SecretsProvider) ClearTextSecretsProvider(org.apache.pulsar.functions.secretsprovider.ClearTextSecretsProvider)

Example 3 with SecretsProvider

use of org.apache.pulsar.functions.secretsprovider.SecretsProvider in project pulsar by apache.

the class LocalRunner method startThreadedMode.

private void startThreadedMode(org.apache.pulsar.functions.proto.Function.FunctionDetails functionDetails, int parallelism, int instanceIdOffset, String serviceUrl, String stateStorageServiceUrl, AuthenticationConfig authConfig, String userCodeFile) throws Exception {
    if (metricsPortStart != null) {
        if (metricsPortStart < 0 || metricsPortStart > 65535) {
            throw new IllegalArgumentException("Metrics port need to be within the range of 0 and 65535");
        }
    }
    SecretsProvider secretsProvider;
    if (secretsProviderClassName != null) {
        secretsProvider = (SecretsProvider) Reflections.createInstance(secretsProviderClassName, ClassLoader.getSystemClassLoader());
        Map<String, String> config = null;
        if (secretsProviderConfig != null) {
            config = (Map<String, String>) new Gson().fromJson(secretsProviderConfig, Map.class);
        }
        secretsProvider.init(config);
    } else {
        secretsProvider = new ClearTextSecretsProvider();
    }
    boolean exposePulsarAdminClientEnabled = false;
    if (functionConfig != null && functionConfig.getExposePulsarAdminClientEnabled() != null) {
        exposePulsarAdminClientEnabled = functionConfig.getExposePulsarAdminClientEnabled();
    }
    // Collector Registry for prometheus metrics
    FunctionCollectorRegistry collectorRegistry = FunctionCollectorRegistry.getDefaultImplementation();
    RuntimeUtils.registerDefaultCollectors(collectorRegistry);
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (userCodeClassLoader != null) {
            Thread.currentThread().setContextClassLoader(userCodeClassLoader);
        }
        runtimeFactory = new ThreadRuntimeFactory("LocalRunnerThreadGroup", serviceUrl, stateStorageImplClass, stateStorageServiceUrl, authConfig, secretsProvider, collectorRegistry, narExtractionDirectory, null, exposePulsarAdminClientEnabled, webServiceUrl);
    } finally {
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
    for (int i = 0; i < parallelism; ++i) {
        InstanceConfig instanceConfig = new InstanceConfig();
        instanceConfig.setFunctionDetails(functionDetails);
        // TODO: correctly implement function version and id
        instanceConfig.setFunctionVersion(UUID.randomUUID().toString());
        instanceConfig.setFunctionId(UUID.randomUUID().toString());
        instanceConfig.setInstanceId(i + instanceIdOffset);
        instanceConfig.setMaxBufferedTuples(1024);
        if (metricsPortStart != null) {
            instanceConfig.setMetricsPort(metricsPortStart);
        }
        instanceConfig.setClusterName("local");
        if (functionConfig != null) {
            instanceConfig.setMaxPendingAsyncRequests(functionConfig.getMaxPendingAsyncRequests());
            if (functionConfig.getExposePulsarAdminClientEnabled() != null) {
                instanceConfig.setExposePulsarAdminClientEnabled(functionConfig.getExposePulsarAdminClientEnabled());
            }
        }
        RuntimeSpawner runtimeSpawner = new RuntimeSpawner(instanceConfig, userCodeFile, null, runtimeFactory, instanceLivenessCheck);
        spawners.add(runtimeSpawner);
        runtimeSpawner.start();
    }
    if (metricsPortStart != null) {
        // starting metrics server
        log.info("Starting metrics server on port {}", metricsPortStart);
        metricsServer = new HTTPServer(new InetSocketAddress(metricsPortStart), collectorRegistry, true);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FunctionCollectorRegistry(org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry) Gson(com.google.gson.Gson) ClearTextSecretsProvider(org.apache.pulsar.functions.secretsprovider.ClearTextSecretsProvider) HTTPServer(io.prometheus.client.exporter.HTTPServer) InstanceConfig(org.apache.pulsar.functions.instance.InstanceConfig) ThreadRuntimeFactory(org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactory) SecretsProvider(org.apache.pulsar.functions.secretsprovider.SecretsProvider) ClearTextSecretsProvider(org.apache.pulsar.functions.secretsprovider.ClearTextSecretsProvider) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner)

Example 4 with SecretsProvider

use of org.apache.pulsar.functions.secretsprovider.SecretsProvider in project pulsar by yahoo.

the class LocalRunner method startThreadedMode.

private void startThreadedMode(org.apache.pulsar.functions.proto.Function.FunctionDetails functionDetails, int parallelism, int instanceIdOffset, String serviceUrl, String stateStorageServiceUrl, AuthenticationConfig authConfig, String userCodeFile) throws Exception {
    if (metricsPortStart != null) {
        if (metricsPortStart < 0 || metricsPortStart > 65535) {
            throw new IllegalArgumentException("Metrics port need to be within the range of 0 and 65535");
        }
    }
    SecretsProvider secretsProvider;
    if (secretsProviderClassName != null) {
        secretsProvider = (SecretsProvider) Reflections.createInstance(secretsProviderClassName, ClassLoader.getSystemClassLoader());
        Map<String, String> config = null;
        if (secretsProviderConfig != null) {
            config = (Map<String, String>) new Gson().fromJson(secretsProviderConfig, Map.class);
        }
        secretsProvider.init(config);
    } else {
        secretsProvider = new ClearTextSecretsProvider();
    }
    boolean exposePulsarAdminClientEnabled = false;
    if (functionConfig != null && functionConfig.getExposePulsarAdminClientEnabled() != null) {
        exposePulsarAdminClientEnabled = functionConfig.getExposePulsarAdminClientEnabled();
    }
    // Collector Registry for prometheus metrics
    FunctionCollectorRegistry collectorRegistry = FunctionCollectorRegistry.getDefaultImplementation();
    RuntimeUtils.registerDefaultCollectors(collectorRegistry);
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        if (userCodeClassLoader != null) {
            Thread.currentThread().setContextClassLoader(userCodeClassLoader);
        }
        runtimeFactory = new ThreadRuntimeFactory("LocalRunnerThreadGroup", serviceUrl, stateStorageImplClass, stateStorageServiceUrl, authConfig, secretsProvider, collectorRegistry, narExtractionDirectory, null, exposePulsarAdminClientEnabled, webServiceUrl);
    } finally {
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
    for (int i = 0; i < parallelism; ++i) {
        InstanceConfig instanceConfig = new InstanceConfig();
        instanceConfig.setFunctionDetails(functionDetails);
        // TODO: correctly implement function version and id
        instanceConfig.setFunctionVersion(UUID.randomUUID().toString());
        instanceConfig.setFunctionId(UUID.randomUUID().toString());
        instanceConfig.setInstanceId(i + instanceIdOffset);
        instanceConfig.setMaxBufferedTuples(1024);
        if (metricsPortStart != null) {
            instanceConfig.setMetricsPort(metricsPortStart);
        }
        instanceConfig.setClusterName("local");
        if (functionConfig != null) {
            instanceConfig.setMaxPendingAsyncRequests(functionConfig.getMaxPendingAsyncRequests());
            if (functionConfig.getExposePulsarAdminClientEnabled() != null) {
                instanceConfig.setExposePulsarAdminClientEnabled(functionConfig.getExposePulsarAdminClientEnabled());
            }
        }
        RuntimeSpawner runtimeSpawner = new RuntimeSpawner(instanceConfig, userCodeFile, null, runtimeFactory, instanceLivenessCheck);
        spawners.add(runtimeSpawner);
        runtimeSpawner.start();
    }
    if (metricsPortStart != null) {
        // starting metrics server
        log.info("Starting metrics server on port {}", metricsPortStart);
        metricsServer = new HTTPServer(new InetSocketAddress(metricsPortStart), collectorRegistry, true);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FunctionCollectorRegistry(org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry) Gson(com.google.gson.Gson) ClearTextSecretsProvider(org.apache.pulsar.functions.secretsprovider.ClearTextSecretsProvider) HTTPServer(io.prometheus.client.exporter.HTTPServer) InstanceConfig(org.apache.pulsar.functions.instance.InstanceConfig) ThreadRuntimeFactory(org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactory) SecretsProvider(org.apache.pulsar.functions.secretsprovider.SecretsProvider) ClearTextSecretsProvider(org.apache.pulsar.functions.secretsprovider.ClearTextSecretsProvider) RuntimeSpawner(org.apache.pulsar.functions.runtime.RuntimeSpawner)

Example 5 with SecretsProvider

use of org.apache.pulsar.functions.secretsprovider.SecretsProvider in project pulsar by yahoo.

the class ThreadRuntimeFactory method createContainer.

@Override
public ThreadRuntime createContainer(InstanceConfig instanceConfig, String jarFile, String originalCodeFileName, Long expectedHealthCheckInterval) {
    SecretsProvider secretsProvider = defaultSecretsProvider;
    if (secretsProvider == null) {
        String secretsProviderClassName = secretsProviderConfigurator.getSecretsProviderClassName(instanceConfig.getFunctionDetails());
        secretsProvider = (SecretsProvider) Reflections.createInstance(secretsProviderClassName, this.rootClassLoader);
        log.info("Initializing secrets provider {} with configs: {}", secretsProvider.getClass().getName(), secretsProviderConfigurator.getSecretsProviderConfig(instanceConfig.getFunctionDetails()));
        secretsProvider.init(secretsProviderConfigurator.getSecretsProviderConfig(instanceConfig.getFunctionDetails()));
    }
    return new ThreadRuntime(instanceConfig, fnCache, threadGroup, jarFile, pulsarClient, clientBuilder, pulsarAdmin, stateStorageImplClass, storageServiceUrl, secretsProvider, collectorRegistry, narExtractionDirectory, connectorsManager);
}
Also used : SecretsProvider(org.apache.pulsar.functions.secretsprovider.SecretsProvider)

Aggregations

SecretsProvider (org.apache.pulsar.functions.secretsprovider.SecretsProvider)9 Gson (com.google.gson.Gson)6 HTTPServer (io.prometheus.client.exporter.HTTPServer)6 InetSocketAddress (java.net.InetSocketAddress)6 InstanceConfig (org.apache.pulsar.functions.instance.InstanceConfig)6 FunctionCollectorRegistry (org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry)6 ThreadRuntimeFactory (org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactory)6 ClearTextSecretsProvider (org.apache.pulsar.functions.secretsprovider.ClearTextSecretsProvider)6 JCommander (com.beust.jcommander.JCommander)3 Type (java.lang.reflect.Type)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 Function (org.apache.pulsar.functions.proto.Function)3 RuntimeSpawner (org.apache.pulsar.functions.runtime.RuntimeSpawner)3