Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | 7a9af44 | 2024-01-03 19:26:36 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 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 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
| 22 | "time" |
| 23 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v7/pkg/db" |
| 25 | "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore" |
| 26 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 27 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/status" |
| 30 | ) |
| 31 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 32 | func newKVClient(ctx context.Context, storeType string, address string, timeout time.Duration) (kvstore.Client, error) { |
| 33 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
Abhay Kumar | 4e0f37b | 2024-07-12 09:33:01 +0530 | [diff] [blame^] | 34 | switch storeType { |
| 35 | case "etcd": |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 36 | return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel) |
Abhay Kumar | 4e0f37b | 2024-07-12 09:33:01 +0530 | [diff] [blame^] | 37 | case "redis": |
| 38 | return kvstore.NewRedisClient(address, timeout, false) |
| 39 | case "redis-sentinel": |
| 40 | return kvstore.NewRedisClient(address, timeout, true) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 41 | } |
| 42 | return nil, errors.New("unsupported-kv-store") |
| 43 | } |
| 44 | |
| 45 | func stopKVClient(ctx context.Context, kvClient kvstore.Client) { |
| 46 | // Release all reservations |
| 47 | if err := kvClient.ReleaseAllReservations(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 48 | logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 49 | } |
| 50 | // Close the DB connection |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 51 | kvClient.Close(ctx) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // waitUntilKVStoreReachableOrMaxTries will wait until it can connect to a KV store or until maxtries has been reached |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 55 | func waitUntilKVStoreReachableOrMaxTries(ctx context.Context, kvClient kvstore.Client, maxRetries int, retryInterval time.Duration, serviceName string) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 56 | logger.Infow(ctx, "verifying-KV-store-connectivity", log.Fields{"retries": maxRetries, "retryInterval": retryInterval}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 57 | count := 0 |
| 58 | for { |
| 59 | if !kvClient.IsConnectionUp(ctx) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 60 | logger.Info(ctx, "KV-store-unreachable") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 61 | if maxRetries != -1 { |
| 62 | if count >= maxRetries { |
| 63 | return status.Error(codes.Unavailable, "kv store unreachable") |
| 64 | } |
| 65 | } |
| 66 | count++ |
| 67 | |
| 68 | // Take a nap before retrying |
| 69 | select { |
| 70 | case <-ctx.Done(): |
| 71 | //ctx canceled |
| 72 | return ctx.Err() |
| 73 | case <-time.After(retryInterval): |
| 74 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 75 | logger.Infow(ctx, "retry-KV-store-connectivity", log.Fields{"retryCount": count, "maxRetries": maxRetries, "retryInterval": retryInterval}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 76 | } else { |
| 77 | break |
| 78 | } |
| 79 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 80 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 81 | logger.Info(ctx, "KV-store-reachable") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 82 | return nil |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Thread to monitor kvstore Liveness (connection status) |
| 87 | * |
| 88 | * This function constantly monitors Liveness State of kvstore as reported |
| 89 | * periodically by backend and updates the Status of kv-store service registered |
| 90 | * with rw_core probe. |
| 91 | * |
| 92 | * If no liveness event has been seen within a timeout, then the thread will |
| 93 | * perform a "liveness" check attempt, which will in turn trigger a liveness event on |
| 94 | * the liveness channel, true or false depending on whether the attempt succeeded. |
| 95 | * |
| 96 | * The gRPC server in turn monitors the state of the readiness probe and will |
| 97 | * start issuing UNAVAILABLE response while the probe is not ready. |
| 98 | */ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 99 | func monitorKVStoreLiveness(ctx context.Context, backend *db.Backend, serviceName string, liveProbeInterval, notLiveProbeInterval time.Duration) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 100 | logger.Info(ctx, "start-monitoring-kvstore-liveness") |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 101 | |
| 102 | // Instruct backend to create Liveness channel for transporting state updates |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 103 | livenessChannel := backend.EnableLivenessChannel(ctx) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 104 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 105 | logger.Infow(ctx, "enabled-liveness-channel", log.Fields{"service-name": serviceName}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 106 | |
| 107 | // Default state for kvstore is alive for rw_core |
| 108 | timeout := liveProbeInterval |
| 109 | loop: |
| 110 | for { |
| 111 | timeoutTimer := time.NewTimer(timeout) |
| 112 | select { |
| 113 | |
| 114 | case liveness := <-livenessChannel: |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 115 | logger.Debugw(ctx, "received-liveness-change-notification", log.Fields{"liveness": liveness, "service-name": serviceName}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 116 | |
| 117 | if !liveness { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 118 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusNotReady) |
| 119 | logger.Infow(ctx, "service-not-ready", log.Fields{"service-name": serviceName}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 120 | |
| 121 | timeout = notLiveProbeInterval |
| 122 | |
| 123 | } else { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 124 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
| 125 | logger.Infow(ctx, "service-ready", log.Fields{"service-name": serviceName}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 126 | |
| 127 | timeout = liveProbeInterval |
| 128 | } |
| 129 | |
| 130 | if !timeoutTimer.Stop() { |
| 131 | <-timeoutTimer.C |
| 132 | } |
| 133 | |
| 134 | case <-ctx.Done(): |
| 135 | break loop |
| 136 | |
| 137 | case <-timeoutTimer.C: |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 138 | logger.Infow(ctx, "perform-liveness-check-on-timeout", log.Fields{"service-name": serviceName}) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 139 | |
| 140 | // Trigger Liveness check if no liveness update received within the timeout period. |
| 141 | // The Liveness check will push Live state to same channel which this routine is |
| 142 | // reading and processing. This, do it asynchronously to avoid blocking for |
| 143 | // backend response and avoid any possibility of deadlock |
| 144 | go backend.PerformLivenessCheck(ctx) |
| 145 | } |
| 146 | } |
| 147 | } |