You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by "ivandika3 (via GitHub)" <gi...@apache.org> on 2026/02/20 09:51:11 UTC

[PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

ivandika3 opened a new pull request, #9796:
URL: https://github.com/apache/ozone/pull/9796

   ## What changes were proposed in this pull request?
   
   Currently, if OM follower read is enabled, all OM followers will serve read requests even if the client does not enable the follower read. 
   
   For read-only client, this might be unexpected since they expect the OM follower to throw OMNotLeaderException. 
   
   We can support some kind of OM request metadata to indicate to the OM whether the client enables the follower read or not.
   
   The implementation introduces `ReadConsistencyHint` field in `OMRequest` which allows client to hint to the OM about what the read consistency the client wants. However, for compatibility reasons, the client `ReadConsistencyHint` is only supported by OM in a best-effort basis. For example, if the new client sends the hint to old OM or OM that disables linearizable read, the OM can choose not to respect the read consistency.
   
   Currently, client can configure the default leader and follower read consistency which applies to corresponding requests. However, the long term idea is to allow client to specifies per-request consistency requirements. For example, for S3 we can use custom header (e.g. "x-ozone-read-consistency") to pick the desired consistency.
   
   The idea of client-defined consistency level is inspired by other systems
   - RQLite: https://rqlite.io/docs/api/read-consistency/
   - Consul: https://developer.hashicorp.com/consul/api-docs/features/consistency
   
   ## What is the link to the Apache JIRA
   
   https://issues.apache.org/jira/browse/HDDS-14509
   
   ## How was this patch tested?
   
   IT.
   
   Clean CI run: https://github.com/ivandika3/ozone/actions/runs/22216665699


-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2844372646


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,42 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasConsistencyType() ||
+        request.getReadConsistencyHint().getConsistencyType() == CONSISTENCY_TYPE_UNKNOWN) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyType consistencyType = readConsistencyHint.getConsistencyType();
+      RaftServerStatus raftServerStatus;
+      switch (consistencyType) {
+      case STALE:
+        // Serve the stale read request immediately for both leader and follower
+        ozoneManager.getMetrics().incNumStaleRead();
+        return handler.handleReadRequest(request);
+      case LOCAL_LEASE_FOLLOWER_READ:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.getLagLimit() > 0 ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.getLeaseTimeMs() > 0 ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_NOT_READY:

Review Comment:
   Thanks for catching this, updated.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2850700708


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {

Review Comment:
   Thanks, updated.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -270,12 +383,19 @@ boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLogLimit,
       return false; // lease time expired
     }
 
+    if (leaseLagLimit == -1) {
+      // Allow infinite lag time, which allows unbounded stale reads

Review Comment:
   Thanks for catching this, updated.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2842421637


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,42 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasConsistencyType() ||
+        request.getReadConsistencyHint().getConsistencyType() == CONSISTENCY_TYPE_UNKNOWN) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyType consistencyType = readConsistencyHint.getConsistencyType();
+      RaftServerStatus raftServerStatus;
+      switch (consistencyType) {
+      case STALE:
+        // Serve the stale read request immediately for both leader and follower
+        ozoneManager.getMetrics().incNumStaleRead();
+        return handler.handleReadRequest(request);
+      case LOCAL_LEASE_FOLLOWER_READ:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.getLagLimit() > 0 ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.getLeaseTimeMs() > 0 ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_NOT_READY:

Review Comment:
   For LOCAL_LEASE_FOLLOWER_READ, the LEADER_AND_NOT_READY case should be treated as the same as the NOT_LEADER case, i.e. just consider it as a follower. (fixed typo "LEADER_AND_READY")
   
   



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2850701205


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyProto readConsistency = readConsistencyHint.getReadConsistency();
+      RaftServerStatus raftServerStatus;
+      switch (readConsistency) {
+      case LOCAL_LEASE:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.hasLagLimit() ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.hasLeaseTimeMs() ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          // Although local lease does not apply for leader (since leader is always up-to-date)
+          // We still add the local lease metrics for compatibility reasons
+          ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+          return handler.handleReadRequest(request);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_LEADER_ONLY:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            // If linearizable read is not enabled, fallback to leader read
+            return handler.handleReadRequest(request);
+          }
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_ALLOW_FOLLOWER:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case NOT_LEADER:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+        case LEADER_AND_READY:
+          if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+            // leader directly serves local committed data
+            return handler.handleReadRequest(request);
+          }
+
+          // If the Raft server read option is not LINEARIZABLE, this will
+          // use leader read
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+          }
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case DEFAULT:
+      default:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_READY:
+          return handler.handleReadRequest(request);
+        case LEADER_AND_NOT_READY:
+        case NOT_LEADER:
+          throw createLeaderErrorException(raftServerStatus);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      }
     }
   }
 
