chihsuan commented on code in PR #11418: URL: https://github.com/apache/ozone/pull/11418#discussion_r4369504954
########## hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/ha/TestFollowerReadFailoverProxyProviderBase.java: ########## @@ -0,0 +1,205 @@ +/* + * 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.ha; + +import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_ADDRESS_KEY; +import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_NODES_KEY; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.ozone.ha.ConfUtils; +import org.apache.hadoop.ozone.om.helpers.ReadConsistency; +import org.apache.hadoop.ozone.om.protocolPB.OzoneManagerProtocolPB; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Type; +import org.apache.hadoop.security.UserGroupInformation; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * Tests for {@link FollowerReadFailoverProxyProviderBase}. + */ +public class TestFollowerReadFailoverProxyProviderBase { + private static final String OM_SERVICE_ID = "om-service-test1"; + + private HadoopRpcOMFailoverProxyProvider<OzoneManagerProtocolPB> leaderProxy; + /** The OM nodes in the order used for follower read. */ + private List<String> nodeIds; + + @BeforeEach + void setUp() throws IOException { + leaderProxy = newLeaderProxy("om1", "om2", "om3"); + nodeIds = new ArrayList<>(leaderProxy.getOMProxyMap().getNodeIds()); + } + + @Test + void testRejectInvalidReadConsistency() { + assertThrows(IllegalStateException.class, () -> new FollowerReadProvider(leaderProxy, + ReadConsistency.LINEARIZABLE_LEADER_ONLY, ReadConsistency.DEFAULT, true)); + assertThrows(IllegalStateException.class, () -> new FollowerReadProvider(leaderProxy, + ReadConsistency.LOCAL_LEASE, ReadConsistency.LOCAL_LEASE, true)); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void testReadWithoutHintUsesFollowerReadDefault(boolean defaultFollowerReadEnabled) { + final FollowerReadProvider provider = newProvider(defaultFollowerReadEnabled); + + assertEquals(defaultFollowerReadEnabled, provider.shouldUseFollowerRead(newRequest(Type.ListVolume))); + assertFalse(provider.shouldUseFollowerRead(newRequest(Type.CreateVolume))); + } + + @Test + void testReadConsistencyHintOverridesFollowerReadDefault() { + final FollowerReadProvider disabledByDefault = newProvider(false); + assertTrue(disabledByDefault.shouldUseFollowerRead(newReadRequest(ReadConsistency.LOCAL_LEASE))); + assertTrue(disabledByDefault.shouldUseFollowerRead(newReadRequest(ReadConsistency.LINEARIZABLE_ALLOW_FOLLOWER))); + + final FollowerReadProvider enabledByDefault = newProvider(true); + assertFalse(enabledByDefault.shouldUseFollowerRead(newReadRequest(ReadConsistency.LINEARIZABLE_LEADER_ONLY))); + assertFalse(enabledByDefault.shouldUseFollowerRead(newReadRequest(ReadConsistency.DEFAULT))); + } + + @Test + void testNoFollowerReadAfterFollowerReadIsDisabled() { + final FollowerReadProvider provider = newProvider(true); + assertTrue(provider.isOmServiceSupportsFollowerRead()); + + provider.disableFollowerRead(); + + assertFalse(provider.isOmServiceSupportsFollowerRead()); + assertFalse(provider.shouldUseFollowerRead(newRequest(Type.ListVolume))); + assertFalse(provider.shouldUseFollowerRead(newReadRequest(ReadConsistency.LOCAL_LEASE))); + } + + @Test + void testAddReadConsistencyHint() { + final FollowerReadProvider provider = newProvider(true); + + final OMRequest withoutHint = newRequest(Type.ListVolume); + assertEquals(ReadConsistency.LINEARIZABLE_ALLOW_FOLLOWER.getHint(), + provider.addReadConsistencyHint(withoutHint, true).getReadConsistencyHint()); + assertEquals(ReadConsistency.DEFAULT.getHint(), + provider.addReadConsistencyHint(withoutHint, false).getReadConsistencyHint()); + + // An existing hint is kept. + final OMRequest withHint = newReadRequest(ReadConsistency.LOCAL_LEASE); + assertSame(withHint, provider.addReadConsistencyHint(withHint, true)); + assertSame(withHint, provider.addReadConsistencyHint(withHint, false)); + } + + @Test + void testChangeFollowerReadNode() { + final FollowerReadProvider provider = newProvider(true); + assertEquals(nodeIds.get(0), provider.getCurrentFollowerReadNodeId()); + + provider.changeFollowerReadNodeId(nodeIds.get(0)); + assertEquals(nodeIds.get(1), provider.getCurrentFollowerReadNodeId()); + + // A change from a node which is no longer the current node is ignored. + provider.changeFollowerReadNodeId(nodeIds.get(0)); + assertEquals(nodeIds.get(1), provider.getCurrentFollowerReadNodeId()); + + // The change goes back to the first node after the last node. + provider.changeFollowerReadNodeId(nodeIds.get(1)); + provider.changeFollowerReadNodeId(nodeIds.get(2)); + assertEquals(nodeIds.get(0), provider.getCurrentFollowerReadNodeId()); + + provider.changeInitialProxyForTest(nodeIds.get(2)); + assertEquals(nodeIds.get(2), provider.getCurrentFollowerReadNodeId()); + } + + @Test + void testOnlyLocalLeaseReadSkipsLeader() { + final FollowerReadProvider provider = newProvider(true); + changeLeader(nodeIds.get(0)); + + assertEquals(nodeIds.get(0), provider.selectFollowerReadNodeId(ReadConsistency.LINEARIZABLE_ALLOW_FOLLOWER)); + assertEquals(nodeIds.get(1), provider.selectFollowerReadNodeId(ReadConsistency.LOCAL_LEASE)); + assertEquals(nodeIds.get(1), provider.getCurrentFollowerReadNodeId()); + } + + @Test + void testLocalLeaseReadWithoutFollower() throws Exception { + final FollowerReadProvider provider = new FollowerReadProvider(newLeaderProxy("om1"), + ReadConsistency.LINEARIZABLE_ALLOW_FOLLOWER, ReadConsistency.DEFAULT, true); + + assertNull(provider.selectFollowerReadNodeId(ReadConsistency.LOCAL_LEASE)); + } + + private static HadoopRpcOMFailoverProxyProvider<OzoneManagerProtocolPB> newLeaderProxy(String... omNodeIds) + throws IOException { + final OzoneConfiguration conf = new OzoneConfiguration(); + conf.set(ConfUtils.addKeySuffixes(OZONE_OM_NODES_KEY, OM_SERVICE_ID), String.join(",", omNodeIds)); + for (String nodeId : omNodeIds) { + conf.set(ConfUtils.addKeySuffixes(OZONE_OM_ADDRESS_KEY, OM_SERVICE_ID, nodeId), "0.0.0.0:8080"); Review Comment: Let's switch to `localhost:8080` here so the test passes the new wildcard address check from HDDS-16308. ```suggestion conf.set(ConfUtils.addKeySuffixes(OZONE_OM_ADDRESS_KEY, OM_SERVICE_ID, nodeId), "localhost:8080"); ``` ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/ha/GrpcOMFollowerReadFailoverProxyProvider.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.ha; + +import static org.apache.hadoop.ozone.om.ha.OMFailoverProxyProviderBase.getLeaderNotReadyException; +import static org.apache.hadoop.ozone.om.ha.OMFailoverProxyProviderBase.getNotLeaderException; +import static org.apache.hadoop.ozone.om.ha.OMFailoverProxyProviderBase.getReadException; +import static org.apache.hadoop.ozone.om.ha.OMFailoverProxyProviderBase.getReadIndexException; + +import io.grpc.StatusRuntimeException; +import java.io.IOException; +import org.apache.hadoop.ozone.om.helpers.ReadConsistency; +import org.apache.hadoop.ozone.om.protocolPB.GrpcOmTransport; +import org.apache.hadoop.ozone.om.protocolPB.OzoneManagerProtocolPB; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMResponse; +import org.apache.ratis.protocol.exceptions.ReadException; +import org.apache.ratis.protocol.exceptions.ReadIndexException; +import org.apache.ratis.util.function.CheckedBiFunction; +import org.apache.ratis.util.function.CheckedFunction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Follower read support for {@link GrpcOmTransport}. + * <p> + * If follower read is enabled, a read request is sent to the current follower read OM node, + * which can be either a leader or a follower. If the OM node fails, the request is sent + * to the next OM node. If all the OM nodes have failed, the request falls back to the leader OM. + * <p> + * Write requests, and read requests when follower read is disabled, are sent to the leader OM. + */ +public class GrpcOMFollowerReadFailoverProxyProvider extends FollowerReadFailoverProxyProviderBase { + private static final Logger LOG = LoggerFactory.getLogger(GrpcOMFollowerReadFailoverProxyProvider.class); + + /** The inner proxy provider used for leader-based failover. */ + private final GrpcOMFailoverProxyProvider<OzoneManagerProtocolPB> leaderProxy; + + public GrpcOMFollowerReadFailoverProxyProvider(GrpcOMFailoverProxyProvider<OzoneManagerProtocolPB> leaderProxy, + ReadConsistency followerReadConsistencyType, ReadConsistency leaderReadConsistencyType, + boolean defaultFollowerReadEnabled) { + super(followerReadConsistencyType, leaderReadConsistencyType, defaultFollowerReadEnabled); + this.leaderProxy = leaderProxy; + } + + @Override + protected GrpcOMFailoverProxyProvider<OzoneManagerProtocolPB> getLeaderProxy() { + return leaderProxy; + } + + /** + * Submit the request to an OM follower if follower read is used, otherwise to the leader OM. + * + * @param payload the request to submit. + * @param submitToHost submits a request to the OM at the given gRPC address. + * @param submitToLeader submits a request to the leader OM, failing over between the OMs if needed. + * @return the response of the request. + */ + public OMResponse submitRequest(OMRequest payload, + CheckedBiFunction<OMRequest, String, OMResponse, IOException> submitToHost, + CheckedFunction<OMRequest, OMResponse, IOException> submitToLeader) throws IOException { + if (shouldUseFollowerRead(payload)) { + OMRequest followerPayload = addReadConsistencyHint(payload, true); + ReadConsistency readConsistency = getReadConsistency(payload); + int failedCount = 0; + for (int i = 0; i < getOMNodeCount(); i++) { + String nodeId = selectFollowerReadNodeId(readConsistency); + if (nodeId == null) { + break; + } + String followerHost = leaderProxy.getGrpcProxyAddress(nodeId); + try { + OMResponse response = submitToHost.apply(followerPayload, followerHost); + LOG.debug("Invocation with cmdType {} using follower read host {} was successful", + followerPayload.getCmdType(), followerHost); + return response; + } catch (StatusRuntimeException e) { + LOG.debug("Invocation with cmdType {} using follower read host {} failed", + followerPayload.getCmdType(), followerHost, e); + Exception unwrapped = GrpcOmTransport.unwrapException(new Exception(e)); + if (getNotLeaderException(unwrapped) != null) { + LOG.debug("Encountered OMNotLeaderException from {}. " + + "Disable OM follower read and retry OM leader directly.", followerHost); + disableFollowerRead(); + break; + } + if (getLeaderNotReadyException(unwrapped) != null) { Review Comment: Just curious, should this break to the leader on `OMLeaderNotReadyException` while [Hadoop RPC rethrows](https://github.com/apache/ozone/blob/e7c1c3882c162c8d51cc56cff8f3609781f8a68f/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/ha/HadoopRpcOMFollowerReadFailoverProxyProvider.java#L255), or could we unify them in a follow-up? ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/ha/FollowerReadFailoverProxyProviderBase.java: ########## @@ -0,0 +1,168 @@ +/* + * 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.ha; + +import jakarta.annotation.Nonnull; +import org.apache.hadoop.ozone.OmUtils; +import org.apache.hadoop.ozone.om.helpers.ReadConsistency; +import org.apache.hadoop.ozone.om.protocolPB.OzoneManagerProtocolPB; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.ReadConsistencyHint; +import org.apache.ratis.util.Preconditions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Base class for the OM follower read failover proxy providers. + * <p> + * It keeps the follower read configuration and selects the OM node to send follower reads to. + * Leader-based failover is delegated to the underlying {@link OMFailoverProxyProviderBase}. + */ +public abstract class FollowerReadFailoverProxyProviderBase { + private static final Logger LOG = LoggerFactory.getLogger(FollowerReadFailoverProxyProviderBase.class); + + /** Whether follower reads are supported by the OM service. */ + private volatile boolean omServiceSupportsFollowerRead; + + /** Whether eligible reads without an explicit consistency hint use followers. */ + private final boolean defaultFollowerReadEnabled; + + /** The read consistency hint used when follower read is enabled. */ + private final ReadConsistencyHint followerReadConsistency; + /** The read consistency hint used when follower read is disabled or when follower read fails. */ + private final ReadConsistencyHint leaderReadConsistency; + + /** + * The index of the OM node used for follower read, in the order of the leader proxy's OM nodes. + * Should only be accessed in synchronized methods. + */ + private int currentIndex = 0; + + protected FollowerReadFailoverProxyProviderBase(ReadConsistency followerReadConsistencyType, + ReadConsistency leaderReadConsistencyType, boolean defaultFollowerReadEnabled) { + Preconditions.assertTrue(followerReadConsistencyType.allowFollowerRead(), + "Invalid follower read consistency " + followerReadConsistencyType); + Preconditions.assertTrue(!leaderReadConsistencyType.allowFollowerRead(), + "Invalid leader read consistency " + leaderReadConsistencyType); + // At the start, we don't know whether OM service supports follower read. Therefore, if the client + // is configured to use follower read, we should assume that OM service supports follower read and + // only sets this to false if there is an evidence otherwise (i.e. OM throws OMNotLeaderException). + this.omServiceSupportsFollowerRead = true; + this.defaultFollowerReadEnabled = defaultFollowerReadEnabled; + this.followerReadConsistency = followerReadConsistencyType.getHint(); + this.leaderReadConsistency = leaderReadConsistencyType.getHint(); + } + + /** @return the inner proxy provider used for leader-based failover. */ + protected abstract OMFailoverProxyProviderBase<OzoneManagerProtocolPB> getLeaderProxy(); + + public boolean isOmServiceSupportsFollowerRead() { Review Comment: nit: Should we keep `@VisibleForTesting` here, as in the original Hadoop RPC provider? -- 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]
