forked from mesos/mr-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instance.go
279 lines (218 loc) · 7.01 KB
/
Instance.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package types
import (
"encoding/json"
"fmt"
"log"
"path/filepath"
"strconv"
"github.com/mesos/mr-redis/common/store/etcd"
)
//A Instance structure that will be able to store a tree of data, Everything related to a redis intance
type Instance struct {
Name string //Name of the instance
Type string //Type of the instance "Single Instance = S; Master-Slave = MS; Cluster = C"
Capacity int //Capacity of the Instance in MB
Masters int //Number of masters in this Instance
Slaves int //Number of slaves in this Instance
ExpMasters int //Expected number of Masters
ExpSlaves int //Expected number of Slaves
Status string //Status of this instance "CREATING/RUNNING/DISABLED"
Mname string //Name / task id of the master redis proc
Snames []string //Name of the slave
Procs map[string]*Proc //An array of redis procs to be filled later
}
// NewInstance Creates a new instance variable
// Fills up the structure and updates the central store
// Returns an instance pointer
// Returns nil if the instance already exists
func NewInstance(Name string, Type string, Masters int, Slaves int, Cap int) *Instance {
p := &Instance{Name: Name, Type: Type, ExpMasters: Masters, ExpSlaves: Slaves, Capacity: Cap}
return p
}
// LoadInstance Load an instance from the store using Instance Name from the store
// if the instance is unavailable then return nil
func LoadInstance(Name string) *Instance {
if Gdb.IsSetup() != true {
return nil
}
nodeName := etcd.ETC_INST_DIR + "/" + Name
if ok, _ := Gdb.IsKey(nodeName); !ok {
return nil
}
I := &Instance{Name: Name}
I.Load()
return I
}
// Load Loads up the datastructure for the given Service Name to the struture
// If the Instance cannot be loaded the it returns an error
func (I *Instance) Load() bool {
var err error
var tmpStr string
var SnamesKey []string
if Gdb.IsSetup() != true {
return false
}
nodeName := etcd.ETC_INST_DIR + "/" + I.Name + "/"
I.Type, err = Gdb.Get(nodeName + "Type")
tmpStr, err = Gdb.Get(nodeName + "Capacity")
I.Capacity, err = strconv.Atoi(tmpStr)
tmpStr, err = Gdb.Get(nodeName + "Masters")
I.Masters, err = strconv.Atoi(tmpStr)
tmpStr, err = Gdb.Get(nodeName + "Slaves")
I.Slaves, err = strconv.Atoi(tmpStr)
tmpStr, err = Gdb.Get(nodeName + "ExpMasters")
I.ExpMasters, err = strconv.Atoi(tmpStr)
tmpStr, err = Gdb.Get(nodeName + "ExpSlaves")
I.ExpSlaves, err = strconv.Atoi(tmpStr)
I.Status, err = Gdb.Get(nodeName + "Status")
I.Mname, err = Gdb.Get(nodeName + "Mname")
nodeNameSlaves := nodeName + "Snames/"
SnamesKey, err = Gdb.ListSection(nodeNameSlaves, false)
if err != nil {
log.Printf("The error value is %v", err)
}
for _, snamekey := range SnamesKey {
_, sname := filepath.Split(snamekey)
I.Snames = append(I.Snames, sname)
}
I.LoadProcs()
return true
}
//Sync Writes the entier content of an instance into store, an instance could have many keys to be updated this is a write intensive function should be used carefully, do not call this if you are planning to update only a single attribute of an instance
func (I *Instance) Sync() bool {
if Gdb.IsSetup() != true {
return false
}
nodeName := etcd.ETC_INST_DIR + "/" + I.Name + "/"
Gdb.Set(nodeName+"Type", I.Type)
Gdb.Set(nodeName+"Masters", fmt.Sprintf("%d", I.Masters))
Gdb.Set(nodeName+"Slaves", fmt.Sprintf("%d", I.Slaves))
Gdb.Set(nodeName+"Capacity", fmt.Sprintf("%d", I.Capacity))
Gdb.Set(nodeName+"ExpMasters", fmt.Sprintf("%d", I.ExpMasters))
Gdb.Set(nodeName+"ExpSlaves", fmt.Sprintf("%d", I.ExpSlaves))
Gdb.Set(nodeName+"Status", I.Status)
Gdb.Set(nodeName+"Mname", I.Mname)
//Create Section for Slaves and Procs
nodeNameSlaves := nodeName + "Snames/"
Gdb.CreateSection(nodeNameSlaves)
for _, sname := range I.Snames {
Gdb.Set(nodeNameSlaves+sname, sname)
}
nodeNameProcs := nodeName + "Procs/"
Gdb.CreateSection(nodeNameProcs)
//for _, p := range I.Procs {
//p.Sync()
//}
return true
}
//SyncType Write only the TYPE attribute to the DB/store
func (I *Instance) SyncType(string) bool {
if Gdb.IsSetup() != true {
return false
}
nodeName := etcd.ETC_INST_DIR + "/" + I.Name + "/"
Gdb.Set(nodeName+"Type", I.Type)
return true
}
//SyncStatus Flushes only the status attribute to the DB
func (I *Instance) SyncStatus() bool {
if Gdb.IsSetup() != true {
return false
}
nodeName := etcd.ETC_INST_DIR + "/" + I.Name + "/"
Gdb.Set(nodeName+"Status", I.Status)
return true
}
//SyncSlaves Flushes only the Slaves attribute to the DB, used when a Slave died or promoted as a master
func (I *Instance) SyncSlaves() bool {
if Gdb.IsSetup() != true {
return false
}
nodeName := etcd.ETC_INST_DIR + "/" + I.Name + "/"
Gdb.Set(nodeName+"Slaves", fmt.Sprintf("%d", I.Slaves))
//Create Section for Slaves and Procs
nodeNameSlaves := nodeName + "Snames/"
Gdb.CreateSection(nodeNameSlaves)
for _, sname := range I.Snames {
Gdb.Set(nodeNameSlaves+sname, sname)
}
return true
}
//SyncMasters Flushes only the master attribute to the DB, used when a new redis master is choose.
func (I *Instance) SyncMasters() bool {
if Gdb.IsSetup() != true {
return false
}
nodeName := etcd.ETC_INST_DIR + "/" + I.Name + "/"
Gdb.Set(nodeName+"Masters", fmt.Sprintf("%d", I.Masters))
Gdb.Set(nodeName+"Mname", I.Mname)
return true
}
//LoadProcs Should be called when all the PROCs need to be loaded to the lateest value, PS High DISK intensive function, should be used carefully
func (I *Instance) LoadProcs() bool {
if I.Procs == nil {
I.Procs = make(map[string]*Proc)
}
I.Procs[I.Mname] = LoadProc(I.Name + "::" + I.Mname)
for _, n := range I.Snames {
log.Printf("Laoding proc key=%v ", n)
I.Procs[n] = LoadProc(I.Name + "::" + n)
}
return true
}
//Instance_Json Filtered elementes of an Instnace that will be sent as an HTTP response
type Instance_Json struct {
Name string
Type string
Status string
Capacity int
Master *ProcJson
Slaves []*ProcJson
}
/*
type Proc_Json struct {
IP string
Port string
}
*/
//ToJson_Obj Filtered elementes of an Instnace that will be sent as an HTTP response
func (I *Instance) ToJson_Obj() Instance_Json {
var res Instance_Json
res.Name = I.Name
res.Type = I.Type
res.Capacity = I.Capacity
res.Status = I.Status
if I.Status == INST_STATUS_RUNNING {
var p *Proc
p = I.Procs[I.Mname]
res.Master = p.ToJson()
for _, sname := range I.Snames {
p = I.Procs[sname]
res.Slaves = append(res.Slaves, p.ToJson())
}
}
return res
}
//ToJson Marshall the Instane to a JSON
func (I *Instance) ToJson() string {
var res Instance_Json
res.Name = I.Name
res.Type = I.Type
res.Capacity = I.Capacity
res.Status = I.Status
if I.Status == INST_STATUS_RUNNING {
var p *Proc
p = I.Procs[I.Mname]
res.Master = p.ToJson()
res.Master.Port = p.Port
for _, sname := range I.Snames {
p = I.Procs[sname]
res.Slaves = append(res.Slaves, p.ToJson())
}
}
b, err := json.Marshal(res)
if err != nil {
return "Marshaling error"
}
return string(b)
}