View Javadoc

1   /**
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.HRegionInfo;
22  import org.apache.hadoop.hbase.Server;
23  import org.apache.hadoop.hbase.ServerName;
24  import org.apache.hadoop.hbase.TableName;
25  import org.apache.hadoop.hbase.TableStateManager;
26  import org.apache.hadoop.hbase.testclassification.SmallTests;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  import java.util.Arrays;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.UUID;
34  
35  import static junit.framework.Assert.assertFalse;
36  import static org.mockito.Matchers.isA;
37  import static org.mockito.Mockito.mock;
38  import static org.mockito.Mockito.when;
39  
40  @Category(SmallTests.class)
41  public class TestRegionStates {
42  
43  
44    @Test
45    public void testWeDontReturnDrainingServersForOurBalancePlans() throws Exception {
46      Server server = mock(Server.class);
47      when(server.getServerName()).thenReturn(ServerName.valueOf("master,1,1"));
48      Configuration configuration = mock(Configuration.class);
49      when(server.getConfiguration()).thenReturn(configuration);
50      TableStateManager tsm = mock(TableStateManager.class);
51      ServerManager sm = mock(ServerManager.class);
52      when(sm.isServerOnline(isA(ServerName.class))).thenReturn(true);
53  
54      RegionStateStore rss = mock(RegionStateStore.class);
55      RegionStates regionStates = new RegionStates(server, tsm, sm, rss);
56  
57      ServerName one = mockServer("one", 1);
58      ServerName two = mockServer("two", 1);
59      ServerName three = mockServer("three", 1);
60  
61      when(sm.getDrainingServersList()).thenReturn(Arrays.asList(three));
62  
63      regionStates.regionOnline(createFakeRegion(), one);
64      regionStates.regionOnline(createFakeRegion(), two);
65      regionStates.regionOnline(createFakeRegion(), three);
66  
67  
68      Map<TableName, Map<ServerName, List<HRegionInfo>>> result =
69          regionStates.getAssignmentsByTable();
70      for (Map<ServerName, List<HRegionInfo>> map : result.values()) {
71        assertFalse(map.keySet().contains(three));
72      }
73    }
74  
75    private HRegionInfo createFakeRegion() {
76      HRegionInfo info = mock(HRegionInfo.class);
77      when(info.getEncodedName()).thenReturn(UUID.randomUUID().toString());
78      return info;
79    }
80  
81    private ServerName mockServer(String fakeHost, int fakePort) {
82      ServerName serverName = mock(ServerName.class);
83      when(serverName.getHostname()).thenReturn(fakeHost);
84      when(serverName.getPort()).thenReturn(fakePort);
85      return serverName;
86    }
87  }