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.fs; 019 020import java.io.IOException; 021 022import org.apache.hadoop.classification.InterfaceAudience; 023import org.apache.hadoop.classification.InterfaceStability; 024 025/** 026 * Represents the network location of a block, information about the hosts 027 * that contain block replicas, and other block metadata (E.g. the file 028 * offset associated with the block, length, whether it is corrupt, etc). 029 */ 030@InterfaceAudience.Public 031@InterfaceStability.Stable 032public class BlockLocation { 033 private String[] hosts; // Datanode hostnames 034 private String[] cachedHosts; // Datanode hostnames with a cached replica 035 private String[] names; // Datanode IP:xferPort for accessing the block 036 private String[] topologyPaths; // Full path name in network topology 037 private String[] storageIds; // Storage ID of each replica 038 private long offset; // Offset of the block in the file 039 private long length; 040 private boolean corrupt; 041 042 private static final String[] EMPTY_STR_ARRAY = new String[0]; 043 044 /** 045 * Default Constructor 046 */ 047 public BlockLocation() { 048 this(EMPTY_STR_ARRAY, EMPTY_STR_ARRAY, 0L, 0L); 049 } 050 051 /** 052 * Copy constructor 053 */ 054 public BlockLocation(BlockLocation that) { 055 this.hosts = that.hosts; 056 this.cachedHosts = that.cachedHosts; 057 this.names = that.names; 058 this.topologyPaths = that.topologyPaths; 059 this.offset = that.offset; 060 this.length = that.length; 061 this.corrupt = that.corrupt; 062 this.storageIds = that.storageIds; 063 } 064 065 /** 066 * Constructor with host, name, offset and length 067 */ 068 public BlockLocation(String[] names, String[] hosts, long offset, 069 long length) { 070 this(names, hosts, offset, length, false); 071 } 072 073 /** 074 * Constructor with host, name, offset, length and corrupt flag 075 */ 076 public BlockLocation(String[] names, String[] hosts, long offset, 077 long length, boolean corrupt) { 078 this(names, hosts, null, offset, length, corrupt); 079 } 080 081 /** 082 * Constructor with host, name, network topology, offset and length 083 */ 084 public BlockLocation(String[] names, String[] hosts, String[] topologyPaths, 085 long offset, long length) { 086 this(names, hosts, topologyPaths, offset, length, false); 087 } 088 089 /** 090 * Constructor with host, name, network topology, offset, length 091 * and corrupt flag 092 */ 093 public BlockLocation(String[] names, String[] hosts, String[] topologyPaths, 094 long offset, long length, boolean corrupt) { 095 this(names, hosts, null, topologyPaths, offset, length, corrupt); 096 } 097 098 public BlockLocation(String[] names, String[] hosts, String[] cachedHosts, 099 String[] topologyPaths, long offset, long length, boolean corrupt) { 100 this(names, hosts, cachedHosts, topologyPaths, null, offset, length, 101 corrupt); 102 } 103 104 public BlockLocation(String[] names, String[] hosts, String[] cachedHosts, 105 String[] topologyPaths, String[] storageIds, 106 long offset, long length, boolean corrupt) { 107 if (names == null) { 108 this.names = EMPTY_STR_ARRAY; 109 } else { 110 this.names = names; 111 } 112 if (hosts == null) { 113 this.hosts = EMPTY_STR_ARRAY; 114 } else { 115 this.hosts = hosts; 116 } 117 if (cachedHosts == null) { 118 this.cachedHosts = EMPTY_STR_ARRAY; 119 } else { 120 this.cachedHosts = cachedHosts; 121 } 122 if (topologyPaths == null) { 123 this.topologyPaths = EMPTY_STR_ARRAY; 124 } else { 125 this.topologyPaths = topologyPaths; 126 } 127 if (storageIds == null) { 128 this.storageIds = EMPTY_STR_ARRAY; 129 } else { 130 this.storageIds = storageIds; 131 } 132 this.offset = offset; 133 this.length = length; 134 this.corrupt = corrupt; 135 } 136 137 /** 138 * Get the list of hosts (hostname) hosting this block 139 */ 140 public String[] getHosts() throws IOException { 141 return hosts; 142 } 143 144 /** 145 * Get the list of hosts (hostname) hosting a cached replica of the block 146 */ 147 public String[] getCachedHosts() { 148 return cachedHosts; 149 } 150 151 /** 152 * Get the list of names (IP:xferPort) hosting this block 153 */ 154 public String[] getNames() throws IOException { 155 return names; 156 } 157 158 /** 159 * Get the list of network topology paths for each of the hosts. 160 * The last component of the path is the "name" (IP:xferPort). 161 */ 162 public String[] getTopologyPaths() throws IOException { 163 return topologyPaths; 164 } 165 166 /** 167 * Get the storageID of each replica of the block. 168 */ 169 public String[] getStorageIds() { 170 return storageIds; 171 } 172 173 /** 174 * Get the start offset of file associated with this block 175 */ 176 public long getOffset() { 177 return offset; 178 } 179 180 /** 181 * Get the length of the block 182 */ 183 public long getLength() { 184 return length; 185 } 186 187 /** 188 * Get the corrupt flag. 189 */ 190 public boolean isCorrupt() { 191 return corrupt; 192 } 193 194 /** 195 * Set the start offset of file associated with this block 196 */ 197 public void setOffset(long offset) { 198 this.offset = offset; 199 } 200 201 /** 202 * Set the length of block 203 */ 204 public void setLength(long length) { 205 this.length = length; 206 } 207 208 /** 209 * Set the corrupt flag. 210 */ 211 public void setCorrupt(boolean corrupt) { 212 this.corrupt = corrupt; 213 } 214 215 /** 216 * Set the hosts hosting this block 217 */ 218 public void setHosts(String[] hosts) throws IOException { 219 if (hosts == null) { 220 this.hosts = EMPTY_STR_ARRAY; 221 } else { 222 this.hosts = hosts; 223 } 224 } 225 226 /** 227 * Set the hosts hosting a cached replica of this block 228 */ 229 public void setCachedHosts(String[] cachedHosts) { 230 if (cachedHosts == null) { 231 this.cachedHosts = EMPTY_STR_ARRAY; 232 } else { 233 this.cachedHosts = cachedHosts; 234 } 235 } 236 237 /** 238 * Set the names (host:port) hosting this block 239 */ 240 public void setNames(String[] names) throws IOException { 241 if (names == null) { 242 this.names = EMPTY_STR_ARRAY; 243 } else { 244 this.names = names; 245 } 246 } 247 248 /** 249 * Set the network topology paths of the hosts 250 */ 251 public void setTopologyPaths(String[] topologyPaths) throws IOException { 252 if (topologyPaths == null) { 253 this.topologyPaths = EMPTY_STR_ARRAY; 254 } else { 255 this.topologyPaths = topologyPaths; 256 } 257 } 258 259 public void setStorageIds(String[] storageIds) { 260 if (storageIds == null) { 261 this.storageIds = EMPTY_STR_ARRAY; 262 } else { 263 this.storageIds = storageIds; 264 } 265 } 266 267 @Override 268 public String toString() { 269 StringBuilder result = new StringBuilder(); 270 result.append(offset); 271 result.append(','); 272 result.append(length); 273 if (corrupt) { 274 result.append("(corrupt)"); 275 } 276 for(String h: hosts) { 277 result.append(','); 278 result.append(h); 279 } 280 return result.toString(); 281 } 282}