Skip to content

Commit ad108c8

Browse files
authored
HDDS-11997. Duplicate snapshot purge request causes NPE (#7627)
1 parent c3003fd commit ad108c8

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/snapshot/OMSnapshotPurgeRequest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ private SnapshotInfo getUpdatedSnapshotInfo(String snapshotTableKey, OMMetadataM
227227

228228
if (snapshotInfo == null) {
229229
snapshotInfo = omMetadataManager.getSnapshotInfoTable().get(snapshotTableKey);
230-
updatedSnapshotInfos.put(snapshotTableKey, snapshotInfo);
230+
if (snapshotInfo != null) {
231+
updatedSnapshotInfos.put(snapshotTableKey, snapshotInfo);
232+
}
231233
}
232234
return snapshotInfo;
233235
}

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/response/snapshot/OMSnapshotPurgeResponse.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.hadoop.ozone.om.response.snapshot;
2020

21+
import com.google.common.annotations.VisibleForTesting;
2122
import org.apache.commons.io.FileUtils;
2223
import org.apache.hadoop.hdds.utils.db.BatchOperation;
2324
import org.apache.hadoop.ozone.om.OMMetadataManager;
@@ -138,4 +139,9 @@ private void deleteCheckpointDirectory(OMMetadataManager omMetadataManager,
138139
}
139140
}
140141
}
142+
143+
@VisibleForTesting
144+
public Map<String, SnapshotInfo> getUpdatedSnapInfos() {
145+
return updatedSnapInfos;
146+
}
141147
}

hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/snapshot/TestOMSnapshotPurgeRequestAndResponse.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import static org.junit.jupiter.api.Assertions.assertEquals;
5757
import static org.junit.jupiter.api.Assertions.assertFalse;
5858
import static org.junit.jupiter.api.Assertions.assertNotEquals;
59+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5960
import static org.junit.jupiter.api.Assertions.assertNull;
6061
import static org.junit.jupiter.api.Assertions.assertTrue;
6162
import static org.mockito.Mockito.anyString;
@@ -186,6 +187,42 @@ public void testValidateAndUpdateCache() throws Exception {
186187
assertEquals(initialSnapshotPurgeFailCount, getOmMetrics().getNumSnapshotPurgeFails());
187188
}
188189

190+
@Test
191+
public void testDuplicateSnapshotPurge() throws Exception {
192+
List<String> snapshotDbKeysToPurge = createSnapshots(1);
193+
assertFalse(getOmMetadataManager().getSnapshotInfoTable().isEmpty());
194+
OMRequest snapshotPurgeRequest = createPurgeKeysRequest(
195+
snapshotDbKeysToPurge);
196+
197+
OMSnapshotPurgeRequest omSnapshotPurgeRequest = preExecute(snapshotPurgeRequest);
198+
199+
OMSnapshotPurgeResponse omSnapshotPurgeResponse = (OMSnapshotPurgeResponse)
200+
omSnapshotPurgeRequest.validateAndUpdateCache(getOzoneManager(), 200L);
201+
202+
try (BatchOperation batchOperation = getOmMetadataManager().getStore().initBatchOperation()) {
203+
omSnapshotPurgeResponse.checkAndUpdateDB(getOmMetadataManager(), batchOperation);
204+
getOmMetadataManager().getStore().commitBatchOperation(batchOperation);
205+
}
206+
207+
// Check if the entries are deleted.
208+
assertTrue(getOmMetadataManager().getSnapshotInfoTable().isEmpty());
209+
210+
OMSnapshotPurgeResponse omSnapshotPurgeResponse1 = (OMSnapshotPurgeResponse)
211+
omSnapshotPurgeRequest.validateAndUpdateCache(getOzoneManager(), 201L);
212+
213+
for (Map.Entry<String, SnapshotInfo> purgedSnapshot : omSnapshotPurgeResponse1.getUpdatedSnapInfos().entrySet()) {
214+
assertNotNull(purgedSnapshot.getValue());
215+
}
216+
for (String snapshotTableKey: snapshotDbKeysToPurge) {
217+
assertNull(getOmMetadataManager().getSnapshotInfoTable().get(snapshotTableKey));
218+
}
219+
220+
try (BatchOperation batchOperation = getOmMetadataManager().getStore().initBatchOperation()) {
221+
omSnapshotPurgeResponse1.checkAndUpdateDB(getOmMetadataManager(), batchOperation);
222+
getOmMetadataManager().getStore().commitBatchOperation(batchOperation);
223+
}
224+
}
225+
189226
/**
190227
* This test is mainly to validate metrics and error code.
191228
*/

0 commit comments

Comments
 (0)