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.HBaseTestingUtil;
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 HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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
073    ds.putIfAbsent(hostname1234);
074
075    ds.putIfAbsent(hostname12345);
076
077    // Already dead = 127.0.0.1,9090,112321
078    // Coming back alive = 127.0.0.1,9090,223341
079
080    final ServerName deadServer = ServerName.valueOf("127.0.0.1", 9090, 112321L);
081    assertFalse(ds.cleanPreviousInstance(deadServer));
082    ds.putIfAbsent(deadServer);
083    assertTrue(ds.isDeadServer(deadServer));
084    Set<ServerName> deadServerNames = ds.copyServerNames();
085    for (ServerName eachDeadServer : deadServerNames) {
086      Assert.assertNotNull(ds.getTimeOfDeath(eachDeadServer));
087    }
088    final ServerName deadServerHostComingAlive = ServerName.valueOf("127.0.0.1", 9090, 223341L);
089    assertTrue(ds.cleanPreviousInstance(deadServerHostComingAlive));
090    assertFalse(ds.isDeadServer(deadServer));
091    assertFalse(ds.cleanPreviousInstance(deadServerHostComingAlive));
092  }
093
094  @Test
095  public void testCrashProcedureReplay() throws Exception {
096    HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
097    final ProcedureExecutor<MasterProcedureEnv> pExecutor = master.getMasterProcedureExecutor();
098    ServerCrashProcedure proc =
099      new ServerCrashProcedure(pExecutor.getEnvironment(), hostname123, false, false);
100
101    pExecutor.stop();
102    ProcedureTestingUtility.submitAndWait(pExecutor, proc);
103    assertTrue(master.getServerManager().areDeadServersInProgress());
104
105    ProcedureTestingUtility.restart(pExecutor);
106    ProcedureTestingUtility.waitProcedure(pExecutor, proc);
107    assertFalse(master.getServerManager().areDeadServersInProgress());
108  }
109
110  @Test
111  public void testSortExtract() {
112    ManualEnvironmentEdge mee = new ManualEnvironmentEdge();
113    EnvironmentEdgeManager.injectEdge(mee);
114    mee.setValue(1);
115
116    DeadServer d = new DeadServer();
117
118    d.putIfAbsent(hostname123);
119    mee.incValue(1);
120    d.putIfAbsent(hostname1234);
121    mee.incValue(1);
122    d.putIfAbsent(hostname12345);
123
124    List<Pair<ServerName, Long>> copy = d.copyDeadServersSince(2L);
125    Assert.assertEquals(2, copy.size());
126
127    Assert.assertEquals(hostname1234, copy.get(0).getFirst());
128    Assert.assertEquals(new Long(2L), copy.get(0).getSecond());
129
130    Assert.assertEquals(hostname12345, copy.get(1).getFirst());
131    Assert.assertEquals(new Long(3L), copy.get(1).getSecond());
132
133    EnvironmentEdgeManager.reset();
134  }
135
136  @Test
137  public void testClean() {
138    DeadServer d = new DeadServer();
139    d.putIfAbsent(hostname123);
140
141    d.cleanPreviousInstance(hostname12345);
142    Assert.assertFalse(d.isEmpty());
143
144    d.cleanPreviousInstance(hostname1234);
145    Assert.assertFalse(d.isEmpty());
146
147    d.cleanPreviousInstance(hostname123_2);
148    Assert.assertTrue(d.isEmpty());
149  }
150
151  @Test
152  public void testClearDeadServer() {
153    DeadServer d = new DeadServer();
154    d.putIfAbsent(hostname123);
155    d.putIfAbsent(hostname1234);
156    Assert.assertEquals(2, d.size());
157
158    d.removeDeadServer(hostname123);
159    Assert.assertEquals(1, d.size());
160    d.removeDeadServer(hostname1234);
161    Assert.assertTrue(d.isEmpty());
162
163    d.putIfAbsent(hostname1234);
164    Assert.assertFalse(d.removeDeadServer(hostname123_2));
165    Assert.assertEquals(1, d.size());
166    Assert.assertTrue(d.removeDeadServer(hostname1234));
167    Assert.assertTrue(d.isEmpty());
168  }
169}