Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to start a zookeeper server instance in process, say for unit tests?

Calling org.apache.zookeeper.server.quorum.QuorumPeerMain.main() isn't working.

like image 219
marathon Avatar asked Feb 14 '12 23:02

marathon


People also ask

How do I start a ZooKeeper service?

To start the ZooKeeper server on a Linux system, use the Zookeeper/zookeeper/bin/zkServer.sh restart command from your Watson Explorer installation directory. On Microsoft Windows systems, use the Zookeeper\zookeeper\bin\zkServer. cmd command.

What is a ZooKeeper instance?

ZooKeeper is an open source Apache project that provides a centralized service for providing configuration information, naming, synchronization and group services over large clusters in distributed systems. The goal is to make these systems easier to manage with improved, more reliable propagation of changes.

How does a ZooKeeper quorum work?

It's basically the minimum number of server nodes that must be up and running and available for client requests. Any update done to the ZooKeeper tree by the clients must be persistently stored in this quorum of nodes for a transaction to be completed successfully.


2 Answers

Netfix opensourced Curator a framework to make use of Zookeeper even more convenient. It has build in test server class. Just add this test dependency to your project descriptor be it maven, gradle or else:

org.apache.curator:curator-framework:4.0.1 org.apache.curator:curator-test:4.0.1 

And here are the test essentials.

TestingServer zkTestServer; CuratorFramework cli;  @Before public void startZookeeper() throws Exception {     zkTestServer = new TestingServer(2181);     cli = CuratorFrameworkFactory.newClient(zkTestServer.getConnectString(), new RetryOneTime(2000));     cli.start(); }  @After public void stopZookeeper() throws IOException {     cli.close();     zkTestServer.stop(); } 

With cli creating any test data is very easy (requires the curator-framework dependency).

cli.create()    .creatingParentsIfNeeded()    .forPath("/a1", "testvalue".getBytes("UTF-8")); 
like image 105
gertas Avatar answered Oct 12 '22 00:10

gertas


To start ZooKeeper you have to execute ZooKeeperServerMain class.

You can use following code to start ZooKeeper in embedded mode.

Properties startupProperties = ...  QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig(); try {     quorumConfiguration.parseProperties(startupProperties); } catch(Exception e) {     throw new RuntimeException(e); }  zooKeeperServer = new ZooKeeperServerMain(); final ServerConfig configuration = new ServerConfig(); configuration.readFrom(quorumConfiguration);  new Thread() {     public void run() {         try {             zooKeeperServer.runFromConfig(configuration);         } catch (IOException e) {             log.error("ZooKeeper Failed", e);         }     } }.start(); 
like image 21
Mairbek Khadikov Avatar answered Oct 12 '22 01:10

Mairbek Khadikov