blob: 226fc3c4ab79849cd2f87885b4fe81003ee4f586 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
19import (
20 "bytes"
21 "compress/gzip"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040022 "github.com/golang/protobuf/proto"
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040023 "github.com/opencord/voltha-go/common/log"
Stephane Barbariee0a4c792019-01-16 11:26:29 -050024 "github.com/opencord/voltha-go/db/kvstore"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040025 "reflect"
Stephane Barbarie1ab43272018-12-08 21:42:13 -050026 "strings"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050027 "sync"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040028)
29
Stephane Barbariedc5022d2018-11-19 15:21:44 -050030// PersistedRevision holds information of revision meant to be saved in a persistent storage
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040031type PersistedRevision struct {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040032 Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040033 Compress bool
Stephane Barbariee0a4c792019-01-16 11:26:29 -050034
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040035 events chan *kvstore.Event
36 kvStore *Backend
37 mutex sync.RWMutex
Stephane Barbarie3cb01222019-01-16 17:15:56 -050038 isStored bool
39 isWatched bool
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040040}
41
Stephane Barbarie40fd3b22019-04-23 21:50:47 -040042type watchCache struct {
43 Cache sync.Map
44}
45
46var watchCacheInstance *watchCache
47var watchCacheOne sync.Once
48
49func Watches() *watchCache {
50 watchCacheOne.Do(func() {
51 watchCacheInstance = &watchCache{Cache: sync.Map{}}
52 })
53 return watchCacheInstance
54}
55
Stephane Barbariedc5022d2018-11-19 15:21:44 -050056// NewPersistedRevision creates a new instance of a PersistentRevision structure
Stephane Barbarieec0919b2018-09-05 14:14:29 -040057func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040058 pr := &PersistedRevision{}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050059 pr.kvStore = branch.Node.GetRoot().KvStore
60 pr.Revision = NewNonPersistedRevision(nil, branch, data, children)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040061 return pr
62}
63
Stephane Barbariedc5022d2018-11-19 15:21:44 -050064// Finalize is responsible of saving the revision in the persistent storage
Stephane Barbarie1ab43272018-12-08 21:42:13 -050065func (pr *PersistedRevision) Finalize(skipOnExist bool) {
66 pr.store(skipOnExist)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040067}
68
Stephane Barbarie1ab43272018-12-08 21:42:13 -050069func (pr *PersistedRevision) store(skipOnExist bool) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040070 if pr.GetBranch().Txid != "" {
71 return
72 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040073
Stephane Barbarie7512fc82019-05-07 12:25:46 -040074 log.Debugw("ready-to-store-revision", log.Fields{"hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetData()})
Stephane Barbarie1ab43272018-12-08 21:42:13 -050075
Stephane Barbarieec0919b2018-09-05 14:14:29 -040076 if blob, err := proto.Marshal(pr.GetConfig().Data.(proto.Message)); err != nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040077 // TODO report error
78 } else {
79 if pr.Compress {
80 var b bytes.Buffer
81 w := gzip.NewWriter(&b)
82 w.Write(blob)
83 w.Close()
84 blob = b.Bytes()
85 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -050086
Stephane Barbarief7fc1782019-03-28 22:33:41 -040087 if err := pr.kvStore.Put(pr.GetName(), blob); err != nil {
Stephane Barbarie7512fc82019-05-07 12:25:46 -040088 log.Warnw("problem-storing-revision", log.Fields{"error": err, "hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetConfig().Data})
Stephane Barbariedc5022d2018-11-19 15:21:44 -050089 } else {
Stephane Barbarie7512fc82019-05-07 12:25:46 -040090 log.Debugw("storing-revision", log.Fields{"hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetConfig().Data})
Stephane Barbarie3cb01222019-01-16 17:15:56 -050091 pr.isStored = true
Stephane Barbariedc5022d2018-11-19 15:21:44 -050092 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040093 }
94}
95
Stephane Barbariee0a4c792019-01-16 11:26:29 -050096func (pr *PersistedRevision) SetupWatch(key string) {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -040097 if key == "" {
Stephane Barbarie7512fc82019-05-07 12:25:46 -040098 log.Debugw("ignoring-watch", log.Fields{"key": key, "revision-hash": pr.GetHash()})
Stephane Barbarie40fd3b22019-04-23 21:50:47 -040099 return
100 }
101
102 if _, exists := Watches().Cache.LoadOrStore(key+"-"+pr.GetHash(), struct{}{}); exists {
103 return
104 }
105
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500106 if pr.events == nil {
107 pr.events = make(chan *kvstore.Event)
108
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400109 log.Debugw("setting-watch-channel", log.Fields{"key": key, "revision-hash": pr.GetHash()})
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500110
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400111 pr.SetName(key)
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500112 pr.events = pr.kvStore.CreateWatch(key)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400113 }
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500114
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400115 if !pr.isWatched {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500116 pr.isWatched = true
117
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400118 log.Debugw("setting-watch-routine", log.Fields{"key": key, "revision-hash": pr.GetHash()})
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400119
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500120 // Start watching
121 go pr.startWatching()
122 }
123}
124
125func (pr *PersistedRevision) startWatching() {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400126 log.Debugw("starting-watch", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()})
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500127
128StopWatchLoop:
129 for {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400130 if pr.IsDiscarded() {
131 break StopWatchLoop
132 }
133
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500134 select {
135 case event, ok := <-pr.events:
136 if !ok {
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400137 log.Errorw("event-channel-failure: stopping watch loop", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()})
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500138 break StopWatchLoop
139 }
140
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400141 log.Debugw("received-event", log.Fields{"type": event.EventType, "watch": pr.GetName()})
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500142
143 switch event.EventType {
144 case kvstore.DELETE:
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400145 log.Debugw("delete-from-memory", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()})
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500146 pr.Revision.Drop("", true)
147 break StopWatchLoop
148
149 case kvstore.PUT:
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400150 log.Debugw("update-in-memory", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()})
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500151
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400152 data := reflect.New(reflect.TypeOf(pr.GetData()).Elem())
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500153
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400154 if err := proto.Unmarshal(event.Value.([]byte), data.Interface().(proto.Message)); err != nil {
155 log.Errorw("failed-to-unmarshal-watch-data", log.Fields{"key": pr.GetHash(), "watch": pr.GetName(), "error": err})
156 } else {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400157 log.Debugw("un-marshaled-watch-data", log.Fields{"key": pr.GetHash(), "watch": pr.GetName(), "data": data.Interface()})
158
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400159 var pathLock string
160 var pac *proxyAccessControl
161 var blobs map[string]*kvstore.KVPair
162
163 // The watch reported new persistence data.
164 // Construct an object that will be used to update the memory
165 blobs = make(map[string]*kvstore.KVPair)
166 key, _ := kvstore.ToString(event.Key)
167 blobs[key] = &kvstore.KVPair{
168 Key: key,
169 Value: event.Value,
170 Session: "",
171 Lease: 0,
172 }
173
174 if pr.GetNode().GetProxy() != nil {
175 //
176 // If a proxy exists for this revision, use it to lock access to the path
177 // and prevent simultaneous updates to the object in memory
178 //
179 pathLock, _ = pr.GetNode().GetProxy().parseForControlledPath(pr.GetNode().GetProxy().getFullPath())
180
181 //If the proxy already has a request in progress, then there is no need to process the watch
182 log.Debugw("checking-if-path-is-locked", log.Fields{"key": pr.GetHash(), "pathLock": pathLock})
183 if PAC().IsReserved(pathLock) {
184 log.Debugw("operation-in-progress", log.Fields{
185 "key": pr.GetHash(),
186 "path": pr.GetNode().GetProxy().getFullPath(),
Stephane Barbarie802aca42019-05-21 12:19:28 -0400187 "operation": pr.GetNode().GetProxy().Operation.String(),
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400188 })
189
Stephane Barbarie802aca42019-05-21 12:19:28 -0400190 // Identify the operation type and determine if the watch event should be applied or not.
191 switch pr.GetNode().GetProxy().Operation {
192 case PROXY_REMOVE:
193 fallthrough
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400194
Stephane Barbarie802aca42019-05-21 12:19:28 -0400195 case PROXY_ADD:
196 fallthrough
197
198 case PROXY_UPDATE:
199 // We will need to reload once the operation completes.
200 // Therefore, the data of the current event is most likely out-dated
201 // and should be ignored
202 log.Debugw("ignore-watch-event", log.Fields{
203 "key": pr.GetHash(),
204 "path": pr.GetNode().GetProxy().getFullPath(),
205 "operation": pr.GetNode().GetProxy().Operation.String(),
206 })
207
208 continue
209
210 case PROXY_CREATE:
211 fallthrough
212
213 case PROXY_LIST:
214 fallthrough
215
216 case PROXY_GET:
217 fallthrough
218
219 default:
220 log.Debugw("process-watch-event", log.Fields{
221 "key": pr.GetHash(),
222 "path": pr.GetNode().GetProxy().getFullPath(),
223 "operation": pr.GetNode().GetProxy().Operation.String(),
224 })
225 }
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400226 }
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400227
228 // Reserve the path to prevent others to modify while we reload from persistence
229 log.Debugw("reserve-and-lock-path", log.Fields{"key": pr.GetHash(), "path": pathLock})
230 pac = PAC().ReservePath(pr.GetNode().GetProxy().getFullPath(), pr.GetNode().GetProxy(), pathLock)
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400231 pac.lock()
Stephane Barbarie802aca42019-05-21 12:19:28 -0400232 pr.GetNode().GetProxy().Operation = PROXY_UPDATE
233 pac.SetProxy(pr.GetNode().GetProxy())
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400234
235 // Load changes and apply to memory
236 pr.LoadFromPersistence(pr.GetName(), "", blobs)
237
238 log.Debugw("release-and-unlock-path", log.Fields{"key": pr.GetHash(), "path": pathLock})
Stephane Barbarie802aca42019-05-21 12:19:28 -0400239 pac.getProxy().Operation = PROXY_GET
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400240 pac.unlock()
241 PAC().ReleasePath(pathLock)
242
243 } else {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400244 // This block should be reached only if coming from a non-proxied request
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400245 log.Debugw("revision-with-no-proxy", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()})
246
247 // Load changes and apply to memory
248 pr.LoadFromPersistence(pr.GetName(), "", blobs)
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500249 }
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500250 }
251
252 default:
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400253 log.Debugw("unhandled-event", log.Fields{"key": pr.GetHash(), "watch": pr.GetName(), "type": event.EventType})
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500254 }
255 }
256 }
257
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400258 Watches().Cache.Delete(pr.GetName() + "-" + pr.GetHash())
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500259
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400260 log.Debugw("exiting-watch", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400261}
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400262
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500263// UpdateData modifies the information in the data model and saves it in the persistent storage
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400264func (pr *PersistedRevision) UpdateData(data interface{}, branch *Branch) Revision {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500265 log.Debugw("updating-persisted-data", log.Fields{"hash": pr.GetHash()})
266
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400267 newNPR := pr.Revision.UpdateData(data, branch)
268
269 newPR := &PersistedRevision{
270 Revision: newNPR,
271 Compress: pr.Compress,
khenaidoob9203542018-09-17 22:56:37 -0400272 kvStore: pr.kvStore,
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400273 events: pr.events,
274 }
275
276 if newPR.GetHash() != pr.GetHash() {
277 newPR.isWatched = false
278 newPR.isStored = false
279 pr.Drop(branch.Txid, false)
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400280 pr.Drop(branch.Txid, false)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400281 } else {
282 newPR.isWatched = true
283 newPR.isStored = true
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400284 }
285
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400286 return newPR
287}
288
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500289// UpdateChildren modifies the children of a revision and of a specific component and saves it in the persistent storage
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400290func (pr *PersistedRevision) UpdateChildren(name string, children []Revision,
291 branch *Branch) Revision {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500292 log.Debugw("updating-persisted-children", log.Fields{"hash": pr.GetHash()})
293
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400294 newNPR := pr.Revision.UpdateChildren(name, children, branch)
295
296 newPR := &PersistedRevision{
297 Revision: newNPR,
298 Compress: pr.Compress,
khenaidoob9203542018-09-17 22:56:37 -0400299 kvStore: pr.kvStore,
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400300 events: pr.events,
301 }
302
303 if newPR.GetHash() != pr.GetHash() {
304 newPR.isWatched = false
305 newPR.isStored = false
306 pr.Drop(branch.Txid, false)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400307 } else {
308 newPR.isWatched = true
309 newPR.isStored = true
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400310 }
311
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400312 return newPR
313}
314
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500315// UpdateAllChildren modifies the children for all components of a revision and saves it in the peristent storage
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400316func (pr *PersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500317 log.Debugw("updating-all-persisted-children", log.Fields{"hash": pr.GetHash()})
318
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400319 newNPR := pr.Revision.UpdateAllChildren(children, branch)
320
321 newPR := &PersistedRevision{
322 Revision: newNPR,
323 Compress: pr.Compress,
khenaidoob9203542018-09-17 22:56:37 -0400324 kvStore: pr.kvStore,
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400325 events: pr.events,
326 }
327
328 if newPR.GetHash() != pr.GetHash() {
329 newPR.isWatched = false
330 newPR.isStored = false
331 pr.Drop(branch.Txid, false)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400332 } else {
333 newPR.isWatched = true
334 newPR.isStored = true
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400335 }
336
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400337 return newPR
338}
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400339
340// Drop takes care of eliminating a revision hash that is no longer needed
341// and its associated config when required
342func (pr *PersistedRevision) Drop(txid string, includeConfig bool) {
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400343 pr.Revision.Drop(txid, includeConfig)
344}
345
346// Drop takes care of eliminating a revision hash that is no longer needed
347// and its associated config when required
348func (pr *PersistedRevision) StorageDrop(txid string, includeConfig bool) {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500349 log.Debugw("dropping-revision",
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400350 log.Fields{"txid": txid, "hash": pr.GetHash(), "config-hash": pr.GetConfig().Hash})
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500351
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500352 pr.mutex.Lock()
353 defer pr.mutex.Unlock()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400354 if pr.kvStore != nil && txid == "" {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500355 if pr.isStored {
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400356 if pr.isWatched {
357 pr.kvStore.DeleteWatch(pr.GetName(), pr.events)
358 pr.isWatched = false
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500359 }
360
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400361 if err := pr.kvStore.Delete(pr.GetName()); err != nil {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500362 log.Errorw("failed-to-remove-revision", log.Fields{"hash": pr.GetHash(), "error": err.Error()})
363 } else {
364 pr.isStored = false
365 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400366 }
367
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400368 } else {
369 if includeConfig {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500370 log.Debugw("attempted-to-remove-transacted-revision-config", log.Fields{"hash": pr.GetConfig().Hash, "txid": txid})
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400371 }
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500372 log.Debugw("attempted-to-remove-transacted-revision", log.Fields{"hash": pr.GetHash(), "txid": txid})
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400373 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500374
375 pr.Revision.Drop(txid, includeConfig)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400376}
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400377
378// verifyPersistedEntry validates if the provided data is available or not in memory and applies updates as required
379func (pr *PersistedRevision) verifyPersistedEntry(data interface{}, typeName string, keyName string, keyValue string, txid string) (response Revision) {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400380 // Parent which holds the current node entry
381 parent := pr.GetBranch().Node.Root
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400382
Stephane Barbarie802aca42019-05-21 12:19:28 -0400383 // Get a copy of the parent's children
384 children := make([]Revision, len(parent.GetBranch(NONE).Latest.GetChildren(typeName)))
385 copy(children, parent.GetBranch(NONE).Latest.GetChildren(typeName))
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400386
Stephane Barbarie802aca42019-05-21 12:19:28 -0400387 // Verify if a child with the provided key value can be found
388 if childIdx, childRev := pr.GetNode().findRevByKey(children, keyName, keyValue); childRev != nil {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400389 // A child matching the provided key exists in memory
Stephane Barbarie802aca42019-05-21 12:19:28 -0400390 // Verify if the data differs from what was retrieved from persistence
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400391 if childRev.GetData().(proto.Message).String() != data.(proto.Message).String() {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400392 log.Debugw("revision-data-is-different", log.Fields{
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400393 "key": childRev.GetHash(),
394 "name": childRev.GetName(),
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400395 "data": childRev.GetData(),
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400396 })
397
Stephane Barbarie802aca42019-05-21 12:19:28 -0400398 //
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400399 // Data has changed; replace the child entry and update the parent revision
Stephane Barbarie802aca42019-05-21 12:19:28 -0400400 //
401
402 // BEGIN Lock child -- prevent any incoming changes
403 childRev.GetBranch().LatestLock.Lock()
404
405 // Update child
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400406 updatedChildRev := childRev.UpdateData(data, childRev.GetBranch())
Stephane Barbarie802aca42019-05-21 12:19:28 -0400407
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400408 updatedChildRev.GetNode().SetProxy(childRev.GetNode().GetProxy())
409 updatedChildRev.SetupWatch(updatedChildRev.GetName())
Stephane Barbarie802aca42019-05-21 12:19:28 -0400410 childRev.Drop(txid, false)
411 updatedChildRev.SetLastUpdate()
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400412
Stephane Barbarie802aca42019-05-21 12:19:28 -0400413 // Update cache
414 GetRevCache().Cache.Store(updatedChildRev.GetName(), updatedChildRev)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400415
Stephane Barbarie802aca42019-05-21 12:19:28 -0400416 childRev.GetBranch().LatestLock.Unlock()
417 // END lock child
418
419 // Update child entry
420 children[childIdx] = updatedChildRev
421
422 // BEGIN lock parent -- Update parent
423 parent.GetBranch(NONE).LatestLock.Lock()
424
425 updatedRev := parent.GetBranch(NONE).Latest.UpdateChildren(typeName, children, parent.GetBranch(NONE))
426 parent.GetBranch(NONE).Node.makeLatest(parent.GetBranch(NONE), updatedRev, nil)
427
428 parent.GetBranch(NONE).LatestLock.Unlock()
429 // END lock parent
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400430
431 // Drop the previous child revision
Stephane Barbarie802aca42019-05-21 12:19:28 -0400432 parent.GetBranch(NONE).Latest.ChildDrop(typeName, childRev.GetHash())
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400433
434 if updatedChildRev != nil {
435 log.Debugw("verify-persisted-entry--adding-child", log.Fields{
436 "key": updatedChildRev.GetHash(),
437 "name": updatedChildRev.GetName(),
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400438 "data": updatedChildRev.GetData(),
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400439 })
440 response = updatedChildRev
441 }
442 } else {
443 // Data is the same. Continue to the next entry
Stephane Barbarie802aca42019-05-21 12:19:28 -0400444 log.Debugw("same-revision-data", log.Fields{
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400445 "key": childRev.GetHash(),
446 "name": childRev.GetName(),
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400447 "data": childRev.GetData(),
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400448 })
449 if childRev != nil {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400450 log.Debugw("keeping-same-revision-data", log.Fields{
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400451 "key": childRev.GetHash(),
452 "name": childRev.GetName(),
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400453 "data": childRev.GetData(),
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400454 })
Stephane Barbarie802aca42019-05-21 12:19:28 -0400455
456 // Update timestamp to reflect when it was last read and to reset tracked timeout
457 childRev.SetLastUpdate()
458 GetRevCache().Cache.Store(childRev.GetName(), childRev)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400459 response = childRev
460 }
461 }
462 } else {
463 // There is no available child with that key value.
464 // Create a new child and update the parent revision.
Stephane Barbarie802aca42019-05-21 12:19:28 -0400465 log.Debugw("no-such-revision-entry", log.Fields{
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400466 "key": keyValue,
467 "name": typeName,
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400468 "data": data,
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400469 })
470
Stephane Barbarie802aca42019-05-21 12:19:28 -0400471 // BEGIN child lock
472 pr.GetBranch().LatestLock.Lock()
473
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400474 // Construct a new child node with the retrieved persistence data
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400475 childRev = pr.GetBranch().Node.MakeNode(data, txid).Latest(txid)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400476
477 // We need to start watching this entry for future changes
478 childRev.SetName(typeName + "/" + keyValue)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400479 childRev.SetupWatch(childRev.GetName())
480
Stephane Barbarie802aca42019-05-21 12:19:28 -0400481 pr.GetBranch().Node.makeLatest(pr.GetBranch(), childRev, nil)
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400482 pr.GetBranch().LatestLock.Unlock()
Stephane Barbarie802aca42019-05-21 12:19:28 -0400483 // END child lock
484
485 //
486 // Add the child to the parent revision
487 //
488
489 // BEGIN parent lock
490 parent.GetBranch(NONE).LatestLock.Lock()
491 children = append(children, childRev)
492 updatedRev := parent.GetBranch(NONE).Latest.UpdateChildren(typeName, children, parent.GetBranch(NONE))
493 updatedRev.GetNode().SetProxy(parent.GetBranch(NONE).Node.GetProxy())
494
495 parent.GetBranch(NONE).Node.makeLatest(parent.GetBranch(NONE), updatedRev, nil)
496 parent.GetBranch(NONE).LatestLock.Unlock()
497 // END parent lock
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400498
499 // Child entry is valid and can be included in the response object
500 if childRev != nil {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400501 log.Debugw("adding-revision-to-response", log.Fields{
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400502 "key": childRev.GetHash(),
503 "name": childRev.GetName(),
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400504 "data": childRev.GetData(),
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400505 })
506 response = childRev
507 }
508 }
509
510 return response
511}
512
513// LoadFromPersistence retrieves data from kv store at the specified location and refreshes the memory
514// by adding missing entries, updating changed entries and ignoring unchanged ones
Stephane Barbarie802aca42019-05-21 12:19:28 -0400515func (pr *PersistedRevision) LoadFromPersistence(
516 path string, txid string, blobs map[string]*kvstore.KVPair) []Revision {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400517 pr.mutex.Lock()
518 defer pr.mutex.Unlock()
519
520 log.Debugw("loading-from-persistence", log.Fields{"path": path, "txid": txid})
521
522 var response []Revision
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400523
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400524 for strings.HasPrefix(path, "/") {
525 path = path[1:]
526 }
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400527
528 if pr.kvStore != nil && path != "" {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400529 if blobs == nil || len(blobs) == 0 {
530 log.Debugw("retrieve-from-kv", log.Fields{"path": path, "txid": txid})
531 blobs, _ = pr.kvStore.List(path)
532 }
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400533
534 partition := strings.SplitN(path, "/", 2)
535 name := partition[0]
536
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400537 var nodeType interface{}
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400538 if len(partition) < 2 {
539 path = ""
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400540 nodeType = pr.GetBranch().Node.Type
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400541 } else {
542 path = partition[1]
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400543 nodeType = pr.GetBranch().Node.Root.Type
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400544 }
545
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400546 field := ChildrenFields(nodeType)[name]
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400547
548 if field != nil && field.IsContainer {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400549 log.Debugw("parsing-data-blobs", log.Fields{
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400550 "path": path,
551 "name": name,
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400552 "size": len(blobs),
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400553 })
554
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400555 for _, blob := range blobs {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400556 output := blob.Value.([]byte)
557
558 data := reflect.New(field.ClassType.Elem())
559
560 if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400561 log.Errorw("failed-to-unmarshal", log.Fields{
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400562 "path": path,
563 "txid": txid,
564 "error": err,
565 })
566 } else if path == "" {
567 if field.Key != "" {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400568 log.Debugw("no-path-with-container-key", log.Fields{
569 "path": path,
570 "txid": txid,
571 "data": data.Interface(),
572 })
573
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400574 // Retrieve the key identifier value from the data structure
575 // based on the field's key attribute
576 _, key := GetAttributeValue(data.Interface(), field.Key, 0)
577
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400578 if entry := pr.verifyPersistedEntry(data.Interface(), name, field.Key, key.String(),
579 txid); entry != nil {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400580 response = append(response, entry)
581 }
Stephane Barbarie802aca42019-05-21 12:19:28 -0400582 } else {
583 log.Debugw("path-with-no-container-key", log.Fields{
584 "path": path,
585 "txid": txid,
586 "data": data.Interface(),
587 })
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400588 }
589
590 } else if field.Key != "" {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400591 log.Debugw("path-with-container-key", log.Fields{
592 "path": path,
593 "txid": txid,
594 "data": data.Interface(),
595 })
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400596 // The request is for a specific entry/id
597 partition := strings.SplitN(path, "/", 2)
598 key := partition[0]
599 if len(partition) < 2 {
600 path = ""
601 } else {
602 path = partition[1]
603 }
604 keyValue := field.KeyFromStr(key)
605
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400606 if entry := pr.verifyPersistedEntry(data.Interface(), name, field.Key, keyValue.(string),
607 txid); entry != nil {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400608 response = append(response, entry)
609 }
610 }
611 }
612
Stephane Barbarie802aca42019-05-21 12:19:28 -0400613 log.Debugw("no-more-data-blobs", log.Fields{"path": path, "name": name})
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400614 } else {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400615 log.Debugw("cannot-process-field", log.Fields{
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400616 "type": pr.GetBranch().Node.Type,
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400617 "name": name,
618 })
619 }
620 }
621
622 return response
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400623}