/// <exception cref="System.IO.IOException"/> private void SerializeFileDiffList(INodeFile file, OutputStream @out) { FileWithSnapshotFeature sf = file.GetFileWithSnapshotFeature(); if (sf != null) { IList <FileDiff> diffList = sf.GetDiffs().AsList(); FsImageProto.SnapshotDiffSection.DiffEntry entry = ((FsImageProto.SnapshotDiffSection.DiffEntry )FsImageProto.SnapshotDiffSection.DiffEntry.NewBuilder().SetInodeId(file.GetId() ).SetType(FsImageProto.SnapshotDiffSection.DiffEntry.Type.Filediff).SetNumOfDiff (diffList.Count).Build()); entry.WriteDelimitedTo(@out); for (int i = diffList.Count - 1; i >= 0; i--) { FileDiff diff = diffList[i]; FsImageProto.SnapshotDiffSection.FileDiff.Builder fb = FsImageProto.SnapshotDiffSection.FileDiff .NewBuilder().SetSnapshotId(diff.GetSnapshotId()).SetFileSize(diff.GetFileSize() ); if (diff.GetBlocks() != null) { foreach (Block block in diff.GetBlocks()) { fb.AddBlocks(PBHelper.Convert(block)); } } INodeFileAttributes copy = diff.snapshotINode; if (copy != null) { fb.SetName(ByteString.CopyFrom(copy.GetLocalNameBytes())).SetSnapshotCopy(FSImageFormatPBINode.Saver.BuildINodeFile (copy, parent.GetSaverContext())); } ((FsImageProto.SnapshotDiffSection.FileDiff)fb.Build()).WriteDelimitedTo(@out); } } }
/// <summary>Load FileDiff list for a file with snapshot feature</summary> /// <exception cref="System.IO.IOException"/> private void LoadFileDiffList(InputStream @in, INodeFile file, int size) { FileDiffList diffs = new FileDiffList(); FSImageFormatProtobuf.LoaderContext state = parent.GetLoaderContext(); for (int i = 0; i < size; i++) { FsImageProto.SnapshotDiffSection.FileDiff pbf = FsImageProto.SnapshotDiffSection.FileDiff .ParseDelimitedFrom(@in); INodeFileAttributes copy = null; if (pbf.HasSnapshotCopy()) { FsImageProto.INodeSection.INodeFile fileInPb = pbf.GetSnapshotCopy(); PermissionStatus permission = FSImageFormatPBINode.Loader.LoadPermission(fileInPb .GetPermission(), state.GetStringTable()); AclFeature acl = null; if (fileInPb.HasAcl()) { int[] entries = AclEntryStatusFormat.ToInt(FSImageFormatPBINode.Loader.LoadAclEntries (fileInPb.GetAcl(), state.GetStringTable())); acl = new AclFeature(entries); } XAttrFeature xAttrs = null; if (fileInPb.HasXAttrs()) { xAttrs = new XAttrFeature(FSImageFormatPBINode.Loader.LoadXAttrs(fileInPb.GetXAttrs (), state.GetStringTable())); } copy = new INodeFileAttributes.SnapshotCopy(pbf.GetName().ToByteArray(), permission , acl, fileInPb.GetModificationTime(), fileInPb.GetAccessTime(), (short)fileInPb .GetReplication(), fileInPb.GetPreferredBlockSize(), unchecked ((byte)fileInPb.GetStoragePolicyID ()), xAttrs); } FileDiff diff = new FileDiff(pbf.GetSnapshotId(), copy, null, pbf.GetFileSize()); IList <HdfsProtos.BlockProto> bpl = pbf.GetBlocksList(); BlockInfoContiguous[] blocks = new BlockInfoContiguous[bpl.Count]; for (int j = 0; j < e; ++j) { Block blk = PBHelper.Convert(bpl[j]); BlockInfoContiguous storedBlock = fsn.GetBlockManager().GetStoredBlock(blk); if (storedBlock == null) { storedBlock = fsn.GetBlockManager().AddBlockCollection(new BlockInfoContiguous(blk , copy.GetFileReplication()), file); } blocks[j] = storedBlock; } if (blocks.Length > 0) { diff.SetBlocks(blocks); } diffs.AddFirst(diff); } file.AddSnapshotFeature(diffs); }
/// <exception cref="System.IO.IOException"/> private static FileDiff LoadFileDiff(FileDiff posterior, DataInput @in, FSImageFormat.Loader loader) { // 1. Read the id of the Snapshot root to identify the Snapshot Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot snapshot = loader.GetSnapshot (@in); // 2. Load file size long fileSize = @in.ReadLong(); // 3. Load snapshotINode INodeFileAttributes snapshotINode = @in.ReadBoolean() ? loader.LoadINodeFileAttributes (@in) : null; return(new FileDiff(snapshot.GetId(), snapshotINode, posterior, fileSize)); }
internal virtual bool ChangedBetweenSnapshots(INodeFile file, Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot from, Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot to) { int[] diffIndexPair = diffs.ChangedBetweenSnapshots(from, to); if (diffIndexPair == null) { return(false); } int earlierDiffIndex = diffIndexPair[0]; int laterDiffIndex = diffIndexPair[1]; IList <FileDiff> diffList = diffs.AsList(); long earlierLength = diffList[earlierDiffIndex].GetFileSize(); long laterLength = laterDiffIndex == diffList.Count ? file.ComputeFileSize(true, false) : diffList[laterDiffIndex].GetFileSize(); if (earlierLength != laterLength) { // file length has been changed return(true); } INodeFileAttributes earlierAttr = null; // check the metadata for (int i = earlierDiffIndex; i < laterDiffIndex; i++) { FileDiff diff = diffList[i]; if (diff.snapshotINode != null) { earlierAttr = diff.snapshotINode; break; } } if (earlierAttr == null) { // no meta-change at all, return false return(false); } INodeFileAttributes laterAttr = diffs.GetSnapshotINode(Math.Max(Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot .GetSnapshotId(from), Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot.GetSnapshotId (to)), file); return(!earlierAttr.MetadataEquals(laterAttr)); }