Example usage for org.apache.hadoop.hdfs.protocol.datatransfer Op WRITE_BLOCK

List of usage examples for org.apache.hadoop.hdfs.protocol.datatransfer Op WRITE_BLOCK

Introduction

In this page you can find the example usage for org.apache.hadoop.hdfs.protocol.datatransfer Op WRITE_BLOCK.

Prototype

Op WRITE_BLOCK

To view the source code for org.apache.hadoop.hdfs.protocol.datatransfer Op WRITE_BLOCK.

Click Source Link

Usage

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

License:Apache License

private void processOPRHeaderRequest(Msg msg) throws IOException, NoSuchFieldException, NoSuchMethodException,
        IllegalArgumentException, IllegalAccessException {
    if (serverSession.getIsClosing()) {
        LOG.warn("Process OPRHeaderRequest for a closed session, discarding...");
        return;//from ww  w .  j a va2  s.c o  m
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("Processing block header request. uri=" + DataXceiverBase.this.uri);
    }

    msg.getIn().position(0);
    DataInputStream in = new DataInputStream(new ByteBufferInputStream(msg.getIn()));
    final short version = in.readShort();
    if (version != DataTransferProtocol.DATA_TRANSFER_VERSION) {
        in.close();
        throw new IOException("Version Mismatch (Expected: " + DataTransferProtocol.DATA_TRANSFER_VERSION
                + ", Received: " + version + " )");
    }
    Op op = Op.read(in);
    if (op != Op.WRITE_BLOCK) {
        throw new IOException("Unknown op " + op + " in data stream");
    }

    parseOpWriteBlock(in);

    // check single target for transfer-RBW/Finalized
    if (oprHeader.isTransfer() && oprHeader.getTargets().length > 0) {
        throw new IOException(oprHeader.getStage() + " does not support multiple targets "
                + Arrays.asList(oprHeader.getTargets()));
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("uri= " + DataXceiverBase.this.uri + "\nopWriteBlock: stage=" + oprHeader.getStage()
                + ", clientname=" + oprHeader.getClientName() + "\n  block  =" + oprHeader.getBlock()
                + ", newGs=" + oprHeader.getLatestGenerationStamp() + ", bytesRcvd=["
                + oprHeader.getMinBytesRcvd() + ", " + oprHeader.getMaxBytesRcvd() + "]" + "\n  targets="
                + Arrays.asList(oprHeader.getTargets()) + "; pipelineSize=" + oprHeader.getPipelineSize()
                + ", srcDataNode=" + oprHeader.getSrcDataNode() + ", isDatanode=" + oprHeader.isDatanode()
                + ", isClient=" + oprHeader.isClient() + ", isTransfer=" + oprHeader.isTransfer()
                + ", writeBlock receive buf size " + msg.getIn().limit());
    }

    // We later mutate block's generation stamp and length, but we need to
    // forward the original version of the block to downstream mirrors, so
    // make a copy here.
    final ExtendedBlock originalBlock = new ExtendedBlock(oprHeader.getBlock());
    oprHeader.getBlock().setNumBytes(dnBridge.getEstimateBlockSize());
    LOG.info("Receiving " + oprHeader.getBlock() + " src: " + uri);

    boolean isTokenAccessOk = checkAccess(oprHeader.isClient(), oprHeader.getBlock(), oprHeader.getBlockToken(),
            Op.WRITE_BLOCK, BlockTokenSecretManager.AccessMode.WRITE, msg);

    if (isTokenAccessOk) {

        if (oprHeader.isDatanode() || oprHeader.getStage() != BlockConstructionStage.PIPELINE_CLOSE_RECOVERY) {
            // open a block receiver
            createBlockReciver(in, NUM_OF_BLOCK_RECEIVER_CREATION_ATTEMPTS);

            if (LOG.isTraceEnabled()) {
                LOG.trace("After BlockReceiver creation: " + blockReceiver);
            }

        } else {
            dnBridge.recoverClose(oprHeader.getBlock(), oprHeader.getLatestGenerationStamp(),
                    oprHeader.getMinBytesRcvd());
        }

        //
        // Connect to downstream machine, if appropriate
        //
        if (hasPipeline()) {
            try {
                blockReceiver.setMirrorOut(new DummyDataOutputStream()); // we send to pipeline with RDMA and then keep using vanila's
                                                                         // original
                                                                         // receivePacket function by modifying mirror stream with dummy
                                                                         // stream to
                                                                         // avoid sending to pipeline from vanila's flow
                                                                         // openPipelineConnection();
                                                                         // sendOprHeaderToPipeline(msg, originalBlock);
                ClientSession.Callbacks csCBs = DataXceiverBase.this.new CSCallbacks();
                String clientURI = DataXceiverBase.this.uri.toString();
                spw.queueAsyncPipelineConnection(csCBs, clientURI, oprHeader, DataXceiverBase.this);
                /* queue request to send OPR Header to pipeline */
                Msg mirror = msg.getMirror(false);
                mirror.getOut().clear();
                DataOutputStream mirrorOut = new DataOutputStream(new ByteBufferOutputStream(mirror.getOut()));
                senderWriteBlock(mirrorOut, originalBlock);
                mirrorOut.flush();
                spw.queueAsyncRequest(mirror, this);
            } catch (Exception e) {
                if (oprHeader.isClient()) {
                    replyHeaderAck(msg, ERROR, oprHeader.getTargetByIndex(0).getXferAddr());
                    // NB: Unconditionally using the xfer addr w/o hostname
                    LOG.error(dnBridge.getDN() + ":Exception transfering block " + oprHeader.getBlock()
                            + " to mirror " + oprHeader.getTargetByIndex(0).getInfoAddr() + ": "
                            + StringUtils.stringifyException(e));
                } else {
                    LOG.info(dnBridge.getDN() + ":Exception transfering " + oprHeader.getBlock() + " to mirror "
                            + oprHeader.getTargetByIndex(0).getInfoAddr() + "- continuing without the mirror",
                            e);
                }
            }
        } else if (oprHeader.isClient() && !oprHeader.isTransfer()) {
            replyHeaderAck(msg); // async
        }
    }
}