blob: c644e148841a1b7426f61ce769cf7ba5c5b51162 [file] [log] [blame]
Matt Jeanneretcab955f2019-04-10 15:45:57 -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 */
16
17package model
18
19import (
20 "bytes"
21 "compress/gzip"
Manikkaraj kb1d51442019-07-23 10:41:02 -040022 "context"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040023 "github.com/golang/protobuf/proto"
Manikkaraj kb1d51442019-07-23 10:41:02 -040024 "github.com/google/uuid"
Scott Bakerf8424cc2019-10-18 11:26:50 -070025 "github.com/opencord/voltha-lib-go/pkg/db/kvstore"
26 "github.com/opencord/voltha-lib-go/pkg/log"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040027 "reflect"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040028 "strings"
29 "sync"
30)
31
32// PersistedRevision holds information of revision meant to be saved in a persistent storage
33type PersistedRevision struct {
34 Revision
35 Compress bool
36
Manikkaraj kb1d51442019-07-23 10:41:02 -040037 events chan *kvstore.Event
38 kvStore *Backend
39 mutex sync.RWMutex
40 versionMutex sync.RWMutex
41 Version int64
42 isStored bool
43 isWatched bool
Matt Jeanneretcab955f2019-04-10 15:45:57 -040044}
45
Matt Jeanneret384d8c92019-05-06 14:27:31 -040046type watchCache struct {
47 Cache sync.Map
48}
49
50var watchCacheInstance *watchCache
51var watchCacheOne sync.Once
52
53func Watches() *watchCache {
54 watchCacheOne.Do(func() {
55 watchCacheInstance = &watchCache{Cache: sync.Map{}}
56 })
57 return watchCacheInstance
58}
59
Matt Jeanneretcab955f2019-04-10 15:45:57 -040060// NewPersistedRevision creates a new instance of a PersistentRevision structure
61func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision {
62 pr := &PersistedRevision{}
63 pr.kvStore = branch.Node.GetRoot().KvStore
Manikkaraj kb1d51442019-07-23 10:41:02 -040064 pr.Version = 1
Matt Jeanneretcab955f2019-04-10 15:45:57 -040065 pr.Revision = NewNonPersistedRevision(nil, branch, data, children)
66 return pr
67}
68
Manikkaraj kb1d51442019-07-23 10:41:02 -040069func (pr *PersistedRevision) getVersion() int64 {
70 pr.versionMutex.RLock()
71 defer pr.versionMutex.RUnlock()
72 return pr.Version
73}
74
75func (pr *PersistedRevision) setVersion(version int64) {
76 pr.versionMutex.Lock()
77 defer pr.versionMutex.Unlock()
78 pr.Version = version
79}
80
Matt Jeanneretcab955f2019-04-10 15:45:57 -040081// Finalize is responsible of saving the revision in the persistent storage
82func (pr *PersistedRevision) Finalize(skipOnExist bool) {
83 pr.store(skipOnExist)
84}
85
Matt Jeanneretcab955f2019-04-10 15:45:57 -040086func (pr *PersistedRevision) store(skipOnExist bool) {
87 if pr.GetBranch().Txid != "" {
88 return
89 }
90
manikkaraj k9eb6cac2019-05-09 12:32:03 -040091 log.Debugw("ready-to-store-revision", log.Fields{"hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetData()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -040092
Manikkaraj kb1d51442019-07-23 10:41:02 -040093 // clone the revision data to avoid any race conditions with processes
94 // accessing the same data
95 cloned := proto.Clone(pr.GetConfig().Data.(proto.Message))
96
97 if blob, err := proto.Marshal(cloned); err != nil {
98 log.Errorw("problem-to-marshal", log.Fields{"error": err, "hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetData()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -040099 } else {
100 if pr.Compress {
101 var b bytes.Buffer
102 w := gzip.NewWriter(&b)
103 w.Write(blob)
104 w.Close()
105 blob = b.Bytes()
106 }
107
Manikkaraj kb1d51442019-07-23 10:41:02 -0400108 GetRevCache().Set(pr.GetName(), pr)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400109 if err := pr.kvStore.Put(pr.GetName(), blob); err != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400110 log.Warnw("problem-storing-revision", log.Fields{"error": err, "hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetConfig().Data})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400111 } else {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400112 log.Debugw("storing-revision", log.Fields{"hash": pr.GetHash(), "name": pr.GetName(), "data": pr.GetConfig().Data, "version": pr.getVersion()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400113 pr.isStored = true
114 }
115 }
116}
117
118func (pr *PersistedRevision) SetupWatch(key string) {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400119 if key == "" {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400120 log.Debugw("ignoring-watch", log.Fields{"key": key, "revision-hash": pr.GetHash()})
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400121 return
122 }
123
124 if _, exists := Watches().Cache.LoadOrStore(key+"-"+pr.GetHash(), struct{}{}); exists {
125 return
126 }
127
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400128 if pr.events == nil {
129 pr.events = make(chan *kvstore.Event)
130
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400131 log.Debugw("setting-watch-channel", log.Fields{"key": key, "revision-hash": pr.GetHash()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400132
133 pr.SetName(key)
134 pr.events = pr.kvStore.CreateWatch(key)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400135 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400136
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400137 if !pr.isWatched {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400138 pr.isWatched = true
139
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400140 log.Debugw("setting-watch-routine", log.Fields{"key": key, "revision-hash": pr.GetHash()})
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400141
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400142 // Start watching
143 go pr.startWatching()
144 }
145}
146
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400147func (pr *PersistedRevision) startWatching() {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400148 log.Debugw("starting-watch", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400149
150StopWatchLoop:
151 for {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700152 latestRev := pr.GetBranch().GetLatest()
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400153
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400154 select {
155 case event, ok := <-pr.events:
156 if !ok {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700157 log.Errorw("event-channel-failure: stopping watch loop", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400158 break StopWatchLoop
159 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700160 log.Debugw("received-event", log.Fields{"type": event.EventType, "watch": latestRev.GetName()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400161
162 switch event.EventType {
163 case kvstore.DELETE:
Mahir Gunyele77977b2019-06-27 05:36:22 -0700164 log.Debugw("delete-from-memory", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400165 pr.Revision.Drop("", true)
166 break StopWatchLoop
167
168 case kvstore.PUT:
Mahir Gunyele77977b2019-06-27 05:36:22 -0700169 log.Debugw("update-in-memory", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName()})
Manikkaraj kb1d51442019-07-23 10:41:02 -0400170 if latestRev.getVersion() >= event.Version {
171 log.Debugw("skipping-matching-or-older-revision", log.Fields{
172 "watch": latestRev.GetName(),
173 "watch-version": event.Version,
174 "latest-version": latestRev.getVersion(),
175 })
176 continue
177 } else {
178 log.Debugw("watch-revision-is-newer", log.Fields{
179 "watch": latestRev.GetName(),
180 "watch-version": event.Version,
181 "latest-version": latestRev.getVersion(),
182 })
183 }
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400184
Mahir Gunyele77977b2019-06-27 05:36:22 -0700185 data := reflect.New(reflect.TypeOf(latestRev.GetData()).Elem())
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400186
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400187 if err := proto.Unmarshal(event.Value.([]byte), data.Interface().(proto.Message)); err != nil {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700188 log.Errorw("failed-to-unmarshal-watch-data", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName(), "error": err})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400189 } else {
Mahir Gunyele77977b2019-06-27 05:36:22 -0700190 log.Debugw("un-marshaled-watch-data", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName(), "data": data.Interface()})
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400191
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400192 var pathLock string
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400193 var blobs map[string]*kvstore.KVPair
194
195 // The watch reported new persistence data.
196 // Construct an object that will be used to update the memory
197 blobs = make(map[string]*kvstore.KVPair)
198 key, _ := kvstore.ToString(event.Key)
199 blobs[key] = &kvstore.KVPair{
200 Key: key,
201 Value: event.Value,
202 Session: "",
203 Lease: 0,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400204 Version: event.Version,
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400205 }
206
Mahir Gunyele77977b2019-06-27 05:36:22 -0700207 if latestRev.GetNode().GetProxy() != nil {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400208 //
209 // If a proxy exists for this revision, use it to lock access to the path
210 // and prevent simultaneous updates to the object in memory
211 //
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400212
213 //If the proxy already has a request in progress, then there is no need to process the watch
Manikkaraj kb1d51442019-07-23 10:41:02 -0400214 if latestRev.GetNode().GetProxy().GetOperation() != PROXY_NONE {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400215 log.Debugw("operation-in-progress", log.Fields{
Mahir Gunyele77977b2019-06-27 05:36:22 -0700216 "key": latestRev.GetHash(),
217 "path": latestRev.GetNode().GetProxy().getFullPath(),
Manikkaraj kb1d51442019-07-23 10:41:02 -0400218 "operation": latestRev.GetNode().GetProxy().operation.String(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400219 })
Manikkaraj kb1d51442019-07-23 10:41:02 -0400220 continue
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400221 }
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400222
Manikkaraj kb1d51442019-07-23 10:41:02 -0400223 pathLock, _ = latestRev.GetNode().GetProxy().parseForControlledPath(latestRev.GetNode().GetProxy().getFullPath())
224
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400225 // Reserve the path to prevent others to modify while we reload from persistence
Manikkaraj kb1d51442019-07-23 10:41:02 -0400226 latestRev.GetNode().GetProxy().GetRoot().KvStore.Client.Reserve(pathLock+"_", uuid.New().String(), ReservationTTL)
227 latestRev.GetNode().GetProxy().SetOperation(PROXY_WATCH)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400228
229 // Load changes and apply to memory
Manikkaraj kb1d51442019-07-23 10:41:02 -0400230 latestRev.LoadFromPersistence(context.Background(), latestRev.GetName(), "", blobs)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400231
Manikkaraj kb1d51442019-07-23 10:41:02 -0400232 // Release path
233 latestRev.GetNode().GetProxy().GetRoot().KvStore.Client.ReleaseReservation(pathLock + "_")
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400234
235 } else {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400236 // This block should be reached only if coming from a non-proxied request
Mahir Gunyele77977b2019-06-27 05:36:22 -0700237 log.Debugw("revision-with-no-proxy", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName()})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400238
239 // Load changes and apply to memory
Manikkaraj kb1d51442019-07-23 10:41:02 -0400240 latestRev.LoadFromPersistence(context.Background(), latestRev.GetName(), "", blobs)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400241 }
242 }
243
244 default:
Mahir Gunyele77977b2019-06-27 05:36:22 -0700245 log.Debugw("unhandled-event", log.Fields{"key": latestRev.GetHash(), "watch": latestRev.GetName(), "type": event.EventType})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400246 }
247 }
248 }
249
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400250 Watches().Cache.Delete(pr.GetName() + "-" + pr.GetHash())
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400251
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400252 log.Debugw("exiting-watch", log.Fields{"key": pr.GetHash(), "watch": pr.GetName()})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400253}
254
255// UpdateData modifies the information in the data model and saves it in the persistent storage
Manikkaraj kb1d51442019-07-23 10:41:02 -0400256func (pr *PersistedRevision) UpdateData(ctx context.Context, data interface{}, branch *Branch) Revision {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400257 log.Debugw("updating-persisted-data", log.Fields{"hash": pr.GetHash()})
258
Manikkaraj kb1d51442019-07-23 10:41:02 -0400259 newNPR := pr.Revision.UpdateData(ctx, data, branch)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400260
261 newPR := &PersistedRevision{
Mahir Gunyele77977b2019-06-27 05:36:22 -0700262 Revision: newNPR,
263 Compress: pr.Compress,
264 kvStore: pr.kvStore,
265 events: pr.events,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400266 Version: pr.getVersion(),
Mahir Gunyele77977b2019-06-27 05:36:22 -0700267 isWatched: pr.isWatched,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400268 }
269
270 if newPR.GetHash() != pr.GetHash() {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400271 newPR.isStored = false
272 pr.Drop(branch.Txid, false)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400273 pr.Drop(branch.Txid, false)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400274 } else {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400275 newPR.isStored = true
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400276 }
277
278 return newPR
279}
280
281// UpdateChildren modifies the children of a revision and of a specific component and saves it in the persistent storage
Manikkaraj kb1d51442019-07-23 10:41:02 -0400282func (pr *PersistedRevision) UpdateChildren(ctx context.Context, name string, children []Revision, branch *Branch) Revision {
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400283 log.Debugw("updating-persisted-children", log.Fields{"hash": pr.GetHash()})
284
Manikkaraj kb1d51442019-07-23 10:41:02 -0400285 newNPR := pr.Revision.UpdateChildren(ctx, name, children, branch)
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400286
287 newPR := &PersistedRevision{
Mahir Gunyele77977b2019-06-27 05:36:22 -0700288 Revision: newNPR,
289 Compress: pr.Compress,
290 kvStore: pr.kvStore,
291 events: pr.events,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400292 Version: pr.getVersion(),
Mahir Gunyele77977b2019-06-27 05:36:22 -0700293 isWatched: pr.isWatched,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400294 }
295
296 if newPR.GetHash() != pr.GetHash() {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400297 newPR.isStored = false
298 pr.Drop(branch.Txid, false)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400299 } else {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400300 newPR.isStored = true
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400301 }
302
303 return newPR
304}
305
306// UpdateAllChildren modifies the children for all components of a revision and saves it in the peristent storage
307func (pr *PersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision {
308 log.Debugw("updating-all-persisted-children", log.Fields{"hash": pr.GetHash()})
309
310 newNPR := pr.Revision.UpdateAllChildren(children, branch)
311
312 newPR := &PersistedRevision{
Mahir Gunyele77977b2019-06-27 05:36:22 -0700313 Revision: newNPR,
314 Compress: pr.Compress,
315 kvStore: pr.kvStore,
316 events: pr.events,
Manikkaraj kb1d51442019-07-23 10:41:02 -0400317 Version: pr.getVersion(),
Mahir Gunyele77977b2019-06-27 05:36:22 -0700318 isWatched: pr.isWatched,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400319 }
320
321 if newPR.GetHash() != pr.GetHash() {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400322 newPR.isStored = false
323 pr.Drop(branch.Txid, false)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400324 } else {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400325 newPR.isStored = true
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400326 }
327
328 return newPR
329}
330
331// Drop takes care of eliminating a revision hash that is no longer needed
332// and its associated config when required
333func (pr *PersistedRevision) Drop(txid string, includeConfig bool) {
334 pr.Revision.Drop(txid, includeConfig)
335}
336
337// Drop takes care of eliminating a revision hash that is no longer needed
338// and its associated config when required
339func (pr *PersistedRevision) StorageDrop(txid string, includeConfig bool) {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400340 log.Debugw("dropping-revision", log.Fields{"txid": txid, "hash": pr.GetHash(), "config-hash": pr.GetConfig().Hash})
Matt Jeanneretcab955f2019-04-10 15:45:57 -0400341
342 pr.mutex.Lock()
343 defer pr.mutex.Unlock()
344 if pr.kvStore != nil && txid == "" {
345 if pr.isStored {
346 if pr.isWatched {
347 pr.kvStore.DeleteWatch(pr.GetName(), pr.events)
348 pr.isWatched = false
349 }
350
351 if err := pr.kvStore.Delete(pr.GetName()); err != nil {
352 log.Errorw("failed-to-remove-revision", log.Fields{"hash": pr.GetHash(), "error": err.Error()})
353 } else {
354 pr.isStored = false
355 }
356 }
357
358 } else {
359 if includeConfig {
360 log.Debugw("attempted-to-remove-transacted-revision-config", log.Fields{"hash": pr.GetConfig().Hash, "txid": txid})
361 }
362 log.Debugw("attempted-to-remove-transacted-revision", log.Fields{"hash": pr.GetHash(), "txid": txid})
363 }
364
365 pr.Revision.Drop(txid, includeConfig)
366}
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400367
368// verifyPersistedEntry validates if the provided data is available or not in memory and applies updates as required
Manikkaraj kb1d51442019-07-23 10:41:02 -0400369func (pr *PersistedRevision) verifyPersistedEntry(ctx context.Context, data interface{}, typeName string, keyName string,
370 keyValue string, txid string, version int64) (response Revision) {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400371 // Parent which holds the current node entry
Manikkaraj kb1d51442019-07-23 10:41:02 -0400372 parent := pr.GetBranch().Node.GetRoot()
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400373
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400374 // Get a copy of the parent's children
375 children := make([]Revision, len(parent.GetBranch(NONE).Latest.GetChildren(typeName)))
376 copy(children, parent.GetBranch(NONE).Latest.GetChildren(typeName))
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400377
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400378 // Verify if a child with the provided key value can be found
379 if childIdx, childRev := pr.GetNode().findRevByKey(children, keyName, keyValue); childRev != nil {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400380 // A child matching the provided key exists in memory
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400381 // Verify if the data differs from what was retrieved from persistence
Mahir Gunyele77977b2019-06-27 05:36:22 -0700382 // Also check if we are treating a newer revision of the data or not
Manikkaraj kb1d51442019-07-23 10:41:02 -0400383 if childRev.GetData().(proto.Message).String() != data.(proto.Message).String() && childRev.getVersion() < version {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400384 log.Debugw("revision-data-is-different", log.Fields{
Manikkaraj kb1d51442019-07-23 10:41:02 -0400385 "key": childRev.GetHash(),
386 "name": childRev.GetName(),
387 "data": childRev.GetData(),
388 "version": childRev.getVersion(),
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400389 })
390
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400391 //
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400392 // Data has changed; replace the child entry and update the parent revision
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400393 //
394
395 // BEGIN Lock child -- prevent any incoming changes
396 childRev.GetBranch().LatestLock.Lock()
397
398 // Update child
Manikkaraj kb1d51442019-07-23 10:41:02 -0400399 updatedChildRev := childRev.UpdateData(ctx, data, childRev.GetBranch())
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400400
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400401 updatedChildRev.GetNode().SetProxy(childRev.GetNode().GetProxy())
402 updatedChildRev.SetupWatch(updatedChildRev.GetName())
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400403 updatedChildRev.SetLastUpdate()
Manikkaraj kb1d51442019-07-23 10:41:02 -0400404 updatedChildRev.(*PersistedRevision).setVersion(version)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400405
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400406 // Update cache
Manikkaraj kb1d51442019-07-23 10:41:02 -0400407 GetRevCache().Set(updatedChildRev.GetName(), updatedChildRev)
Mahir Gunyele77977b2019-06-27 05:36:22 -0700408 childRev.Drop(txid, false)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400409
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400410 childRev.GetBranch().LatestLock.Unlock()
411 // END lock child
412
413 // Update child entry
414 children[childIdx] = updatedChildRev
415
416 // BEGIN lock parent -- Update parent
417 parent.GetBranch(NONE).LatestLock.Lock()
418
Manikkaraj kb1d51442019-07-23 10:41:02 -0400419 updatedRev := parent.GetBranch(NONE).GetLatest().UpdateChildren(ctx, typeName, children, parent.GetBranch(NONE))
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400420 parent.GetBranch(NONE).Node.makeLatest(parent.GetBranch(NONE), updatedRev, nil)
421
422 parent.GetBranch(NONE).LatestLock.Unlock()
423 // END lock parent
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400424
425 // Drop the previous child revision
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400426 parent.GetBranch(NONE).Latest.ChildDrop(typeName, childRev.GetHash())
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400427
428 if updatedChildRev != nil {
429 log.Debugw("verify-persisted-entry--adding-child", log.Fields{
430 "key": updatedChildRev.GetHash(),
431 "name": updatedChildRev.GetName(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400432 "data": updatedChildRev.GetData(),
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400433 })
434 response = updatedChildRev
435 }
436 } else {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400437 if childRev != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400438 log.Debugw("keeping-revision-data", log.Fields{
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400439 "key": childRev.GetHash(),
440 "name": childRev.GetName(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400441 "data": childRev.GetData(),
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400442 })
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400443
444 // Update timestamp to reflect when it was last read and to reset tracked timeout
445 childRev.SetLastUpdate()
Manikkaraj kb1d51442019-07-23 10:41:02 -0400446 if childRev.getVersion() < version {
447 childRev.(*PersistedRevision).setVersion(version)
448 }
449 GetRevCache().Set(childRev.GetName(), childRev)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400450 response = childRev
451 }
452 }
Mahir Gunyele77977b2019-06-27 05:36:22 -0700453
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400454 } else {
455 // There is no available child with that key value.
456 // Create a new child and update the parent revision.
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400457 log.Debugw("no-such-revision-entry", log.Fields{
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400458 "key": keyValue,
459 "name": typeName,
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400460 "data": data,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400461 })
462
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400463 // BEGIN child lock
464 pr.GetBranch().LatestLock.Lock()
465
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400466 // Construct a new child node with the retrieved persistence data
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400467 childRev = pr.GetBranch().Node.MakeNode(data, txid).Latest(txid)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400468
469 // We need to start watching this entry for future changes
470 childRev.SetName(typeName + "/" + keyValue)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400471 childRev.SetupWatch(childRev.GetName())
Manikkaraj kb1d51442019-07-23 10:41:02 -0400472 childRev.(*PersistedRevision).setVersion(version)
473
474 // Add entry to cache
475 GetRevCache().Set(childRev.GetName(), childRev)
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400476
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400477 pr.GetBranch().LatestLock.Unlock()
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400478 // END child lock
479
480 //
481 // Add the child to the parent revision
482 //
483
484 // BEGIN parent lock
485 parent.GetBranch(NONE).LatestLock.Lock()
486 children = append(children, childRev)
Manikkaraj kb1d51442019-07-23 10:41:02 -0400487 updatedRev := parent.GetBranch(NONE).GetLatest().UpdateChildren(ctx, typeName, children, parent.GetBranch(NONE))
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400488 updatedRev.GetNode().SetProxy(parent.GetBranch(NONE).Node.GetProxy())
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400489 parent.GetBranch(NONE).Node.makeLatest(parent.GetBranch(NONE), updatedRev, nil)
490 parent.GetBranch(NONE).LatestLock.Unlock()
491 // END parent lock
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400492
493 // Child entry is valid and can be included in the response object
494 if childRev != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400495 log.Debugw("adding-revision-to-response", log.Fields{
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400496 "key": childRev.GetHash(),
497 "name": childRev.GetName(),
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400498 "data": childRev.GetData(),
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400499 })
500 response = childRev
501 }
502 }
503
504 return response
505}
506
507// LoadFromPersistence retrieves data from kv store at the specified location and refreshes the memory
508// by adding missing entries, updating changed entries and ignoring unchanged ones
Manikkaraj kb1d51442019-07-23 10:41:02 -0400509func (pr *PersistedRevision) LoadFromPersistence(ctx context.Context, path string, txid string, blobs map[string]*kvstore.KVPair) []Revision {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400510 pr.mutex.Lock()
511 defer pr.mutex.Unlock()
512
513 log.Debugw("loading-from-persistence", log.Fields{"path": path, "txid": txid})
514
515 var response []Revision
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400516
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400517 for strings.HasPrefix(path, "/") {
518 path = path[1:]
519 }
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400520
521 if pr.kvStore != nil && path != "" {
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400522 if blobs == nil || len(blobs) == 0 {
523 log.Debugw("retrieve-from-kv", log.Fields{"path": path, "txid": txid})
524 blobs, _ = pr.kvStore.List(path)
525 }
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400526
527 partition := strings.SplitN(path, "/", 2)
528 name := partition[0]
529
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400530 var nodeType interface{}
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400531 if len(partition) < 2 {
532 path = ""
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400533 nodeType = pr.GetBranch().Node.Type
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400534 } else {
535 path = partition[1]
Manikkaraj kb1d51442019-07-23 10:41:02 -0400536 nodeType = pr.GetBranch().Node.GetRoot().Type
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400537 }
538
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400539 field := ChildrenFields(nodeType)[name]
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400540
541 if field != nil && field.IsContainer {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400542 log.Debugw("parsing-data-blobs", log.Fields{
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400543 "path": path,
544 "name": name,
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400545 "size": len(blobs),
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400546 })
547
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400548 for _, blob := range blobs {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400549 output := blob.Value.([]byte)
550
551 data := reflect.New(field.ClassType.Elem())
552
553 if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400554 log.Errorw("failed-to-unmarshal", log.Fields{
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400555 "path": path,
556 "txid": txid,
557 "error": err,
558 })
559 } else if path == "" {
560 if field.Key != "" {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400561 log.Debugw("no-path-with-container-key", log.Fields{
562 "path": path,
563 "txid": txid,
564 "data": data.Interface(),
565 })
566
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400567 // Retrieve the key identifier value from the data structure
568 // based on the field's key attribute
569 _, key := GetAttributeValue(data.Interface(), field.Key, 0)
570
Manikkaraj kb1d51442019-07-23 10:41:02 -0400571 if entry := pr.verifyPersistedEntry(ctx, data.Interface(), name, field.Key, key.String(), txid, blob.Version); entry != nil {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400572 response = append(response, entry)
573 }
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400574 } else {
575 log.Debugw("path-with-no-container-key", log.Fields{
576 "path": path,
577 "txid": txid,
578 "data": data.Interface(),
579 })
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400580 }
581
582 } else if field.Key != "" {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400583 log.Debugw("path-with-container-key", log.Fields{
584 "path": path,
585 "txid": txid,
586 "data": data.Interface(),
587 })
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400588 // The request is for a specific entry/id
589 partition := strings.SplitN(path, "/", 2)
590 key := partition[0]
591 if len(partition) < 2 {
592 path = ""
593 } else {
594 path = partition[1]
595 }
596 keyValue := field.KeyFromStr(key)
597
Manikkaraj kb1d51442019-07-23 10:41:02 -0400598 if entry := pr.verifyPersistedEntry(ctx, data.Interface(), name, field.Key, keyValue.(string), txid, blob.Version); entry != nil {
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400599 response = append(response, entry)
600 }
601 }
602 }
603
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400604 log.Debugw("no-more-data-blobs", log.Fields{"path": path, "name": name})
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400605 } else {
Chaitrashree G Sbe6ab942019-05-24 06:42:49 -0400606 log.Debugw("cannot-process-field", log.Fields{
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400607 "type": pr.GetBranch().Node.Type,
Matt Jeanneret384d8c92019-05-06 14:27:31 -0400608 "name": name,
609 })
610 }
611 }
612
613 return response
614}