Example usage for org.apache.hadoop.hdfs DistributedFileSystem recoverLease

List of usage examples for org.apache.hadoop.hdfs DistributedFileSystem recoverLease

Introduction

In this page you can find the example usage for org.apache.hadoop.hdfs DistributedFileSystem recoverLease.

Prototype

public boolean recoverLease(final Path f) throws IOException 

Source Link

Document

Start the lease recovery of a file

Usage

From source file:co.cask.tephra.persist.HDFSUtil.java

License:Apache License

/**
 * Try to recover the lease.//from www .  j av a  2  s  .co  m
 * @param dfs The filesystem instance.
 * @param nbAttempt Count number of this attempt.
 * @param p Path of the file to recover.
 * @param startWaiting Timestamp of when we started attempting to recover the file lease.
 * @return True if dfs#recoverLease came by true.
 * @throws java.io.FileNotFoundException
 */
boolean recoverLease(final DistributedFileSystem dfs, final int nbAttempt, final Path p,
        final long startWaiting) throws FileNotFoundException {
    boolean recovered = false;
    try {
        recovered = dfs.recoverLease(p);
        LOG.info("recoverLease=" + recovered + ", " + getLogMessageDetail(nbAttempt, p, startWaiting));
    } catch (IOException e) {
        if (e instanceof LeaseExpiredException && e.getMessage().contains("File does not exist")) {
            // This exception comes out instead of FNFE, fix it
            throw new FileNotFoundException("The given file wasn't found at " + p);
        } else if (e instanceof FileNotFoundException) {
            throw (FileNotFoundException) e;
        }
        LOG.warn(getLogMessageDetail(nbAttempt, p, startWaiting), e);
    }
    return recovered;
}

From source file:com.mvad.flink.demo.streaming.lib.sink.bucketing.BucketingSink.java

License:Apache License

