GithubHelp home page GithubHelp logo

classicvalues / wookiee-zookeeper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from oracle/wookiee-zookeeper

0.0 1.0 0.0 214 KB

Wookiee Component - Zookeeper

License: Apache License 2.0

Scala 99.31% Java 0.37% Shell 0.32%

wookiee-zookeeper's Introduction

Wookiee - Component: ZooKeeper

Build Status Latest Release License

Main Wookiee Project

For Configuration information see ZooKeeper Config

Adding to Pom

Add the jfrog repo to your project first:

<repositories>
    <repository>
        <id>JFrog</id>
        <url>http://oss.jfrog.org/oss-release-local</url>
    </repository>
</repositories>

Add latest version of wookiee:

<dependency>
    <groupId>com.webtrends</groupId>
    <artifactId>wookiee-zookeeper_2.11</artifactId>
    <version>${wookiee.version}</version>
</dependency>

Overview

Each instance of Wookiee can automatically manages itself with ZooKeeper so that others nodes can make use of clustering. Wookiee does this through the use of Curator which then allows the platform to provide interaction with ZooKeeper through the instance of Curator that is managed.

Basic Usage

The system is designed so that a developer simply needs to apply a trait (interface) on an actor in order to have full access to this functionality. Wookiee is responsible for managing the communication with ZooKeeper and thus a service does not need to manage its own communication. Currently, not all functionality is supported, but it does include the ability to get node data, set node data, create nodes, getting node children, receiving ZooKeeper state events, receiving node child events, and managing node leadership.

Interacting with ZooKeeper Nodes

In order to take full benefit of the functionality one simply needs to apply the trait ZookeeperAdapter and then start interacting with the system.

import com.webtrends.harness.component.zookeeper.ZookeeperAdapter

// Define the actor and have it apply the ZookeeperAdapter trait
class MyZookeeperActor extends Actor with ActorLoggingAdapter with ZookeeperAdapter {
     import context.dispatcher
     implicit val timeout = Timeout(2000)
     val host = InetAddress.getLocalHost.getCanonicalHostName

     def preStart: Unit = {
          // Lets create a node and give it some data
          createNode(s"/plugin-example/ephnodes/$host", true /* Is this node ephemeral? */, None /* I could give it the data here instead of making the next call */).onComplete {
               case Success(path) =>
                   // The node was created so lets set some data
                    setData(path, "mydata".getBytes)
               case Failure(fail) => // Handle Error
          }

          // We could have also combined the previous example into this call
          // setData(s"/plugin-example/ephnodes/$host",  "mydata".getBytes, true /* Create the node if it does not exist */, true /* Is the node ephemeral ?* /)
          // Lets get some data
          getData(s"/plugin-example/ephnodes/$host").onComplete {
               case Success(data /* Array[Bytes] */) => // Do something with the data
               case Failure(fail) => // Handle the error
          }

          // Lets get some information about child nodes
          getChildren("s"/plugin-example/ephnodes", true /* Do we want the data as well? */).onComplete {
               case Success(childrenWithData /* Seq[(String, Option[Array[Byte]])] */) => // Do something with the children and data
               case Failure(fail) => Handle the error
          }
     }
}

ZooKeeper Events

The ability to receive events for ZooKeeper changes are also available through the trait ZookeeperEventAdapter. If this trait is applied to an actor then the ability to register/unregister for events becomes available.

import com.webtrends.harness.component.zookeeper.ZookeeperEventAdapter

// Define the actor and have it apply the ZookeeperEventAdapter
class MyZookeeperEventActor with Actor with ActorLoggingAdapter with ZookeeperEventAdapter {
     def preStart: Unit = {
         // Register for ZooKeeper State events
          register(self, ZookeeperStateEventRegistration(self))
          // Register for ZooKeeper Child events on the given path
          register(self, ZookeeperChildEventRegistration(self, "/someparentpath"))
          // Register for ZooKeeper leadership events
          register(self, ZookeeperLeaderEventRegistration(self, "/someleadershippath")
     }
     def postStop: Unit = {
          // Unregister for state events
          unregister(self, ZookeeperStateEventRegistration(self))
          // Unregister for child events on the given path
          unregister(self, ZookeeperChildEventRegistration(self, "/someparentpath"))
          // Unregister for leadership events
          unregister(self, ZookeeperLeaderEventRegistration(self, "someleadershippath"))
     }
     def receive = {
          case ZookeeperStateEvent(state) =>
               // A connection state event occured (see com.netflix.curator.framework.state.ConnectionState)
          case ZookeeperChildEvent(event) =>
               // A child event occurred on the registered path (see com.netflix.curator.framework.recipes.cache.PathChildrenCacheEvent)
          case ZookeeperLeadershipEvent(leader /* Am I the leader */) =>
               // A leadership event occured on the registered path
     }
}

Testing Zookeeper Mock

Requires org.apache.curator:curator-test

To use ZK service from a test class:

val zkServer = new TestingServer()
implicit val system = ActorSystem("test")
lazy val zkService = MockZookeeper(zkServer.getConnectString)

Now you should be able to call ZK state changing methods in ZookeeperService.

Running a test Zookeeper Server Locally

If you'd like to run your service locally and not point to an existing ZK server, you can set wookiee-zookeeper to create a TestingServer for you on startup which will serve as a fresh, test zookeeper server on each run.

To enable this, don't set wookiee-zookeeper.quorum but instead set:

wookiee-zookeeper {
  mock-enabled = true
}

wookiee-zookeeper's People

Contributors

malibuworkcrew avatar kraagen avatar ladinu avatar stotten avatar pcross616 avatar mjwallin1 avatar jayras-o avatar ev0ldave avatar blavid avatar splintercat avatar jlleitschuh avatar krishnabvkr avatar roberttowne avatar jayras avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.