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 .assignment ;
19+
20+ import static org .junit .Assert .assertEquals ;
21+ import static org .junit .Assert .assertFalse ;
22+ import static org .junit .Assert .assertTrue ;
23+
24+ import java .util .Collections ;
25+ import java .util .List ;
26+ import java .util .Map ;
27+ import java .util .Set ;
28+ import java .util .concurrent .Future ;
29+
30+ import org .apache .hadoop .hbase .HBaseClassTestRule ;
31+ import org .apache .hadoop .hbase .ServerName ;
32+ import org .apache .hadoop .hbase .TableName ;
33+ import org .apache .hadoop .hbase .client .RegionInfo ;
34+ import org .apache .hadoop .hbase .client .RegionInfoBuilder ;
35+ import org .apache .hadoop .hbase .testclassification .MasterTests ;
36+ import org .apache .hadoop .hbase .testclassification .MediumTests ;
37+ import org .apache .hadoop .hbase .util .Pair ;
38+ import org .junit .ClassRule ;
39+ import org .junit .Test ;
40+ import org .junit .experimental .categories .Category ;
41+ import org .slf4j .Logger ;
42+ import org .slf4j .LoggerFactory ;
43+
44+ @ Category ({ MasterTests .class , MediumTests .class })
45+ public class TestAMProblematicRegions extends TestAssignmentManagerBase {
46+ private static final Logger LOG = LoggerFactory .getLogger (TestAMProblematicRegions .class );
47+
48+ @ ClassRule
49+ public static final HBaseClassTestRule CLASS_RULE =
50+ HBaseClassTestRule .forClass (TestAMProblematicRegions .class );
51+
52+ @ Test
53+ public void testForMeta () {
54+ byte [] metaRegionNameAsBytes = RegionInfoBuilder .FIRST_META_REGIONINFO .getRegionName ();
55+ String metaRegionName = RegionInfoBuilder .FIRST_META_REGIONINFO .getRegionNameAsString ();
56+ List <ServerName > serverNames = master .getServerManager ().getOnlineServersList ();
57+ assertEquals (NSERVERS , serverNames .size ());
58+
59+ Map <String , Pair <ServerName , Set <ServerName >>> problematicRegions = am .getProblematicRegions ();
60+
61+ // Test for case1: Master thought this region opened, but no regionserver reported it.
62+ assertTrue (problematicRegions .containsKey (metaRegionName ));
63+ Pair <ServerName , Set <ServerName >> pair = problematicRegions .get (metaRegionName );
64+ ServerName locationInMeta = pair .getFirst ();
65+ Set <ServerName > reportedRegionServers = pair .getSecond ();
66+ assertTrue (serverNames .contains (locationInMeta ));
67+ assertEquals (0 , reportedRegionServers .size ());
68+
69+ // Reported right region location. Then not in problematic regions.
70+ am .reportOnlineRegions (locationInMeta , Collections .singleton (metaRegionNameAsBytes ));
71+ problematicRegions = am .getProblematicRegions ();
72+ assertFalse (problematicRegions .containsKey (metaRegionName ));
73+ }
74+
75+ @ Test
76+ public void testForUserTable () throws Exception {
77+ TableName tableName = TableName .valueOf ("testForUserTable" );
78+ RegionInfo hri = createRegionInfo (tableName , 1 );
79+ String regionName = hri .getRegionNameAsString ();
80+ rsDispatcher .setMockRsExecutor (new GoodRsExecutor ());
81+ Future <byte []> future = submitProcedure (createAssignProcedure (hri ));
82+ waitOnFuture (future );
83+
84+ List <ServerName > serverNames = master .getServerManager ().getOnlineServersList ();
85+ assertEquals (NSERVERS , serverNames .size ());
86+
87+ // Test for case1: Master thought this region opened, but no regionserver reported it.
88+ Map <String , Pair <ServerName , Set <ServerName >>> problematicRegions = am .getProblematicRegions ();
89+ assertTrue (problematicRegions .containsKey (regionName ));
90+ Pair <ServerName , Set <ServerName >> pair = problematicRegions .get (regionName );
91+ ServerName locationInMeta = pair .getFirst ();
92+ Set <ServerName > reportedRegionServers = pair .getSecond ();
93+ assertTrue (serverNames .contains (locationInMeta ));
94+ assertEquals (0 , reportedRegionServers .size ());
95+
96+ // Test for case2: Master thought this region opened on Server1, but regionserver reported
97+ // Server2
98+ final ServerName tempLocationInMeta = locationInMeta ;
99+ final ServerName anotherServer =
100+ serverNames .stream ().filter (s -> !s .equals (tempLocationInMeta )).findFirst ().get ();
101+ am .reportOnlineRegions (anotherServer , Collections .singleton (hri .getRegionName ()));
102+ problematicRegions = am .getProblematicRegions ();
103+ assertTrue (problematicRegions .containsKey (regionName ));
104+ pair = problematicRegions .get (regionName );
105+ locationInMeta = pair .getFirst ();
106+ reportedRegionServers = pair .getSecond ();
107+ assertEquals (1 , reportedRegionServers .size ());
108+ assertFalse (reportedRegionServers .contains (locationInMeta ));
109+ assertTrue (reportedRegionServers .contains (anotherServer ));
110+
111+ // Test for case3: More than one regionservers reported opened this region.
112+ am .reportOnlineRegions (locationInMeta , Collections .singleton (hri .getRegionName ()));
113+ problematicRegions = am .getProblematicRegions ();
114+ assertTrue (problematicRegions .containsKey (regionName ));
115+ pair = problematicRegions .get (regionName );
116+ locationInMeta = pair .getFirst ();
117+ reportedRegionServers = pair .getSecond ();
118+ assertEquals (2 , reportedRegionServers .size ());
119+ assertTrue (reportedRegionServers .contains (locationInMeta ));
120+ assertTrue (reportedRegionServers .contains (anotherServer ));
121+
122+ // Reported right region location. Then not in problematic regions.
123+ am .reportOnlineRegions (anotherServer , Collections .EMPTY_SET );
124+ problematicRegions = am .getProblematicRegions ();
125+ assertFalse (problematicRegions .containsKey (regionName ));
126+ }
127+ }
0 commit comments