@Override
public void restoreState(State<T> state) {
    this.state = state;

    FileSystem fs;/* w w w.  ja  v  a  2s . c o  m*/
    try {
        fs = new Path(basePath).getFileSystem(HadoopFileSystem.getHadoopConfiguration());
    } catch (IOException e) {
        LOG.error("Error while creating FileSystem in checkpoint restore.", e);
        throw new RuntimeException("Error while creating FileSystem in checkpoint restore.", e);
    }

    for (BucketState<T> bucketState : state.bucketStates.values()) {
        // we can clean all the pending files since they where renamed to final files
        // after this checkpoint was successful
        bucketState.pendingFiles.clear();

        if (bucketState.currentFile != null) {
            // We were writing to a file when the last checkpoint occured. This file can either
            // be still in-progress or became a pending file at some point after the checkpoint.
            // Either way, we have to truncate it back to a valid state (or write a .valid-length)
            // file that specifies up to which length it is valid and rename it to the final name
            // before starting a new bucket file.
            Path partPath = new Path(bucketState.currentFile);
            try {
                Path partPendingPath = new Path(partPath.getParent(), pendingPrefix + partPath.getName())
                        .suffix(pendingSuffix);
                Path partInProgressPath = new Path(partPath.getParent(), inProgressPrefix + partPath.getName())
                        .suffix(inProgressSuffix);

                if (fs.exists(partPendingPath)) {
                    LOG.debug(
                            "In-progress file {} has been moved to pending after checkpoint, moving to final location.",
                            partPath);
                    // has been moved to pending in the mean time, rename to final location
                    fs.rename(partPendingPath, partPath);
                } else if (fs.exists(partInProgressPath)) {
                    LOG.debug("In-progress file {} is still in-progress, moving to final location.", partPath);
                    // it was still in progress, rename to final path
                    fs.rename(partInProgressPath, partPath);
                } else if (fs.exists(partPath)) {
                    LOG.debug("In-Progress file {} was already moved to final location {}.",
                            bucketState.currentFile, partPath);
                } else {
                    LOG.debug(
                            "In-Progress file {} was neither moved to pending nor is still in progress. Possibly, "
                                    + "it was moved to final location by a previous snapshot restore",
                            bucketState.currentFile);
                }

                refTruncate = reflectTruncate(fs);
                // truncate it or write a ".valid-length" file to specify up to which point it is valid
                if (refTruncate != null) {
                    LOG.debug("Truncating {} to valid length {}", partPath, bucketState.currentFileValidLength);
                    // some-one else might still hold the lease from a previous try, we are
                    // recovering, after all ...
                    if (fs instanceof DistributedFileSystem) {
                        DistributedFileSystem dfs = (DistributedFileSystem) fs;
                        LOG.debug("Trying to recover file lease {}", partPath);
                        dfs.recoverLease(partPath);
                        boolean isclosed = dfs.isFileClosed(partPath);
                        StopWatch sw = new StopWatch();
                        sw.start();
                        while (!isclosed) {
                            if (sw.getTime() > asyncTimeout) {
                                break;
                            }
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e1) {
                                // ignore it
                            }
                            isclosed = dfs.isFileClosed(partPath);
                        }
                    }
                    Boolean truncated = (Boolean) refTruncate.invoke(fs, partPath,
                            bucketState.currentFileValidLength);
                    if (!truncated) {
                        LOG.debug("Truncate did not immediately complete for {}, waiting...", partPath);

                        // we must wait for the asynchronous truncate operation to complete
                        StopWatch sw = new StopWatch();
                        sw.start();
                        long newLen = fs.getFileStatus(partPath).getLen();
                        while (newLen != bucketState.currentFileValidLength) {
                            if (sw.getTime() > asyncTimeout) {
                                break;
                            }
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e1) {
                                // ignore it
                            }
                            newLen = fs.getFileStatus(partPath).getLen();
                        }
                        if (newLen != bucketState.currentFileValidLength) {
                            throw new RuntimeException("Truncate did not truncate to right length. Should be "
                                    + bucketState.currentFileValidLength + " is " + newLen + ".");
                        }
                    }

                } else {
                    LOG.debug("Writing valid-length file for {} to specify valid length {}", partPath,
                            bucketState.currentFileValidLength);
                    Path validLengthFilePath = new Path(partPath.getParent(),
                            validLengthPrefix + partPath.getName()).suffix(validLengthSuffix);
                    if (!fs.exists(validLengthFilePath)) {
                        FSDataOutputStream lengthFileOut = fs.create(validLengthFilePath);
                        lengthFileOut.writeUTF(Long.toString(bucketState.currentFileValidLength));
                        lengthFileOut.close();
                    }
                }

                // Now that we've restored the bucket to a valid state, reset the current file info
                bucketState.currentFile = null;
                bucketState.currentFileValidLength = -1;
            } catch (IOException e) {
                LOG.error("Error while restoring BucketingSink state.", e);
                throw new RuntimeException("Error while restoring BucketingSink state.", e);
            } catch (InvocationTargetException | IllegalAccessException e) {
                LOG.error("Cound not invoke truncate.", e);
                throw new RuntimeException("Could not invoke truncate.", e);
            }
        }

        LOG.debug("Clearing pending/in-progress files.");

        // Move files that are confirmed by a checkpoint but did not get moved to final location
        // because the checkpoint notification did not happen before a failure

        Set<Long> pastCheckpointIds = bucketState.pendingFilesPerCheckpoint.keySet();
        LOG.debug("Moving pending files to final location on restore.");
        for (Long pastCheckpointId : pastCheckpointIds) {
            // All the pending files are buckets that have been completed but are waiting to be renamed
            // to their final name
            for (String filename : bucketState.pendingFilesPerCheckpoint.get(pastCheckpointId)) {
                Path finalPath = new Path(filename);
                Path pendingPath = new Path(finalPath.getParent(), pendingPrefix + finalPath.getName())
                        .suffix(pendingSuffix);

                try {
                    if (fs.exists(pendingPath)) {
                        LOG.debug(
                                "(RESTORE) Moving pending file {} to final location after complete checkpoint {}.",
                                pendingPath, pastCheckpointId);
                        fs.rename(pendingPath, finalPath);
                    }
                } catch (IOException e) {
                    LOG.error("(RESTORE) Error while renaming pending file {} to final path {}: {}",
                            pendingPath, finalPath, e);
                    throw new RuntimeException(
                            "Error while renaming pending file " + pendingPath + " to final path " + finalPath,
                            e);
                }
            }
        }

        synchronized (bucketState.pendingFilesPerCheckpoint) {
            bucketState.pendingFilesPerCheckpoint.clear();
        }
    }

    // we need to get this here since open() has not yet been called
    int subtaskIndex = getRuntimeContext().getIndexOfThisSubtask();
    // delete pending files
    try {

        RemoteIterator<LocatedFileStatus> bucketFiles = fs.listFiles(new Path(basePath), true);

        while (bucketFiles.hasNext()) {
            LocatedFileStatus file = bucketFiles.next();
            if (file.getPath().toString().endsWith(pendingSuffix)) {
                // only delete files that contain our subtask index
                if (file.getPath().toString().contains(partPrefix + "-" + subtaskIndex + "-")) {
                    LOG.debug("(RESTORE) Deleting pending file {}", file.getPath().toString());
                    fs.delete(file.getPath(), true);
                }
            }
            if (file.getPath().toString().endsWith(inProgressSuffix)) {
                // only delete files that contain our subtask index
                if (file.getPath().toString().contains(partPrefix + "-" + subtaskIndex + "-")) {
                    LOG.debug("(RESTORE) Deleting in-progress file {}", file.getPath().toString());
                    fs.delete(file.getPath(), true);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("Error while deleting old pending files: {}", e);
        throw new RuntimeException("Error while deleting old pending files.", e);
    }
}

From source file:com.uber.hoodie.common.util.FSUtils.java

License:Apache License

/**
 * When a file was opened and the task died without closing the stream, another task executor
 * cannot open because the existing lease will be active. We will try to recover the lease, from
 * HDFS. If a data node went down, it takes about 10 minutes for the lease to be rocovered. But if
 * the client dies, this should be instant.
 *//*w  w  w  .  j  a v  a  2s .c o  m*/
public static boolean recoverDFSFileLease(final DistributedFileSystem dfs, final Path p)
        throws IOException, InterruptedException {
    LOG.info("Recover lease on dfs file " + p);
    // initiate the recovery
    boolean recovered = false;
    for (int nbAttempt = 0; nbAttempt < MAX_ATTEMPTS_RECOVER_LEASE; nbAttempt++) {
        LOG.info("Attempt " + nbAttempt + " to recover lease on dfs file " + p);
        recovered = dfs.recoverLease(p);
        if (recovered) {
            break;
        }
        // Sleep for 1 second before trying again. Typically it takes about 2-3 seconds to recover
        // under default settings
        Thread.sleep(1000);
    }
    return recovered;
}

From source file:org.apache.accumulo.server.master.recovery.HadoopLogCloser.java

License:Apache License

@Override
public long close(AccumuloConfiguration conf, VolumeManager fs, Path source) throws IOException {
    FileSystem ns = fs.getVolumeByPath(source).getFileSystem();

    // if path points to a viewfs path, then resolve to underlying filesystem
    if (ViewFSUtils.isViewFS(ns)) {
        Path newSource = ns.resolvePath(source);
        if (!newSource.equals(source) && newSource.toUri().getScheme() != null) {
            ns = newSource.getFileSystem(CachedConfiguration.getInstance());
            source = newSource;//from  w  w w.  j av  a 2s. c  o m
        }
    }

    if (ns instanceof DistributedFileSystem) {
        DistributedFileSystem dfs = (DistributedFileSystem) ns;
        try {
            if (!dfs.recoverLease(source)) {
                log.info("Waiting for file to be closed " + source.toString());
                return conf.getTimeInMillis(Property.MASTER_LEASE_RECOVERY_WAITING_PERIOD);
            }
            log.info("Recovered lease on " + source.toString());
        } catch (FileNotFoundException ex) {
            throw ex;
        } catch (Exception ex) {
            log.warn("Error recovering lease on " + source.toString(), ex);
            ns.append(source).close();
            log.info("Recovered lease on " + source.toString() + " using append");
        }
    } else if (ns instanceof LocalFileSystem || ns instanceof RawLocalFileSystem) {
        // ignore
    } else {
        throw new IllegalStateException("Don't know how to recover a lease for " + ns.getClass().getName());
    }
    return 0;
}

From source file:org.apache.carbondata.core.util.path.HDFSLeaseUtils.java

License:Apache License

/**
 * Recovers lease on a file//w  w w. j  a  v  a 2 s.  c  o  m
 *
 * @param filePath
 * @param path
 * @param fs
 * @return
 * @throws IOException
 */
private static boolean recoverLeaseOnFile(String filePath, Path path, DistributedFileSystem fs)
        throws IOException {
    int maxAttempts = getLeaseRecoveryRetryCount();
    int retryInterval = getLeaseRecoveryRetryInterval();
    boolean leaseRecovered = false;
    IOException ioException = null;
    for (int retryCount = 1; retryCount <= maxAttempts; retryCount++) {
        try {
            leaseRecovered = fs.recoverLease(path);
            if (!leaseRecovered) {
                try {
                    LOGGER.info("Failed to recover lease after attempt " + retryCount
                            + " . Will try again after " + retryInterval + " ms...");
                    Thread.sleep(retryInterval);
                } catch (InterruptedException e) {
                    LOGGER.error("Interrupted exception occurred while recovering lease for file : " + filePath,
                            e);
                }
            }
        } catch (IOException e) {
            if (e instanceof LeaseExpiredException && e.getMessage().contains("File does not exist")) {
                LOGGER.error("The given file does not exist at path " + filePath);
                throw e;
            } else if (e instanceof FileNotFoundException) {
                LOGGER.error("The given file does not exist at path " + filePath);
                throw e;
            } else {
                LOGGER.error("Recover lease threw exception : " + e.getMessage());
                ioException = e;
            }
        }
        LOGGER.info("Retrying again after interval of " + retryInterval + " ms...");
    }
    if (leaseRecovered) {
        LOGGER.info("Successfully able to recover lease on file: " + filePath);
        return true;
    } else {
        LOGGER.error("Failed to recover lease on file: " + filePath + " after retrying for " + maxAttempts
                + " at an interval of " + retryInterval);
        if (null != ioException) {
            throw ioException;
        } else {
            return false;
        }
    }
}

From source file:org.apache.flink.runtime.fs.hdfs.HadoopRecoverableFsDataOutputStream.java

License:Apache License

/**
 * Called when resuming execution after a failure and waits until the lease
 * of the file we are resuming is free./* w  ww.j a v a 2  s.  c  o  m*/
 *
 * <p>The lease of the file we are resuming writing/committing to may still
 * belong to the process that failed previously and whose state we are
 * recovering.
 *
 * @param path The path to the file we want to resume writing to.
 */
private boolean waitUntilLeaseIsRevoked(final Path path) throws IOException {
    Preconditions.checkState(fs instanceof DistributedFileSystem);

    final DistributedFileSystem dfs = (DistributedFileSystem) fs;
    dfs.recoverLease(path);

    final Deadline deadline = Deadline.now().plus(Duration.ofMillis(LEASE_TIMEOUT));

    final StopWatch sw = new StopWatch();
    sw.start();

    boolean isClosed = dfs.isFileClosed(path);
    while (!isClosed && deadline.hasTimeLeft()) {
        try {
            Thread.sleep(500L);
        } catch (InterruptedException e1) {
            throw new IOException("Recovering the lease failed: ", e1);
        }
        isClosed = dfs.isFileClosed(path);
    }
    return isClosed;
}

From source file:org.apache.flink.streaming.connectors.fs.bucketing.BucketingSink.java

License:Apache License

private void handlePendingInProgressFile(String file, long validLength) {
    if (file != null) {

        // We were writing to a file when the last checkpoint occurred. This file can either
        // be still in-progress or became a pending file at some point after the checkpoint.
        // Either way, we have to truncate it back to a valid state (or write a .valid-length
        // file that specifies up to which length it is valid) and rename it to the final name
        // before starting a new bucket file.

        Path partPath = new Path(file);
        try {//from  ww w  . j  a v  a2s  .c  o m
            Path partPendingPath = getPendingPathFor(partPath);
            Path partInProgressPath = getInProgressPathFor(partPath);

            if (fs.exists(partPendingPath)) {
                LOG.debug(
                        "In-progress file {} has been moved to pending after checkpoint, moving to final location.",
                        partPath);
                // has been moved to pending in the mean time, rename to final location
                fs.rename(partPendingPath, partPath);
            } else if (fs.exists(partInProgressPath)) {
                LOG.debug("In-progress file {} is still in-progress, moving to final location.", partPath);
                // it was still in progress, rename to final path
                fs.rename(partInProgressPath, partPath);
            } else if (fs.exists(partPath)) {
                LOG.debug("In-Progress file {} was already moved to final location {}.", file, partPath);
            } else {
                LOG.debug(
                        "In-Progress file {} was neither moved to pending nor is still in progress. Possibly, "
                                + "it was moved to final location by a previous snapshot restore",
                        file);
            }

            // We use reflection to get the .truncate() method, this
            // is only available starting with Hadoop 2.7
            if (this.refTruncate == null) {
                this.refTruncate = reflectTruncate(fs);
            }

            // truncate it or write a ".valid-length" file to specify up to which point it is valid
            if (refTruncate != null) {
                LOG.debug("Truncating {} to valid length {}", partPath, validLength);
                // some-one else might still hold the lease from a previous try, we are
                // recovering, after all ...
                if (fs instanceof DistributedFileSystem) {
                    DistributedFileSystem dfs = (DistributedFileSystem) fs;
                    LOG.debug("Trying to recover file lease {}", partPath);
                    dfs.recoverLease(partPath);
                    boolean isclosed = dfs.isFileClosed(partPath);
                    StopWatch sw = new StopWatch();
                    sw.start();
                    while (!isclosed) {
                        if (sw.getTime() > asyncTimeout) {
                            break;
                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e1) {
                            // ignore it
                        }
                        isclosed = dfs.isFileClosed(partPath);
                    }
                }
                Boolean truncated = (Boolean) refTruncate.invoke(fs, partPath, validLength);
                if (!truncated) {
                    LOG.debug("Truncate did not immediately complete for {}, waiting...", partPath);

                    // we must wait for the asynchronous truncate operation to complete
                    StopWatch sw = new StopWatch();
                    sw.start();
                    long newLen = fs.getFileStatus(partPath).getLen();
                    while (newLen != validLength) {
                        if (sw.getTime() > asyncTimeout) {
                            break;
                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e1) {
                            // ignore it
                        }
                        newLen = fs.getFileStatus(partPath).getLen();
                    }
                    if (newLen != validLength) {
                        throw new RuntimeException("Truncate did not truncate to right length. Should be "
                                + validLength + " is " + newLen + ".");
                    }
                }
            } else {
                LOG.debug("Writing valid-length file for {} to specify valid length {}", partPath, validLength);
                Path validLengthFilePath = getValidLengthPathFor(partPath);
                if (!fs.exists(validLengthFilePath) && fs.exists(partPath)) {
                    FSDataOutputStream lengthFileOut = fs.create(validLengthFilePath);
                    lengthFileOut.writeUTF(Long.toString(validLength));
                    lengthFileOut.close();
                }
            }

        } catch (IOException e) {
            LOG.error("Error while restoring BucketingSink state.", e);
            throw new RuntimeException("Error while restoring BucketingSink state.", e);
        } catch (InvocationTargetException | IllegalAccessException e) {
            LOG.error("Could not invoke truncate.", e);
            throw new RuntimeException("Could not invoke truncate.", e);
        }
    }
}

From source file:org.apache.flink.streaming.connectors.fs.RollingSink.java

License:Apache License

@Override
public void restoreState(BucketState state) {
    bucketState = state;//w  w w  .jav  a 2s . c  o  m
    // we can clean all the pending files since they where renamed to final files
    // after this checkpoint was successfull
    bucketState.pendingFiles.clear();
    FileSystem fs = null;
    try {
        fs = new Path(basePath).getFileSystem(new org.apache.hadoop.conf.Configuration());
    } catch (IOException e) {
        LOG.error("Error while creating FileSystem in checkpoint restore.", e);
        throw new RuntimeException("Error while creating FileSystem in checkpoint restore.", e);
    }
    if (bucketState.currentFile != null) {
        // We were writing to a file when the last checkpoint occured. This file can either
        // be still in-progress or became a pending file at some point after the checkpoint.
        // Either way, we have to truncate it back to a valid state (or write a .valid-length)
        // file that specifies up to which length it is valid and rename it to the final name
        // before starting a new bucket file.
        Path partPath = new Path(bucketState.currentFile);
        try {
            Path partPendingPath = new Path(partPath.getParent(), pendingPrefix + partPath.getName())
                    .suffix(pendingSuffix);
            Path partInProgressPath = new Path(partPath.getParent(), inProgressPrefix + partPath.getName())
                    .suffix(inProgressSuffix);

            if (fs.exists(partPendingPath)) {
                LOG.debug(
                        "In-progress file {} has been moved to pending after checkpoint, moving to final location.",
                        partPath);
                // has been moved to pending in the mean time, rename to final location
                fs.rename(partPendingPath, partPath);
            } else if (fs.exists(partInProgressPath)) {
                LOG.debug("In-progress file {} is still in-progress, moving to final location.", partPath);
                // it was still in progress, rename to final path
                fs.rename(partInProgressPath, partPath);
            } else if (fs.exists(partPath)) {
                LOG.debug("In-Progress file {} was already moved to final location {}.",
                        bucketState.currentFile, partPath);
            } else {
                LOG.debug(
                        "In-Progress file {} was neither moved to pending nor is still in progress. Possibly, "
                                + "it was moved to final location by a previous snapshot restore",
                        bucketState.currentFile);
            }

            refTruncate = reflectTruncate(fs);
            // truncate it or write a ".valid-length" file to specify up to which point it is valid
            if (refTruncate != null) {
                LOG.debug("Truncating {} to valid length {}", partPath, bucketState.currentFileValidLength);
                // some-one else might still hold the lease from a previous try, we are
                // recovering, after all ...
                if (fs instanceof DistributedFileSystem) {
                    DistributedFileSystem dfs = (DistributedFileSystem) fs;
                    LOG.debug("Trying to recover file lease {}", partPath);
                    dfs.recoverLease(partPath);
                    boolean isclosed = dfs.isFileClosed(partPath);
                    StopWatch sw = new StopWatch();
                    sw.start();
                    while (!isclosed) {
                        if (sw.getTime() > asyncTimeout) {
                            break;
                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e1) {
                            // ignore it
                        }
                        isclosed = dfs.isFileClosed(partPath);
                    }
                }
                Boolean truncated = (Boolean) refTruncate.invoke(fs, partPath,
                        bucketState.currentFileValidLength);
                if (!truncated) {
                    LOG.debug("Truncate did not immediately complete for {}, waiting...", partPath);

                    // we must wait for the asynchronous truncate operation to complete
                    StopWatch sw = new StopWatch();
                    sw.start();
                    long newLen = fs.getFileStatus(partPath).getLen();
                    while (newLen != bucketState.currentFileValidLength) {
                        if (sw.getTime() > asyncTimeout) {
                            break;
                        }
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e1) {
                            // ignore it
                        }
                        newLen = fs.getFileStatus(partPath).getLen();
                    }
                    if (newLen != bucketState.currentFileValidLength) {
                        throw new RuntimeException("Truncate did not truncate to right length. Should be "
                                + bucketState.currentFileValidLength + " is " + newLen + ".");
                    }
                }

            } else {
                LOG.debug("Writing valid-length file for {} to specify valid length {}", partPath,
                        bucketState.currentFileValidLength);
                Path validLengthFilePath = new Path(partPath.getParent(),
                        validLengthPrefix + partPath.getName()).suffix(validLengthSuffix);
                if (!fs.exists(validLengthFilePath)) {
                    FSDataOutputStream lengthFileOut = fs.create(validLengthFilePath);
                    lengthFileOut.writeUTF(Long.toString(bucketState.currentFileValidLength));
                    lengthFileOut.close();
                }
            }

            // invalidate in the state object
            bucketState.currentFile = null;
            bucketState.currentFileValidLength = -1;
        } catch (IOException e) {
            LOG.error("Error while restoring RollingSink state.", e);
            throw new RuntimeException("Error while restoring RollingSink state.", e);
        } catch (InvocationTargetException | IllegalAccessException e) {
            LOG.error("Cound not invoke truncate.", e);
            throw new RuntimeException("Could not invoke truncate.", e);
        }
    }

    LOG.debug("Clearing pending/in-progress files.");

    // Move files that are confirmed by a checkpoint but did not get moved to final location
    // because the checkpoint notification did not happen before a failure

    Set<Long> pastCheckpointIds = bucketState.pendingFilesPerCheckpoint.keySet();
    LOG.debug("Moving pending files to final location on restore.");
    for (Long pastCheckpointId : pastCheckpointIds) {
        // All the pending files are buckets that have been completed but are waiting to be renamed
        // to their final name
        for (String filename : bucketState.pendingFilesPerCheckpoint.get(pastCheckpointId)) {
            Path finalPath = new Path(filename);
            Path pendingPath = new Path(finalPath.getParent(), pendingPrefix + finalPath.getName())
                    .suffix(pendingSuffix);

            try {
                if (fs.exists(pendingPath)) {
                    LOG.debug(
                            "(RESTORE) Moving pending file {} to final location after complete checkpoint {}.",
                            pendingPath, pastCheckpointId);
                    fs.rename(pendingPath, finalPath);
                }
            } catch (IOException e) {
                LOG.error("(RESTORE) Error while renaming pending file {} to final path {}: {}", pendingPath,
                        finalPath, e);
                throw new RuntimeException(
                        "Error while renaming pending file " + pendingPath + " to final path " + finalPath, e);
            }
        }
    }
    bucketState.pendingFiles.clear();
    synchronized (bucketState.pendingFilesPerCheckpoint) {
        bucketState.pendingFilesPerCheckpoint.clear();
    }

    // we need to get this here since open() has not yet been called
    int subtaskIndex = getRuntimeContext().getIndexOfThisSubtask();
    // delete pending files
    try {

        RemoteIterator<LocatedFileStatus> bucketFiles = fs.listFiles(new Path(basePath), true);

        while (bucketFiles.hasNext()) {
            LocatedFileStatus file = bucketFiles.next();
            if (file.getPath().toString().endsWith(pendingSuffix)) {
                // only delete files that contain our subtask index
                if (file.getPath().toString().contains(partPrefix + "-" + subtaskIndex + "-")) {
                    LOG.debug("(RESTORE) Deleting pending file {}", file.getPath().toString());
                    fs.delete(file.getPath(), true);
                }
            }
            if (file.getPath().toString().endsWith(inProgressSuffix)) {
                // only delete files that contain our subtask index
                if (file.getPath().toString().contains(partPrefix + "-" + subtaskIndex + "-")) {
                    LOG.debug("(RESTORE) Deleting in-progress file {}", file.getPath().toString());
                    fs.delete(file.getPath(), true);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("Error while deleting old pending files: {}", e);
        throw new RuntimeException("Error while deleting old pending files.", e);
    }
}

From source file:org.apache.solr.util.FSHDFSUtils.java

License:Apache License

/**
 * Try to recover the lease./*from   w  w w  .ja  va2 s .  c o m*/
 * @return True if dfs#recoverLease came by true.
 */
static boolean recoverLease(final DistributedFileSystem dfs, final int nbAttempt, final Path p,
        final long startWaiting) throws FileNotFoundException {
    boolean recovered = false;
    try {
        recovered = dfs.recoverLease(p);
        log.info("recoverLease=" + recovered + ", " + getLogMessageDetail(nbAttempt, p, startWaiting));
    } catch (IOException e) {
        if (e instanceof LeaseExpiredException && e.getMessage().contains("File does not exist")) {
            // This exception comes out instead of FNFE, fix it
            throw new FileNotFoundException("The given transactionlog file wasn't found at " + p);
        } else if (e instanceof FileNotFoundException) {
            throw (FileNotFoundException) e;
        }
        log.warn(getLogMessageDetail(nbAttempt, p, startWaiting), e);
    }
    return recovered;
}

From source file:org.hypertable.DfsBroker.hadoop.FSHDFSUtils.java

License:Apache License

/**
 * Try to recover the lease.//  ww  w  . ja v  a2s  . c  o  m
 * @param dfs
 * @param nbAttempt
 * @param p
 * @param startWaiting
 * @return True if dfs#recoverLease came by true.
 * @throws FileNotFoundException
 */
boolean recoverLease(final DistributedFileSystem dfs, final int nbAttempt, final Path p,
        final long startWaiting) throws FileNotFoundException {
    boolean recovered = false;
    try {
        recovered = dfs.recoverLease(p);
        LOG.info("recoverLease=" + recovered + ", " + getLogMessageDetail(nbAttempt, p, startWaiting));
    } catch (IOException e) {
        if (e instanceof LeaseExpiredException && e.getMessage().contains("File does not exist")) {
            // This exception comes out instead of FNFE, fix it
            throw new FileNotFoundException("The given HLog wasn't found at " + p);
        } else if (e instanceof FileNotFoundException) {
            throw (FileNotFoundException) e;
        }
        LOG.warn(getLogMessageDetail(nbAttempt, p, startWaiting), e);
    }
    return recovered;
}