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);
}
}
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();
}
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());
}
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));
}
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);
}
Aggregations