use of org.apache.bookkeeper.bookie.BookieException.InvalidCookieException in project bookkeeper by apache.
the class Bookie method checkEnvironmentWithStorageExpansion.
public static void checkEnvironmentWithStorageExpansion(ServerConfiguration conf, MetadataBookieDriver metadataDriver, List<File> journalDirectories, List<File> allLedgerDirs) throws BookieException {
RegistrationManager rm = metadataDriver.getRegistrationManager();
try {
// 1. retrieve the instance id
String instanceId = rm.getClusterInstanceId();
// 2. build the master cookie from the configuration
Cookie.Builder builder = Cookie.generateCookie(conf);
if (null != instanceId) {
builder.setInstanceId(instanceId);
}
Cookie masterCookie = builder.build();
boolean allowExpansion = conf.getAllowStorageExpansion();
// 3. read the cookie from registration manager. it is the `source-of-truth` of a given bookie.
// if it doesn't exist in registration manager, this bookie is a new bookie, otherwise it is
// an old bookie.
List<BookieSocketAddress> possibleBookieIds = possibleBookieIds(conf);
final Versioned<Cookie> rmCookie = readAndVerifyCookieFromRegistrationManager(masterCookie, rm, possibleBookieIds, allowExpansion);
// 4. check if the cookie appear in all the directories.
List<File> missedCookieDirs = new ArrayList<>();
List<Cookie> existingCookies = Lists.newArrayList();
if (null != rmCookie) {
existingCookies.add(rmCookie.getValue());
}
// 4.1 verify the cookies in journal directories
Pair<List<File>, List<Cookie>> journalResult = verifyAndGetMissingDirs(masterCookie, allowExpansion, journalDirectories);
missedCookieDirs.addAll(journalResult.getLeft());
existingCookies.addAll(journalResult.getRight());
// 4.2. verify the cookies in ledger directories
Pair<List<File>, List<Cookie>> ledgerResult = verifyAndGetMissingDirs(masterCookie, allowExpansion, allLedgerDirs);
missedCookieDirs.addAll(ledgerResult.getLeft());
existingCookies.addAll(ledgerResult.getRight());
// - a directory has been corrupted/wiped, which is an error
if (!missedCookieDirs.isEmpty()) {
if (rmCookie == null) {
// 5.1 new environment: all directories should be empty
verifyDirsForNewEnvironment(missedCookieDirs);
stampNewCookie(conf, masterCookie, rm, Version.NEW, journalDirectories, allLedgerDirs);
} else if (allowExpansion) {
// 5.2 storage is expanding
Set<File> knownDirs = getKnownDirs(existingCookies);
verifyDirsForStorageExpansion(missedCookieDirs, knownDirs);
stampNewCookie(conf, masterCookie, rm, rmCookie.getVersion(), journalDirectories, allLedgerDirs);
} else {
// 5.3 Cookie-less directories and
// we can't do anything with them
LOG.error("There are directories without a cookie," + " and this is neither a new environment," + " nor is storage expansion enabled. " + "Empty directories are {}", missedCookieDirs);
throw new InvalidCookieException();
}
}
} catch (IOException ioe) {
LOG.error("Error accessing cookie on disks", ioe);
throw new BookieException.InvalidCookieException(ioe);
}
}
use of org.apache.bookkeeper.bookie.BookieException.InvalidCookieException in project bookkeeper by apache.
the class CookieTest method testRestartWithAdvertisedAddressAsBookieID.
/**
* Test restart bookie with new advertisedAddress, which had cookie generated with ip.
*/
@Test
public void testRestartWithAdvertisedAddressAsBookieID() throws Exception {
String[] ledgerDirs = new String[] { newDirectory(), newDirectory(), newDirectory() };
String journalDir = newDirectory();
ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
conf.setJournalDirName(journalDir).setLedgerDirNames(ledgerDirs).setBookiePort(bookiePort).setZkServers(zkUtil.getZooKeeperConnectString());
conf.setUseHostNameAsBookieID(false);
// should work fine
Bookie b = new Bookie(conf);
b.start();
b.shutdown();
conf.setAdvertisedAddress("unknown");
try {
new Bookie(conf);
fail("Should not start a bookie with ip if the bookie has been started with an ip");
} catch (InvalidCookieException e) {
// expected
}
}
use of org.apache.bookkeeper.bookie.BookieException.InvalidCookieException in project bookkeeper by apache.
the class CookieTest method testRestartWithIpAddressAsBookieID.
/**
* Test restart bookie with useHostNameAsBookieID=false, which had cookie generated
* with hostname.
*/
@Test
public void testRestartWithIpAddressAsBookieID() throws Exception {
String[] ledgerDirs = new String[] { newDirectory(), newDirectory(), newDirectory() };
String journalDir = newDirectory();
ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
conf.setJournalDirName(journalDir).setLedgerDirNames(ledgerDirs).setBookiePort(bookiePort).setZkServers(zkUtil.getZooKeeperConnectString());
conf.setUseHostNameAsBookieID(true);
// should work fine
Bookie b = new Bookie(conf);
b.start();
b.shutdown();
conf.setUseHostNameAsBookieID(false);
try {
new Bookie(conf);
fail("Should not start a bookie with ip if the bookie has been started with an ip");
} catch (InvalidCookieException e) {
// expected
}
}
use of org.apache.bookkeeper.bookie.BookieException.InvalidCookieException in project bookkeeper by apache.
the class Bookie method verifyDirsForStorageExpansion.
private static void verifyDirsForStorageExpansion(List<File> missedCookieDirs, Set<File> existingLedgerDirs) throws InvalidCookieException {
List<File> dirsMissingData = new ArrayList<File>();
List<File> nonEmptyDirs = new ArrayList<File>();
for (File dir : missedCookieDirs) {
if (existingLedgerDirs.contains(dir.getParentFile())) {
// if one of the existing ledger dirs doesn't have cookie,
// let us not proceed further
dirsMissingData.add(dir);
continue;
}
String[] content = dir.list();
if (content != null && content.length != 0) {
nonEmptyDirs.add(dir);
}
}
if (dirsMissingData.size() > 0 || nonEmptyDirs.size() > 0) {
LOG.error("Either not all local directories have cookies or directories being added " + " newly are not empty. " + "Directories missing cookie file are: " + dirsMissingData + " New directories that are not empty are: " + nonEmptyDirs);
throw new InvalidCookieException();
}
}
use of org.apache.bookkeeper.bookie.BookieException.InvalidCookieException in project bookkeeper by apache.
the class Cookie method readFromRegistrationManager.
/**
* Read cookie from registration manager for a given bookie <i>address</i>.
*
* @param rm registration manager
* @param address bookie address
* @return versioned cookie object
* @throws BookieException when fail to read cookie
*/
public static Versioned<Cookie> readFromRegistrationManager(RegistrationManager rm, BookieSocketAddress address) throws BookieException {
Versioned<byte[]> cookieData = rm.readCookie(address.toString());
try {
try (BufferedReader reader = new BufferedReader(new StringReader(new String(cookieData.getValue(), UTF_8)))) {
Builder builder = parse(reader);
Cookie cookie = builder.build();
return new Versioned<Cookie>(cookie, cookieData.getVersion());
}
} catch (IOException ioe) {
throw new InvalidCookieException(ioe);
}
}
Aggregations