1 /**
2 * Copyright 2012 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package org.apache.hadoop.hbase.master.balancer;
21
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.HRegionInfo;
29 import org.apache.hadoop.hbase.ServerName;
30 import org.jboss.netty.util.internal.ConcurrentHashMap;
31
32 /**
33 * This class contains the mapping information between each region and
34 * its favored region server list. Used by {@link FavoredNodeLoadBalancer} set
35 * of classes and from unit tests (hence the class is public)
36 *
37 * All the access to this class is thread-safe.
38 */
39 @InterfaceAudience.Private
40 public class FavoredNodes {
41 protected static final Log LOG = LogFactory.getLog(
42 FavoredNodes.class.getName());
43
44 /** the map between each region and its favored region server list */
45 private Map<HRegionInfo, List<ServerName>> favoredNodesMap;
46
47 public static enum Position {
48 PRIMARY,
49 SECONDARY,
50 TERTIARY;
51 };
52
53 public FavoredNodes() {
54 favoredNodesMap = new ConcurrentHashMap<HRegionInfo, List<ServerName>>();
55 }
56
57 /**
58 * Add an assignment to the plan
59 * @param region
60 * @param servers
61 */
62 public synchronized void updateFavoredNodesMap(HRegionInfo region,
63 List<ServerName> servers) {
64 if (region == null || servers == null || servers.size() ==0)
65 return;
66 this.favoredNodesMap.put(region, servers);
67 }
68
69 /**
70 * @param region
71 * @return the list of favored region server for this region based on the plan
72 */
73 public synchronized List<ServerName> getFavoredNodes(HRegionInfo region) {
74 return favoredNodesMap.get(region);
75 }
76
77 /**
78 * Return the position of the server in the favoredNodes list. Assumes the
79 * favoredNodes list is of size 3.
80 * @param favoredNodes
81 * @param server
82 * @return position
83 */
84 static Position getFavoredServerPosition(
85 List<ServerName> favoredNodes, ServerName server) {
86 if (favoredNodes == null || server == null ||
87 favoredNodes.size() != FavoredNodeAssignmentHelper.FAVORED_NODES_NUM) {
88 return null;
89 }
90 for (Position p : Position.values()) {
91 if (favoredNodes.get(p.ordinal()).equals(server)) {
92 return p;
93 }
94 }
95 return null;
96 }
97 }