use of org.apache.bookkeeper.meta.MetadataBookieDriver in project bookkeeper by apache.
the class Bookie method instantiateMetadataDriver.
/**
* Instantiate the metadata driver for the Bookie.
*/
private MetadataBookieDriver instantiateMetadataDriver(ServerConfiguration conf) throws BookieException {
try {
String metadataServiceUriStr = conf.getMetadataServiceUri();
if (null == metadataServiceUriStr) {
return null;
}
MetadataBookieDriver driver = MetadataDrivers.getBookieDriver(URI.create(metadataServiceUriStr));
driver.initialize(conf, () -> {
stateManager.forceToUnregistered();
// schedule a re-register operation
stateManager.registerBookie(false);
}, statsLogger);
return driver;
} catch (MetadataException me) {
throw new MetadataStoreException("Failed to initialize metadata bookie driver", me);
} catch (ConfigurationException e) {
throw new BookieIllegalOpException(e);
}
}
use of org.apache.bookkeeper.meta.MetadataBookieDriver in project bookkeeper by apache.
the class ExpandStorageService method handle.
/*
* Add new empty ledger/index directories.
* Update the directories info in the conf file before running the command.
*/
@Override
public HttpServiceResponse handle(HttpServiceRequest request) throws Exception {
HttpServiceResponse response = new HttpServiceResponse();
if (HttpServer.Method.PUT == request.getMethod()) {
File[] ledgerDirectories = Bookie.getCurrentDirectories(conf.getLedgerDirs());
File[] journalDirectories = Bookie.getCurrentDirectories(conf.getJournalDirs());
File[] indexDirectories;
if (null == conf.getIndexDirs()) {
indexDirectories = ledgerDirectories;
} else {
indexDirectories = Bookie.getCurrentDirectories(conf.getIndexDirs());
}
List<File> allLedgerDirs = Lists.newArrayList();
allLedgerDirs.addAll(Arrays.asList(ledgerDirectories));
if (indexDirectories != ledgerDirectories) {
allLedgerDirs.addAll(Arrays.asList(indexDirectories));
}
try (MetadataBookieDriver driver = MetadataDrivers.getBookieDriver(URI.create(conf.getMetadataServiceUri()))) {
driver.initialize(conf, () -> {
}, NullStatsLogger.INSTANCE);
Bookie.checkEnvironmentWithStorageExpansion(conf, driver, Lists.newArrayList(journalDirectories), allLedgerDirs);
} catch (BookieException e) {
LOG.error("Exception occurred while updating cookie for storage expansion", e);
response.setCode(HttpServer.StatusCode.INTERNAL_ERROR);
response.setBody("Exception while updating cookie for storage expansion");
return response;
}
String jsonResponse = "Success expand storage";
LOG.debug("output body:" + jsonResponse);
response.setBody(jsonResponse);
response.setCode(HttpServer.StatusCode.OK);
return response;
} else {
response.setCode(HttpServer.StatusCode.NOT_FOUND);
response.setBody("Not found method. Should be PUT method");
return response;
}
}
use of org.apache.bookkeeper.meta.MetadataBookieDriver in project bookkeeper by apache.
the class BookieInitializationTest method testBookieRegistration.
/**
* Verify the bookie reg. Restarting bookie server will wait for the session
* timeout when previous reg node exists in zk. On zNode delete event,
* should continue startup
*/
@Test
public void testBookieRegistration() throws Exception {
final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
conf.setZkServers(zkUtil.getZooKeeperConnectString()).setListeningInterface(null);
String bookieId = Bookie.getBookieAddress(conf).toString();
final String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + bookieId;
driver.initialize(conf, () -> {
}, NullStatsLogger.INSTANCE);
try (StateManager manager = new BookieStateManager(conf, driver)) {
manager.registerBookie(true).get();
}
Stat bkRegNode1 = zkc.exists(bkRegPath, false);
assertNotNull("Bookie registration has been failed", bkRegNode1);
// zkclient and doing the registration.
try (MetadataBookieDriver newDriver = new ZKMetadataBookieDriver()) {
newDriver.initialize(conf, () -> {
}, NullStatsLogger.INSTANCE);
try (ZooKeeperClient newZk = createNewZKClient()) {
// deleting the znode, so that the bookie registration should
// continue successfully on NodeDeleted event
new Thread(() -> {
try {
Thread.sleep(conf.getZkTimeout() / 3);
zkc.delete(bkRegPath, -1);
} catch (Exception e) {
// Not handling, since the testRegisterBookie will fail
LOG.error("Failed to delete the znode :" + bkRegPath, e);
}
}).start();
try (StateManager newMgr = new BookieStateManager(conf, newDriver)) {
newMgr.registerBookie(true).get();
} catch (IOException e) {
Throwable t = e.getCause();
if (t instanceof KeeperException) {
KeeperException ke = (KeeperException) t;
assertTrue("ErrorCode:" + ke.code() + ", Registration node exists", ke.code() != KeeperException.Code.NODEEXISTS);
}
throw e;
}
// verify ephemeral owner of the bkReg znode
Stat bkRegNode2 = newZk.exists(bkRegPath, false);
assertNotNull("Bookie registration has been failed", bkRegNode2);
assertTrue("Bookie is referring to old registration znode:" + bkRegNode1 + ", New ZNode:" + bkRegNode2, bkRegNode1.getEphemeralOwner() != bkRegNode2.getEphemeralOwner());
}
}
}
use of org.apache.bookkeeper.meta.MetadataBookieDriver 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.meta.MetadataBookieDriver in project bookkeeper by apache.
the class BookieInitializationTest method testRegNodeExistsAfterSessionTimeOut.
/**
* Verify the bookie registration, it should throw
* KeeperException.NodeExistsException if the znode still exists even after
* the zk session timeout.
*/
@Test
public void testRegNodeExistsAfterSessionTimeOut() throws Exception {
final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(zkUtil.getZooKeeperConnectString()).setListeningInterface(null);
String bookieId = InetAddress.getLocalHost().getHostAddress() + ":" + conf.getBookiePort();
String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + bookieId;
driver.initialize(conf, () -> {
}, NullStatsLogger.INSTANCE);
try (StateManager manager = new BookieStateManager(conf, driver)) {
manager.registerBookie(true).get();
assertTrue("Bookie registration node doesn't exists!", driver.getRegistrationManager().isBookieRegistered(bookieId));
}
Stat bkRegNode1 = zkc.exists(bkRegPath, false);
assertNotNull("Bookie registration has been failed", bkRegNode1);
// zkclient and doing the registration.
try (MetadataBookieDriver newDriver = new ZKMetadataBookieDriver()) {
newDriver.initialize(conf, () -> {
}, NullStatsLogger.INSTANCE);
try (StateManager newMgr = new BookieStateManager(conf, newDriver)) {
newMgr.registerBookie(true).get();
fail("Should throw NodeExistsException as the znode is not getting expired");
} catch (ExecutionException ee) {
// IOException
Throwable e = ee.getCause();
// BookieException.MetadataStoreException
Throwable t1 = e.getCause();
// IOException
Throwable t2 = t1.getCause();
// KeeperException.NodeExistsException
Throwable t3 = t2.getCause();
if (t3 instanceof KeeperException) {
KeeperException ke = (KeeperException) t3;
assertTrue("ErrorCode:" + ke.code() + ", Registration node doesn't exists", ke.code() == KeeperException.Code.NODEEXISTS);
// verify ephemeral owner of the bkReg znode
Stat bkRegNode2 = zkc.exists(bkRegPath, false);
assertNotNull("Bookie registration has been failed", bkRegNode2);
assertTrue("Bookie wrongly registered. Old registration znode:" + bkRegNode1 + ", New znode:" + bkRegNode2, bkRegNode1.getEphemeralOwner() == bkRegNode2.getEphemeralOwner());
return;
}
throw ee;
}
}
}
Aggregations