001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.util.List;
024import java.util.Set;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
029import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
030import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
031import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
032import org.apache.hadoop.hbase.testclassification.MasterTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
035import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
036import org.apache.hadoop.hbase.util.Pair;
037import org.junit.AfterClass;
038import org.junit.Assert;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043
044@Category({ MasterTests.class, MediumTests.class })
045public class TestDeadServer {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestDeadServer.class);
050
051  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
052
053  final ServerName hostname123 = ServerName.valueOf("127.0.0.1", 123, 3L);
054  final ServerName hostname123_2 = ServerName.valueOf("127.0.0.1", 123, 4L);
055  final ServerName hostname1234 = ServerName.valueOf("127.0.0.2", 1234, 4L);
056  final ServerName hostname12345 = ServerName.valueOf("127.0.0.2", 12345, 4L);
057
058  @BeforeClass
059  public static void setupBeforeClass() throws Exception {
060    TEST_UTIL.startMiniCluster();
061  }
062
063  @AfterClass
064  public static void tearDownAfterClass() throws Exception {
065    TEST_UTIL.shutdownMiniCluster();
066  }
067
068  @Test
069  public void testIsDead() {
070    DeadServer ds = new DeadServer();
071    ds.putIfAbsent(hostname123);
072    ds.processing(hostname123);
073    assertTrue(ds.areDeadServersInProgress());
074    ds.finish(hostname123);
075    assertFalse(ds.areDeadServersInProgress());
076
077    ds.putIfAbsent(hostname1234);
078    ds.processing(hostname1234);
079    assertTrue(ds.areDeadServersInProgress());
080    ds.finish(hostname1234);
081    assertFalse(ds.areDeadServersInProgress());
082
083    ds.putIfAbsent(hostname12345);
084    ds.processing(hostname12345);
085    assertTrue(ds.areDeadServersInProgress());
086    ds.finish(hostname12345);
087    assertFalse(ds.areDeadServersInProgress());
088
089    // Already dead = 127.0.0.1,9090,112321
090    // Coming back alive = 127.0.0.1,9090,223341
091
092    final ServerName deadServer = ServerName.valueOf("127.0.0.1", 9090, 112321L);
093    assertFalse(ds.cleanPreviousInstance(deadServer));
094    ds.putIfAbsent(deadServer);
095    assertTrue(ds.isDeadServer(deadServer));
096    Set<ServerName> deadServerNames = ds.copyServerNames();
097    for (ServerName eachDeadServer : deadServerNames) {
098      Assert.assertNotNull(ds.getTimeOfDeath(eachDeadServer));
099    }
100    final ServerName deadServerHostComingAlive = ServerName.valueOf("127.0.0.1", 9090, 223341L);
101    assertTrue(ds.cleanPreviousInstance(deadServerHostComingAlive));
102    assertFalse(ds.isDeadServer(deadServer));
103    assertFalse(ds.cleanPreviousInstance(deadServerHostComingAlive));
104  }
105
106  @Test
107  public void testCrashProcedureReplay() {
108    HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
109    final ProcedureExecutor<MasterProcedureEnv> pExecutor = master.getMasterProcedureExecutor();
110    ServerCrashProcedure proc =
111      new ServerCrashProcedure(pExecutor.getEnvironment(), hostname123, false, false);
112
113    ProcedureTestingUtility.submitAndWait(pExecutor, proc);
114
115    assertFalse(master.getServerManager().getDeadServers().areDeadServersInProgress());
116  }
117
118  @Test
119  public void testSortExtract() {
120    ManualEnvironmentEdge mee = new ManualEnvironmentEdge();
121    EnvironmentEdgeManager.injectEdge(mee);
122    mee.setValue(1);
123
124    DeadServer d = new DeadServer();
125
126    d.putIfAbsent(hostname123);
127    mee.incValue(1);
128    d.putIfAbsent(hostname1234);
129    mee.incValue(1);
130    d.putIfAbsent(hostname12345);
131
132    List<Pair<ServerName, Long>> copy = d.copyDeadServersSince(2L);
133    Assert.assertEquals(2, copy.size());
134
135    Assert.assertEquals(hostname1234, copy.get(0).getFirst());
136    Assert.assertEquals(new Long(2L), copy.get(0).getSecond());
137
138    Assert.assertEquals(hostname12345, copy.get(1).getFirst());
139    Assert.assertEquals(new Long(3L), copy.get(1).getSecond());
140
141    EnvironmentEdgeManager.reset();
142  }
143
144  @Test
145  public void testClean() {
146    DeadServer d = new DeadServer();
147    d.putIfAbsent(hostname123);
148
149    d.cleanPreviousInstance(hostname12345);
150    Assert.assertFalse(d.isEmpty());
151
152    d.cleanPreviousInstance(hostname1234);
153    Assert.assertFalse(d.isEmpty());
154
155    d.cleanPreviousInstance(hostname123_2);
156    Assert.assertTrue(d.isEmpty());
157  }
158
159  @Test
160  public void testClearDeadServer() {
161    DeadServer d = new DeadServer();
162    d.putIfAbsent(hostname123);
163    d.putIfAbsent(hostname1234);
164    Assert.assertEquals(2, d.size());
165
166    d.finish(hostname123);
167    d.removeDeadServer(hostname123);
168    Assert.assertEquals(1, d.size());
169    d.finish(hostname1234);
170    d.removeDeadServer(hostname1234);
171    Assert.assertTrue(d.isEmpty());
172
173    d.putIfAbsent(hostname1234);
174    Assert.assertFalse(d.removeDeadServer(hostname123_2));
175    Assert.assertEquals(1, d.size());
176    d.finish(hostname1234);
177    Assert.assertTrue(d.removeDeadServer(hostname1234));
178    Assert.assertTrue(d.isEmpty());
179  }
180}