Search in sources :

Example 1 with FileWithSnapshotFeature

use of org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature in project hadoop by apache.

the class INodeFile method computeQuotaDeltaForTruncate.

/**
   * compute the quota usage change for a truncate op
   * @param newLength the length for truncation
   * TODO: properly handle striped blocks (HDFS-7622)
   **/
void computeQuotaDeltaForTruncate(long newLength, BlockStoragePolicy bsps, QuotaCounts delta) {
    final BlockInfo[] blocks = getBlocks();
    if (blocks.length == 0) {
        return;
    }
    long size = 0;
    for (BlockInfo b : blocks) {
        size += b.getNumBytes();
    }
    BlockInfo[] sblocks = null;
    FileWithSnapshotFeature sf = getFileWithSnapshotFeature();
    if (sf != null) {
        FileDiff diff = sf.getDiffs().getLast();
        sblocks = diff != null ? diff.getBlocks() : null;
    }
    for (int i = blocks.length - 1; i >= 0 && size > newLength; size -= blocks[i].getNumBytes(), --i) {
        BlockInfo bi = blocks[i];
        long truncatedBytes;
        if (size - newLength < bi.getNumBytes()) {
            // Record a full block as the last block will be copied during
            // recovery
            truncatedBytes = bi.getNumBytes() - getPreferredBlockSize();
        } else {
            truncatedBytes = bi.getNumBytes();
        }
        // existing files
        if (sblocks != null && i < sblocks.length && bi.equals(sblocks[i])) {
            truncatedBytes -= bi.getNumBytes();
        }
        delta.addStorageSpace(-truncatedBytes * bi.getReplication());
        if (bsps != null) {
            List<StorageType> types = bsps.chooseStorageTypes(bi.getReplication());
            for (StorageType t : types) {
                if (t.supportTypeQuota()) {
                    delta.addTypeSpace(t, -truncatedBytes);
                }
            }
        }
    }
}
Also used : FileWithSnapshotFeature(org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature) StorageType(org.apache.hadoop.fs.StorageType) BlockInfo(org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo) FileDiff(org.apache.hadoop.hdfs.server.namenode.snapshot.FileDiff)

Example 2 with FileWithSnapshotFeature

use of org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature in project hadoop by apache.

the class INodeFile method addSnapshotFeature.

/* End of Under-Construction Feature */
/* Start of Snapshot Feature */
public FileWithSnapshotFeature addSnapshotFeature(FileDiffList diffs) {
    Preconditions.checkState(!isWithSnapshot(), "File is already with snapshot");
    FileWithSnapshotFeature sf = new FileWithSnapshotFeature(diffs);
    this.addFeature(sf);
    return sf;
}
Also used : FileWithSnapshotFeature(org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature)

Example 3 with FileWithSnapshotFeature

use of org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature in project hadoop by apache.

the class INodeFile method computeQuotaUsage.

// This is the only place that needs to use the BlockStoragePolicySuite to
// derive the intended storage type usage for quota by storage type
@Override
public final QuotaCounts computeQuotaUsage(BlockStoragePolicySuite bsps, byte blockStoragePolicyId, boolean useCache, int lastSnapshotId) {
    final QuotaCounts counts = new QuotaCounts.Builder().nameSpace(1).build();
    final BlockStoragePolicy bsp = (blockStoragePolicyId == BLOCK_STORAGE_POLICY_ID_UNSPECIFIED) ? null : bsps.getPolicy(blockStoragePolicyId);
    FileWithSnapshotFeature sf = getFileWithSnapshotFeature();
    if (sf == null) {
        counts.add(storagespaceConsumed(bsp));
        return counts;
    }
    FileDiffList fileDiffList = sf.getDiffs();
    int last = fileDiffList.getLastSnapshotId();
    if (lastSnapshotId == Snapshot.CURRENT_STATE_ID || last == Snapshot.CURRENT_STATE_ID) {
        counts.add(storagespaceConsumed(bsp));
        return counts;
    }
    final long ssDeltaNoReplication;
    short replication;
    if (isStriped()) {
        return computeQuotaUsageWithStriped(bsp, counts);
    }
    if (last < lastSnapshotId) {
        ssDeltaNoReplication = computeFileSize(true, false);
        replication = getFileReplication();
    } else {
        int sid = fileDiffList.getSnapshotById(lastSnapshotId);
        ssDeltaNoReplication = computeFileSize(sid);
        replication = getFileReplication(sid);
    }
    counts.addStorageSpace(ssDeltaNoReplication * replication);
    if (bsp != null) {
        List<StorageType> storageTypes = bsp.chooseStorageTypes(replication);
        for (StorageType t : storageTypes) {
            if (!t.supportTypeQuota()) {
                continue;
            }
            counts.addTypeSpace(t, ssDeltaNoReplication);
        }
    }
    return counts;
}
Also used : FileWithSnapshotFeature(org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature) StorageType(org.apache.hadoop.fs.StorageType) FileDiffList(org.apache.hadoop.hdfs.server.namenode.snapshot.FileDiffList) BlockStoragePolicy(org.apache.hadoop.hdfs.protocol.BlockStoragePolicy)

Example 4 with FileWithSnapshotFeature

use of org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature in project hadoop by apache.

the class INodeFile method isBlockInLatestSnapshot.

/**
   * @return true if the block is contained in a snapshot or false otherwise.
   */
boolean isBlockInLatestSnapshot(BlockInfo block) {
    FileWithSnapshotFeature sf = this.getFileWithSnapshotFeature();
    if (sf == null || sf.getDiffs() == null) {
        return false;
    }
    BlockInfo[] snapshotBlocks = getDiffs().findEarlierSnapshotBlocks(getDiffs().getLastSnapshotId());
    return snapshotBlocks != null && Arrays.asList(snapshotBlocks).contains(block);
}
Also used : FileWithSnapshotFeature(org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature) BlockInfo(org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo)

Example 5 with FileWithSnapshotFeature

use of org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature in project hadoop by apache.

the class TestTruncateQuotaUpdate method addSnapshotFeature.

private static void addSnapshotFeature(INodeFile file, BlockInfo[] blocks) {
    FileDiff diff = mock(FileDiff.class);
    when(diff.getBlocks()).thenReturn(blocks);
    FileDiffList diffList = new FileDiffList();
    @SuppressWarnings("unchecked") ArrayList<FileDiff> diffs = ((ArrayList<FileDiff>) Whitebox.getInternalState(diffList, "diffs"));
    diffs.add(diff);
    FileWithSnapshotFeature sf = new FileWithSnapshotFeature(diffList);
    file.addFeature(sf);
}
Also used : FileWithSnapshotFeature(org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature) FileDiffList(org.apache.hadoop.hdfs.server.namenode.snapshot.FileDiffList) FileDiff(org.apache.hadoop.hdfs.server.namenode.snapshot.FileDiff)

Aggregations

FileWithSnapshotFeature (org.apache.hadoop.hdfs.server.namenode.snapshot.FileWithSnapshotFeature)10 BlockInfo (org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo)4 StorageType (org.apache.hadoop.fs.StorageType)3 FileDiff (org.apache.hadoop.hdfs.server.namenode.snapshot.FileDiff)3 HashSet (java.util.HashSet)2 FileDiffList (org.apache.hadoop.hdfs.server.namenode.snapshot.FileDiffList)2 BlockStoragePolicy (org.apache.hadoop.hdfs.protocol.BlockStoragePolicy)1 ErasureCodingPolicy (org.apache.hadoop.hdfs.protocol.ErasureCodingPolicy)1