Search in sources :

Example 1 with RegistrationManager

use of org.apache.bookkeeper.discover.RegistrationManager 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 RegistrationManager

use of org.apache.bookkeeper.discover.RegistrationManager in project bookkeeper by apache.

the class ZKMetadataBookieDriver method close.

@Override
public void close() {
    RegistrationManager rmToClose;
    synchronized (this) {
        rmToClose = regManager;
        regManager = null;
    }
    if (null != rmToClose) {
        rmToClose.close();
    }
    super.close();
}
Also used : ZKRegistrationManager(org.apache.bookkeeper.discover.ZKRegistrationManager) RegistrationManager(org.apache.bookkeeper.discover.RegistrationManager)

Example 3 with RegistrationManager

use of org.apache.bookkeeper.discover.RegistrationManager in project bookkeeper by apache.

the class BookieInitializationTest method testExitCodeZK_REG_FAIL.

/**
 * Verify the bookie server exit code. On ZooKeeper exception, should return
 * exit code ZK_REG_FAIL = 4
 */
@Test
public void testExitCodeZK_REG_FAIL() throws Exception {
    File tmpDir = createTempDir("bookie", "test");
    final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
    conf.setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() }).setZkServers(zkUtil.getZooKeeperConnectString());
    RegistrationManager rm = mock(RegistrationManager.class);
    doThrow(new MetadataStoreException("mocked exception")).when(rm).registerBookie(anyString(), anyBoolean());
    // simulating ZooKeeper exception by assigning a closed zk client to bk
    BookieServer bkServer = new BookieServer(conf) {

        protected Bookie newBookie(ServerConfiguration conf) throws IOException, KeeperException, InterruptedException, BookieException {
            Bookie bookie = new Bookie(conf);
            MetadataBookieDriver driver = Whitebox.getInternalState(bookie, "metadataDriver");
            ((ZKMetadataBookieDriver) driver).setRegManager(rm);
            return bookie;
        }
    };
    bkServer.start();
    bkServer.join();
    assertEquals("Failed to return ExitCode.ZK_REG_FAIL", ExitCode.ZK_REG_FAIL, bkServer.getExitCode());
}
Also used : RegistrationManager(org.apache.bookkeeper.discover.RegistrationManager) MetadataStoreException(org.apache.bookkeeper.bookie.BookieException.MetadataStoreException) ZKMetadataBookieDriver(org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) ZKMetadataBookieDriver(org.apache.bookkeeper.meta.zk.ZKMetadataBookieDriver) MetadataBookieDriver(org.apache.bookkeeper.meta.MetadataBookieDriver) BookieServer(org.apache.bookkeeper.proto.BookieServer) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) Test(org.junit.Test)

Example 4 with RegistrationManager

use of org.apache.bookkeeper.discover.RegistrationManager in project bookkeeper by apache.

the class BookieShellTest method setup.

