Example usage for java.io DataOutputStream writeBoolean

List of usage examples for java.io DataOutputStream writeBoolean

Introduction

In this page you can find the example usage for java.io DataOutputStream writeBoolean.

Prototype

public final void writeBoolean(boolean v) throws IOException 

Source Link

Document

Writes a boolean to the underlying output stream as a 1-byte value.

Usage

From source file:org.hyperic.hq.agent.db.DiskList.java

/**
 * Add the string to the list of data being stored in the DiskList.
 *
 * @param data Data to add to the end of the list
 *///from w  w  w  .  j av a  2 s .  c  o  m
public void addToList(String data) throws IOException {
    if (this.closed) {
        throw new IOException("Datafile already closed");
    }
    ByteArrayOutputStream bOs = new ByteArrayOutputStream(this.recordSize);
    DataOutputStream dOs = new DataOutputStream(bOs);
    dOs.writeUTF(data);
    if (bOs.size() > this.recordSize) {
        throw new IOException(
                "Data length(" + bOs.size() + ") exceeds " + "maximum record length(" + this.recordSize + ")");
    }
    final long start = now();
    bOs.write(this.padBytes, 0, this.recordSize - bOs.size());
    byte[] bytes = bOs.toByteArray();

    synchronized (this.dataFile) {
        Long firstFreeL;
        long firstFree;

        this.modNum = this.rand.nextInt();

        try {
            firstFreeL = (Long) this.freeList.first();
            firstFree = firstFreeL.longValue();
            this.freeList.remove(firstFreeL);
        } catch (NoSuchElementException exc) {
            // Else we're adding to the end
            firstFree = this.indexFile.length() / IDX_REC_LEN;
        }

        // Write the record to the data file
        this.dataFile.seek(firstFree * this.recordSize);
        this.dataFile.write(bytes);

        bOs.reset();
        dOs.writeBoolean(true); // Is Used
        dOs.writeLong(this.lastRec); // Previous record idx
        dOs.writeLong(-1); // Next record idx

        // Write the index for the record we just made
        this.indexFile.seek(firstFree * IDX_REC_LEN);
        bytes = bOs.toByteArray();
        this.indexFile.write(bytes, 0, bytes.length);

        // Update the previous 'last' record to point to us
        if (this.lastRec != -1) {
            this.indexFile.seek((this.lastRec * IDX_REC_LEN) + 1 + 8);
            this.indexFile.writeLong(firstFree);
        }

        this.lastRec = firstFree;
        if (this.firstRec == -1) {
            this.firstRec = firstFree;
        }
    }

    if (this.dataFile.length() > this.maxLength) {
        this.log.error("Maximum file size for data file: " + this.fileName + " reached (" + this.maxLength
                + " bytes), truncating.");
        deleteAllRecords();
    }
    long duration = now() - start;
    statsCollector.addStat(duration, DISK_LIST_ADD_TO_LIST_TIME);
}

From source file:org.apache.hadoop.hdfs.tools.offlineImageViewer.OfflineImageDecompressor.java

/**
 * Process image file.//from  ww w  .j  a  v  a2 s  .c o m
 */
private void go() throws IOException {
    long start = System.currentTimeMillis();
    System.out.println("Decompressing image file: " + inputFile + " to " + outputFile);
    DataInputStream in = null;
    DataOutputStream out = null;

    try {
        // setup in
        PositionTrackingInputStream ptis = new PositionTrackingInputStream(
                new FileInputStream(new File(inputFile)));
        in = new DataInputStream(ptis);

        // read header information
        int imgVersion = in.readInt();
        if (!LayoutVersion.supports(Feature.FSIMAGE_COMPRESSION, imgVersion)) {
            System.out.println("Image is not compressed. No output will be produced.");
            return;
        }
        int namespaceId = in.readInt();
        long numFiles = in.readLong();
        long genstamp = in.readLong();

        long imgTxId = -1;
        if (LayoutVersion.supports(Feature.STORED_TXIDS, imgVersion)) {
            imgTxId = in.readLong();
        }
        FSImageCompression compression = FSImageCompression.readCompressionHeader(new Configuration(), in);
        if (compression.isNoOpCompression()) {
            System.out.println("Image is not compressed. No output will be produced.");
            return;
        }
        in = BufferedByteInputStream.wrapInputStream(compression.unwrapInputStream(in),
                FSImage.LOAD_SAVE_BUFFER_SIZE, FSImage.LOAD_SAVE_CHUNK_SIZE);
        System.out.println("Starting decompression.");

        // setup output
        out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));

        // write back the uncompressed information
        out.writeInt(imgVersion);
        out.writeInt(namespaceId);
        out.writeLong(numFiles);
        out.writeLong(genstamp);
        if (LayoutVersion.supports(Feature.STORED_TXIDS, imgVersion)) {
            out.writeLong(imgTxId);
        }
        // no compression
        out.writeBoolean(false);

        // copy the data
        long size = new File(inputFile).length();
        // read in 1MB chunks
        byte[] block = new byte[1024 * 1024];
        while (true) {
            int bytesRead = in.read(block);
            if (bytesRead <= 0)
                break;
            out.write(block, 0, bytesRead);
            printProgress(ptis.getPos(), size);
        }

        out.close();

        long stop = System.currentTimeMillis();
        System.out.println("Input file : " + inputFile + " size: " + size);
        System.out.println("Output file: " + outputFile + " size: " + new File(outputFile).length());
        System.out.println("Decompression completed in " + (stop - start) + " ms.");
    } finally {
        if (in != null)
            in.close();
        if (out != null)
            out.close();
    }
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Serializes a <code>NodePropBundle</code> to a data output stream
 *
 * @param out the output stream//from   w  w w.j av  a  2 s  .com
 * @param bundle the bundle to serialize
 * @throws IOException if an I/O error occurs.
 */
