Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -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 | */ |
| 16 | |
| 17 | /* |
| 18 | * Two voltha cores receive the same request; each tries to acquire ownership of the request |
| 19 | * by writing its identifier (e.g. container name or pod name) to the transaction key named |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 20 | * after the serial number of the request. The core that loses the race for acquisition |
| 21 | * monitors the progress of the core actually serving the request by watching for changes |
| 22 | * in the value of the transaction key. Once the request is complete, the |
| 23 | * serving core closes the transaction by invoking the KVTransaction's Close method, which |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 24 | * replaces the value of the transaction (i.e. serial number) key with the string |
| 25 | * TRANSACTION_COMPLETE. The standby core observes this update, stops watching the transaction, |
| 26 | * and then deletes the transaction key. |
| 27 | * |
| 28 | * To ensure the key is removed despite possible standby core failures, a KV operation is |
| 29 | * scheduled in the background on both cores to delete the key well after the transaction is |
| 30 | * completed. The value of TransactionContext parameter timeToDeleteCompletedKeys should be |
| 31 | * long enough, on the order of many seconds, to ensure the standby sees the transaction |
| 32 | * closure. The aim is to prevent a growing list of TRANSACTION_COMPLETE values from loading |
| 33 | * the KV store. |
| 34 | */ |
| 35 | package core |
| 36 | |
| 37 | import ( |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 38 | log "github.com/opencord/voltha-go/common/log" |
| 39 | "github.com/opencord/voltha-go/db/kvstore" |
| 40 | "time" |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 41 | ) |
| 42 | |
| 43 | // Transaction acquisition results |
| 44 | const ( |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 45 | UNKNOWN = iota |
| 46 | SEIZED_BY_SELF |
| 47 | COMPLETED_BY_OTHER |
| 48 | ABANDONED_BY_OTHER |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 49 | STOPPED_WATCHING_KEY |
| 50 | STOPPED_WAITING_FOR_KEY |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 51 | ) |
| 52 | |
| 53 | const ( |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 54 | TRANSACTION_COMPLETE = "TRANSACTION-COMPLETE" |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 55 | ) |
| 56 | |
| 57 | type TransactionContext struct { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 58 | kvClient kvstore.Client |
| 59 | kvOperationTimeout int |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 60 | monitorLoopTime int64 |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 61 | owner string |
| 62 | timeToDeleteCompletedKeys int |
| 63 | txnPrefix string |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 64 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 65 | |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 66 | var ctx *TransactionContext |
| 67 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 68 | var txnState = []string{ |
| 69 | "UNKNOWN", |
| 70 | "SEIZED-BY-SELF", |
| 71 | "COMPLETED-BY-OTHER", |
| 72 | "ABANDONED-BY-OTHER", |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 73 | "STOPPED-WATCHING-KEY", |
| 74 | "STOPPED-WAITING-FOR-KEY"} |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 75 | |
| 76 | func init() { |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 77 | log.AddPackage(log.JSON, log.DebugLevel, nil) |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | func NewTransactionContext( |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 81 | owner string, |
| 82 | txnPrefix string, |
| 83 | kvClient kvstore.Client, |
| 84 | kvOpTimeout int, |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 85 | keyDeleteTime int, |
| 86 | monLoopTime int64) *TransactionContext { |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 87 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 88 | return &TransactionContext{ |
| 89 | owner: owner, |
| 90 | txnPrefix: txnPrefix, |
| 91 | kvClient: kvClient, |
| 92 | kvOperationTimeout: kvOpTimeout, |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 93 | monitorLoopTime: monLoopTime, |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 94 | timeToDeleteCompletedKeys: keyDeleteTime} |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Before instantiating a KVTransaction, a TransactionContext must be created. |
| 99 | * The parameters stored in the context govern the behaviour of all KVTransaction |
| 100 | * instances. |
| 101 | * |
| 102 | * :param owner: The owner (i.e. voltha core name) of a transaction |
| 103 | * :param txnPrefix: The key prefix under which all transaction IDs, or serial numbers, |
| 104 | * will be created (e.g. "service/voltha/transactions") |
| 105 | * :param kvClient: The client API used for all interactions with the KV store. Currently |
| 106 | * only the etcd client is supported. |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 107 | * :param: kvOpTimeout: The maximum time, in seconds, to be taken by any KV operation |
| 108 | * used by this package |
| 109 | * :param keyDeleteTime: The time (seconds) to wait, in the background, before deleting |
| 110 | * a TRANSACTION_COMPLETE key |
| 111 | * :param monLoopTime: The time in milliseconds that the monitor sleeps between |
| 112 | * checks for the existence of the transaction key |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 113 | */ |
| 114 | func SetTransactionContext(owner string, |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 115 | txnPrefix string, |
| 116 | kvClient kvstore.Client, |
| 117 | kvOpTimeout int, |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 118 | keyDeleteTime int, |
| 119 | monLoopTime int64) error { |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 120 | |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 121 | ctx = NewTransactionContext(owner, txnPrefix, kvClient, kvOpTimeout, keyDeleteTime, monLoopTime) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 122 | return nil |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 125 | type KVTransaction struct { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 126 | ch chan int |
| 127 | txnId string |
| 128 | txnKey string |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * A KVTransaction constructor |
| 133 | * |
| 134 | * :param txnId: The serial number of a voltha request. |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 135 | * :return: A KVTransaction instance |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 136 | */ |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 137 | func NewKVTransaction(txnId string) *KVTransaction { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 138 | return &KVTransaction{ |
| 139 | txnId: txnId, |
| 140 | txnKey: ctx.txnPrefix + txnId} |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* |
| 144 | * This function returns a boolean indicating whether or not the caller should process |
| 145 | * the request. True is returned in one of two cases: |
| 146 | * (1) The current core successfully reserved the request's serial number with the KV store |
| 147 | * (2) The current core failed in its reservation attempt but observed that the serving core |
| 148 | * has abandoned processing the request |
| 149 | * |
| 150 | * :param duration: The duration of the reservation in milliseconds |
| 151 | * :return: true - reservation acquired, process the request |
| 152 | * false - reservation not acquired, request being processed by another core |
| 153 | */ |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 154 | func (c *KVTransaction) Acquired(duration int64) bool { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 155 | var acquired bool |
| 156 | var currOwner string = "" |
| 157 | var res int |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 158 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 159 | // Convert milliseconds to seconds, rounding up |
| 160 | // The reservation TTL is specified in seconds |
| 161 | durationInSecs := duration / 1000 |
| 162 | if remainder := duration % 1000; remainder > 0 { |
| 163 | durationInSecs++ |
| 164 | } |
| 165 | value, err := ctx.kvClient.Reserve(c.txnKey, ctx.owner, durationInSecs) |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 166 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 167 | // If the reservation failed, do we simply abort or drop into watch mode anyway? |
| 168 | // Setting value to nil leads to watch mode |
| 169 | if value != nil { |
| 170 | if currOwner, err = kvstore.ToString(value); err != nil { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 171 | log.Errorw("unexpected-owner-type", log.Fields{"txn": c.txnId}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 172 | value = nil |
| 173 | } |
| 174 | } |
| 175 | if err == nil && value != nil && currOwner == ctx.owner { |
| 176 | // Process the request immediately |
| 177 | res = SEIZED_BY_SELF |
| 178 | } else { |
| 179 | // Another core instance has reserved the request |
| 180 | // Watch for reservation expiry or successful request completion |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 181 | log.Debugw("watch-other-server", |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 182 | log.Fields{"txn": c.txnId, "owner": currOwner, "timeout": duration}) |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 183 | |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 184 | res = c.Watch(duration) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 185 | } |
| 186 | // Clean-up: delete the transaction key after a long delay |
| 187 | go c.deleteTransactionKey() |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 188 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 189 | log.Debugw("acquire-transaction", log.Fields{"txn": c.txnId, "result": txnState[res]}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 190 | switch res { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 191 | case SEIZED_BY_SELF, ABANDONED_BY_OTHER, STOPPED_WATCHING_KEY: |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 192 | acquired = true |
| 193 | default: |
| 194 | acquired = false |
| 195 | } |
Richard Jankowski | 00a0466 | 2019-02-05 12:18:53 -0500 | [diff] [blame] | 196 | // Ensure the request watcher does not reply before the request server |
| 197 | if !acquired { |
| 198 | time.Sleep(1 * time.Second) |
| 199 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 200 | return acquired |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 203 | /* |
| 204 | * This function monitors the progress of a request that's been reserved by another |
| 205 | * Voltha core. |
| 206 | * |
| 207 | * :param duration: The duration of the reservation in milliseconds |
| 208 | * :return: true - reservation abandoned by the other core, process the request |
| 209 | * false - reservation not owned, request being processed by another core |
| 210 | */ |
| 211 | func (c *KVTransaction) Monitor(duration int64) bool { |
| 212 | var acquired bool |
| 213 | var res int |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 214 | |
| 215 | // Convert milliseconds to seconds, rounding up |
| 216 | // The reservation TTL is specified in seconds |
| 217 | durationInSecs := duration / 1000 |
| 218 | if remainder := duration % 1000; remainder > 0 { |
| 219 | durationInSecs++ |
| 220 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 221 | |
| 222 | res = c.Watch(duration) |
| 223 | |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 224 | // Clean-up: delete the transaction key after a long delay |
| 225 | go c.deleteTransactionKey() |
| 226 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 227 | log.Debugw("monitor-transaction", log.Fields{"txn": c.txnId, "result": txnState[res]}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 228 | switch res { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 229 | case ABANDONED_BY_OTHER, STOPPED_WATCHING_KEY, STOPPED_WAITING_FOR_KEY: |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 230 | acquired = true |
| 231 | default: |
| 232 | acquired = false |
| 233 | } |
| 234 | // Ensure the request watcher does not reply before the request server |
| 235 | if !acquired { |
| 236 | time.Sleep(1 * time.Second) |
| 237 | } |
| 238 | return acquired |
| 239 | } |
| 240 | |
| 241 | // duration in milliseconds |
| 242 | func (c *KVTransaction) Watch(duration int64) int { |
| 243 | var res int |
| 244 | |
| 245 | events := ctx.kvClient.Watch(c.txnKey) |
| 246 | select { |
| 247 | // Add a timeout here in case we miss an event from the KV |
| 248 | case <-time.After(time.Duration(duration) * time.Millisecond): |
| 249 | // In case of missing events, let's check the transaction key |
| 250 | kvp, err := ctx.kvClient.Get(c.txnKey, ctx.kvOperationTimeout, false) |
| 251 | if err == nil && kvp == nil { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 252 | log.Debugw("missed-delete-event", log.Fields{"txn": c.txnId}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 253 | res = ABANDONED_BY_OTHER |
| 254 | } else if val, err := kvstore.ToString(kvp.Value); err == nil && val == TRANSACTION_COMPLETE { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 255 | log.Debugw("missed-put-event", log.Fields{"txn": c.txnId, "value": val}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 256 | res = COMPLETED_BY_OTHER |
| 257 | } else { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 258 | log.Debugw("watch-timeout", log.Fields{"txn": c.txnId, "value": val}) |
| 259 | res = STOPPED_WATCHING_KEY |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | case event := <-events: |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 263 | log.Debugw("received-event", log.Fields{"txn": c.txnId, "type": event.EventType}) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 264 | if event.EventType == kvstore.DELETE { |
| 265 | // The other core failed to process the request |
| 266 | res = ABANDONED_BY_OTHER |
| 267 | } else if event.EventType == kvstore.PUT { |
| 268 | key, e1 := kvstore.ToString(event.Key) |
| 269 | val, e2 := kvstore.ToString(event.Value) |
| 270 | if e1 == nil && key == c.txnKey && e2 == nil && val == TRANSACTION_COMPLETE { |
| 271 | res = COMPLETED_BY_OTHER |
| 272 | // Successful request completion has been detected |
| 273 | // Remove the transaction key |
| 274 | c.Delete() |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | return res |
| 279 | } |
| 280 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 281 | func (c *KVTransaction) deleteTransactionKey() { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 282 | log.Debugw("schedule-key-deletion", log.Fields{"txnId": c.txnId, "txnkey": c.txnKey}) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 283 | time.Sleep(time.Duration(ctx.timeToDeleteCompletedKeys) * time.Second) |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 284 | log.Debugw("background-key-deletion", log.Fields{"txn": c.txnId, "txnkey": c.txnKey}) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 285 | ctx.kvClient.Delete(c.txnKey, ctx.kvOperationTimeout, false) |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 286 | } |
| 287 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 288 | func (c *KVTransaction) Close() error { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 289 | log.Debugw("close", log.Fields{"txn": c.txnId}) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 290 | return ctx.kvClient.Put(c.txnKey, TRANSACTION_COMPLETE, ctx.kvOperationTimeout, false) |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 293 | func (c *KVTransaction) Delete() error { |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 294 | log.Debugw("delete", log.Fields{"txn": c.txnId}) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 295 | err := ctx.kvClient.Delete(c.txnKey, ctx.kvOperationTimeout, false) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 296 | return err |
Richard Jankowski | 215a3e2 | 2018-10-04 13:56:11 -0400 | [diff] [blame] | 297 | } |