-
Notifications
You must be signed in to change notification settings - Fork 9
/
manifest.go
151 lines (127 loc) · 4.37 KB
/
manifest.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
package manifest
import (
"context"
"fmt"
"strings"
"github.com/samkumar/etcdstruct"
"github.com/ugorji/go/codec"
etcd "github.com/coreos/etcd/clientv3"
)
const manifestpath = "manifest/"
const manifestlockpath = "manifestlocks/"
var etcdprefix = ""
var mp codec.Handle = &codec.MsgpackHandle{}
type ManifestDeviceStream struct {
CanonicalName string `codec:"-" yaml:"-"`
Metadata map[string]string `codec:"metadata,omitempty" yaml:"metadata"`
}
type ManifestDevice struct {
Descriptor string `codec:"-" yaml:"-"`
Metadata map[string]string `codec:"metadata,omitempty" yaml:"metadata"`
Streams map[string]*ManifestDeviceStream `codec:"streams" yaml:"streams"`
retrievedRevision int64
}
func (md *ManifestDevice) SetRetrievedRevision(rev int64) {
md.retrievedRevision = rev
}
func (md *ManifestDevice) GetRetrievedRevision() int64 {
return md.retrievedRevision
}
func SetEtcdKeyPrefix(prefix string) {
etcdprefix = prefix
}
func getEtcdKey(name string) string {
return fmt.Sprintf("%s%s%s", etcdprefix, manifestpath, name)
}
func getEtcdLockKey(name string) string {
return fmt.Sprintf("%s%s%s", etcdprefix, manifestlockpath, name)
}
func getNameFromEtcdKey(etcdKey string) string {
return etcdKey[len(etcdprefix)+len(manifestpath):]
}
func RetrieveManifestDevice(ctx context.Context, etcdClient *etcd.Client, descriptor string) (md *ManifestDevice, err error) {
md = &ManifestDevice{Descriptor: descriptor}
exists, err := etcdstruct.RetrieveEtcdStruct(ctx, etcdClient, getEtcdKey(descriptor), md)
if !exists {
md = nil
} else {
if md.Metadata == nil {
md.Metadata = make(map[string]string)
}
if md.Streams == nil {
md.Streams = make(map[string]*ManifestDeviceStream)
}
}
return
}
func UpsertManifestDevice(ctx context.Context, etcdClient *etcd.Client, md *ManifestDevice) error {
return etcdstruct.UpsertEtcdStruct(ctx, etcdClient, getEtcdKey(md.Descriptor), md)
}
func UpsertManifestDeviceAtomically(ctx context.Context, etcdClient *etcd.Client, md *ManifestDevice) (bool, error) {
return etcdstruct.UpsertEtcdStructAtomic(ctx, etcdClient, getEtcdKey(md.Descriptor), md)
}
func GetLockTable(ctx context.Context, etcdClient *etcd.Client) (map[string][]string, error) {
locktableprefix := fmt.Sprintf("%s%s", etcdprefix, manifestlockpath)
resp, err := etcdClient.Get(ctx, locktableprefix, etcd.WithPrefix())
if err != nil {
return nil, err
}
ltable := make(map[string][]string)
for _, r := range resp.Kvs {
did := strings.TrimPrefix(string(r.Key), locktableprefix)
ltable[string(r.Value)] = append(ltable[string(r.Value)], did)
}
return ltable, nil
}
func ObtainDeviceLock(ctx context.Context, etcdClient *etcd.Client, md *ManifestDevice, myid string) (bool, error) {
lockkey := getEtcdLockKey(md.Descriptor)
lockval := myid
resp, err := etcdClient.Grant(ctx, 5)
if err != nil {
return false, err
}
txr, err := etcdClient.Txn(ctx).
If(etcd.Compare(etcd.CreateRevision(lockkey), "=", 0)).
Then(etcd.OpPut(lockkey, lockval, etcd.WithLease(resp.ID))).
Commit()
if err != nil {
return false, err
}
if !txr.Succeeded {
return false, nil
}
ch, err := etcdClient.KeepAlive(ctx, resp.ID)
if err != nil {
return false, err
}
go func() {
for _ = range ch {
}
panic("ETCD lease ended unexpectedly, restarting")
}()
return true, nil
}
func RetrieveMultipleManifestDevices(ctx context.Context, etcdClient *etcd.Client, descprefix string) ([]*ManifestDevice, error) {
etcdKeyPrefix := getEtcdKey(descprefix)
devs := make([]*ManifestDevice, 0, 1024)
err := etcdstruct.RetrieveEtcdStructs(ctx, etcdClient, func(key []byte) etcdstruct.EtcdStruct {
dev := &ManifestDevice{Descriptor: getNameFromEtcdKey(string(key))}
devs = append(devs, dev)
return dev
}, func(es etcdstruct.EtcdStruct, key []byte) {
dev := es.(*ManifestDevice)
dev.Descriptor = getNameFromEtcdKey(string(key))
dev.Streams = nil
}, etcdKeyPrefix, etcd.WithPrefix())
if err != nil {
return nil, err
}
return devs, err
}
func DeleteManifestDevice(ctx context.Context, etcdClient *etcd.Client, descriptor string) error {
_, err := etcdstruct.DeleteEtcdStructs(ctx, etcdClient, getEtcdKey(descriptor))
return err
}
func DeleteMultipleManifestDevices(ctx context.Context, etcdClient *etcd.Client, descprefix string) (int64, error) {
return etcdstruct.DeleteEtcdStructs(ctx, etcdClient, getEtcdKey(descprefix), etcd.WithPrefix())
}