|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hadoop.hdds.scm.container.replication; |
| 19 | + |
| 20 | +import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor.THREE; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | +import static org.mockito.ArgumentMatchers.any; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Set; |
| 33 | +import java.util.UUID; |
| 34 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 35 | +import org.apache.commons.lang3.tuple.Pair; |
| 36 | +import org.apache.hadoop.hdds.client.RatisReplicationConfig; |
| 37 | +import org.apache.hadoop.hdds.conf.OzoneConfiguration; |
| 38 | +import org.apache.hadoop.hdds.protocol.DatanodeDetails; |
| 39 | +import org.apache.hadoop.hdds.protocol.MockDatanodeDetails; |
| 40 | +import org.apache.hadoop.hdds.protocol.proto.HddsProtos; |
| 41 | +import org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos; |
| 42 | +import org.apache.hadoop.hdds.scm.container.ContainerInfo; |
| 43 | +import org.apache.hadoop.hdds.scm.container.ContainerReplica; |
| 44 | +import org.apache.hadoop.hdds.scm.node.NodeStatus; |
| 45 | +import org.apache.hadoop.hdds.scm.node.states.NodeNotFoundException; |
| 46 | +import org.apache.hadoop.ozone.protocol.commands.SCMCommand; |
| 47 | +import org.apache.ratis.protocol.exceptions.NotLeaderException; |
| 48 | +import org.junit.jupiter.api.BeforeEach; |
| 49 | +import org.junit.jupiter.api.Test; |
| 50 | + |
| 51 | +/** |
| 52 | + * Test for QuasiClosedStuckOverReplicationHandler. |
| 53 | + */ |
| 54 | +public class TestQuasiClosedStuckOverReplicationHandler { |
| 55 | + |
| 56 | + private static final RatisReplicationConfig RATIS_REPLICATION_CONFIG = RatisReplicationConfig.getInstance(THREE); |
| 57 | + private ContainerInfo container; |
| 58 | + private ReplicationManager replicationManager; |
| 59 | + private ReplicationManagerMetrics metrics; |
| 60 | + private Set<Pair<DatanodeDetails, SCMCommand<?>>> commandsSent; |
| 61 | + private QuasiClosedStuckOverReplicationHandler handler; |
| 62 | + private UUID origin1 = UUID.randomUUID(); |
| 63 | + private UUID origin2 = UUID.randomUUID(); |
| 64 | + |
| 65 | + @BeforeEach |
| 66 | + void setup() throws NodeNotFoundException, |
| 67 | + CommandTargetOverloadedException, NotLeaderException { |
| 68 | + container = ReplicationTestUtil.createContainer( |
| 69 | + HddsProtos.LifeCycleState.QUASI_CLOSED, RATIS_REPLICATION_CONFIG); |
| 70 | + |
| 71 | + replicationManager = mock(ReplicationManager.class); |
| 72 | + OzoneConfiguration ozoneConfiguration = new OzoneConfiguration(); |
| 73 | + ozoneConfiguration.setBoolean("hdds.scm.replication.push", true); |
| 74 | + when(replicationManager.getConfig()) |
| 75 | + .thenReturn(ozoneConfiguration.getObject( |
| 76 | + ReplicationManager.ReplicationManagerConfiguration.class)); |
| 77 | + metrics = ReplicationManagerMetrics.create(replicationManager); |
| 78 | + when(replicationManager.getMetrics()).thenReturn(metrics); |
| 79 | + |
| 80 | + /* |
| 81 | + Return NodeStatus with NodeOperationalState as specified in |
| 82 | + DatanodeDetails, and NodeState as HEALTHY. |
| 83 | + */ |
| 84 | + when( |
| 85 | + replicationManager.getNodeStatus(any(DatanodeDetails.class))) |
| 86 | + .thenAnswer(invocationOnMock -> { |
| 87 | + DatanodeDetails dn = invocationOnMock.getArgument(0); |
| 88 | + return new NodeStatus(dn.getPersistedOpState(), |
| 89 | + HddsProtos.NodeState.HEALTHY); |
| 90 | + }); |
| 91 | + |
| 92 | + commandsSent = new HashSet<>(); |
| 93 | + ReplicationTestUtil.mockRMSendThrottledDeleteCommand( |
| 94 | + replicationManager, commandsSent); |
| 95 | + handler = new QuasiClosedStuckOverReplicationHandler(replicationManager); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testReturnsZeroIfNotOverReplicated() throws IOException { |
| 100 | + Set<ContainerReplica> replicas = ReplicationTestUtil.createReplicasWithOriginAndOpState(container.containerID(), |
| 101 | + StorageContainerDatanodeProtocolProtos.ContainerReplicaProto.State.QUASI_CLOSED, |
| 102 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 103 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 104 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 105 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE)); |
| 106 | + |
| 107 | + int count = handler.processAndSendCommands(replicas, Collections.emptyList(), getOverReplicatedHealthResult(), 1); |
| 108 | + assertEquals(0, count); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testNoCommandsScheduledIfPendingOps() throws IOException { |
| 113 | + Set<ContainerReplica> replicas = ReplicationTestUtil.createReplicasWithOriginAndOpState(container.containerID(), |
| 114 | + StorageContainerDatanodeProtocolProtos.ContainerReplicaProto.State.QUASI_CLOSED, |
| 115 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 116 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 117 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 118 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 119 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 120 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE)); |
| 121 | + List<ContainerReplicaOp> pendingOps = new ArrayList<>(); |
| 122 | + pendingOps.add(ContainerReplicaOp.create( |
| 123 | + ContainerReplicaOp.PendingOpType.DELETE, MockDatanodeDetails.randomDatanodeDetails(), 0)); |
| 124 | + |
| 125 | + int count = handler.processAndSendCommands(replicas, pendingOps, getOverReplicatedHealthResult(), 1); |
| 126 | + assertEquals(0, count); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testCommandScheduledForOverReplicatedContainer() throws IOException { |
| 131 | + Set<ContainerReplica> replicas = ReplicationTestUtil.createReplicasWithOriginAndOpState(container.containerID(), |
| 132 | + StorageContainerDatanodeProtocolProtos.ContainerReplicaProto.State.QUASI_CLOSED, |
| 133 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 134 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 135 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 136 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 137 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE)); |
| 138 | + |
| 139 | + int count = handler.processAndSendCommands(replicas, Collections.emptyList(), getOverReplicatedHealthResult(), 1); |
| 140 | + assertEquals(1, count); |
| 141 | + SCMCommand<?> command = commandsSent.iterator().next().getRight(); |
| 142 | + assertEquals(StorageContainerDatanodeProtocolProtos.SCMCommandProto.Type.deleteContainerCommand, command.getType()); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testOverloadedExceptionContinuesAndThrows() throws NotLeaderException, CommandTargetOverloadedException { |
| 147 | + Set<ContainerReplica> replicas = ReplicationTestUtil.createReplicasWithOriginAndOpState(container.containerID(), |
| 148 | + StorageContainerDatanodeProtocolProtos.ContainerReplicaProto.State.QUASI_CLOSED, |
| 149 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 150 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 151 | + Pair.of(origin1, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 152 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 153 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE), |
| 154 | + Pair.of(origin2, HddsProtos.NodeOperationalState.IN_SERVICE)); |
| 155 | + |
| 156 | + ReplicationTestUtil.mockRMSendThrottledDeleteCommand(replicationManager, commandsSent, new AtomicBoolean(true)); |
| 157 | + |
| 158 | + assertThrows(CommandTargetOverloadedException.class, () -> |
| 159 | + handler.processAndSendCommands(replicas, Collections.emptyList(), getOverReplicatedHealthResult(), 1)); |
| 160 | + assertEquals(1, commandsSent.size()); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + private ContainerHealthResult.OverReplicatedHealthResult getOverReplicatedHealthResult() { |
| 165 | + ContainerHealthResult.OverReplicatedHealthResult |
| 166 | + healthResult = mock(ContainerHealthResult.OverReplicatedHealthResult.class); |
| 167 | + when(healthResult.getContainerInfo()).thenReturn(container); |
| 168 | + return healthResult; |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments