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