Skip to content

Commit

Permalink
ZooKeeper Lock Working
Browse files Browse the repository at this point in the history
  • Loading branch information
malvads committed May 28, 2024
1 parent bd30279 commit fffe3ca
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions dev/data/myid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Binary file added dev/data/version-2/log.1
Binary file not shown.
Binary file added dev/data/version-2/snapshot.0
Binary file not shown.
13 changes: 13 additions & 0 deletions dev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: '3.8'

services:
zookeeper:
image: zookeeper:latest
container_name: zookeeper
ports:
- "2181:2181"
volumes:
- ./zookeeper.cfg:/conf/zookeeper.cfg
- ./data:/data
command: ["sh", "-c", "chmod -R 777 /data /datalog && exec zkServer.sh start-foreground /conf/zookeeper.cfg"]
restart: always
13 changes: 13 additions & 0 deletions dev/zoo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

path=$(dirname $0)

cd $path

sudo docker-compose up -d

echo "ZooKeeper is running. Press Ctrl+C to stop."

while true; do
sleep 1
done
6 changes: 6 additions & 0 deletions dev/zookeeper.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tickTime=2000
dataDir=/data
clientPort=2181
initLimit=5
syncLimit=2
server.1=0.0.0.0:2888:3888
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/redBorder/rbforwarder v0.0.0-20240314111631-41c7550172fa
github.com/redBorder/sarama-cluster v2.1.1+incompatible
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/redBorder/rbforwarder v0.0.0-20240314111631-41c7550172fa h1:oQlOQ8HBp
github.com/redBorder/rbforwarder v0.0.0-20240314111631-41c7550172fa/go.mod h1:PNYzPdp+hIROmuLx8paLd8ikxNymLyTCI+wzWFWGZq0=
github.com/redBorder/sarama-cluster v2.1.1+incompatible h1:fF+IACoKStThXc7YvKc4aPlPNcXQ0++cQqbgjbwJBgk=
github.com/redBorder/sarama-cluster v2.1.1+incompatible/go.mod h1:GkBF54G9l95UU8kaYBngZ1LnSkLdHJDJzQ1qixc9BLI=
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414 h1:AJNDS0kP60X8wwWFvbLPwDuojxubj9pbfK7pjHw0vKg=
github.com/samuel/go-zookeeper v0.0.0-20201211165307-7117e9ea2414/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/streamrail/concurrent-map v0.0.0-20160823150647-8bf1e9bacbf6 h1:XklXvOrWxWCDX2n4vdEQWkjuIP820XD6C4kF0O0FzH4=
Expand Down
66 changes: 66 additions & 0 deletions lib/zookeeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package lib

import (
"log"
"strconv"
"time"

"github.com/samuel/go-zookeeper/zk"
)

const (
lockPath = "/rb-arubacentral/lock"
defPath = "/rb-arubacentral"
)

type ZookeeperClient struct {
Conn *zk.Conn
}

func NewZookeeperClient(servers []string) (*ZookeeperClient, error) {
conn, _, err := zk.Connect(servers, time.Second)
if err != nil {
return nil, err
}
_, err = conn.Create(defPath, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil && err != zk.ErrNodeExists {
conn.Close()
return nil, err
}
return &ZookeeperClient{Conn: conn}, nil
}

func (z *ZookeeperClient) Get(path string) ([]byte, error) {
return z.Conn.Get(path)
}

func (z *ZookeeperClient) Delete(path string) error {
return z.Conn.Delete(path, -1)
}

func (z *ZookeeperClient) Lock() {
sessionID := z.Conn.SessionID()
sessionIDBytes := []byte(strconv.FormatInt(int64(sessionID), 10))
_, err := z.Conn.Create(lockPath, sessionIDBytes, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
if err != nil {
log.Fatalf("Failed to create lock: %v", err)
}
}

func (z *ZookeeperClient) ReleaseLock() {
if err := z.Delete(lockPath); err != nil {
log.Fatalf("Failed to release lock: %v", err)
}
}

func (z *ZookeeperClient) IsLocked() bool {
data, err := z.Get(lockPath)
if err != nil {
return false
}
return len(data) > 0
}

func (z *ZookeeperClient) Close() {
z.Conn.Close()
}
29 changes: 29 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@ package main

import (
"fmt"
"log"
"time"

lib "redborder.com/rb-arubacentral/lib"

Check failure on line 8 in main.go

View workflow job for this annotation

GitHub Actions / build

could not import redborder.com/rb-arubacentral/lib (-: redborder.com/rb-arubacentral/lib
arubacentral "redborder.com/rb-arubacentral/rest"
)

func connectToZooKeeper(GoAruba *arubacentral.ArubaClient) {
servers := []string{"127.0.0.1:2181"}

client, err := lib.NewZookeeperClient(servers)
if err != nil {
log.Fatalf("Failed to connect to Zookeeper: %v", err)
}
defer client.Close()

for {
isLocked := client.IsLocked()
if !isLocked {
log.Println("Locking...")
client.Lock()
time.Sleep(5 * time.Second)
client.ReleaseLock()
} else {
log.Println("A lock is already in place. Waiting...")
time.Sleep(5 * time.Second)
}
}
}

func main() {
GoAruba := arubacentral.NewArubaClient(
"",
Expand All @@ -15,6 +41,9 @@ func main() {
"",
"",
)

connectToZooKeeper(GoAruba)

resp, err := GoAruba.Get("/visualrf_api/v1/campus")

if err != nil {
Expand Down

0 comments on commit fffe3ca

Please sign in to comment.