View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.rsgroup;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import java.io.IOException;
28  import java.util.Iterator;
29  import java.util.Set;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.HColumnDescriptor;
35  import org.apache.hadoop.hbase.HConstants;
36  import org.apache.hadoop.hbase.HTableDescriptor;
37  import org.apache.hadoop.hbase.MiniHBaseCluster;
38  import org.apache.hadoop.hbase.NamespaceDescriptor;
39  import org.apache.hadoop.hbase.ServerName;
40  import org.apache.hadoop.hbase.TableName;
41  import org.apache.hadoop.hbase.Waiter;
42  import org.apache.hadoop.hbase.Waiter.Predicate;
43  import org.apache.hadoop.hbase.coprocessor.BaseMasterObserver;
44  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
45  import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
46  import org.apache.hadoop.hbase.coprocessor.MasterObserver;
47  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
48  import org.apache.hadoop.hbase.master.HMaster;
49  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
50  import org.apache.hadoop.hbase.master.ServerManager;
51  import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
52  import org.apache.hadoop.hbase.net.Address;
53  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
54  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
55  import org.apache.hadoop.hbase.testclassification.MediumTests;
56  import org.apache.hadoop.hbase.util.Bytes;
57  import org.junit.After;
58  import org.junit.AfterClass;
59  import org.junit.Assert;
60  import org.junit.Before;
61  import org.junit.BeforeClass;
62  import org.junit.Test;
63  import org.junit.experimental.categories.Category;
64  
65  import com.google.common.collect.Sets;
66  
67  @Category({MediumTests.class})
68  public class TestRSGroups extends TestRSGroupsBase {
69    protected static final Log LOG = LogFactory.getLog(TestRSGroups.class);
70    private static HMaster master;
71    private static boolean init = false;
72    private static RSGroupAdminEndpoint RSGroupAdminEndpoint;
73    private static CPMasterObserver observer;
74  
75    @BeforeClass
76    public static void setUp() throws Exception {
77      TEST_UTIL = new HBaseTestingUtility();
78      TEST_UTIL.getConfiguration().setFloat(
79              "hbase.master.balancer.stochastic.tableSkewCost", 6000);
80      TEST_UTIL.getConfiguration().set(
81          HConstants.HBASE_MASTER_LOADBALANCER_CLASS,
82          RSGroupBasedLoadBalancer.class.getName());
83      TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
84          RSGroupAdminEndpoint.class.getName() + "," + CPMasterObserver.class.getName());
85      TEST_UTIL.getConfiguration().setBoolean(
86          HConstants.ZOOKEEPER_USEMULTI,
87          true);
88      TEST_UTIL.startMiniCluster(NUM_SLAVES_BASE - 1);
89      TEST_UTIL.getConfiguration().setInt(
90          ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART,
91          NUM_SLAVES_BASE - 1);
92      TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
93  
94      admin = TEST_UTIL.getHBaseAdmin();
95      cluster = TEST_UTIL.getHBaseCluster();
96      master = ((MiniHBaseCluster)cluster).getMaster();
97  
98      //wait for balancer to come online
99      TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
100       @Override
101       public boolean evaluate() throws Exception {
102         return master.isInitialized() &&
103             ((RSGroupBasedLoadBalancer) master.getLoadBalancer()).isOnline();
104       }
105     });
106     admin.setBalancerRunning(false,true);
107     rsGroupAdmin = new VerifyingRSGroupAdminClient(new RSGroupAdminClient(TEST_UTIL.getConnection()),
108         TEST_UTIL.getConfiguration());
109     MasterCoprocessorHost host = master.getMasterCoprocessorHost();
110     observer = (CPMasterObserver) host.findCoprocessor(CPMasterObserver.class.getName());
111     RSGroupAdminEndpoint =
112         host.findCoprocessors(RSGroupAdminEndpoint.class).get(0);
113   }
114 
115   @AfterClass
116   public static void tearDown() throws Exception {
117     TEST_UTIL.shutdownMiniCluster();
118   }
119 
120   @Before
121   public void beforeMethod() throws Exception {
122     if(!init) {
123       init = true;
124       afterMethod();
125     }
126 
127   }
128 
129   @After
130   public void afterMethod() throws Exception {
131     deleteTableIfNecessary();
132     deleteNamespaceIfNecessary();
133     deleteGroups();
134 
135     int missing = NUM_SLAVES_BASE - getNumServers();
136     LOG.info("Restoring servers: "+missing);
137     for(int i=0; i<missing; i++) {
138       ((MiniHBaseCluster)cluster).startRegionServer();
139     }
140 
141     rsGroupAdmin.addRSGroup("master");
142     ServerName masterServerName =
143         ((MiniHBaseCluster)cluster).getMaster().getServerName();
144 
145     try {
146       rsGroupAdmin.moveServers(
147           Sets.newHashSet(masterServerName.getAddress()),
148           "master");
149     } catch (Exception ex) {
150       // ignore
151     }
152     assertTrue(observer.preMoveServersCalled);
153     TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
154       @Override
155       public boolean evaluate() throws Exception {
156         LOG.info("Waiting for cleanup to finish " + rsGroupAdmin.listRSGroups());
157         //Might be greater since moving servers back to default
158         //is after starting a server
159 
160         return rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP).getServers().size()
161             == NUM_SLAVES_BASE;
162       }
163     });
164   }
165 
166   @Test
167   public void testBasicStartUp() throws IOException {
168     RSGroupInfo defaultInfo = rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP);
169     assertEquals(4, defaultInfo.getServers().size());
170     // Assignment of root and meta regions.
171     int count = master.getAssignmentManager().getRegionStates().getRegionAssignments().size();
172     //3 meta,namespace, group
173     assertEquals(3, count);
174   }
175 
176   @Test
177   public void testNamespaceCreateAndAssign() throws Exception {
178     LOG.info("testNamespaceCreateAndAssign");
179     String nsName = tablePrefix+"_foo";
180     final TableName tableName = TableName.valueOf(nsName, tablePrefix + "_testCreateAndAssign");
181     RSGroupInfo appInfo = addGroup(rsGroupAdmin, "appInfo", 1);
182     admin.createNamespace(NamespaceDescriptor.create(nsName)
183         .addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, "appInfo").build());
184     final HTableDescriptor desc = new HTableDescriptor(tableName);
185     desc.addFamily(new HColumnDescriptor("f"));
186     admin.createTable(desc);
187     //wait for created table to be assigned
188     TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
189       @Override
190       public boolean evaluate() throws Exception {
191         return getTableRegionMap().get(desc.getTableName()) != null;
192       }
193     });
194     ServerName targetServer =
195         ServerName.parseServerName(appInfo.getServers().iterator().next().toString());
196     AdminProtos.AdminService.BlockingInterface rs = admin.getConnection().getAdmin(targetServer);
197     //verify it was assigned to the right group
198     Assert.assertEquals(1, ProtobufUtil.getOnlineRegions(rs).size());
199   }
200 
201   @Test
202   public void testDefaultNamespaceCreateAndAssign() throws Exception {
203     LOG.info("testDefaultNamespaceCreateAndAssign");
204     final byte[] tableName = Bytes.toBytes(tablePrefix + "_testCreateAndAssign");
205     admin.modifyNamespace(NamespaceDescriptor.create("default")
206         .addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, "default").build());
207     final HTableDescriptor desc = new HTableDescriptor(tableName);
208     desc.addFamily(new HColumnDescriptor("f"));
209     admin.createTable(desc);
210     //wait for created table to be assigned
211     TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
212       @Override
213       public boolean evaluate() throws Exception {
214         return getTableRegionMap().get(desc.getTableName()) != null;
215       }
216     });
217   }
218 
219   @Test
220   public void testNamespaceConstraint() throws Exception {
221     String nsName = tablePrefix+"_foo";
222     String groupName = tablePrefix+"_foo";
223     LOG.info("testNamespaceConstraint");
224     rsGroupAdmin.addRSGroup(groupName);
225     assertTrue(observer.preAddRSGroupCalled);
226     assertTrue(observer.postAddRSGroupCalled);
227 
228     admin.createNamespace(NamespaceDescriptor.create(nsName)
229         .addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, groupName)
230         .build());
231     //test removing a referenced group
232     try {
233       rsGroupAdmin.removeRSGroup(groupName);
234       fail("Expected a constraint exception");
235     } catch (IOException ex) {
236     }
237     //test modify group
238     //changing with the same name is fine
239     admin.modifyNamespace(
240         NamespaceDescriptor.create(nsName)
241           .addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, groupName)
242           .build());
243     String anotherGroup = tablePrefix+"_anotherGroup";
244     rsGroupAdmin.addRSGroup(anotherGroup);
245     //test add non-existent group
246     admin.deleteNamespace(nsName);
247     rsGroupAdmin.removeRSGroup(groupName);
248     assertTrue(observer.preRemoveRSGroupCalled);
249     assertTrue(observer.postRemoveRSGroupCalled);
250     try {
251       admin.createNamespace(NamespaceDescriptor.create(nsName)
252           .addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, "foo")
253           .build());
254       fail("Expected a constraint exception");
255     } catch (IOException ex) {
256     }
257   }
258 
259   @Test
260   public void testGroupInfoMultiAccessing() throws Exception {
261     RSGroupInfoManager manager = RSGroupAdminEndpoint.getGroupInfoManager();
262     final RSGroupInfo defaultGroup = manager.getRSGroup("default");
263     // getRSGroup updates default group's server list
264     // this process must not affect other threads iterating the list
265     Iterator<Address> it = defaultGroup.getServers().iterator();
266     manager.getRSGroup("default");
267     it.next();
268   }
269 
270   public static class CPMasterObserver extends BaseMasterObserver {
271     boolean preBalanceRSGroupCalled = false;
272     boolean postBalanceRSGroupCalled = false;
273     boolean preMoveServersCalled = false;
274     boolean postMoveServersCalled = false;
275     boolean preMoveTablesCalled = false;
276     boolean postMoveTablesCalled = false;
277     boolean preAddRSGroupCalled = false;
278     boolean postAddRSGroupCalled = false;
279     boolean preRemoveRSGroupCalled = false;
280     boolean postRemoveRSGroupCalled = false;
281     boolean preRemoveServersCalled = false;
282     boolean postRemoveServersCalled = false;
283     boolean preMoveServersAndTables = false;
284     boolean postMoveServersAndTables = false;
285 
286     @Override
287     public void preMoveServersAndTables(final ObserverContext<MasterCoprocessorEnvironment> ctx,
288         Set<Address> servers, Set<TableName> tables, String targetGroup) throws IOException {
289       preMoveServersAndTables = true;
290     }
291     @Override
292     public void postMoveServersAndTables(final ObserverContext<MasterCoprocessorEnvironment> ctx,
293         Set<Address> servers, Set<TableName> tables, String targetGroup) throws IOException {
294       postMoveServersAndTables = true;
295     }
296     @Override
297     public void preRemoveServers(
298         final ObserverContext<MasterCoprocessorEnvironment> ctx,
299         Set<Address> servers) throws IOException {
300       preRemoveServersCalled = true;
301     }
302     @Override
303     public void postRemoveServers(
304         final ObserverContext<MasterCoprocessorEnvironment> ctx,
305         Set<Address> servers) throws IOException {
306       postRemoveServersCalled = true;
307     }
308     @Override
309     public void preRemoveRSGroup(final ObserverContext<MasterCoprocessorEnvironment> ctx,
310         String name) throws IOException {
311       preRemoveRSGroupCalled = true;
312     }
313     @Override
314     public void postRemoveRSGroup(final ObserverContext<MasterCoprocessorEnvironment> ctx,
315         String name) throws IOException {
316       postRemoveRSGroupCalled = true;
317     }
318     @Override
319     public void preAddRSGroup(final ObserverContext<MasterCoprocessorEnvironment> ctx,
320         String name) throws IOException {
321       preAddRSGroupCalled = true;
322     }
323     @Override
324     public void postAddRSGroup(final ObserverContext<MasterCoprocessorEnvironment> ctx,
325         String name) throws IOException {
326       postAddRSGroupCalled = true;
327     }
328     @Override
329     public void preMoveTables(final ObserverContext<MasterCoprocessorEnvironment> ctx,
330         Set<TableName> tables, String targetGroup) throws IOException {
331       preMoveTablesCalled = true;
332     }
333     @Override
334     public void postMoveTables(final ObserverContext<MasterCoprocessorEnvironment> ctx,
335         Set<TableName> tables, String targetGroup) throws IOException {
336       postMoveTablesCalled = true;
337     }
338     @Override
339     public void preMoveServers(final ObserverContext<MasterCoprocessorEnvironment> ctx,
340         Set<Address> servers, String targetGroup) throws IOException {
341       preMoveServersCalled = true;
342     }
343 
344     @Override
345     public void postMoveServers(final ObserverContext<MasterCoprocessorEnvironment> ctx,
346         Set<Address> servers, String targetGroup) throws IOException {
347       postMoveServersCalled = true;
348     }
349     @Override
350     public void preBalanceRSGroup(final ObserverContext<MasterCoprocessorEnvironment> ctx,
351         String groupName) throws IOException {
352       preBalanceRSGroupCalled = true;
353     }
354     @Override
355     public void postBalanceRSGroup(final ObserverContext<MasterCoprocessorEnvironment> ctx,
356         String groupName, boolean balancerRan) throws IOException {
357       postBalanceRSGroupCalled = true;
358     }
359   }
360   @Test
361   public void testMoveServersAndTables() throws Exception {
362     super.testMoveServersAndTables();
363     assertTrue(observer.preMoveServersAndTables);
364     assertTrue(observer.postMoveServersAndTables);
365   }
366   @Test
367   public void testTableMoveTruncateAndDrop() throws Exception {
368     super.testTableMoveTruncateAndDrop();
369     assertTrue(observer.preMoveTablesCalled);
370     assertTrue(observer.postMoveTablesCalled);
371   }
372 
373   @Test
374   public void testRemoveServers() throws Exception {
375     super.testRemoveServers();
376     assertTrue(observer.preRemoveServersCalled);
377   }
378 
379   @Test
380   public void testGroupBalance() throws Exception {
381     super.testGroupBalance();
382     assertTrue(observer.preBalanceRSGroupCalled);
383     assertTrue(observer.postBalanceRSGroupCalled);
384   }
385 
386   @Test
387   public void testMisplacedRegions() throws Exception {
388     final TableName tableName = TableName.valueOf(tablePrefix+"_testMisplacedRegions");
389     LOG.info("testMisplacedRegions");
390 
391     final RSGroupInfo RSGroupInfo = addGroup(rsGroupAdmin, "testMisplacedRegions", 1);
392 
393     TEST_UTIL.createMultiRegionTable(tableName, new byte[]{'f'}, 15);
394     TEST_UTIL.waitUntilAllRegionsAssigned(tableName);
395 
396     RSGroupAdminEndpoint.getGroupInfoManager()
397         .moveTables(Sets.newHashSet(tableName), RSGroupInfo.getName());
398 
399     assertTrue(rsGroupAdmin.balanceRSGroup(RSGroupInfo.getName()));
400 
401     TEST_UTIL.waitFor(60000, new Predicate<Exception>() {
402       @Override
403       public boolean evaluate() throws Exception {
404         ServerName serverName =
405             ServerName.valueOf(RSGroupInfo.getServers().iterator().next().toString(), 1);
406         return admin.getConnection().getAdmin()
407             .getOnlineRegions(serverName).size() == 15;
408       }
409     });
410   }
411 
412   @Test
413   public void testRSGroupBalancerSwitch() throws IOException {
414     //Balancer is initially off in the test, set to true and check
415     assertFalse(admin.setBalancerRunning(true, true));
416     assertTrue(admin.isBalancerEnabled());
417     //Set balancer off and check if it actually turned off
418     assertTrue(admin.setBalancerRunning(false,true));
419     assertFalse(admin.isBalancerEnabled());
420   }
421 
422   @Test
423   public void testCloneSnapshot() throws Exception {
424     final TableName tableName = TableName.valueOf(tablePrefix+"_testCloneSnapshot");
425     LOG.info("testCloneSnapshot");
426 
427     byte[] FAMILY = Bytes.toBytes("test");
428     String snapshotName = tableName.getNameAsString() + "_snap";
429     TableName clonedTableName = TableName.valueOf(tableName.getNameAsString() + "_clone");
430 
431     // create base table
432     TEST_UTIL.createTable(tableName, FAMILY);
433 
434     // create snapshot
435     admin.snapshot(snapshotName, tableName);
436 
437     // clone
438     admin.cloneSnapshot(snapshotName, clonedTableName);
439   }
440 
441 }