khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 17 | package utils |
| 18 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 19 | import ( |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 20 | "context" |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 21 | "os" |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 22 | "sync" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 23 | "time" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 24 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 26 | "google.golang.org/grpc/codes" |
| 27 | "google.golang.org/grpc/status" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 30 | // ResponseCallback is the function signature for callbacks to execute after a response is received. |
| 31 | type ResponseCallback func(rpc string, response interface{}, reqArgs ...interface{}) |
| 32 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 33 | // DeviceID represent device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 34 | type DeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 35 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 36 | } |
| 37 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 38 | // LogicalDeviceID rpresent logical device id attribute |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 39 | type LogicalDeviceID struct { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 40 | ID string |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 41 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 42 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 43 | // GetHostName returns host name |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 44 | func GetHostName() string { |
| 45 | return os.Getenv("HOSTNAME") |
| 46 | } |
| 47 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 48 | type request struct { |
| 49 | channel chan struct{} |
| 50 | done chan struct{} |
| 51 | } |
| 52 | |
| 53 | func newRequest() *request { |
| 54 | return &request{ |
| 55 | channel: make(chan struct{}), |
| 56 | done: make(chan struct{}), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // RequestQueue represents a request processing queue where each request is processed to completion before another |
| 61 | // request is given the green light to proceed. |
| 62 | type RequestQueue struct { |
| 63 | queue chan *request |
| 64 | requestCompleteIndication chan struct{} |
| 65 | queueID string |
| 66 | stopOnce sync.Once |
| 67 | stopped bool |
| 68 | } |
| 69 | |
| 70 | // NewRequestQueue creates a new request queue. maxQueueSize is the maximum size of the queue. queueID is used mostly |
| 71 | // for logging. |
| 72 | func NewRequestQueue(queueID string, maxQueueSize int) *RequestQueue { |
| 73 | return &RequestQueue{ |
| 74 | queueID: queueID, |
| 75 | queue: make(chan *request, maxQueueSize), |
| 76 | requestCompleteIndication: make(chan struct{}), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Start starts the request processing queue in its own go routine |
| 81 | func (rq *RequestQueue) Start() { |
| 82 | go func() { |
| 83 | for { |
| 84 | req, ok := <-rq.queue |
| 85 | if !ok { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame^] | 86 | logger.Warnw("request-sequencer-queue-closed", log.Fields{"id": rq.queueID}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 87 | break |
| 88 | } |
| 89 | // If the request is waiting then closing the reqChnl will trigger the request to proceed. Otherwise, |
| 90 | // if the request was cancelled then this will just clean up. |
| 91 | close(req.channel) |
| 92 | |
| 93 | // Wait for either a request complete indication or a request aborted due to timeout |
| 94 | select { |
| 95 | case <-req.done: |
| 96 | case <-rq.requestCompleteIndication: |
| 97 | } |
| 98 | } |
| 99 | }() |
| 100 | } |
| 101 | |
| 102 | // WaitForGreenLight is invoked by a function processing a request to receive the green light before |
| 103 | // proceeding. The caller can also provide a context with timeout. The timeout will be triggered if the wait is |
| 104 | // too long (previous requests taking too long) |
| 105 | func (rq *RequestQueue) WaitForGreenLight(ctx context.Context) error { |
| 106 | if rq.stopped { |
| 107 | return status.Errorf(codes.Aborted, "queue-already-stopped-%s", rq.queueID) |
| 108 | } |
| 109 | request := newRequest() |
| 110 | // Queue the request |
| 111 | rq.queue <- request |
| 112 | select { |
| 113 | case <-request.channel: |
| 114 | return nil |
| 115 | case <-ctx.Done(): |
| 116 | close(request.done) |
| 117 | return ctx.Err() |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // RequestComplete must be invoked by a process when it completes processing the request. That process must have |
| 122 | // invoked WaitForGreenLight() before. |
| 123 | func (rq *RequestQueue) RequestComplete() { |
| 124 | if !rq.stopped { |
| 125 | rq.requestCompleteIndication <- struct{}{} |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Stop must only be invoked by the process that started the request queue. Prior to invoking Stop, WaitForGreenLight |
| 130 | // must be invoked. |
| 131 | func (rq *RequestQueue) Stop() { |
| 132 | rq.stopOnce.Do(func() { |
| 133 | rq.stopped = true |
| 134 | close(rq.requestCompleteIndication) |
| 135 | close(rq.queue) |
| 136 | }) |
| 137 | } |
| 138 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 139 | // Response - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 140 | type Response struct { |
| 141 | *response |
| 142 | } |
| 143 | type response struct { |
| 144 | err error |
| 145 | ch chan struct{} |
| 146 | done bool |
| 147 | } |
| 148 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 149 | // NewResponse - |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 150 | func NewResponse() Response { |
| 151 | return Response{ |
| 152 | &response{ |
| 153 | ch: make(chan struct{}), |
| 154 | }, |
| 155 | } |
| 156 | } |
| 157 | |
A R Karthick | 5c28f55 | 2019-12-11 22:47:44 -0800 | [diff] [blame] | 158 | // Fake a completed response. |
| 159 | func DoneResponse() Response { |
| 160 | r := Response{ |
| 161 | &response{ |
| 162 | err: nil, |
| 163 | ch: make(chan struct{}), |
| 164 | done: true, |
| 165 | }, |
| 166 | } |
| 167 | close(r.ch) |
| 168 | return r |
| 169 | } |
| 170 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 171 | // Error sends a response with the given error. It may only be called once. |
| 172 | func (r Response) Error(err error) { |
| 173 | // if this is called twice, it will panic; this is intentional |
| 174 | r.err = err |
| 175 | r.done = true |
| 176 | close(r.ch) |
| 177 | } |
| 178 | |
| 179 | // Done sends a non-error response unless Error has already been called, in which case this is a no-op. |
| 180 | func (r Response) Done() { |
| 181 | if !r.done { |
| 182 | close(r.ch) |
| 183 | } |
| 184 | } |
| 185 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 186 | //WaitForNilOrErrorResponses waits on a variadic number of channels for either a nil response or an error |
| 187 | //response. If an error is received from a given channel then the returned error array will contain that error. |
| 188 | //The error will be at the index corresponding to the order in which the channel appear in the parameter list. |
| 189 | //If no errors is found then nil is returned. This method also takes in a timeout in milliseconds. If a |
| 190 | //timeout is obtained then this function will stop waiting for the remaining responses and abort. |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 191 | func WaitForNilOrErrorResponses(timeout time.Duration, responses ...Response) []error { |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 192 | timedOut := make(chan struct{}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 193 | timer := time.AfterFunc(timeout, func() { close(timedOut) }) |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 194 | defer timer.Stop() |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 195 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 196 | gotError := false |
| 197 | errors := make([]error, 0, len(responses)) |
| 198 | for _, response := range responses { |
| 199 | var err error |
| 200 | select { |
| 201 | case <-response.ch: |
| 202 | // if a response is already available, use it |
| 203 | err = response.err |
| 204 | default: |
| 205 | // otherwise, wait for either a response or a timeout |
| 206 | select { |
| 207 | case <-response.ch: |
| 208 | err = response.err |
| 209 | case <-timedOut: |
| 210 | err = status.Error(codes.Aborted, "timeout") |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 211 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 212 | } |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 213 | gotError = gotError || err != nil |
| 214 | errors = append(errors, err) |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Kent Hagerman | 8da2f1e | 2019-11-25 17:28:09 -0500 | [diff] [blame] | 217 | if gotError { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 218 | return errors |
| 219 | } |
| 220 | return nil |
| 221 | } |