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  
26  import com.google.common.collect.Lists;
27  
28  import java.io.IOException;
29  import java.util.List;
30  import java.util.Set;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.hbase.HColumnDescriptor;
35  import org.apache.hadoop.hbase.HTableDescriptor;
36  import org.apache.hadoop.hbase.NamespaceDescriptor;
37  import org.apache.hadoop.hbase.ServerName;
38  import org.apache.hadoop.hbase.TableName;
39  import org.apache.hadoop.hbase.Waiter;
40  import org.apache.hadoop.hbase.client.ClusterConnection;
41  import org.apache.hadoop.hbase.master.HMaster;
42  import org.apache.hadoop.hbase.net.Address;
43  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
44  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
45  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetServerInfoRequest;
46  import org.apache.hadoop.hbase.testclassification.MediumTests;
47  import org.apache.hadoop.hbase.util.Bytes;
48  import org.junit.After;
49  import org.junit.AfterClass;
50  import org.junit.Assert;
51  import org.junit.Before;
52  import org.junit.BeforeClass;
53  import org.junit.Test;
54  import org.junit.experimental.categories.Category;
55  
56  @Category({MediumTests.class})
57  public class TestRSGroupsBasics extends TestRSGroupsBase {
58    protected static final Log LOG = LogFactory.getLog(TestRSGroupsBasics.class);
59  
60    @BeforeClass
61    public static void setUp() throws Exception {
62      setUpTestBeforeClass();
63    }
64  
65    @AfterClass
66    public static void tearDown() throws Exception {
67      tearDownAfterClass();
68    }
69  
70    @Before
71    public void beforeMethod() throws Exception {
72      setUpBeforeMethod();
73    }
74  
75    @After
76    public void afterMethod() throws Exception {
77      tearDownAfterMethod();
78    }
79  
80    @Test
81    public void testBasicStartUp() throws IOException {
82      RSGroupInfo defaultInfo = rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP);
83      assertEquals(4, defaultInfo.getServers().size());
84      // Assignment of root and meta regions.
85      int count = master.getAssignmentManager().getRegionStates().getRegionAssignments().size();
86      //3 meta,namespace, group
87      assertEquals(3, count);
88    }
89  
90    @Test
91    public void testCreateAndDrop() throws Exception {
92      LOG.info("testCreateAndDrop");
93      final TableName tableName = TableName.valueOf(tablePrefix + "_testCreateAndDrop");
94      TEST_UTIL.createTable(tableName, Bytes.toBytes("cf"));
95      //wait for created table to be assigned
96      TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
97        @Override
98        public boolean evaluate() throws Exception {
99          return getTableRegionMap().get(tableName) != null;
100       }
101     });
102     TEST_UTIL.deleteTable(tableName);
103   }
104 
105   @Test
106   public void testCreateMultiRegion() throws IOException {
107     LOG.info("testCreateMultiRegion");
108     TableName tableName = TableName.valueOf(tablePrefix + "_testCreateMultiRegion");
109     byte[] end = {1,3,5,7,9};
110     byte[] start = {0,2,4,6,8};
111     byte[][] f = {Bytes.toBytes("f")};
112     TEST_UTIL.createTable(tableName, f,1,start,end,10);
113   }
114 
115   @Test
116   public void testNamespaceCreateAndAssign() throws Exception {
117     LOG.info("testNamespaceCreateAndAssign");
118     String nsName = tablePrefix+"_foo";
119     final TableName tableName = TableName.valueOf(nsName, tablePrefix + "_testCreateAndAssign");
120     RSGroupInfo appInfo = addGroup(rsGroupAdmin, "appInfo", 1);
121     admin.createNamespace(NamespaceDescriptor.create(nsName)
122         .addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, "appInfo").build());
123     final HTableDescriptor desc = new HTableDescriptor(tableName);
124     desc.addFamily(new HColumnDescriptor("f"));
125     admin.createTable(desc);
126     //wait for created table to be assigned
127     TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
128       @Override
129       public boolean evaluate() throws Exception {
130         return getTableRegionMap().get(desc.getTableName()) != null;
131       }
132     });
133     ServerName targetServer =
134         ServerName.parseServerName(appInfo.getServers().iterator().next().toString());
135     AdminProtos.AdminService.BlockingInterface rs = admin.getConnection().getAdmin(targetServer);
136     //verify it was assigned to the right group
137     Assert.assertEquals(1, ProtobufUtil.getOnlineRegions(rs).size());
138   }
139 
140   @Test
141   public void testDefaultNamespaceCreateAndAssign() throws Exception {
142     LOG.info("testDefaultNamespaceCreateAndAssign");
143     final byte[] tableName = Bytes.toBytes(tablePrefix + "_testCreateAndAssign");
144     admin.modifyNamespace(NamespaceDescriptor.create("default")
145         .addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, "default").build());
146     final HTableDescriptor desc = new HTableDescriptor(tableName);
147     desc.addFamily(new HColumnDescriptor("f"));
148     admin.createTable(desc);
149     //wait for created table to be assigned
150     TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
151       @Override
152       public boolean evaluate() throws Exception {
153         return getTableRegionMap().get(desc.getTableName()) != null;
154       }
155     });
156   }
157 
158   @Test
159   public void testCloneSnapshot() throws Exception {
160     final TableName tableName = TableName.valueOf(tablePrefix+"_testCloneSnapshot");
161     LOG.info("testCloneSnapshot");
162 
163     byte[] FAMILY = Bytes.toBytes("test");
164     String snapshotName = tableName.getNameAsString() + "_snap";
165     TableName clonedTableName = TableName.valueOf(tableName.getNameAsString() + "_clone");
166 
167     // create base table
168     TEST_UTIL.createTable(tableName, FAMILY);
169 
170     // create snapshot
171     admin.snapshot(snapshotName, tableName);
172 
173     // clone
174     admin.cloneSnapshot(snapshotName, clonedTableName);
175   }
176 
177   @Test
178   public void testClearDeadServers() throws Exception {
179     final RSGroupInfo newGroup = addGroup(rsGroupAdmin, "testClearDeadServers", 3);
180 
181     ServerName targetServer = ServerName.parseServerName(
182         newGroup.getServers().iterator().next().toString());
183     AdminProtos.AdminService.BlockingInterface targetRS =
184         ((ClusterConnection) admin.getConnection()).getAdmin(targetServer);
185     try {
186       targetServer = ProtobufUtil.toServerName(targetRS.getServerInfo(null,
187           GetServerInfoRequest.newBuilder().build()).getServerInfo().getServerName());
188       //stopping may cause an exception
189       //due to the connection loss
190       targetRS.stopServer(null,
191           AdminProtos.StopServerRequest.newBuilder().setReason("Die").build());
192     } catch(Exception e) {
193     }
194     final HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
195     //wait for stopped regionserver to dead server list
196     TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
197       @Override
198       public boolean evaluate() throws Exception {
199         return !master.getServerManager().areDeadServersInProgress()
200             && cluster.getClusterStatus().getDeadServerNames().size() > 0;
201       }
202     });
203     assertFalse(cluster.getClusterStatus().getServers().contains(targetServer));
204     assertTrue(cluster.getClusterStatus().getDeadServerNames().contains(targetServer));
205     assertTrue(newGroup.getServers().contains(targetServer.getAddress()));
206 
207     //clear dead servers list
208     List<ServerName> notClearedServers = admin.clearDeadServers(Lists.newArrayList(targetServer));
209     assertEquals(0, notClearedServers.size());
210 
211     Set<Address> newGroupServers = rsGroupAdmin.getRSGroupInfo(newGroup.getName()).getServers();
212     assertFalse(newGroupServers.contains(targetServer.getAddress()));
213     assertEquals(2, newGroupServers.size());
214   }
215 }