/
consul.go
172 lines (150 loc) · 4.96 KB
/
consul.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
/*
Copyright 2017 Shlomi Noach, GitHub Inc.
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 kv
import (
"crypto/tls"
"fmt"
"net/http"
"sync"
"sync/atomic"
"github.com/openark/orchestrator/go/config"
consulapi "github.com/armon/consul-api"
"github.com/patrickmn/go-cache"
"github.com/openark/golib/log"
)
// getConsulKVCacheKey returns a Consul KV cache key for a given datacenter
func getConsulKVCacheKey(dc, key string) string {
return fmt.Sprintf("%s;%s", dc, key)
}
// A Consul store based on config's `ConsulAddress`, `ConsulScheme`, and `ConsulKVPrefix`
type consulStore struct {
client *consulapi.Client
kvCache *cache.Cache
pairsDistributionSuccessMutex sync.Mutex
distributionReentry int64
}
// NewConsulStore creates a new consul store. It is possible that the client for this store is nil,
// which is the case if no consul config is provided.
func NewConsulStore() KVStore {
store := &consulStore{
kvCache: cache.New(cache.NoExpiration, cache.DefaultExpiration),
}
if config.Config.ConsulAddress != "" {
consulConfig := consulapi.DefaultConfig()
consulConfig.Address = config.Config.ConsulAddress
consulConfig.Scheme = config.Config.ConsulScheme
if config.Config.ConsulScheme == "https" {
consulConfig.HttpClient = &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
}
}
// ConsulAclToken defaults to ""
consulConfig.Token = config.Config.ConsulAclToken
if client, err := consulapi.NewClient(consulConfig); err != nil {
log.Errore(err)
} else {
store.client = client
}
}
return store
}
func (this *consulStore) PutKeyValue(key string, value string) (err error) {
if this.client == nil {
return nil
}
pair := &consulapi.KVPair{Key: key, Value: []byte(value)}
_, err = this.client.KV().Put(pair, nil)
return err
}
func (this *consulStore) GetKeyValue(key string) (value string, found bool, err error) {
if this.client == nil {
return value, found, nil
}
pair, _, err := this.client.KV().Get(key, nil)
if err != nil {
return value, found, err
}
return string(pair.Value), (pair != nil), nil
}
func (this *consulStore) PutKVPairs(kvPairs []*KVPair) (err error) {
if this.client == nil {
return nil
}
for _, pair := range kvPairs {
if err := this.PutKeyValue(pair.Key, pair.Value); err != nil {
return err
}
}
return nil
}
func (this *consulStore) DistributePairs(kvPairs [](*KVPair)) (err error) {
// This function is non re-entrant (it can only be running once at any point in time)
if atomic.CompareAndSwapInt64(&this.distributionReentry, 0, 1) {
defer atomic.StoreInt64(&this.distributionReentry, 0)
} else {
return
}
if !config.Config.ConsulCrossDataCenterDistribution {
return nil
}
datacenters, err := this.client.Catalog().Datacenters()
if err != nil {
return err
}
log.Debugf("consulStore.DistributePairs(): distributing %d pairs to %d datacenters", len(kvPairs), len(datacenters))
consulPairs := [](*consulapi.KVPair){}
for _, kvPair := range kvPairs {
consulPairs = append(consulPairs, &consulapi.KVPair{Key: kvPair.Key, Value: []byte(kvPair.Value)})
}
var wg sync.WaitGroup
for _, datacenter := range datacenters {
datacenter := datacenter
wg.Add(1)
go func() {
defer wg.Done()
writeOptions := &consulapi.WriteOptions{Datacenter: datacenter}
queryOptions := &consulapi.QueryOptions{Datacenter: datacenter}
skipped := 0
existing := 0
written := 0
failed := 0
for _, consulPair := range consulPairs {
val := string(consulPair.Value)
kcCacheKey := getConsulKVCacheKey(datacenter, consulPair.Key)
if value, found := this.kvCache.Get(kcCacheKey); found && val == value {
skipped++
continue
}
if pair, _, err := this.client.KV().Get(consulPair.Key, queryOptions); err == nil && pair != nil {
if val == string(pair.Value) {
existing++
this.kvCache.SetDefault(kcCacheKey, val)
continue
}
}
if _, e := this.client.KV().Put(consulPair, writeOptions); e != nil {
log.Errorf("consulStore.DistributePairs(): failed %s", kcCacheKey)
failed++
err = e
} else {
log.Debugf("consulStore.DistributePairs(): written %s=%s", kcCacheKey, val)
written++
this.kvCache.SetDefault(kcCacheKey, val)
}
}
log.Debugf("consulStore.DistributePairs(): datacenter: %s; skipped: %d, existing: %d, written: %d, failed: %d", datacenter, skipped, existing, written, failed)
}()
}
wg.Wait()
return err
}