Browse Source

AMBARI-13405. Improve update repo custom action.(vbrodetskyi)

Vitaly Brodetskyi 10 years ago
parent
commit
80c1c9c308

+ 1 - 0
ambari-server/src/main/java/org/apache/ambari/server/Role.java

@@ -114,6 +114,7 @@ public class Role {
   public static final Role AMS_SERVICE_CHECK = valueOf("AMBARI_METRICS_SERVICE_CHECK");
   public static final Role ACCUMULO_CLIENT = valueOf("ACCUMULO_CLIENT");
   public static final Role INSTALL_PACKAGES = valueOf("install_packages");
+  public static final Role UPDATE_REPO = valueOf("update_repo");
 
   private String name = null;
 

+ 33 - 2
ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariActionExecutionHelper.java

@@ -71,6 +71,10 @@ public class AmbariActionExecutionHelper {
   private final static Logger LOG =
       LoggerFactory.getLogger(AmbariActionExecutionHelper.class);
   private static final String TYPE_PYTHON = "PYTHON";
+  private static final String ACTION_UPDATE_REPO = "update_repo";
+  private static final String SUCCESS_FACTOR_PARAMETER = "success_factor";
+
+  private static final float UPDATE_REPO_SUCCESS_FACTOR_DEFAULT = 0f;
 
   @Inject
   private Clusters clusters;
@@ -224,8 +228,6 @@ public class AmbariActionExecutionHelper {
    *
    * @param actionContext  the context associated with the action
    * @param stage          stage into which tasks must be inserted
-   * @param retryAllowed   indicates whether retry is allowed on failure
-   *
    * @throws AmbariException if the task can not be added
    */
   public void addExecutionCommandsToStage(final ActionExecutionContext actionContext, Stage stage)
@@ -346,6 +348,8 @@ public class AmbariActionExecutionHelper {
       }
     }
 
+    setAdditionalParametersForStageAccordingToAction(stage, actionContext);
+
     // create tasks for each host
     for (String hostName : targetHosts) {
       stage.addHostRoleExecutionCommand(hostName, Role.valueOf(actionContext.getActionName()),
@@ -429,6 +433,33 @@ public class AmbariActionExecutionHelper {
     }
   }
 
+  /*
+  * This method adds additional properties
+  * to action params. For example: success factor.
+  *
+  * */
+
+  private void setAdditionalParametersForStageAccordingToAction(Stage stage, ActionExecutionContext actionExecutionContext) throws AmbariException {
+    if (actionExecutionContext.getActionName().equals(ACTION_UPDATE_REPO)) {
+      Map<String, String> params = actionExecutionContext.getParameters();
+      float successFactor = UPDATE_REPO_SUCCESS_FACTOR_DEFAULT;
+      if (params != null && params.containsKey(SUCCESS_FACTOR_PARAMETER)) {
+        try{
+          successFactor = Float.valueOf(params.get(SUCCESS_FACTOR_PARAMETER));
+        } catch (Exception ex) {
+          throw new AmbariException("Failed to cast success_factor value to float!", ex.getCause());
+        }
+      }
+      stage.getSuccessFactors().put(Role.UPDATE_REPO, successFactor);
+    }
+  }
+
+  /*
+  * This method builds and adds repo info
+  * to hostLevelParams of action
+  *
+  * */
+
   private void addRepoInfoToHostLevelParams(Cluster cluster, Map<String, String> hostLevelParams, String hostName) throws AmbariException {
     if (cluster != null) {
       JsonObject rootJsonObject = new JsonObject();

+ 14 - 0
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java

@@ -4514,6 +4514,10 @@ public class AmbariManagementControllerTest {
         "a2", ActionType.SYSTEM, "", "HDFS", "DATANODE", "Does file exist",
         TargetHostType.ANY, Short.valueOf("100")));
 
+    controller.getAmbariMetaInfo().addActionDefinition(new ActionDefinition(
+            "update_repo", ActionType.SYSTEM, "", "HDFS", "DATANODE", "Does file exist",
+            TargetHostType.ANY, Short.valueOf("100")));
+
     controller.getAmbariMetaInfo().addActionDefinition(new ActionDefinition(
         "a3", ActionType.SYSTEM, "", "MAPREDUCE", "MAPREDUCE_CLIENT", "Does file exist",
         TargetHostType.ANY, Short.valueOf("100")));
@@ -4594,6 +4598,16 @@ public class AmbariManagementControllerTest {
     expectActionCreationErrorWithMessage(actionRequest, requestProperties,
         "Request specifies host h6 but its not a valid host based on the target service=HDFS and component=DATANODE");
 
+    hosts.clear();
+    hosts.add("h1");
+    resourceFilters.clear();
+    resourceFilter = new RequestResourceFilter("", "", hosts);
+    resourceFilters.add(resourceFilter);
+    params.put("success_factor", "1r");
+    actionRequest = new ExecuteActionRequest("c1", null, "update_repo", resourceFilters, null, params, false);
+    expectActionCreationErrorWithMessage(actionRequest, requestProperties,
+            "Failed to cast success_factor value to float!");
+
     resourceFilters.clear();
     resourceFilter = new RequestResourceFilter("HIVE", "", null);
     resourceFilters.add(resourceFilter);