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.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNotEquals;
023import static org.junit.Assert.assertTrue;
024
025import java.io.IOException;
026import java.util.List;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HColumnDescriptor;
029import org.apache.hadoop.hbase.HTableDescriptor;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.TableNotDisabledException;
032import org.apache.hadoop.hbase.TableNotFoundException;
033import org.apache.hadoop.hbase.client.Admin;
034import org.apache.hadoop.hbase.client.SnapshotDescription;
035import org.apache.hadoop.hbase.procedure2.Procedure;
036import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
037import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
038import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
039import org.apache.hadoop.hbase.testclassification.LargeTests;
040import org.apache.hadoop.hbase.testclassification.MasterTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
043import org.junit.After;
044import org.junit.Before;
045import org.junit.ClassRule;
046import org.junit.Rule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049import org.junit.rules.TestName;
050import org.slf4j.Logger;
051import org.slf4j.LoggerFactory;
052
053import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
054import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
055
056@Category({ MasterTests.class, LargeTests.class })
057public class TestRestoreSnapshotProcedure extends TestTableDDLProcedureBase {
058
059  @ClassRule
060  public static final HBaseClassTestRule CLASS_RULE =
061    HBaseClassTestRule.forClass(TestRestoreSnapshotProcedure.class);
062
063  private static final Logger LOG = LoggerFactory.getLogger(TestRestoreSnapshotProcedure.class);
064
065  protected final TableName snapshotTableName = TableName.valueOf("testRestoreSnapshot");
066  protected final byte[] CF1 = Bytes.toBytes("cf1");
067  protected final byte[] CF2 = Bytes.toBytes("cf2");
068  protected final byte[] CF3 = Bytes.toBytes("cf3");
069  protected final byte[] CF4 = Bytes.toBytes("cf4");
070  protected final int rowCountCF1 = 10;
071  protected final int rowCountCF2 = 40;
072  protected final int rowCountCF3 = 40;
073  protected final int rowCountCF4 = 40;
074  protected final int rowCountCF1addition = 10;
075
076  private SnapshotProtos.SnapshotDescription snapshot = null;
077  private HTableDescriptor snapshotHTD = null;
078
079  @Rule
080  public TestName name = new TestName();
081
082  @Before
083  @Override
084  public void setup() throws Exception {
085    super.setup();
086    setupSnapshotAndUpdateTable();
087  }
088
089  @After
090  @Override
091  public void tearDown() throws Exception {
092    super.tearDown();
093    SnapshotTestingUtils.deleteAllSnapshots(UTIL.getAdmin());
094    SnapshotTestingUtils.deleteArchiveDirectory(UTIL);
095  }
096
097  private int getNumReplicas() {
098    return 1;
099  }
100
101  private void setupSnapshotAndUpdateTable() throws Exception {
102    long tid = EnvironmentEdgeManager.currentTime();
103    final byte[] snapshotName = Bytes.toBytes("snapshot-" + tid);
104    Admin admin = UTIL.getAdmin();
105    // create Table
106    SnapshotTestingUtils.createTable(UTIL, snapshotTableName, getNumReplicas(), CF1, CF2);
107    // Load data
108    SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF1, CF1);
109    SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF2, CF2);
110    SnapshotTestingUtils.verifyRowCount(UTIL, snapshotTableName, rowCountCF1 + rowCountCF2);
111
112    snapshotHTD = admin.getTableDescriptor(snapshotTableName);
113
114    admin.disableTable(snapshotTableName);
115    // take a snapshot
116    admin.snapshot(snapshotName, snapshotTableName);
117
118    List<SnapshotDescription> snapshotList = admin.listSnapshots();
119    snapshot = ProtobufUtil.createHBaseProtosSnapshotDesc(snapshotList.get(0));
120
121    // modify the table
122    HColumnDescriptor columnFamilyDescriptor3 = new HColumnDescriptor(CF3);
123    HColumnDescriptor columnFamilyDescriptor4 = new HColumnDescriptor(CF4);
124    admin.addColumnFamily(snapshotTableName, columnFamilyDescriptor3);
125    admin.addColumnFamily(snapshotTableName, columnFamilyDescriptor4);
126    admin.deleteColumnFamily(snapshotTableName, CF2);
127    // enable table and insert data
128    admin.enableTable(snapshotTableName);
129    SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF3, CF3);
130    SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF4, CF4);
131    SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF1addition, CF1);
132    HTableDescriptor currentHTD = admin.getTableDescriptor(snapshotTableName);
133    assertTrue(currentHTD.hasFamily(CF1));
134    assertFalse(currentHTD.hasFamily(CF2));
135    assertTrue(currentHTD.hasFamily(CF3));
136    assertTrue(currentHTD.hasFamily(CF4));
137    assertNotEquals(currentHTD.getFamiliesKeys().size(), snapshotHTD.getFamiliesKeys().size());
138    SnapshotTestingUtils.verifyRowCount(UTIL, snapshotTableName,
139      rowCountCF1 + rowCountCF3 + rowCountCF4 + rowCountCF1addition);
140    admin.disableTable(snapshotTableName);
141  }
142
143  private static HTableDescriptor createHTableDescriptor(final TableName tableName,
144    final byte[]... family) {
145    HTableDescriptor htd = new HTableDescriptor(tableName);
146    for (int i = 0; i < family.length; ++i) {
147      htd.addFamily(new HColumnDescriptor(family[i]));
148    }
149    return htd;
150  }
151
152  @Test
153  public void testRestoreSnapshot() throws Exception {
154    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
155
156    long procId = ProcedureTestingUtility.submitAndWait(procExec,
157      new RestoreSnapshotProcedure(procExec.getEnvironment(), snapshotHTD, snapshot));
158    ProcedureTestingUtility.assertProcNotFailed(procExec.getResult(procId));
159
160    validateSnapshotRestore();
161  }
162
163  @Test
164  public void testRestoreSnapshotToDifferentTable() throws Exception {
165    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
166    final TableName restoredTableName = TableName.valueOf(name.getMethodName());
167    final HTableDescriptor newHTD = createHTableDescriptor(restoredTableName, CF1, CF2);
168
169    long procId = ProcedureTestingUtility.submitAndWait(procExec,
170      new RestoreSnapshotProcedure(procExec.getEnvironment(), newHTD, snapshot));
171    Procedure<?> result = procExec.getResult(procId);
172    assertTrue(result.isFailed());
173    LOG.debug("Restore snapshot failed with exception: " + result.getException());
174    assertTrue(ProcedureTestingUtility.getExceptionCause(result) instanceof TableNotFoundException);
175  }
176
177  @Test
178  public void testRestoreSnapshotToEnabledTable() throws Exception {
179    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
180
181    try {
182      UTIL.getAdmin().enableTable(snapshotTableName);
183
184      long procId = ProcedureTestingUtility.submitAndWait(procExec,
185        new RestoreSnapshotProcedure(procExec.getEnvironment(), snapshotHTD, snapshot));
186      Procedure<?> result = procExec.getResult(procId);
187      assertTrue(result.isFailed());
188      LOG.debug("Restore snapshot failed with exception: " + result.getException());
189      assertTrue(
190        ProcedureTestingUtility.getExceptionCause(result) instanceof TableNotDisabledException);
191    } finally {
192      UTIL.getAdmin().disableTable(snapshotTableName);
193    }
194  }
195
196  @Test
197  public void testRecoveryAndDoubleExecution() throws Exception {
198    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
199
200    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
201
202    // Start the Restore snapshot procedure && kill the executor
203    long procId = procExec.submitProcedure(
204      new RestoreSnapshotProcedure(procExec.getEnvironment(), snapshotHTD, snapshot));
205
206    // Restart the executor and execute the step twice
207    MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId);
208
209    resetProcExecutorTestingKillFlag();
210    validateSnapshotRestore();
211  }
212
213  @Test
214  public void testRecoverWithRestoreAclFlag() throws Exception {
215    // This test is to solve the problems mentioned in HBASE-26462,
216    // this needs to simulate the case of RestoreSnapshotProcedure failure and recovery,
217    // and verify whether 'restoreAcl' flag can obtain the correct value.
218
219    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
220    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
221
222    // Start the Restore snapshot procedure (with restoreAcl 'true') && kill the executor
223    long procId = procExec.submitProcedure(
224      new RestoreSnapshotProcedure(procExec.getEnvironment(), snapshotHTD, snapshot, true));
225    MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId);
226
227    RestoreSnapshotProcedure result = (RestoreSnapshotProcedure) procExec.getResult(procId);
228    // check whether the restoreAcl flag is true after deserialization from Pb.
229    assertEquals(true, result.getRestoreAcl());
230  }
231
232  private void validateSnapshotRestore() throws IOException {
233    try {
234      UTIL.getAdmin().enableTable(snapshotTableName);
235
236      HTableDescriptor currentHTD = UTIL.getAdmin().getTableDescriptor(snapshotTableName);
237      assertTrue(currentHTD.hasFamily(CF1));
238      assertTrue(currentHTD.hasFamily(CF2));
239      assertFalse(currentHTD.hasFamily(CF3));
240      assertFalse(currentHTD.hasFamily(CF4));
241      assertEquals(currentHTD.getFamiliesKeys().size(), snapshotHTD.getFamiliesKeys().size());
242      SnapshotTestingUtils.verifyRowCount(UTIL, snapshotTableName, rowCountCF1 + rowCountCF2);
243    } finally {
244      UTIL.getAdmin().disableTable(snapshotTableName);
245    }
246  }
247}