View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.master;
21  
22  
23  import org.apache.hadoop.hbase.testclassification.MediumTests;
24  import org.apache.hadoop.hbase.ServerName;
25  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
26  import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
27  import org.apache.hadoop.hbase.util.Pair;
28  import org.junit.Assert;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  @Category(MediumTests.class) // Plays with the ManualEnvironmentEdge
37  public class TestClusterStatusPublisher {
38    private ManualEnvironmentEdge mee = new ManualEnvironmentEdge();
39  
40    @Before
41    public void before() {
42      mee.setValue(0);
43      EnvironmentEdgeManager.injectEdge(mee);
44    }
45  
46    @Test
47    public void testEmpty() {
48      ClusterStatusPublisher csp = new ClusterStatusPublisher() {
49        @Override
50        protected List<Pair<ServerName, Long>> getDeadServers(long since) {
51          return new ArrayList<Pair<ServerName, Long>>();
52        }
53      };
54  
55      Assert.assertTrue(csp.generateDeadServersListToSend().isEmpty());
56    }
57  
58    @Test
59    public void testMaxSend() {
60      ClusterStatusPublisher csp = new ClusterStatusPublisher() {
61        @Override
62        protected List<Pair<ServerName, Long>> getDeadServers(long since) {
63          List<Pair<ServerName, Long>> res = new ArrayList<Pair<ServerName, Long>>();
64          switch ((int) EnvironmentEdgeManager.currentTime()) {
65            case 2:
66              res.add(new Pair<ServerName, Long>(ServerName.valueOf("hn", 10, 10), 1L));
67              break;
68            case 1000:
69              break;
70          }
71  
72          return res;
73        }
74      };
75  
76      mee.setValue(2);
77      for (int i = 0; i < ClusterStatusPublisher.NB_SEND; i++) {
78        Assert.assertEquals("i=" + i, 1, csp.generateDeadServersListToSend().size());
79      }
80      mee.setValue(1000);
81      Assert.assertTrue(csp.generateDeadServersListToSend().isEmpty());
82    }
83  
84    @Test
85    public void testOrder() {
86      ClusterStatusPublisher csp = new ClusterStatusPublisher() {
87        @Override
88        protected List<Pair<ServerName, Long>> getDeadServers(long since) {
89          List<Pair<ServerName, Long>> res = new ArrayList<Pair<ServerName, Long>>();
90          for (int i = 0; i < 25; i++) {
91            res.add(new Pair<ServerName, Long>(ServerName.valueOf("hn" + i, 10, 10), 20L));
92          }
93  
94          return res;
95        }
96      };
97  
98  
99      mee.setValue(3);
100     List<ServerName> allSNS = csp.generateDeadServersListToSend();
101 
102     Assert.assertEquals(10, ClusterStatusPublisher.MAX_SERVER_PER_MESSAGE);
103     Assert.assertEquals(10, allSNS.size());
104 
105     List<ServerName> nextMes = csp.generateDeadServersListToSend();
106     Assert.assertEquals(10, nextMes.size());
107     for (ServerName sn : nextMes) {
108       if (!allSNS.contains(sn)) {
109         allSNS.add(sn);
110       }
111     }
112     Assert.assertEquals(20, allSNS.size());
113 
114     nextMes = csp.generateDeadServersListToSend();
115     Assert.assertEquals(10, nextMes.size());
116     for (ServerName sn : nextMes) {
117       if (!allSNS.contains(sn)) {
118         allSNS.add(sn);
119       }
120     }
121     Assert.assertEquals(25, allSNS.size());
122 
123     nextMes = csp.generateDeadServersListToSend();
124     Assert.assertEquals(10, nextMes.size());
125     for (ServerName sn : nextMes) {
126       if (!allSNS.contains(sn)) {
127         allSNS.add(sn);
128       }
129     }
130     Assert.assertEquals(25, allSNS.size());
131   }
132 }