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 019package org.apache.hadoop.fs; 020 021import java.io.EOFException; 022import java.io.FileNotFoundException; 023import java.io.IOException; 024import java.io.InputStream; 025import java.nio.channels.ClosedChannelException; 026import java.util.Arrays; 027 028import org.apache.hadoop.classification.InterfaceAudience; 029import org.apache.hadoop.classification.InterfaceStability; 030import org.apache.hadoop.conf.Configuration; 031import org.apache.hadoop.fs.permission.FsPermission; 032import org.apache.hadoop.util.DataChecksum; 033import org.apache.hadoop.util.Progressable; 034 035/**************************************************************** 036 * Abstract Checksumed FileSystem. 037 * It provide a basic implementation of a Checksumed FileSystem, 038 * which creates a checksum file for each raw file. 039 * It generates & verifies checksums at the client side. 040 * 041 *****************************************************************/ 042@InterfaceAudience.Public 043@InterfaceStability.Stable 044public abstract class ChecksumFileSystem extends FilterFileSystem { 045 private static final byte[] CHECKSUM_VERSION = new byte[] {'c', 'r', 'c', 0}; 046 private int bytesPerChecksum = 512; 047 private boolean verifyChecksum = true; 048 private boolean writeChecksum = true; 049 050 public static double getApproxChkSumLength(long size) { 051 return ChecksumFSOutputSummer.CHKSUM_AS_FRACTION * size; 052 } 053 054 public ChecksumFileSystem(FileSystem fs) { 055 super(fs); 056 } 057 058 @Override 059 public void setConf(Configuration conf) { 060 super.setConf(conf); 061 if (conf != null) { 062 bytesPerChecksum = conf.getInt(LocalFileSystemConfigKeys.LOCAL_FS_BYTES_PER_CHECKSUM_KEY, 063 LocalFileSystemConfigKeys.LOCAL_FS_BYTES_PER_CHECKSUM_DEFAULT); 064 } 065 } 066 067 /** 068 * Set whether to verify checksum. 069 */ 070 @Override 071 public void setVerifyChecksum(boolean verifyChecksum) { 072 this.verifyChecksum = verifyChecksum; 073 } 074 075 @Override 076 public void setWriteChecksum(boolean writeChecksum) { 077 this.writeChecksum = writeChecksum; 078 } 079 080 /** get the raw file system */ 081 @Override 082 public FileSystem getRawFileSystem() { 083 return fs; 084 } 085 086 /** Return the name of the checksum file associated with a file.*/ 087 public Path getChecksumFile(Path file) { 088 return new Path(file.getParent(), "." + file.getName() + ".crc"); 089 } 090 091 /** Return true iff file is a checksum file name.*/ 092 public static boolean isChecksumFile(Path file) { 093 String name = file.getName(); 094 return name.startsWith(".") && name.endsWith(".crc"); 095 } 096 097 /** Return the length of the checksum file given the size of the 098 * actual file. 099 **/ 100 public long getChecksumFileLength(Path file, long fileSize) { 101 return getChecksumLength(fileSize, getBytesPerSum()); 102 } 103 104 /** Return the bytes Per Checksum */ 105 public int getBytesPerSum() { 106 return bytesPerChecksum; 107 } 108 109 private int getSumBufferSize(int bytesPerSum, int bufferSize) { 110 int defaultBufferSize = getConf().getInt( 111 LocalFileSystemConfigKeys.LOCAL_FS_STREAM_BUFFER_SIZE_KEY, 112 LocalFileSystemConfigKeys.LOCAL_FS_STREAM_BUFFER_SIZE_DEFAULT); 113 int proportionalBufferSize = bufferSize / bytesPerSum; 114 return Math.max(bytesPerSum, 115 Math.max(proportionalBufferSize, defaultBufferSize)); 116 } 117 118 /******************************************************* 119 * For open()'s FSInputStream 120 * It verifies that data matches checksums. 121 *******************************************************/ 122 private static class ChecksumFSInputChecker extends FSInputChecker { 123 private ChecksumFileSystem fs; 124 private FSDataInputStream datas; 125 private FSDataInputStream sums; 126 127 private static final int HEADER_LENGTH = 8; 128 129 private int bytesPerSum = 1; 130 131 public ChecksumFSInputChecker(ChecksumFileSystem fs, Path file) 132 throws IOException { 133 this(fs, file, fs.getConf().getInt( 134 LocalFileSystemConfigKeys.LOCAL_FS_STREAM_BUFFER_SIZE_KEY, 135 LocalFileSystemConfigKeys.LOCAL_FS_STREAM_BUFFER_SIZE_DEFAULT)); 136 } 137 138 public ChecksumFSInputChecker(ChecksumFileSystem fs, Path file, int bufferSize) 139 throws IOException { 140 super( file, fs.getFileStatus(file).getReplication() ); 141 this.datas = fs.getRawFileSystem().open(file, bufferSize); 142 this.fs = fs; 143 Path sumFile = fs.getChecksumFile(file); 144 try { 145 int sumBufferSize = fs.getSumBufferSize(fs.getBytesPerSum(), bufferSize); 146 sums = fs.getRawFileSystem().open(sumFile, sumBufferSize); 147 148 byte[] version = new byte[CHECKSUM_VERSION.length]; 149 sums.readFully(version); 150 if (!Arrays.equals(version, CHECKSUM_VERSION)) 151 throw new IOException("Not a checksum file: "+sumFile); 152 this.bytesPerSum = sums.readInt(); 153 set(fs.verifyChecksum, DataChecksum.newCrc32(), bytesPerSum, 4); 154 } catch (FileNotFoundException e) { // quietly ignore 155 set(fs.verifyChecksum, null, 1, 0); 156 } catch (IOException e) { // loudly ignore 157 LOG.warn("Problem opening checksum file: "+ file + 158 ". Ignoring exception: " , e); 159 set(fs.verifyChecksum, null, 1, 0); 160 } 161 } 162 163 private long getChecksumFilePos( long dataPos ) { 164 return HEADER_LENGTH + 4*(dataPos/bytesPerSum); 165 } 166 167 @Override 168 protected long getChunkPosition( long dataPos ) { 169 return dataPos/bytesPerSum*bytesPerSum; 170 } 171 172 @Override 173 public int available() throws IOException { 174 return datas.available() + super.available(); 175 } 176 177 @Override 178 public int read(long position, byte[] b, int off, int len) 179 throws IOException { 180 // parameter check 181 if ((off | len | (off + len) | (b.length - (off + len))) < 0) { 182 throw new IndexOutOfBoundsException(); 183 } else if (len == 0) { 184 return 0; 185 } 186 if( position<0 ) { 187 throw new IllegalArgumentException( 188 "Parameter position can not to be negative"); 189 } 190 191 ChecksumFSInputChecker checker = new ChecksumFSInputChecker(fs, file); 192 checker.seek(position); 193 int nread = checker.read(b, off, len); 194 checker.close(); 195 return nread; 196 } 197 198 @Override 199 public void close() throws IOException { 200 datas.close(); 201 if( sums != null ) { 202 sums.close(); 203 } 204 set(fs.verifyChecksum, null, 1, 0); 205 } 206 207 208 @Override 209 public boolean seekToNewSource(long targetPos) throws IOException { 210 long sumsPos = getChecksumFilePos(targetPos); 211 fs.reportChecksumFailure(file, datas, targetPos, sums, sumsPos); 212 boolean newDataSource = datas.seekToNewSource(targetPos); 213 return sums.seekToNewSource(sumsPos) || newDataSource; 214 } 215 216 @Override 217 protected int readChunk(long pos, byte[] buf, int offset, int len, 218 byte[] checksum) throws IOException { 219 220 boolean eof = false; 221 if (needChecksum()) { 222 assert checksum != null; // we have a checksum buffer 223 assert checksum.length % CHECKSUM_SIZE == 0; // it is sane length 224 assert len >= bytesPerSum; // we must read at least one chunk 225 226 final int checksumsToRead = Math.min( 227 len/bytesPerSum, // number of checksums based on len to read 228 checksum.length / CHECKSUM_SIZE); // size of checksum buffer 229 long checksumPos = getChecksumFilePos(pos); 230 if(checksumPos != sums.getPos()) { 231 sums.seek(checksumPos); 232 } 233 234 int sumLenRead = sums.read(checksum, 0, CHECKSUM_SIZE * checksumsToRead); 235 if (sumLenRead >= 0 && sumLenRead % CHECKSUM_SIZE != 0) { 236 throw new ChecksumException( 237 "Checksum file not a length multiple of checksum size " + 238 "in " + file + " at " + pos + " checksumpos: " + checksumPos + 239 " sumLenread: " + sumLenRead, 240 pos); 241 } 242 if (sumLenRead <= 0) { // we're at the end of the file 243 eof = true; 244 } else { 245 // Adjust amount of data to read based on how many checksum chunks we read 246 len = Math.min(len, bytesPerSum * (sumLenRead / CHECKSUM_SIZE)); 247 } 248 } 249 if(pos != datas.getPos()) { 250 datas.seek(pos); 251 } 252 int nread = readFully(datas, buf, offset, len); 253 if (eof && nread > 0) { 254 throw new ChecksumException("Checksum error: "+file+" at "+pos, pos); 255 } 256 return nread; 257 } 258 } 259 260 private static class FSDataBoundedInputStream extends FSDataInputStream { 261 private FileSystem fs; 262 private Path file; 263 private long fileLen = -1L; 264 265 FSDataBoundedInputStream(FileSystem fs, Path file, InputStream in) { 266 super(in); 267 this.fs = fs; 268 this.file = file; 269 } 270 271 @Override 272 public boolean markSupported() { 273 return false; 274 } 275 276 /* Return the file length */ 277 private long getFileLength() throws IOException { 278 if( fileLen==-1L ) { 279 fileLen = fs.getContentSummary(file).getLength(); 280 } 281 return fileLen; 282 } 283 284 /** 285 * Skips over and discards <code>n</code> bytes of data from the 286 * input stream. 287 * 288 *The <code>skip</code> method skips over some smaller number of bytes 289 * when reaching end of file before <code>n</code> bytes have been skipped. 290 * The actual number of bytes skipped is returned. If <code>n</code> is 291 * negative, no bytes are skipped. 292 * 293 * @param n the number of bytes to be skipped. 294 * @return the actual number of bytes skipped. 295 * @exception IOException if an I/O error occurs. 296 * ChecksumException if the chunk to skip to is corrupted 297 */ 298 @Override 299 public synchronized long skip(long n) throws IOException { 300 long curPos = getPos(); 301 long fileLength = getFileLength(); 302 if( n+curPos > fileLength ) { 303 n = fileLength - curPos; 304 } 305 return super.skip(n); 306 } 307 308 /** 309 * Seek to the given position in the stream. 310 * The next read() will be from that position. 311 * 312 * <p>This method does not allow seek past the end of the file. 313 * This produces IOException. 314 * 315 * @param pos the postion to seek to. 316 * @exception IOException if an I/O error occurs or seeks after EOF 317 * ChecksumException if the chunk to seek to is corrupted 318 */ 319 320 @Override 321 public synchronized void seek(long pos) throws IOException { 322 if (pos > getFileLength()) { 323 throw new EOFException("Cannot seek after EOF"); 324 } 325 super.seek(pos); 326 } 327 328 } 329 330 /** 331 * Opens an FSDataInputStream at the indicated Path. 332 * @param f the file name to open 333 * @param bufferSize the size of the buffer to be used. 334 */ 335 @Override 336 public FSDataInputStream open(Path f, int bufferSize) throws IOException { 337 FileSystem fs; 338 InputStream in; 339 if (verifyChecksum) { 340 fs = this; 341 in = new ChecksumFSInputChecker(this, f, bufferSize); 342 } else { 343 fs = getRawFileSystem(); 344 in = fs.open(f, bufferSize); 345 } 346 return new FSDataBoundedInputStream(fs, f, in); 347 } 348 349 @Override 350 public FSDataOutputStream append(Path f, int bufferSize, 351 Progressable progress) throws IOException { 352 throw new IOException("Not supported"); 353 } 354 355 /** 356 * Calculated the length of the checksum file in bytes. 357 * @param size the length of the data file in bytes 358 * @param bytesPerSum the number of bytes in a checksum block 359 * @return the number of bytes in the checksum file 360 */ 361 public static long getChecksumLength(long size, int bytesPerSum) { 362 //the checksum length is equal to size passed divided by bytesPerSum + 363 //bytes written in the beginning of the checksum file. 364 return ((size + bytesPerSum - 1) / bytesPerSum) * 4 + 365 CHECKSUM_VERSION.length + 4; 366 } 367 368 /** This class provides an output stream for a checksummed file. 369 * It generates checksums for data. */ 370 private static class ChecksumFSOutputSummer extends FSOutputSummer { 371 private FSDataOutputStream datas; 372 private FSDataOutputStream sums; 373 private static final float CHKSUM_AS_FRACTION = 0.01f; 374 private boolean isClosed = false; 375 376 public ChecksumFSOutputSummer(ChecksumFileSystem fs, 377 Path file, 378 boolean overwrite, 379 int bufferSize, 380 short replication, 381 long blockSize, 382 Progressable progress, 383 FsPermission permission) 384 throws IOException { 385 super(DataChecksum.newDataChecksum(DataChecksum.Type.CRC32, 386 fs.getBytesPerSum())); 387 int bytesPerSum = fs.getBytesPerSum(); 388 this.datas = fs.getRawFileSystem().create(file, permission, overwrite, 389 bufferSize, replication, blockSize, 390 progress); 391 int sumBufferSize = fs.getSumBufferSize(bytesPerSum, bufferSize); 392 this.sums = fs.getRawFileSystem().create(fs.getChecksumFile(file), 393 permission, true, sumBufferSize, 394 replication, blockSize, null); 395 sums.write(CHECKSUM_VERSION, 0, CHECKSUM_VERSION.length); 396 sums.writeInt(bytesPerSum); 397 } 398 399 @Override 400 public void close() throws IOException { 401 try { 402 flushBuffer(); 403 sums.close(); 404 datas.close(); 405 } finally { 406 isClosed = true; 407 } 408 } 409 410 @Override 411 protected void writeChunk(byte[] b, int offset, int len, byte[] checksum, 412 int ckoff, int cklen) 413 throws IOException { 414 datas.write(b, offset, len); 415 sums.write(checksum, ckoff, cklen); 416 } 417 418 @Override 419 protected void checkClosed() throws IOException { 420 if (isClosed) { 421 throw new ClosedChannelException(); 422 } 423 } 424 } 425 426 @Override 427 public FSDataOutputStream create(Path f, FsPermission permission, 428 boolean overwrite, int bufferSize, short replication, long blockSize, 429 Progressable progress) throws IOException { 430 return create(f, permission, overwrite, true, bufferSize, 431 replication, blockSize, progress); 432 } 433 434 private FSDataOutputStream create(Path f, FsPermission permission, 435 boolean overwrite, boolean createParent, int bufferSize, 436 short replication, long blockSize, 437 Progressable progress) throws IOException { 438 Path parent = f.getParent(); 439 if (parent != null) { 440 if (!createParent && !exists(parent)) { 441 throw new FileNotFoundException("Parent directory doesn't exist: " 442 + parent); 443 } else if (!mkdirs(parent)) { 444 throw new IOException("Mkdirs failed to create " + parent 445 + " (exists=" + exists(parent) + ", cwd=" + getWorkingDirectory() 446 + ")"); 447 } 448 } 449 final FSDataOutputStream out; 450 if (writeChecksum) { 451 out = new FSDataOutputStream( 452 new ChecksumFSOutputSummer(this, f, overwrite, bufferSize, replication, 453 blockSize, progress, permission), null); 454 } else { 455 out = fs.create(f, permission, overwrite, bufferSize, replication, 456 blockSize, progress); 457 // remove the checksum file since we aren't writing one 458 Path checkFile = getChecksumFile(f); 459 if (fs.exists(checkFile)) { 460 fs.delete(checkFile, true); 461 } 462 } 463 return out; 464 } 465 466 @Override 467 public FSDataOutputStream createNonRecursive(Path f, FsPermission permission, 468 boolean overwrite, int bufferSize, short replication, long blockSize, 469 Progressable progress) throws IOException { 470 return create(f, permission, overwrite, false, bufferSize, replication, 471 blockSize, progress); 472 } 473 474 /** 475 * Set replication for an existing file. 476 * Implement the abstract <tt>setReplication</tt> of <tt>FileSystem</tt> 477 * @param src file name 478 * @param replication new replication 479 * @throws IOException 480 * @return true if successful; 481 * false if file does not exist or is a directory 482 */ 483 @Override 484 public boolean setReplication(Path src, short replication) throws IOException { 485 boolean value = fs.setReplication(src, replication); 486 if (!value) 487 return false; 488 489 Path checkFile = getChecksumFile(src); 490 if (exists(checkFile)) 491 fs.setReplication(checkFile, replication); 492 493 return true; 494 } 495 496 /** 497 * Rename files/dirs 498 */ 499 @Override 500 public boolean rename(Path src, Path dst) throws IOException { 501 if (fs.isDirectory(src)) { 502 return fs.rename(src, dst); 503 } else { 504 if (fs.isDirectory(dst)) { 505 dst = new Path(dst, src.getName()); 506 } 507 508 boolean value = fs.rename(src, dst); 509 if (!value) 510 return false; 511 512 Path srcCheckFile = getChecksumFile(src); 513 Path dstCheckFile = getChecksumFile(dst); 514 if (fs.exists(srcCheckFile)) { //try to rename checksum 515 value = fs.rename(srcCheckFile, dstCheckFile); 516 } else if (fs.exists(dstCheckFile)) { 517 // no src checksum, so remove dst checksum 518 value = fs.delete(dstCheckFile, true); 519 } 520 521 return value; 522 } 523 } 524 525 /** 526 * Implement the delete(Path, boolean) in checksum 527 * file system. 528 */ 529 @Override 530 public boolean delete(Path f, boolean recursive) throws IOException{ 531 FileStatus fstatus = null; 532 try { 533 fstatus = fs.getFileStatus(f); 534 } catch(FileNotFoundException e) { 535 return false; 536 } 537 if (fstatus.isDirectory()) { 538 //this works since the crcs are in the same 539 //directories and the files. so we just delete 540 //everything in the underlying filesystem 541 return fs.delete(f, recursive); 542 } else { 543 Path checkFile = getChecksumFile(f); 544 if (fs.exists(checkFile)) { 545 fs.delete(checkFile, true); 546 } 547 return fs.delete(f, true); 548 } 549 } 550 551 final private static PathFilter DEFAULT_FILTER = new PathFilter() { 552 @Override 553 public boolean accept(Path file) { 554 return !isChecksumFile(file); 555 } 556 }; 557 558 /** 559 * List the statuses of the files/directories in the given path if the path is 560 * a directory. 561 * 562 * @param f 563 * given path 564 * @return the statuses of the files/directories in the given path 565 * @throws IOException 566 */ 567 @Override 568 public FileStatus[] listStatus(Path f) throws IOException { 569 return fs.listStatus(f, DEFAULT_FILTER); 570 } 571 572 /** 573 * List the statuses of the files/directories in the given path if the path is 574 * a directory. 575 * 576 * @param f 577 * given path 578 * @return the statuses of the files/directories in the given patch 579 * @throws IOException 580 */ 581 @Override 582 public RemoteIterator<LocatedFileStatus> listLocatedStatus(Path f) 583 throws IOException { 584 return fs.listLocatedStatus(f, DEFAULT_FILTER); 585 } 586 587 @Override 588 public boolean mkdirs(Path f) throws IOException { 589 return fs.mkdirs(f); 590 } 591 592 @Override 593 public void copyFromLocalFile(boolean delSrc, Path src, Path dst) 594 throws IOException { 595 Configuration conf = getConf(); 596 FileUtil.copy(getLocal(conf), src, this, dst, delSrc, conf); 597 } 598 599 /** 600 * The src file is under FS, and the dst is on the local disk. 601 * Copy it from FS control to the local dst name. 602 */ 603 @Override 604 public void copyToLocalFile(boolean delSrc, Path src, Path dst) 605 throws IOException { 606 Configuration conf = getConf(); 607 FileUtil.copy(this, src, getLocal(conf), dst, delSrc, conf); 608 } 609 610 /** 611 * The src file is under FS, and the dst is on the local disk. 612 * Copy it from FS control to the local dst name. 613 * If src and dst are directories, the copyCrc parameter 614 * determines whether to copy CRC files. 615 */ 616 public void copyToLocalFile(Path src, Path dst, boolean copyCrc) 617 throws IOException { 618 if (!fs.isDirectory(src)) { // source is a file 619 fs.copyToLocalFile(src, dst); 620 FileSystem localFs = getLocal(getConf()).getRawFileSystem(); 621 if (localFs.isDirectory(dst)) { 622 dst = new Path(dst, src.getName()); 623 } 624 dst = getChecksumFile(dst); 625 if (localFs.exists(dst)) { //remove old local checksum file 626 localFs.delete(dst, true); 627 } 628 Path checksumFile = getChecksumFile(src); 629 if (copyCrc && fs.exists(checksumFile)) { //copy checksum file 630 fs.copyToLocalFile(checksumFile, dst); 631 } 632 } else { 633 FileStatus[] srcs = listStatus(src); 634 for (FileStatus srcFile : srcs) { 635 copyToLocalFile(srcFile.getPath(), 636 new Path(dst, srcFile.getPath().getName()), copyCrc); 637 } 638 } 639 } 640 641 @Override 642 public Path startLocalOutput(Path fsOutputFile, Path tmpLocalFile) 643 throws IOException { 644 return tmpLocalFile; 645 } 646 647 @Override 648 public void completeLocalOutput(Path fsOutputFile, Path tmpLocalFile) 649 throws IOException { 650 moveFromLocalFile(tmpLocalFile, fsOutputFile); 651 } 652 653 /** 654 * Report a checksum error to the file system. 655 * @param f the file name containing the error 656 * @param in the stream open on the file 657 * @param inPos the position of the beginning of the bad data in the file 658 * @param sums the stream open on the checksum file 659 * @param sumsPos the position of the beginning of the bad data in the checksum file 660 * @return if retry is neccessary 661 */ 662 public boolean reportChecksumFailure(Path f, FSDataInputStream in, 663 long inPos, FSDataInputStream sums, long sumsPos) { 664 return false; 665 } 666}