srkukarni commented on a change in pull request #2365: Add support to restart function URL: https://github.com/apache/incubator-pulsar/pull/2365#discussion_r209824676
########## File path: pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/FunctionRuntimeManager.java ########## @@ -317,6 +326,104 @@ public synchronized void removeAssignments(Collection<Assignment> assignments) { return functionStatus; } + public Response restartFunctionInstance(String tenant, String namespace, String functionName, int instanceId) throws Exception { + Assignment assignment = this.findAssignment(tenant, namespace, functionName, instanceId); + final String fullFunctionName = String.format("%s/%s/%s/%s", tenant, namespace, functionName, instanceId); + if (assignment == null) { + return Response.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)) { + restartFunction(Utils.getFullyQualifiedInstanceId(assignment.getInstance())); + return Response.status(Status.OK).build(); + } 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) { + return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON) + .entity(new ErrorData(fullFunctionName + " has not been assigned yet")).build(); + } + + URI redirect = null; + final String redirectUrl = String.format("http://%s:%d/admin/functions/%s/%s/%s/%d/restart", + workerInfo.getWorkerHostname(), workerInfo.getPort(), tenant, namespace, functionName, instanceId); + try { + redirect = new URI(redirectUrl); + } catch (URISyntaxException e) { + log.error("Error in preparing redirect url for {}/{}/{}/{}: {}", tenant, namespace, functionName, + instanceId, e.getMessage(), e); + return Response.status(Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON) + .entity(new ErrorData(fullFunctionName + " invalid redirection url")).build(); + } + throw new WebApplicationException(Response.temporaryRedirect(redirect).build()); + } + } + + public Response 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()) { + return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON) + .entity(new ErrorData(fullFunctionName + " has not been assigned yet")).build(); + } + for (Assignment assignment : assignments) { + final String assignedWorkerId = assignment.getWorkerId(); Review comment: a good refactor could be to reuse above function? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services