public void writeBundle(DataOutputStream out, NodePropBundle bundle) throws IOException {
    long size = out.size();

    // primaryType and version
    out.writeInt((VERSION_CURRENT << 24) | nsIndex.stringToIndex(bundle.getNodeTypeName().getNamespaceURI()));
    out.writeInt(nameIndex.stringToIndex(bundle.getNodeTypeName().getLocalName()));

    // parentUUID
    writeID(out, bundle.getParentId());

    // definitionId
    out.writeUTF(bundle.getNodeDefId().toString());

    // mixin types
    for (Name name : bundle.getMixinTypeNames()) {
        writeIndexedQName(out, name);
    }
    writeIndexedQName(out, null);

    // properties
    for (Name pName : bundle.getPropertyNames()) {
        // skip redundant primaryType, mixinTypes and uuid properties
        if (pName.equals(NameConstants.JCR_PRIMARYTYPE) || pName.equals(NameConstants.JCR_MIXINTYPES)
                || pName.equals(NameConstants.JCR_UUID)) {
            continue;
        }
        NodePropBundle.PropertyEntry pState = bundle.getPropertyEntry(pName);
        if (pState == null) {
            log.error("PropertyState missing in bundle: " + pName);
        } else {
            writeIndexedQName(out, pName);
            writeState(out, pState);
        }
    }
    writeIndexedQName(out, null);

    // write uuid flag
    out.writeBoolean(bundle.isReferenceable());

    // child nodes (list of uuid/name pairs)
    for (NodePropBundle.ChildNodeEntry entry : bundle.getChildNodeEntries()) {
        writeID(out, entry.getId()); // uuid
        writeQName(out, entry.getName()); // name
    }
    writeID(out, null);

    // write mod count
    writeModCount(out, bundle.getModCount());

    // write shared set
    for (NodeId nodeId : bundle.getSharedSet()) {
        writeID(out, nodeId);
    }
    writeID(out, null);

    // set size of bundle
    bundle.setSize(out.size() - size);
}

From source file:org.apache.hadoop.hdfs.server.namenode.AvatarNode.java

private void writeFailoverTestData(String fsck) throws IOException {
    if (!enableTestFramework) {
        LOG.info("Failover: Test framework - disabled");
        return;//from  w  w w  .j  a  va 2 s .  com
    }
    File snapshotFile = getSnapshotFile(confg, true);
    if (snapshotFile == null)
        return;

    float samplePercent = confg.getFloat("dfs.avatarnode.failover.sample.percent", 0.05f);
    LOG.info("Failover: Test framework - using " + (100.0 * samplePercent) + " % sample size");
    List<FileStatusExtended> stat = super.getRandomFilesSample(samplePercent);
    AvatarFailoverSnapshot snapshot = new AvatarFailoverSnapshot(super.namesystem.getOpenFiles(), stat);
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(snapshotFile)));
    try {
        snapshot.write(out);
        out.writeBoolean(enableTestFrameworkFsck);
        if (enableTestFrameworkFsck) {
            Text.writeString(out, fsck);
        }
    } finally {
        out.close();
    }
    LOG.info("Failover: Test framework - saved snapshot file : " + snapshotFile);
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Serializes a <code>NodePropBundle</code> to a data output stream
 *
 * @param out the output stream// w  w  w. ja  v  a2 s.  c  o  m
 * @param bundle the bundle to serialize
 * @throws IOException if an I/O error occurs.
 */
public void writeBundle(DataOutputStream out, NodePropBundle bundle) throws IOException {
    long size = out.size();

    // primaryType and version
    out.writeInt((VERSION_CURRENT << 24) | nsIndex.stringToIndex(bundle.getNodeTypeName().getNamespaceURI()));
    out.writeInt(nameIndex.stringToIndex(bundle.getNodeTypeName().getLocalName()));

    // parentUUID
    writeID(out, bundle.getParentId());

    // definitionId
    out.writeUTF("");

    // mixin types
    Iterator iter = bundle.getMixinTypeNames().iterator();
    while (iter.hasNext()) {
        writeIndexedQName(out, (Name) iter.next());
    }
    writeIndexedQName(out, null);

    // properties
    iter = bundle.getPropertyNames().iterator();
    while (iter.hasNext()) {
        Name pName = (Name) iter.next();
        // skip redundant primaryType, mixinTypes and uuid properties
        if (pName.equals(NameConstants.JCR_PRIMARYTYPE) || pName.equals(NameConstants.JCR_MIXINTYPES)
                || pName.equals(NameConstants.JCR_UUID)) {
            continue;
        }
        NodePropBundle.PropertyEntry pState = bundle.getPropertyEntry(pName);
        if (pState == null) {
            log.error("PropertyState missing in bundle: " + pName);
        } else {
            writeIndexedQName(out, pName);
            writeState(out, pState);
        }
    }
    writeIndexedQName(out, null);

    // write uuid flag
    out.writeBoolean(bundle.isReferenceable());

    // child nodes (list of uuid/name pairs)
    iter = bundle.getChildNodeEntries().iterator();
    while (iter.hasNext()) {
        NodePropBundle.ChildNodeEntry entry = (NodePropBundle.ChildNodeEntry) iter.next();
        writeID(out, entry.getId()); // uuid
        writeQName(out, entry.getName()); // name
    }
    writeID(out, null);

    // write mod count
    writeModCount(out, bundle.getModCount());

    // write shared set
    iter = bundle.getSharedSet().iterator();
    while (iter.hasNext()) {
        writeID(out, (NodeId) iter.next());
    }
    writeID(out, null);

    // set size of bundle
    bundle.setSize(out.size() - size);
}

