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.io.IOException;
024import java.util.List;
025import java.util.Set;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
031import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
032import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
033import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
037import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
038import org.apache.hadoop.hbase.util.Pair;
039import org.junit.AfterClass;
040import org.junit.Assert;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045
046@Category({MasterTests.class, MediumTests.class})
047public class TestDeadServer {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051      HBaseClassTestRule.forClass(TestDeadServer.class);
052
053  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
054
055  final ServerName hostname123 = ServerName.valueOf("127.0.0.1", 123, 3L);
056  final ServerName hostname123_2 = ServerName.valueOf("127.0.0.1", 123, 4L);
057  final ServerName hostname1234 = ServerName.valueOf("127.0.0.2", 1234, 4L);
058  final ServerName hostname12345 = ServerName.valueOf("127.0.0.2", 12345, 4L);
059
060  @BeforeClass
061  public static void setupBeforeClass() throws Exception {
062    TEST_UTIL.startMiniCluster();
063  }
064
065  @AfterClass
066  public static void tearDownAfterClass() throws Exception {
067    TEST_UTIL.shutdownMiniCluster();
068  }
069
070  @Test public void testIsDead() {
071    DeadServer ds = new DeadServer();
072    ds.putIfAbsent(hostname123);
073
074    ds.putIfAbsent(hostname1234);
075
076    ds.putIfAbsent(hostname12345);
077
078    // Already dead =       127.0.0.1,9090,112321
079    // Coming back alive =  127.0.0.1,9090,223341
080
081    final ServerName deadServer = ServerName.valueOf("127.0.0.1", 9090, 112321L);
082    assertFalse(ds.cleanPreviousInstance(deadServer));
083    ds.putIfAbsent(deadServer);
084    assertTrue(ds.isDeadServer(deadServer));
085    Set<ServerName> deadServerNames = ds.copyServerNames();
086    for (ServerName eachDeadServer : deadServerNames) {
087      Assert.assertNotNull(ds.getTimeOfDeath(eachDeadServer));
088    }
089    final ServerName deadServerHostComingAlive =
090        ServerName.valueOf("127.0.0.1", 9090, 223341L);
091    assertTrue(ds.cleanPreviousInstance(deadServerHostComingAlive));
092    assertFalse(ds.isDeadServer(deadServer));
093    assertFalse(ds.cleanPreviousInstance(deadServerHostComingAlive));
094  }
095
096  @Test
097  public void testCrashProcedureReplay() throws Exception {
098    HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
099    final ProcedureExecutor<MasterProcedureEnv> pExecutor = master.getMasterProcedureExecutor();
100    ServerCrashProcedure proc = new ServerCrashProcedure(
101      pExecutor.getEnvironment(), hostname123, false, false);
102
103    pExecutor.stop();
104    ProcedureTestingUtility.submitAndWait(pExecutor, proc);
105    assertTrue(master.getServerManager().areDeadServersInProgress());
106
107    ProcedureTestingUtility.restart(pExecutor);
108    ProcedureTestingUtility.waitProcedure(pExecutor, proc);
109    assertFalse(master.getServerManager().areDeadServersInProgress());
110  }
111
112  @Test
113  public void testSortExtract(){
114    ManualEnvironmentEdge mee = new ManualEnvironmentEdge();
115    EnvironmentEdgeManager.injectEdge(mee);
116    mee.setValue(1);
117
118    DeadServer d = new DeadServer();
119
120    d.putIfAbsent(hostname123);
121    mee.incValue(1);
122    d.putIfAbsent(hostname1234);
123    mee.incValue(1);
124    d.putIfAbsent(hostname12345);
125
126    List<Pair<ServerName, Long>> copy = d.copyDeadServersSince(2L);
127    Assert.assertEquals(2, copy.size());
128
129    Assert.assertEquals(hostname1234, copy.get(0).getFirst());
130    Assert.assertEquals(new Long(2L), copy.get(0).getSecond());
131
132    Assert.assertEquals(hostname12345, copy.get(1).getFirst());
133    Assert.assertEquals(new Long(3L), copy.get(1).getSecond());
134
135    EnvironmentEdgeManager.reset();
136  }
137
138  @Test
139  public void testClean(){
140    DeadServer d = new DeadServer();
141    d.putIfAbsent(hostname123);
142
143    d.cleanPreviousInstance(hostname12345);
144    Assert.assertFalse(d.isEmpty());
145
146    d.cleanPreviousInstance(hostname1234);
147    Assert.assertFalse(d.isEmpty());
148
149    d.cleanPreviousInstance(hostname123_2);
150    Assert.assertTrue(d.isEmpty());
151  }
152
153  @Test
154  public void testClearDeadServer(){
155    DeadServer d = new DeadServer();
156    d.putIfAbsent(hostname123);
157    d.putIfAbsent(hostname1234);
158    Assert.assertEquals(2, d.size());
159
160    d.removeDeadServer(hostname123);
161    Assert.assertEquals(1, d.size());
162    d.removeDeadServer(hostname1234);
163    Assert.assertTrue(d.isEmpty());
164
165    d.putIfAbsent(hostname1234);
166    Assert.assertFalse(d.removeDeadServer(hostname123_2));
167    Assert.assertEquals(1, d.size());
168    Assert.assertTrue(d.removeDeadServer(hostname1234));
169    Assert.assertTrue(d.isEmpty());
170  }
171}
172