Example usage for org.apache.hadoop.hdfs.protocol Block Block

List of usage examples for org.apache.hadoop.hdfs.protocol Block Block

Introduction

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

Prototype

public Block(File f, long len, long genstamp) 

Source Link

Document

Find the blockid from the given filename

Usage

From source file:common.DataNode.java

License:Apache License

/** Block synchronization */
void syncBlock(RecoveringBlock rBlock, List<BlockRecord> syncList) throws IOException {
    Block block = rBlock.getBlock();//from w  w  w  . j  ava 2  s. c o  m
    long recoveryId = rBlock.getNewGenerationStamp();
    if (LOG.isDebugEnabled()) {
        LOG.debug("block=" + block + ", (length=" + block.getNumBytes() + "), syncList=" + syncList);
    }

    // syncList.isEmpty() means that all data-nodes do not have the block
    // or their replicas have 0 length.
    // The block can be deleted.
    if (syncList.isEmpty()) {
        namenode.commitBlockSynchronization(block, recoveryId, 0, true, true, DatanodeID.EMPTY_ARRAY);
        return;
    }

    // Calculate the best available replica state.
    ReplicaState bestState = ReplicaState.RWR;
    long finalizedLength = -1;
    for (BlockRecord r : syncList) {
        assert r.rInfo.getNumBytes() > 0 : "zero length replica";
        ReplicaState rState = r.rInfo.getOriginalReplicaState();
        if (rState.getValue() < bestState.getValue())
            bestState = rState;
        if (rState == ReplicaState.FINALIZED) {
            if (finalizedLength > 0 && finalizedLength != r.rInfo.getNumBytes())
                throw new IOException("Inconsistent size of finalized replicas. " + "Replica " + r.rInfo
                        + " expected size: " + finalizedLength);
            finalizedLength = r.rInfo.getNumBytes();
        }
    }

    // Calculate list of nodes that will participate in the recovery
    // and the new block size
    List<BlockRecord> participatingList = new ArrayList<BlockRecord>();
    Block newBlock = new Block(block.getBlockId(), -1, recoveryId);
    switch (bestState) {
    case FINALIZED:
        assert finalizedLength > 0 : "finalizedLength is not positive";
        for (BlockRecord r : syncList) {
            ReplicaState rState = r.rInfo.getOriginalReplicaState();
            if (rState == ReplicaState.FINALIZED
                    || rState == ReplicaState.RBW && r.rInfo.getNumBytes() == finalizedLength)
                participatingList.add(r);
        }
        newBlock.setNumBytes(finalizedLength);
        break;
    case RBW:
    case RWR:
        long minLength = Long.MAX_VALUE;
        for (BlockRecord r : syncList) {
            ReplicaState rState = r.rInfo.getOriginalReplicaState();
            if (rState == bestState) {
                minLength = Math.min(minLength, r.rInfo.getNumBytes());
                participatingList.add(r);
            }
        }
        newBlock.setNumBytes(minLength);
        break;
    case RUR:
    case TEMPORARY:
        assert false : "bad replica state: " + bestState;
    }

    List<DatanodeID> failedList = new ArrayList<DatanodeID>();
    List<DatanodeID> successList = new ArrayList<DatanodeID>();
    for (BlockRecord r : participatingList) {
        try {
            Block reply = r.datanode.updateReplicaUnderRecovery(r.rInfo, recoveryId, newBlock.getNumBytes());
            assert reply.equals(newBlock) && reply.getNumBytes() == newBlock
                    .getNumBytes() : "Updated replica must be the same as the new block.";
            successList.add(r.id);
        } catch (IOException e) {
            InterDatanodeProtocol.LOG
                    .warn("Failed to updateBlock (newblock=" + newBlock + ", datanode=" + r.id + ")", e);
            failedList.add(r.id);
        }
    }

    // If any of the data-nodes failed, the recovery fails, because
    // we never know the actual state of the replica on failed data-nodes.
    // The recovery should be started over.
    if (!failedList.isEmpty()) {
        StringBuilder b = new StringBuilder();
        for (DatanodeID id : failedList) {
            b.append("\n  " + id);
        }
        throw new IOException("Cannot recover " + block + ", the following " + failedList.size()
                + " data-nodes failed {" + b + "\n}");
    }

    // Notify the name-node about successfully recovered replicas.
    DatanodeID[] nlist = successList.toArray(new DatanodeID[successList.size()]);
    namenode.commitBlockSynchronization(block, newBlock.getGenerationStamp(), newBlock.getNumBytes(), true,
            false, nlist);
}

From source file:io.hops.experiments.benchmarks.blockreporting.TinyDatanode.java

License:Apache License

void formBlockReport(boolean isDataNodePopulated) throws Exception {
    // fill remaining slots with blocks that do not exist
    for (int idx = blocks.size() - 1; idx >= nrBlocks; idx--) {
        blocks.set(idx, new Block(Long.MAX_VALUE - (blocks.size() - idx), 0, 0));
    }/* w w w  .  j av a2  s  .co  m*/
    blockReportList = new BlockListAsLongs(blocks, null).getBlockListAsLongs();

    //first block report
    if (isDataNodePopulated) {
        firstBlockReport(blockReportList);
    }

    Logger.printMsg("Datanode # " + this.dnIdx + " has generated a block report of size " + blocks.size());
}

From source file:io.hops.experiments.benchmarks.blockreporting.TinyDatanodesHelper.java

License:Apache License

public void readDataNodesStateFromDisk(TinyDatanode[] datanodes) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(DATANODES_STATE));
    String line = null;//from   w ww .j ava2s . c  o m
    while ((line = reader.readLine()) != null) {
        String[] rs = line.split(",");
        datanodes[Integer.valueOf(rs[0])]
                .addBlock(new Block(Long.valueOf(rs[1]), Long.valueOf(rs[2]), Long.valueOf(rs[3])));
    }
    reader.close();
}

From source file:io.hops.metadata.adaptor.BlockInfoDALAdaptor.java

License:Apache License

@Override
public org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo convertDALtoHDFS(BlockInfo dalClass)
        throws StorageException {
    if (dalClass != null) {
        Block b = new Block(dalClass.getBlockId(), dalClass.getNumBytes(), dalClass.getGenerationStamp());
        org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo blockInfo = null;

        if (dalClass.getBlockUCState() > 0) { //UNDER_CONSTRUCTION, UNDER_RECOVERY, COMMITED
            blockInfo = new BlockInfoUnderConstruction(b, dalClass.getInodeId());
            ((BlockInfoUnderConstruction) blockInfo).setBlockUCStateNoPersistance(
                    HdfsServerConstants.BlockUCState.values()[dalClass.getBlockUCState()]);
            ((BlockInfoUnderConstruction) blockInfo)
                    .setPrimaryNodeIndexNoPersistance(dalClass.getPrimaryNodeIndex());
            ((BlockInfoUnderConstruction) blockInfo)
                    .setBlockRecoveryIdNoPersistance(dalClass.getBlockRecoveryId());
        } else if (dalClass.getBlockUCState() == HdfsServerConstants.BlockUCState.COMPLETE.ordinal()) {
            blockInfo = new org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo(b, dalClass.getInodeId());
        }/* w  w  w. j  a va 2  s .co  m*/

        blockInfo.setINodeIdNoPersistance(dalClass.getInodeId());
        blockInfo.setTimestampNoPersistance(dalClass.getTimeStamp());
        blockInfo.setBlockIndexNoPersistance(dalClass.getBlockIndex());

        return blockInfo;
    } else {
        return null;
    }
}