|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.master; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.ConcurrentNavigableMap; |
| 25 | +import java.util.concurrent.ConcurrentSkipListMap; |
| 26 | + |
| 27 | +import org.apache.hadoop.conf.Configuration; |
| 28 | +import org.apache.hadoop.hbase.ScheduledChore; |
| 29 | +import org.apache.hadoop.hbase.ServerMetrics; |
| 30 | +import org.apache.hadoop.hbase.ServerName; |
| 31 | +import org.apache.yetus.audience.InterfaceAudience; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +/** |
| 36 | + * The ReplicationServerManager class manages info about replication servers. |
| 37 | + * <p> |
| 38 | + * Maintains lists of online and dead servers. |
| 39 | + * <p> |
| 40 | + * Servers are distinguished in two different ways. A given server has a |
| 41 | + * location, specified by hostname and port, and of which there can only be one |
| 42 | + * online at any given time. A server instance is specified by the location |
| 43 | + * (hostname and port) as well as the startcode (timestamp from when the server |
| 44 | + * was started). This is used to differentiate a restarted instance of a given |
| 45 | + * server from the original instance. |
| 46 | + */ |
| 47 | +@InterfaceAudience.Private |
| 48 | +public class ReplicationServerManager { |
| 49 | + |
| 50 | + private static final Logger LOG = LoggerFactory.getLogger(ReplicationServerManager.class); |
| 51 | + |
| 52 | + public static final String ONLINE_SERVER_REFRESH_INTERVAL = |
| 53 | + "hbase.master.replication.server.refresh.interval"; |
| 54 | + public static final int ONLINE_SERVER_REFRESH_INTERVAL_DEFAULT = 60 * 1000; // 1 mins |
| 55 | + |
| 56 | + private final MasterServices master; |
| 57 | + |
| 58 | + /** Map of registered servers to their current load */ |
| 59 | + private final ConcurrentNavigableMap<ServerName, ServerMetrics> onlineServers = |
| 60 | + new ConcurrentSkipListMap<>(); |
| 61 | + |
| 62 | + private OnlineServerRefresher onlineServerRefresher; |
| 63 | + private int refreshPeriod; |
| 64 | + |
| 65 | + /** |
| 66 | + * Constructor. |
| 67 | + */ |
| 68 | + public ReplicationServerManager(final MasterServices master) { |
| 69 | + this.master = master; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * start chore in ServerManager |
| 74 | + */ |
| 75 | + public void startChore() { |
| 76 | + Configuration conf = master.getConfiguration(); |
| 77 | + refreshPeriod = conf.getInt(ONLINE_SERVER_REFRESH_INTERVAL, |
| 78 | + ONLINE_SERVER_REFRESH_INTERVAL_DEFAULT); |
| 79 | + onlineServerRefresher = new OnlineServerRefresher("ReplicationServerRefresher", refreshPeriod); |
| 80 | + master.getChoreService().scheduleChore(onlineServerRefresher); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Stop the ServerManager. |
| 85 | + */ |
| 86 | + public void stop() { |
| 87 | + if (onlineServerRefresher != null) { |
| 88 | + onlineServerRefresher.cancel(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void serverReport(ServerName sn, ServerMetrics sl) { |
| 93 | + if (null == this.onlineServers.replace(sn, sl)) { |
| 94 | + if (!checkAndRecordNewServer(sn, sl)) { |
| 95 | + LOG.info("ReplicationServerReport ignored, could not record the server: {}", sn); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Check is a server of same host and port already exists, |
| 102 | + * if not, or the existed one got a smaller start code, record it. |
| 103 | + * |
| 104 | + * @param serverName the server to check and record |
| 105 | + * @param sl the server load on the server |
| 106 | + * @return true if the server is recorded, otherwise, false |
| 107 | + */ |
| 108 | + private boolean checkAndRecordNewServer(final ServerName serverName, final ServerMetrics sl) { |
| 109 | + ServerName existingServer = null; |
| 110 | + synchronized (this.onlineServers) { |
| 111 | + existingServer = findServerWithSameHostnamePort(serverName); |
| 112 | + if (existingServer != null && (existingServer.getStartcode() > serverName.getStartcode())) { |
| 113 | + LOG.info("ReplicationServer serverName={} rejected; we already have {} registered with " |
| 114 | + + "same hostname and port", serverName, existingServer); |
| 115 | + return false; |
| 116 | + } |
| 117 | + recordNewServer(serverName, sl); |
| 118 | + // Note that we assume that same ts means same server, and don't expire in that case. |
| 119 | + if (existingServer != null && (existingServer.getStartcode() < serverName.getStartcode())) { |
| 120 | + LOG.info("Triggering server recovery; existingServer {} looks stale, new server: {}", |
| 121 | + existingServer, serverName); |
| 122 | + expireServer(existingServer); |
| 123 | + } |
| 124 | + } |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Assumes onlineServers is locked. |
| 130 | + * @return ServerName with matching hostname and port. |
| 131 | + */ |
| 132 | + private ServerName findServerWithSameHostnamePort(final ServerName serverName) { |
| 133 | + ServerName end = ServerName.valueOf(serverName.getHostname(), serverName.getPort(), |
| 134 | + Long.MAX_VALUE); |
| 135 | + |
| 136 | + ServerName r = onlineServers.lowerKey(end); |
| 137 | + if (r != null && ServerName.isSameAddress(r, serverName)) { |
| 138 | + return r; |
| 139 | + } |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Assumes onlineServers is locked. |
| 145 | + */ |
| 146 | + private void recordNewServer(final ServerName serverName, final ServerMetrics sl) { |
| 147 | + LOG.info("Registering ReplicationServer={}", serverName); |
| 148 | + this.onlineServers.put(serverName, sl); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Assumes onlineServers is locked. |
| 153 | + * Expire the passed server. Remove it from list of online servers |
| 154 | + */ |
| 155 | + public void expireServer(final ServerName serverName) { |
| 156 | + LOG.info("Expiring ReplicationServer={}", serverName); |
| 157 | + onlineServers.remove(serverName); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @return Read-only map of servers to serverinfo |
| 162 | + */ |
| 163 | + public Map<ServerName, ServerMetrics> getOnlineServers() { |
| 164 | + // Presumption is that iterating the returned Map is OK. |
| 165 | + synchronized (this.onlineServers) { |
| 166 | + return Collections.unmodifiableMap(this.onlineServers); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @return A copy of the internal list of online servers. |
| 172 | + */ |
| 173 | + public List<ServerName> getOnlineServersList() { |
| 174 | + return new ArrayList<>(this.onlineServers.keySet()); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @param serverName server name |
| 179 | + * @return ServerMetrics if serverName is known else null |
| 180 | + */ |
| 181 | + public ServerMetrics getServerMetrics(final ServerName serverName) { |
| 182 | + return this.onlineServers.get(serverName); |
| 183 | + } |
| 184 | + |
| 185 | + private class OnlineServerRefresher extends ScheduledChore { |
| 186 | + |
| 187 | + public OnlineServerRefresher(String name, int p) { |
| 188 | + super(name, master, p, 60 * 1000); // delay one minute before first execute |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + protected void chore() { |
| 193 | + synchronized (onlineServers) { |
| 194 | + List<ServerName> servers = getOnlineServersList(); |
| 195 | + servers.forEach(s -> { |
| 196 | + ServerMetrics metrics = onlineServers.get(s); |
| 197 | + if (metrics.getReportTimestamp() + refreshPeriod < System.currentTimeMillis()) { |
| 198 | + expireServer(s); |
| 199 | + } |
| 200 | + }); |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments