Java Code Examples for org.apache.hadoop.io.nativeio.NativeIO#renameTo()

The following examples show how to use org.apache.hadoop.io.nativeio.NativeIO#renameTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileJournalManager.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Override
synchronized public void finalizeLogSegment(long firstTxId, long lastTxId)
    throws IOException {
  File inprogressFile = NNStorage.getInProgressEditsFile(sd, firstTxId);

  File dstFile = NNStorage.getFinalizedEditsFile(
      sd, firstTxId, lastTxId);
  LOG.info("Finalizing edits file " + inprogressFile + " -> " + dstFile);
  
  Preconditions.checkState(!dstFile.exists(),
      "Can't finalize edits file " + inprogressFile + " since finalized file " +
      "already exists");

  try {
    NativeIO.renameTo(inprogressFile, dstFile);
  } catch (IOException e) {
    errorReporter.reportErrorOnFile(dstFile);
    throw new IllegalStateException("Unable to finalize edits file " + inprogressFile, e);
  }

  if (inprogressFile.equals(currentInProgress)) {
    currentInProgress = null;
  }
}
 
Example 2
Source File: FileJournalManager.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private void renameSelf(String newSuffix) throws IOException {
  File src = file;
  File dst = new File(src.getParent(), src.getName() + newSuffix);
  // renameTo fails on Windows if the destination file already exists.
  try {
    if (dst.exists()) {
      if (!dst.delete()) {
        throw new IOException("Couldn't delete " + dst);
      }
    }
    NativeIO.renameTo(src, dst);
  } catch (IOException e) {
    throw new IOException(
        "Couldn't rename log " + src + " to " + dst, e);
  }
  file = dst;
}
 
Example 3
Source File: FsDatasetImpl.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Bump a replica's generation stamp to a new one.
 * Its on-disk meta file name is renamed to be the new one too.
 * 
 * @param replicaInfo a replica
 * @param newGS new generation stamp
 * @throws IOException if rename fails
 */
private void bumpReplicaGS(ReplicaInfo replicaInfo, 
    long newGS) throws IOException { 
  long oldGS = replicaInfo.getGenerationStamp();
  File oldmeta = replicaInfo.getMetaFile();
  replicaInfo.setGenerationStamp(newGS);
  File newmeta = replicaInfo.getMetaFile();

  // rename meta file to new GS
  if (LOG.isDebugEnabled()) {
    LOG.debug("Renaming " + oldmeta + " to " + newmeta);
  }
  try {
    NativeIO.renameTo(oldmeta, newmeta);
  } catch (IOException e) {
    replicaInfo.setGenerationStamp(oldGS); // restore old GS
    throw new IOException("Block " + replicaInfo + " reopen failed. " +
                          " Unable to move meta file  " + oldmeta +
                          " to " + newmeta, e);
  }
}
 
Example 4
Source File: FileJournalManager.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Override
synchronized public void finalizeLogSegment(long firstTxId, long lastTxId)
    throws IOException {
  File inprogressFile = NNStorage.getInProgressEditsFile(sd, firstTxId);

  File dstFile = NNStorage.getFinalizedEditsFile(
      sd, firstTxId, lastTxId);
  LOG.info("Finalizing edits file " + inprogressFile + " -> " + dstFile);
  
  Preconditions.checkState(!dstFile.exists(),
      "Can't finalize edits file " + inprogressFile + " since finalized file " +
      "already exists");

  try {
    NativeIO.renameTo(inprogressFile, dstFile);
  } catch (IOException e) {
    errorReporter.reportErrorOnFile(dstFile);
    throw new IllegalStateException("Unable to finalize edits file " + inprogressFile, e);
  }

  if (inprogressFile.equals(currentInProgress)) {
    currentInProgress = null;
  }
}
 
Example 5
Source File: FileJournalManager.java    From big-c with Apache License 2.0 6 votes vote down vote up
private void renameSelf(String newSuffix) throws IOException {
  File src = file;
  File dst = new File(src.getParent(), src.getName() + newSuffix);
  // renameTo fails on Windows if the destination file already exists.
  try {
    if (dst.exists()) {
      if (!dst.delete()) {
        throw new IOException("Couldn't delete " + dst);
      }
    }
    NativeIO.renameTo(src, dst);
  } catch (IOException e) {
    throw new IOException(
        "Couldn't rename log " + src + " to " + dst, e);
  }
  file = dst;
}
 
Example 6
Source File: FsDatasetImpl.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Bump a replica's generation stamp to a new one.
 * Its on-disk meta file name is renamed to be the new one too.
 * 
 * @param replicaInfo a replica
 * @param newGS new generation stamp
 * @throws IOException if rename fails
 */
