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