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" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v3/pkg/db" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore" |
| 30 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 31 | ) |
| 32 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 33 | // PersistedRevision holds information of revision meant to be saved in a persistent storage |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 34 | type PersistedRevision struct { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 35 | Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 36 | Compress bool |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 37 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 38 | events chan *kvstore.Event |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 39 | kvStore *db.Backend |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 40 | mutex sync.RWMutex |
| 41 | versionMutex sync.RWMutex |
| 42 | Version int64 |
| 43 | isStored bool |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 46 | // NewPersistedRevision creates a new instance of a PersistentRevision structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 47 | func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 48 | pr := &PersistedRevision{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 49 | pr.kvStore = branch.Node.GetRoot().KvStore |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 50 | pr.Version = 1 |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 51 | pr.Revision = NewNonPersistedRevision(nil, branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 52 | return pr |
| 53 | } |
| 54 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 55 | func (pr *PersistedRevision) getVersion() int64 { |
| 56 | pr.versionMutex.RLock() |
| 57 | defer pr.versionMutex.RUnlock() |
| 58 | return pr.Version |
| 59 | } |
| 60 | |
| 61 | func (pr *PersistedRevision) setVersion(version int64) { |
| 62 | pr.versionMutex.Lock() |
| 63 | defer pr.versionMutex.Unlock() |
| 64 | pr.Version = version |
| 65 | } |
| 66 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 67 | // Finalize is responsible of saving the revision in the persistent storage |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 68 | func (pr *PersistedRevision) Finalize(ctx context.Context, skipOnExist bool) { |
| 69 | pr.store(ctx, skipOnExist) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 70 | } |
| 71 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 72 | func (pr *PersistedRevision) store(ctx context.Context, skipOnExist bool) { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 73 | if pr.GetBranch().Txid != "" { |
| 74 | return |
| 75 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 76 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 77 | 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] | 78 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 79 | // clone the revision data to avoid any race conditions with processes |
| 80 | // accessing the same data |
| 81 | cloned := proto.Clone(pr.GetConfig().Data.(proto.Message)) |
| 82 | |
| 83 | if blob, err := proto.Marshal(cloned); err != nil { |
| 84 | 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] | 85 | } else { |
| 86 | if pr.Compress { |
| 87 | var b bytes.Buffer |
| 88 | w := gzip.NewWriter(&b) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 89 | if _, err := w.Write(blob); err != nil { |
| 90 | log.Errorw("Unable to write a compressed form of p to the underlying io.Writer.", log.Fields{"error": err}) |
| 91 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 92 | w.Close() |
| 93 | blob = b.Bytes() |
| 94 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 95 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 96 | getRevCache().Set(pr.GetName(), pr) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 97 | if err := pr.kvStore.Put(ctx, pr.GetName(), blob); err != nil { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 98 | 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] | 99 | } else { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 100 | 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] | 101 | pr.isStored = true |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 102 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 106 | // 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] | 107 | func (pr *PersistedRevision) UpdateData(ctx context.Context, data interface{}, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 108 | log.Debugw("updating-persisted-data", log.Fields{"hash": pr.GetHash()}) |
| 109 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 110 | newNPR := pr.Revision.UpdateData(ctx, data, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 111 | |
| 112 | newPR := &PersistedRevision{ |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 113 | Revision: newNPR, |
| 114 | Compress: pr.Compress, |
| 115 | kvStore: pr.kvStore, |
| 116 | events: pr.events, |
| 117 | Version: pr.getVersion(), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | if newPR.GetHash() != pr.GetHash() { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 121 | newPR.isStored = false |
| 122 | pr.Drop(branch.Txid, false) |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 123 | pr.Drop(branch.Txid, false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 124 | } else { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 125 | newPR.isStored = true |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 128 | return newPR |
| 129 | } |
| 130 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 131 | // 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] | 132 | 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] | 133 | log.Debugw("updating-persisted-children", log.Fields{"hash": pr.GetHash()}) |
| 134 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 135 | newNPR := pr.Revision.UpdateChildren(ctx, name, children, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 136 | |
| 137 | newPR := &PersistedRevision{ |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 138 | Revision: newNPR, |
| 139 | Compress: pr.Compress, |
| 140 | kvStore: pr.kvStore, |
| 141 | events: pr.events, |
| 142 | Version: pr.getVersion(), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if newPR.GetHash() != pr.GetHash() { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 146 | newPR.isStored = false |
| 147 | pr.Drop(branch.Txid, false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 148 | } else { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 149 | newPR.isStored = true |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 152 | return newPR |
| 153 | } |
| 154 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 155 | // 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] | 156 | 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] | 157 | log.Debugw("updating-all-persisted-children", log.Fields{"hash": pr.GetHash()}) |
| 158 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 159 | newNPR := pr.Revision.UpdateAllChildren(ctx, children, branch) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 160 | |
| 161 | newPR := &PersistedRevision{ |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 162 | Revision: newNPR, |
| 163 | Compress: pr.Compress, |
| 164 | kvStore: pr.kvStore, |
| 165 | events: pr.events, |
| 166 | Version: pr.getVersion(), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if newPR.GetHash() != pr.GetHash() { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 170 | newPR.isStored = false |
| 171 | pr.Drop(branch.Txid, false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 172 | } else { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 173 | newPR.isStored = true |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 176 | return newPR |
| 177 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 178 | |
| 179 | // Drop takes care of eliminating a revision hash that is no longer needed |
| 180 | // and its associated config when required |
| 181 | func (pr *PersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | f7fc178 | 2019-03-28 22:33:41 -0400 | [diff] [blame] | 182 | pr.Revision.Drop(txid, includeConfig) |
| 183 | } |
| 184 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 185 | // 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] | 186 | // and its associated config when required |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 187 | func (pr *PersistedRevision) StorageDrop(ctx context.Context, txid string, includeConfig bool) { |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 188 | 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] | 189 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 190 | pr.mutex.Lock() |
| 191 | defer pr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 192 | if pr.kvStore != nil && txid == "" { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 193 | if err := pr.kvStore.Delete(ctx, pr.GetName()); err != nil { |
khenaidoo | 4908535 | 2020-01-13 19:15:43 -0500 | [diff] [blame] | 194 | log.Errorw("failed-to-remove-revision", log.Fields{"hash": pr.GetHash(), "error": err.Error()}) |
| 195 | } else { |
| 196 | pr.isStored = false |
| 197 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 198 | } else { |
| 199 | if includeConfig { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 200 | 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] | 201 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 202 | 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] | 203 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 204 | |
| 205 | pr.Revision.Drop(txid, includeConfig) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 206 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 207 | |
| 208 | // 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] | 209 | func (pr *PersistedRevision) verifyPersistedEntry(ctx context.Context, data interface{}, typeName string, keyName string, |
| 210 | keyValue string, txid string, version int64) (response Revision) { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 211 | // Parent which holds the current node entry |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 212 | parent := pr.GetBranch().Node.GetRoot() |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 213 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 214 | // Get a copy of the parent's children |
| 215 | children := make([]Revision, len(parent.GetBranch(NONE).Latest.GetChildren(typeName))) |
| 216 | copy(children, parent.GetBranch(NONE).Latest.GetChildren(typeName)) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 217 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 218 | // Verify if a child with the provided key value can be found |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 219 | if childIdx, childRev := pr.getNode().findRevByKey(children, keyName, keyValue); childRev != nil { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 220 | // A child matching the provided key exists in memory |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 221 | // Verify if the data differs from what was retrieved from persistence |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 222 | // 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] | 223 | 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] | 224 | log.Debugw("revision-data-is-different", log.Fields{ |
David Bainbridge | bdae73c | 2019-10-23 17:05:41 +0000 | [diff] [blame] | 225 | "key": childRev.GetHash(), |
| 226 | "name": childRev.GetName(), |
| 227 | "data": childRev.GetData(), |
| 228 | "in-memory-version": childRev.getVersion(), |
| 229 | "persisted-version": version, |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 230 | }) |
| 231 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 232 | // |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 233 | // Data has changed; replace the child entry and update the parent revision |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 234 | // |
| 235 | |
| 236 | // BEGIN Lock child -- prevent any incoming changes |
| 237 | childRev.GetBranch().LatestLock.Lock() |
| 238 | |
| 239 | // Update child |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 240 | updatedChildRev := childRev.UpdateData(ctx, data, childRev.GetBranch()) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 241 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 242 | updatedChildRev.getNode().SetProxy(childRev.getNode().GetProxy()) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 243 | updatedChildRev.SetLastUpdate() |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 244 | updatedChildRev.(*PersistedRevision).setVersion(version) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 245 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 246 | // Update cache |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 247 | getRevCache().Set(updatedChildRev.GetName(), updatedChildRev) |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 248 | childRev.Drop(txid, false) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 249 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 250 | childRev.GetBranch().LatestLock.Unlock() |
| 251 | // END lock child |
| 252 | |
| 253 | // Update child entry |
| 254 | children[childIdx] = updatedChildRev |
| 255 | |
| 256 | // BEGIN lock parent -- Update parent |
| 257 | parent.GetBranch(NONE).LatestLock.Lock() |
| 258 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 259 | updatedRev := parent.GetBranch(NONE).GetLatest().UpdateChildren(ctx, typeName, children, parent.GetBranch(NONE)) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 260 | parent.GetBranch(NONE).Node.makeLatest(parent.GetBranch(NONE), updatedRev, nil) |
| 261 | |
| 262 | parent.GetBranch(NONE).LatestLock.Unlock() |
| 263 | // END lock parent |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 264 | |
| 265 | // Drop the previous child revision |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 266 | parent.GetBranch(NONE).Latest.ChildDrop(typeName, childRev.GetHash()) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 267 | |
| 268 | if updatedChildRev != nil { |
| 269 | log.Debugw("verify-persisted-entry--adding-child", log.Fields{ |
| 270 | "key": updatedChildRev.GetHash(), |
| 271 | "name": updatedChildRev.GetName(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 272 | "data": updatedChildRev.GetData(), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 273 | }) |
| 274 | response = updatedChildRev |
| 275 | } |
| 276 | } else { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 277 | log.Debugw("keeping-revision-data", log.Fields{ |
| 278 | "key": childRev.GetHash(), |
| 279 | "name": childRev.GetName(), |
| 280 | "data": childRev.GetData(), |
| 281 | "in-memory-version": childRev.getVersion(), |
| 282 | "persistence-version": version, |
| 283 | }) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 284 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 285 | // Update timestamp to reflect when it was last read and to reset tracked timeout |
| 286 | childRev.SetLastUpdate() |
| 287 | if childRev.getVersion() < version { |
| 288 | childRev.(*PersistedRevision).setVersion(version) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 289 | } |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 290 | getRevCache().Set(childRev.GetName(), childRev) |
| 291 | response = childRev |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 292 | } |
Stephane Barbarie | c92d107 | 2019-06-07 16:21:49 -0400 | [diff] [blame] | 293 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 294 | } else { |
| 295 | // There is no available child with that key value. |
| 296 | // Create a new child and update the parent revision. |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 297 | log.Debugw("no-such-revision-entry", log.Fields{ |
David Bainbridge | bdae73c | 2019-10-23 17:05:41 +0000 | [diff] [blame] | 298 | "key": keyValue, |
| 299 | "name": typeName, |
| 300 | "data": data, |
| 301 | "version": version, |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 302 | }) |
| 303 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 304 | // BEGIN child lock |
| 305 | pr.GetBranch().LatestLock.Lock() |
| 306 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 307 | // Construct a new child node with the retrieved persistence data |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 308 | childRev = pr.GetBranch().Node.MakeNode(data, txid).Latest(txid) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 309 | |
| 310 | // We need to start watching this entry for future changes |
| 311 | childRev.SetName(typeName + "/" + keyValue) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 312 | childRev.(*PersistedRevision).setVersion(version) |
| 313 | |
| 314 | // Add entry to cache |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 315 | getRevCache().Set(childRev.GetName(), childRev) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 316 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 317 | pr.GetBranch().LatestLock.Unlock() |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 318 | // END child lock |
| 319 | |
| 320 | // |
| 321 | // Add the child to the parent revision |
| 322 | // |
| 323 | |
| 324 | // BEGIN parent lock |
| 325 | parent.GetBranch(NONE).LatestLock.Lock() |
| 326 | children = append(children, childRev) |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 327 | updatedRev := parent.GetBranch(NONE).GetLatest().UpdateChildren(ctx, typeName, children, parent.GetBranch(NONE)) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 328 | updatedRev.getNode().SetProxy(parent.GetBranch(NONE).Node.GetProxy()) |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 329 | parent.GetBranch(NONE).Node.makeLatest(parent.GetBranch(NONE), updatedRev, nil) |
| 330 | parent.GetBranch(NONE).LatestLock.Unlock() |
| 331 | // END parent lock |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 332 | |
| 333 | // Child entry is valid and can be included in the response object |
| 334 | if childRev != nil { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 335 | log.Debugw("adding-revision-to-response", log.Fields{ |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 336 | "key": childRev.GetHash(), |
| 337 | "name": childRev.GetName(), |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 338 | "data": childRev.GetData(), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 339 | }) |
| 340 | response = childRev |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return response |
| 345 | } |
| 346 | |
| 347 | // LoadFromPersistence retrieves data from kv store at the specified location and refreshes the memory |
| 348 | // by adding missing entries, updating changed entries and ignoring unchanged ones |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 349 | 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] | 350 | pr.mutex.Lock() |
| 351 | defer pr.mutex.Unlock() |
| 352 | |
| 353 | log.Debugw("loading-from-persistence", log.Fields{"path": path, "txid": txid}) |
| 354 | |
| 355 | var response []Revision |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 356 | var err error |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 357 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 358 | for strings.HasPrefix(path, "/") { |
| 359 | path = path[1:] |
| 360 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 361 | |
| 362 | if pr.kvStore != nil && path != "" { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 363 | if len(blobs) == 0 { |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 364 | log.Debugw("retrieve-from-kv", log.Fields{"path": path, "txid": txid}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 365 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 366 | if blobs, err = pr.kvStore.List(ctx, path); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 367 | log.Errorw("failed-to-retrieve-data-from-kvstore", log.Fields{"error": err}) |
| 368 | return nil, err |
| 369 | } |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 370 | } |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 371 | |
| 372 | partition := strings.SplitN(path, "/", 2) |
| 373 | name := partition[0] |
| 374 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 375 | var nodeType interface{} |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 376 | if len(partition) < 2 { |
| 377 | path = "" |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 378 | nodeType = pr.GetBranch().Node.Type |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 379 | } else { |
| 380 | path = partition[1] |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 381 | nodeType = pr.GetBranch().Node.GetRoot().Type |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 382 | } |
| 383 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 384 | field := ChildrenFields(nodeType)[name] |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 385 | |
| 386 | if field != nil && field.IsContainer { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 387 | log.Debugw("parsing-data-blobs", log.Fields{ |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 388 | "path": path, |
| 389 | "name": name, |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 390 | "size": len(blobs), |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 391 | }) |
| 392 | |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 393 | for _, blob := range blobs { |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 394 | output := blob.Value.([]byte) |
| 395 | |
| 396 | data := reflect.New(field.ClassType.Elem()) |
| 397 | |
| 398 | if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 399 | log.Errorw("failed-to-unmarshal", log.Fields{ |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 400 | "path": path, |
| 401 | "txid": txid, |
| 402 | "error": err, |
| 403 | }) |
| 404 | } else if path == "" { |
| 405 | if field.Key != "" { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 406 | log.Debugw("no-path-with-container-key", log.Fields{ |
| 407 | "path": path, |
| 408 | "txid": txid, |
| 409 | "data": data.Interface(), |
| 410 | }) |
| 411 | |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 412 | // Retrieve the key identifier value from the data structure |
| 413 | // based on the field's key attribute |
| 414 | _, key := GetAttributeValue(data.Interface(), field.Key, 0) |
| 415 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 416 | 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] | 417 | response = append(response, entry) |
| 418 | } |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 419 | } else { |
| 420 | log.Debugw("path-with-no-container-key", log.Fields{ |
| 421 | "path": path, |
| 422 | "txid": txid, |
| 423 | "data": data.Interface(), |
| 424 | }) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | } else if field.Key != "" { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 428 | log.Debugw("path-with-container-key", log.Fields{ |
| 429 | "path": path, |
| 430 | "txid": txid, |
| 431 | "data": data.Interface(), |
| 432 | }) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 433 | // The request is for a specific entry/id |
| 434 | partition := strings.SplitN(path, "/", 2) |
| 435 | key := partition[0] |
| 436 | if len(partition) < 2 { |
| 437 | path = "" |
| 438 | } else { |
| 439 | path = partition[1] |
| 440 | } |
| 441 | keyValue := field.KeyFromStr(key) |
| 442 | |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 443 | 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] | 444 | response = append(response, entry) |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 449 | log.Debugw("no-more-data-blobs", log.Fields{"path": path, "name": name}) |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 450 | } else { |
Stephane Barbarie | 802aca4 | 2019-05-21 12:19:28 -0400 | [diff] [blame] | 451 | log.Debugw("cannot-process-field", log.Fields{ |
Stephane Barbarie | 7512fc8 | 2019-05-07 12:25:46 -0400 | [diff] [blame] | 452 | "type": pr.GetBranch().Node.Type, |
Stephane Barbarie | 40fd3b2 | 2019-04-23 21:50:47 -0400 | [diff] [blame] | 453 | "name": name, |
| 454 | }) |
| 455 | } |
| 456 | } |
| 457 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 458 | return response, nil |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 459 | } |