|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.master.procedure; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collections; |
| 28 | +import org.apache.hadoop.hbase.DoNotRetryIOException; |
| 29 | +import org.apache.hadoop.hbase.TableName; |
| 30 | +import org.apache.hadoop.hbase.client.RegionInfo; |
| 31 | +import org.apache.hadoop.hbase.client.RegionInfoBuilder; |
| 32 | +import org.apache.hadoop.hbase.master.assignment.RegionStateNode; |
| 33 | +import org.apache.hadoop.hbase.master.assignment.RegionStates; |
| 34 | +import org.apache.hadoop.hbase.testclassification.SmallTests; |
| 35 | +import org.apache.hadoop.hbase.util.ModifyRegionUtils; |
| 36 | +import org.junit.jupiter.api.Tag; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests for encoded region-name collision detection (HBASE-30160). If two regions end up with the |
| 41 | + * same encoded name, we should fail fast instead of allowing subtle metadata corruption later. |
| 42 | + */ |
| 43 | + |
| 44 | +@Tag(SmallTests.TAG) |
| 45 | +public class TestEncodedNameCollisionDetection { |
| 46 | + |
| 47 | + /** |
| 48 | + * Happy-path check: distinct candidate regions should pass without throwing. |
| 49 | + */ |
| 50 | + @Test |
| 51 | + public void testAcceptsDistinctCandidates() { |
| 52 | + TableName tableName = TableName.valueOf("test_table"); |
| 53 | + long regionId = System.currentTimeMillis(); |
| 54 | + |
| 55 | + RegionInfo ri1 = RegionInfoBuilder.newBuilder(tableName).setStartKey(new byte[] { 0, 0 }) |
| 56 | + .setEndKey(new byte[] { 1, 0 }).setSplit(false).setRegionId(regionId).build(); |
| 57 | + |
| 58 | + RegionInfo ri2 = RegionInfoBuilder.newBuilder(tableName).setStartKey(new byte[] { 1, 0 }) |
| 59 | + .setEndKey(new byte[] { 2, 0 }).setSplit(false).setRegionId(regionId + 1).build(); |
| 60 | + |
| 61 | + RegionStates regionStates = mock(RegionStates.class); |
| 62 | + |
| 63 | + assertDoesNotThrow( |
| 64 | + () -> ModifyRegionUtils.checkForEncodedNameCollisions(Arrays.asList(ri1, ri2), regionStates)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Verifies that duplicate encoded region names within candidate regions are rejected. |
| 69 | + */ |
| 70 | + @Test |
| 71 | + public void testDetectsDuplicatesInCandidates() { |
| 72 | + TableName tableName = TableName.valueOf("test_table"); |
| 73 | + RegionInfo ri1 = mockRegionInfo(tableName, "same-encoded-name"); |
| 74 | + RegionInfo ri2 = mockRegionInfo(tableName, "same-encoded-name"); |
| 75 | + |
| 76 | + RegionStates regionStates = mock(RegionStates.class); |
| 77 | + |
| 78 | + DoNotRetryIOException exception = assertThrows(DoNotRetryIOException.class, |
| 79 | + () -> ModifyRegionUtils.checkForEncodedNameCollisions(Arrays.asList(ri1, ri2), regionStates)); |
| 80 | + assertTrue(exception.getMessage().contains("Encoded region name collision detected")); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * A candidate region should be rejected if its encoded name already exists. |
| 85 | + */ |
| 86 | + @Test |
| 87 | + public void testDetectsCollisionWithExistingRegions() { |
| 88 | + TableName tableName = TableName.valueOf("test_table"); |
| 89 | + RegionInfo candidateRegion = mockRegionInfo(tableName, "same-encoded-name"); |
| 90 | + RegionStates regionStates = mock(RegionStates.class); |
| 91 | + when(regionStates.getRegionStateNodeFromEncodedRegionName("same-encoded-name")) |
| 92 | + .thenReturn(mock(RegionStateNode.class)); |
| 93 | + |
| 94 | + DoNotRetryIOException exception = |
| 95 | + assertThrows(DoNotRetryIOException.class, () -> ModifyRegionUtils |
| 96 | + .checkForEncodedNameCollisions(Arrays.asList(candidateRegion), regionStates)); |
| 97 | + assertTrue(exception.getMessage().contains("Encoded region name collision detected")); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test that checkForEncodedNameCollisions handles empty/null inputs and rejects null RegionStates |
| 102 | + * when candidates are present. |
| 103 | + */ |
| 104 | + @Test |
| 105 | + public void testInputValidationAndNullRegionStatesBehavior() { |
| 106 | + assertDoesNotThrow(() -> ModifyRegionUtils.checkForEncodedNameCollisions(null, null)); |
| 107 | + assertDoesNotThrow( |
| 108 | + () -> ModifyRegionUtils.checkForEncodedNameCollisions(Collections.emptyList(), null)); |
| 109 | + assertDoesNotThrow( |
| 110 | + () -> ModifyRegionUtils.checkForEncodedNameCollisions(null, mock(RegionStates.class))); |
| 111 | + assertDoesNotThrow(() -> ModifyRegionUtils |
| 112 | + .checkForEncodedNameCollisions(Collections.emptyList(), mock(RegionStates.class))); |
| 113 | + |
| 114 | + RegionInfo candidateRegion = mockRegionInfo(TableName.valueOf("test_table"), "candidate"); |
| 115 | + assertThrows(NullPointerException.class, |
| 116 | + () -> ModifyRegionUtils.checkForEncodedNameCollisions(Arrays.asList(candidateRegion), null)); |
| 117 | + } |
| 118 | + |
| 119 | + private RegionInfo mockRegionInfo(TableName tableName, String encodedName) { |
| 120 | + RegionInfo regionInfo = mock(RegionInfo.class); |
| 121 | + when(regionInfo.getEncodedName()).thenReturn(encodedName); |
| 122 | + when(regionInfo.getTable()).thenReturn(tableName); |
| 123 | + return regionInfo; |
| 124 | + } |
| 125 | +} |
0 commit comments