This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from heketi/heketi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_entry.go
368 lines (297 loc) · 7.72 KB
/
node_entry.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//
// Copyright (c) 2015 The heketi Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package glusterfs
import (
"bytes"
"encoding/gob"
"fmt"
"sort"
"github.com/boltdb/bolt"
"github.com/heketi/heketi/pkg/glusterfs/api"
"github.com/heketi/heketi/pkg/utils"
"github.com/lpabon/godbc"
)
type NodeEntry struct {
Entry
Info api.NodeInfo
Devices sort.StringSlice
}
func NewNodeEntry() *NodeEntry {
entry := &NodeEntry{}
entry.Devices = make(sort.StringSlice, 0)
entry.SetOnline()
return entry
}
func NewNodeEntryFromRequest(req *api.NodeAddRequest) *NodeEntry {
godbc.Require(req != nil)
node := NewNodeEntry()
node.Info.Id = utils.GenUUID()
node.Info.ClusterId = req.ClusterId
node.Info.Hostnames = req.Hostnames
node.Info.Zone = req.Zone
return node
}
func NewNodeEntryFromId(tx *bolt.Tx, id string) (*NodeEntry, error) {
godbc.Require(tx != nil)
entry := NewNodeEntry()
err := EntryLoad(tx, entry, id)
if err != nil {
return nil, err
}
return entry, nil
}
func (n *NodeEntry) registerManageKey(host string) string {
return "MANAGE" + host
}
func (n *NodeEntry) registerStorageKey(host string) string {
return "STORAGE" + host
}
func (n *NodeEntry) Register(tx *bolt.Tx) error {
// Save manage hostnames
for _, h := range n.Info.Hostnames.Manage {
val, err := EntryRegister(tx, n, n.registerManageKey(h), []byte(n.Info.Id))
if err == ErrKeyExists {
// Now check if the node actually exists. This only happens
// when the application crashes and it doesn't clean up stale
// registrations.
conflictId := string(val)
_, err := NewNodeEntryFromId(tx, conflictId)
if err == ErrNotFound {
// (stale) There is actually no conflict, we can allow
// the registration
return nil
} else if err != nil {
return logger.Err(err)
}
// Return that we found a conflict
return fmt.Errorf("Hostname %v already used by node with id %v\n",
h, conflictId)
} else if err != nil {
return err
}
}
// Save storage hostnames
for _, h := range n.Info.Hostnames.Storage {
val, err := EntryRegister(tx, n, n.registerStorageKey(h), []byte(n.Info.Id))
if err == ErrKeyExists {
// Check if it exists
conflictId := string(val)
_, err := NewNodeEntryFromId(tx, conflictId)
if err == ErrNotFound {
// (stale) There is actually no conflict, we can allow
// the registration
return nil
} else if err != nil {
return logger.Err(err)
}
// Return that we found a conflict
return fmt.Errorf("Hostname %v already used by node with id %v\n",
h, conflictId)
} else if err != nil {
return err
}
}
return nil
}
func (n *NodeEntry) Deregister(tx *bolt.Tx) error {
// Remove manage hostnames from Db
for _, h := range n.Info.Hostnames.Manage {
err := EntryDelete(tx, n, n.registerManageKey(h))
if err != nil {
return err
}
}
// Remove storage hostnames
for _, h := range n.Info.Hostnames.Storage {
err := EntryDelete(tx, n, n.registerStorageKey(h))
if err != nil {
return err
}
}
return nil
}
func (n *NodeEntry) BucketName() string {
return BOLTDB_BUCKET_NODE
}
func (n *NodeEntry) Save(tx *bolt.Tx) error {
godbc.Require(tx != nil)
godbc.Require(len(n.Info.Id) > 0)
return EntrySave(tx, n, n.Info.Id)
}
func (n *NodeEntry) ManageHostName() string {
godbc.Require(n.Info.Hostnames.Manage != nil)
godbc.Require(len(n.Info.Hostnames.Manage) > 0)
return n.Info.Hostnames.Manage[0]
}
func (n *NodeEntry) StorageHostName() string {
godbc.Require(n.Info.Hostnames.Storage != nil)
godbc.Require(len(n.Info.Hostnames.Storage) > 0)
return n.Info.Hostnames.Storage[0]
}
func (n *NodeEntry) IsDeleteOk() bool {
// Check if the nodes still has drives
if len(n.Devices) > 0 {
return false
}
return true
}
func (n *NodeEntry) ConflictString() string {
return fmt.Sprintf("Unable to delete node [%v] because it contains devices", n.Info.Id)
}
func (n *NodeEntry) Delete(tx *bolt.Tx) error {
godbc.Require(tx != nil)
// Check if the nodes still has drives
if !n.IsDeleteOk() {
logger.Warning(n.ConflictString())
return ErrConflict
}
return EntryDelete(tx, n, n.Info.Id)
}
func (n *NodeEntry) removeAllDisksFromRing(tx *bolt.Tx,
a Allocator) error {
cluster, err := NewClusterEntryFromId(tx, n.Info.ClusterId)
if err != nil {
return err
}
for _, deviceId := range n.Devices {
device, err := NewDeviceEntryFromId(tx, deviceId)
if err != nil {
return err
}
// Remove device
err = a.RemoveDevice(cluster, n, device)
if err != nil {
return err
}
}
return nil
}
func (n *NodeEntry) addAllDisksToRing(tx *bolt.Tx,
a Allocator) error {
cluster, err := NewClusterEntryFromId(tx, n.Info.ClusterId)
if err != nil {
return err
}
// Add all devices
for _, deviceId := range n.Devices {
device, err := NewDeviceEntryFromId(tx, deviceId)
if err != nil {
return err
}
// Add device
if device.isOnline() {
err = a.AddDevice(cluster, n, device)
if err != nil {
return err
}
}
}
return nil
}
func (n *NodeEntry) SetState(tx *bolt.Tx,
a Allocator,
s api.EntryState) error {
// Check current state
switch n.State {
case api.EntryStateFailed:
if s == api.EntryStateFailed {
return nil
}
return fmt.Errorf("Cannot reuse a failed node")
case api.EntryStateOnline:
switch s {
case api.EntryStateOnline:
return nil
case api.EntryStateFailed:
case api.EntryStateOffline:
default:
return fmt.Errorf("Unknown state type: %v", s)
}
// Remove all disks from Ring
err := n.removeAllDisksFromRing(tx, a)
if err != nil {
return err
}
// Save state
n.State = s
case api.EntryStateOffline:
switch s {
case api.EntryStateOffline:
return nil
case api.EntryStateOnline:
// Add disks back
err := n.addAllDisksToRing(tx, a)
if err != nil {
return err
}
case api.EntryStateFailed:
// Only thing to do here is to set the state
default:
return fmt.Errorf("Unknown state type: %v", s)
}
n.State = s
}
return nil
}
func (n *NodeEntry) NewInfoReponse(tx *bolt.Tx) (*api.NodeInfoResponse, error) {
godbc.Require(tx != nil)
info := &api.NodeInfoResponse{}
info.ClusterId = n.Info.ClusterId
info.Hostnames = n.Info.Hostnames
info.Id = n.Info.Id
info.Zone = n.Info.Zone
info.State = n.State
info.DevicesInfo = make([]api.DeviceInfoResponse, 0)
// Add each drive information
for _, deviceid := range n.Devices {
device, err := NewDeviceEntryFromId(tx, deviceid)
if err != nil {
return nil, err
}
driveinfo, err := device.NewInfoResponse(tx)
if err != nil {
return nil, err
}
info.DevicesInfo = append(info.DevicesInfo, *driveinfo)
}
return info, nil
}
func (n *NodeEntry) Marshal() ([]byte, error) {
var buffer bytes.Buffer
enc := gob.NewEncoder(&buffer)
err := enc.Encode(*n)
return buffer.Bytes(), err
}
func (n *NodeEntry) Unmarshal(buffer []byte) error {
dec := gob.NewDecoder(bytes.NewReader(buffer))
err := dec.Decode(n)
if err != nil {
return err
}
// Make sure to setup arrays if nil
if n.Devices == nil {
n.Devices = make(sort.StringSlice, 0)
}
return nil
}
func (n *NodeEntry) DeviceAdd(id string) {
godbc.Require(!utils.SortedStringHas(n.Devices, id))
n.Devices = append(n.Devices, id)
n.Devices.Sort()
}
func (n *NodeEntry) DeviceDelete(id string) {
n.Devices = utils.SortedStringsDelete(n.Devices, id)
}