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
.HBaseTestingUtility
;
30 import org
.apache
.hadoop
.hbase
.MiniHBaseCluster
;
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
.junit
.After
;
35 import org
.junit
.AfterClass
;
36 import org
.junit
.Before
;
37 import org
.junit
.BeforeClass
;
38 import org
.junit
.ClassRule
;
39 import org
.junit
.Test
;
40 import org
.junit
.experimental
.categories
.Category
;
41 import org
.slf4j
.Logger
;
42 import org
.slf4j
.LoggerFactory
;
44 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.util
.concurrent
.Uninterruptibles
;
46 @Category(MediumTests
.class)
47 public class TestBackupManager
{
49 private static final Logger LOG
= LoggerFactory
.getLogger(TestBackupManager
.class);
52 public static final HBaseClassTestRule CLASS_RULE
=
53 HBaseClassTestRule
.forClass(TestBackupManager
.class);
55 private static final HBaseTestingUtility UTIL
= new HBaseTestingUtility();
56 protected static Configuration conf
= UTIL
.getConfiguration();
57 protected static MiniHBaseCluster cluster
;
58 protected static Connection conn
;
59 protected BackupManager backupManager
;
62 public static void setUp() throws Exception
{
63 conf
.setBoolean(BackupRestoreConstants
.BACKUP_ENABLE_KEY
, true);
64 BackupManager
.decorateMasterConfiguration(conf
);
65 BackupManager
.decorateRegionServerConfiguration(conf
);
66 cluster
= UTIL
.startMiniCluster();
67 conn
= UTIL
.getConnection();
71 public static void tearDown() throws IOException
{
72 if (cluster
!= null) {
78 public void before() throws IOException
{
79 backupManager
= new BackupManager(conn
, conn
.getConfiguration());
84 backupManager
.close();
87 AtomicLongArray startTimes
= new AtomicLongArray(2);
88 AtomicLongArray stopTimes
= new AtomicLongArray(2);
91 public void testStartBackupExclusiveOperation() {
93 long sleepTime
= 2000;
94 Runnable r
= new Runnable() {
98 backupManager
.startBackupSession();
99 boolean result
= startTimes
.compareAndSet(0, 0, System
.currentTimeMillis());
101 result
= startTimes
.compareAndSet(1, 0, System
.currentTimeMillis());
103 throw new IOException("PANIC! Unreachable code");
106 Thread
.sleep(sleepTime
);
107 result
= stopTimes
.compareAndSet(0, 0, System
.currentTimeMillis());
109 result
= stopTimes
.compareAndSet(1, 0, System
.currentTimeMillis());
111 throw new IOException("PANIC! Unreachable code");
114 backupManager
.finishBackupSession();
115 } catch (IOException
| InterruptedException e
) {
116 fail("Unexpected exception: " + e
.getMessage());
121 Thread
[] workers
= new Thread
[2];
122 for (int i
= 0; i
< workers
.length
; i
++) {
123 workers
[i
] = new Thread(r
);
127 for (int i
= 0; i
< workers
.length
; i
++) {
128 Uninterruptibles
.joinUninterruptibly(workers
[i
]);
130 LOG
.info("Diff start time=" + (startTimes
.get(1) - startTimes
.get(0)) + "ms");
131 LOG
.info("Diff finish time=" + (stopTimes
.get(1) - stopTimes
.get(0)) + "ms");
132 assertTrue(startTimes
.get(1) - startTimes
.get(0) >= sleepTime
);
133 assertTrue(stopTimes
.get(1) - stopTimes
.get(0) >= sleepTime
);