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 | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 22 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/db/kvstore" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | "reflect" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 26 | "runtime/debug" |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 27 | "strings" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 28 | "sync" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 29 | ) |
| 30 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 31 | // 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] | 32 | type PersistedRevision struct { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 33 | Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 34 | Compress bool |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 35 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 36 | events chan *kvstore.Event `json:"-"` |
| 37 | kvStore *Backend `json:"-"` |
| 38 | mutex sync.RWMutex `json:"-"` |
| 39 | isStored bool |
| 40 | isWatched bool |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 41 | } |
| 42 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 43 | // NewPersistedRevision creates a new instance of a PersistentRevision structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 44 | func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 45 | pr := &PersistedRevision{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 46 | pr.kvStore = branch.Node.GetRoot().KvStore |
| 47 | pr.Revision = NewNonPersistedRevision(nil, branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 48 | return pr |
| 49 | } |
| 50 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 51 | // Finalize is responsible of saving the revision in the persistent storage |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 52 | func (pr *PersistedRevision) Finalize(skipOnExist bool) { |
| 53 | pr.store(skipOnExist) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | type revData struct { |
| 57 | Children map[string][]string |
| 58 | Config string |
| 59 | } |
| 60 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 61 | func (pr *PersistedRevision) store(skipOnExist bool) { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 62 | if pr.GetBranch().Txid != "" { |
| 63 | return |
| 64 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 65 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 66 | if pair, _ := pr.kvStore.Get(pr.GetHash()); pair != nil && skipOnExist { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 67 | log.Debugw("revision-config-already-exists", log.Fields{"hash": pr.GetHash()}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 68 | return |
| 69 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 70 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 71 | if blob, err := proto.Marshal(pr.GetConfig().Data.(proto.Message)); err != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 72 | // TODO report error |
| 73 | } else { |
| 74 | if pr.Compress { |
| 75 | var b bytes.Buffer |
| 76 | w := gzip.NewWriter(&b) |
| 77 | w.Write(blob) |
| 78 | w.Close() |
| 79 | blob = b.Bytes() |
| 80 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 81 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 82 | if err := pr.kvStore.Put(pr.GetHash(), blob); err != nil { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 83 | log.Warnw("problem-storing-revision-config", |
| 84 | log.Fields{"error": err, "hash": pr.GetHash(), "data": pr.GetConfig().Data}) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 85 | } else { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 86 | log.Debugw("storing-revision-config", |
| 87 | log.Fields{"hash": pr.GetHash(), "data": pr.GetConfig().Data}) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 88 | pr.isStored = true |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 89 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 93 | func (pr *PersistedRevision) SetupWatch(key string) { |
| 94 | if pr.events == nil { |
| 95 | pr.events = make(chan *kvstore.Event) |
| 96 | |
| 97 | log.Debugw("setting-watch", log.Fields{"key": key}) |
| 98 | |
| 99 | pr.events = pr.kvStore.CreateWatch(key) |
| 100 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 101 | pr.isWatched = true |
| 102 | |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 103 | // Start watching |
| 104 | go pr.startWatching() |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func (pr *PersistedRevision) startWatching() { |
| 109 | log.Debugw("starting-watch", log.Fields{"key": pr.GetHash()}) |
| 110 | |
| 111 | StopWatchLoop: |
| 112 | for { |
| 113 | select { |
| 114 | case event, ok := <-pr.events: |
| 115 | if !ok { |
| 116 | log.Errorw("event-channel-failure: stopping watch loop", log.Fields{"key": pr.GetHash()}) |
| 117 | break StopWatchLoop |
| 118 | } |
| 119 | |
| 120 | log.Debugw("received-event", log.Fields{"type": event.EventType}) |
| 121 | |
| 122 | switch event.EventType { |
| 123 | case kvstore.DELETE: |
| 124 | log.Debugw("delete-from-memory", log.Fields{"key": pr.GetHash()}) |
| 125 | pr.Revision.Drop("", true) |
| 126 | break StopWatchLoop |
| 127 | |
| 128 | case kvstore.PUT: |
| 129 | log.Debugw("update-in-memory", log.Fields{"key": pr.GetHash()}) |
| 130 | |
| 131 | if dataPair, err := pr.kvStore.Get(pr.GetHash()); err != nil || dataPair == nil { |
| 132 | log.Errorw("update-in-memory--key-retrieval-failed", log.Fields{"key": pr.GetHash(), "error": err}) |
| 133 | } else { |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 134 | data := reflect.New(reflect.TypeOf(pr.GetData()).Elem()) |
| 135 | |
| 136 | if err := proto.Unmarshal(dataPair.Value.([]byte), data.Interface().(proto.Message)); err != nil { |
| 137 | log.Errorw("update-in-memory--unmarshal-failed", log.Fields{"key": pr.GetHash(), "error": err}) |
| 138 | } else { |
Stephane Barbarie | 0a97e9b | 2019-02-11 22:02:17 -0500 | [diff] [blame] | 139 | // Apply changes to current revision |
| 140 | branch := pr.GetBranch() |
| 141 | rev := branch.GetLatest() |
| 142 | updatedRev := rev.UpdateData(data.Interface(), branch) |
| 143 | |
Stephane Barbarie | b0c7989 | 2019-02-13 11:29:59 -0500 | [diff] [blame] | 144 | // ensure that we keep the previous hash value |
| 145 | updatedRev.SetHash(rev.GetHash()) |
| 146 | |
| 147 | // Save revision |
| 148 | branch.SetLatest(updatedRev) |
Stephane Barbarie | df5479f | 2019-01-29 22:13:00 -0500 | [diff] [blame] | 149 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | default: |
| 153 | log.Debugw("unhandled-event", log.Fields{"key": pr.GetHash(), "type": event.EventType}) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | log.Debugw("exiting-watch", log.Fields{"key": pr.GetHash()}) |
| 159 | } |
| 160 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 161 | func (pr *PersistedRevision) LoadFromPersistence(path string, txid string) []Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 162 | log.Debugw("loading-from-persistence", log.Fields{"path": path, "txid": txid}) |
| 163 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 164 | var response []Revision |
| 165 | var rev Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 166 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 167 | rev = pr |
| 168 | |
| 169 | if pr.kvStore != nil { |
| 170 | blobMap, _ := pr.kvStore.List(path) |
| 171 | |
| 172 | partition := strings.SplitN(path, "/", 2) |
| 173 | name := partition[0] |
| 174 | |
| 175 | if len(partition) < 2 { |
| 176 | path = "" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 177 | } else { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 178 | path = partition[1] |
| 179 | } |
| 180 | |
| 181 | field := ChildrenFields(rev.GetBranch().Node.Type)[name] |
| 182 | |
| 183 | if field.IsContainer { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 184 | var children []Revision |
| 185 | children = make([]Revision, len(rev.GetChildren(name))) |
| 186 | copy(children, rev.GetChildren(name)) |
| 187 | existChildMap := make(map[string]int) |
| 188 | for i, child := range rev.GetChildren(name) { |
| 189 | existChildMap[child.GetHash()] = i |
| 190 | } |
| 191 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 192 | for _, blob := range blobMap { |
| 193 | output := blob.Value.([]byte) |
| 194 | |
| 195 | data := reflect.New(field.ClassType.Elem()) |
| 196 | |
| 197 | if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil { |
Stephane Barbarie | aa46794 | 2019-02-06 14:09:44 -0500 | [diff] [blame] | 198 | log.Errorw( |
| 199 | "loading-from-persistence--failed-to-unmarshal", |
| 200 | log.Fields{"path": path, "txid": txid, "error": err}, |
| 201 | ) |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 202 | } else if field.Key != "" { |
| 203 | var key reflect.Value |
| 204 | var keyValue interface{} |
| 205 | var keyStr string |
| 206 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 207 | if path == "" { |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 208 | // e.g. /logical_devices --> path="" name=logical_devices key="" |
| 209 | _, key = GetAttributeValue(data.Interface(), field.Key, 0) |
| 210 | keyStr = key.String() |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 211 | |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 212 | } else { |
| 213 | // e.g. |
| 214 | // /logical_devices/abcde --> path="abcde" name=logical_devices |
| 215 | // /logical_devices/abcde/image_downloads --> path="abcde/image_downloads" name=logical_devices |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 216 | |
| 217 | partition := strings.SplitN(path, "/", 2) |
| 218 | key := partition[0] |
| 219 | if len(partition) < 2 { |
| 220 | path = "" |
| 221 | } else { |
| 222 | path = partition[1] |
| 223 | } |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 224 | keyValue = field.KeyFromStr(key) |
| 225 | keyStr = keyValue.(string) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 226 | |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 227 | if idx, childRev := rev.GetBranch().Node.findRevByKey(children, field.Key, keyValue); childRev != nil { |
| 228 | // Key is memory, continue recursing path |
| 229 | newChildRev := childRev.LoadFromPersistence(path, txid) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 230 | |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 231 | children[idx] = newChildRev[0] |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 232 | |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 233 | rev := rev.UpdateChildren(name, rev.GetChildren(name), rev.GetBranch()) |
| 234 | rev.GetBranch().Node.makeLatest(rev.GetBranch(), rev, nil) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 235 | |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 236 | response = append(response, newChildRev[0]) |
| 237 | continue |
| 238 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 239 | } |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 240 | |
| 241 | childRev := rev.GetBranch().Node.MakeNode(data.Interface(), txid).Latest(txid) |
| 242 | childRev.SetHash(name + "/" + keyStr) |
| 243 | |
| 244 | // Do not process a child that is already in memory |
| 245 | if _, childExists := existChildMap[childRev.GetHash()]; !childExists { |
| 246 | // Create watch for <component>/<key> |
| 247 | childRev.SetupWatch(childRev.GetHash()) |
| 248 | |
| 249 | children = append(children, childRev) |
| 250 | rev = rev.UpdateChildren(name, children, rev.GetBranch()) |
| 251 | |
| 252 | rev.GetBranch().Node.makeLatest(rev.GetBranch(), rev, nil) |
| 253 | } |
| 254 | response = append(response, childRev) |
| 255 | continue |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 256 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
Stephane Barbarie | 11b88e7 | 2019-02-07 12:28:29 -0500 | [diff] [blame] | 260 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 261 | return response |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 262 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 263 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 264 | // UpdateData modifies the information in the data model and saves it in the persistent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 265 | func (pr *PersistedRevision) UpdateData(data interface{}, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 266 | log.Debugw("updating-persisted-data", log.Fields{"hash": pr.GetHash()}) |
| 267 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 268 | newNPR := pr.Revision.UpdateData(data, branch) |
| 269 | |
| 270 | newPR := &PersistedRevision{ |
| 271 | Revision: newNPR, |
| 272 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 273 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 274 | } |
| 275 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 276 | return newPR |
| 277 | } |
| 278 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 279 | // UpdateChildren modifies the children of a revision and of a specific component and saves it in the persistent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 280 | func (pr *PersistedRevision) UpdateChildren(name string, children []Revision, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 281 | log.Debugw("updating-persisted-children", log.Fields{"hash": pr.GetHash()}) |
| 282 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 283 | newNPR := pr.Revision.UpdateChildren(name, children, branch) |
| 284 | |
| 285 | newPR := &PersistedRevision{ |
| 286 | Revision: newNPR, |
| 287 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 288 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 291 | return newPR |
| 292 | } |
| 293 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 294 | // UpdateAllChildren modifies the children for all components of a revision and saves it in the peristent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 295 | func (pr *PersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 296 | log.Debugw("updating-all-persisted-children", log.Fields{"hash": pr.GetHash()}) |
| 297 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 298 | newNPR := pr.Revision.UpdateAllChildren(children, branch) |
| 299 | |
| 300 | newPR := &PersistedRevision{ |
| 301 | Revision: newNPR, |
| 302 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 303 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 304 | } |
| 305 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 306 | return newPR |
| 307 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 308 | |
| 309 | // Drop takes care of eliminating a revision hash that is no longer needed |
| 310 | // and its associated config when required |
| 311 | func (pr *PersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 312 | log.Debugw("dropping-revision", |
| 313 | log.Fields{"txid": txid, "hash": pr.GetHash(), "config-hash": pr.GetConfig().Hash, "stack": string(debug.Stack())}) |
| 314 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 315 | pr.mutex.Lock() |
| 316 | defer pr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 317 | if pr.kvStore != nil && txid == "" { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 318 | if pr.isStored { |
| 319 | if includeConfig { |
| 320 | if err := pr.kvStore.Delete(pr.GetConfig().Hash); err != nil { |
| 321 | log.Errorw("failed-to-remove-revision-config", log.Fields{"hash": pr.GetConfig().Hash, "error": err.Error()}) |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if err := pr.kvStore.Delete(pr.GetHash()); err != nil { |
| 326 | log.Errorw("failed-to-remove-revision", log.Fields{"hash": pr.GetHash(), "error": err.Error()}) |
| 327 | } else { |
| 328 | pr.isStored = false |
| 329 | } |
| 330 | |
| 331 | if pr.isWatched { |
| 332 | pr.kvStore.DeleteWatch(pr.GetHash(), pr.events) |
| 333 | pr.isWatched = false |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 337 | } else { |
| 338 | if includeConfig { |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 339 | 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] | 340 | } |
Stephane Barbarie | e0a4c79 | 2019-01-16 11:26:29 -0500 | [diff] [blame] | 341 | 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] | 342 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 343 | |
| 344 | pr.Revision.Drop(txid, includeConfig) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 345 | } |