fapifta commented on a change in pull request #1611:
URL: https://github.com/apache/ozone/pull/1611#discussion_r530713077



##########
File path: 
hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/upgrade/BasicUpgradeFinalizer.java
##########
@@ -0,0 +1,300 @@
+/**
+ * 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.hadoop.ozone.upgrade;
+
+import static 
org.apache.hadoop.ozone.upgrade.UpgradeException.ResultCodes.INVALID_REQUEST;
+import static 
org.apache.hadoop.ozone.upgrade.UpgradeException.ResultCodes.LAYOUT_FEATURE_FINALIZATION_FAILED;
+import static 
org.apache.hadoop.ozone.upgrade.UpgradeException.ResultCodes.PERSIST_UPGRADE_TO_LAYOUT_VERSION_FAILED;
+import static 
org.apache.hadoop.ozone.upgrade.UpgradeException.ResultCodes.REMOVE_UPGRADE_TO_LAYOUT_VERSION_FAILED;
+import static 
org.apache.hadoop.ozone.upgrade.UpgradeException.ResultCodes.UPDATE_LAYOUT_VERSION_FAILED;
+import static 
org.apache.hadoop.ozone.upgrade.UpgradeFinalizer.Status.FINALIZATION_DONE;
+import static 
org.apache.hadoop.ozone.upgrade.UpgradeFinalizer.Status.FINALIZATION_IN_PROGRESS;
+import static 
org.apache.hadoop.ozone.upgrade.UpgradeFinalizer.Status.FINALIZATION_REQUIRED;
+import static 
org.apache.hadoop.ozone.upgrade.UpgradeFinalizer.Status.STARTING_FINALIZATION;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+import org.apache.hadoop.ozone.common.Storage;
+import org.apache.hadoop.ozone.upgrade.LayoutFeature.UpgradeAction;
+import org.apache.hadoop.ozone.upgrade.UpgradeException.ResultCodes;
+
+/**
+ * UpgradeFinalizer implementation for the Storage Container Manager service.
+ */
+@SuppressWarnings("checkstyle:VisibilityModifier")
+public class BasicUpgradeFinalizer<T, V extends AbstractLayoutVersionManager>
+    implements UpgradeFinalizer<T> {
+
+  protected V versionManager;
+  protected String clientID;
+  protected T component;
+
+  private Queue<String> msgs = new ConcurrentLinkedQueue<>();
+  protected boolean isDone = false;
+
+  public BasicUpgradeFinalizer(V versionManager) {
+    this.versionManager = versionManager;
+  }
+
+  public StatusAndMessages preFinalize(String upgradeClientID, T id)
+      throws UpgradeException {
+    switch (versionManager.getUpgradeState()) {
+    case STARTING_FINALIZATION:
+      return STARTING_MSG;
+    case FINALIZATION_IN_PROGRESS:
+      return FINALIZATION_IN_PROGRESS_MSG;
+    case FINALIZATION_DONE:
+    case ALREADY_FINALIZED:
+      return FINALIZED_MSG;
+    default:
+      if (!versionManager.needsFinalization()) {
+        throw new UpgradeException("Upgrade found in inconsistent state. " +
+            "Upgrade state is FINALIZATION_REQUIRED while MLV has been " +
+            "upgraded to SLV.", INVALID_REQUEST);
+      }
+      versionManager.setUpgradeState(STARTING_FINALIZATION);
+
+      clientID = upgradeClientID;
+      this.component = id;
+      return FINALIZATION_REQUIRED_MSG;
+    }
+  }
+
+  /*
+   * This method must be overriden by the component implementing the
+   * finalization logic.
+   */
+  public StatusAndMessages finalize(String upgradeClientID, T id)
+      throws IOException {
+    StatusAndMessages response = preFinalize(upgradeClientID, id);
+    if (response.status() != FINALIZATION_REQUIRED) {
+      return response;
+    }
+
+    /**
+     * Overriding class should schedule actual finalization logic
+     * in a separate thread here.
+     */
+    return STARTING_MSG;
+  }
+
+  @Override
+  public synchronized StatusAndMessages reportStatus(
+      String upgradeClientID, boolean takeover
+  ) throws UpgradeException {
+    if (takeover) {
+      clientID = upgradeClientID;
+    }
+    assertClientId(upgradeClientID);
+    List<String> returningMsgs = new ArrayList<>(msgs.size()+10);
+    Status status = isDone ? FINALIZATION_DONE : FINALIZATION_IN_PROGRESS;
+    while (msgs.size() > 0) {
+      returningMsgs.add(msgs.poll());
+    }
+    return new StatusAndMessages(status, returningMsgs);
+  }
+
+  private void assertClientId(String id) throws UpgradeException {
+    if (!this.clientID.equals(id)) {
+      throw new UpgradeException("Unknown client tries to get finalization " +
+          "status.\n The requestor is not the initiating client of the " +
+          "finalization, if you want to take over, and get unsent status " +
+          "messages, check -takeover option.", INVALID_REQUEST);
+    }
+  }
+
+  protected void finalizeFeature(LayoutFeature feature, Storage config)
+      throws UpgradeException {
+    Optional<? extends UpgradeAction> action = feature.onFinalizeAction();
+
+    if (action.isPresent()) {

Review comment:
       Sorry for this, but my wording was not correct as it seems.
   Let me put it this way:
   Here if action is present in the optional, we emit a noop message and 
return, I believe we want to emit a noop message and return if action.isPresent 
yields is false, so we miss a negation in the condition.




----------------------------------------------------------------
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.

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