use of org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry 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();
}
use of org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry 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);
}
}
use of org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry 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);
}
}
use of org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry in project incubator-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);
}
}
use of org.apache.pulsar.functions.instance.stats.FunctionCollectorRegistry in project incubator-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(() -> {
// 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();
}
Aggregations