From source file:org.apache.geode.internal.cache.tier.sockets.HandShake.java

public void accept(OutputStream out, InputStream in, byte epType, int qSize,
        CommunicationMode communicationMode, Principal principal) throws IOException {
    DataOutputStream dos = new DataOutputStream(out);
    DataInputStream dis;//  w w  w .j a  v a  2 s .com
    if (clientVersion.compareTo(Version.CURRENT) < 0) {
        dis = new VersionedDataInputStream(in, clientVersion);
        dos = new VersionedDataOutputStream(dos, clientVersion);
    } else {
        dis = new DataInputStream(in);
    }
    // Write ok reply
    if (communicationMode.isWAN() && principal != null) {
        dos.writeByte(REPLY_WAN_CREDENTIALS);
    } else {
        dos.writeByte(REPLY_OK);// byte 59
    }

    // additional byte of wan site needs to send for Gateway BC
    if (communicationMode.isWAN()) {
        Version.writeOrdinal(dos, ServerHandShakeProcessor.currentServerVersion.ordinal(), true);
    }

    dos.writeByte(epType);
    dos.writeInt(qSize);

    // Write the server's member
    DistributedMember member = this.system.getDistributedMember();
    ServerHandShakeProcessor.writeServerMember(member, dos);

    // Write no message
    dos.writeUTF("");

    // Write delta-propagation property value if this is not WAN.
    if (!communicationMode.isWAN() && this.clientVersion.compareTo(Version.GFE_61) >= 0) {
        dos.writeBoolean(((InternalDistributedSystem) this.system).getConfig().getDeltaPropagation());
    }

    // Neeraj: Now if the communication mode is GATEWAY_TO_GATEWAY
    // and principal not equal to null then send the credentials also
    if (communicationMode.isWAN() && principal != null) {
        sendCredentialsForWan(dos, dis);
    }

    // Write the distributed system id if this is a 6.6 or greater client
    // on the remote side of the gateway
    if (communicationMode.isWAN() && this.clientVersion.compareTo(Version.GFE_66) >= 0
            && ServerHandShakeProcessor.currentServerVersion.compareTo(Version.GFE_66) >= 0) {
        dos.writeByte(
                ((InternalDistributedSystem) this.system).getDistributionManager().getDistributedSystemId());
    }

    if ((communicationMode.isWAN()) && this.clientVersion.compareTo(Version.GFE_80) >= 0
            && ServerHandShakeProcessor.currentServerVersion.compareTo(Version.GFE_80) >= 0) {
        int pdxSize = PeerTypeRegistration.getPdxRegistrySize();
        dos.writeInt(pdxSize);
    }

    // Flush
    dos.flush();
}

From source file:org.apache.hadoop.hdfs.server.datanode.DataWriter.java

/**
 * Write a block to disk./*  w  w w.  ja  v a2  s  .co m*/
 * 
 * @param in The stream to read from
 * @throws IOException
 */
