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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.util.ArrayList;
024import java.util.List;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.testclassification.MasterTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
029import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
030import org.apache.hadoop.hbase.util.Pair;
031import org.junit.jupiter.api.BeforeEach;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034
035@Tag(MasterTests.TAG)
036@Tag(SmallTests.TAG) // Plays with the ManualEnvironmentEdge
037public class TestClusterStatusPublisher {
038
039  private ManualEnvironmentEdge mee = new ManualEnvironmentEdge();
040
041  @BeforeEach
042  public void before() {
043    mee.setValue(0);
044    EnvironmentEdgeManager.injectEdge(mee);
045  }
046
047  @Test
048  public void testEmpty() {
049    ClusterStatusPublisher csp = new ClusterStatusPublisher() {
050      @Override
051      protected List<Pair<ServerName, Long>> getDeadServers(long since) {
052        return new ArrayList<>();
053      }
054    };
055
056    assertTrue(csp.generateDeadServersListToSend().isEmpty());
057  }
058
059  @Test
060  public void testMaxSend() {
061    ClusterStatusPublisher csp = new ClusterStatusPublisher() {
062      @SuppressWarnings("MissingDefault")
063      @Override
064      protected List<Pair<ServerName, Long>> getDeadServers(long since) {
065        List<Pair<ServerName, Long>> res = new ArrayList<>();
066        switch ((int) EnvironmentEdgeManager.currentTime()) {
067          case 2:
068            res.add(new Pair<>(ServerName.valueOf("hn", 10, 10), 1L));
069            break;
070          case 1000:
071            break;
072        }
073
074        return res;
075      }
076    };
077
078    mee.setValue(2);
079    for (int i = 0; i < ClusterStatusPublisher.NB_SEND; i++) {
080      assertEquals(1, csp.generateDeadServersListToSend().size(), "i=" + i);
081    }
082    mee.setValue(1000);
083    assertTrue(csp.generateDeadServersListToSend().isEmpty());
084  }
085
086  @Test
087  public void testOrder() {
088    ClusterStatusPublisher csp = new ClusterStatusPublisher() {
089      @Override
090      protected List<Pair<ServerName, Long>> getDeadServers(long since) {
091        List<Pair<ServerName, Long>> res = new ArrayList<>();
092        for (int i = 0; i < 25; i++) {
093          res.add(new Pair<>(ServerName.valueOf("hn" + i, 10, 10), 20L));
094        }
095
096        return res;
097      }
098    };
099
100    mee.setValue(3);
101    List<ServerName> allSNS = csp.generateDeadServersListToSend();
102
103    assertEquals(10, ClusterStatusPublisher.MAX_SERVER_PER_MESSAGE);
104    assertEquals(10, allSNS.size());
105
106    List<ServerName> nextMes = csp.generateDeadServersListToSend();
107    assertEquals(10, nextMes.size());
108    for (ServerName sn : nextMes) {
109      if (!allSNS.contains(sn)) {
110        allSNS.add(sn);
111      }
112    }
113    assertEquals(20, allSNS.size());
114
115    nextMes = csp.generateDeadServersListToSend();
116    assertEquals(10, nextMes.size());
117    for (ServerName sn : nextMes) {
118      if (!allSNS.contains(sn)) {
119        allSNS.add(sn);
120      }
121    }
122    assertEquals(25, allSNS.size());
123
124    nextMes = csp.generateDeadServersListToSend();
125    assertEquals(10, nextMes.size());
126    for (ServerName sn : nextMes) {
127      if (!allSNS.contains(sn)) {
128        allSNS.add(sn);
129      }
130    }
131    assertEquals(25, allSNS.size());
132  }
133}