/** * Check that the environment for the bookie is correct. This means that the configuration has * stayed the same as the first run and the filesystem structure is up to date. */ private void checkEnvironment(ZooKeeper zk) throws BookieException, IOException { List<File> allLedgerDirs = new ArrayList<File>( ledgerDirsManager.getAllLedgerDirs().size() + indexDirsManager.getAllLedgerDirs().size()); allLedgerDirs.addAll(ledgerDirsManager.getAllLedgerDirs()); if (indexDirsManager != ledgerDirsManager) { allLedgerDirs.addAll(indexDirsManager.getAllLedgerDirs()); } if (zk == null) { // exists only for testing, just make sure directories are correct checkDirectoryStructure(journalDirectory); for (File dir : allLedgerDirs) { checkDirectoryStructure(dir); } return; } try { String instanceId = getInstanceId(zk); boolean newEnv = false; Cookie masterCookie = Cookie.generateCookie(conf); if (null != instanceId) { masterCookie.setInstanceId(instanceId); } try { Cookie zkCookie = Cookie.readFromZooKeeper(zk, conf); masterCookie.verify(zkCookie); } catch (KeeperException.NoNodeException nne) { newEnv = true; } List<File> missedCookieDirs = new ArrayList<File>(); checkDirectoryStructure(journalDirectory); // try to read cookie from journal directory try { Cookie journalCookie = Cookie.readFromDirectory(journalDirectory); journalCookie.verify(masterCookie); } catch (FileNotFoundException fnf) { missedCookieDirs.add(journalDirectory); } for (File dir : allLedgerDirs) { checkDirectoryStructure(dir); try { Cookie c = Cookie.readFromDirectory(dir); c.verify(masterCookie); } catch (FileNotFoundException fnf) { missedCookieDirs.add(dir); } } if (!newEnv && missedCookieDirs.size() > 0) { LOG.error( "Cookie exists in zookeeper, but not in all local directories. " + " Directories missing cookie file are " + missedCookieDirs); throw new BookieException.InvalidCookieException(); } if (newEnv) { if (missedCookieDirs.size() > 0) { LOG.debug("Directories missing cookie file are {}", missedCookieDirs); masterCookie.writeToDirectory(journalDirectory); for (File dir : allLedgerDirs) { masterCookie.writeToDirectory(dir); } } masterCookie.writeToZooKeeper(zk, conf); } } catch (KeeperException ke) { LOG.error("Couldn't access cookie in zookeeper", ke); throw new BookieException.InvalidCookieException(ke); } catch (UnknownHostException uhe) { LOG.error("Couldn't check cookies, networking is broken", uhe); throw new BookieException.InvalidCookieException(uhe); } catch (IOException ioe) { LOG.error("Error accessing cookie on disks", ioe); throw new BookieException.InvalidCookieException(ioe); } catch (InterruptedException ie) { LOG.error("Thread interrupted while checking cookies, exiting", ie); throw new BookieException.InvalidCookieException(ie); } }