@Before
public void setup() throws Exception {
    // setup the required mocks before constructing bookie shell.
    this.mockLastMarkCommand = mock(LastMarkCommand.class);
    whenNew(LastMarkCommand.class).withNoArguments().thenReturn(mockLastMarkCommand);
    this.mockSimpleTestCommand = spy(new SimpleTestCommand());
    doNothing().when(mockSimpleTestCommand).run(any(ServerConfiguration.class));
    whenNew(SimpleTestCommand.class).withNoArguments().thenReturn(mockSimpleTestCommand);
    this.mockListBookiesCommand = spy(new ListBookiesCommand());
    doNothing().when(mockListBookiesCommand).run(any(ServerConfiguration.class));
    whenNew(ListBookiesCommand.class).withNoArguments().thenReturn(mockListBookiesCommand);
    // construct the bookie shell.
    this.shell = new BookieShell(LedgerIdFormatter.LONG_LEDGERID_FORMATTER, EntryFormatter.STRING_FORMATTER);
    this.admin = PowerMockito.mock(BookKeeperAdmin.class);
    whenNew(BookKeeperAdmin.class).withParameterTypes(ClientConfiguration.class).withArguments(any(ClientConfiguration.class)).thenReturn(admin);
    this.clientConf = new ClientConfiguration();
    this.clientConf.setMetadataServiceUri("zk://127.0.0.1/path/to/ledgers");
    when(admin.getConf()).thenReturn(this.clientConf);
    this.rm = PowerMockito.mock(RegistrationManager.class);
    this.cookie = Cookie.newBuilder().setBookieHost("127.0.0.1:3181").setInstanceId("xyz").setJournalDirs("/path/to/journal/dir").setLedgerDirs("/path/to/journal/dir").setLayoutVersion(Cookie.CURRENT_COOKIE_LAYOUT_VERSION).build();
    this.version = new LongVersion(1L);
    when(rm.readCookie(anyString())).thenReturn(new Versioned<>(cookie.toString().getBytes(UTF_8), version));
    this.driver = mock(MetadataBookieDriver.class);
    when(driver.getRegistrationManager()).thenReturn(rm);
    PowerMockito.mockStatic(MetadataDrivers.class);
    PowerMockito.doAnswer(invocationOnMock -> {
        Function<RegistrationManager, Object> function = invocationOnMock.getArgument(1);
        function.apply(rm);
        return null;
    }).when(MetadataDrivers.class, "runFunctionWithRegistrationManager", any(ServerConfiguration.class), any(Function.class));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LongVersion(org.apache.bookkeeper.versioning.LongVersion) EntryFormatter(org.apache.bookkeeper.util.EntryFormatter) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) LedgerMetadata(org.apache.bookkeeper.client.LedgerMetadata) UTF_8(com.google.common.base.Charsets.UTF_8) PowerMockito.verifyNew(org.powermock.api.mockito.PowerMockito.verifyNew) RecoverCmd(org.apache.bookkeeper.bookie.BookieShell.RecoverCmd) MetadataDrivers(org.apache.bookkeeper.meta.MetadataDrivers) Function(java.util.function.Function) RegistrationManager(org.apache.bookkeeper.discover.RegistrationManager) ListBookiesCommand(org.apache.bookkeeper.tools.cli.commands.cluster.ListBookiesCommand) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) BasicParser(org.apache.commons.cli.BasicParser) Versioned(org.apache.bookkeeper.versioning.Versioned) CommandLine(org.apache.commons.cli.CommandLine) Assert.fail(org.junit.Assert.fail) PowerMockito.whenNew(org.powermock.api.mockito.PowerMockito.whenNew) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) Maps(com.google.common.collect.Maps) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) LastMarkCommand(org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand) SimpleTestCommand(org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand) PowerMockito.spy(org.powermock.api.mockito.PowerMockito.spy) MyCommand(org.apache.bookkeeper.bookie.BookieShell.MyCommand) ParseException(org.apache.commons.cli.ParseException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) LedgerIdFormatter(org.apache.bookkeeper.util.LedgerIdFormatter) Assert.assertEquals(org.junit.Assert.assertEquals) SortedMap(java.util.SortedMap) MetadataBookieDriver(org.apache.bookkeeper.meta.MetadataBookieDriver) Version(org.apache.bookkeeper.versioning.Version) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Mockito.mock(org.mockito.Mockito.mock) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) RegistrationManager(org.apache.bookkeeper.discover.RegistrationManager) SimpleTestCommand(org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) MetadataBookieDriver(org.apache.bookkeeper.meta.MetadataBookieDriver) Function(java.util.function.Function) LastMarkCommand(org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand) ListBookiesCommand(org.apache.bookkeeper.tools.cli.commands.cluster.ListBookiesCommand) LongVersion(org.apache.bookkeeper.versioning.LongVersion) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Before(org.junit.Before)

Example 5 with RegistrationManager

use of org.apache.bookkeeper.discover.RegistrationManager in project bookkeeper by apache.

the class ZKMetadataBookieDriverTest method testGetRegManager.

@Test
public void testGetRegManager() throws Exception {
    RegistrationListener listener = mock(RegistrationListener.class);
    driver.initialize(conf, listener, NullStatsLogger.INSTANCE);
    assertSame(conf, driver.serverConf);
    assertSame(listener, driver.listener);
    assertNull(driver.regManager);
    ZKRegistrationManager mockRegManager = PowerMockito.mock(ZKRegistrationManager.class);
    PowerMockito.whenNew(ZKRegistrationManager.class).withParameterTypes(ServerConfiguration.class, ZooKeeper.class, RegistrationListener.class).withArguments(any(ServerConfiguration.class), any(ZooKeeper.class), any(RegistrationListener.class)).thenReturn(mockRegManager);
    RegistrationManager manager = driver.getRegistrationManager();
    assertSame(mockRegManager, manager);
    assertSame(mockRegManager, driver.regManager);
    PowerMockito.verifyNew(ZKRegistrationManager.class, times(1)).withArguments(same(conf), same(mockZkc), same(listener));
    driver.close();
    verify(mockRegManager, times(1)).close();
    assertNull(driver.regManager);
}
Also used : RegistrationListener(org.apache.bookkeeper.discover.RegistrationManager.RegistrationListener) ZKRegistrationManager(org.apache.bookkeeper.discover.ZKRegistrationManager) RegistrationManager(org.apache.bookkeeper.discover.RegistrationManager) ZKRegistrationManager(org.apache.bookkeeper.discover.ZKRegistrationManager) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

RegistrationManager (org.apache.bookkeeper.discover.RegistrationManager)5 Test (org.junit.Test)3 File (java.io.File)2 Set (java.util.Set)2 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)2 ZKRegistrationManager (org.apache.bookkeeper.discover.ZKRegistrationManager)2 MetadataBookieDriver (org.apache.bookkeeper.meta.MetadataBookieDriver)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 UTF_8 (com.google.common.base.Charsets.UTF_8)1 Maps (com.google.common.collect.Maps)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SortedMap (java.util.SortedMap)1 Function (java.util.function.Function)1 InvalidCookieException (org.apache.bookkeeper.bookie.BookieException.InvalidCookieException)1 MetadataStoreException (org.apache.bookkeeper.bookie.BookieException.MetadataStoreException)1 MyCommand (org.apache.bookkeeper.bookie.BookieShell.MyCommand)1 RecoverCmd (org.apache.bookkeeper.bookie.BookieShell.RecoverCmd)1