devmadhuu commented on code in PR #10549:
URL: https://github.com/apache/ozone/pull/10549#discussion_r3528281318
##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/persistence/ContainerHealthSchemaManager.java:
##########
@@ -227,6 +291,43 @@ private int deleteScmStatesForContainers(DSLContext
dslContext,
return totalDeleted;
}
+ private void deleteStaleUnhealthyRecords(DSLContext dslContext,
Review Comment:
This impl also not doing any batching.
##########
hadoop-ozone/recon/src/test/java/org/apache/hadoop/ozone/recon/persistence/TestContainerHealthSchemaManager.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.recon.persistence;
+
+import static
org.apache.ozone.recon.schema.generated.tables.UnhealthyContainersTable.UNHEALTHY_CONTAINERS;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import
org.apache.hadoop.ozone.recon.persistence.ContainerHealthSchemaManager.ContainerStateKey;
+import
org.apache.hadoop.ozone.recon.persistence.ContainerHealthSchemaManager.UnhealthyContainerRecord;
+import org.apache.ozone.recon.schema.ContainerSchemaDefinition;
+import
org.apache.ozone.recon.schema.ContainerSchemaDefinition.UnHealthyContainerStates;
+import org.jooq.DSLContext;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Functional tests for {@link
ContainerHealthSchemaManager#syncUnhealthyContainerRecordsAtomically}.
+ */
+public class TestContainerHealthSchemaManager extends AbstractReconSqlDBTest {
+
+ private static final long ORIGINAL_TIMESTAMP = 1_000L;
+ private static final long UPDATED_TIMESTAMP = 2_000L;
+
+ private ContainerSchemaDefinition schemaDefinition;
+ private ContainerHealthSchemaManager schemaManager;
+
+ @BeforeEach
+ public void setUpSchemaManager() {
+ schemaDefinition = getSchemaDefinition(ContainerSchemaDefinition.class);
+ schemaManager =
+ new ContainerHealthSchemaManager(schemaDefinition, new
OzoneConfiguration());
+ }
+
+ @Test
+ public void testSyncInsertsNewUnhealthyRecords() {
+ Map<ContainerStateKey, Long> existing = Collections.emptyMap();
+ List<UnhealthyContainerRecord> desired = Arrays.asList(
+ record(1L, UnHealthyContainerStates.MISSING, ORIGINAL_TIMESTAMP, 3, 0),
+ record(2L, UnHealthyContainerStates.UNDER_REPLICATED,
ORIGINAL_TIMESTAMP, 3, 2));
+
+ schemaManager.syncUnhealthyContainerRecordsAtomically(existing, desired);
+
+ assertEquals(Arrays.asList(1L, 2L), remainingContainerIds());
+ assertEquals(2L, countRows());
+ }
+
+ @Test
+ public void testSyncDeletesStaleRowsWhenContainerBecomesHealthy() {
+ insert(record(1L, UnHealthyContainerStates.MISSING, ORIGINAL_TIMESTAMP, 3,
0));
+ insert(record(1L, UnHealthyContainerStates.UNDER_REPLICATED,
ORIGINAL_TIMESTAMP, 3, 2));
+ insert(record(2L, UnHealthyContainerStates.MISSING, ORIGINAL_TIMESTAMP, 3,
0));
+
+ Map<ContainerStateKey, Long> existing = existingMap(
+ key(1L, UnHealthyContainerStates.MISSING),
+ key(1L, UnHealthyContainerStates.UNDER_REPLICATED),
+ key(2L, UnHealthyContainerStates.MISSING));
+
+ // Container 1 recovered; container 2 still missing.
+ List<UnhealthyContainerRecord> desired = Collections.singletonList(
+ record(2L, UnHealthyContainerStates.MISSING, ORIGINAL_TIMESTAMP, 3,
0));
+
+ schemaManager.syncUnhealthyContainerRecordsAtomically(existing, desired);
+
+ assertEquals(Collections.singletonList(2L), remainingContainerIds());
+ assertEquals(1L, countRows());
+ }
+
+ @Test
+ public void testSyncUpdatesExistingRowAndDeletesChangedState() {
+ insert(record(1L, UnHealthyContainerStates.MISSING, ORIGINAL_TIMESTAMP, 3,
0));
+
+ Map<ContainerStateKey, Long> existing = existingMap(
+ key(1L, UnHealthyContainerStates.MISSING));
+
+ // Same container now under-replicated instead of missing.
+ List<UnhealthyContainerRecord> desired = Collections.singletonList(
+ record(1L, UnHealthyContainerStates.UNDER_REPLICATED,
UPDATED_TIMESTAMP, 3, 2));
+
+ schemaManager.syncUnhealthyContainerRecordsAtomically(existing, desired);
+
+ assertEquals(Collections.singletonList(1L), remainingContainerIds());
+ assertEquals(1L, countRows());
+ UnhealthyContainerRecord row = fetchRow(1L,
UnHealthyContainerStates.UNDER_REPLICATED);
+ assertEquals(UPDATED_TIMESTAMP, row.getInStateSince());
+ assertEquals(2, row.getActualReplicaCount());
+ assertTrue(countByState(UnHealthyContainerStates.MISSING) == 0);
+ }
+
+ @Test
+ public void testSyncUpdatesExistingRowInPlace() {
+ insert(record(1L, UnHealthyContainerStates.UNDER_REPLICATED,
+ ORIGINAL_TIMESTAMP, 3, 2, "old reason"));
+
+ Map<ContainerStateKey, Long> existing = existingMap(
+ key(1L, UnHealthyContainerStates.UNDER_REPLICATED));
+
+ List<UnhealthyContainerRecord> desired = Collections.singletonList(
+ record(1L, UnHealthyContainerStates.UNDER_REPLICATED,
+ ORIGINAL_TIMESTAMP, 3, 1, "new reason"));
+
+ schemaManager.syncUnhealthyContainerRecordsAtomically(existing, desired);
+
+ assertEquals(1L, countRows());
+ UnhealthyContainerRecord row = fetchRow(1L,
UnHealthyContainerStates.UNDER_REPLICATED);
+ assertEquals(ORIGINAL_TIMESTAMP, row.getInStateSince());
+ assertEquals(1, row.getActualReplicaCount());
+ assertEquals("new reason", row.getReason());
+ }
+
+ @Test
+ public void testBatchDeleteStillRemovesAllScmStatesForContainer() {
Review Comment:
This test calls `batchDeleteSCMStatesForContainers` — the old delete method.
It does not test the new `syncUnhealthyContainerRecordsAtomically` method.
A short test like this would be helpful:
1. Insert an `ALL_REPLICAS_BAD` row for container 1 (a non-SCM state).
2. Also insert, say, a `MISSING` row for container 1 (an SCM state) and put
it in the "existing" map.
3. Run `syncUnhealthyContainerRecordsAtomically` with a "desired" result
where container 1 is no longer `MISSING` (so the SCM row should be deleted).
4. Assert that the `MISSING` row is gone but the `ALL_REPLICAS_BAD` row is
still there.
That directly proves, against the real production method, "sync cleans up
SCM states but never harms non-SCM states."
##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/persistence/ContainerHealthSchemaManager.java:
##########
@@ -179,6 +179,70 @@ public void batchDeleteSCMStatesForContainers(List<Long>
containerIds) {
totalDeleted, containerIds.size());
}
+ /**
+ * Atomically syncs unhealthy rows for a scan chunk: insert new
+ * (container_id, state) pairs, update existing pairs, and delete only stale
+ * pairs that are no longer unhealthy.
+ *
+ * <p>Unlike {@link #replaceUnhealthyContainerRecordsAtomically}, this does
not
+ * delete all SCM states for every container that previously had a row. It
+ * removes only keys present in {@code existingByKey} but absent from the
+ * desired scan result (containers that recovered or changed state).</p>
+ *
+ * @param existingByKey prior rows for this chunk, keyed by
+ * (container_id, container_state)
+ * @param desiredRecords unhealthy rows produced by the current scan
+ */
+ public void syncUnhealthyContainerRecordsAtomically(
+ Map<ContainerStateKey, Long> existingByKey,
+ List<UnhealthyContainerRecord> desiredRecords) {
+ if ((existingByKey == null || existingByKey.isEmpty())
+ && (desiredRecords == null || desiredRecords.isEmpty())) {
+ return;
+ }
+
+ Map<ContainerStateKey, UnhealthyContainerRecord> desiredByKey = new
HashMap<>();
+ if (desiredRecords != null) {
+ for (UnhealthyContainerRecord record : desiredRecords) {
+ desiredByKey.put(new ContainerStateKey(record.getContainerId(),
+ record.getContainerState()), record);
+ }
+ }
+
+ Map<ContainerStateKey, Long> existing =
+ existingByKey == null ? new HashMap<>() : existingByKey;
+
+ List<ContainerStateKey> staleKeys = new ArrayList<>();
+ List<UnhealthyContainerRecord> toInsert = new ArrayList<>();
+ List<UnhealthyContainerRecord> toUpdate = new ArrayList<>();
+
+ for (ContainerStateKey key : existing.keySet()) {
+ if (!desiredByKey.containsKey(key)) {
+ staleKeys.add(key);
+ }
+ }
+ for (UnhealthyContainerRecord record : desiredByKey.values()) {
+ ContainerStateKey key = new ContainerStateKey(record.getContainerId(),
+ record.getContainerState());
+ if (existing.containsKey(key)) {
+ toUpdate.add(record);
+ } else {
+ toInsert.add(record);
+ }
+ }
+
+ DSLContext dslContext = containerSchemaDefinition.getDSLContext();
+ dslContext.transaction(configuration -> {
+ DSLContext txContext = configuration.dsl();
+ deleteStaleUnhealthyRecords(txContext, staleKeys);
+ batchInsertInChunks(txContext, toInsert);
+ batchUpdateInChunks(txContext, toUpdate);
Review Comment:
I think , here this is not determining real rows to be updated. Only by
checking if row already exists doesn't necessarily required to be updated.
Better to identify row to be updated if row content really changed.
##########
hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/persistence/ContainerHealthSchemaManager.java:
##########
@@ -227,6 +291,43 @@ private int deleteScmStatesForContainers(DSLContext
dslContext,
return totalDeleted;
}
+ private void deleteStaleUnhealthyRecords(DSLContext dslContext,
+ List<ContainerStateKey> staleKeys) {
+ if (staleKeys.isEmpty()) {
+ return;
+ }
+ for (ContainerStateKey key : staleKeys) {
+ dslContext.deleteFrom(UNHEALTHY_CONTAINERS)
+ .where(UNHEALTHY_CONTAINERS.CONTAINER_ID.eq(key.getContainerId()))
+
.and(UNHEALTHY_CONTAINERS.CONTAINER_STATE.eq(key.getContainerState()))
+ .execute();
+ }
+ }
+
+ private void batchUpdateInChunks(DSLContext dslContext,
+ List<UnhealthyContainerRecord> recs) {
+ if (recs.isEmpty()) {
+ return;
+ }
+ for (int from = 0; from < recs.size(); from += BATCH_INSERT_CHUNK_SIZE) {
Review Comment:
This impl doesn't seem to doing batch update.
--
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]