Copilot commented on code in PR #9840:
URL: https://github.com/apache/ozone/pull/9840#discussion_r3018923109


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/ozone/upgrade/AbstractLayoutVersionManager.java:
##########
@@ -45,155 +49,183 @@ public abstract class AbstractLayoutVersionManager<T 
extends LayoutFeature>
   private static final Logger LOG =
       LoggerFactory.getLogger(AbstractLayoutVersionManager.class);
 
-  private volatile int metadataLayoutVersion; // MLV.
-  private volatile int softwareLayoutVersion; // SLV.
+  private final AtomicReference<State> state = new AtomicReference<>();
+
+  private final AtomicBoolean initialized = new AtomicBoolean(false);
+
   @VisibleForTesting
-  protected final TreeMap<Integer, LayoutFeature> features = new TreeMap<>();
+  protected volatile NavigableMap<Integer, LayoutFeature> features =
+      Collections.unmodifiableNavigableMap(new TreeMap<>());
   @VisibleForTesting
-  protected final Map<String, LayoutFeature> featureMap = new HashMap<>();
-  private volatile Status currentUpgradeState;
+  protected volatile Map<String, LayoutFeature> featureMap =
+      Collections.unmodifiableMap(new HashMap<>());
   // Allows querying upgrade state while an upgrade is in progress.
   // Note that MLV may have been incremented during the upgrade
   // by the time the value is read/used.
-  private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
   private ObjectName mBean;
 
