peterxcli commented on code in PR #11252: URL: https://github.com/apache/ozone/pull/11252#discussion_r4152712315
########## hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/ReadConsistencyContext.java: ########## @@ -0,0 +1,108 @@ +/* + * 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.s3.util; + +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.INVALID_ARGUMENT; +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.newError; +import static org.apache.hadoop.ozone.s3.util.S3Consts.LOCAL_LEASE_LOG_LIMIT_HEADER; +import static org.apache.hadoop.ozone.s3.util.S3Consts.READ_CONSISTENCY_HEADER; + +import java.util.Locale; +import javax.ws.rs.core.HttpHeaders; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.ozone.om.helpers.ReadConsistency; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; + +/** + * Read consistency values parsed from S3 request headers. + */ +public final class ReadConsistencyContext { + private final ReadConsistency readConsistency; + private final Long localLeaseLogLimit; + + private ReadConsistencyContext(ReadConsistency readConsistency, + Long localLeaseLogLimit) { + this.readConsistency = readConsistency; + this.localLeaseLogLimit = localLeaseLogLimit; + } + + public static ReadConsistencyContext fromHeaders(HttpHeaders headers) { + return fromHeaders(headers.getHeaderString(READ_CONSISTENCY_HEADER), + headers.getHeaderString(LOCAL_LEASE_LOG_LIMIT_HEADER)); + } + + public static ReadConsistencyContext fromHeaders(String readConsistencyHeader, + String localLeaseLogLimitHeader) { + ReadConsistency readConsistency = parseReadConsistency(readConsistencyHeader); + Long localLeaseLogLimit = parseLocalLeaseLogLimit(localLeaseLogLimitHeader); + if (readConsistency != ReadConsistency.LOCAL_LEASE + && localLeaseLogLimit != null) { + OS3Exception ex = newError(INVALID_ARGUMENT, READ_CONSISTENCY_HEADER); + ex.setErrorMessage("Local lease context requires read consistency: follower-stale"); + throw ex; + } + return new ReadConsistencyContext(readConsistency, localLeaseLogLimit); + } + + public ReadConsistency getReadConsistency() { + return readConsistency; + } + + public Long getLocalLeaseLogLimit() { + return localLeaseLogLimit; + } + + private static ReadConsistency parseReadConsistency(String header) { + if (StringUtils.isBlank(header)) { + return null; + } + switch (header.trim().toLowerCase(Locale.ROOT)) { + case "follower-stale": + return ReadConsistency.LOCAL_LEASE; + case "leader-only": + return ReadConsistency.LINEARIZABLE_LEADER_ONLY; + case "follower-linearizable": Review Comment: any change these liternal string could be enum? maybe put them into `ReadConsistency.java`? ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/protocolPB/GrpcOmTransport.java: ########## @@ -324,6 +334,25 @@ private synchronized String getCurrentFollowerReadNodeId() { .get(currentFollowerReadIndex); } + private synchronized String getCurrentFollowerReadNodeId( + ReadConsistency readConsistency) { + String nodeId = getCurrentFollowerReadNodeId(); + if (readConsistency != ReadConsistency.LOCAL_LEASE) { + return nodeId; + } + + String leaderNodeId = omFailoverProxyProvider.getCurrentProxyOMNodeId(); + for (int i = 0; i < omFailoverProxyProvider.getOMProxyMap().getNodeIds().size(); + i++) { + if (!nodeId.equals(leaderNodeId)) { + return nodeId; + } + changeFollowerReadProxy(nodeId); + nodeId = getCurrentFollowerReadNodeId(); + } + return null; + } Review Comment: This is a little bit hard to understand, so we're doing mutation in a getXXX function? any chance we could refactor this? ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/ha/HadoopRpcOMFollowerReadFailoverProxyProvider.java: ########## @@ -117,7 +111,8 @@ public HadoopRpcOMFollowerReadFailoverProxyProvider( .map(a -> a.proxyInfo) .reduce((a, b) -> a + ", " + b).orElse("") + "]"; combinedProxy = new ProxyInfo<>(new FollowerReadProxy(), combinedInfo); - this.useFollowerRead = useFollowerRead; + this.omServiceSupportsFollowerRead = true; Review Comment: This is test only? Curious why we can set follow read directly to true without checking anything(eg. om version, config) ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/ha/OMFailoverProxyProviderBase.java: ########## @@ -425,6 +427,25 @@ public List<OMProxyInfo<T>> getOMProxies() { return omProxies.getProxies(); } + /** + * Determines whether an eligible read request may be sent to an OM follower. + * + * <p>An explicit consistency hint takes precedence over the configured + * default follower-read behavior.</p> + */ + public static boolean shouldUseFollowerRead(OMRequest request, + boolean omServiceSupportsFollowerRead, + boolean defaultFollowerReadEnabled) { + if (!omServiceSupportsFollowerRead || !OmUtils.shouldSendToFollower(request)) { + return false; + } + if (request.hasReadConsistencyHint()) { + return ReadConsistency.fromProto(request.getReadConsistencyHint() + .getReadConsistency()).allowFollowerRead(); + } + return defaultFollowerReadEnabled; + } Review Comment: we might want to extract/refactor the change introduced in https://github.com/apache/ozone/pull/10591 to `GrpcOMFollowerReadFailoverProxyProvider`, and add `OMollowerReadFailoverProxyProviderBase` as base class for `GrpcOMFollowerReadFailoverProxyProvider` and `HadoopRpcOMFollowerReadFailoverProxyProvider`, so we can put follower read things (eg. `defaultFollowerReadEnabled`, some helper) in it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
