rdhabalia commented on a change in pull request #2365: Add support to restart 
function
URL: https://github.com/apache/incubator-pulsar/pull/2365#discussion_r209829347
 
 

 ##########
 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:
   actually if function owns by same worker then I created 
`restartFunction(fullyQualifiedInstanceId);` to reuse code. but if function 
owner is different then we need a blocking call to aggregate result. however, 
in restartInstance function, we just redirect request to other host without 
blocking call. so, we can't reuse redirection part.
   Also, I am going to create separate PR for line 400 to use pulsar-admin to 
reuse Web-Client and support proper auth&auth.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to