Introduction

Amdatu Herding provides locking and leader election services to help you herd a cluster of nodes.

How to use

Leader election

The leader election service will elect a leader for each election group configured. To create such a group create a factory configuration file with factory pid org.amdatu.herding.election.group and a value for the name property.

An ElectionService instance is created for each election group and the node that has been elected as the leader of a group will also have a Leader service registered for that group.

To create a component that should only work on the leader node:

@Component
public class LeaderElectionExample {

    @ServiceDependency(filter = "(" + Leader.GROUP_NAME + "=example-group)") (1)
    private volatile Leader leader;

    @Start
    void start() {
        System.out.println("Elected as leader");
    }

    @Stop
    void stop() {
        System.out.println("Lost leader role");
    }

}
1 The dependency on the Leader service will cause this service to only be available on the node that has been elected as leader for the group

Locking

The LockingService can be used to obtain a distributed lock.

Example:

@Component
public class LockingExample {

    @ServiceDependency
    private volatile LockingService lockingService;

    public void lockingExample() throws InterruptedException {
        Lock lock = lockingService.getLock("example"); (1)

        if (lock.lock(1000)) { (2)
            try {
                // Do work
            } finally {
                lock.release(); (3)
            }
        } else {
            // Failed to lock
        }
    }
}
1 Obtain a lock from the LockingService
2 Acquire the lock
3 Release the lock

Configuration

Single node

pid: org.amdatu.herding.singlenode

Property

Type

Required

Description

Default value

nodeName

String

yes

The name of the node

Zookeeper

pid: org.amdatu.herding.zookeeper

Property

Type

Required

Description

Default value

nodeName

String

yes

Name for this node in the cluster. MUST be an unique name within the cluster

connectString

String

yes

Zookeeper connect string. Comma separated host:port pairs, each corresponding to a zk server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".

namespace

String

false

Zookeeper namespace, used to prefix Zookeeper paths when applications clusters share a Zoookeeper cluster.

Components

Amdatu Herding provides the following components:

Bundle

Description

org.amdatu.herding

Amdatu Herding api

org.amdatu.herding.singlenode

Single node implementation

org.amdatu.herding.zookeeper

Multi node implementation backed by Apache Zookeeper

Resources