khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -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 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "compress/gzip" |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 22 | "encoding/hex" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 23 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 24 | "github.com/google/uuid" |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/db/kvstore" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 27 | "reflect" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 28 | "runtime/debug" |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 29 | "strings" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 30 | "sync" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 31 | ) |
| 32 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 33 | // PersistedRevision holds information of revision meant to be saved in a persistent storage |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 34 | type PersistedRevision struct { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 35 | Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 36 | Compress bool |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 37 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 38 | events chan *kvstore.Event `json:"-"` |
| 39 | kvStore *Backend `json:"-"` |
| 40 | mutex sync.RWMutex `json:"-"` |
| 41 | isStored bool |
| 42 | isWatched bool |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 43 | watchName string |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 46 | // NewPersistedRevision creates a new instance of a PersistentRevision structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 47 | func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 48 | pr := &PersistedRevision{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 49 | pr.kvStore = branch.Node.GetRoot().KvStore |
| 50 | pr.Revision = NewNonPersistedRevision(nil, branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 51 | return pr |
| 52 | } |
| 53 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 54 | // Finalize is responsible of saving the revision in the persistent storage |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 55 | func (pr *PersistedRevision) Finalize(skipOnExist bool) { |
| 56 | pr.store(skipOnExist) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | type revData struct { |
| 60 | Children map[string][]string |
| 61 | Config string |
| 62 | } |
| 63 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 64 | func (pr *PersistedRevision) store(skipOnExist bool) { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 65 | if pr.GetBranch().Txid != "" { |
| 66 | return |
| 67 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 68 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 69 | if pair, _ := pr.kvStore.Get(pr.GetHash()); pair != nil && skipOnExist { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 70 | log.Debugw("revision-config-already-exists", log.Fields{"hash": pr.GetHash()}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 71 | return |
| 72 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 73 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 74 | if blob, err := proto.Marshal(pr.GetConfig().Data.(proto.Message)); err != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 75 | // TODO report error |
| 76 | } else { |
| 77 | if pr.Compress { |
| 78 | var b bytes.Buffer |
| 79 | w := gzip.NewWriter(&b) |
| 80 | w.Write(blob) |
| 81 | w.Close() |
| 82 | blob = b.Bytes() |
| 83 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 84 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 85 | if err := pr.kvStore.Put(pr.GetHash(), blob); err != nil { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 86 | log.Warnw("problem-storing-revision-config", |
| 87 | log.Fields{"error": err, "hash": pr.GetHash(), "data": pr.GetConfig().Data}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 88 | } else { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 89 | log.Debugw("storing-revision-config", |
| 90 | log.Fields{"hash": pr.GetHash(), "data": pr.GetConfig().Data}) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 91 | pr.isStored = true |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 92 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 96 | func (pr *PersistedRevision) SetupWatch(key string) { |
| 97 | if pr.events == nil { |
| 98 | pr.events = make(chan *kvstore.Event) |
| 99 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 100 | log.Debugw("setting-watch", log.Fields{"key": key, "revision-hash": pr.GetHash()}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 101 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 102 | pr.watchName = key |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 103 | pr.events = pr.kvStore.CreateWatch(key) |
| 104 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 105 | pr.isWatched = true |
| 106 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 107 | // Start watching |
| 108 | go pr.startWatching() |
| 109 | } |
| 110 | } |
| 111 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 112 | //func (pr *PersistedRevision) mergeWithMemory(pacBlock bool) Revision { |
| 113 | // if pair, err := pr.kvStore.Get(pr.GetHash()); err != nil { |
| 114 | // log.Debugw("merge-with-memory--error-occurred", log.Fields{"hash": pr.GetHash(), "error": err}) |
| 115 | // } else if pair == nil { |
| 116 | // log.Debugw("merge-with-memory--no-data-to-merge", log.Fields{"hash": pr.GetHash()}) |
| 117 | // } else { |
| 118 | // data := reflect.New(reflect.TypeOf(pr.GetData()).Elem()).Interface() |
| 119 | // |
| 120 | // if err := proto.Unmarshal(pair.Value.([]byte), data.(proto.Message)); err != nil { |
| 121 | // log.Errorw("merge-with-memory--unmarshal-failed", log.Fields{"key": pr.GetHash(), "error": err}) |
| 122 | // } else { |
| 123 | // if pr.GetNode().GetProxy() != nil && pacBlock { |
| 124 | // var pac *proxyAccessControl |
| 125 | // var pathLock string |
| 126 | // |
| 127 | // pathLock, _ = pr.GetNode().GetProxy().parseForControlledPath(pr.GetNode().GetProxy().getFullPath()) |
| 128 | // log.Debugw("update-in-memory--reserve-and-lock", log.Fields{"key": pr.GetHash(), "path": pathLock}) |
| 129 | // |
| 130 | // pac = PAC().ReservePath(pr.GetNode().GetProxy().getFullPath(), pr.GetNode().GetProxy(), pathLock) |
| 131 | // pac.SetProxy(pr.GetNode().GetProxy()) |
| 132 | // pac.lock() |
| 133 | // |
| 134 | // defer log.Debugw("update-in-memory--release-and-unlock", log.Fields{"key": pr.GetHash(), "path": pathLock}) |
| 135 | // defer pac.unlock() |
| 136 | // defer PAC().ReleasePath(pathLock) |
| 137 | // } |
| 138 | // //Prepare the transaction |
| 139 | // branch := pr.GetBranch() |
| 140 | // //latest := branch.GetLatest() |
| 141 | // txidBin, _ := uuid.New().MarshalBinary() |
| 142 | // txid := hex.EncodeToString(txidBin)[:12] |
| 143 | // |
| 144 | // makeBranch := func(node *node) *Branch { |
| 145 | // return node.MakeBranch(txid) |
| 146 | // } |
| 147 | // |
| 148 | // //Apply the update in a transaction branch |
| 149 | // updatedRev := pr.GetNode().Update("", data, false, txid, makeBranch) |
| 150 | // updatedRev.SetHash(pr.GetHash()) |
| 151 | // |
| 152 | // //Merge the transaction branch in memory |
| 153 | // if mergedRev, _ := pr.GetNode().MergeBranch(txid, false); mergedRev != nil { |
| 154 | // branch.SetLatest(mergedRev) |
| 155 | // return mergedRev |
| 156 | // } |
| 157 | // } |
| 158 | // } |
| 159 | // |
| 160 | // return nil |
| 161 | //} |
| 162 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 163 | func (pr *PersistedRevision) updateInMemory(data interface{}) { |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 164 | pr.mutex.Lock() |
| 165 | defer pr.mutex.Unlock() |
| 166 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 167 | var pac *proxyAccessControl |
| 168 | var pathLock string |
| 169 | |
| 170 | // |
| 171 | // If a proxy exists for this revision, use it to lock access to the path |
| 172 | // and prevent simultaneous updates to the object in memory |
| 173 | // |
| 174 | if pr.GetNode().GetProxy() != nil { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 175 | pathLock, _ = pr.GetNode().GetProxy().parseForControlledPath(pr.GetNode().GetProxy().getFullPath()) |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 176 | |
| 177 | // If the proxy already has a request in progress, then there is no need to process the watch |
| 178 | log.Debugw("update-in-memory--checking-pathlock", log.Fields{"key": pr.GetHash(), "pathLock": pathLock}) |
| 179 | if PAC().IsReserved(pathLock) { |
| 180 | switch pr.GetNode().GetRoot().GetProxy().Operation { |
| 181 | case PROXY_ADD: |
| 182 | fallthrough |
| 183 | case PROXY_REMOVE: |
| 184 | fallthrough |
| 185 | case PROXY_UPDATE: |
| 186 | log.Debugw("update-in-memory--skipping", log.Fields{"key": pr.GetHash(), "path": pr.GetNode().GetProxy().getFullPath()}) |
| 187 | return |
| 188 | default: |
| 189 | log.Debugw("update-in-memory--operation", log.Fields{"operation": pr.GetNode().GetRoot().GetProxy().Operation}) |
| 190 | } |
| 191 | } else { |
| 192 | log.Debugw("update-in-memory--path-not-locked", log.Fields{"key": pr.GetHash(), "path": pr.GetNode().GetProxy().getFullPath()}) |
| 193 | } |
| 194 | |
| 195 | log.Debugw("update-in-memory--reserve-and-lock", log.Fields{"key": pr.GetHash(), "path": pathLock}) |
| 196 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 197 | pac = PAC().ReservePath(pr.GetNode().GetProxy().getFullPath(), pr.GetNode().GetProxy(), pathLock) |
| 198 | pac.SetProxy(pr.GetNode().GetProxy()) |
| 199 | pac.lock() |
| 200 | |
| 201 | defer log.Debugw("update-in-memory--release-and-unlock", log.Fields{"key": pr.GetHash(), "path": pathLock}) |
| 202 | defer pac.unlock() |
| 203 | defer PAC().ReleasePath(pathLock) |
| 204 | } |
| 205 | |
| 206 | // |
| 207 | // Update the object in memory through a transaction |
| 208 | // This will allow for the object to be subsequently merged with any changes |
| 209 | // that might have occurred in memory |
| 210 | // |
| 211 | |
| 212 | log.Debugw("update-in-memory--custom-transaction", log.Fields{"key": pr.GetHash()}) |
| 213 | |
| 214 | // Prepare the transaction |
| 215 | branch := pr.GetBranch() |
| 216 | latest := branch.GetLatest() |
| 217 | txidBin, _ := uuid.New().MarshalBinary() |
| 218 | txid := hex.EncodeToString(txidBin)[:12] |
| 219 | |
| 220 | makeBranch := func(node *node) *Branch { |
| 221 | return node.MakeBranch(txid) |
| 222 | } |
| 223 | |
| 224 | // Apply the update in a transaction branch |
| 225 | updatedRev := latest.GetNode().Update("", data, false, txid, makeBranch) |
| 226 | updatedRev.SetHash(latest.GetHash()) |
| 227 | |
| 228 | // Merge the transaction branch in memory |
| 229 | if mergedRev, _ := latest.GetNode().MergeBranch(txid, false); mergedRev != nil { |
| 230 | branch.SetLatest(mergedRev) |
| 231 | } |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 232 | } |
| 233 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 234 | func (pr *PersistedRevision) startWatching() { |
| 235 | log.Debugw("starting-watch", log.Fields{"key": pr.GetHash()}) |
| 236 | |
| 237 | StopWatchLoop: |
| 238 | for { |
| 239 | select { |
| 240 | case event, ok := <-pr.events: |
| 241 | if !ok { |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 242 | log.Errorw("event-channel-failure: stopping watch loop", log.Fields{"key": pr.GetHash(), "watch": pr.watchName}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 243 | break StopWatchLoop |
| 244 | } |
| 245 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 246 | log.Debugw("received-event", log.Fields{"type": event.EventType, "watch": pr.watchName}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 247 | |
| 248 | switch event.EventType { |
| 249 | case kvstore.DELETE: |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 250 | log.Debugw("delete-from-memory", log.Fields{"key": pr.GetHash(), "watch": pr.watchName}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 251 | pr.Revision.Drop("", true) |
| 252 | break StopWatchLoop |
| 253 | |
| 254 | case kvstore.PUT: |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 255 | log.Debugw("update-in-memory", log.Fields{"key": pr.GetHash(), "watch": pr.watchName}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 256 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 257 | if dataPair, err := pr.kvStore.Get(pr.watchName); err != nil || dataPair == nil { |
| 258 | log.Errorw("update-in-memory--key-retrieval-failed", log.Fields{"key": pr.GetHash(), "watch": pr.watchName, "error": err}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 259 | } else { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 260 | data := reflect.New(reflect.TypeOf(pr.GetData()).Elem()) |
| 261 | |
| 262 | if err := proto.Unmarshal(dataPair.Value.([]byte), data.Interface().(proto.Message)); err != nil { |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 263 | log.Errorw("update-in-memory--unmarshal-failed", log.Fields{"key": pr.GetHash(), "watch": pr.watchName, "error": err}) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 264 | } else { |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 265 | pr.updateInMemory(data.Interface()) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 266 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | default: |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 270 | log.Debugw("unhandled-event", log.Fields{"key": pr.GetHash(), "watch": pr.watchName, "type": event.EventType}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame^] | 275 | log.Debugw("exiting-watch", log.Fields{"key": pr.GetHash(), "watch": pr.watchName}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 278 | func (pr *PersistedRevision) LoadFromPersistence(path string, txid string) []Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 279 | log.Debugw("loading-from-persistence", log.Fields{"path": path, "txid": txid}) |
| 280 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 281 | var response []Revision |
| 282 | var rev Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 283 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 284 | rev = pr |
| 285 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 286 | if pr.kvStore != nil && path != "" { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 287 | blobMap, _ := pr.kvStore.List(path) |
| 288 | |
| 289 | partition := strings.SplitN(path, "/", 2) |
| 290 | name := partition[0] |
| 291 | |
| 292 | if len(partition) < 2 { |
| 293 | path = "" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 294 | } else { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 295 | path = partition[1] |
| 296 | } |
| 297 | |
| 298 | field := ChildrenFields(rev.GetBranch().Node.Type)[name] |
| 299 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 300 | if field != nil && field.IsContainer { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 301 | var children []Revision |
| 302 | children = make([]Revision, len(rev.GetChildren(name))) |
| 303 | copy(children, rev.GetChildren(name)) |
| 304 | existChildMap := make(map[string]int) |
| 305 | for i, child := range rev.GetChildren(name) { |
| 306 | existChildMap[child.GetHash()] = i |
| 307 | } |
| 308 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 309 | for _, blob := range blobMap { |
| 310 | output := blob.Value.([]byte) |
| 311 | |
| 312 | data := reflect.New(field.ClassType.Elem()) |
| 313 | |
| 314 | if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 315 | log.Errorw( |
| 316 | "loading-from-persistence--failed-to-unmarshal", |
| 317 | log.Fields{"path": path, "txid": txid, "error": err}, |
| 318 | ) |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 319 | } else if field.Key != "" { |
| 320 | var key reflect.Value |
| 321 | var keyValue interface{} |
| 322 | var keyStr string |
| 323 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 324 | if path == "" { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 325 | // e.g. /logical_devices --> path="" name=logical_devices key="" |
| 326 | _, key = GetAttributeValue(data.Interface(), field.Key, 0) |
| 327 | keyStr = key.String() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 328 | |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 329 | } else { |
| 330 | // e.g. |
| 331 | // /logical_devices/abcde --> path="abcde" name=logical_devices |
| 332 | // /logical_devices/abcde/image_downloads --> path="abcde/image_downloads" name=logical_devices |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 333 | |
| 334 | partition := strings.SplitN(path, "/", 2) |
| 335 | key := partition[0] |
| 336 | if len(partition) < 2 { |
| 337 | path = "" |
| 338 | } else { |
| 339 | path = partition[1] |
| 340 | } |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 341 | keyValue = field.KeyFromStr(key) |
| 342 | keyStr = keyValue.(string) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 343 | |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 344 | if idx, childRev := rev.GetBranch().Node.findRevByKey(children, field.Key, keyValue); childRev != nil { |
| 345 | // Key is memory, continue recursing path |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 346 | if newChildRev := childRev.LoadFromPersistence(path, txid); newChildRev != nil { |
| 347 | children[idx] = newChildRev[0] |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 348 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 349 | rev := rev.UpdateChildren(name, rev.GetChildren(name), rev.GetBranch()) |
| 350 | rev.GetBranch().Node.makeLatest(rev.GetBranch(), rev, nil) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 351 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 352 | response = append(response, newChildRev[0]) |
| 353 | continue |
| 354 | } |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 355 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 356 | } |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 357 | |
| 358 | childRev := rev.GetBranch().Node.MakeNode(data.Interface(), txid).Latest(txid) |
| 359 | childRev.SetHash(name + "/" + keyStr) |
| 360 | |
| 361 | // Do not process a child that is already in memory |
| 362 | if _, childExists := existChildMap[childRev.GetHash()]; !childExists { |
| 363 | // Create watch for <component>/<key> |
| 364 | childRev.SetupWatch(childRev.GetHash()) |
| 365 | |
| 366 | children = append(children, childRev) |
| 367 | rev = rev.UpdateChildren(name, children, rev.GetBranch()) |
| 368 | |
| 369 | rev.GetBranch().Node.makeLatest(rev.GetBranch(), rev, nil) |
| 370 | } |
| 371 | response = append(response, childRev) |
| 372 | continue |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 373 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | } |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 377 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 378 | return response |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 379 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 380 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 381 | // UpdateData modifies the information in the data model and saves it in the persistent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 382 | func (pr *PersistedRevision) UpdateData(data interface{}, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 383 | log.Debugw("updating-persisted-data", log.Fields{"hash": pr.GetHash()}) |
| 384 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 385 | newNPR := pr.Revision.UpdateData(data, branch) |
| 386 | |
| 387 | newPR := &PersistedRevision{ |
| 388 | Revision: newNPR, |
| 389 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 390 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 393 | return newPR |
| 394 | } |
| 395 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 396 | // UpdateChildren modifies the children of a revision and of a specific component and saves it in the persistent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 397 | func (pr *PersistedRevision) UpdateChildren(name string, children []Revision, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 398 | log.Debugw("updating-persisted-children", log.Fields{"hash": pr.GetHash()}) |
| 399 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 400 | newNPR := pr.Revision.UpdateChildren(name, children, branch) |
| 401 | |
| 402 | newPR := &PersistedRevision{ |
| 403 | Revision: newNPR, |
| 404 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 405 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 406 | } |
| 407 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 408 | return newPR |
| 409 | } |
| 410 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 411 | // UpdateAllChildren modifies the children for all components of a revision and saves it in the peristent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 412 | func (pr *PersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 413 | log.Debugw("updating-all-persisted-children", log.Fields{"hash": pr.GetHash()}) |
| 414 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 415 | newNPR := pr.Revision.UpdateAllChildren(children, branch) |
| 416 | |
| 417 | newPR := &PersistedRevision{ |
| 418 | Revision: newNPR, |
| 419 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 420 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 423 | return newPR |
| 424 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 425 | |
| 426 | // Drop takes care of eliminating a revision hash that is no longer needed |
| 427 | // and its associated config when required |
| 428 | func (pr *PersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 429 | log.Debugw("dropping-revision", |
| 430 | log.Fields{"txid": txid, "hash": pr.GetHash(), "config-hash": pr.GetConfig().Hash, "stack": string(debug.Stack())}) |
| 431 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 432 | pr.mutex.Lock() |
| 433 | defer pr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 434 | if pr.kvStore != nil && txid == "" { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 435 | if pr.isStored { |
| 436 | if includeConfig { |
| 437 | if err := pr.kvStore.Delete(pr.GetConfig().Hash); err != nil { |
| 438 | log.Errorw("failed-to-remove-revision-config", log.Fields{"hash": pr.GetConfig().Hash, "error": err.Error()}) |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | if err := pr.kvStore.Delete(pr.GetHash()); err != nil { |
| 443 | log.Errorw("failed-to-remove-revision", log.Fields{"hash": pr.GetHash(), "error": err.Error()}) |
| 444 | } else { |
| 445 | pr.isStored = false |
| 446 | } |
| 447 | |
| 448 | if pr.isWatched { |
| 449 | pr.kvStore.DeleteWatch(pr.GetHash(), pr.events) |
| 450 | pr.isWatched = false |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 454 | } else { |
| 455 | if includeConfig { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 456 | log.Debugw("attempted-to-remove-transacted-revision-config", log.Fields{"hash": pr.GetConfig().Hash, "txid": txid}) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 457 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 458 | log.Debugw("attempted-to-remove-transacted-revision", log.Fields{"hash": pr.GetHash(), "txid": txid}) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 459 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 460 | |
| 461 | pr.Revision.Drop(txid, includeConfig) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 462 | } |