forked from singnet/snet-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcddb_client.go
320 lines (248 loc) · 7.77 KB
/
etcddb_client.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
package etcddb
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/singnet/snet-daemon/blockchain"
"io/ioutil"
"strings"
"time"
"github.com/singnet/snet-daemon/config"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/clientv3/concurrency"
)
// EtcdClientMutex mutex struct for etcd client
type EtcdClientMutex struct {
mutex *concurrency.Mutex
}
// Lock lock etcd key
func (mutex *EtcdClientMutex) Lock(ctx context.Context) (err error) {
return mutex.mutex.Lock(ctx)
}
// Unlock unlock etcd key
func (mutex *EtcdClientMutex) Unlock(ctx context.Context) (err error) {
return mutex.mutex.Unlock(ctx)
}
// EtcdClient struct has some useful methods to wolrk with etcd client
type EtcdClient struct {
timeout time.Duration
session *concurrency.Session
etcdv3 *clientv3.Client
}
// NewEtcdClient create new etcd storage client.
func NewEtcdClient(metaData *blockchain.OrganizationMetaData) (client *EtcdClient, err error) {
return NewEtcdClientFromVip(config.Vip(),metaData)
}
// NewEtcdClientFromVip create new etcd storage client from viper.
func NewEtcdClientFromVip(vip *viper.Viper,metaData *blockchain.OrganizationMetaData) (client *EtcdClient, err error) {
conf, err := GetEtcdClientConf(vip,metaData)
if err != nil {
return nil,err
}
log.WithField("PaymentChannelStorageClient", fmt.Sprintf("%+v", conf)).Info()
var etcdv3 *clientv3.Client
if err != nil {
return nil,err
}
if checkIfHttps(metaData.GetPaymentStorageEndPoints()) {
if tlsConfig,err := getTlsConfig();err == nil {
etcdv3, err = clientv3.New(clientv3.Config{
Endpoints: metaData.GetPaymentStorageEndPoints(),
DialTimeout: conf.ConnectionTimeout,
TLS: tlsConfig,
})
}else {
return nil,err
}
}else {
//Regular http call
etcdv3, err = clientv3.New(clientv3.Config{
Endpoints: metaData.GetPaymentStorageEndPoints(),
DialTimeout: conf.ConnectionTimeout,
})
if err != nil {
return nil,err
}
}
session, err := concurrency.NewSession(etcdv3)
if err != nil {
return
}
client = &EtcdClient{
timeout: conf.RequestTimeout,
session: session,
etcdv3: etcdv3,
}
return
}
func getTlsConfig() (*tls.Config, error) {
log.Debug("enabling SSL support via X509 keypair")
cert, err := tls.LoadX509KeyPair(config.GetString(config.PaymentChannelCertPath), config.GetString(config.PaymentChannelKeyPath))
if err != nil {
panic("unable to load specific SSL X509 keypair for etcd")
}
caCert, err := ioutil.ReadFile(config.GetString(config.PaymentChannelCaPath))
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
return tlsConfig, nil
}
func checkIfHttps(endpoints []string ) bool {
for _,endpoint:= range endpoints {
if strings.Contains(endpoint,"https") {
return true
}
}
return false
}
// Get gets value from etcd by key
func (client *EtcdClient) Get(key string) (value string, ok bool, err error) {
log := log.WithField("func", "Get").WithField("key", key).WithField("client", client)
ctx, cancel := context.WithTimeout(context.Background(), client.timeout)
defer cancel()
response, err := client.etcdv3.Get(ctx, key)
if err != nil {
log.WithError(err).Error("Unable to get value by key")
return
}
for _, kv := range response.Kvs {
ok = true
value = string(kv.Value)
return
}
return
}
// GetByKeyPrefix gets all values which have the same key prefix
func (client *EtcdClient) GetByKeyPrefix(key string) (values []string, err error) {
log := log.WithField("func", "GetByKeyPrefix").WithField("key", key).WithField("client", client)
ctx, cancel := context.WithTimeout(context.Background(), client.timeout)
defer cancel()
keyEnd := clientv3.GetPrefixRangeEnd(key)
response, err := client.etcdv3.Get(ctx, key, clientv3.WithRange(keyEnd))
if err != nil {
log.WithError(err).Error("Unable to get value by key prefix")
return
}
for _, kv := range response.Kvs {
value := string(kv.Value)
values = append(values, value)
}
return
}
// Put puts key and value to etcd
func (client *EtcdClient) Put(key string, value string) (err error) {
log := log.WithField("func", "Put").WithField("key", key).WithField("client", client)
etcdv3 := client.etcdv3
ctx, cancel := context.WithTimeout(context.Background(), client.timeout)
defer cancel()
_, err = etcdv3.Put(ctx, key, value)
if err != nil {
log.WithError(err).Error("Unable to put value by key")
}
return err
}
// Delete deletes the existing key and value from etcd
func (client *EtcdClient) Delete(key string) error {
log := log.WithField("func", "Delete").WithField("key", key).WithField("client", client)
etcdv3 := client.etcdv3
ctx, cancel := context.WithTimeout(context.Background(), client.timeout)
defer cancel()
_, err := etcdv3.Delete(ctx, key)
if err != nil {
log.WithError(err).Error("Unable to delete value by key")
}
return err
}
// EtcdKeyValue contains key and value
type EtcdKeyValue struct {
key string
value string
}
// CompareAndSwap uses CAS operation to set a value
func (client *EtcdClient) CompareAndSwap(key string, prevValue string, newValue string) (ok bool, err error) {
return client.Transaction(
[]EtcdKeyValue{EtcdKeyValue{key: key, value: prevValue}},
[]EtcdKeyValue{EtcdKeyValue{key: key, value: newValue}},
)
}
// Transaction uses CAS operation to compare and set multiple key values
func (client *EtcdClient) Transaction(compare []EtcdKeyValue, swap []EtcdKeyValue) (ok bool, err error) {
log := log.WithField("func", "CompareAndSwap").WithField("client", client)
etcdv3 := client.etcdv3
ctx, cancel := context.WithTimeout(context.Background(), client.timeout)
defer cancel()
cmps := make([]clientv3.Cmp, len(compare))
for index, cmp := range compare {
cmps[index] = clientv3.Compare(clientv3.Value(cmp.key), "=", cmp.value)
}
ops := make([]clientv3.Op, len(swap))
for index, op := range swap {
ops[index] = clientv3.OpPut(op.key, op.value)
}
response, err := etcdv3.KV.Txn(ctx).If(cmps...).Then(ops...).Commit()
if err != nil {
keys := []string{}
for _, keyValue := range compare {
keys = append(keys, keyValue.key)
}
log = log.WithField("keys", strings.Join(keys, ", "))
log.WithError(err).Error("Unable to compare and swap value by keys")
return false, err
}
return response.Succeeded, nil
}
// PutIfAbsent puts value if absent
func (client *EtcdClient) PutIfAbsent(key string, value string) (ok bool, err error) {
log := log.WithField("func", "PutIfAbsent").WithField("key", key).WithField("client", client)
ctx, cancel := context.WithTimeout(context.Background(), client.timeout)
defer cancel()
etcdv3 := client.etcdv3
session, err := concurrency.NewSession(etcdv3)
if err != nil {
log.WithError(err).Error("Unable to create new session")
return
}
// TODO: implement it using etcdv3.KV.Txn(ctx).If(...).Then(...).Commit() as in CompareAndSwap()
mu := concurrency.NewMutex(session, key)
err = mu.Lock(ctx)
if err != nil {
log.WithError(err).Error("Unable to lock mutex")
return
}
defer mu.Unlock(context.Background())
response, err := etcdv3.Get(ctx, key)
if err != nil {
log.WithError(err).Error("Unable to get value")
return
}
if response.Count != 0 {
return
}
_, err = etcdv3.Put(ctx, key, value)
if err != nil {
log.WithError(err).Error("Unable to put value")
return
}
ok = true
return
}
// NewMutex Create a mutex for the given key
func (client *EtcdClient) NewMutex(key string) (mutex *EtcdClientMutex, err error) {
m := concurrency.NewMutex(client.session, key)
mutex = &EtcdClientMutex{mutex: m}
return
}
// Close closes etcd client
func (client *EtcdClient) Close() {
defer client.session.Close()
defer client.etcdv3.Close()
}