diff --git a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/snapshot/SnapshotDiffReportOzone.java b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/snapshot/SnapshotDiffReportOzone.java index aec0c6d12ccd..3d14e266daa4 100644 --- a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/snapshot/SnapshotDiffReportOzone.java +++ b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/snapshot/SnapshotDiffReportOzone.java @@ -101,13 +101,16 @@ public String toString() { .append(" and snapshot: ") .append(getLaterSnapshotName()) .append(LINE_SEPARATOR); - for (DiffReportEntry entry : getDiffList()) { - str.append(entry.toString()).append(LINE_SEPARATOR); - } - if (StringUtils.isNotEmpty(token)) { - str.append("Next token: ") - .append(token) - .append(LINE_SEPARATOR); + if (!getDiffList().isEmpty()) { + for (DiffReportEntry entry : getDiffList()) { + str.append(entry.toString()).append(LINE_SEPARATOR); + } + if (StringUtils.isNotEmpty(token)) { + str.append("Next token: ") + .append(token); + } + } else { + str.append("No diff or no more diff for the request parameters."); } return str.toString(); } diff --git a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshot.java b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshot.java index ff444a88cebe..7e9fe787df67 100644 --- a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshot.java +++ b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshot.java @@ -1429,6 +1429,14 @@ public void testSnapDiff() throws Exception { SnapshotDiffReport.DiffType.MODIFY, key3) ); assertEquals(expectedDiffList, diff5.getDiffList()); + + IOException ioException = assertThrows(IOException.class, + () -> store.snapshotDiff(volume, bucket, snap6, + snap7, "3", 0, forceFullSnapshotDiff, disableNativeDiff)); + assertThat(ioException.getMessage(), containsString("Index (given: 3) " + + "should be a number >= 0 and < totalDiffEntries: 2. Page size " + + "(given: 1000) should be a positive number > 0.")); + } @Test diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotDiffManager.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotDiffManager.java index 651ed06cbe1c..c34594cffe88 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotDiffManager.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotDiffManager.java @@ -569,11 +569,12 @@ SnapshotDiffReportOzone createPageResponse( final int index, final int pageSize ) throws IOException { - if (index < 0 || pageSize <= 0) { - throw new IllegalArgumentException(String.format( - "Index should be a number >= 0. Given index %d. Page size " + - "should be a positive number > 0. Given page size is %d", - index, pageSize)); + if (index < 0 || index > snapDiffJob.getTotalDiffEntries() + || pageSize <= 0) { + throw new IOException(String.format( + "Index (given: %d) should be a number >= 0 and < totalDiffEntries: " + + "%d. Page size (given: %d) should be a positive number > 0.", + index, snapDiffJob.getTotalDiffEntries(), pageSize)); } OFSPath path = getSnapshotRootPath(volumeName, bucketName); diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java index 8d8921ddff81..41b6f73d8cb0 100644 --- a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java +++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotDiffManager.java @@ -893,7 +893,7 @@ private DiffReportEntry getTestDiffEntry(String jobId, * objectId Map of diff keys to be checked with their corresponding key names. */ @ParameterizedTest - @CsvSource({"0,10,1000", "1,10,8", "1000,1000,10", "-1,1000,10000", + @CsvSource({"0,10,1000", "1,10,8", "10,1000,10", "-1,1000,10000", "1,0,1000", "1,-1,1000"}) public void testCreatePageResponse(int startIdx, int pageSize, @@ -933,7 +933,7 @@ public void testCreatePageResponse(int startIdx, codecRegistry.asRawData(snapshotDiffJob2)); if (pageSize <= 0 || startIdx < 0) { - Assertions.assertThrows(IllegalArgumentException.class, + Assertions.assertThrows(IOException.class, () -> snapshotDiffManager.createPageResponse(snapshotDiffJob, "vol", "buck", "fs", "ts", startIdx, pageSize)); return;