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.migrate;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022
023import java.io.IOException;
024import org.apache.commons.lang3.StringUtils;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableDescriptors;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.TableDescriptor;
032import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
033import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.junit.jupiter.api.AfterEach;
038import org.junit.jupiter.api.BeforeEach;
039import org.junit.jupiter.api.Tag;
040import org.junit.jupiter.api.Test;
041
042@Tag(MediumTests.TAG)
043@Tag(MasterTests.TAG)
044public class TestInitializeStoreFileTracker {
045
046  private final static String[] tables = new String[] { "t1", "t2", "t3", "t4", "t5", "t6" };
047  private final static String famStr = "f1";
048  private final static byte[] fam = Bytes.toBytes(famStr);
049
050  private HBaseTestingUtil HTU;
051  private Configuration conf;
052  private TableDescriptor tableDescriptor;
053
054  @BeforeEach
055  public void setUp() throws Exception {
056    conf = HBaseConfiguration.create();
057    // Speed up the launch of RollingUpgradeChore
058    conf.setInt(RollingUpgradeChore.ROLLING_UPGRADE_CHORE_PERIOD_SECONDS_KEY, 1);
059    conf.setLong(RollingUpgradeChore.ROLLING_UPGRADE_CHORE_DELAY_SECONDS_KEY, 1);
060    // Set the default implementation to file instead of default, to confirm we will not set SFT to
061    // file
062    conf.set(StoreFileTrackerFactory.TRACKER_IMPL, StoreFileTrackerFactory.Trackers.FILE.name());
063    HTU = new HBaseTestingUtil(conf);
064    HTU.startMiniCluster();
065  }
066
067  @AfterEach
068  public void tearDown() throws Exception {
069    HTU.shutdownMiniCluster();
070  }
071
072  @Test
073  public void testMigrateStoreFileTracker() throws IOException, InterruptedException {
074    // create tables to test
075    for (int i = 0; i < tables.length; i++) {
076      tableDescriptor = HTU.createModifyableTableDescriptor(tables[i])
077        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(fam).build()).build();
078      HTU.createTable(tableDescriptor, null);
079    }
080    TableDescriptors tableDescriptors = HTU.getMiniHBaseCluster().getMaster().getTableDescriptors();
081    for (int i = 0; i < tables.length; i++) {
082      TableDescriptor tdAfterCreated = tableDescriptors.get(TableName.valueOf(tables[i]));
083      // make sure that TRACKER_IMPL was set by default after tables have been created.
084      assertNotNull(tdAfterCreated.getValue(StoreFileTrackerFactory.TRACKER_IMPL));
085      // Remove StoreFileTracker impl from tableDescriptor
086      TableDescriptor tdRemovedSFT = TableDescriptorBuilder.newBuilder(tdAfterCreated)
087        .removeValue(StoreFileTrackerFactory.TRACKER_IMPL).build();
088      tableDescriptors.update(tdRemovedSFT);
089    }
090    HTU.getMiniHBaseCluster().stopMaster(0).join();
091    HTU.getMiniHBaseCluster().startMaster();
092    HTU.getMiniHBaseCluster().waitForActiveAndReadyMaster(30000);
093    // wait until all tables have been migrated
094    TableDescriptors tds = HTU.getMiniHBaseCluster().getMaster().getTableDescriptors();
095    HTU.waitFor(30000, () -> {
096      try {
097        for (int i = 0; i < tables.length; i++) {
098          TableDescriptor td = tds.get(TableName.valueOf(tables[i]));
099          if (StringUtils.isEmpty(td.getValue(StoreFileTrackerFactory.TRACKER_IMPL))) {
100            return false;
101          }
102        }
103        return true;
104      } catch (IOException e) {
105        return false;
106      }
107    });
108    for (String table : tables) {
109      TableDescriptor td = tds.get(TableName.valueOf(table));
110      assertEquals(StoreFileTrackerFactory.Trackers.DEFAULT.name(),
111        td.getValue(StoreFileTrackerFactory.TRACKER_IMPL));
112    }
113  }
114}