private void bumpReplicaGS(ReplicaInfo replicaInfo, 
    long newGS) throws IOException { 
  long oldGS = replicaInfo.getGenerationStamp();
  File oldmeta = replicaInfo.getMetaFile();
  replicaInfo.setGenerationStamp(newGS);
  File newmeta = replicaInfo.getMetaFile();

  // rename meta file to new GS
  if (LOG.isDebugEnabled()) {
    LOG.debug("Renaming " + oldmeta + " to " + newmeta);
  }
  try {
    NativeIO.renameTo(oldmeta, newmeta);
  } catch (IOException e) {
    replicaInfo.setGenerationStamp(oldGS); // restore old GS
    throw new IOException("Block " + replicaInfo + " reopen failed. " +
                          " Unable to move meta file  " + oldmeta +
                          " to " + newmeta, e);
  }
}
 
Example 7
Source File: KeyValueContainer.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Writes to .container file.
 *
 * @param containerFile container file name
 * @param isCreate True if creating a new file. False is updating an
 *                 existing container file.
 * @throws StorageContainerException
 */
private void writeToContainerFile(File containerFile, boolean isCreate)
    throws StorageContainerException {
  File tempContainerFile = null;
  long containerId = containerData.getContainerID();
  try {
    tempContainerFile = createTempFile(containerFile);
    ContainerDataYaml.createContainerFile(
        ContainerType.KeyValueContainer, containerData, tempContainerFile);

    // NativeIO.renameTo is an atomic function. But it might fail if the
    // container file already exists. Hence, we handle the two cases
    // separately.
    if (isCreate) {
      NativeIO.renameTo(tempContainerFile, containerFile);
    } else {
      Files.move(tempContainerFile.toPath(), containerFile.toPath(),
          StandardCopyOption.REPLACE_EXISTING);
    }

  } catch (IOException ex) {
    throw new StorageContainerException("Error while creating/ updating " +
        ".container file. ContainerID: " + containerId, ex,
        CONTAINER_FILES_CREATE_ERROR);
  } finally {
    if (tempContainerFile != null && tempContainerFile.exists()) {
      if (!tempContainerFile.delete()) {
        LOG.warn("Unable to delete container temporary file: {}.",
            tempContainerFile.getAbsolutePath());
      }
    }
  }
}
 
Example 8
Source File: Storage.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public static void rename(File from, File to) throws IOException {
  try {
    NativeIO.renameTo(from, to);
  } catch (NativeIOException e) {
    throw new IOException("Failed to rename " + from.getCanonicalPath()
      + " to " + to.getCanonicalPath() + " due to failure in native rename. "
      + e.toString());
  }
}
 
Example 9
Source File: AtomicFileOutputStream.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws IOException {
  boolean triedToClose = false, success = false;
  try {
    flush();
    ((FileOutputStream)out).getChannel().force(true);

    triedToClose = true;
    super.close();
    success = true;
  } finally {
    if (success) {
      boolean renamed = tmpFile.renameTo(origFile);
      if (!renamed) {
        // On windows, renameTo does not replace.
        if (origFile.exists() && !origFile.delete()) {
          throw new IOException("Could not delete original file " + origFile);
        }
        try {
          NativeIO.renameTo(tmpFile, origFile);
        } catch (NativeIOException e) {
          throw new IOException("Could not rename temporary file " + tmpFile
            + " to " + origFile + " due to failure in native rename. "
            + e.toString());
        }
      }
    } else {
      if (!triedToClose) {
        // If we failed when flushing, try to close it to not leak an FD
        IOUtils.closeStream(out);
      }
      // close wasn't successful, try to delete the tmp file
      if (!tmpFile.delete()) {
        LOG.warn("Unable to delete tmp file " + tmpFile);
      }
    }
  }
}
 
Example 10
Source File: Storage.java    From big-c with Apache License 2.0 5 votes vote down vote up
public static void rename(File from, File to) throws IOException {
  try {
    NativeIO.renameTo(from, to);
  } catch (NativeIOException e) {
    throw new IOException("Failed to rename " + from.getCanonicalPath()
      + " to " + to.getCanonicalPath() + " due to failure in native rename. "
      + e.toString());
  }
}
 
Example 11
Source File: AtomicFileOutputStream.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws IOException {
  boolean triedToClose = false, success = false;
  try {
    flush();
    ((FileOutputStream)out).getChannel().force(true);

    triedToClose = true;
    super.close();
    success = true;
  } finally {
    if (success) {
      boolean renamed = tmpFile.renameTo(origFile);
      if (!renamed) {
        // On windows, renameTo does not replace.
        if (origFile.exists() && !origFile.delete()) {
          throw new IOException("Could not delete original file " + origFile);
        }
        try {
          NativeIO.renameTo(tmpFile, origFile);
        } catch (NativeIOException e) {
          throw new IOException("Could not rename temporary file " + tmpFile
            + " to " + origFile + " due to failure in native rename. "
            + e.toString());
        }
      }
    } else {
      if (!triedToClose) {
        // If we failed when flushing, try to close it to not leak an FD
        IOUtils.closeStream(out);
      }
      // close wasn't successful, try to delete the tmp file
      if (!tmpFile.delete()) {
        LOG.warn("Unable to delete tmp file " + tmpFile);
      }
    }
  }
}