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.locking;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.util.List;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HRegionInfo;
029import org.apache.hadoop.hbase.NamespaceDescriptor;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.master.MasterServices;
032import org.apache.hadoop.hbase.master.procedure.MasterProcedureConstants;
033import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
034import org.apache.hadoop.hbase.procedure2.LockType;
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.testclassification.MasterTests;
039import org.apache.hadoop.hbase.testclassification.MediumTests;
040import org.junit.After;
041import org.junit.AfterClass;
042import org.junit.BeforeClass;
043import org.junit.ClassRule;
044import org.junit.Rule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047import org.junit.rules.TestName;
048import org.slf4j.Logger;
049import org.slf4j.LoggerFactory;
050
051@Category({ MasterTests.class, MediumTests.class })
052public class TestLockManager {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestLockManager.class);
057
058  @Rule
059  public TestName testName = new TestName();
060  // crank this up if this test turns out to be flaky.
061  private static final int LOCAL_LOCKS_TIMEOUT = 1000;
062
063  private static final Logger LOG = LoggerFactory.getLogger(TestLockManager.class);
064  protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
065  private static MasterServices masterServices;
066
067  private static String namespace = "namespace";
068  private static TableName tableName = TableName.valueOf(namespace, "table");
069  private static HRegionInfo[] tableRegions;
070
071  private static void setupConf(Configuration conf) {
072    conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
073    conf.setInt(MasterProcedureConstants.MASTER_URGENT_PROCEDURE_THREADS, 0);
074    conf.setBoolean("hbase.procedure.check.owner.set", false);  // since rpc user will be null
075    conf.setInt(LockProcedure.LOCAL_MASTER_LOCKS_TIMEOUT_MS_CONF, LOCAL_LOCKS_TIMEOUT);
076  }
077
078  @BeforeClass
079  public static void setupCluster() throws Exception {
080    setupConf(UTIL.getConfiguration());
081    UTIL.startMiniCluster(1);
082    masterServices = UTIL.getMiniHBaseCluster().getMaster();
083    UTIL.getAdmin().createNamespace(NamespaceDescriptor.create(namespace).build());
084    UTIL.createTable(tableName, new byte[][]{"fam".getBytes()}, new byte[][] {"1".getBytes()});
085    List<HRegionInfo> regions = UTIL.getAdmin().getTableRegions(tableName);
086    assert regions.size() > 0;
087    tableRegions = new HRegionInfo[regions.size()];
088    regions.toArray(tableRegions);
089  }
090
091  @AfterClass
092  public static void cleanupTest() throws Exception {
093    try {
094      UTIL.shutdownMiniCluster();
095    } catch (Exception e) {
096      LOG.warn("failure shutting down cluster", e);
097    }
098  }
099
100  @After
101  public void tearDown() throws Exception {
102    for (Procedure<?> proc : getMasterProcedureExecutor().getProcedures()) {
103      if (proc instanceof LockProcedure) {
104        ((LockProcedure) proc).unlock(getMasterProcedureExecutor().getEnvironment());
105        ProcedureTestingUtility.waitProcedure(getMasterProcedureExecutor(), proc);
106      }
107    }
108    assertEquals(0, getMasterProcedureExecutor().getEnvironment().getProcedureScheduler().size());
109  }
110
111  private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
112    return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
113  }
114
115  /**
116   * Tests that basic lock functionality works.
117   */
118  @Test
119  public void testMasterLockAcquire() throws Exception {
120    LockManager.MasterLock lock = masterServices.getLockManager().createMasterLock(namespace,
121        LockType.EXCLUSIVE, "desc");
122    assertTrue(lock.tryAcquire(2000));
123    assertTrue(lock.getProc().isLocked());
124    lock.release();
125    assertEquals(null, lock.getProc());
126  }
127
128  /**
129   * Two locks try to acquire lock on same table, assert that later one times out.
130   */
131  @Test
132  public void testMasterLockAcquireTimeout() throws Exception {
133    LockManager.MasterLock lock = masterServices.getLockManager().createMasterLock(
134        tableName, LockType.EXCLUSIVE, "desc");
135    LockManager.MasterLock lock2 = masterServices.getLockManager().createMasterLock(
136        tableName, LockType.EXCLUSIVE, "desc");
137    assertTrue(lock.tryAcquire(2000));
138    assertFalse(lock2.tryAcquire(LOCAL_LOCKS_TIMEOUT/2));  // wait less than other lock's timeout
139    assertEquals(null, lock2.getProc());
140    lock.release();
141    assertTrue(lock2.tryAcquire(2000));
142    assertTrue(lock2.getProc().isLocked());
143    lock2.release();
144  }
145
146  /**
147   * Take region lock, they try table exclusive lock, later one should time out.
148   */
149  @Test
150  public void testMasterLockAcquireTimeoutRegionVsTableExclusive() throws Exception {
151    LockManager.MasterLock lock = masterServices.getLockManager().createMasterLock(
152        tableRegions, "desc");
153    LockManager.MasterLock lock2 = masterServices.getLockManager().createMasterLock(
154        tableName, LockType.EXCLUSIVE, "desc");
155    assertTrue(lock.tryAcquire(2000));
156    assertFalse(lock2.tryAcquire(LOCAL_LOCKS_TIMEOUT/2));  // wait less than other lock's timeout
157    assertEquals(null, lock2.getProc());
158    lock.release();
159    assertTrue(lock2.tryAcquire(2000));
160    assertTrue(lock2.getProc().isLocked());
161    lock2.release();
162  }
163}