FSImage implements Closeable { public synchronized boolean saveNamespace(long timeWindow, long txGap, FSNamesystem source) throws IOException { if (timeWindow > 0 || txGap > 0) { final FSImageStorageInspector inspector = storage.readAndInspectDirs( EnumSet.of(NameNodeFile.IMAGE, NameNodeFile.IMAGE_ROLLBACK), StartupOption.REGULAR); FSImageFile image = inspector.getLatestImages().get(0); File imageFile = image.getFile(); final long checkpointTxId = image.getCheckpointTxId(); final long checkpointAge = Time.now() - imageFile.lastModified(); if (checkpointAge <= timeWindow * 1000 && checkpointTxId >= this.getLastAppliedOrWrittenTxId() - txGap) { return false; } } saveNamespace(source, NameNodeFile.IMAGE, null); return true; } FSImage(Configuration conf); protected  FSImage(Configuration conf,
                    Collection<URI> imageDirs,
                    List<URI> editsDirs); static boolean recoverStorageDirs(StartupOption startOpt,
      NNStorage storage, Map<StorageDirectory, StorageState> dataDirStates); static void checkUpgrade(NNStorage storage); boolean hasRollbackFSImage(); FSEditLog getEditLog(); void initEditLog(StartupOption startOpt); long loadEdits(Iterable<EditLogInputStream> editStreams,
      FSNamesystem target); void saveLegacyOIVImage(FSNamesystem source, String targetDir,
      Canceler canceler); synchronized void updateStorageVersion(); synchronized boolean saveNamespace(long timeWindow, long txGap,
      FSNamesystem source); void saveNamespace(FSNamesystem source); synchronized void saveNamespace(FSNamesystem source, NameNodeFile nnf,
      Canceler canceler); boolean addToCheckpointing(long txid); void removeFromCheckpointing(long txid); synchronized void saveDigestAndRenameCheckpointImage(NameNodeFile nnf,
      long txid, MD5Hash digest); @Override synchronized void close(); NNStorage getStorage(); int getLayoutVersion(); int getNamespaceID(); String getClusterID(); String getBlockPoolID(); synchronized long getLastAppliedTxId(); long getLastAppliedOrWrittenTxId(); void updateLastAppliedTxIdFromWritten(); long getMostRecentCheckpointTxId(); static final Log LOG; }