-  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLogLimit, long leaseTimeMsLimit) {
+  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLagLimit, long leaseTimeMsLimit) {
     final DivisionInfo divisionInfo = ratisDivision.getInfo();
     final FollowerInfoProto followerInfo = divisionInfo.getRoleInfoProto().getFollowerInfo();
+    if (leaseTimeMsLimit == -1) {
+      leaseTimeMsLimit = Long.MAX_VALUE;
+    }

Review Comment:
   Thanks, updated.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on PR #9796:
URL: https://github.com/apache/ozone/pull/9796#issuecomment-3939552849

   @ivandika3 , thanks for working on this!
   
   This PR also includes some refactoring/renaming and the patch file is ~70KB.   Could you separate the refactoring/renaming to another JIRA?  It will be easier to review.


-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2842292299


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/ReadConsistency.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.om.helpers;
+
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.ReadConsistencyType;
+
+/**
+ * Supported read consistency. It consists of two elements
+ * <ol>
+ *   <li>
+ *     {@link ConsistencyType} which specifies different types of read consistency
+ *     (e.g. LINEARIZABLE, LOCAL_LEASE)
+ *   </li>
+ *   <li>
+ *     Whether to allow follower read. Some consistency types supports
+ *     follower read (e.g. LINEARIZABLE) while other consistency types only
+ *     supports leader read (e.g. NON_LINEARIZABLE).
+ *   </li>
+ * </ol>
+ *
+ * handles different types of read consistency (e.g. LINEARIZABLE, LOCAL_LEASE)
+ */
+public enum ReadConsistency {
+  DEFAULT(ConsistencyType.NON_LINEARIZABLE, false),
+  STALE(ConsistencyType.STALE, true),
+  LINEARIZABLE_LEADER_READ(ConsistencyType.LINEARIZABLE, false),
+  LINEARIZABLE_FOLLOWER_READ(ConsistencyType.LINEARIZABLE, true),
+  LOCAL_LEASE_FOLLOWER_READ(ConsistencyType.LOCAL_LEASE, true);

Review Comment:
   1. When LINEARIZABLE is enabled, do we really want to disallow follower read?
   2. Stale read can be supported by local lease with infinite log lag and time limit.  We may remove STALE for simplicity.
   3. I think we don't need the inner enum `ConsistencyType`.  Just add a boolean for linearizable.
   4. `isAllowFollowerRead` sounds odd.  Let's use nouns for the fields and add the verb to the method.
   
   If the answer of 1. is "no", I suggest the code below:
   
   ```java
   public enum ReadConsistency {
     DEFAULT(false, false),
     LINEARIZABLE(true, true),
     LOCAL_LEASE(false, true);
   
     private final boolean linearizable;
     private final boolean followerRead;
   
     ReadConsistency(boolean linearizable, boolean followerRead) {
       this.linearizable = linearizable;
       this.followerRead = followerRead;
     }
   
     public boolean isLinearizable() {
       return linearizable;
     }
   
     public boolean allowFollowerRead() {
       return followerRead;
     }
   
     ...
   }
   ```
   



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,42 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasConsistencyType() ||
+        request.getReadConsistencyHint().getConsistencyType() == CONSISTENCY_TYPE_UNKNOWN) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyType consistencyType = readConsistencyHint.getConsistencyType();
+      RaftServerStatus raftServerStatus;
+      switch (consistencyType) {
+      case STALE:
+        // Serve the stale read request immediately for both leader and follower
+        ozoneManager.getMetrics().incNumStaleRead();
+        return handler.handleReadRequest(request);
+      case LOCAL_LEASE_FOLLOWER_READ:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.getLagLimit() > 0 ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.getLeaseTimeMs() > 0 ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_NOT_READY:

Review Comment:
   For LOCAL_LEASE_FOLLOWER_READ, the LEADER_AND_READY case should be treated as the same as the NOT_LEADER case, i.e. just consider it as a follower.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,42 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasConsistencyType() ||
+        request.getReadConsistencyHint().getConsistencyType() == CONSISTENCY_TYPE_UNKNOWN) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyType consistencyType = readConsistencyHint.getConsistencyType();
+      RaftServerStatus raftServerStatus;
+      switch (consistencyType) {
+      case STALE:
+        // Serve the stale read request immediately for both leader and follower
+        ozoneManager.getMetrics().incNumStaleRead();
+        return handler.handleReadRequest(request);
+      case LOCAL_LEASE_FOLLOWER_READ:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.getLagLimit() > 0 ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.getLeaseTimeMs() > 0 ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();

Review Comment:
   Use localLeaseContext.getLagLimit() and hasLeaseTimeMs().  We may use -1 for allowing infinite lag/time.
   



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2850707132


##########
hadoop-ozone/interface-client/src/main/proto/OmClientProtocol.proto:
##########
@@ -2355,6 +2357,67 @@ message DeleteObjectTaggingRequest {
 message DeleteObjectTaggingResponse {
 }
 
+enum ReadConsistencyProto {
+  // Unknown consistency, the read consistency behavior is decided
+  // by the OM
+  UNKNOWN_READ_CONSISTENCY = 0;

Review Comment:
   Got it, updated. We might add some kind of suffix or prefix (e.g. `UNSPECIFIED_CONSISTENCY`) since AFAIK once one one enum uses `UNSPECIFIED` other enum within the scope cannot use `UNSPECIFIED` anymore. Please let me know what you think.



##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/ha/HadoopRpcOMFollowerReadFailoverProxyProvider.java:
##########
@@ -213,7 +228,26 @@ public Object invoke(Object proxy, final Method method, final Object[] args)
         return method.invoke(this, args);
       }
       OMRequest omRequest = parseOMRequest(args);
-      if (useFollowerRead && OmUtils.shouldSendToFollower(omRequest)) {
+
+      // Apply default consistency hint once, before any routing decision.
+      // In the future, we will support per-request hints which allows client (e.g. S3 clients)
+      // to specify a custom request header (e.g. x-ozone-read-consistency) as a consistency hint
+      // for read requests.
+      boolean isFollowerReadEligible = useFollowerRead && OmUtils.shouldSendToFollower(omRequest);
+      if (!omRequest.hasReadConsistencyHint()) {
+        ReadConsistencyProto defaultReadConsistency = isFollowerReadEligible
+            ? followerReadConsistencyType : leaderReadConsistencyType;
+        if (defaultReadConsistency != null &&
+            defaultReadConsistency != ReadConsistencyProto.UNKNOWN_READ_CONSISTENCY) {
+          omRequest = omRequest.toBuilder()
+              .setReadConsistencyHint(ReadConsistencyHint.newBuilder()
+                  .setReadConsistency(defaultReadConsistency).build())
+              .build();

Review Comment:
   Thanks for this optimization, updated.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2851313023


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyProto readConsistency = readConsistencyHint.getReadConsistency();
+      RaftServerStatus raftServerStatus;
+      switch (readConsistency) {
+      case LOCAL_LEASE:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.hasLagLimit() ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.hasLeaseTimeMs() ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          // Although local lease does not apply for leader (since leader is always up-to-date)
+          // We still add the local lease metrics for compatibility reasons
+          ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+          return handler.handleReadRequest(request);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_LEADER_ONLY:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            // If linearizable read is not enabled, fallback to leader read
+            return handler.handleReadRequest(request);
+          }
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_ALLOW_FOLLOWER:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case NOT_LEADER:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+        case LEADER_AND_READY:
+          if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+            // leader directly serves local committed data
+            return handler.handleReadRequest(request);
+          }
+
+          // If the Raft server read option is not LINEARIZABLE, this will
+          // use leader read
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+          }
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case DEFAULT:
+      default:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_READY:
+          return handler.handleReadRequest(request);
+        case LEADER_AND_NOT_READY:
+        case NOT_LEADER:
+          throw createLeaderErrorException(raftServerStatus);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      }
     }
   }
 
-  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLogLimit, long leaseTimeMsLimit) {
+  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLagLimit, long leaseTimeMsLimit) {
     final DivisionInfo divisionInfo = ratisDivision.getInfo();
     final FollowerInfoProto followerInfo = divisionInfo.getRoleInfoProto().getFollowerInfo();
+    if (leaseTimeMsLimit == -1) {
+      leaseTimeMsLimit = Long.MAX_VALUE;
+    }

Review Comment:
   The initial local lease implementation allows negative `leaseTimeMsLimit` which means that lease will be rejected all the time? It is tested by `testAllowFollowerReadLocalLease`. IMO, any negative `leaseTimeMsLimit` should infer infinite log and lag time? In that case, should we change the test to reflect this?



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2850703353


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyProto readConsistency = readConsistencyHint.getReadConsistency();
+      RaftServerStatus raftServerStatus;
+      switch (readConsistency) {
+      case LOCAL_LEASE:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.hasLagLimit() ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.hasLeaseTimeMs() ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          // Although local lease does not apply for leader (since leader is always up-to-date)
+          // We still add the local lease metrics for compatibility reasons
+          ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+          return handler.handleReadRequest(request);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_LEADER_ONLY:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            // If linearizable read is not enabled, fallback to leader read
+            return handler.handleReadRequest(request);
+          }
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_ALLOW_FOLLOWER:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case NOT_LEADER:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+        case LEADER_AND_READY:
+          if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+            // leader directly serves local committed data
+            return handler.handleReadRequest(request);
+          }
+
+          // If the Raft server read option is not LINEARIZABLE, this will
+          // use leader read
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+          }
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case DEFAULT:
+      default:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_READY:
+          return handler.handleReadRequest(request);
+        case LEADER_AND_NOT_READY:
+        case NOT_LEADER:
+          throw createLeaderErrorException(raftServerStatus);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      }
     }
   }
 