private void writeBlock() throws IOException {
    DatanodeInfo srcDataNode = null;
    LOG.debug("writeBlock receive buf size " + s.getReceiveBufferSize() + " tcp no delay " + s.getTcpNoDelay());
    //
    // Read in the header
    //
    long startTime = System.currentTimeMillis();
    int namespaceid = in.readInt();
    Block block = new Block(in.readLong(), dataXceiverServer.estimateBlockSize, in.readLong());
    LOG.info("Receiving block " + block + " src: " + remoteAddress + " dest: " + localAddress);
    int pipelineSize = in.readInt(); // num of datanodes in entire pipeline
    boolean isRecovery = in.readBoolean(); // is this part of recovery?
    String client = Text.readString(in); // working on behalf of this client
    boolean hasSrcDataNode = in.readBoolean(); // is src node info present
    if (hasSrcDataNode) {
        srcDataNode = new DatanodeInfo();
        srcDataNode.readFields(in);
    }
    int numTargets = in.readInt();
    if (numTargets < 0) {
        throw new IOException("Mislabelled incoming datastream.");
    }
    DatanodeInfo targets[] = new DatanodeInfo[numTargets];
    for (int i = 0; i < targets.length; i++) {
        DatanodeInfo tmp = new DatanodeInfo();
        tmp.readFields(in);
        targets[i] = tmp;
    }

    DataOutputStream mirrorOut = null; // stream to next target
    DataInputStream mirrorIn = null; // reply from next target
    DataOutputStream replyOut = null; // stream to prev target
    Socket mirrorSock = null; // socket to next target
    BlockReceiver blockReceiver = null; // responsible for data handling
    String mirrorNode = null; // the name:port of next target
    String firstBadLink = ""; // first datanode that failed in connection setup

    updateCurrentThreadName("receiving block " + block + " client=" + client);
    try {
        // open a block receiver and check if the block does not exist
        blockReceiver = new BlockReceiver(namespaceid, block, in, s.getRemoteSocketAddress().toString(),
                s.getLocalSocketAddress().toString(), isRecovery, client, srcDataNode, datanode);

        // get a connection back to the previous target
        replyOut = new DataOutputStream(new BufferedOutputStream(
                NetUtils.getOutputStream(s, datanode.socketWriteTimeout), SMALL_BUFFER_SIZE));

        //
        // Open network conn to backup machine, if 
        // appropriate
        //
        if (targets.length > 0) {
            InetSocketAddress mirrorTarget = null;
            // Connect to backup machine
            mirrorNode = targets[0].getName();
            mirrorTarget = NetUtils.createSocketAddr(mirrorNode);
            mirrorSock = datanode.newSocket();
            try {
                int timeoutValue = datanode.socketTimeout + (datanode.socketReadExtentionTimeout * numTargets);
                int writeTimeout = datanode.socketWriteTimeout
                        + (datanode.socketWriteExtentionTimeout * numTargets);
                NetUtils.connect(mirrorSock, mirrorTarget, timeoutValue);
                mirrorSock.setSoTimeout(timeoutValue);
                mirrorSock.setSendBufferSize(DEFAULT_DATA_SOCKET_SIZE);
                mirrorOut = new DataOutputStream(new BufferedOutputStream(
                        NetUtils.getOutputStream(mirrorSock, writeTimeout), SMALL_BUFFER_SIZE));
                mirrorIn = new DataInputStream(NetUtils.getInputStream(mirrorSock));

                // Write header: Copied from DFSClient.java!
                mirrorOut.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION);
                mirrorOut.write(DataTransferProtocol.OP_WRITE_BLOCK);
                mirrorOut.writeInt(namespaceid);
                mirrorOut.writeLong(block.getBlockId());
                mirrorOut.writeLong(block.getGenerationStamp());
                mirrorOut.writeInt(pipelineSize);
                mirrorOut.writeBoolean(isRecovery);
                Text.writeString(mirrorOut, client);
                mirrorOut.writeBoolean(hasSrcDataNode);
                if (hasSrcDataNode) { // pass src node information
                    srcDataNode.write(mirrorOut);
                }
                mirrorOut.writeInt(targets.length - 1);
                for (int i = 1; i < targets.length; i++) {
                    targets[i].write(mirrorOut);
                }

                blockReceiver.writeChecksumHeader(mirrorOut);
                mirrorOut.flush();

                // read connect ack (only for clients, not for replication req)
                if (client.length() != 0) {
                    firstBadLink = Text.readString(mirrorIn);
                    if (LOG.isDebugEnabled() || firstBadLink.length() > 0) {
                        LOG.info("Datanode " + targets.length + " got response for connect ack "
                                + " from downstream datanode with firstbadlink as " + firstBadLink);
                    }
                }

            } catch (IOException e) {
                if (client.length() != 0) {
                    Text.writeString(replyOut, mirrorNode);
                    replyOut.flush();
                }
                IOUtils.closeStream(mirrorOut);
                mirrorOut = null;
                IOUtils.closeStream(mirrorIn);
                mirrorIn = null;
                IOUtils.closeSocket(mirrorSock);
                mirrorSock = null;
                if (client.length() > 0) {
                    throw e;
                } else {
                    LOG.info(datanode.getDatanodeInfo() + ":Exception transfering block " + block
                            + " to mirror " + mirrorNode + ". continuing without the mirror.\n"
                            + StringUtils.stringifyException(e));
                }
            }
        }

        // send connect ack back to source (only for clients)
        if (client.length() != 0) {
            if (LOG.isDebugEnabled() || firstBadLink.length() > 0) {
                LOG.info("Datanode " + targets.length + " forwarding connect ack to upstream firstbadlink is "
                        + firstBadLink);
            }
            Text.writeString(replyOut, firstBadLink);
            replyOut.flush();
        }

        // receive the block and mirror to the next target
        String mirrorAddr = (mirrorSock == null) ? null : mirrorNode;
        long totalReceiveSize = blockReceiver.receiveBlock(mirrorOut, mirrorIn, replyOut, mirrorAddr, null,
                targets.length);

        // if this write is for a replication request (and not
        // from a client), then confirm block. For client-writes,
        // the block is finalized in the PacketResponder.
        if (client.length() == 0) {
            datanode.notifyNamenodeReceivedBlock(namespaceid, block, null);
            LOG.info("Received block " + block + " src: " + remoteAddress + " dest: " + localAddress
                    + " of size " + block.getNumBytes());
        } else {
            // Log the fact that the block has been received by this datanode and
            // has been written to the local disk on this datanode.
            LOG.info("Received Block " + block + " src: " + remoteAddress + " dest: " + localAddress
                    + " of size " + block.getNumBytes() + " and written to local disk");
        }

        if (datanode.blockScanner != null) {
            datanode.blockScanner.addBlock(namespaceid, block);
        }

        long writeDuration = System.currentTimeMillis() - startTime;
        datanode.myMetrics.bytesWrittenLatency.inc(writeDuration);
        if (totalReceiveSize > KB_RIGHT_SHIFT_MIN) {
            datanode.myMetrics.bytesWrittenRate.inc((int) (totalReceiveSize >> KB_RIGHT_SHIFT_BITS),
                    writeDuration);
        }

    } catch (IOException ioe) {
        LOG.info("writeBlock " + block + " received exception " + ioe);
        throw ioe;
    } finally {
        // close all opened streams
        IOUtils.closeStream(mirrorOut);
        IOUtils.closeStream(mirrorIn);
        IOUtils.closeStream(replyOut);
        IOUtils.closeSocket(mirrorSock);
        IOUtils.closeStream(blockReceiver);
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Write a block to disk.//www . j ava  2 s .c o m
 * 
 * @param in The stream to read from
 * @throws IOException
 */
private void writeBlock(DataInputStream in) throws IOException {
    DatanodeInfo srcDataNode = null;
    LOG.debug("writeBlock receive buf size " + s.getReceiveBufferSize() + " tcp no delay " + s.getTcpNoDelay());
    //
    // Read in the header
    //
    Block block = new Block(in.readLong(), dataXceiverServer.estimateBlockSize, in.readLong());
    LOG.info("Receiving block " + block + " src: " + remoteAddress + " dest: " + localAddress);
    int pipelineSize = in.readInt(); // num of datanodes in entire pipeline
    boolean isRecovery = in.readBoolean(); // is this part of recovery?
    String client = Text.readString(in); // working on behalf of this client
    boolean hasSrcDataNode = in.readBoolean(); // is src node info present
    if (hasSrcDataNode) {
        srcDataNode = new DatanodeInfo();
        srcDataNode.readFields(in);
    }
    int numTargets = in.readInt();
    if (numTargets < 0) {
        throw new IOException("Mislabelled incoming datastream.");
    }
    DatanodeInfo targets[] = new DatanodeInfo[numTargets];
    for (int i = 0; i < targets.length; i++) {
        DatanodeInfo tmp = new DatanodeInfo();
        tmp.readFields(in);
        targets[i] = tmp;
    }
    Token<BlockTokenIdentifier> accessToken = new Token<BlockTokenIdentifier>();
    accessToken.readFields(in);
    DataOutputStream replyOut = null; // stream to prev target
    replyOut = new DataOutputStream(NetUtils.getOutputStream(s, datanode.socketWriteTimeout));
    if (datanode.isBlockTokenEnabled) {
        try {
            datanode.blockTokenSecretManager.checkAccess(accessToken, null, block,
                    BlockTokenSecretManager.AccessMode.WRITE);
        } catch (InvalidToken e) {
            try {
                if (client.length() != 0) {
                    replyOut.writeShort((short) DataTransferProtocol.OP_STATUS_ERROR_ACCESS_TOKEN);
                    Text.writeString(replyOut, datanode.dnRegistration.getName());
                    replyOut.flush();
                }
                throw new IOException("Access token verification failed, for client " + remoteAddress
                        + " for OP_WRITE_BLOCK for block " + block);
            } finally {
                IOUtils.closeStream(replyOut);
            }
        }
    }

    DataOutputStream mirrorOut = null; // stream to next target
    DataInputStream mirrorIn = null; // reply from next target
    Socket mirrorSock = null; // socket to next target
    BlockReceiver blockReceiver = null; // responsible for data handling
    String mirrorNode = null; // the name:port of next target
    String firstBadLink = ""; // first datanode that failed in connection setup
    short mirrorInStatus = (short) DataTransferProtocol.OP_STATUS_SUCCESS;
    try {
        // open a block receiver and check if the block does not exist
        blockReceiver = new BlockReceiver(block, in, s.getRemoteSocketAddress().toString(),
                s.getLocalSocketAddress().toString(), isRecovery, client, srcDataNode, datanode);

        //
        // Open network conn to backup machine, if 
        // appropriate
        //
        if (targets.length > 0) {
            InetSocketAddress mirrorTarget = null;
            // Connect to backup machine
            mirrorNode = targets[0].getName();
            mirrorTarget = NetUtils.createSocketAddr(mirrorNode);
            mirrorSock = datanode.newSocket();
            try {
                int timeoutValue = datanode.socketTimeout + (HdfsConstants.READ_TIMEOUT_EXTENSION * numTargets);
                int writeTimeout = datanode.socketWriteTimeout
                        + (HdfsConstants.WRITE_TIMEOUT_EXTENSION * numTargets);
                NetUtils.connect(mirrorSock, mirrorTarget, timeoutValue);
                mirrorSock.setSoTimeout(timeoutValue);
                mirrorSock.setSendBufferSize(DEFAULT_DATA_SOCKET_SIZE);
                mirrorOut = new DataOutputStream(new BufferedOutputStream(
                        NetUtils.getOutputStream(mirrorSock, writeTimeout), SMALL_BUFFER_SIZE));
                mirrorIn = new DataInputStream(NetUtils.getInputStream(mirrorSock));

                // Write header: Copied from DFSClient.java!
                mirrorOut.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION);
                mirrorOut.write(DataTransferProtocol.OP_WRITE_BLOCK);
                mirrorOut.writeLong(block.getBlockId());
                mirrorOut.writeLong(block.getGenerationStamp());
                mirrorOut.writeInt(pipelineSize);
                mirrorOut.writeBoolean(isRecovery);
                Text.writeString(mirrorOut, client);
                mirrorOut.writeBoolean(hasSrcDataNode);
                if (hasSrcDataNode) { // pass src node information
                    srcDataNode.write(mirrorOut);
                }
                mirrorOut.writeInt(targets.length - 1);
                for (int i = 1; i < targets.length; i++) {
                    targets[i].write(mirrorOut);
                }
                accessToken.write(mirrorOut);

                blockReceiver.writeChecksumHeader(mirrorOut);
                mirrorOut.flush();

                // read connect ack (only for clients, not for replication req)
                if (client.length() != 0) {
                    mirrorInStatus = mirrorIn.readShort();
                    firstBadLink = Text.readString(mirrorIn);
                    if (LOG.isDebugEnabled() || mirrorInStatus != DataTransferProtocol.OP_STATUS_SUCCESS) {
                        LOG.info("Datanode " + targets.length + " got response for connect ack "
                                + " from downstream datanode with firstbadlink as " + firstBadLink);
                    }
                }

            } catch (IOException e) {
                if (client.length() != 0) {
                    replyOut.writeShort((short) DataTransferProtocol.OP_STATUS_ERROR);
                    Text.writeString(replyOut, mirrorNode);
                    replyOut.flush();
                }
                IOUtils.closeStream(mirrorOut);
                mirrorOut = null;
                IOUtils.closeStream(mirrorIn);
                mirrorIn = null;
                IOUtils.closeSocket(mirrorSock);
                mirrorSock = null;
                if (client.length() > 0) {
                    throw e;
                } else {
                    LOG.info(datanode.dnRegistration + ":Exception transfering block " + block + " to mirror "
                            + mirrorNode + ". continuing without the mirror.\n"
                            + StringUtils.stringifyException(e));
                }
            }
        }

        // send connect ack back to source (only for clients)
        if (client.length() != 0) {
            if (LOG.isDebugEnabled() || mirrorInStatus != DataTransferProtocol.OP_STATUS_SUCCESS) {
                LOG.info("Datanode " + targets.length + " forwarding connect ack to upstream firstbadlink is "
                        + firstBadLink);
            }
            replyOut.writeShort(mirrorInStatus);
            Text.writeString(replyOut, firstBadLink);
            replyOut.flush();
        }

        // receive the block and mirror to the next target
        String mirrorAddr = (mirrorSock == null) ? null : mirrorNode;
        blockReceiver.receiveBlock(mirrorOut, mirrorIn, replyOut, mirrorAddr, null, targets.length);

        // if this write is for a replication request (and not
        // from a client), then confirm block. For client-writes,
        // the block is finalized in the PacketResponder.
        if (client.length() == 0) {
            datanode.notifyNamenodeReceivedBlock(block, DataNode.EMPTY_DEL_HINT);
            LOG.info("Received block " + block + " src: " + remoteAddress + " dest: " + localAddress
                    + " of size " + block.getNumBytes());
        }

        if (datanode.blockScanner != null) {
            datanode.blockScanner.addBlock(block);
        }

    } catch (IOException ioe) {
        LOG.info("writeBlock " + block + " received exception " + ioe);
        throw ioe;
    } finally {
        // close all opened streams
        IOUtils.closeStream(mirrorOut);
        IOUtils.closeStream(mirrorIn);
        IOUtils.closeStream(replyOut);
        IOUtils.closeSocket(mirrorSock);
        IOUtils.closeStream(blockReceiver);
    }
}

