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));
}
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());
}
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;
}
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));
}
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());
}
Aggregations