Example usage for org.apache.hadoop.hdfs.shortcircuit ShortCircuitCache freeSlot

List of usage examples for org.apache.hadoop.hdfs.shortcircuit ShortCircuitCache freeSlot

Introduction

In this page you can find the example usage for org.apache.hadoop.hdfs.shortcircuit ShortCircuitCache freeSlot.

Prototype

public void freeSlot(Slot slot) 

Source Link

Document

Free a slot immediately.

Usage

From source file:BlockReaderFactory.java

License:Apache License

/**
 * Fetch a pair of short-circuit block descriptors from a local DataNode.
 *
 * @return    Null if we could not communicate with the datanode,
 *            a new ShortCircuitReplicaInfo object otherwise.
 *            ShortCircuitReplicaInfo objects may contain either an InvalidToken
 *            exception, or a ShortCircuitReplica object ready to use.
 *///from w  w  w  .j a  va2s. c  o m
@Override
public ShortCircuitReplicaInfo createShortCircuitReplicaInfo() {
    if (createShortCircuitReplicaInfoCallback != null) {
        ShortCircuitReplicaInfo info = createShortCircuitReplicaInfoCallback.createShortCircuitReplicaInfo();
        if (info != null)
            return info;
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace(this + ": trying to create ShortCircuitReplicaInfo.");
    }
    BlockReaderPeer curPeer;
    while (true) {
        curPeer = nextDomainPeer();
        if (curPeer == null)
            break;
        if (curPeer.fromCache)
            remainingCacheTries--;
        DomainPeer peer = (DomainPeer) curPeer.peer;
        Slot slot = null;
        ShortCircuitCache cache = clientContext.getShortCircuitCache();
        try {
            MutableBoolean usedPeer = new MutableBoolean(false);
            slot = cache.allocShmSlot(datanode, peer, usedPeer,
                    new ExtendedBlockId(block.getBlockId(), block.getBlockPoolId()), clientName);
            if (usedPeer.booleanValue()) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace(this + ": allocShmSlot used up our previous socket " + peer.getDomainSocket()
                            + ".  Allocating a new one...");
                }
                curPeer = nextDomainPeer();
                if (curPeer == null)
                    break;
                peer = (DomainPeer) curPeer.peer;
            }
            ShortCircuitReplicaInfo info = requestFileDescriptors(peer, slot);
            clientContext.getPeerCache().put(datanode, peer);
            return info;
        } catch (IOException e) {
            if (slot != null) {
                cache.freeSlot(slot);
            }
            if (curPeer.fromCache) {
                // Handle an I/O error we got when using a cached socket.
                // These are considered less serious, because the socket may be stale.
                if (LOG.isDebugEnabled()) {
                    LOG.debug(this + ": closing stale domain peer " + peer, e);
                }
                IOUtils.cleanup(LOG, peer);
            } else {
                // Handle an I/O error we got when using a newly created socket.
                // We temporarily disable the domain socket path for a few minutes in
                // this case, to prevent wasting more time on it.
                LOG.warn(this + ": I/O error requesting file descriptors.  " + "Disabling domain socket "
                        + peer.getDomainSocket(), e);
                IOUtils.cleanup(LOG, peer);
                clientContext.getDomainSocketFactory().disableDomainSocketPath(pathInfo.getPath());
                return null;
            }
        }
    }
    return null;
}