Example usage for org.apache.hadoop.hdfs.protocol HdfsConstants LEASE_HARDLIMIT_PERIOD

List of usage examples for org.apache.hadoop.hdfs.protocol HdfsConstants LEASE_HARDLIMIT_PERIOD

Introduction

In this page you can find the example usage for org.apache.hadoop.hdfs.protocol HdfsConstants LEASE_HARDLIMIT_PERIOD.

Prototype

long LEASE_HARDLIMIT_PERIOD

To view the source code for org.apache.hadoop.hdfs.protocol HdfsConstants LEASE_HARDLIMIT_PERIOD.

Click Source Link

Document

For a HDFS client to write to a file, a lease is granted; During the lease period, no other client can write to the file.

Usage

From source file:com.mellanox.r4h.DFSClient.java

License:Apache License

/**
 * Renew leases.//from  www  .  j av a 2s.c  om
 * 
 * @return true if lease was renewed. May return false if this
 *         client has been closed or has no files open.
 **/
boolean renewLease() throws IOException {
    if (clientRunning && !isFilesBeingWrittenEmpty()) {
        try {
            namenode.renewLease(clientName);
            updateLastLeaseRenewal();
            return true;
        } catch (IOException e) {
            // Abort if the lease has already expired.
            final long elapsed = Time.monotonicNow() - getLastLeaseRenewal();
            if (elapsed > HdfsConstants.LEASE_HARDLIMIT_PERIOD) {
                LOG.warn("Failed to renew lease for " + clientName + " for " + (elapsed / 1000)
                        + " seconds (>= hard-limit =" + (HdfsConstants.LEASE_HARDLIMIT_PERIOD / 1000)
                        + " seconds.) " + "Closing all files being written ...", e);
                closeAllFilesBeingWritten(true);
            } else {
                // Let the lease renewer handle it and retry.
                throw e;
            }
        }
    }
    return false;
}

From source file:org.apache.giraffa.TestLeaseManagement.java

License:Apache License

@Test
public void testLeaseRecovery() throws IOException {
    String src = "/testLeaseRecovery";
    Path path = new Path(src);

    HRegionServer server = UTIL.getHBaseCluster().getRegionServer(0);
    LeaseManager leaseManager = LeaseManager
            .originateSharedLeaseManager(server.getRpcServer().getListenerAddress().toString());

    FSDataOutputStream outputStream = grfs.create(path);
    String clientName = grfs.grfaClient.getClientName();
    outputStream.write(1);/* w  w w .  j a va  2 s .com*/
    outputStream.write(2);
    outputStream.hflush();
    try {
        leaseManager.setHardLimit(10L);
        INodeFile iNode = null;
        for (int i = 0; i < 100; i++) {
            leaseManager.triggerLeaseRecovery();
            try {
                Thread.sleep(100L);
            } catch (InterruptedException ignored) {
            }
            iNode = INodeFile.valueOf(nodeManager.getINode(src));
            if (iNode.getFileState() == FileState.CLOSED)
                break;
        }
        assertThat(iNode.getFileState(), is(FileState.CLOSED));
        assertThat(iNode.getLen(), is(2L));
        assertThat(iNode.getLease(), is(nullValue()));
        assertThat(leaseManager.getLeases(clientName), is(nullValue()));
    } finally {
        leaseManager.setHardLimit(HdfsConstants.LEASE_HARDLIMIT_PERIOD);
        IOUtils.closeStream(outputStream);
    }
}