From source file:org.apache.pulsar.testclient.LoadSimulationClient.java

private void handle(final byte command, final DataInputStream inputStream, final DataOutputStream outputStream)
        throws Exception {
    final TradeConfiguration tradeConf = new TradeConfiguration();
    tradeConf.command = command;/*from   w ww.  j  av a  2s  . co  m*/
    switch (command) {
    case CHANGE_COMMAND:
        // Change the topic's settings if it exists.
        decodeProducerOptions(tradeConf, inputStream);
        if (topicsToTradeUnits.containsKey(tradeConf.topic)) {
            topicsToTradeUnits.get(tradeConf.topic).change(tradeConf);
        }
        break;
    case STOP_COMMAND:
        // Stop the topic if it exists.
        tradeConf.topic = inputStream.readUTF();
        if (topicsToTradeUnits.containsKey(tradeConf.topic)) {
            topicsToTradeUnits.get(tradeConf.topic).stop.set(true);
        }
        break;
    case TRADE_COMMAND:
        // Create the topic. It is assumed that the topic does not already exist.
        decodeProducerOptions(tradeConf, inputStream);
        final TradeUnit tradeUnit = new TradeUnit(tradeConf, client, producerConf, consumerConf, payloadCache);
        topicsToTradeUnits.put(tradeConf.topic, tradeUnit);
        executor.submit(() -> {
            try {
                final String topic = tradeConf.topic;
                final String namespace = topic.substring("persistent://".length(), topic.lastIndexOf('/'));
                try {
                    admin.namespaces().createNamespace(namespace);
                } catch (PulsarAdminException.ConflictException e) {
                    // Ignore, already created namespace.
                }
                tradeUnit.start();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });
        break;
    case CHANGE_GROUP_COMMAND:
        // Change the settings of all topics belonging to a group.
        decodeGroupOptions(tradeConf, inputStream);
        tradeConf.size = inputStream.readInt();
        tradeConf.rate = inputStream.readDouble();
        // See if a topic belongs to this tenant and group using this regex.
        final String groupRegex = ".*://" + tradeConf.tenant + "/.*/" + tradeConf.group + "-.*/.*";
        for (Map.Entry<String, TradeUnit> entry : topicsToTradeUnits.entrySet()) {
            final String destination = entry.getKey();
            final TradeUnit unit = entry.getValue();
            if (destination.matches(groupRegex)) {
                unit.change(tradeConf);
            }
        }
        break;
    case STOP_GROUP_COMMAND:
        // Stop all topics belonging to a group.
        decodeGroupOptions(tradeConf, inputStream);
        // See if a topic belongs to this tenant and group using this regex.
        final String regex = ".*://" + tradeConf.tenant + "/.*/" + tradeConf.group + "-.*/.*";
        for (Map.Entry<String, TradeUnit> entry : topicsToTradeUnits.entrySet()) {
            final String destination = entry.getKey();
            final TradeUnit unit = entry.getValue();
            if (destination.matches(regex)) {
                unit.stop.set(true);
            }
        }
        break;
    case FIND_COMMAND:
        // Write a single boolean indicating if the topic was found.
        outputStream.writeBoolean(topicsToTradeUnits.containsKey(inputStream.readUTF()));
        outputStream.flush();
        break;
    default:
        throw new IllegalArgumentException("Unrecognized command code received: " + command);
    }
}

From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Write a block to disk.//from  w w  w.  j a v  a  2 s .  com
 * 
 * @param in The stream to read from
 * @throws IOException
 */
private void writeBlock(DataInputStream in) throws IOException {
    LOG.debug("Mathod called : writeBlock()");
    DatanodeInfo srcDataNode = null;
    LOG.debug("writeBlock receive buf size " + s.getReceiveBufferSize() + " tcp no delay " + s.getTcpNoDelay());
    //
    // Read in the header
    //
    Block block = new Block(in.readLong(), dataXceiverServer.estimateBlockSize, in.readLong());
    LOG.info("Receiving block " + block + " src: " + remoteAddress + " dest: " + localAddress);

    int pipelineSize = in.readInt(); // num of datanodes in entire pipeline
    boolean isRecovery = in.readBoolean(); // is this part of recovery?
    String client = Text.readString(in); // working on behalf of this client

    boolean hasSrcDataNode = in.readBoolean(); // is src node info present
    if (hasSrcDataNode) {
        srcDataNode = new DatanodeInfo();
        srcDataNode.readFields(in);
    }

    int numTargets = in.readInt();
    if (numTargets < 0) {
        throw new IOException("Mislabelled incoming datastream.");
    }

    DatanodeInfo targets[] = new DatanodeInfo[numTargets];
    for (int i = 0; i < targets.length; i++) {
        DatanodeInfo tmp = new DatanodeInfo();
        tmp.readFields(in);
        targets[i] = tmp;
    }

    DataOutputStream mirrorOut = null; // stream to next target
    DataInputStream mirrorIn = null; // reply from next target
    DataOutputStream replyOut = null; // stream to prev target
    JxtaSocket mirrorSock = null; // socket to next target
    BlockReceiver blockReceiver = null; // responsible for data handling
    String mirrorNode = null; // the name:port of next target
    String firstBadLink = ""; // first datanode that failed in connection setup

    try {
        // open a block receiver and check if the block does not exist
        /*blockReceiver = new BlockReceiver(block, in, 
            s.getRemoteSocketAddress().toString(),
            s.getLocalSocketAddress().toString(),
            isRecovery, client, srcDataNode, datanode);*/
        blockReceiver = new BlockReceiver(block, in,
                ((JxtaSocketAddress) s.getRemoteSocketAddress()).getPeerId().toString(),
                ((JxtaSocketAddress) s.getLocalSocketAddress()).getPeerId().toString(), isRecovery, client,
                srcDataNode, datanode);

        // get a connection back to the previous target
        //replyOut = new DataOutputStream(
        //     NetUtils.getOutputStream(s, datanode.socketWriteTimeout));
        ReliableOutputStream replyOutRos = (ReliableOutputStream) s.getOutputStream();
        replyOut = new DataOutputStream(replyOutRos);

        //
        // Open network conn to backup machine, if 
        // appropriate
        //
        if (targets.length > 0) {
            // JxtaSocketAddress mirrorTarget = null;
            // Connect to backup machine
            mirrorNode = targets[0].getPeerId();
            // mirrorTarget = NetUtils.createSocketAddr(mirrorNode);
            // mirrorSock = datanode.newSocket();

            try {
                //int timeoutValue = numTargets * datanode.socketTimeout;
                //int writeTimeout = datanode.socketWriteTimeout + 
                //                   (HdfsConstants.WRITE_TIMEOUT_EXTENSION * numTargets);
                // NetUtils.connect(mirrorSock, mirrorTarget, timeoutValue);
                mirrorSock = datanode.getDnPeer().getInfoSocket(mirrorNode.toString());
                if (mirrorSock == null)
                    throw new IOException("Failed to get a mirror socket");
                //mirrorSock.setSoTimeout(timeoutValue);
                //mirrorSock.setTcpNoDelay(true);
                //mirrorSock.setSoTimeout(Integer.parseInt(datanode.getConf().get("hadoop.p2p.info.timeout")));
                //mirrorSock.setSendBufferSize(DEFAULT_DATA_SOCKET_SIZE);
                /*mirrorOut = new DataOutputStream(
                   new BufferedOutputStream(
                         NetUtils.getOutputStream(mirrorSock, writeTimeout),
                 SMALL_BUFFER_SIZE));
                mirrorIn = new DataInputStream(NetUtils.getInputStream(mirrorSock));
                */
                mirrorOut = new DataOutputStream((ReliableOutputStream) mirrorSock.getOutputStream());
                mirrorIn = new DataInputStream((ReliableInputStream) mirrorSock.getInputStream());

                // Write header: Copied from DFSClient.java!
                mirrorOut.writeShort(DataTransferProtocol.DATA_TRANSFER_VERSION);
                mirrorOut.write(DataTransferProtocol.OP_WRITE_BLOCK);
                mirrorOut.writeLong(block.getBlockId());
                mirrorOut.writeLong(block.getGenerationStamp());
                mirrorOut.writeInt(pipelineSize);
                mirrorOut.writeBoolean(isRecovery);
                Text.writeString(mirrorOut, client);
                mirrorOut.writeBoolean(hasSrcDataNode);

                if (hasSrcDataNode) { // pass src node information
                    srcDataNode.write(mirrorOut);
                }

                mirrorOut.writeInt(targets.length - 1);
                for (int i = 1; i < targets.length; i++) {
                    targets[i].write(mirrorOut);
                }

                blockReceiver.writeChecksumHeader(mirrorOut);
                mirrorOut.flush();

                // read connect ack (only for clients, not for replication req)
                if (client.length() != 0) {
                    firstBadLink = Text.readString(mirrorIn);
                    if (LOG.isDebugEnabled() || firstBadLink.length() > 0) {
                        LOG.info("Datanode " + targets.length + " got response for connect ack "
                                + " from downstream datanode with firstbadlink as " + firstBadLink);
                    }
                }

            } catch (SocketTimeoutException ste) {
                LOG.debug("Time out while receiving data on DataXceiver");
                LOG.debug(ste);
                ste.printStackTrace();
            } catch (IOException e) {
                LOG.debug("IOException occurred : " + e.getMessage());
                if (client.length() != 0) {
                    Text.writeString(replyOut, mirrorNode);
                    replyOut.flush();
                }
                IOUtils.closeStream(mirrorOut);
                mirrorOut = null;
                IOUtils.closeStream(mirrorIn);
                mirrorIn = null;
                if (mirrorSock != null) {
                    IOUtils.closeSocket(mirrorSock);
                    mirrorSock = null;
                }
                if (client.length() > 0) {
                    throw e;
                } else {
                    LOG.info(datanode.dnRegistration + ":Exception transfering block " + block + " to mirror "
                            + mirrorNode + ". continuing without the mirror.\n"
                            + StringUtils.stringifyException(e));
                }
            }
        }

        // send connect ack back to source (only for clients)
        if (client.length() != 0) {
            if (LOG.isDebugEnabled() || firstBadLink.length() > 0) {
                LOG.info("Datanode " + targets.length + " forwarding connect ack to upstream firstbadlink is "
                        + firstBadLink);
            }
            Text.writeString(replyOut, firstBadLink);
            replyOut.flush();
        }

        // receive the block and mirror to the next target
        String mirrorAddr = (mirrorSock == null) ? null : mirrorNode;
        blockReceiver.receiveBlock(mirrorOut, mirrorIn, replyOut, mirrorAddr, null, targets.length);

        // if this write is for a replication request (and not
        // from a client), then confirm block. For client-writes,
        // the block is finalized in the PacketResponder.
        if (client.length() == 0) {
            datanode.notifyNamenodeReceivedBlock(block, DataNode.EMPTY_DEL_HINT);
            LOG.info("Received block " + block + " src: " + remoteAddress + " dest: " + localAddress
                    + " of size " + block.getNumBytes());
        }

        if (datanode.blockScanner != null) {
            datanode.blockScanner.addBlock(block);
        }

    } catch (IOException ioe) {
        LOG.info("writeBlock " + block + " received exception " + ioe);
        throw ioe;
    } catch (Exception e) {
        LOG.warn("Exception occurred in writting block : " + e.getMessage());
    } finally {
        // close all opened streams

        LOG.debug("Finalizing : writeBlock()");
        IOUtils.closeStream(mirrorOut);
        IOUtils.closeStream(mirrorIn);
        IOUtils.closeStream(replyOut);
        IOUtils.closeSocket(mirrorSock);
        IOUtils.closeStream(blockReceiver);
    }
}