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.assertFalse;
23  
24  import com.google.common.collect.Sets;
25  
26  import java.util.Set;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.hbase.HColumnDescriptor;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.NamespaceDescriptor;
33  import org.apache.hadoop.hbase.ServerName;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.Waiter;
36  import org.apache.hadoop.hbase.net.Address;
37  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
38  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
39  import org.apache.hadoop.hbase.testclassification.MediumTests;
40  import org.junit.After;
41  import org.junit.AfterClass;
42  import org.junit.Assert;
43  import org.junit.Before;
44  import org.junit.BeforeClass;
45  import org.junit.Test;
46  import org.junit.experimental.categories.Category;
47  
48  @Category({MediumTests.class})
49  public class TestRSGroupsKillRS extends TestRSGroupsBase {
50    protected static final Log LOG = LogFactory.getLog(TestRSGroupsKillRS.class);
51  
52    @BeforeClass
53    public static void setUp() throws Exception {
54      setUpTestBeforeClass();
55    }
56  
57    @AfterClass
58    public static void tearDown() throws Exception {
59      tearDownAfterClass();
60    }
61  
62    @Before
63    public void beforeMethod() throws Exception {
64      setUpBeforeMethod();
65    }
66  
67    @After
68    public void afterMethod() throws Exception {
69      tearDownAfterMethod();
70    }
71  
72    @Test
73    public void testKillRS() throws Exception {
74      LOG.info("testKillRS");
75      RSGroupInfo appInfo = addGroup(rsGroupAdmin, "appInfo", 1);
76  
77      final TableName tableName = TableName.valueOf(tablePrefix+"_ns", "_testKillRS");
78      admin.createNamespace(
79          NamespaceDescriptor.create(tableName.getNamespaceAsString())
80              .addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, appInfo.getName()).build());
81      final HTableDescriptor desc = new HTableDescriptor(tableName);
82      desc.addFamily(new HColumnDescriptor("f"));
83      admin.createTable(desc);
84      //wait for created table to be assigned
85      TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
86        @Override
87        public boolean evaluate() throws Exception {
88          return getTableRegionMap().get(desc.getTableName()) != null;
89        }
90      });
91  
92      ServerName targetServer = ServerName.parseServerName(
93          appInfo.getServers().iterator().next().toString());
94      AdminProtos.AdminService.BlockingInterface targetRS =
95          admin.getConnection().getAdmin(targetServer);
96      Assert.assertEquals(1, ProtobufUtil.getOnlineRegions(targetRS).size());
97  
98      try {
99        //stopping may cause an exception
100       //due to the connection loss
101       targetRS.stopServer(null,
102           AdminProtos.StopServerRequest.newBuilder().setReason("Die").build());
103     } catch(Exception e) {
104     }
105     assertFalse(cluster.getClusterStatus().getServers().contains(targetServer));
106 
107     //wait for created table to be assigned
108     TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
109       @Override
110       public boolean evaluate() throws Exception {
111         return cluster.getClusterStatus().getRegionsInTransition().size() == 0;
112       }
113     });
114     Set<Address> newServers = Sets.newHashSet();
115     newServers.add(
116         rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP).getServers().iterator().next());
117     rsGroupAdmin.moveServers(newServers, appInfo.getName());
118 
119     //Make sure all the table's regions get reassigned
120     //disabling the table guarantees no conflicting assign/unassign (ie SSH) happens
121     admin.disableTable(tableName);
122     admin.enableTable(tableName);
123 
124     //wait for region to be assigned
125     TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
126       @Override
127       public boolean evaluate() throws Exception {
128         return cluster.getClusterStatus().getRegionsInTransition().size() == 0;
129       }
130     });
131 
132     targetServer = ServerName.parseServerName(
133         newServers.iterator().next().toString());
134     targetRS =
135         admin.getConnection().getAdmin(targetServer);
136     Assert.assertEquals(1, ProtobufUtil.getOnlineRegions(targetRS).size());
137     Assert.assertEquals(tableName,
138         ProtobufUtil.getOnlineRegions(targetRS).get(0).getTable());
139   }
140 
141 }