jackjlli commented on a change in pull request #7174:
URL: https://github.com/apache/pinot/pull/7174#discussion_r685532513



##########
File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotControllerPeriodicTaskRestletResource.java
##########
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.controller.api.resources;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+import java.util.ArrayList;
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import org.apache.helix.ClusterMessagingService;
+import org.apache.helix.Criteria;
+import org.apache.helix.InstanceType;
+import org.apache.pinot.common.messages.RunPeriodicTaskMessage;
+import org.apache.pinot.controller.helix.core.PinotHelixResourceManager;
+import org.apache.pinot.core.periodictask.PeriodicTaskScheduler;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+@Api(tags = Constants.PERIODIC_TASK_TAG)
+@Path("/periodictask")
+public class PinotControllerPeriodicTaskRestletResource {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PinotControllerPeriodicTaskRestletResource.class);
+
+  @Inject
+  PinotHelixResourceManager _pinotHelixResourceManager;
+
+  @Inject
+  PeriodicTaskScheduler _periodicTaskScheduler;
+
+  @GET
+  @Produces(MediaType.APPLICATION_JSON)
+  @Path("/run")
+  @ApiOperation(value = "Run controller periodic task against the specified 
table. If no table name is specified, task will run against all tables.")
+  public boolean runPeriodicTask(
+      @ApiParam(value = "Periodic Task Name", required = true) 
@QueryParam("taskname") String periodicTaskName,
+      @ApiParam(value = "Table Name (with type)", required = false) 
@QueryParam("tablename") String tableName) {

Review comment:
       Can it be a list of table names?

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/periodictask/PeriodicTask.java
##########
@@ -59,6 +59,12 @@
   @Override
   void run();
 
+  /**
+   * Execute the task once. This method will calls the {@link #run} method.
+   * @param filter An implementation specific string that may dictate how the 
task will be run. null by default.
+   */
+  void run(String filter);

Review comment:
       Can we use Context object instead of a filter string? By using Context, 
it'll be easier to maintain and more flexible to extend.

##########
File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotControllerPeriodicTaskRestletResource.java
##########
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.controller.api.resources;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+import java.util.ArrayList;
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import org.apache.helix.ClusterMessagingService;
+import org.apache.helix.Criteria;
+import org.apache.helix.InstanceType;
+import org.apache.pinot.common.messages.RunPeriodicTaskMessage;
+import org.apache.pinot.controller.helix.core.PinotHelixResourceManager;
+import org.apache.pinot.core.periodictask.PeriodicTaskScheduler;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+@Api(tags = Constants.PERIODIC_TASK_TAG)
+@Path("/periodictask")
+public class PinotControllerPeriodicTaskRestletResource {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PinotControllerPeriodicTaskRestletResource.class);
+
+  @Inject
+  PinotHelixResourceManager _pinotHelixResourceManager;
+
+  @Inject
+  PeriodicTaskScheduler _periodicTaskScheduler;
+
+  @GET
+  @Produces(MediaType.APPLICATION_JSON)
+  @Path("/run")
+  @ApiOperation(value = "Run controller periodic task against the specified 
table. If no table name is specified, task will run against all tables.")
+  public boolean runPeriodicTask(
+      @ApiParam(value = "Periodic Task Name", required = true) 
@QueryParam("taskname") String periodicTaskName,
+      @ApiParam(value = "Table Name (with type)", required = false) 
@QueryParam("tablename") String tableName) {
+    if (!_periodicTaskScheduler.hasTask(periodicTaskName)) {
+      throw new WebApplicationException("Periodic task '" + periodicTaskName + 
"' not found.",
+          Response.Status.NOT_FOUND);
+    }
+
+    LOGGER.info("Sending periodic task execution message to all controllers 
for running task {} against {}.",
+        periodicTaskName, tableName != null ? tableName + " table" : "all 
tables");
+
+    // Create and send message to send to all controllers (including this one)
+    Criteria recipientCriteria = new Criteria();
+    recipientCriteria.setRecipientInstanceType(InstanceType.PARTICIPANT);
+    recipientCriteria.setInstanceName("%");
+    recipientCriteria.setSessionSpecific(true);
+    
recipientCriteria.setResource(CommonConstants.Helix.LEAD_CONTROLLER_RESOURCE_NAME);
+    recipientCriteria.setSelfExcluded(false);
+    RunPeriodicTaskMessage runPeriodicTaskMessage = new 
RunPeriodicTaskMessage(periodicTaskName, tableName);
+
+    ClusterMessagingService clusterMessagingService =
+        _pinotHelixResourceManager.getHelixZkManager().getMessagingService();
+    int messageCount = clusterMessagingService.send(recipientCriteria, 
runPeriodicTaskMessage, null, -1);
+    LOGGER.info("Periodic task execution message sent to {} controllers.", 
messageCount);
+    return messageCount > 0;
+  }
+
+  @GET
+  @Produces(MediaType.APPLICATION_JSON)
+  @Path("/names")
+  @ApiOperation(value = "Get comma-delimited list of all available periodic 
task names.")
+  public ArrayList<String> getPeriodicTaskNames() {
+    ArrayList<String> list = new ArrayList<>();
+    
list.add(org.apache.pinot.controller.validation.BrokerResourceValidationManager.class.getSimpleName());

Review comment:
       There is a variable called `_tasksWithValidInterval` in 
PeriodicTaskScheduler class, sth you can refer to instead of keeping a list of 
controller names here, which is hard to maintain.

##########
File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/ControllerMessageHandlerFactory.java
##########
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.controller;
+
+import org.apache.helix.NotificationContext;
+import org.apache.helix.messaging.handling.HelixTaskResult;
+import org.apache.helix.messaging.handling.MessageHandler;
+import org.apache.helix.messaging.handling.MessageHandlerFactory;
+import org.apache.helix.model.Message;
+import org.apache.pinot.common.messages.RunPeriodicTaskMessage;
+import org.apache.pinot.core.periodictask.PeriodicTaskScheduler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/** Factory class for creating message handlers for incoming helix messages. */
+public class ControllerMessageHandlerFactory implements MessageHandlerFactory {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(ControllerMessageHandlerFactory.class);
+  private static final String USER_DEFINED_MSG_STRING = 
Message.MessageType.USER_DEFINE_MSG.toString();
+
+  private final PeriodicTaskScheduler _periodicTaskScheduler;
+
+  public ControllerMessageHandlerFactory(PeriodicTaskScheduler 
periodicTaskScheduler) {
+    _periodicTaskScheduler = periodicTaskScheduler;
+  }
+
+  @Override
+  public MessageHandler createHandler(Message message, NotificationContext 
notificationContext) {
+    String messageType = message.getMsgSubType();
+    if 
(messageType.equals(RunPeriodicTaskMessage.RUN_PERIODIC_TASK_MSG_SUB_TYPE)) {
+      return new RunPeriodicTaskMessageHandler(new 
RunPeriodicTaskMessage(message), notificationContext, _periodicTaskScheduler);
+    }
+
+    LOGGER.warn("Unknown message type {} received by controller. ", 
messageType);
+    return null;
+  }
+
+  @Override
+  public String getMessageType() {
+    return USER_DEFINED_MSG_STRING;
+  }
+
+  @Override
+  public void reset() {
+  }
+
+  /** Message handler for "Run Periodic Task" message. */
+  private static class RunPeriodicTaskMessageHandler extends MessageHandler {
+    private final String _periodicTaskName;
+    private final String _tableName;

Review comment:
       Could this table name be any table name like raw table name and table 
name with suffix? Can we add some javadoc on that?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to