Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package kvstore |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
| 21 | "fmt" |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 22 | "os" |
| 23 | "strconv" |
| 24 | "sync" |
| 25 | "time" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 26 | |
| 27 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 28 | v3Client "go.etcd.io/etcd/clientv3" |
| 29 | v3rpcTypes "go.etcd.io/etcd/etcdserver/api/v3rpc/rpctypes" |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | const ( |
| 33 | poolCapacityEnvName = "VOLTHA_ETCD_CLIENT_POOL_CAPACITY" |
| 34 | maxUsageEnvName = "VOLTHA_ETCD_CLIENT_MAX_USAGE" |
| 35 | ) |
| 36 | |
| 37 | const ( |
| 38 | defaultMaxPoolCapacity = 1000 // Default size of an Etcd Client pool |
| 39 | defaultMaxPoolUsage = 100 // Maximum concurrent request an Etcd Client is allowed to process |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | // EtcdClient represents the Etcd KV store client |
| 43 | type EtcdClient struct { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 44 | pool EtcdClientAllocator |
| 45 | watchedChannels sync.Map |
| 46 | watchedClients map[string]*v3Client.Client |
| 47 | watchedClientsLock sync.RWMutex |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 48 | } |
| 49 | |
David K. Bainbridge | bc38107 | 2021-03-19 20:56:04 +0000 | [diff] [blame] | 50 | // NewEtcdCustomClient returns a new client for the Etcd KV store allowing |
| 51 | // the called to specify etcd client configuration |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 52 | func NewEtcdCustomClient(ctx context.Context, addr string, timeout time.Duration, level log.LogLevel) (*EtcdClient, error) { |
| 53 | // Get the capacity and max usage from the environment |
| 54 | capacity := defaultMaxPoolCapacity |
| 55 | maxUsage := defaultMaxPoolUsage |
| 56 | if capacityStr, present := os.LookupEnv(poolCapacityEnvName); present { |
| 57 | if val, err := strconv.Atoi(capacityStr); err == nil { |
| 58 | capacity = val |
| 59 | logger.Infow(ctx, "env-variable-set", log.Fields{"pool-capacity": capacity}) |
| 60 | } else { |
| 61 | logger.Warnw(ctx, "invalid-capacity-value", log.Fields{"error": err, "capacity": capacityStr}) |
| 62 | } |
| 63 | } |
| 64 | if maxUsageStr, present := os.LookupEnv(maxUsageEnvName); present { |
| 65 | if val, err := strconv.Atoi(maxUsageStr); err == nil { |
| 66 | maxUsage = val |
| 67 | logger.Infow(ctx, "env-variable-set", log.Fields{"max-usage": maxUsage}) |
| 68 | } else { |
| 69 | logger.Warnw(ctx, "invalid-max-usage-value", log.Fields{"error": err, "max-usage": maxUsageStr}) |
| 70 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 71 | } |
| 72 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 73 | var err error |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 74 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 75 | pool, err := NewRoundRobinEtcdClientAllocator([]string{addr}, timeout, capacity, maxUsage, level) |
| 76 | if err != nil { |
| 77 | logger.Errorw(ctx, "failed-to-create-rr-client", log.Fields{ |
| 78 | "error": err, |
| 79 | }) |
| 80 | } |
| 81 | |
| 82 | logger.Infow(ctx, "etcd-pool-created", log.Fields{"capacity": capacity, "max-usage": maxUsage}) |
| 83 | |
| 84 | return &EtcdClient{pool: pool, |
| 85 | watchedClients: make(map[string]*v3Client.Client), |
| 86 | }, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 87 | } |
| 88 | |
David K. Bainbridge | bc38107 | 2021-03-19 20:56:04 +0000 | [diff] [blame] | 89 | // NewEtcdClient returns a new client for the Etcd KV store |
| 90 | func NewEtcdClient(ctx context.Context, addr string, timeout time.Duration, level log.LogLevel) (*EtcdClient, error) { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 91 | return NewEtcdCustomClient(ctx, addr, timeout, level) |
David K. Bainbridge | bc38107 | 2021-03-19 20:56:04 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 94 | // IsConnectionUp returns whether the connection to the Etcd KV store is up. If a timeout occurs then |
| 95 | // it is assumed the connection is down or unreachable. |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 96 | func (c *EtcdClient) IsConnectionUp(ctx context.Context) bool { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 97 | // Let's try to get a non existent key. If the connection is up then there will be no error returned. |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 98 | if _, err := c.Get(ctx, "non-existent-key"); err != nil { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 99 | return false |
| 100 | } |
| 101 | return true |
| 102 | } |
| 103 | |
| 104 | // List returns an array of key-value pairs with key as a prefix. Timeout defines how long the function will |
| 105 | // wait for a response |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 106 | func (c *EtcdClient) List(ctx context.Context, key string) (map[string]*KVPair, error) { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 107 | client, err := c.pool.Get(ctx) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | defer c.pool.Put(client) |
| 112 | resp, err := client.Get(ctx, key, v3Client.WithPrefix()) |
| 113 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 114 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 115 | logger.Error(ctx, err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 116 | return nil, err |
| 117 | } |
| 118 | m := make(map[string]*KVPair) |
| 119 | for _, ev := range resp.Kvs { |
| 120 | m[string(ev.Key)] = NewKVPair(string(ev.Key), ev.Value, "", ev.Lease, ev.Version) |
| 121 | } |
| 122 | return m, nil |
| 123 | } |
| 124 | |
| 125 | // Get returns a key-value pair for a given key. Timeout defines how long the function will |
| 126 | // wait for a response |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 127 | func (c *EtcdClient) Get(ctx context.Context, key string) (*KVPair, error) { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 128 | client, err := c.pool.Get(ctx) |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | defer c.pool.Put(client) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 133 | |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 134 | attempt := 0 |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 135 | |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 136 | startLoop: |
| 137 | for { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 138 | resp, err := client.Get(ctx, key) |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 139 | if err != nil { |
| 140 | switch err { |
| 141 | case context.Canceled: |
| 142 | logger.Warnw(ctx, "context-cancelled", log.Fields{"error": err}) |
| 143 | case context.DeadlineExceeded: |
| 144 | logger.Warnw(ctx, "context-deadline-exceeded", log.Fields{"error": err, "context": ctx}) |
| 145 | case v3rpcTypes.ErrEmptyKey: |
| 146 | logger.Warnw(ctx, "etcd-client-error", log.Fields{"error": err}) |
| 147 | case v3rpcTypes.ErrLeaderChanged, |
| 148 | v3rpcTypes.ErrGRPCNoLeader, |
| 149 | v3rpcTypes.ErrTimeout, |
| 150 | v3rpcTypes.ErrTimeoutDueToLeaderFail, |
| 151 | v3rpcTypes.ErrTimeoutDueToConnectionLost: |
| 152 | // Retry for these server errors |
| 153 | attempt += 1 |
| 154 | if er := backoff(ctx, attempt); er != nil { |
| 155 | logger.Warnw(ctx, "get-retries-failed", log.Fields{"key": key, "error": er, "attempt": attempt}) |
| 156 | return nil, err |
| 157 | } |
| 158 | logger.Warnw(ctx, "retrying-get", log.Fields{"key": key, "error": err, "attempt": attempt}) |
| 159 | goto startLoop |
| 160 | default: |
| 161 | logger.Warnw(ctx, "etcd-server-error", log.Fields{"error": err}) |
| 162 | } |
| 163 | return nil, err |
| 164 | } |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 165 | |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 166 | for _, ev := range resp.Kvs { |
| 167 | // Only one value is returned |
| 168 | return NewKVPair(string(ev.Key), ev.Value, "", ev.Lease, ev.Version), nil |
| 169 | } |
| 170 | return nil, nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 171 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Put writes a key-value pair to the KV store. Value can only be a string or []byte since the etcd API |
| 175 | // accepts only a string as a value for a put operation. Timeout defines how long the function will |
| 176 | // wait for a response |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 177 | func (c *EtcdClient) Put(ctx context.Context, key string, value interface{}) error { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 178 | |
| 179 | // Validate that we can convert value to a string as etcd API expects a string |
| 180 | var val string |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 181 | var err error |
| 182 | if val, err = ToString(value); err != nil { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 183 | return fmt.Errorf("unexpected-type-%T", value) |
| 184 | } |
| 185 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 186 | client, err := c.pool.Get(ctx) |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | defer c.pool.Put(client) |
| 191 | |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 192 | attempt := 0 |
| 193 | startLoop: |
| 194 | for { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 195 | _, err = client.Put(ctx, key, val) |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 196 | if err != nil { |
| 197 | switch err { |
| 198 | case context.Canceled: |
| 199 | logger.Warnw(ctx, "context-cancelled", log.Fields{"error": err}) |
| 200 | case context.DeadlineExceeded: |
| 201 | logger.Warnw(ctx, "context-deadline-exceeded", log.Fields{"error": err, "context": ctx}) |
| 202 | case v3rpcTypes.ErrEmptyKey: |
| 203 | logger.Warnw(ctx, "etcd-client-error", log.Fields{"error": err}) |
| 204 | case v3rpcTypes.ErrLeaderChanged, |
| 205 | v3rpcTypes.ErrGRPCNoLeader, |
| 206 | v3rpcTypes.ErrTimeout, |
| 207 | v3rpcTypes.ErrTimeoutDueToLeaderFail, |
| 208 | v3rpcTypes.ErrTimeoutDueToConnectionLost: |
| 209 | // Retry for these server errors |
| 210 | attempt += 1 |
| 211 | if er := backoff(ctx, attempt); er != nil { |
| 212 | logger.Warnw(ctx, "put-retries-failed", log.Fields{"key": key, "error": er, "attempt": attempt}) |
| 213 | return err |
| 214 | } |
| 215 | logger.Warnw(ctx, "retrying-put", log.Fields{"key": key, "error": err, "attempt": attempt}) |
| 216 | goto startLoop |
| 217 | default: |
| 218 | logger.Warnw(ctx, "etcd-server-error", log.Fields{"error": err}) |
| 219 | } |
| 220 | return err |
| 221 | } |
| 222 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 223 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Delete removes a key from the KV store. Timeout defines how long the function will |
| 227 | // wait for a response |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 228 | func (c *EtcdClient) Delete(ctx context.Context, key string) error { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 229 | client, err := c.pool.Get(ctx) |
| 230 | if err != nil { |
| 231 | return err |
| 232 | } |
| 233 | defer c.pool.Put(client) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 234 | |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 235 | attempt := 0 |
| 236 | startLoop: |
| 237 | for { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 238 | _, err = client.Delete(ctx, key) |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 239 | if err != nil { |
| 240 | switch err { |
| 241 | case context.Canceled: |
| 242 | logger.Warnw(ctx, "context-cancelled", log.Fields{"error": err}) |
| 243 | case context.DeadlineExceeded: |
| 244 | logger.Warnw(ctx, "context-deadline-exceeded", log.Fields{"error": err, "context": ctx}) |
| 245 | case v3rpcTypes.ErrEmptyKey: |
| 246 | logger.Warnw(ctx, "etcd-client-error", log.Fields{"error": err}) |
| 247 | case v3rpcTypes.ErrLeaderChanged, |
| 248 | v3rpcTypes.ErrGRPCNoLeader, |
| 249 | v3rpcTypes.ErrTimeout, |
| 250 | v3rpcTypes.ErrTimeoutDueToLeaderFail, |
| 251 | v3rpcTypes.ErrTimeoutDueToConnectionLost: |
| 252 | // Retry for these server errors |
| 253 | attempt += 1 |
| 254 | if er := backoff(ctx, attempt); er != nil { |
| 255 | logger.Warnw(ctx, "delete-retries-failed", log.Fields{"key": key, "error": er, "attempt": attempt}) |
| 256 | return err |
| 257 | } |
| 258 | logger.Warnw(ctx, "retrying-delete", log.Fields{"key": key, "error": err, "attempt": attempt}) |
| 259 | goto startLoop |
| 260 | default: |
| 261 | logger.Warnw(ctx, "etcd-server-error", log.Fields{"error": err}) |
| 262 | } |
| 263 | return err |
| 264 | } |
| 265 | logger.Debugw(ctx, "key(s)-deleted", log.Fields{"key": key}) |
| 266 | return nil |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 267 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Serkant Uluderya | 198de90 | 2020-11-16 20:29:17 +0300 | [diff] [blame] | 270 | func (c *EtcdClient) DeleteWithPrefix(ctx context.Context, prefixKey string) error { |
| 271 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 272 | client, err := c.pool.Get(ctx) |
| 273 | if err != nil { |
| 274 | return err |
| 275 | } |
| 276 | defer c.pool.Put(client) |
| 277 | |
Serkant Uluderya | 198de90 | 2020-11-16 20:29:17 +0300 | [diff] [blame] | 278 | //delete the prefix |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 279 | if _, err := client.Delete(ctx, prefixKey, v3Client.WithPrefix()); err != nil { |
Serkant Uluderya | 198de90 | 2020-11-16 20:29:17 +0300 | [diff] [blame] | 280 | logger.Errorw(ctx, "failed-to-delete-prefix-key", log.Fields{"key": prefixKey, "error": err}) |
| 281 | return err |
| 282 | } |
| 283 | logger.Debugw(ctx, "key(s)-deleted", log.Fields{"key": prefixKey}) |
| 284 | return nil |
| 285 | } |
| 286 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 287 | // Watch provides the watch capability on a given key. It returns a channel onto which the callee needs to |
| 288 | // listen to receive Events. |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 289 | func (c *EtcdClient) Watch(ctx context.Context, key string, withPrefix bool) chan *Event { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 290 | var err error |
| 291 | // Reuse the Etcd client when multiple callees are watching the same key. |
| 292 | c.watchedClientsLock.Lock() |
| 293 | client, exist := c.watchedClients[key] |
| 294 | if !exist { |
| 295 | client, err = c.pool.Get(ctx) |
| 296 | if err != nil { |
| 297 | logger.Errorw(ctx, "failed-to-an-etcd-client", log.Fields{"key": key, "error": err}) |
| 298 | c.watchedClientsLock.Unlock() |
| 299 | return nil |
| 300 | } |
| 301 | c.watchedClients[key] = client |
| 302 | } |
| 303 | c.watchedClientsLock.Unlock() |
| 304 | |
| 305 | w := v3Client.NewWatcher(client) |
npujar | 5bf737f | 2020-01-16 19:35:25 +0530 | [diff] [blame] | 306 | ctx, cancel := context.WithCancel(ctx) |
divyadesai | 8bf9686 | 2020-02-07 12:24:26 +0000 | [diff] [blame] | 307 | var channel v3Client.WatchChan |
| 308 | if withPrefix { |
| 309 | channel = w.Watch(ctx, key, v3Client.WithPrefix()) |
| 310 | } else { |
| 311 | channel = w.Watch(ctx, key) |
| 312 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 313 | |
| 314 | // Create a new channel |
| 315 | ch := make(chan *Event, maxClientChannelBufferSize) |
| 316 | |
| 317 | // Keep track of the created channels so they can be closed when required |
| 318 | channelMap := make(map[chan *Event]v3Client.Watcher) |
| 319 | channelMap[ch] = w |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 320 | channelMaps := c.addChannelMap(key, channelMap) |
| 321 | |
| 322 | // Changing the log field (from channelMaps) as the underlying logger cannot format the map of channels into a |
| 323 | // json format. |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 324 | logger.Debugw(ctx, "watched-channels", log.Fields{"len": len(channelMaps)}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 325 | // Launch a go routine to listen for updates |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 326 | go c.listenForKeyChange(ctx, channel, ch, cancel) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 327 | |
| 328 | return ch |
| 329 | |
| 330 | } |
| 331 | |
| 332 | func (c *EtcdClient) addChannelMap(key string, channelMap map[chan *Event]v3Client.Watcher) []map[chan *Event]v3Client.Watcher { |
| 333 | var channels interface{} |
| 334 | var exists bool |
| 335 | |
| 336 | if channels, exists = c.watchedChannels.Load(key); exists { |
| 337 | channels = append(channels.([]map[chan *Event]v3Client.Watcher), channelMap) |
| 338 | } else { |
| 339 | channels = []map[chan *Event]v3Client.Watcher{channelMap} |
| 340 | } |
| 341 | c.watchedChannels.Store(key, channels) |
| 342 | |
| 343 | return channels.([]map[chan *Event]v3Client.Watcher) |
| 344 | } |
| 345 | |
| 346 | func (c *EtcdClient) removeChannelMap(key string, pos int) []map[chan *Event]v3Client.Watcher { |
| 347 | var channels interface{} |
| 348 | var exists bool |
| 349 | |
| 350 | if channels, exists = c.watchedChannels.Load(key); exists { |
| 351 | channels = append(channels.([]map[chan *Event]v3Client.Watcher)[:pos], channels.([]map[chan *Event]v3Client.Watcher)[pos+1:]...) |
| 352 | c.watchedChannels.Store(key, channels) |
| 353 | } |
| 354 | |
| 355 | return channels.([]map[chan *Event]v3Client.Watcher) |
| 356 | } |
| 357 | |
| 358 | func (c *EtcdClient) getChannelMaps(key string) ([]map[chan *Event]v3Client.Watcher, bool) { |
| 359 | var channels interface{} |
| 360 | var exists bool |
| 361 | |
| 362 | channels, exists = c.watchedChannels.Load(key) |
| 363 | |
| 364 | if channels == nil { |
| 365 | return nil, exists |
| 366 | } |
| 367 | |
| 368 | return channels.([]map[chan *Event]v3Client.Watcher), exists |
| 369 | } |
| 370 | |
| 371 | // CloseWatch closes a specific watch. Both the key and the channel are required when closing a watch as there |
| 372 | // may be multiple listeners on the same key. The previously created channel serves as a key |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 373 | func (c *EtcdClient) CloseWatch(ctx context.Context, key string, ch chan *Event) { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 374 | // Get the array of channels mapping |
| 375 | var watchedChannels []map[chan *Event]v3Client.Watcher |
| 376 | var ok bool |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 377 | |
| 378 | if watchedChannels, ok = c.getChannelMaps(key); !ok { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 379 | logger.Warnw(ctx, "key-has-no-watched-channels", log.Fields{"key": key}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 380 | return |
| 381 | } |
| 382 | // Look for the channels |
| 383 | var pos = -1 |
| 384 | for i, chMap := range watchedChannels { |
| 385 | if t, ok := chMap[ch]; ok { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 386 | logger.Debug(ctx, "channel-found") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 387 | // Close the etcd watcher before the client channel. This should close the etcd channel as well |
| 388 | if err := t.Close(); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 389 | logger.Errorw(ctx, "watcher-cannot-be-closed", log.Fields{"key": key, "error": err}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 390 | } |
| 391 | pos = i |
| 392 | break |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | channelMaps, _ := c.getChannelMaps(key) |
| 397 | // Remove that entry if present |
| 398 | if pos >= 0 { |
| 399 | channelMaps = c.removeChannelMap(key, pos) |
| 400 | } |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 401 | |
| 402 | // If we don't have any keys being watched then return the Etcd client to the pool |
| 403 | if len(channelMaps) == 0 { |
| 404 | c.watchedClientsLock.Lock() |
| 405 | // Sanity |
| 406 | if client, ok := c.watchedClients[key]; ok { |
| 407 | c.pool.Put(client) |
| 408 | delete(c.watchedClients, key) |
| 409 | } |
| 410 | c.watchedClientsLock.Unlock() |
| 411 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 412 | logger.Infow(ctx, "watcher-channel-exiting", log.Fields{"key": key, "channel": channelMaps}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 415 | func (c *EtcdClient) listenForKeyChange(ctx context.Context, channel v3Client.WatchChan, ch chan<- *Event, cancel context.CancelFunc) { |
| 416 | logger.Debug(ctx, "start-listening-on-channel ...") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 417 | defer cancel() |
| 418 | defer close(ch) |
| 419 | for resp := range channel { |
| 420 | for _, ev := range resp.Events { |
| 421 | ch <- NewEvent(getEventType(ev), ev.Kv.Key, ev.Kv.Value, ev.Kv.Version) |
| 422 | } |
| 423 | } |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 424 | logger.Debug(ctx, "stop-listening-on-channel ...") |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | func getEventType(event *v3Client.Event) int { |
| 428 | switch event.Type { |
| 429 | case v3Client.EventTypePut: |
| 430 | return PUT |
| 431 | case v3Client.EventTypeDelete: |
| 432 | return DELETE |
| 433 | } |
| 434 | return UNKNOWN |
| 435 | } |
| 436 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 437 | // Close closes all the connection in the pool store client |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 438 | func (c *EtcdClient) Close(ctx context.Context) { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 439 | logger.Debug(ctx, "closing-etcd-pool") |
| 440 | c.pool.Close(ctx) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 441 | } |
| 442 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 443 | // The APIs below are not used |
| 444 | var errUnimplemented = errors.New("deprecated") |
| 445 | |
| 446 | // Reserve is deprecated |
| 447 | func (c *EtcdClient) Reserve(ctx context.Context, key string, value interface{}, ttl time.Duration) (interface{}, error) { |
| 448 | return nil, errUnimplemented |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 449 | } |
| 450 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 451 | // ReleaseAllReservations is deprecated |
| 452 | func (c *EtcdClient) ReleaseAllReservations(ctx context.Context) error { |
| 453 | return errUnimplemented |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 454 | } |
| 455 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 456 | // ReleaseReservation is deprecated |
| 457 | func (c *EtcdClient) ReleaseReservation(ctx context.Context, key string) error { |
| 458 | return errUnimplemented |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 459 | } |
| 460 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 461 | // RenewReservation is deprecated |
| 462 | func (c *EtcdClient) RenewReservation(ctx context.Context, key string) error { |
| 463 | return errUnimplemented |
| 464 | } |
| 465 | |
| 466 | // AcquireLock is deprecated |
Neha Sharma | 130ac6d | 2020-04-08 08:46:32 +0000 | [diff] [blame] | 467 | func (c *EtcdClient) AcquireLock(ctx context.Context, lockName string, timeout time.Duration) error { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 468 | return errUnimplemented |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 469 | } |
| 470 | |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 471 | // ReleaseLock is deprecated |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 472 | func (c *EtcdClient) ReleaseLock(lockName string) error { |
khenaidoo | 6f415b2 | 2021-06-22 18:08:53 -0400 | [diff] [blame] | 473 | return errUnimplemented |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 474 | } |