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;
019
020import static org.junit.jupiter.api.Assertions.assertArrayEquals;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.client.RegionInfoBuilder;
027import org.apache.hadoop.hbase.testclassification.ClientTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.junit.jupiter.api.TestInfo;
033
034@Tag(ClientTests.TAG)
035@Tag(SmallTests.TAG)
036public class TestCatalogFamilyFormat {
037
038  @Test
039  public void testParseReplicaIdFromServerColumn() {
040    String column1 = HConstants.SERVER_QUALIFIER_STR;
041    assertEquals(0, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column1)));
042    String column2 = column1 + CatalogFamilyFormat.META_REPLICA_ID_DELIMITER;
043    assertEquals(-1, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column2)));
044    String column3 = column2 + "00";
045    assertEquals(-1, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column3)));
046    String column4 = column3 + "2A";
047    assertEquals(42, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column4)));
048    String column5 = column4 + "2A";
049    assertEquals(-1, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column5)));
050    String column6 = HConstants.STARTCODE_QUALIFIER_STR;
051    assertEquals(-1, CatalogFamilyFormat.parseReplicaIdFromServerColumn(Bytes.toBytes(column6)));
052  }
053
054  @Test
055  public void testMetaReaderGetColumnMethods() {
056    assertArrayEquals(HConstants.SERVER_QUALIFIER, CatalogFamilyFormat.getServerColumn(0));
057    assertArrayEquals(
058      Bytes.toBytes(
059        HConstants.SERVER_QUALIFIER_STR + CatalogFamilyFormat.META_REPLICA_ID_DELIMITER + "002A"),
060      CatalogFamilyFormat.getServerColumn(42));
061
062    assertArrayEquals(HConstants.STARTCODE_QUALIFIER, CatalogFamilyFormat.getStartCodeColumn(0));
063    assertArrayEquals(
064      Bytes.toBytes(HConstants.STARTCODE_QUALIFIER_STR
065        + CatalogFamilyFormat.META_REPLICA_ID_DELIMITER + "002A"),
066      CatalogFamilyFormat.getStartCodeColumn(42));
067
068    assertArrayEquals(HConstants.SEQNUM_QUALIFIER, CatalogFamilyFormat.getSeqNumColumn(0));
069    assertArrayEquals(
070      Bytes.toBytes(
071        HConstants.SEQNUM_QUALIFIER_STR + CatalogFamilyFormat.META_REPLICA_ID_DELIMITER + "002A"),
072      CatalogFamilyFormat.getSeqNumColumn(42));
073  }
074
075  /**
076   * The info we can get from the regionName is: table name, start key, regionId, replicaId.
077   */
078  @Test
079  public void testParseRegionInfoFromRegionName(TestInfo testInfo) throws IOException {
080    String name = testInfo.getTestMethod().get().getName();
081    RegionInfo originalRegionInfo =
082      RegionInfoBuilder.newBuilder(TableName.valueOf(name)).setRegionId(999999L)
083        .setStartKey(Bytes.toBytes("2")).setEndKey(Bytes.toBytes("3")).setReplicaId(1).build();
084    RegionInfo newParsedRegionInfo =
085      CatalogFamilyFormat.parseRegionInfoFromRegionName(originalRegionInfo.getRegionName());
086    assertEquals(originalRegionInfo.getTable(), newParsedRegionInfo.getTable(),
087      "Parse TableName error");
088    assertEquals(originalRegionInfo.getRegionId(), newParsedRegionInfo.getRegionId(),
089      "Parse regionId error");
090    assertTrue(Bytes.equals(originalRegionInfo.getStartKey(), newParsedRegionInfo.getStartKey()),
091      "Parse startKey error");
092    assertEquals(originalRegionInfo.getReplicaId(), newParsedRegionInfo.getReplicaId(),
093      "Parse replicaId error");
094    assertTrue(Bytes.equals(HConstants.EMPTY_END_ROW, newParsedRegionInfo.getEndKey()),
095      "We can't parse endKey from regionName only");
096  }
097}