-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZKServiceMonitor.hpp
104 lines (96 loc) · 3.53 KB
/
ZKServiceMonitor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef ZK_SERVICE_MONITOR_H_BY_SATANSON
#define ZK_SERVICE_MONITOR_H_BY_SATANSON
#include<ZooKeeper.hpp>
#include<string>
#include<vector>
#include<set>
#include<stdexcept>
#include<iostream>
using namespace std;
using namespace org::apache::zookeeper;
class ServiceMonitor{
public:
class SpinLock{
pthread_spinlock_t m_mutex;
public:
class Sync{
SpinLock* m_lock;
public:
Sync(SpinLock* lock):m_lock(lock){pthread_spin_lock(m_lock->get());}
~Sync(){pthread_spin_unlock(m_lock->get());}
};
SpinLock(){pthread_spin_init(&this->m_mutex,PTHREAD_PROCESS_PRIVATE);}
~SpinLock(){pthread_spin_destroy(&this->m_mutex);}
pthread_spinlock_t* get(){return &this->m_mutex;}
};
class ZKSM_Watcher:public Watcher{
private:
ServiceMonitor *zksm;
string baseNode;
public:
ZKSM_Watcher(string const& znode,ServiceMonitor* m):baseNode(znode),zksm(m){}
void process(WatchedEvent event){
LOGi("Events are processed by ZooKeeper Service Monitor Watcher");
if (event.keeperState !=KeeperState::Connected){
LOGe("Lost connection to ZooKeeper");
THROW("Lost Connection to ZooKeeper");
}
if (event.path==baseNode &&
event.eventType == EventType::NodeChildrenChanged){
LOGi("Child znodes of %s are changed",baseNode.c_str());
zksm->registered();
}
}
};
string pop(){
SpinLock::Sync sync(&lock);
if (!services.size()){
return string();
}
else {
string serv=*services.begin();
services.erase(serv);
return serv;
}
}
void push(string const& service){
SpinLock::Sync sync(&lock);
services.insert(service);
}
ServiceMonitor(string const& conString,int timeout,string const& baseNode){
this->baseNode=baseNode;
try{
watcher.reset(new ZKSM_Watcher(baseNode,this));
zk.reset(new ZooKeeper(conString,timeout,watcher));
}catch(string& err){
LOGe("Failed to initialize ZooKeeper.Error is caused by\n%s",err.c_str());
THROW("Failed to instantiate ServiceMonitor");
}catch(...){
THROW("Unknown error happened");
}
LOGi("Succeeded in instantiating ServiceMonitor");
}
void registered(){
if(!zk->zk_exists(baseNode,false)){
LOGe("Base znode %s not exists yet",baseNode.c_str());
THROW("Base znode %s not exists yet",baseNode.c_str());
}
LOGi("Base znode %s already exists",baseNode.c_str());
list<string> slist=zk->zk_getChildren(baseNode,true);
while(!slist.empty()){
string s=slist.front();
LOGi("Service:%s is available",s.c_str());
push(s);
slist.pop_front();
}
}
~ServiceMonitor(){
}
private:
shared_ptr<ZooKeeper> zk;
shared_ptr<Watcher> watcher;
string baseNode;
set<string> services;
SpinLock lock;
};
#endif