2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertNull
;
22 import static org
.junit
.Assert
.assertTrue
;
24 import java
.io
.IOException
;
25 import org
.apache
.hadoop
.hbase
.master
.RegionState
;
26 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
27 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
28 import org
.apache
.hadoop
.hbase
.util
.Threads
;
29 import org
.apache
.hadoop
.hbase
.zookeeper
.MetaTableLocator
;
30 import org
.apache
.hadoop
.hbase
.zookeeper
.ZKWatcher
;
31 import org
.apache
.zookeeper
.KeeperException
;
32 import org
.junit
.After
;
33 import org
.junit
.AfterClass
;
34 import org
.junit
.Before
;
35 import org
.junit
.BeforeClass
;
36 import org
.junit
.ClassRule
;
37 import org
.junit
.Test
;
38 import org
.junit
.experimental
.categories
.Category
;
39 import org
.mockito
.Mockito
;
40 import org
.slf4j
.Logger
;
41 import org
.slf4j
.LoggerFactory
;
43 import org
.apache
.hbase
.thirdparty
.com
.google
.protobuf
.RpcController
;
44 import org
.apache
.hbase
.thirdparty
.com
.google
.protobuf
.ServiceException
;
46 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.ClientProtos
;
47 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.ClientProtos
.GetRequest
;
48 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.ClientProtos
.GetResponse
;
51 * Test {@link org.apache.hadoop.hbase.zookeeper.MetaTableLocator}
53 @Category({ MiscTests
.class, MediumTests
.class })
54 public class TestMetaTableLocator
{
57 public static final HBaseClassTestRule CLASS_RULE
=
58 HBaseClassTestRule
.forClass(TestMetaTableLocator
.class);
60 private static final Logger LOG
= LoggerFactory
.getLogger(TestMetaTableLocator
.class);
61 private static final HBaseTestingUtility UTIL
= new HBaseTestingUtility();
62 private static final ServerName SN
=
63 ServerName
.valueOf("example.org", 1234, System
.currentTimeMillis());
64 private ZKWatcher watcher
;
65 private Abortable abortable
;
68 public static void beforeClass() throws Exception
{
69 // Set this down so tests run quicker
70 UTIL
.getConfiguration().setInt(HConstants
.HBASE_CLIENT_RETRIES_NUMBER
, 3);
71 UTIL
.startMiniZKCluster();
75 public static void afterClass() throws IOException
{
76 UTIL
.getZkCluster().shutdown();
80 public void before() throws IOException
{
81 this.abortable
= new Abortable() {
83 public void abort(String why
, Throwable e
) {
88 public boolean isAborted() {
93 new ZKWatcher(UTIL
.getConfiguration(), this.getClass().getSimpleName(), this.abortable
, true);
99 // Clean out meta location or later tests will be confused... they presume
100 // start fresh in zk.
101 MetaTableLocator
.deleteMetaLocation(this.watcher
);
102 } catch (KeeperException e
) {
103 LOG
.warn("Unable to delete hbase:meta location", e
);
106 this.watcher
.close();
110 * Test normal operations
113 public void testMetaLookup()
114 throws IOException
, InterruptedException
, ServiceException
, KeeperException
{
115 final ClientProtos
.ClientService
.BlockingInterface client
=
116 Mockito
.mock(ClientProtos
.ClientService
.BlockingInterface
.class);
118 Mockito
.when(client
.get((RpcController
) Mockito
.any(), (GetRequest
) Mockito
.any()))
119 .thenReturn(GetResponse
.newBuilder().build());
121 assertNull(MetaTableLocator
.getMetaRegionLocation(this.watcher
));
122 for (RegionState
.State state
: RegionState
.State
.values()) {
123 if (state
.equals(RegionState
.State
.OPEN
)) {
126 MetaTableLocator
.setMetaLocation(this.watcher
, SN
, state
);
127 assertNull(MetaTableLocator
.getMetaRegionLocation(this.watcher
));
128 assertEquals(state
, MetaTableLocator
.getMetaRegionState(this.watcher
).getState());
130 MetaTableLocator
.setMetaLocation(this.watcher
, SN
, RegionState
.State
.OPEN
);
131 assertEquals(SN
, MetaTableLocator
.getMetaRegionLocation(this.watcher
));
132 assertEquals(RegionState
.State
.OPEN
,
133 MetaTableLocator
.getMetaRegionState(this.watcher
).getState());
135 MetaTableLocator
.deleteMetaLocation(this.watcher
);
136 assertNull(MetaTableLocator
.getMetaRegionState(this.watcher
).getServerName());
137 assertEquals(RegionState
.State
.OFFLINE
,
138 MetaTableLocator
.getMetaRegionState(this.watcher
).getState());
139 assertNull(MetaTableLocator
.getMetaRegionLocation(this.watcher
));
142 @Test(expected
= NotAllMetaRegionsOnlineException
.class)
143 public void testTimeoutWaitForMeta() throws IOException
, InterruptedException
{
144 MetaTableLocator
.waitMetaRegionLocation(watcher
, 100);
148 * Test waiting on meat w/ no timeout specified.
151 public void testNoTimeoutWaitForMeta() throws IOException
, InterruptedException
, KeeperException
{
152 ServerName hsa
= MetaTableLocator
.getMetaRegionLocation(watcher
);
155 // Now test waiting on meta location getting set.
156 Thread t
= new WaitOnMetaThread();
157 startWaitAliveThenWaitItLives(t
, 1);
158 // Set a meta location.
159 MetaTableLocator
.setMetaLocation(this.watcher
, SN
, RegionState
.State
.OPEN
);
161 // Join the thread... should exit shortly.
163 // Now meta is available.
164 assertTrue(MetaTableLocator
.getMetaRegionLocation(watcher
).equals(hsa
));
167 private void startWaitAliveThenWaitItLives(final Thread t
, final int ms
) {
169 UTIL
.waitFor(2000, t
::isAlive
);
172 assertTrue("Assert " + t
.getName() + " still waiting", t
.isAlive());
178 class WaitOnMetaThread
extends Thread
{
188 } catch (InterruptedException e
) {
189 throw new RuntimeException("Failed wait", e
);
191 LOG
.info("Exiting " + getName());
194 void doWaiting() throws InterruptedException
{
197 if (MetaTableLocator
.waitMetaRegionLocation(watcher
, 10000) != null) {
201 } catch (NotAllMetaRegionsOnlineException e
) {