HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-backup / src / test / java / org / apache / hadoop / hbase / backup / TestBackupManager.java
blob91bd185b872c5dd662a8613391c79014b48ec682
1 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 package org.apache.hadoop.hbase.backup;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
24 import java.io.IOException;
25 import java.util.concurrent.atomic.AtomicLongArray;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.HBaseClassTestRule;
29 import org.apache.hadoop.hbase.HBaseTestingUtil;
30 import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
31 import org.apache.hadoop.hbase.backup.impl.BackupManager;
32 import org.apache.hadoop.hbase.client.Connection;
33 import org.apache.hadoop.hbase.testclassification.MediumTests;
34 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.ClassRule;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import org.apache.hbase.thirdparty.com.google.common.util.concurrent.Uninterruptibles;
47 @Category(MediumTests.class)
48 public class TestBackupManager {
50 private static final Logger LOG = LoggerFactory.getLogger(TestBackupManager.class);
52 @ClassRule
53 public static final HBaseClassTestRule CLASS_RULE =
54 HBaseClassTestRule.forClass(TestBackupManager.class);
56 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
57 protected static Configuration conf = UTIL.getConfiguration();
58 protected static SingleProcessHBaseCluster cluster;
59 protected static Connection conn;
60 protected BackupManager backupManager;
62 @BeforeClass
63 public static void setUp() throws Exception {
64 conf.setBoolean(BackupRestoreConstants.BACKUP_ENABLE_KEY, true);
65 BackupManager.decorateMasterConfiguration(conf);
66 BackupManager.decorateRegionServerConfiguration(conf);
67 cluster = UTIL.startMiniCluster();
68 conn = UTIL.getConnection();
71 @AfterClass
72 public static void tearDown() throws IOException {
73 if (cluster != null) {
74 cluster.shutdown();
78 @Before
79 public void before() throws IOException {
80 backupManager = new BackupManager(conn, conn.getConfiguration());
83 @After
84 public void after() {
85 backupManager.close();
88 AtomicLongArray startTimes = new AtomicLongArray(2);
89 AtomicLongArray stopTimes = new AtomicLongArray(2);
91 @Test
92 public void testStartBackupExclusiveOperation() {
94 long sleepTime = 2000;
95 Runnable r = new Runnable() {
96 @Override
97 public void run() {
98 try {
99 backupManager.startBackupSession();
100 boolean result = startTimes.compareAndSet(0, 0, EnvironmentEdgeManager.currentTime());
101 if (!result) {
102 result = startTimes.compareAndSet(1, 0, EnvironmentEdgeManager.currentTime());
103 if (!result) {
104 throw new IOException("PANIC! Unreachable code");
107 Thread.sleep(sleepTime);
108 result = stopTimes.compareAndSet(0, 0, EnvironmentEdgeManager.currentTime());
109 if (!result) {
110 result = stopTimes.compareAndSet(1, 0, EnvironmentEdgeManager.currentTime());
111 if (!result) {
112 throw new IOException("PANIC! Unreachable code");
115 backupManager.finishBackupSession();
116 } catch (IOException | InterruptedException e) {
117 fail("Unexpected exception: " + e.getMessage());
122 Thread[] workers = new Thread[2];
123 for (int i = 0; i < workers.length; i++) {
124 workers[i] = new Thread(r);
125 workers[i].start();
128 for (int i = 0; i < workers.length; i++) {
129 Uninterruptibles.joinUninterruptibly(workers[i]);
131 LOG.info("Diff start time=" + (startTimes.get(1) - startTimes.get(0)) + "ms");
132 LOG.info("Diff finish time=" + (stopTimes.get(1) - stopTimes.get(0)) + "ms");
133 assertTrue(startTimes.get(1) - startTimes.get(0) >= sleepTime);
134 assertTrue(stopTimes.get(1) - stopTimes.get(0) >= sleepTime);