-  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLogLimit, long leaseTimeMsLimit) {
+  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLagLimit, long leaseTimeMsLimit) {

Review Comment:
   Thanks for catching this, updated. The initial change was because of the configuration which lead me to believe it's a typo.



##########
hadoop-ozone/ozonefs-hadoop2/src/main/java/org/apache/hadoop/fs/ozone/Hadoop27RpcTransport.java:
##########
@@ -46,6 +52,8 @@ public class Hadoop27RpcTransport implements OmTransport {
   private final OzoneManagerProtocolPB rpcProxy;
 
   private final HadoopRpcOMFailoverProxyProvider<OzoneManagerProtocolPB> omFailoverProxyProvider;
+  private final boolean followerReadEnabled;
+  private final ReadConsistencyProto defaultLeaderReadConsistency;

Review Comment:
   Thanks, updated.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2844371816


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/ReadConsistency.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.om.helpers;
+
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.ReadConsistencyType;
+
+/**
+ * Supported read consistency. It consists of two elements
+ * <ol>
+ *   <li>
+ *     {@link ConsistencyType} which specifies different types of read consistency
+ *     (e.g. LINEARIZABLE, LOCAL_LEASE)
+ *   </li>
+ *   <li>
+ *     Whether to allow follower read. Some consistency types supports
+ *     follower read (e.g. LINEARIZABLE) while other consistency types only
+ *     supports leader read (e.g. NON_LINEARIZABLE).
+ *   </li>
+ * </ol>
+ *
+ * handles different types of read consistency (e.g. LINEARIZABLE, LOCAL_LEASE)
+ */
+public enum ReadConsistency {
+  DEFAULT(ConsistencyType.NON_LINEARIZABLE, false),
+  STALE(ConsistencyType.STALE, true),
+  LINEARIZABLE_LEADER_READ(ConsistencyType.LINEARIZABLE, false),
+  LINEARIZABLE_FOLLOWER_READ(ConsistencyType.LINEARIZABLE, true),
+  LOCAL_LEASE_FOLLOWER_READ(ConsistencyType.LOCAL_LEASE, true);

Review Comment:
   Thanks for the review and the suggestion on streamlining the ReadConsistency. I have changed accordingly and change some namings (e.g. removed "Consistency Type" naming).



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2849023255


##########
hadoop-ozone/interface-client/src/main/proto/OmClientProtocol.proto:
##########
@@ -2355,6 +2357,67 @@ message DeleteObjectTaggingRequest {
 message DeleteObjectTaggingResponse {
 }
 
+enum ReadConsistencyProto {
+  // Unknown consistency, the read consistency behavior is decided
+  // by the OM
+  UNKNOWN_READ_CONSISTENCY = 0;

Review Comment:
   It is a good idea to have this as described in https://protobuf.dev/best-practices/dos-donts/#unspecified-enum.  How about calling it `UNSPECIFIED`?



##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/ReadConsistency.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.om.helpers;
+
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.ReadConsistencyProto;
+
+/**
+ * Supported read consistency.
+ */
+public enum ReadConsistency {

Review Comment:
   Add static ReadConsistencyHint to avoid building the same proto (except LOCAL_LEASE_HINT which need to add context):
   ```java
     private static final ReadConsistencyHint DEFAULT_HINT = ...;
     private static final ReadConsistencyHint LOCAL_LEASE_HINT = ...;
     private static final ReadConsistencyHint LINEARIZABLE_LEADER_ONLY_HINT = ...;
     private static final ReadConsistencyHint LINEARIZABLE_ALLOW_FOLLOWER_HINT = ...;
   
     public ReadConsistencyHint getHint() {
       switch (this) {
         case DEFAULT:
           return DEFAULT_HINT;
         case LOCAL_LEASE:
           return LOCAL_LEASE_HINT;
         case LINEARIZABLE_LEADER_ONLY:
           return LINEARIZABLE_LEADER_ONLY_HINT;
         case LINEARIZABLE_ALLOW_FOLLOWER:
           return LINEARIZABLE_ALLOW_FOLLOWER_HINT;
         default:
           return null;
       }
     }
   ```
   



##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/ha/HadoopRpcOMFollowerReadFailoverProxyProvider.java:
##########
@@ -213,7 +228,26 @@ public Object invoke(Object proxy, final Method method, final Object[] args)
         return method.invoke(this, args);
       }
       OMRequest omRequest = parseOMRequest(args);
-      if (useFollowerRead && OmUtils.shouldSendToFollower(omRequest)) {
+
+      // Apply default consistency hint once, before any routing decision.
+      // In the future, we will support per-request hints which allows client (e.g. S3 clients)
+      // to specify a custom request header (e.g. x-ozone-read-consistency) as a consistency hint
+      // for read requests.
+      boolean isFollowerReadEligible = useFollowerRead && OmUtils.shouldSendToFollower(omRequest);
+      if (!omRequest.hasReadConsistencyHint()) {
+        ReadConsistencyProto defaultReadConsistency = isFollowerReadEligible
+            ? followerReadConsistencyType : leaderReadConsistencyType;
+        if (defaultReadConsistency != null &&
+            defaultReadConsistency != ReadConsistencyProto.UNKNOWN_READ_CONSISTENCY) {
+          omRequest = omRequest.toBuilder()
+              .setReadConsistencyHint(ReadConsistencyHint.newBuilder()
+                  .setReadConsistency(defaultReadConsistency).build())
+              .build();

Review Comment:
   Change them to followerReadConsistencyType and leaderReadConsistencyType to ReadConsistencyHint.  So it does not need to build again and again.
   
   ```java
         if (!omRequest.hasReadConsistencyHint()) {
           final ReadConsistencyHint defaultReadConsistency = isFollowerReadEligible
               ? followerReadConsistencyType : leaderReadConsistencyType;
           if (defaultReadConsistency != null) {
             omRequest = omRequest.toBuilder()
                 .setReadConsistencyHint(defaultReadConsistency)
                 .build();
             args[1] = omRequest;
           }
         }
   ```



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -270,12 +383,19 @@ boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLogLimit,
       return false; // lease time expired
     }
 
+    if (leaseLagLimit == -1) {
+      // Allow infinite lag time, which allows unbounded stale reads

Review Comment:
   It should be "infinite log lag ~time~"



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyProto readConsistency = readConsistencyHint.getReadConsistency();
+      RaftServerStatus raftServerStatus;
+      switch (readConsistency) {
+      case LOCAL_LEASE:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.hasLagLimit() ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.hasLeaseTimeMs() ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          // Although local lease does not apply for leader (since leader is always up-to-date)
+          // We still add the local lease metrics for compatibility reasons
+          ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+          return handler.handleReadRequest(request);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_LEADER_ONLY:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            // If linearizable read is not enabled, fallback to leader read
+            return handler.handleReadRequest(request);
+          }
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_ALLOW_FOLLOWER:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case NOT_LEADER:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+        case LEADER_AND_READY:
+          if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+            // leader directly serves local committed data
+            return handler.handleReadRequest(request);
+          }
+
+          // If the Raft server read option is not LINEARIZABLE, this will
+          // use leader read
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+          }
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case DEFAULT:
+      default:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_READY:
+          return handler.handleReadRequest(request);
+        case LEADER_AND_NOT_READY:
+        case NOT_LEADER:
+          throw createLeaderErrorException(raftServerStatus);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      }
     }
   }
 
-  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLogLimit, long leaseTimeMsLimit) {
+  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLagLimit, long leaseTimeMsLimit) {

Review Comment:
   Since there are two types of lag, time lag and log lag, the original name `leaseLogLimit` is better.
   
   BTW, we should change the conf
   - `ozone.om.follower.read.local.lease.lag.limit`
   
   to
   - `ozone.om.follower.read.local.lease.log.limit`
   



##########
hadoop-ozone/ozonefs-hadoop2/src/main/java/org/apache/hadoop/fs/ozone/Hadoop27RpcTransport.java:
##########
@@ -46,6 +52,8 @@ public class Hadoop27RpcTransport implements OmTransport {
   private final OzoneManagerProtocolPB rpcProxy;
 
   private final HadoopRpcOMFailoverProxyProvider<OzoneManagerProtocolPB> omFailoverProxyProvider;
+  private final boolean followerReadEnabled;
+  private final ReadConsistencyProto defaultLeaderReadConsistency;

Review Comment:
   Use ReadConsistencyHint.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {

Review Comment:
   For better readability, add private methods:
   ```java
       if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
           request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {
         return submitReadRequestToOmWithoutHint(request);
       } else {
         // If read consistency hint is specified, we should try to respect it although
         // there is no guarantee since it depends on the OM node configuration (e.g.
         // whether OM Raft server enables linearizable read).
         final ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
         final ReadConsistencyProto readConsistency = readConsistencyHint.getReadConsistency();
         switch (readConsistency) {
           case LOCAL_LEASE:
             return submitReadRequestToOmLocalLease(request, readConsistencyHint.getLocalLeaseContext());
           case LINEARIZABLE_LEADER_ONLY:
             return submitReadRequestToOmLinearizableLeaderOnly(request);
           case LINEARIZABLE_ALLOW_FOLLOWER:
             return submitReadRequestToOmLinearizableAllowFollower(request);
           case DEFAULT:
           default:
             return submitReadRequestToOmDefault(request);
         }
       }
     }
   ```



##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/protocolPB/Hadoop3OmTransport.java:
##########
@@ -45,11 +51,13 @@ public class Hadoop3OmTransport implements OmTransport {
    */
   private static final RpcController NULL_RPC_CONTROLLER = null;
 
+  private final OzoneManagerProtocolPB rpcProxy;
+
   private final HadoopRpcOMFailoverProxyProvider<OzoneManagerProtocolPB> omFailoverProxyProvider;
+  private final boolean followerReadEnabled;
+  private final ReadConsistencyProto defaultLeaderReadConsistency;

Review Comment:
   Use ReadConsistencyHint



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/ratis/OzoneManagerRatisServer.java:
##########
@@ -514,10 +516,29 @@ private RaftClientRequest createRaftRequestImpl(OMRequest omRequest, boolean isW
         .setMessage(
             Message.valueOf(
                 OMRatisHelper.convertRequestToByteString(omRequest)))
-        .setType(isWrite ? RaftClientRequest.writeRequestType() : RaftClientRequest.readRequestType())
+        .setType(isWrite ? RaftClientRequest.writeRequestType() : getRaftReadRequestType(omRequest))
         .build();
   }
 
+  private RaftClientRequest.Type getRaftReadRequestType(OMRequest omRequest) {

Review Comment:
   Add `static`.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyProto readConsistency = readConsistencyHint.getReadConsistency();
+      RaftServerStatus raftServerStatus;
+      switch (readConsistency) {
+      case LOCAL_LEASE:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.hasLagLimit() ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.hasLeaseTimeMs() ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          // Although local lease does not apply for leader (since leader is always up-to-date)
+          // We still add the local lease metrics for compatibility reasons
+          ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+          return handler.handleReadRequest(request);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_LEADER_ONLY:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            // If linearizable read is not enabled, fallback to leader read
+            return handler.handleReadRequest(request);
+          }
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_ALLOW_FOLLOWER:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case NOT_LEADER:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+        case LEADER_AND_READY:
+          if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+            // leader directly serves local committed data
+            return handler.handleReadRequest(request);
+          }
+
+          // If the Raft server read option is not LINEARIZABLE, this will
+          // use leader read
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+          }
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case DEFAULT:
+      default:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_READY:
+          return handler.handleReadRequest(request);
+        case LEADER_AND_NOT_READY:
+        case NOT_LEADER:
+          throw createLeaderErrorException(raftServerStatus);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      }
     }
   }
 
-  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLogLimit, long leaseTimeMsLimit) {
+  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLagLimit, long leaseTimeMsLimit) {
     final DivisionInfo divisionInfo = ratisDivision.getInfo();
     final FollowerInfoProto followerInfo = divisionInfo.getRoleInfoProto().getFollowerInfo();
+    if (leaseTimeMsLimit == -1) {
+      leaseTimeMsLimit = Long.MAX_VALUE;
+    }

Review Comment:
   Combine to the if-condition below:
   ```java
       if (leaseTimeMsLimit >= 0 && leaderInfo.getLastRpcElapsedTimeMs() > leaseTimeMsLimit) {
   ```



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyProto readConsistency = readConsistencyHint.getReadConsistency();
+      RaftServerStatus raftServerStatus;
+      switch (readConsistency) {
+      case LOCAL_LEASE:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.hasLagLimit() ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.hasLeaseTimeMs() ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          // Although local lease does not apply for leader (since leader is always up-to-date)
+          // We still add the local lease metrics for compatibility reasons
+          ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+          return handler.handleReadRequest(request);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_LEADER_ONLY:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            // If linearizable read is not enabled, fallback to leader read
+            return handler.handleReadRequest(request);
+          }
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_ALLOW_FOLLOWER:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);

Review Comment:
   Remove this line since LEADER_AND_NOT_READY and NOT_LEADER should be the same.
   



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2848931904


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/ha/HadoopRpcOMFollowerReadFailoverProxyProvider.java:
##########
@@ -213,7 +228,26 @@ public Object invoke(Object proxy, final Method method, final Object[] args)
         return method.invoke(this, args);
       }
       OMRequest omRequest = parseOMRequest(args);
