001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master.procedure;
019
020import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.when;
025
026import java.util.Arrays;
027import java.util.Collections;
028import org.apache.hadoop.hbase.DoNotRetryIOException;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.client.RegionInfoBuilder;
032import org.apache.hadoop.hbase.master.assignment.RegionStateNode;
033import org.apache.hadoop.hbase.master.assignment.RegionStates;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.apache.hadoop.hbase.util.ModifyRegionUtils;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038
039/**
040 * Tests for encoded region-name collision detection (HBASE-30160). If two regions end up with the
041 * same encoded name, we should fail fast instead of allowing subtle metadata corruption later.
042 */
043
044@Tag(SmallTests.TAG)
045public class TestEncodedNameCollisionDetection {
046
047  /**
048   * Happy-path check: distinct candidate regions should pass without throwing.
049   */
050  @Test
051  public void testAcceptsDistinctCandidates() {
052    TableName tableName = TableName.valueOf("test_table");
053    long regionId = System.currentTimeMillis();
054
055    RegionInfo ri1 = RegionInfoBuilder.newBuilder(tableName).setStartKey(new byte[] { 0, 0 })
056      .setEndKey(new byte[] { 1, 0 }).setSplit(false).setRegionId(regionId).build();
057
058    RegionInfo ri2 = RegionInfoBuilder.newBuilder(tableName).setStartKey(new byte[] { 1, 0 })
059      .setEndKey(new byte[] { 2, 0 }).setSplit(false).setRegionId(regionId + 1).build();
060
061    RegionStates regionStates = mock(RegionStates.class);
062
063    assertDoesNotThrow(
064      () -> ModifyRegionUtils.checkForEncodedNameCollisions(Arrays.asList(ri1, ri2), regionStates));
065  }
066
067  /**
068   * Verifies that duplicate encoded region names within candidate regions are rejected.
069   */
070  @Test
071  public void testDetectsDuplicatesInCandidates() {
072    TableName tableName = TableName.valueOf("test_table");
073    RegionInfo ri1 = mockRegionInfo(tableName, "same-encoded-name");
074    RegionInfo ri2 = mockRegionInfo(tableName, "same-encoded-name");
075
076    RegionStates regionStates = mock(RegionStates.class);
077
078    DoNotRetryIOException exception = assertThrows(DoNotRetryIOException.class,
079      () -> ModifyRegionUtils.checkForEncodedNameCollisions(Arrays.asList(ri1, ri2), regionStates));
080    assertTrue(exception.getMessage().contains("Encoded region name collision detected"));
081  }
082
083  /**
084   * A candidate region should be rejected if its encoded name already exists.
085   */
086  @Test
087  public void testDetectsCollisionWithExistingRegions() {
088    TableName tableName = TableName.valueOf("test_table");
089    RegionInfo candidateRegion = mockRegionInfo(tableName, "same-encoded-name");
090    RegionStates regionStates = mock(RegionStates.class);
091    when(regionStates.getRegionStateNodeFromEncodedRegionName("same-encoded-name"))
092      .thenReturn(mock(RegionStateNode.class));
093
094    DoNotRetryIOException exception =
095      assertThrows(DoNotRetryIOException.class, () -> ModifyRegionUtils
096        .checkForEncodedNameCollisions(Arrays.asList(candidateRegion), regionStates));
097    assertTrue(exception.getMessage().contains("Encoded region name collision detected"));
098  }
099
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}