Skip to content

Commit acbb439

Browse files
authored
HDDS-13338. Move check for sufficient space in pipeline to allocateContainer (apache#9568)
1 parent eb17806 commit acbb439

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/ContainerManagerImpl.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ private ContainerInfo createContainer(Pipeline pipeline, String owner)
237237
private ContainerInfo allocateContainer(final Pipeline pipeline,
238238
final String owner)
239239
throws IOException {
240+
if (!pipelineManager.hasEnoughSpace(pipeline, maxContainerSize)) {
241+
LOG.debug("Cannot allocate a new container because pipeline {} does not have the required space {}.",
242+
pipeline, maxContainerSize);
243+
return null;
244+
}
245+
240246
final long uniqueId = sequenceIdGen.getNextId(CONTAINER_ID);
241247
Preconditions.checkState(uniqueId > 0,
242248
"Cannot allocate container, negative container id" +
@@ -350,24 +356,17 @@ public ContainerInfo getMatchingContainer(final long size, final String owner,
350356
synchronized (pipeline.getId()) {
351357
containerIDs = getContainersForOwner(pipeline, owner);
352358
if (containerIDs.size() < pipelineManager.openContainerLimit(pipeline.getNodes())) {
353-
if (pipelineManager.hasEnoughSpace(pipeline, maxContainerSize)) {
354-
allocateContainer(pipeline, owner);
359+
ContainerInfo allocated = allocateContainer(pipeline, owner);
360+
if (allocated != null) {
361+
// New container was created, refresh IDs so it becomes eligible.
355362
containerIDs = getContainersForOwner(pipeline, owner);
356-
} else {
357-
LOG.debug("Cannot allocate a new container because pipeline {} does not have the required space {}.",
358-
pipeline, maxContainerSize);
359363
}
360364
}
361365
containerIDs.removeAll(excludedContainerIDs);
362366
containerInfo = containerStateManager.getMatchingContainer(
363367
size, owner, pipeline.getId(), containerIDs);
364368
if (containerInfo == null) {
365-
if (pipelineManager.hasEnoughSpace(pipeline, maxContainerSize)) {
366-
containerInfo = allocateContainer(pipeline, owner);
367-
} else {
368-
LOG.debug("Cannot allocate a new container because pipeline {} does not have the required space {}.",
369-
pipeline, maxContainerSize);
370-
}
369+
containerInfo = allocateContainer(pipeline, owner);
371370
}
372371
return containerInfo;
373372
}

hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/container/TestContainerManagerImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,15 @@ void setUp() throws Exception {
9999
NodeManager nodeManager = new MockNodeManager(true, 10);
100100
sequenceIdGen = new SequenceIdGenerator(
101101
conf, scmhaManager, SCMDBDefinition.SEQUENCE_ID.getTable(dbStore));
102-
pipelineManager = new MockPipelineManager(dbStore, scmhaManager, nodeManager);
102+
PipelineManager base = new MockPipelineManager(dbStore, scmhaManager, nodeManager);
103+
pipelineManager = spy(base);
104+
105+
// Default: allow allocation in tests unless a test overrides it.
106+
doReturn(true).when(pipelineManager).hasEnoughSpace(any(Pipeline.class), anyLong());
107+
103108
pipelineManager.createPipeline(RatisReplicationConfig.getInstance(
104109
ReplicationFactor.THREE));
110+
105111
pendingOpsMock = mock(ContainerReplicaPendingOps.class);
106112
containerManager = new ContainerManagerImpl(conf,
107113
scmhaManager, sequenceIdGen, pipelineManager,
@@ -122,6 +128,8 @@ void testAllocateContainer() throws Exception {
122128
final ContainerInfo container = containerManager.allocateContainer(
123129
RatisReplicationConfig.getInstance(
124130
ReplicationFactor.THREE), "admin");
131+
132+
assertNotNull(container);
125133
assertEquals(1, containerManager.getContainers().size());
126134
assertNotNull(containerManager.getContainer(
127135
container.containerID()));
@@ -133,6 +141,8 @@ void testAllocateContainer() throws Exception {
133141
*/
134142
@Test
135143
public void testGetMatchingContainerReturnsNullWhenNotEnoughSpaceInDatanodes() throws IOException {
144+
doReturn(false).when(pipelineManager).hasEnoughSpace(any(), anyLong());
145+
136146
long sizeRequired = 256 * 1024 * 1024; // 256 MB
137147
Pipeline pipeline = pipelineManager.getPipelines().iterator().next();
138148
// MockPipelineManager#hasEnoughSpace always returns false

hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/node/TestContainerPlacement.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
import static org.apache.hadoop.hdds.scm.net.NetConstants.ROOT_SCHEMA;
2525
import static org.apache.hadoop.hdds.upgrade.HDDSLayoutVersionManager.maxLayoutVersion;
2626
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.mockito.ArgumentMatchers.any;
29+
import static org.mockito.ArgumentMatchers.anyLong;
30+
import static org.mockito.Mockito.doReturn;
2731
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.spy;
2833
import static org.mockito.Mockito.when;
2934

3035
import java.io.File;
@@ -154,6 +159,9 @@ SCMNodeManager createNodeManager(OzoneConfiguration config) {
154159

155160
ContainerManager createContainerManager()
156161
throws IOException {
162+
pipelineManager = spy(pipelineManager);
163+
doReturn(true).when(pipelineManager).hasEnoughSpace(any(), anyLong());
164+
157165
return new ContainerManagerImpl(conf,
158166
scmhaManager, sequenceIdGen, pipelineManager,
159167
SCMDBDefinition.CONTAINERS.getTable(dbStore),
@@ -224,6 +232,8 @@ public void testContainerPlacementCapacity() throws IOException,
224232
SCMTestUtils.getReplicationType(conf),
225233
SCMTestUtils.getReplicationFactor(conf)),
226234
OzoneConsts.OZONE);
235+
assertNotNull(container, "allocateContainer returned null (unexpected in this placement test)");
236+
227237
int replicaCount = 0;
228238
for (DatanodeDetails datanodeDetails : datanodes) {
229239
if (replicaCount ==

0 commit comments

Comments
 (0)