Example usage for org.apache.hadoop.hdfs.net DomainPeer getDomainSocket

List of usage examples for org.apache.hadoop.hdfs.net DomainPeer getDomainSocket

Introduction

In this page you can find the example usage for org.apache.hadoop.hdfs.net DomainPeer getDomainSocket.

Prototype

@Override
    public DomainSocket getDomainSocket() 

Source Link

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   www .j  av  a  2  s .  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;
}

From source file:BlockReaderFactory.java

License:Apache License

/**
 * Request file descriptors from a DomainPeer.
 *
 * @param peer   The peer to use for communication.
 * @param slot   If non-null, the shared memory slot to associate with the 
 *               new ShortCircuitReplica.
 * /* w  w  w.  j a  va2s .c  o m*/
 * @return  A ShortCircuitReplica object if we could communicate with the
 *          datanode; null, otherwise. 
 * @throws  IOException If we encountered an I/O exception while communicating
 *          with the datanode.
 */
private ShortCircuitReplicaInfo requestFileDescriptors(DomainPeer peer, Slot slot) throws IOException {
    ShortCircuitCache cache = clientContext.getShortCircuitCache();
    final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(peer.getOutputStream()));
    SlotId slotId = slot == null ? null : slot.getSlotId();
    new Sender(out).requestShortCircuitFds(block, token, slotId, 1);
    DataInputStream in = new DataInputStream(peer.getInputStream());
    BlockOpResponseProto resp = BlockOpResponseProto.parseFrom(PBHelper.vintPrefixed(in));
    DomainSocket sock = peer.getDomainSocket();
    switch (resp.getStatus()) {
    case SUCCESS:
        byte buf[] = new byte[1];
        FileInputStream fis[] = new FileInputStream[2];
        sock.recvFileInputStreams(fis, buf, 0, buf.length);
        ShortCircuitReplica replica = null;
        try {
            ExtendedBlockId key = new ExtendedBlockId(block.getBlockId(), block.getBlockPoolId());
            replica = new ShortCircuitReplica(key, fis[0], fis[1], cache, Time.monotonicNow(), slot);
        } catch (IOException e) {
            // This indicates an error reading from disk, or a format error.  Since
            // it's not a socket communication problem, we return null rather than
            // throwing an exception.
            LOG.warn(this + ": error creating ShortCircuitReplica.", e);
            return null;
        } finally {
            if (replica == null) {
                IOUtils.cleanup(DFSClient.LOG, fis[0], fis[1]);
            }
        }
        return new ShortCircuitReplicaInfo(replica);
    case ERROR_UNSUPPORTED:
        if (!resp.hasShortCircuitAccessVersion()) {
            LOG.warn("short-circuit read access is disabled for " + "DataNode " + datanode + ".  reason: "
                    + resp.getMessage());
            clientContext.getDomainSocketFactory().disableShortCircuitForPath(pathInfo.getPath());
        } else {
            LOG.warn("short-circuit read access for the file " + fileName + " is disabled for DataNode "
                    + datanode + ".  reason: " + resp.getMessage());
        }
        return null;
    case ERROR_ACCESS_TOKEN:
        String msg = "access control error while " + "attempting to set up short-circuit access to " + fileName
                + resp.getMessage();
        if (LOG.isDebugEnabled()) {
            LOG.debug(this + ":" + msg);
        }
        return new ShortCircuitReplicaInfo(new InvalidToken(msg));
    default:
        LOG.warn(this + ": unknown response code " + resp.getStatus()
                + " while attempting to set up short-circuit access. " + resp.getMessage());
        clientContext.getDomainSocketFactory().disableShortCircuitForPath(pathInfo.getPath());
        return null;
    }
}

From source file:BlockReaderFactory.java

License:Apache License

/**
 * Get a RemoteBlockReader that communicates over a UNIX domain socket.
 *
 * @return The new BlockReader, or null if we failed to create the block
 * reader./*from  w  w w .  j  a va  2  s.com*/
 *
 * @throws InvalidToken    If the block token was invalid.
 * Potentially other security-related execptions.
 */
private BlockReader getRemoteBlockReaderFromDomain() throws IOException {
    if (pathInfo == null) {
        pathInfo = clientContext.getDomainSocketFactory().getPathInfo(inetSocketAddress, conf);
    }
    if (!pathInfo.getPathState().getUsableForDataTransfer()) {
        PerformanceAdvisory.LOG.debug(this + ": not trying to create a "
                + "remote block reader because the UNIX domain socket at " + pathInfo + " is not usable.");
        return null;
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace(this + ": trying to create a remote block reader from the " + "UNIX domain socket at "
                + pathInfo.getPath());
    }

    while (true) {
        BlockReaderPeer curPeer = nextDomainPeer();
        if (curPeer == null)
            break;
        if (curPeer.fromCache)
            remainingCacheTries--;
        DomainPeer peer = (DomainPeer) curPeer.peer;
        BlockReader blockReader = null;
        try {
            blockReader = getRemoteBlockReader(peer);
            return blockReader;
        } catch (IOException ioe) {
            IOUtils.cleanup(LOG, peer);
            if (isSecurityException(ioe)) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace(this + ": got security exception while constructing "
                            + "a remote block reader from the unix domain socket at " + pathInfo.getPath(),
                            ioe);
                }
                throw ioe;
            }
            if (curPeer.fromCache) {
                // Handle an I/O error we got when using a cached peer.  These are
                // considered less serious, because the underlying socket may be stale.
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Closed potentially stale domain peer " + peer, ioe);
                }
            } else {
                // Handle an I/O error we got when using a newly created domain peer.
                // We temporarily disable the domain socket path for a few minutes in
                // this case, to prevent wasting more time on it.
                LOG.warn("I/O error constructing remote block reader.  Disabling " + "domain socket "
                        + peer.getDomainSocket(), ioe);
                clientContext.getDomainSocketFactory().disableDomainSocketPath(pathInfo.getPath());
                return null;
            }
        } finally {
            if (blockReader == null) {
                IOUtils.cleanup(LOG, peer);
            }
        }
    }
    return null;
}