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