-      if (useFollowerRead && OmUtils.shouldSendToFollower(omRequest)) {
+
+      // Apply default consistency hint once, before any routing decision.
+      // In the future, we will support per-request hints which allows client (e.g. S3 clients)
+      // to specify a custom request header (e.g. x-ozone-read-consistency) as a consistency hint
+      // for read requests.
+      boolean isFollowerReadEligible = useFollowerRead && OmUtils.shouldSendToFollower(omRequest);
+      if (!omRequest.hasReadConsistencyHint()) {
+        ReadConsistencyProto defaultReadConsistency = isFollowerReadEligible
+            ? followerReadConsistencyType : leaderReadConsistencyType;
+        if (defaultReadConsistency != null &&
+            defaultReadConsistency != ReadConsistencyProto.UNKNOWN_READ_CONSISTENCY) {
+          omRequest = omRequest.toBuilder()
+              .setReadConsistencyHint(ReadConsistencyHint.newBuilder()
+                  .setReadConsistency(defaultReadConsistency).build())
+              .build();

Review Comment:
   Change them to followerReadConsistencyType and leaderReadConsistencyType to ReadConsistencyHint.  So it does not need to build again and again.
   
   ```java
    {
       ....
       this.followerReadConsistencyType = getReadConsistencyHint(followerReadConsistencyType);
       this.leaderReadConsistencyType = getReadConsistencyHint(leaderReadConsistencyType);
     }
   
     static ReadConsistencyHint getReadConsistencyHint(ReadConsistencyProto type) {
       return type == ReadConsistencyProto.UNKNOWN_READ_CONSISTENCY ? null
           : ReadConsistencyHint.newBuilder().setReadConsistency(type).build();
     }
   ```
   
   ```java
         if (!omRequest.hasReadConsistencyHint()) {
           final ReadConsistencyHint defaultReadConsistency = isFollowerReadEligible
               ? followerReadConsistencyType : leaderReadConsistencyType;
           if (defaultReadConsistency != null) {
             omRequest = omRequest.toBuilder()
                 .setReadConsistencyHint(defaultReadConsistency)
                 .build();
             args[1] = omRequest;
           }
         }
   ```



##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/ha/HadoopRpcOMFollowerReadFailoverProxyProvider.java:
##########
@@ -213,7 +228,26 @@ public Object invoke(Object proxy, final Method method, final Object[] args)
         return method.invoke(this, args);
       }
       OMRequest omRequest = parseOMRequest(args);
-      if (useFollowerRead && OmUtils.shouldSendToFollower(omRequest)) {
+
+      // Apply default consistency hint once, before any routing decision.
+      // In the future, we will support per-request hints which allows client (e.g. S3 clients)
+      // to specify a custom request header (e.g. x-ozone-read-consistency) as a consistency hint
+      // for read requests.
+      boolean isFollowerReadEligible = useFollowerRead && OmUtils.shouldSendToFollower(omRequest);
+      if (!omRequest.hasReadConsistencyHint()) {
+        ReadConsistencyProto defaultReadConsistency = isFollowerReadEligible
+            ? followerReadConsistencyType : leaderReadConsistencyType;
+        if (defaultReadConsistency != null &&
+            defaultReadConsistency != ReadConsistencyProto.UNKNOWN_READ_CONSISTENCY) {
+          omRequest = omRequest.toBuilder()
+              .setReadConsistencyHint(ReadConsistencyHint.newBuilder()
+                  .setReadConsistency(defaultReadConsistency).build())
+              .build();

Review Comment:
   Change them to followerReadConsistencyType and leaderReadConsistencyType to ReadConsistencyHint.  So it does not need to build again and again.
   
   ```java
    {
       ....
       this.followerReadConsistencyType = getReadConsistencyHint(followerReadConsistencyType);
       this.leaderReadConsistencyType = getReadConsistencyHint(leaderReadConsistencyType);
     }
   
     static ReadConsistencyHint getReadConsistencyHint(ReadConsistencyProto type) {
       return type == ReadConsistencyProto.UNKNOWN_READ_CONSISTENCY ? null
           : ReadConsistencyHint.newBuilder().setReadConsistency(type).build();
     }
   ```
   
   ```java
         if (!omRequest.hasReadConsistencyHint()) {
           final ReadConsistencyHint defaultReadConsistency = isFollowerReadEligible
               ? followerReadConsistencyType : leaderReadConsistencyType;
           if (defaultReadConsistency != null) {
             omRequest = omRequest.toBuilder()
                 .setReadConsistencyHint(defaultReadConsistency)
                 .build();
             args[1] = omRequest;
           }
         }
   ```



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2850668237


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyProto readConsistency = readConsistencyHint.getReadConsistency();
+      RaftServerStatus raftServerStatus;
+      switch (readConsistency) {
+      case LOCAL_LEASE:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.hasLagLimit() ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.hasLeaseTimeMs() ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          // Although local lease does not apply for leader (since leader is always up-to-date)
+          // We still add the local lease metrics for compatibility reasons
+          ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+          return handler.handleReadRequest(request);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_LEADER_ONLY:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            // If linearizable read is not enabled, fallback to leader read
+            return handler.handleReadRequest(request);
+          }
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_ALLOW_FOLLOWER:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);

Review Comment:
   Good catch. `LeaderStateImpl#getReadIndex` handles `isReady` and will wait until the leader apply the no-op entry.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on PR #9796:
URL: https://github.com/apache/ozone/pull/9796#issuecomment-3939987169

   Thanks @szetszwo for the review. I have removed some of refactoring by reverting rename from `leaderFailoverProxy` to `failoverProxy`. Regarding the refactoring for `transferLeadershipToAnotherNode` to `MiniOzoneHAClusterImpl`, I did it to remove duplication on this method in the new tests. Let me know if you'd prefer to revert this as well.


-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2854408068


##########
hadoop-ozone/interface-client/src/main/proto/OmClientProtocol.proto:
##########
@@ -2355,6 +2357,67 @@ message DeleteObjectTaggingRequest {
 message DeleteObjectTaggingResponse {
 }
 
+enum ReadConsistencyProto {
+  // Unspecified consistency, the read consistency behavior is decided
+  // by the OM
+  UNSPECIFIED = 0;

Review Comment:
   <img width="755" height="348" alt="image" src="https://github.com/user-attachments/assets/b600d2a1-6f66-40fa-a732-2ce557f19875" />
   Just realized that UNSPECIFIED only can be used once.  Let's use READ_CONSISTENCY_UNSPECIFIED



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/ratis/OzoneManagerRatisServer.java:
##########
@@ -514,10 +516,29 @@ private RaftClientRequest createRaftRequestImpl(OMRequest omRequest, boolean isW
         .setMessage(
             Message.valueOf(
                 OMRatisHelper.convertRequestToByteString(omRequest)))
-        .setType(isWrite ? RaftClientRequest.writeRequestType() : RaftClientRequest.readRequestType())
+        .setType(isWrite ? RaftClientRequest.writeRequestType() : getRaftReadRequestType(omRequest))
         .build();
   }
 
+  private static RaftClientRequest.Type getRaftReadRequestType(OMRequest omRequest) {
+    if (!omRequest.hasReadConsistencyHint() || !omRequest.getReadConsistencyHint().hasReadConsistency() ||
+        omRequest.getReadConsistencyHint().getReadConsistency() == ReadConsistencyProto.UNSPECIFIED) {

Review Comment:
   Add a util method.  Use it here and also OzoneManagerProtocolServerSideTranslatorPB.
   ```java
   //OmUtils
     public static boolean specifiedReadConsistency(OMRequest request) {
       return request.hasReadConsistencyHint()
           && request.getReadConsistencyHint().hasReadConsistency()
           && request.getReadConsistencyHint().getReadConsistency() != UNSPECIFIED;
     }
   ```



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2854642168


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,44 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasReadConsistency() ||
+        request.getReadConsistencyHint().getReadConsistency() == UNKNOWN_READ_CONSISTENCY) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyProto readConsistency = readConsistencyHint.getReadConsistency();
+      RaftServerStatus raftServerStatus;
+      switch (readConsistency) {
+      case LOCAL_LEASE:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.hasLagLimit() ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.hasLeaseTimeMs() ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();
+          if (allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              localLeaseLagLimit, localLeaseLeaseTimeMs)) {
+            ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+            return handler.handleReadRequest(request);
+          }
+          // The LocalLease lag is too high, trigger failover
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          // Although local lease does not apply for leader (since leader is always up-to-date)
+          // We still add the local lease metrics for compatibility reasons
+          ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+          return handler.handleReadRequest(request);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_LEADER_ONLY:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case LEADER_AND_READY:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            // If linearizable read is not enabled, fallback to leader read
+            return handler.handleReadRequest(request);
+          }
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case LINEARIZABLE_ALLOW_FOLLOWER:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_NOT_READY:
+          throw createLeaderErrorException(raftServerStatus);
+        case NOT_LEADER:
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+            return ozoneManager.getOmExecutionFlow().submit(request, false);
+          } else {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+        case LEADER_AND_READY:
+          if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+            // leader directly serves local committed data
+            return handler.handleReadRequest(request);
+          }
+
+          // If the Raft server read option is not LINEARIZABLE, this will
+          // use leader read
+          if (omRatisServer.isLinearizableRead()) {
+            ozoneManager.getMetrics().incNumLinearizableRead();
+          }
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      case DEFAULT:
+      default:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case LEADER_AND_READY:
+          return handler.handleReadRequest(request);
+        case LEADER_AND_NOT_READY:
+        case NOT_LEADER:
+          throw createLeaderErrorException(raftServerStatus);
+        default:
+          throw createUnknownRaftServerStatusException(raftServerStatus);
+        }
+      }
     }
   }
 
-  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLogLimit, long leaseTimeMsLimit) {
+  boolean allowFollowerReadLocalLease(Division ratisDivision, long leaseLagLimit, long leaseTimeMsLimit) {
     final DivisionInfo divisionInfo = ratisDivision.getInfo();
     final FollowerInfoProto followerInfo = divisionInfo.getRoleInfoProto().getFollowerInfo();
+    if (leaseTimeMsLimit == -1) {
+      leaseTimeMsLimit = Long.MAX_VALUE;
+    }

Review Comment:
   Let's make `leaseTimeMsLimit` >= -1 and use -1 for infinity.  We should change the test. 
   



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2844374192


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,42 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasConsistencyType() ||
+        request.getReadConsistencyHint().getConsistencyType() == CONSISTENCY_TYPE_UNKNOWN) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyType consistencyType = readConsistencyHint.getConsistencyType();
+      RaftServerStatus raftServerStatus;
+      switch (consistencyType) {
+      case STALE:
+        // Serve the stale read request immediately for both leader and follower
+        ozoneManager.getMetrics().incNumStaleRead();
+        return handler.handleReadRequest(request);
+      case LOCAL_LEASE_FOLLOWER_READ:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.getLagLimit() > 0 ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.getLeaseTimeMs() > 0 ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();

Review Comment:
   Thanks, used the `has...` and also update `allowFollowerReadLocalLease` to allow infinite lag.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/protocolPB/OzoneManagerProtocolServerSideTranslatorPB.java:
##########
@@ -216,42 +221,152 @@ public OMRequest getLastRequestToSubmit() {
 
   private OMResponse submitReadRequestToOM(OMRequest request)
       throws ServiceException {
-    // Read from leader or followers using linearizable read
-    if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
-        allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
-            ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
-      ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
+    if (request.getCmdType().equals(PrepareStatus)) {
+      // PrepareStatus is an OM request that only target a single OM node.
+      // Therefore, all PrepareStatus requests should be served immediately without failover regardless
+      // of the OM node leadership or the read consistency. See PrepareSubCommand.
+      // The implementation is not ideal, but exists for compatibility reason.
       return handler.handleReadRequest(request);
-    } 
-    // Get current OM's role
-    RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
-    // === 1. Follower linearizable read ===
-    if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
-      ozoneManager.getMetrics().incNumLinearizableRead();
-      return ozoneManager.getOmExecutionFlow().submit(request, false);
     }
-    // === 2. Leader local read (skip ReadIndex if allowed) ===
-    if (raftServerStatus == LEADER_AND_READY || request.getCmdType().equals(PrepareStatus)) {
-      if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
-        ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
-        // leader directly serves local committed data
+
+    if (!request.hasReadConsistencyHint() || !request.getReadConsistencyHint().hasConsistencyType() ||
+        request.getReadConsistencyHint().getConsistencyType() == CONSISTENCY_TYPE_UNKNOWN) {
+      // Read from leader or followers using linearizable read
+      if (ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled() &&
+          allowFollowerReadLocalLease(omRatisServer.getServerDivision(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit(),
+              ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs())) {
+        ozoneManager.getMetrics().incNumFollowerReadLocalLeaseSuccess();
         return handler.handleReadRequest(request);
       }
-      // otherwise use linearizable path when enabled
-      if (omRatisServer.isLinearizableRead()) {
+      // Get current OM's role
+      RaftServerStatus raftServerStatus = omRatisServer.getLeaderStatus();
+      // === 1. Follower linearizable read ===
+      if (raftServerStatus == NOT_LEADER && omRatisServer.isLinearizableRead()) {
         ozoneManager.getMetrics().incNumLinearizableRead();
         return ozoneManager.getOmExecutionFlow().submit(request, false);
       }
+      // === 2. Leader local read (skip ReadIndex if allowed) ===
+      if (raftServerStatus == LEADER_AND_READY) {
+        if (ozoneManager.getConfig().isAllowLeaderSkipLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLeaderSkipLinearizableRead();
+          // leader directly serves local committed data
+          return handler.handleReadRequest(request);
+        }
+        // otherwise use linearizable path when enabled
+        if (omRatisServer.isLinearizableRead()) {
+          ozoneManager.getMetrics().incNumLinearizableRead();
+          return ozoneManager.getOmExecutionFlow().submit(request, false);
+        }
 
-      // fallback to local read
-      return handler.handleReadRequest(request);
+        // fallback to local read
+        return handler.handleReadRequest(request);
+      } else {
+        throw createLeaderErrorException(raftServerStatus);
+      }
     } else {
-      throw createLeaderErrorException(raftServerStatus);
+      // If read consistency hint is specified, we should try to respect it although
+      // there is no guarantee since it depends on the OM node configuration (e.g.
+      // whether OM Raft server enables linearizable read).
+      ReadConsistencyHint readConsistencyHint = request.getReadConsistencyHint();
+      ReadConsistencyType consistencyType = readConsistencyHint.getConsistencyType();
+      RaftServerStatus raftServerStatus;
+      switch (consistencyType) {
+      case STALE:
+        // Serve the stale read request immediately for both leader and follower
+        ozoneManager.getMetrics().incNumStaleRead();
+        return handler.handleReadRequest(request);
+      case LOCAL_LEASE_FOLLOWER_READ:
+        raftServerStatus = omRatisServer.getLeaderStatus();
+        switch (raftServerStatus) {
+        case NOT_LEADER:
+          if (!ozoneManager.getConfig().isFollowerReadLocalLeaseEnabled()) {
+            throw createLeaderErrorException(raftServerStatus);
+          }
+          LocalLeaseContext localLeaseContext = readConsistencyHint.getLocalLeaseContext();
+          long localLeaseLagLimit = localLeaseContext.getLagLimit() > 0 ?
+              localLeaseContext.getLagLimit() : ozoneManager.getConfig().getFollowerReadLocalLeaseLagLimit();
+          long localLeaseLeaseTimeMs = localLeaseContext.getLeaseTimeMs() > 0 ?
+              localLeaseContext.getLeaseTimeMs() : ozoneManager.getConfig().getFollowerReadLocalLeaseTimeMs();

Review Comment:
   Thanks, used the `has...` and also updated `allowFollowerReadLocalLease` to allow infinite lag.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2842292299


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/ReadConsistency.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.om.helpers;
+
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.ReadConsistencyType;
+
+/**
+ * Supported read consistency. It consists of two elements
+ * <ol>
+ *   <li>
+ *     {@link ConsistencyType} which specifies different types of read consistency
+ *     (e.g. LINEARIZABLE, LOCAL_LEASE)
+ *   </li>
+ *   <li>
+ *     Whether to allow follower read. Some consistency types supports
+ *     follower read (e.g. LINEARIZABLE) while other consistency types only
+ *     supports leader read (e.g. NON_LINEARIZABLE).
+ *   </li>
+ * </ol>
+ *
+ * handles different types of read consistency (e.g. LINEARIZABLE, LOCAL_LEASE)
+ */
+public enum ReadConsistency {
+  DEFAULT(ConsistencyType.NON_LINEARIZABLE, false),
+  STALE(ConsistencyType.STALE, true),
+  LINEARIZABLE_LEADER_READ(ConsistencyType.LINEARIZABLE, false),
+  LINEARIZABLE_FOLLOWER_READ(ConsistencyType.LINEARIZABLE, true),
+  LOCAL_LEASE_FOLLOWER_READ(ConsistencyType.LOCAL_LEASE, true);

Review Comment:
   1. Stale read can be supported by local lease with infinite log lag and time limit.  We may remove STALE for simplicity.
   2. I think we don't need the inner enum `ConsistencyType`.  Just add a boolean for linearizable.
   3. `isAllowFollowerRead` sounds odd.  Let's use nouns for the fields and add the verb to the method.
   
   If the answer of 1. is "no", I suggest the code below:
   
   ```java
   public enum ReadConsistency {
     DEFAULT(false, false),
     LOCAL_LEASE(false, true),
     LINEARIZABLE_LEADER_ONLY(true, false),
     LINEARIZABLE_ALLOW_FOLLOWER(true, true);
   
     private final boolean linearizable;
     private final boolean followerRead;
   
     ReadConsistency(boolean linearizable, boolean followerRead) {
       this.linearizable = linearizable;
       this.followerRead = followerRead;
     }
   
     public boolean isLinearizable() {
       return linearizable;
     }
   
     public boolean allowFollowerRead() {
       return followerRead;
     }
   
     ...
   }
   ```
   



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "szetszwo (via GitHub)" <gi...@apache.org>.
szetszwo commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2842292299


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/ReadConsistency.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.om.helpers;
+
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.ReadConsistencyType;
+
+/**
+ * Supported read consistency. It consists of two elements
+ * <ol>
+ *   <li>
+ *     {@link ConsistencyType} which specifies different types of read consistency
+ *     (e.g. LINEARIZABLE, LOCAL_LEASE)
+ *   </li>
+ *   <li>
+ *     Whether to allow follower read. Some consistency types supports
+ *     follower read (e.g. LINEARIZABLE) while other consistency types only
+ *     supports leader read (e.g. NON_LINEARIZABLE).
+ *   </li>
+ * </ol>
+ *
+ * handles different types of read consistency (e.g. LINEARIZABLE, LOCAL_LEASE)
+ */
+public enum ReadConsistency {
+  DEFAULT(ConsistencyType.NON_LINEARIZABLE, false),
+  STALE(ConsistencyType.STALE, true),
+  LINEARIZABLE_LEADER_READ(ConsistencyType.LINEARIZABLE, false),
+  LINEARIZABLE_FOLLOWER_READ(ConsistencyType.LINEARIZABLE, true),
+  LOCAL_LEASE_FOLLOWER_READ(ConsistencyType.LOCAL_LEASE, true);

Review Comment:
   1. Stale read can be supported by local lease with infinite log lag and time limit.  We may remove STALE for simplicity.
   2. I think we don't need the inner enum `ConsistencyType`.  Just add a boolean for linearizable.
   3. `isAllowFollowerRead` sounds odd.  Let's use nouns for the fields and add the verb to the method.
   
   Below is my suggestion:
   
   ```java
   public enum ReadConsistency {
     DEFAULT(false, false),
     LOCAL_LEASE(false, true),
     LINEARIZABLE_LEADER_ONLY(true, false),
     LINEARIZABLE_ALLOW_FOLLOWER(true, true);
   
     private final boolean linearizable;
     private final boolean followerRead;
   
     ReadConsistency(boolean linearizable, boolean followerRead) {
       this.linearizable = linearizable;
       this.followerRead = followerRead;
     }
   
     public boolean isLinearizable() {
       return linearizable;
     }
   
     public boolean allowFollowerRead() {
       return followerRead;
     }
   
     ...
   }
   ```
   



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-14509. Allow client to choose the read consistency level [ozone]

Posted by "ivandika3 (via GitHub)" <gi...@apache.org>.
ivandika3 commented on code in PR #9796:
URL: https://github.com/apache/ozone/pull/9796#discussion_r2850703939


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/protocolPB/Hadoop3OmTransport.java:
##########
@@ -45,11 +51,13 @@ public class Hadoop3OmTransport implements OmTransport {
    */
   private static final RpcController NULL_RPC_CONTROLLER = null;
 
+  private final OzoneManagerProtocolPB rpcProxy;
+
   private final HadoopRpcOMFailoverProxyProvider<OzoneManagerProtocolPB> omFailoverProxyProvider;
+  private final boolean followerReadEnabled;
+  private final ReadConsistencyProto defaultLeaderReadConsistency;

Review Comment:
   Thanks, updated.



##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/ReadConsistency.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.om.helpers;
+
+import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.ReadConsistencyProto;
+
+/**
+ * Supported read consistency.
+ */
+public enum ReadConsistency {

Review Comment:
   Good idea, updated.



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org