Search in sources :

Example 1 with InvalidCookieException

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);
    }
}
Also used : RegistrationManager(org.apache.bookkeeper.discover.RegistrationManager) Set(java.util.Set) InvalidCookieException(org.apache.bookkeeper.bookie.BookieException.InvalidCookieException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InvalidCookieException(org.apache.bookkeeper.bookie.BookieException.InvalidCookieException) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) List(java.util.List) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with InvalidCookieException

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
    }
}
Also used : InvalidCookieException(org.apache.bookkeeper.bookie.BookieException.InvalidCookieException) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) Test(org.junit.Test)

Example 3 with InvalidCookieException

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
    }
}
Also used : InvalidCookieException(org.apache.bookkeeper.bookie.BookieException.InvalidCookieException) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) Test(org.junit.Test)

Example 4 with InvalidCookieException

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();
    }
}
Also used : InvalidCookieException(org.apache.bookkeeper.bookie.BookieException.InvalidCookieException) ArrayList(java.util.ArrayList) File(java.io.File)

Example 5 with 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);
    }
}
Also used : InvalidCookieException(org.apache.bookkeeper.bookie.BookieException.InvalidCookieException) Versioned(org.apache.bookkeeper.versioning.Versioned) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IOException(java.io.IOException)

Aggregations

InvalidCookieException (org.apache.bookkeeper.bookie.BookieException.InvalidCookieException)7 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)4 File (java.io.File)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BufferedReader (java.io.BufferedReader)1 StringReader (java.io.StringReader)1 List (java.util.List)1 Set (java.util.Set)1 RegistrationManager (org.apache.bookkeeper.discover.RegistrationManager)1 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)1 BookieServer (org.apache.bookkeeper.proto.BookieServer)1 Versioned (org.apache.bookkeeper.versioning.Versioned)1