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