Search in sources :

Example 1 with WorkerInfo

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;
}
Also used : Response(javax.ws.rs.core.Response) CompletableFuture(java.util.concurrent.CompletableFuture) WorkerInfo(org.apache.pulsar.common.functions.WorkerInfo) ClientErrorException(javax.ws.rs.ClientErrorException) WebTarget(javax.ws.rs.client.WebTarget)

Example 2 with WorkerInfo

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());
        }
    }
}
Also used : Assignment(org.apache.pulsar.functions.proto.Function.Assignment) WebApplicationException(javax.ws.rs.WebApplicationException) WorkerInfo(org.apache.pulsar.common.functions.WorkerInfo) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) URI(java.net.URI)

Example 3 with WorkerInfo

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();
}
Also used : Assignment(org.apache.pulsar.functions.proto.Function.Assignment) FunctionInstanceStatsImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsImpl) WebApplicationException(javax.ws.rs.WebApplicationException) WorkerInfo(org.apache.pulsar.common.functions.WorkerInfo) FunctionStatsImpl(org.apache.pulsar.common.policies.data.FunctionStatsImpl) FunctionInstanceStatsDataImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsDataImpl) URI(java.net.URI)

Example 4 with WorkerInfo

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;
}
Also used : Assignment(org.apache.pulsar.functions.proto.Function.Assignment) WebApplicationException(javax.ws.rs.WebApplicationException) WorkerInfo(org.apache.pulsar.common.functions.WorkerInfo) ErrorData(org.apache.pulsar.common.policies.data.ErrorData)

Example 5 with WorkerInfo

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);
}
Also used : WorkerInfo(org.apache.pulsar.common.functions.WorkerInfo) ContainerExecResult(org.apache.pulsar.tests.integration.docker.ContainerExecResult)

Aggregations

WorkerInfo (org.apache.pulsar.common.functions.WorkerInfo)99 LinkedList (java.util.LinkedList)57 Test (org.testng.annotations.Test)54 Function (org.apache.pulsar.functions.proto.Function)51 Map (java.util.Map)45 ThreadRuntimeFactory (org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactory)45 Assignment (org.apache.pulsar.functions.proto.Function.Assignment)42 Mockito.anyString (org.mockito.Mockito.anyString)39 HashMap (java.util.HashMap)36 Invocation (org.mockito.invocation.Invocation)30 TypedMessageBuilder (org.apache.pulsar.client.api.TypedMessageBuilder)24 URI (java.net.URI)18 WebApplicationException (javax.ws.rs.WebApplicationException)18 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)18 lombok.val (lombok.val)12 PulsarClient (org.apache.pulsar.client.api.PulsarClient)12 Reader (org.apache.pulsar.client.api.Reader)12 ThreadRuntimeFactoryConfig (org.apache.pulsar.functions.runtime.thread.ThreadRuntimeFactoryConfig)12 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)10 RestException (org.apache.pulsar.common.util.RestException)10