Beispiel #1
0
 /**
  * Creates the specified file with the given permission. If the dir already exists and the
  * overwrite flag is false, underlying FileSystem throws an IOE. It is not retried and the IOE is
  * re-thrown to the caller.
  *
  * @param fs
  * @param path
  * @param perm
  * @param overwrite
  * @return
  * @throws IOException
  */
 public static FSDataOutputStream createPathWithPermsOnFileSystem(
     FileSystem fs, Path path, FsPermission perm, boolean overwrite) throws IOException {
   int i = 0;
   IOException lastIOE = null;
   boolean existsBefore = fs.exists(path);
   do {
     try {
       return fs.create(
           path,
           perm,
           overwrite,
           FSUtils.getDefaultBufferSize(fs),
           FSUtils.getDefaultReplication(fs, path),
           FSUtils.getDefaultBlockSize(fs, path),
           null);
     } catch (IOException ioe) {
       lastIOE = ioe;
       if (existsBefore && !overwrite) throw ioe; // a legitimate exception
       sleepBeforeRetry("Create Path with Perms", i + 1);
     }
   } while (++i <= hdfsClientRetriesNumber);
   throw new IOException("Exception in createPathWithPermsOnFileSystem", lastIOE);
 }
  @Override
  public void init(FileSystem fs, Path path, Configuration conf) throws IOException {
    // Should we do our custom WAL compression?
    boolean compress = conf.getBoolean(HConstants.ENABLE_WAL_COMPRESSION, false);
    if (compress) {
      try {
        if (this.compressionContext == null) {
          this.compressionContext = new CompressionContext(LRUDictionary.class);
        } else {
          this.compressionContext.clear();
        }
      } catch (Exception e) {
        throw new IOException("Failed to initiate CompressionContext", e);
      }
    }

    // Create a SF.Writer instance.
    try {
      // reflection for a version of SequenceFile.createWriter that doesn't
      // automatically create the parent directory (see HBASE-2312)
      this.writer =
          (SequenceFile.Writer)
              SequenceFile.class
                  .getMethod(
                      "createWriter",
                      new Class[] {
                        FileSystem.class,
                        Configuration.class,
                        Path.class,
                        Class.class,
                        Class.class,
                        Integer.TYPE,
                        Short.TYPE,
                        Long.TYPE,
                        Boolean.TYPE,
                        CompressionType.class,
                        CompressionCodec.class,
                        Metadata.class
                      })
                  .invoke(
                      null,
                      new Object[] {
                        fs,
                        conf,
                        path,
                        HLogKey.class,
                        WALEdit.class,
                        Integer.valueOf(FSUtils.getDefaultBufferSize(fs)),
                        Short.valueOf(
                            (short)
                                conf.getInt(
                                    "hbase.regionserver.hlog.replication",
                                    FSUtils.getDefaultReplication(fs, path))),
                        Long.valueOf(
                            conf.getLong(
                                "hbase.regionserver.hlog.blocksize",
                                FSUtils.getDefaultBlockSize(fs, path))),
                        Boolean.valueOf(false) /*createParent*/,
                        SequenceFile.CompressionType.NONE,
                        new DefaultCodec(),
                        createMetadata(conf, compress)
                      });
    } catch (InvocationTargetException ite) {
      // function was properly called, but threw it's own exception
      throw new IOException(ite.getCause());
    } catch (Exception e) {
      // ignore all other exceptions. related to reflection failure
    }

    // if reflection failed, use the old createWriter
    if (this.writer == null) {
      LOG.debug("new createWriter -- HADOOP-6840 -- not available");
      this.writer =
          SequenceFile.createWriter(
              fs,
              conf,
              path,
              HLogKey.class,
              WALEdit.class,
              FSUtils.getDefaultBufferSize(fs),
              (short)
                  conf.getInt(
                      "hbase.regionserver.hlog.replication",
                      FSUtils.getDefaultReplication(fs, path)),
              conf.getLong(
                  "hbase.regionserver.hlog.blocksize", FSUtils.getDefaultBlockSize(fs, path)),
              SequenceFile.CompressionType.NONE,
              new DefaultCodec(),
              null,
              createMetadata(conf, compress));
    } else {
      if (LOG.isTraceEnabled()) LOG.trace("Using new createWriter -- HADOOP-6840");
    }

    this.writer_out = getSequenceFilePrivateFSDataOutputStreamAccessible();
    if (LOG.isTraceEnabled()) LOG.trace("Path=" + path + ", compression=" + compress);
  }