use of org.apache.pulsar.common.functions.WorkerInfo in project pulsar by apache.
the class WorkerImpl method getClusterLeaderAsync.
@Override
public CompletableFuture<WorkerInfo> getClusterLeaderAsync() {
WebTarget path = worker.path("cluster").path("leader");
final CompletableFuture<WorkerInfo> future = new CompletableFuture<>();
asyncGetRequest(path, new InvocationCallback<Response>() {
@Override
public void completed(Response response) {
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
future.completeExceptionally(new ClientErrorException(response));
} else {
future.complete(response.readEntity(new GenericType<WorkerInfo>() {
}));
}
}
@Override
public void failed(Throwable throwable) {
future.completeExceptionally(getApiException(throwable.getCause()));
}
});
return future;
}
use of org.apache.pulsar.common.functions.WorkerInfo in project pulsar by apache.
the class FunctionRuntimeManager method restartFunctionInstance.
public synchronized void restartFunctionInstance(String tenant, String namespace, String functionName, int instanceId, URI uri) throws Exception {
if (runtimeFactory.externallyManaged()) {
throw new WebApplicationException(Response.serverError().status(Status.NOT_IMPLEMENTED).type(MediaType.APPLICATION_JSON).entity(new ErrorData("Externally managed schedulers can't do per instance stop")).build());
}
Assignment assignment = this.findAssignment(tenant, namespace, functionName, instanceId);
final String fullFunctionName = String.format("%s/%s/%s/%s", tenant, namespace, functionName, instanceId);
if (assignment == null) {
throw new WebApplicationException(Response.serverError().status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(fullFunctionName + " doesn't exist")).build());
}
final String assignedWorkerId = assignment.getWorkerId();
final String workerId = this.workerConfig.getWorkerId();
if (assignedWorkerId.equals(workerId)) {
stopFunction(FunctionCommon.getFullyQualifiedInstanceId(assignment.getInstance()), true);
return;
} else {
// query other worker
List<WorkerInfo> workerInfoList = this.membershipManager.getCurrentMembership();
WorkerInfo workerInfo = null;
for (WorkerInfo entry : workerInfoList) {
if (assignment.getWorkerId().equals(entry.getWorkerId())) {
workerInfo = entry;
}
}
if (workerInfo == null) {
throw new WebApplicationException(Response.serverError().status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(fullFunctionName + " has not been assigned yet")).build());
}
if (uri == null) {
throw new WebApplicationException(Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build());
} else {
URI redirect = UriBuilder.fromUri(uri).host(workerInfo.getWorkerHostname()).port(workerInfo.getPort()).build();
throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
}
}
}
use of org.apache.pulsar.common.functions.WorkerInfo in project pulsar by apache.
the class FunctionRuntimeManager method getFunctionStats.
/**
* Get stats of all function instances.
*
* @param tenant the tenant the function belongs to
* @param namespace the namespace the function belongs to
* @param functionName the function name
* @return a list of function statuses
* @throws PulsarAdminException
*/
public FunctionStatsImpl getFunctionStats(String tenant, String namespace, String functionName, URI uri) throws PulsarAdminException {
Collection<Assignment> assignments = this.findFunctionAssignments(tenant, namespace, functionName);
FunctionStatsImpl functionStats = new FunctionStatsImpl();
if (assignments.isEmpty()) {
return functionStats;
}
if (runtimeFactory.externallyManaged()) {
Assignment assignment = assignments.iterator().next();
boolean isOwner = this.workerConfig.getWorkerId().equals(assignment.getWorkerId());
if (isOwner) {
int parallelism = assignment.getInstance().getFunctionMetaData().getFunctionDetails().getParallelism();
for (int i = 0; i < parallelism; ++i) {
FunctionInstanceStatsDataImpl functionInstanceStatsData = getFunctionInstanceStats(tenant, namespace, functionName, i, null);
FunctionInstanceStatsImpl functionInstanceStats = new FunctionInstanceStatsImpl();
functionInstanceStats.setInstanceId(i);
functionInstanceStats.setMetrics(functionInstanceStatsData);
functionStats.addInstance(functionInstanceStats);
}
} else {
// find the hostname/port of the worker who is the owner
List<WorkerInfo> workerInfoList = this.membershipManager.getCurrentMembership();
WorkerInfo workerInfo = null;
for (WorkerInfo entry : workerInfoList) {
if (assignment.getWorkerId().equals(entry.getWorkerId())) {
workerInfo = entry;
}
}
if (workerInfo == null) {
return functionStats;
}
if (uri == null) {
throw new WebApplicationException(Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build());
} else {
URI redirect = UriBuilder.fromUri(uri).host(workerInfo.getWorkerHostname()).port(workerInfo.getPort()).build();
throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
}
}
} else {
for (Assignment assignment : assignments) {
boolean isOwner = this.workerConfig.getWorkerId().equals(assignment.getWorkerId());
FunctionInstanceStatsDataImpl functionInstanceStatsData;
if (isOwner) {
functionInstanceStatsData = getFunctionInstanceStats(tenant, namespace, functionName, assignment.getInstance().getInstanceId(), null);
} else {
functionInstanceStatsData = (FunctionInstanceStatsDataImpl) this.functionAdmin.functions().getFunctionStats(assignment.getInstance().getFunctionMetaData().getFunctionDetails().getTenant(), assignment.getInstance().getFunctionMetaData().getFunctionDetails().getNamespace(), assignment.getInstance().getFunctionMetaData().getFunctionDetails().getName(), assignment.getInstance().getInstanceId());
}
FunctionInstanceStatsImpl functionInstanceStats = new FunctionInstanceStatsImpl();
functionInstanceStats.setInstanceId(assignment.getInstance().getInstanceId());
functionInstanceStats.setMetrics(functionInstanceStatsData);
functionStats.addInstance(functionInstanceStats);
}
}
return functionStats.calculateOverall();
}
use of org.apache.pulsar.common.functions.WorkerInfo in project pulsar by apache.
the class FunctionRuntimeManager method restartFunctionInstances.
public synchronized void restartFunctionInstances(String tenant, String namespace, String functionName) throws Exception {
final String fullFunctionName = String.format("%s/%s/%s", tenant, namespace, functionName);
Collection<Assignment> assignments = this.findFunctionAssignments(tenant, namespace, functionName);
if (assignments.isEmpty()) {
throw new WebApplicationException(Response.serverError().status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(fullFunctionName + " has not been assigned yet")).build());
}
if (runtimeFactory.externallyManaged()) {
Assignment assignment = assignments.iterator().next();
final String assignedWorkerId = assignment.getWorkerId();
final String workerId = this.workerConfig.getWorkerId();
String fullyQualifiedInstanceId = FunctionCommon.getFullyQualifiedInstanceId(assignment.getInstance());
if (assignedWorkerId.equals(workerId)) {
stopFunction(fullyQualifiedInstanceId, true);
} else {
List<WorkerInfo> workerInfoList = this.membershipManager.getCurrentMembership();
WorkerInfo workerInfo = null;
for (WorkerInfo entry : workerInfoList) {
if (assignment.getWorkerId().equals(entry.getWorkerId())) {
workerInfo = entry;
}
}
if (workerInfo == null) {
if (log.isDebugEnabled()) {
log.debug("[{}] has not been assigned yet", fullyQualifiedInstanceId);
}
throw new WebApplicationException(Response.serverError().status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(fullFunctionName + " has not been assigned yet")).build());
}
restartFunctionUsingPulsarAdmin(assignment, tenant, namespace, functionName, true);
}
} else {
for (Assignment assignment : assignments) {
final String assignedWorkerId = assignment.getWorkerId();
final String workerId = this.workerConfig.getWorkerId();
String fullyQualifiedInstanceId = FunctionCommon.getFullyQualifiedInstanceId(assignment.getInstance());
if (assignedWorkerId.equals(workerId)) {
stopFunction(fullyQualifiedInstanceId, true);
} else {
List<WorkerInfo> workerInfoList = this.membershipManager.getCurrentMembership();
WorkerInfo workerInfo = null;
for (WorkerInfo entry : workerInfoList) {
if (assignment.getWorkerId().equals(entry.getWorkerId())) {
workerInfo = entry;
}
}
if (workerInfo == null) {
if (log.isDebugEnabled()) {
log.debug("[{}] has not been assigned yet", fullyQualifiedInstanceId);
}
continue;
}
restartFunctionUsingPulsarAdmin(assignment, tenant, namespace, functionName, false);
}
}
}
return;
}
use of org.apache.pulsar.common.functions.WorkerInfo in project pulsar by apache.
the class PulsarWorkerRebalanceDrainTest method getClusterLeader.
private WorkerInfo getClusterLeader() throws Exception {
ContainerExecResult result = pulsarCluster.getAnyWorker().execCmd(PulsarCluster.ADMIN_SCRIPT, "functions-worker", "get-cluster-leader");
List<WorkerInfo> winfos = workerInfoDecode(result.getStdout());
assertEquals(winfos.size(), 1);
return workerInfoDecode(result.getStdout()).get(0);
}
Aggregations