blob: b21deebe4c4a57b4af28fb7abe375e10f8dd5e62 [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 Barbariedc5022d2018-11-19 15:21:44 -050026 "runtime/debug"
Stephane Barbarie1ab43272018-12-08 21:42:13 -050027 "strings"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050028 "sync"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040029)
30
Stephane Barbariedc5022d2018-11-19 15:21:44 -050031// PersistedRevision holds information of revision meant to be saved in a persistent storage
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040032type PersistedRevision struct {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040033 Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040034 Compress bool
Stephane Barbariee0a4c792019-01-16 11:26:29 -050035
Stephane Barbarie3cb01222019-01-16 17:15:56 -050036 events chan *kvstore.Event `json:"-"`
37 kvStore *Backend `json:"-"`
38 mutex sync.RWMutex `json:"-"`
39 isStored bool
40 isWatched bool
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040041}
42
Stephane Barbariedc5022d2018-11-19 15:21:44 -050043// NewPersistedRevision creates a new instance of a PersistentRevision structure
Stephane Barbarieec0919b2018-09-05 14:14:29 -040044func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040045 pr := &PersistedRevision{}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050046 pr.kvStore = branch.Node.GetRoot().KvStore
47 pr.Revision = NewNonPersistedRevision(nil, branch, data, children)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040048 return pr
49}
50
Stephane Barbariedc5022d2018-11-19 15:21:44 -050051// Finalize is responsible of saving the revision in the persistent storage
Stephane Barbarie1ab43272018-12-08 21:42:13 -050052func (pr *PersistedRevision) Finalize(skipOnExist bool) {
53 pr.store(skipOnExist)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040054}
55
56type revData struct {
57 Children map[string][]string
58 Config string
59}
60
Stephane Barbarie1ab43272018-12-08 21:42:13 -050061func (pr *PersistedRevision) store(skipOnExist bool) {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040062 if pr.GetBranch().Txid != "" {
63 return
64 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040065
Stephane Barbarie1ab43272018-12-08 21:42:13 -050066 if pair, _ := pr.kvStore.Get(pr.GetHash()); pair != nil && skipOnExist {
Stephane Barbariee0a4c792019-01-16 11:26:29 -050067 log.Debugw("revision-config-already-exists", log.Fields{"hash": pr.GetHash()})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040068 return
69 }
Stephane Barbarie1ab43272018-12-08 21:42:13 -050070
Stephane Barbarieec0919b2018-09-05 14:14:29 -040071 if blob, err := proto.Marshal(pr.GetConfig().Data.(proto.Message)); err != nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040072 // 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 Barbariedc5022d2018-11-19 15:21:44 -050081
Stephane Barbarie1ab43272018-12-08 21:42:13 -050082 if err := pr.kvStore.Put(pr.GetHash(), blob); err != nil {
Stephane Barbariee0a4c792019-01-16 11:26:29 -050083 log.Warnw("problem-storing-revision-config",
84 log.Fields{"error": err, "hash": pr.GetHash(), "data": pr.GetConfig().Data})
Stephane Barbariedc5022d2018-11-19 15:21:44 -050085 } else {
Stephane Barbariee0a4c792019-01-16 11:26:29 -050086 log.Debugw("storing-revision-config",
87 log.Fields{"hash": pr.GetHash(), "data": pr.GetConfig().Data})
Stephane Barbarie3cb01222019-01-16 17:15:56 -050088 pr.isStored = true
Stephane Barbariedc5022d2018-11-19 15:21:44 -050089 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040090 }
91}
92
Stephane Barbariee0a4c792019-01-16 11:26:29 -050093func (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 Barbarie3cb01222019-01-16 17:15:56 -0500101 pr.isWatched = true
102
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500103 // Start watching
104 go pr.startWatching()
105 }
106}
107
108func (pr *PersistedRevision) startWatching() {
109 log.Debugw("starting-watch", log.Fields{"key": pr.GetHash()})
110
111StopWatchLoop:
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 Barbariedf5479f2019-01-29 22:13:00 -0500134 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 Barbarie11b88e72019-02-07 12:28:29 -0500139 // Traverse the node and update as necessary
140 rev := pr.GetBranch().GetLatest()
141 rev.GetBranch().Node.Update("", data.Interface(), false, "", nil)
Stephane Barbariedf5479f2019-01-29 22:13:00 -0500142 }
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500143 }
144
145 default:
146 log.Debugw("unhandled-event", log.Fields{"key": pr.GetHash(), "type": event.EventType})
147 }
148 }
149 }
150
151 log.Debugw("exiting-watch", log.Fields{"key": pr.GetHash()})
152}
153
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500154func (pr *PersistedRevision) LoadFromPersistence(path string, txid string) []Revision {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500155 log.Debugw("loading-from-persistence", log.Fields{"path": path, "txid": txid})
156
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500157 var response []Revision
158 var rev Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400159
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500160 rev = pr
161
162 if pr.kvStore != nil {
163 blobMap, _ := pr.kvStore.List(path)
164
165 partition := strings.SplitN(path, "/", 2)
166 name := partition[0]
167
168 if len(partition) < 2 {
169 path = ""
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400170 } else {
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500171 path = partition[1]
172 }
173
174 field := ChildrenFields(rev.GetBranch().Node.Type)[name]
175
176 if field.IsContainer {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500177 var children []Revision
178 children = make([]Revision, len(rev.GetChildren(name)))
179 copy(children, rev.GetChildren(name))
180 existChildMap := make(map[string]int)
181 for i, child := range rev.GetChildren(name) {
182 existChildMap[child.GetHash()] = i
183 }
184
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500185 for _, blob := range blobMap {
186 output := blob.Value.([]byte)
187
188 data := reflect.New(field.ClassType.Elem())
189
190 if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500191 log.Errorw(
192 "loading-from-persistence--failed-to-unmarshal",
193 log.Fields{"path": path, "txid": txid, "error": err},
194 )
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500195 } else if field.Key != "" {
196 var key reflect.Value
197 var keyValue interface{}
198 var keyStr string
199
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500200 if path == "" {
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500201 // e.g. /logical_devices --> path="" name=logical_devices key=""
202 _, key = GetAttributeValue(data.Interface(), field.Key, 0)
203 keyStr = key.String()
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500204
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500205 } else {
206 // e.g.
207 // /logical_devices/abcde --> path="abcde" name=logical_devices
208 // /logical_devices/abcde/image_downloads --> path="abcde/image_downloads" name=logical_devices
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500209
210 partition := strings.SplitN(path, "/", 2)
211 key := partition[0]
212 if len(partition) < 2 {
213 path = ""
214 } else {
215 path = partition[1]
216 }
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500217 keyValue = field.KeyFromStr(key)
218 keyStr = keyValue.(string)
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500219
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500220 if idx, childRev := rev.GetBranch().Node.findRevByKey(children, field.Key, keyValue); childRev != nil {
221 // Key is memory, continue recursing path
222 newChildRev := childRev.LoadFromPersistence(path, txid)
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500223
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500224 children[idx] = newChildRev[0]
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500225
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500226 rev := rev.UpdateChildren(name, rev.GetChildren(name), rev.GetBranch())
227 rev.GetBranch().Node.makeLatest(rev.GetBranch(), rev, nil)
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500228
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500229 response = append(response, newChildRev[0])
230 continue
231 }
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500232 }
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500233
234 childRev := rev.GetBranch().Node.MakeNode(data.Interface(), txid).Latest(txid)
235 childRev.SetHash(name + "/" + keyStr)
236
237 // Do not process a child that is already in memory
238 if _, childExists := existChildMap[childRev.GetHash()]; !childExists {
239 // Create watch for <component>/<key>
240 childRev.SetupWatch(childRev.GetHash())
241
242 children = append(children, childRev)
243 rev = rev.UpdateChildren(name, children, rev.GetBranch())
244
245 rev.GetBranch().Node.makeLatest(rev.GetBranch(), rev, nil)
246 }
247 response = append(response, childRev)
248 continue
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500249 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400250 }
251 }
252 }
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500253
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500254 return response
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400255}
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400256
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500257// UpdateData modifies the information in the data model and saves it in the persistent storage
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400258func (pr *PersistedRevision) UpdateData(data interface{}, branch *Branch) Revision {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500259 log.Debugw("updating-persisted-data", log.Fields{"hash": pr.GetHash()})
260
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400261 newNPR := pr.Revision.UpdateData(data, branch)
262
263 newPR := &PersistedRevision{
264 Revision: newNPR,
265 Compress: pr.Compress,
khenaidoob9203542018-09-17 22:56:37 -0400266 kvStore: pr.kvStore,
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400267 }
268
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400269 return newPR
270}
271
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500272// UpdateChildren modifies the children of a revision and of a specific component and saves it in the persistent storage
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400273func (pr *PersistedRevision) UpdateChildren(name string, children []Revision, branch *Branch) Revision {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500274 log.Debugw("updating-persisted-children", log.Fields{"hash": pr.GetHash()})
275
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400276 newNPR := pr.Revision.UpdateChildren(name, children, branch)
277
278 newPR := &PersistedRevision{
279 Revision: newNPR,
280 Compress: pr.Compress,
khenaidoob9203542018-09-17 22:56:37 -0400281 kvStore: pr.kvStore,
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400282 }
283
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400284 return newPR
285}
286
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500287// UpdateAllChildren modifies the children for all components of a revision and saves it in the peristent storage
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400288func (pr *PersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500289 log.Debugw("updating-all-persisted-children", log.Fields{"hash": pr.GetHash()})
290
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400291 newNPR := pr.Revision.UpdateAllChildren(children, branch)
292
293 newPR := &PersistedRevision{
294 Revision: newNPR,
295 Compress: pr.Compress,
khenaidoob9203542018-09-17 22:56:37 -0400296 kvStore: pr.kvStore,
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400297 }
298
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400299 return newPR
300}
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400301
302// Drop takes care of eliminating a revision hash that is no longer needed
303// and its associated config when required
304func (pr *PersistedRevision) Drop(txid string, includeConfig bool) {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500305 log.Debugw("dropping-revision",
306 log.Fields{"txid": txid, "hash": pr.GetHash(), "config-hash": pr.GetConfig().Hash, "stack": string(debug.Stack())})
307
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500308 pr.mutex.Lock()
309 defer pr.mutex.Unlock()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400310 if pr.kvStore != nil && txid == "" {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500311 if pr.isStored {
312 if includeConfig {
313 if err := pr.kvStore.Delete(pr.GetConfig().Hash); err != nil {
314 log.Errorw("failed-to-remove-revision-config", log.Fields{"hash": pr.GetConfig().Hash, "error": err.Error()})
315 }
316 }
317
318 if err := pr.kvStore.Delete(pr.GetHash()); err != nil {
319 log.Errorw("failed-to-remove-revision", log.Fields{"hash": pr.GetHash(), "error": err.Error()})
320 } else {
321 pr.isStored = false
322 }
323
324 if pr.isWatched {
325 pr.kvStore.DeleteWatch(pr.GetHash(), pr.events)
326 pr.isWatched = false
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400327 }
328 }
329
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400330 } else {
331 if includeConfig {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500332 log.Debugw("attempted-to-remove-transacted-revision-config", log.Fields{"hash": pr.GetConfig().Hash, "txid": txid})
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400333 }
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500334 log.Debugw("attempted-to-remove-transacted-revision", log.Fields{"hash": pr.GetHash(), "txid": txid})
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400335 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500336
337 pr.Revision.Drop(txid, includeConfig)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400338}