@Test public void testDigest() throws IOException { Configuration conf = new Configuration(); MiniDFSCluster cluster = null; try { cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build(); DistributedFileSystem fs = cluster.getFileSystem(); fs.setSafeMode(SafeModeAction.SAFEMODE_ENTER); fs.saveNamespace(); fs.setSafeMode(SafeModeAction.SAFEMODE_LEAVE); File currentDir = FSImageTestUtil.getNameNodeCurrentDirs(cluster, 0).get( 0); File fsimage = FSImageTestUtil.findNewestImageFile(currentDir .getAbsolutePath()); assertEquals(MD5FileUtils.readStoredMd5ForFile(fsimage), MD5FileUtils.computeMd5ForFile(fsimage)); } finally { if (cluster != null) { cluster.shutdown(); } } }
@Test public void testHasNonEcBlockUsingStripedIDForLoadFile() throws IOException{ Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = null; try { cluster = new MiniDFSCluster.Builder(conf).numDataNodes(9) .build(); cluster.waitActive(); DistributedFileSystem fs = cluster.getFileSystem(); FSNamesystem fns = cluster.getNamesystem(); String testDir = "/test_block_manager"; String testFile = "testfile_loadfile"; String testFilePath = testDir + "/" + testFile; String clientName = "testUser_loadfile"; String clientMachine = "testMachine_loadfile"; long blkId = -1; long blkNumBytes = 1024; long timestamp = 1426222918; fs.mkdir(new Path(testDir), new FsPermission("755")); Path p = new Path(testFilePath); DFSTestUtil.createFile(fs, p, 0, (short) 1, 1); BlockInfoContiguous cBlk = new BlockInfoContiguous( new Block(blkId, blkNumBytes, timestamp), (short)3); INodeFile file = (INodeFile)fns.getFSDirectory().getINode(testFilePath); file.toUnderConstruction(clientName, clientMachine); file.addBlock(cBlk); file.toCompleteFile(System.currentTimeMillis()); fns.enterSafeMode(false); fns.saveNamespace(0, 0); cluster.restartNameNodes(); cluster.waitActive(); fns = cluster.getNamesystem(); assertTrue(fns.getBlockManager().hasNonEcBlockUsingStripedID()); fs = cluster.getFileSystem(); fs.delete(p,false); fns.enterSafeMode(false); fns.saveNamespace(0, 0); cluster.restartNameNodes(); cluster.waitActive(); fns = cluster.getNamesystem(); assertFalse(fns.getBlockManager().hasNonEcBlockUsingStripedID()); cluster.shutdown(); cluster = null; } finally { if (cluster != null) { cluster.shutdown(); } } }
@Test public void testHasNonEcBlockUsingStripedIDForLoadUCFile() throws IOException{ Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = null; try { cluster = new MiniDFSCluster.Builder(conf).numDataNodes(9) .build(); cluster.waitActive(); DistributedFileSystem fs = cluster.getFileSystem(); FSNamesystem fns = cluster.getNamesystem(); String testDir = "/test_block_manager"; String testFile = "testfile_loaducfile"; String testFilePath = testDir + "/" + testFile; String clientName = "testUser_loaducfile"; String clientMachine = "testMachine_loaducfile"; long blkId = -1; long blkNumBytes = 1024; long timestamp = 1426222918; fs.mkdir(new Path(testDir), new FsPermission("755")); Path p = new Path(testFilePath); DFSTestUtil.createFile(fs, p, 0, (short) 1, 1); BlockInfoContiguous cBlk = new BlockInfoContiguous( new Block(blkId, blkNumBytes, timestamp), (short)3); INodeFile file = (INodeFile)fns.getFSDirectory().getINode(testFilePath); file.toUnderConstruction(clientName, clientMachine); file.addBlock(cBlk); fns.enterSafeMode(false); fns.saveNamespace(0, 0); cluster.restartNameNodes(); cluster.waitActive(); fns = cluster.getNamesystem(); assertTrue(fns.getBlockManager().hasNonEcBlockUsingStripedID()); cluster.shutdown(); cluster = null; } finally { if (cluster != null) { cluster.shutdown(); } } }
@Test public void testHasNonEcBlockUsingStripedIDForLoadSnapshot() throws IOException{ Configuration conf = new HdfsConfiguration(); MiniDFSCluster cluster = null; try { cluster = new MiniDFSCluster.Builder(conf).numDataNodes(9) .build(); cluster.waitActive(); DistributedFileSystem fs = cluster.getFileSystem(); FSNamesystem fns = cluster.getNamesystem(); String testDir = "/test_block_manager"; String testFile = "testfile_loadSnapshot"; String testFilePath = testDir + "/" + testFile; String clientName = "testUser_loadSnapshot"; String clientMachine = "testMachine_loadSnapshot"; long blkId = -1; long blkNumBytes = 1024; long timestamp = 1426222918; Path d = new Path(testDir); fs.mkdir(d, new FsPermission("755")); fs.allowSnapshot(d); Path p = new Path(testFilePath); DFSTestUtil.createFile(fs, p, 0, (short) 1, 1); BlockInfoContiguous cBlk = new BlockInfoContiguous( new Block(blkId, blkNumBytes, timestamp), (short)3); INodeFile file = (INodeFile)fns.getFSDirectory().getINode(testFilePath); file.toUnderConstruction(clientName, clientMachine); file.addBlock(cBlk); file.toCompleteFile(System.currentTimeMillis()); fs.createSnapshot(d,"testHasNonEcBlockUsingStripeID"); fs.truncate(p,0); fns.enterSafeMode(false); fns.saveNamespace(0, 0); cluster.restartNameNodes(); cluster.waitActive(); fns = cluster.getNamesystem(); assertTrue(fns.getBlockManager().hasNonEcBlockUsingStripedID()); cluster.shutdown(); cluster = null; } finally { if (cluster != null) { cluster.shutdown(); } } }