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 019 package org.apache.hadoop.net; 020 021 import org.apache.hadoop.classification.InterfaceAudience; 022 import org.apache.hadoop.classification.InterfaceStability; 023 import org.apache.hadoop.conf.Configurable; 024 import org.apache.hadoop.conf.Configuration; 025 026 /** 027 * This is a base class for DNS to Switch mappings. <p/> It is not mandatory to 028 * derive {@link DNSToSwitchMapping} implementations from it, but it is strongly 029 * recommended, as it makes it easy for the Hadoop developers to add new methods 030 * to this base class that are automatically picked up by all implementations. 031 * <p/> 032 * 033 * This class does not extend the <code>Configured</code> 034 * base class, and should not be changed to do so, as it causes problems 035 * for subclasses. The constructor of the <code>Configured</code> calls 036 * the {@link #setConf(Configuration)} method, which will call into the 037 * subclasses before they have been fully constructed. 038 * 039 */ 040 @InterfaceAudience.Public 041 @InterfaceStability.Evolving 042 public abstract class AbstractDNSToSwitchMapping 043 implements DNSToSwitchMapping, Configurable { 044 045 private Configuration conf; 046 047 /** 048 * Create an unconfigured instance 049 */ 050 protected AbstractDNSToSwitchMapping() { 051 } 052 053 /** 054 * Create an instance, caching the configuration file. 055 * This constructor does not call {@link #setConf(Configuration)}; if 056 * a subclass extracts information in that method, it must call it explicitly. 057 * @param conf the configuration 058 */ 059 protected AbstractDNSToSwitchMapping(Configuration conf) { 060 this.conf = conf; 061 } 062 063 @Override 064 public Configuration getConf() { 065 return conf; 066 } 067 068 @Override 069 public void setConf(Configuration conf) { 070 this.conf = conf; 071 } 072 073 /** 074 * Predicate that indicates that the switch mapping is known to be 075 * single-switch. The base class returns false: it assumes all mappings are 076 * multi-rack. Subclasses may override this with methods that are more aware 077 * of their topologies. 078 * 079 * <p/> 080 * 081 * This method is used when parts of Hadoop need know whether to apply 082 * single rack vs multi-rack policies, such as during block placement. 083 * Such algorithms behave differently if they are on multi-switch systems. 084 * </p> 085 * 086 * @return true if the mapping thinks that it is on a single switch 087 */ 088 public boolean isSingleSwitch() { 089 return false; 090 } 091 092 /** 093 * Query for a {@link DNSToSwitchMapping} instance being on a single 094 * switch. 095 * <p/> 096 * This predicate simply assumes that all mappings not derived from 097 * this class are multi-switch. 098 * @param mapping the mapping to query 099 * @return true if the base class says it is single switch, or the mapping 100 * is not derived from this class. 101 */ 102 public static boolean isMappingSingleSwitch(DNSToSwitchMapping mapping) { 103 return mapping instanceof AbstractDNSToSwitchMapping 104 && ((AbstractDNSToSwitchMapping) mapping).isSingleSwitch(); 105 } 106 107 }