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 | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 22 | "context" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 23 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [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 | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 28 | "strings" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 29 | "sync" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 32 | // 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] | 33 | type PersistedRevision struct { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 34 | Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 35 | Compress bool |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 36 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 37 | events chan *kvstore.Event |
| 38 | kvStore *Backend |
| 39 | mutex sync.RWMutex |
| 40 | versionMutex sync.RWMutex |
| 41 | Version int64 |
| 42 | isStored bool |
| 43 | isWatched bool |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 46 | type watchCache struct { |
| 47 | Cache sync.Map |
| 48 | } |
| 49 | |
| 50 | var watchCacheInstance *watchCache |
| 51 | var watchCacheOne sync.Once |
| 52 | |
| 53 | func Watches() *watchCache { |
| 54 | watchCacheOne.Do(func() { |
| 55 | watchCacheInstance = &watchCache{Cache: sync.Map{}} |
| 56 | }) |
| 57 | return watchCacheInstance |
| 58 | } |
| 59 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 60 | // NewPersistedRevision creates a new instance of a PersistentRevision structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 61 | func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 62 | pr := &PersistedRevision{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 63 | pr.kvStore = branch.Node.GetRoot().KvStore |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 64 | pr.Version = 1 |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 65 | pr.Revision = NewNonPersistedRevision(nil, branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 66 | return pr |
| 67 | } |
| 68 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 69 | func (pr *PersistedRevision) getVersion() int64 { |
| 70 | pr.versionMutex.RLock() |
| 71 | defer pr.versionMutex.RUnlock() |
| 72 | return pr.Version |
| 73 | } |
| 74 | |
| 75 | func (pr *PersistedRevision) setVersion(version int64) { |
| 76 | pr.versionMutex.Lock() |
| 77 | defer pr.versionMutex.Unlock() |
| 78 | pr.Version = version |
| 79 | } |
| 80 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 81 | // Finalize is responsible of saving the revision in the persistent storage |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 82 | func (pr *PersistedRevision) Finalize(skipOnExist bool) { |
| 83 | pr.store(skipOnExist) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 86 | func (pr *PersistedRevision) store(skipOnExist bool) { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 87 | if pr.GetBranch().Txid != "" { |
| 88 | return |
| 89 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 90 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 91 | log.Debugw("ready-to-store-revision", log.Fields{"hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetData()}) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 92 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 93 | // clone the revision data to avoid any race conditions with processes |
| 94 | // accessing the same data |
| 95 | cloned := proto.Clone(pr.GetConfig().Data.(proto.Message)) |
| 96 | |
| 97 | if blob, err := proto.Marshal(cloned); err != nil { |
| 98 | log.Errorw("problem-to-marshal", log.Fields{"error": err, "hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetData()}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 99 | } else { |
| 100 | if pr.Compress { |
| 101 | var b bytes.Buffer |
| 102 | w := gzip.NewWriter(&b) |
| 103 | w.Write(blob) |
| 104 | w.Close() |
| 105 | blob = b.Bytes() |
| 106 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 107 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 108 | GetRevCache().Set(pr.GetName(), pr) |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 109 | if err := pr.kvStore.Put(pr.GetName(), blob); err != nil { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 110 | log.Warnw("problem-storing-revision", 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] | 111 | } else { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 112 | log.Debugw("storing-revision", log.Fields{"hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetConfig().Data, "version": pr.getVersion()}) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 113 | pr.isStored = true |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 114 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 118 | func (pr *PersistedRevision) SetupWatch(key string) { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 119 | if key == "" { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 120 | log.Debugw("ignoring-watch", log.Fields{"key": key, "revision-hash": pr.GetHash()}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 121 | return |
| 122 | } |
| 123 | |
| 124 | if _, exists := Watches().Cache.LoadOrStore(key+"-"+pr.GetHash(), struct{}{}); exists { |
| 125 | return |
| 126 | } |
| 127 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 128 | if pr.events == nil { |
| 129 | pr.events = make(chan *kvstore.Event) |
| 130 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 131 | log.Debugw("setting-watch-channel", log.Fields{"key": key, "revision-hash": pr.GetHash()}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 132 | |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 133 | pr.SetName(key) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 134 | pr.events = pr.kvStore.CreateWatch(key) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 135 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 136 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 137 | if !pr.isWatched { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 138 | pr.isWatched = true |
| 139 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 140 | log.Debugw("setting-watch-routine", log.Fields{"key": key, "revision-hash": pr.GetHash()}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 141 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 142 | // Start watching |
| 143 | go pr.startWatching() |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func (pr *PersistedRevision) startWatching() { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 148 | log.Debugw("starting-watch", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 149 | |
| 150 | StopWatchLoop: |
| 151 | for { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 152 | latestRev := pr.GetBranch().GetLatest() |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 153 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 154 | select { |
| 155 | case event, ok := <-pr.events: |
| 156 | if !ok { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 157 | log.Errorw("event-channel-failure: stopping watch loop", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName()}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 158 | break StopWatchLoop |
| 159 | } |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 160 | log.Debugw("received-event", log.Fields{"type": event.EventType, "watch": latestRev.GetName()}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 161 | |
| 162 | switch event.EventType { |
| 163 | case kvstore.DELETE: |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 164 | log.Debugw("delete-from-memory", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName()}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 165 | pr.Revision.Drop("", true) |
| 166 | break StopWatchLoop |
| 167 | |
| 168 | case kvstore.PUT: |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 169 | log.Debugw("update-in-memory", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName()}) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 170 | if latestRev.getVersion() >= event.Version { |
| 171 | log.Debugw("skipping-matching-or-older-revision", log.Fields{ |
| 172 | "watch": latestRev.GetName(), |
| 173 | "watch-version": event.Version, |
| 174 | "latest-version": latestRev.getVersion(), |
| 175 | }) |
| 176 | continue |
| 177 | } else { |
| 178 | log.Debugw("watch-revision-is-newer", log.Fields{ |
| 179 | "watch": latestRev.GetName(), |
| 180 | "watch-version": event.Version, |
| 181 | "latest-version": latestRev.getVersion(), |
| 182 | }) |
| 183 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 184 | |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 185 | data := reflect.New(reflect.TypeOf(latestRev.GetData()).Elem()) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 186 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 187 | if err := proto.Unmarshal(event.Value.([]byte), data.Interface().(proto.Message)); err != nil { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 188 | log.Errorw("failed-to-unmarshal-watch-data", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName(), "error": err}) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 189 | } else { |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 190 | log.Debugw("un-marshaled-watch-data", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName(), "data": data.Interface()}) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 191 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 192 | var pathLock string |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 193 | var blobs map[string]*kvstore.KVPair |
| 194 | |
| 195 | // The watch reported new persistence data. |
| 196 | // Construct an object that will be used to update the memory |
| 197 | blobs = make(map[string]*kvstore.KVPair) |
| 198 | key, _ := kvstore.ToString(event.Key) |
| 199 | blobs[key] = &kvstore.KVPair{ |
| 200 | Key: key, |
| 201 | Value: event.Value, |
| 202 | Session: "", |
| 203 | Lease: 0, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 204 | Version: event.Version, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 207 | if latestRev.GetNode().GetProxy() != nil { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 208 | // |
| 209 | // If a proxy exists for this revision, use it to lock access to the path |
| 210 | // and prevent simultaneous updates to the object in memory |
| 211 | // |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 212 | |
| 213 | //If the proxy already has a request in progress, then there is no need to process the watch |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 214 | if latestRev.GetNode().GetProxy().GetOperation() != PROXY_NONE { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 215 | log.Debugw("operation-in-progress", log.Fields{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 216 | "key": latestRev.GetHash(), |
| 217 | "path": latestRev.GetNode().GetProxy().getFullPath(), |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 218 | "operation": latestRev.GetNode().GetProxy().operation.String(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 219 | }) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 220 | continue |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 221 | } |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 222 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 223 | pathLock, _ = latestRev.GetNode().GetProxy().parseForControlledPath(latestRev.GetNode().GetProxy().getFullPath()) |
| 224 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 225 | // Reserve the path to prevent others to modify while we reload from persistence |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 226 | latestRev.GetNode().GetProxy().GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL) |
| 227 | latestRev.GetNode().GetProxy().SetOperation(PROXY_WATCH) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 228 | |
| 229 | // Load changes and apply to memory |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 230 | latestRev.LoadFromPersistence(context.Background(), latestRev.GetName(), "", blobs) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 231 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 232 | // Release path |
| 233 | latestRev.GetNode().GetProxy().GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_") |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 234 | |
| 235 | } else { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 236 | // This block should be reached only if coming from a non-proxied request |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 237 | log.Debugw("revision-with-no-proxy", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName()}) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 238 | |
| 239 | // Load changes and apply to memory |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 240 | latestRev.LoadFromPersistence(context.Background(), latestRev.GetName(), "", blobs) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 241 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | default: |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 245 | log.Debugw("unhandled-event", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName(), "type": event.EventType}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 250 | Watches().Cache.Delete(pr.GetName() + "-" + pr.GetHash()) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 251 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 252 | log.Debugw("exiting-watch", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 253 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 254 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 255 | // UpdateData modifies the information in the data model and saves it in the persistent storage |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 256 | func (pr *PersistedRevision) UpdateData(ctx context.Context, data interface{}, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 257 | log.Debugw("updating-persisted-data", log.Fields{"hash": pr.GetHash()}) |
| 258 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 259 | newNPR := pr.Revision.UpdateData(ctx, data, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 260 | |
| 261 | newPR := &PersistedRevision{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 262 | Revision: newNPR, |
| 263 | Compress: pr.Compress, |
| 264 | kvStore: pr.kvStore, |
| 265 | events: pr.events, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 266 | Version: pr.getVersion(), |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 267 | isWatched: pr.isWatched, |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | if newPR.GetHash() != pr.GetHash() { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 271 | newPR.isStored = false |
| 272 | pr.Drop(branch.Txid, false) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 273 | pr.Drop(branch.Txid, false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 274 | } else { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 275 | newPR.isStored = true |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 278 | return newPR |
| 279 | } |
| 280 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 281 | // UpdateChildren modifies the children of a revision and of a specific component and saves it in the persistent storage |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 282 | func (pr *PersistedRevision) UpdateChildren(ctx context.Context, name string, children []Revision, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 283 | log.Debugw("updating-persisted-children", log.Fields{"hash": pr.GetHash()}) |
| 284 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 285 | newNPR := pr.Revision.UpdateChildren(ctx, name, children, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 286 | |
| 287 | newPR := &PersistedRevision{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 288 | Revision: newNPR, |
| 289 | Compress: pr.Compress, |
| 290 | kvStore: pr.kvStore, |
| 291 | events: pr.events, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 292 | Version: pr.getVersion(), |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 293 | isWatched: pr.isWatched, |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if newPR.GetHash() != pr.GetHash() { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 297 | newPR.isStored = false |
| 298 | pr.Drop(branch.Txid, false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 299 | } else { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 300 | newPR.isStored = true |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 303 | return newPR |
| 304 | } |
| 305 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 306 | // 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] | 307 | func (pr *PersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 308 | log.Debugw("updating-all-persisted-children", log.Fields{"hash": pr.GetHash()}) |
| 309 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 310 | newNPR := pr.Revision.UpdateAllChildren(children, branch) |
| 311 | |
| 312 | newPR := &PersistedRevision{ |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 313 | Revision: newNPR, |
| 314 | Compress: pr.Compress, |
| 315 | kvStore: pr.kvStore, |
| 316 | events: pr.events, |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 317 | Version: pr.getVersion(), |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 318 | isWatched: pr.isWatched, |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | if newPR.GetHash() != pr.GetHash() { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 322 | newPR.isStored = false |
| 323 | pr.Drop(branch.Txid, false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 324 | } else { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 325 | newPR.isStored = true |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 328 | return newPR |
| 329 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 330 | |
| 331 | // Drop takes care of eliminating a revision hash that is no longer needed |
| 332 | // and its associated config when required |
| 333 | func (pr *PersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 334 | pr.Revision.Drop(txid, includeConfig) |
| 335 | } |
| 336 | |
| 337 | // Drop takes care of eliminating a revision hash that is no longer needed |
| 338 | // and its associated config when required |
| 339 | func (pr *PersistedRevision) StorageDrop(txid string, includeConfig bool) { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 340 | log.Debugw("dropping-revision", log.Fields{"txid": txid, "hash": pr.GetHash(), "config-hash": pr.GetConfig().Hash}) |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 341 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 342 | pr.mutex.Lock() |
| 343 | defer pr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 344 | if pr.kvStore != nil && txid == "" { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 345 | if pr.isStored { |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 346 | if pr.isWatched { |
| 347 | pr.kvStore.DeleteWatch(pr.GetName(), pr.events) |
| 348 | pr.isWatched = false |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 349 | } |
| 350 | |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 351 | if err := pr.kvStore.Delete(pr.GetName()); err != nil { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 352 | log.Errorw("failed-to-remove-revision", log.Fields{"hash": pr.GetHash(), "error": err.Error()}) |
| 353 | } else { |
| 354 | pr.isStored = false |
| 355 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 358 | } else { |
| 359 | if includeConfig { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 360 | 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] | 361 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 362 | 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] | 363 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 364 | |
| 365 | pr.Revision.Drop(txid, includeConfig) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 366 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 367 | |
| 368 | // verifyPersistedEntry validates if the provided data is available or not in memory and applies updates as required |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 369 | func (pr *PersistedRevision) verifyPersistedEntry(ctx context.Context, data interface{}, typeName string, keyName string, |
| 370 | keyValue string, txid string, version int64) (response Revision) { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 371 | // Parent which holds the current node entry |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 372 | parent := pr.GetBranch().Node.GetRoot() |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 373 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 374 | // Get a copy of the parent's children |
| 375 | children := make([]Revision, len(parent.GetBranch(NONE).Latest.GetChildren(typeName))) |
| 376 | copy(children, parent.GetBranch(NONE).Latest.GetChildren(typeName)) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 377 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 378 | // Verify if a child with the provided key value can be found |
| 379 | if childIdx, childRev := pr.GetNode().findRevByKey(children, keyName, keyValue); childRev != nil { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 380 | // A child matching the provided key exists in memory |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 381 | // Verify if the data differs from what was retrieved from persistence |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 382 | // Also check if we are treating a newer revision of the data or not |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 383 | if childRev.GetData().(proto.Message).String() != data.(proto.Message).String() && childRev.getVersion() < version { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 384 | log.Debugw("revision-data-is-different", log.Fields{ |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 385 | "key": childRev.GetHash(), |
| 386 | "name": childRev.GetName(), |
| 387 | "data": childRev.GetData(), |
| 388 | "version": childRev.getVersion(), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 389 | }) |
| 390 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 391 | // |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 392 | // Data has changed; replace the child entry and update the parent revision |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 393 | // |
| 394 | |
| 395 | // BEGIN Lock child -- prevent any incoming changes |
| 396 | childRev.GetBranch().LatestLock.Lock() |
| 397 | |
| 398 | // Update child |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 399 | updatedChildRev := childRev.UpdateData(ctx, data, childRev.GetBranch()) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 400 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 401 | updatedChildRev.GetNode().SetProxy(childRev.GetNode().GetProxy()) |
| 402 | updatedChildRev.SetupWatch(updatedChildRev.GetName()) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 403 | updatedChildRev.SetLastUpdate() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 404 | updatedChildRev.(*PersistedRevision).setVersion(version) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 405 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 406 | // Update cache |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 407 | GetRevCache().Set(updatedChildRev.GetName(), updatedChildRev) |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 408 | childRev.Drop(txid, false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 409 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 410 | childRev.GetBranch().LatestLock.Unlock() |
| 411 | // END lock child |
| 412 | |
| 413 | // Update child entry |
| 414 | children[childIdx] = updatedChildRev |
| 415 | |
| 416 | // BEGIN lock parent -- Update parent |
| 417 | parent.GetBranch(NONE).LatestLock.Lock() |
| 418 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 419 | updatedRev := parent.GetBranch(NONE).GetLatest().UpdateChildren(ctx, typeName, children, parent.GetBranch(NONE)) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 420 | parent.GetBranch(NONE).Node.makeLatest(parent.GetBranch(NONE), updatedRev, nil) |
| 421 | |
| 422 | parent.GetBranch(NONE).LatestLock.Unlock() |
| 423 | // END lock parent |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 424 | |
| 425 | // Drop the previous child revision |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 426 | parent.GetBranch(NONE).Latest.ChildDrop(typeName, childRev.GetHash()) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 427 | |
| 428 | if updatedChildRev != nil { |
| 429 | log.Debugw("verify-persisted-entry--adding-child", log.Fields{ |
| 430 | "key": updatedChildRev.GetHash(), |
| 431 | "name": updatedChildRev.GetName(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 432 | "data": updatedChildRev.GetData(), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 433 | }) |
| 434 | response = updatedChildRev |
| 435 | } |
| 436 | } else { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 437 | if childRev != nil { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 438 | log.Debugw("keeping-revision-data", log.Fields{ |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 439 | "key": childRev.GetHash(), |
| 440 | "name": childRev.GetName(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 441 | "data": childRev.GetData(), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 442 | }) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 443 | |
| 444 | // Update timestamp to reflect when it was last read and to reset tracked timeout |
| 445 | childRev.SetLastUpdate() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 446 | if childRev.getVersion() < version { |
| 447 | childRev.(*PersistedRevision).setVersion(version) |
| 448 | } |
| 449 | GetRevCache().Set(childRev.GetName(), childRev) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 450 | response = childRev |
| 451 | } |
| 452 | } |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 453 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 454 | } else { |
| 455 | // There is no available child with that key value. |
| 456 | // Create a new child and update the parent revision. |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 457 | log.Debugw("no-such-revision-entry", log.Fields{ |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 458 | "key": keyValue, |
| 459 | "name": typeName, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 460 | "data": data, |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 461 | }) |
| 462 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 463 | // BEGIN child lock |
| 464 | pr.GetBranch().LatestLock.Lock() |
| 465 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 466 | // Construct a new child node with the retrieved persistence data |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 467 | childRev = pr.GetBranch().Node.MakeNode(data, txid).Latest(txid) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 468 | |
| 469 | // We need to start watching this entry for future changes |
| 470 | childRev.SetName(typeName + "/" + keyValue) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 471 | childRev.SetupWatch(childRev.GetName()) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 472 | childRev.(*PersistedRevision).setVersion(version) |
| 473 | |
| 474 | // Add entry to cache |
| 475 | GetRevCache().Set(childRev.GetName(), childRev) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 476 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 477 | pr.GetBranch().LatestLock.Unlock() |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 478 | // END child lock |
| 479 | |
| 480 | // |
| 481 | // Add the child to the parent revision |
| 482 | // |
| 483 | |
| 484 | // BEGIN parent lock |
| 485 | parent.GetBranch(NONE).LatestLock.Lock() |
| 486 | children = append(children, childRev) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 487 | updatedRev := parent.GetBranch(NONE).GetLatest().UpdateChildren(ctx, typeName, children, parent.GetBranch(NONE)) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 488 | updatedRev.GetNode().SetProxy(parent.GetBranch(NONE).Node.GetProxy()) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 489 | parent.GetBranch(NONE).Node.makeLatest(parent.GetBranch(NONE), updatedRev, nil) |
| 490 | parent.GetBranch(NONE).LatestLock.Unlock() |
| 491 | // END parent lock |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 492 | |
| 493 | // Child entry is valid and can be included in the response object |
| 494 | if childRev != nil { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 495 | log.Debugw("adding-revision-to-response", log.Fields{ |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 496 | "key": childRev.GetHash(), |
| 497 | "name": childRev.GetName(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 498 | "data": childRev.GetData(), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 499 | }) |
| 500 | response = childRev |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return response |
| 505 | } |
| 506 | |
| 507 | // LoadFromPersistence retrieves data from kv store at the specified location and refreshes the memory |
| 508 | // by adding missing entries, updating changed entries and ignoring unchanged ones |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 509 | func (pr *PersistedRevision) LoadFromPersistence(ctx context.Context, path string, txid string, blobs map[string]*kvstore.KVPair) []Revision { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 510 | pr.mutex.Lock() |
| 511 | defer pr.mutex.Unlock() |
| 512 | |
| 513 | log.Debugw("loading-from-persistence", log.Fields{"path": path, "txid": txid}) |
| 514 | |
| 515 | var response []Revision |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 516 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 517 | for strings.HasPrefix(path, "/") { |
| 518 | path = path[1:] |
| 519 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 520 | |
| 521 | if pr.kvStore != nil && path != "" { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 522 | if blobs == nil || len(blobs) == 0 { |
| 523 | log.Debugw("retrieve-from-kv", log.Fields{"path": path, "txid": txid}) |
| 524 | blobs, _ = pr.kvStore.List(path) |
| 525 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 526 | |
| 527 | partition := strings.SplitN(path, "/", 2) |
| 528 | name := partition[0] |
| 529 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 530 | var nodeType interface{} |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 531 | if len(partition) < 2 { |
| 532 | path = "" |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 533 | nodeType = pr.GetBranch().Node.Type |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 534 | } else { |
| 535 | path = partition[1] |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 536 | nodeType = pr.GetBranch().Node.GetRoot().Type |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 537 | } |
| 538 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 539 | field := ChildrenFields(nodeType)[name] |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 540 | |
| 541 | if field != nil && field.IsContainer { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 542 | log.Debugw("parsing-data-blobs", log.Fields{ |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 543 | "path": path, |
| 544 | "name": name, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 545 | "size": len(blobs), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 546 | }) |
| 547 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 548 | for _, blob := range blobs { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 549 | output := blob.Value.([]byte) |
| 550 | |
| 551 | data := reflect.New(field.ClassType.Elem()) |
| 552 | |
| 553 | if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 554 | log.Errorw("failed-to-unmarshal", log.Fields{ |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 555 | "path": path, |
| 556 | "txid": txid, |
| 557 | "error": err, |
| 558 | }) |
| 559 | } else if path == "" { |
| 560 | if field.Key != "" { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 561 | log.Debugw("no-path-with-container-key", log.Fields{ |
| 562 | "path": path, |
| 563 | "txid": txid, |
| 564 | "data": data.Interface(), |
| 565 | }) |
| 566 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 567 | // Retrieve the key identifier value from the data structure |
| 568 | // based on the field's key attribute |
| 569 | _, key := GetAttributeValue(data.Interface(), field.Key, 0) |
| 570 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 571 | if entry := pr.verifyPersistedEntry(ctx, data.Interface(), name, field.Key, key.String(), txid, blob.Version); entry != nil { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 572 | response = append(response, entry) |
| 573 | } |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 574 | } else { |
| 575 | log.Debugw("path-with-no-container-key", log.Fields{ |
| 576 | "path": path, |
| 577 | "txid": txid, |
| 578 | "data": data.Interface(), |
| 579 | }) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | } else if field.Key != "" { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 583 | log.Debugw("path-with-container-key", log.Fields{ |
| 584 | "path": path, |
| 585 | "txid": txid, |
| 586 | "data": data.Interface(), |
| 587 | }) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 588 | // The request is for a specific entry/id |
| 589 | partition := strings.SplitN(path, "/", 2) |
| 590 | key := partition[0] |
| 591 | if len(partition) < 2 { |
| 592 | path = "" |
| 593 | } else { |
| 594 | path = partition[1] |
| 595 | } |
| 596 | keyValue := field.KeyFromStr(key) |
| 597 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 598 | if entry := pr.verifyPersistedEntry(ctx, data.Interface(), name, field.Key, keyValue.(string), txid, blob.Version); entry != nil { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 599 | response = append(response, entry) |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 604 | log.Debugw("no-more-data-blobs", log.Fields{"path": path, "name": name}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 605 | } else { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 606 | log.Debugw("cannot-process-field", log.Fields{ |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 607 | "type": pr.GetBranch().Node.Type, |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 608 | "name": name, |
| 609 | }) |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | return response |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 614 | } |