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.regionserver;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022import static org.junit.jupiter.api.Assertions.fail;
023
024import java.io.IOException;
025import org.apache.hadoop.fs.FSDataInputStream;
026import org.apache.hadoop.fs.FSDataOutputStream;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.ActiveClusterSuffix;
030import org.apache.hadoop.hbase.ClusterId;
031import org.apache.hadoop.hbase.HBaseCommonTestingUtil;
032import org.apache.hadoop.hbase.HBaseTestingUtil;
033import org.apache.hadoop.hbase.HConstants;
034import org.apache.hadoop.hbase.master.MasterFileSystem;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.testclassification.RegionServerTests;
037import org.apache.hadoop.hbase.util.CommonFSUtils;
038import org.apache.hadoop.hbase.util.JVMClusterUtil;
039import org.junit.jupiter.api.AfterEach;
040import org.junit.jupiter.api.BeforeEach;
041import org.junit.jupiter.api.Tag;
042import org.junit.jupiter.api.Test;
043
044/**
045 * Test Active Cluster Suffix file.
046 */
047@Tag(RegionServerTests.TAG)
048@Tag(MediumTests.TAG)
049public class TestActiveClusterSuffix {
050
051  private final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
052
053  private JVMClusterUtil.RegionServerThread rst;
054
055  @BeforeEach
056  public void setUp() throws Exception {
057    TEST_UTIL.getConfiguration().setBoolean(ShutdownHook.RUN_SHUTDOWN_HOOK, false);
058  }
059
060  @AfterEach
061  public void tearDown() throws Exception {
062    TEST_UTIL.shutdownMiniCluster();
063    if (rst != null && rst.getRegionServer() != null) {
064      rst.getRegionServer().stop("end of test");
065      rst.join();
066    }
067  }
068
069  @Test
070  public void testActiveClusterSuffixCreated() throws Exception {
071    TEST_UTIL.startMiniZKCluster();
072    TEST_UTIL.startMiniDFSCluster(1);
073    TEST_UTIL.startMiniHBaseCluster();
074
075    Path rootDir = CommonFSUtils.getRootDir(TEST_UTIL.getConfiguration());
076    FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration());
077    Path filePath = new Path(rootDir, HConstants.ACTIVE_CLUSTER_SUFFIX_FILE_NAME);
078
079    assertTrue(fs.exists(filePath), filePath + " should exists ");
080    assertTrue(fs.getFileStatus(filePath).getLen() > 0, filePath + " should not be empty  ");
081
082    MasterFileSystem mfs = TEST_UTIL.getHBaseCluster().getMaster().getMasterFileSystem();
083
084    try (FSDataInputStream in = fs.open(filePath)) {
085      ActiveClusterSuffix suffixFromFile = ActiveClusterSuffix.parseFrom(in.readAllBytes());
086      ActiveClusterSuffix suffixFromConfig =
087        ActiveClusterSuffix.fromConfig(TEST_UTIL.getConfiguration(), mfs.getClusterId());
088      assertEquals(suffixFromFile, suffixFromConfig,
089        "Active Cluster Suffix file content doesn't match configuration");
090    }
091  }
092
093  @Test
094  public void testSuffixFileOnRestart() throws Exception {
095    TEST_UTIL.startMiniZKCluster();
096    TEST_UTIL.startMiniDFSCluster(1);
097    TEST_UTIL.createRootDir();
098    TEST_UTIL.getConfiguration().set(HConstants.HBASE_META_TABLE_SUFFIX, "Test");
099
100    String clusterId = HBaseCommonTestingUtil.getRandomUUID().toString();
101    String cluster_suffix = clusterId + ":" + TEST_UTIL.getConfiguration()
102      .get(HConstants.HBASE_META_TABLE_SUFFIX, HConstants.HBASE_META_TABLE_SUFFIX_DEFAULT_VALUE);
103
104    writeIdFile(clusterId, HConstants.CLUSTER_ID_FILE_NAME);
105    writeIdFile(cluster_suffix, HConstants.ACTIVE_CLUSTER_SUFFIX_FILE_NAME);
106
107    try {
108      TEST_UTIL.startMiniHBaseCluster();
109    } catch (IOException ioe) {
110      fail("Can't start mini hbase cluster.");
111    }
112
113    MasterFileSystem mfs = TEST_UTIL.getHBaseCluster().getMaster().getMasterFileSystem();
114
115    // Compute using file contents
116    ActiveClusterSuffix cluster_suffix1 = mfs.getActiveClusterSuffix();
117    // Compute using config
118    ActiveClusterSuffix cluster_suffix2 =
119      ActiveClusterSuffix.fromConfig(TEST_UTIL.getConfiguration(), new ClusterId(clusterId));
120
121    assertEquals(cluster_suffix1, cluster_suffix2);
122    assertEquals(cluster_suffix, cluster_suffix1.toString());
123  }
124
125  @Test
126  public void testVerifyErrorWhenSuffixNotMatched() throws Exception {
127    TEST_UTIL.startMiniZKCluster();
128    TEST_UTIL.startMiniDFSCluster(1);
129    TEST_UTIL.createRootDir();
130    TEST_UTIL.getConfiguration().setInt("hbase.master.start.timeout.localHBaseCluster", 10000);
131    String cluster_suffix = String.valueOf("2df92f65-d801-46e6-b892-c2bae2df3c21:test");
132    writeIdFile(cluster_suffix, HConstants.ACTIVE_CLUSTER_SUFFIX_FILE_NAME);
133    // Exception: as config in the file and the one set by the user are not matching
134    boolean threwIOE = false;
135    try {
136      TEST_UTIL.startMiniHBaseCluster();
137    } catch (IOException ioe) {
138      threwIOE = true;
139    } finally {
140      assertTrue(threwIOE, "The master should have thrown an exception");
141    }
142  }
143
144  private void writeIdFile(String id, String fileName) throws Exception {
145    Path rootDir = CommonFSUtils.getRootDir(TEST_UTIL.getConfiguration());
146    FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration());
147    Path filePath = new Path(rootDir, fileName);
148    FSDataOutputStream s = null;
149    try {
150      s = fs.create(filePath);
151      s.writeUTF(id);
152    } finally {
153      if (s != null) {
154        s.close();
155      }
156    }
157  }
158}