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 */
018 package org.apache.hadoop.hdfs.server.balancer;
019
020 import org.apache.hadoop.net.NetworkTopology;
021 import org.apache.hadoop.net.Node;
022
023 /** A matcher interface for matching nodes. */
024 public interface Matcher {
025 /** Given the cluster topology, does the left node match the right node? */
026 public boolean match(NetworkTopology cluster, Node left, Node right);
027
028 /** Match datanodes in the same node group. */
029 public static final Matcher SAME_NODE_GROUP = new Matcher() {
030 @Override
031 public boolean match(NetworkTopology cluster, Node left, Node right) {
032 return cluster.isOnSameNodeGroup(left, right);
033 }
034
035 @Override
036 public String toString() {
037 return "SAME_NODE_GROUP";
038 }
039 };
040
041 /** Match datanodes in the same rack. */
042 public static final Matcher SAME_RACK = new Matcher() {
043 @Override
044 public boolean match(NetworkTopology cluster, Node left, Node right) {
045 return cluster.isOnSameRack(left, right);
046 }
047
048 @Override
049 public String toString() {
050 return "SAME_RACK";
051 }
052 };
053
054 /** Match any datanode with any other datanode. */
055 public static final Matcher ANY_OTHER = new Matcher() {
056 @Override
057 public boolean match(NetworkTopology cluster, Node left, Node right) {
058 return left != right;
059 }
060
061 @Override
062 public String toString() {
063 return "ANY_OTHER";
064 }
065 };
066 }