-  protected void init(int version, T[] lfs) throws IOException {
-    lock.writeLock().lock();
-    try {
-      metadataLayoutVersion = version;
-      initializeFeatures(lfs);
-      softwareLayoutVersion = features.lastKey();
-      if (softwareIsBehindMetaData()) {
-        throw new IOException(
-            String.format("Cannot initialize VersionManager. Metadata " +
-                    "layout version (%d) > software layout version (%d)",
-                metadataLayoutVersion, softwareLayoutVersion));
-      } else if (metadataLayoutVersion == softwareLayoutVersion) {
-        currentUpgradeState = ALREADY_FINALIZED;
-      } else {
-        currentUpgradeState = FINALIZATION_REQUIRED;
-      }
+  private static final class State {
+    final int metadataLayoutVersion; // MLV
+    final int softwareLayoutVersion; // SLV
+    final Status currentUpgradeState;
 
-      LayoutFeature mlvFeature = features.get(metadataLayoutVersion);
-      LayoutFeature slvFeature = features.get(softwareLayoutVersion);
-      LOG.info("Initializing Layout version manager with metadata layout" +
-              " = {} (version = {}), software layout = {} (version = {})",
-          mlvFeature, mlvFeature.layoutVersion(),
-          slvFeature, slvFeature.layoutVersion());
-
-      mBean = MBeans.register("LayoutVersionManager",
-          getClass().getSimpleName(), this);
-    } finally {
-      lock.writeLock().unlock();
+    private State(int mlv, int slv, Status status) {
+      this.metadataLayoutVersion = mlv;
+      this.softwareLayoutVersion = slv;
+      this.currentUpgradeState = status;
     }
-  }
 
-  public Status getUpgradeState() {
-    lock.readLock().lock();
-    try {
-      return currentUpgradeState;
-    } finally {
-      lock.readLock().unlock();
+    private static Status computeStatus(int mlv, int slv) {
+      return (mlv >= slv) ? ALREADY_FINALIZED : FINALIZATION_REQUIRED;
     }
-  }
 
-  public void setUpgradeState(Status status) {
-    lock.writeLock().lock();
-    try {
-      currentUpgradeState = status;
-    } finally {
-      lock.writeLock().unlock();
+    private State withStatus(Status status) {
+      return new State(metadataLayoutVersion, softwareLayoutVersion, status);
     }
+
+    private State withMlv(int newMlv) {
+      return new State(newMlv, softwareLayoutVersion, computeStatus(newMlv, 
softwareLayoutVersion));
+    }
+  }
+  
+  private State requireState() {
+    State s = state.get();
+    Preconditions.checkState(s != null, "LayoutVersionManager is not 
initialized.");
+    return s;
   }
 
-  private void initializeFeatures(T[] lfs) {
+  protected void init(int version, T[] lfs) throws IOException {
+    Preconditions.checkArgument(initialized.compareAndSet(false, true),
+        "LayoutVersionManager is already initialized.");
+    final TreeMap<Integer, LayoutFeature> localFeatures = new TreeMap<>();
+    final Map<String, LayoutFeature> localFeatureMap = new HashMap<>();

Review Comment:
   init() sets initialized=true before publishing state via state.set(...). If 
the LayoutVersionManager instance is visible to other threads during init, 
getters will hit requireState()==null and throw even though initialization is 
in progress (previously the RW lock would block readers until init completed). 
Consider using state itself as the initialization guard (eg compareAndSet(null, 
newState) at the end, or an IN_PROGRESS sentinel), or only flipping the 
initialized flag after state is published.



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/ozone/upgrade/AbstractLayoutVersionManager.java:
##########
@@ -45,155 +49,183 @@ public abstract class AbstractLayoutVersionManager<T 
extends LayoutFeature>
   private static final Logger LOG =
       LoggerFactory.getLogger(AbstractLayoutVersionManager.class);
 
-  private volatile int metadataLayoutVersion; // MLV.
-  private volatile int softwareLayoutVersion; // SLV.
+  private final AtomicReference<State> state = new AtomicReference<>();
+
+  private final AtomicBoolean initialized = new AtomicBoolean(false);
+
   @VisibleForTesting
-  protected final TreeMap<Integer, LayoutFeature> features = new TreeMap<>();
+  protected volatile NavigableMap<Integer, LayoutFeature> features =
+      Collections.unmodifiableNavigableMap(new TreeMap<>());
   @VisibleForTesting
-  protected final Map<String, LayoutFeature> featureMap = new HashMap<>();
-  private volatile Status currentUpgradeState;
+  protected volatile Map<String, LayoutFeature> featureMap =
+      Collections.unmodifiableMap(new HashMap<>());
   // Allows querying upgrade state while an upgrade is in progress.
   // Note that MLV may have been incremented during the upgrade
   // by the time the value is read/used.
-  private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
   private ObjectName mBean;
 
-  protected void init(int version, T[] lfs) throws IOException {
-    lock.writeLock().lock();
-    try {
-      metadataLayoutVersion = version;
-      initializeFeatures(lfs);
-      softwareLayoutVersion = features.lastKey();
-      if (softwareIsBehindMetaData()) {
-        throw new IOException(
-            String.format("Cannot initialize VersionManager. Metadata " +
-                    "layout version (%d) > software layout version (%d)",
-                metadataLayoutVersion, softwareLayoutVersion));
-      } else if (metadataLayoutVersion == softwareLayoutVersion) {
-        currentUpgradeState = ALREADY_FINALIZED;
-      } else {
-        currentUpgradeState = FINALIZATION_REQUIRED;
-      }
+  private static final class State {
+    final int metadataLayoutVersion; // MLV
+    final int softwareLayoutVersion; // SLV
+    final Status currentUpgradeState;
 
-      LayoutFeature mlvFeature = features.get(metadataLayoutVersion);
-      LayoutFeature slvFeature = features.get(softwareLayoutVersion);
-      LOG.info("Initializing Layout version manager with metadata layout" +
-              " = {} (version = {}), software layout = {} (version = {})",
-          mlvFeature, mlvFeature.layoutVersion(),
-          slvFeature, slvFeature.layoutVersion());
-
-      mBean = MBeans.register("LayoutVersionManager",
-          getClass().getSimpleName(), this);
-    } finally {
-      lock.writeLock().unlock();
+    private State(int mlv, int slv, Status status) {
+      this.metadataLayoutVersion = mlv;
+      this.softwareLayoutVersion = slv;
+      this.currentUpgradeState = status;
     }
-  }
 
-  public Status getUpgradeState() {
-    lock.readLock().lock();
-    try {
-      return currentUpgradeState;
-    } finally {
-      lock.readLock().unlock();
+    private static Status computeStatus(int mlv, int slv) {
+      return (mlv >= slv) ? ALREADY_FINALIZED : FINALIZATION_REQUIRED;
     }
-  }
 
-  public void setUpgradeState(Status status) {
-    lock.writeLock().lock();
-    try {
-      currentUpgradeState = status;
-    } finally {
-      lock.writeLock().unlock();
+    private State withStatus(Status status) {
+      return new State(metadataLayoutVersion, softwareLayoutVersion, status);
     }
+
+    private State withMlv(int newMlv) {
+      return new State(newMlv, softwareLayoutVersion, computeStatus(newMlv, 
softwareLayoutVersion));

Review Comment:
   State.withMlv() recomputes currentUpgradeState via computeStatus(), so each 
call to finalized() can overwrite FINALIZATION_IN_PROGRESS / 
STARTING_FINALIZATION / FINALIZATION_DONE back to FINALIZATION_REQUIRED (until 
MLV==SLV) or ALREADY_FINALIZED. This breaks upgrade progress reporting and can 
re-trigger finalize flows that key off getUpgradeState()==FINALIZATION_REQUIRED 
(eg datanode finalize command handler). Consider preserving the existing 
upgrade status when advancing MLV (eg carry over cur.currentUpgradeState), and 
only derive the initial status during init().
   ```suggestion
         // Preserve the existing upgrade state when advancing MLV.
         // Initial status should be derived when the State is created (e.g. 
during init()).
         return new State(newMlv, softwareLayoutVersion, currentUpgradeState);
   ```



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