forked from gluster/glusterd2
/
store.go
102 lines (83 loc) · 2.36 KB
/
store.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
// Package store implements the centralized store for GlusterD
//
// We use etcd as the store backend, and use libkv as the frontend to etcd.
// libkv should allow us to change backends easily if required.
package store
import (
"os"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/libkv"
"github.com/docker/libkv/store"
"github.com/docker/libkv/store/etcd"
)
const (
// GlusterPrefix prefixes all paths in the store
GlusterPrefix string = "gluster/"
)
var (
prefixes []string
)
// GDStore is the GlusterD centralized store
type GDStore struct {
store.Store
}
func init() {
etcd.Register()
}
// New creates a new GDStore
func New() *GDStore {
//TODO: Make this configurable
hostname, _ := os.Hostname()
address := hostname + ":2379"
log.WithFields(log.Fields{"type": "etcd", "etcd.config": address}).Debug("Creating new store")
s, err := libkv.NewStore(store.ETCD, []string{address}, &store.Config{ConnectionTimeout: 10 * time.Second})
if err != nil {
log.WithField("error", err).Fatal("Failed to create store")
}
log.Info("Created new store using ETCD")
gds := &GDStore{s}
if e := gds.InitPrefix(GlusterPrefix); e != nil {
log.Fatal("failed to init store prefixes")
}
return gds
}
// initPrefixes initalizes the store prefixes so that GETs on empty prefixes don't fail
// Returns true on success, false otherwise.
func (s *GDStore) initPrefixes() bool {
log.Debug("initing store prefixes")
for _, p := range prefixes {
if e := s.InitPrefix(p); e != nil {
return false
}
}
return true
}
// InitPrefix initializes the given prefix `p` in the store so that GETs on empty prefixes don't fail
// Returns error on failure, nil on success
func (s *GDStore) InitPrefix(p string) error {
// Create the prefix if the prefix is not found. If any other error occurs
// return it. Don't do anything if prefix is found
if _, e := s.Get(p); e != nil {
switch e {
case store.ErrKeyNotFound:
log.WithField("prefix", p).Debug("prefix not found, initing")
if e := s.Put(p, nil, &store.WriteOptions{IsDir: true}); e != nil {
log.WithFields(log.Fields{
"preifx": p,
"error": e,
}).Error("error initing prefix")
return e
}
default:
log.WithFields(log.Fields{
"prefix": p,
"error": e,
}).Error("error getting prefix")
return e
}
} else {
log.WithField("prefix", p).Debug("prefix present")
}
return nil
}