This repository has been archived by the owner on Nov 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathobjdb.go
140 lines (112 loc) · 3.69 KB
/
objdb.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package objdb
import (
"sync"
log "github.com/Sirupsen/logrus"
)
// Lock event types
const (
LockAcquired = iota // Successfully acquired
LockReleased // explicitly released
LockAcquireTimeout // Timeout trying to acquire lock
LockAcquireError // Error while acquiring
LockRefreshError // Error during ttl refresh
LockLost // We lost the lock
)
// LockEvent Lock Event notifications
type LockEvent struct {
EventType uint // Type of event
}
// LockInterface Lock interface
type LockInterface interface {
// Acquire a lock.
// Give up acquiring lock after timeout seconds. if timeout is 0, wait forever
Acquire(timeout uint64) error
// Release the lock. This explicitly releases the lock and cleans up all state
// If we were still waiting to acquire lock, it stops it and cleans up
Release() error
// For debug purposes only.
// Just stops refreshing the lock
Kill() error
// Get the event channel
EventChan() <-chan LockEvent
// Is the lock acquired
IsAcquired() bool
// Get current holder of the lock
GetHolder() string
}
// ServiceInfo hash Information about a service
// Notes:
// There could be multiple instances of a service. hostname:port uniquely
// identify an instance of a service
type ServiceInfo struct {
ServiceName string // Name of the service
Role string // Role of the service. (leader, follower etc)
Version string // Version string for the service
TTL int // TTL for this service
HostAddr string // Host name or IP address where its running
Port int // Port number where its listening
Hostname string // Host name where its running
}
// Watch events
const (
WatchServiceEventAdd = iota // New Service endpoint added
WatchServiceEventDel // A service endpoint was deleted
WatchServiceEventError // Error occurred while watching for service
)
// WatchServiceEvent : watch event on services
type WatchServiceEvent struct {
EventType uint // event type
ServiceInfo ServiceInfo // Information about the service
}
// Plugin interface
type Plugin interface {
// Initialize the plugin, only called once
NewClient(endpoints []string) (API, error)
}
// API Plugin API
type API interface {
// Get a Key from conf store
GetObj(key string, retValue interface{}) error
// Set a key in conf store
SetObj(key string, value interface{}) error
// Remove an object
DelObj(key string) error
// List all objects in a directory
ListDir(key string) ([]string, error)
// Create a new lock
NewLock(name string, holderID string, ttl uint64) (LockInterface, error)
// Register a service
// Service is registered with a ttl for 60sec and a goroutine is created
// to refresh the ttl.
RegisterService(serviceInfo ServiceInfo) error
// List all end points for a service
GetService(name string) ([]ServiceInfo, error)
// Watch for addition/deletion of service end points
WatchService(name string, eventCh chan WatchServiceEvent, stopCh chan bool) error
// Deregister a service
// This removes the service from the registry and stops the refresh groutine
DeregisterService(serviceInfo ServiceInfo) error
}
var (
// List of plugins available
pluginList = make(map[string]Plugin)
pluginMutex = new(sync.Mutex)
)
// RegisterPlugin Register a plugin
func RegisterPlugin(name string, plugin Plugin) error {
pluginMutex.Lock()
defer pluginMutex.Unlock()
pluginList[name] = plugin
return nil
}
// GetPlugin returns the plugin by name
func GetPlugin(name string) Plugin {
// Find the conf store
pluginMutex.Lock()
defer pluginMutex.Unlock()
if pluginList[name] == nil {
log.Errorf("Objdb Plugin %s not registered", name)
return nil
}
return pluginList[name]
}