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