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" |
| 22 | "encoding/json" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 23 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | "io/ioutil" |
| 26 | "reflect" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 27 | "runtime/debug" |
| 28 | "sync" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 29 | "time" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 32 | // 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] | 33 | type PersistedRevision struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 34 | mutex sync.RWMutex |
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 |
| 37 | kvStore *Backend |
| 38 | } |
| 39 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 40 | // NewPersistedRevision creates a new instance of a PersistentRevision structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 41 | func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 42 | pr := &PersistedRevision{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 43 | pr.kvStore = branch.Node.GetRoot().KvStore |
| 44 | pr.Revision = NewNonPersistedRevision(nil, branch, data, children) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 45 | pr.Finalize() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 46 | return pr |
| 47 | } |
| 48 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 49 | // Finalize is responsible of saving the revision in the persistent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 50 | func (pr *PersistedRevision) Finalize() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 51 | pr.store() |
| 52 | } |
| 53 | |
| 54 | type revData struct { |
| 55 | Children map[string][]string |
| 56 | Config string |
| 57 | } |
| 58 | |
| 59 | func (pr *PersistedRevision) store() { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 60 | if pr.GetBranch().Txid != "" { |
| 61 | return |
| 62 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 63 | if ok, _ := pr.kvStore.Get(pr.Revision.GetHash()); ok != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 64 | log.Debugf("Entry already exists - hash:%s, stack: %s", pr.Revision.GetHash(), string(debug.Stack())) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 65 | return |
| 66 | } |
| 67 | |
| 68 | pr.storeConfig() |
| 69 | |
| 70 | childrenHashes := make(map[string][]string) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 71 | for fieldName, children := range pr.GetChildren() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 72 | hashes := []string{} |
| 73 | for _, rev := range children { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 74 | if rev != nil { |
| 75 | hashes = append(hashes, rev.GetHash()) |
| 76 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 77 | } |
| 78 | childrenHashes[fieldName] = hashes |
| 79 | } |
| 80 | data := &revData{ |
| 81 | Children: childrenHashes, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 82 | Config: pr.GetConfig().Hash, |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 83 | } |
| 84 | if blob, err := json.Marshal(data); err != nil { |
| 85 | // TODO report error |
| 86 | } else { |
| 87 | if pr.Compress { |
| 88 | var b bytes.Buffer |
| 89 | w := gzip.NewWriter(&b) |
| 90 | w.Write(blob) |
| 91 | w.Close() |
| 92 | blob = b.Bytes() |
| 93 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 94 | if err := pr.kvStore.Put(pr.GetHash(), blob); err != nil { |
| 95 | log.Warnf("Problem storing revision - error: %s, hash: %s, data: %s", err.Error(), pr.GetHash(), |
| 96 | string(blob)) |
| 97 | } else { |
| 98 | log.Debugf("Stored entry - hash:%s, blob: %s, stack: %s", pr.Revision.GetHash(), string(blob), |
| 99 | string(debug.Stack())) |
| 100 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 104 | // Load retrieves a revision from th persistent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 105 | func (pr *PersistedRevision) Load(branch *Branch, kvStore *Backend, msgClass interface{}, hash string) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 106 | blob, _ := kvStore.Get(hash) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 107 | |
| 108 | start := time.Now() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 109 | output := blob.Value.([]byte) |
| 110 | var data revData |
| 111 | if pr.Compress { |
| 112 | b := bytes.NewBuffer(blob.Value.([]byte)) |
| 113 | if r, err := gzip.NewReader(b); err != nil { |
| 114 | // TODO : report error |
| 115 | } else { |
| 116 | if output, err = ioutil.ReadAll(r); err != nil { |
| 117 | // TODO report error |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | if err := json.Unmarshal(output, &data); err != nil { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 122 | log.Errorf("problem to unmarshal data - %s", err.Error()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 125 | stop := time.Now() |
| 126 | GetProfiling().AddToInMemoryModelTime(stop.Sub(start).Seconds()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 127 | configHash := data.Config |
| 128 | configData := pr.loadConfig(kvStore, msgClass, configHash) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 129 | |
| 130 | assembledChildren := make(map[string][]Revision) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 131 | |
| 132 | childrenHashes := data.Children |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 133 | node := branch.Node |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 134 | for fieldName, child := range ChildrenFields(msgClass) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 135 | var children []Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 136 | for _, childHash := range childrenHashes[fieldName] { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 137 | childNode := node.MakeNode(reflect.New(child.ClassType).Elem().Interface(), "") |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 138 | childNode.LoadLatest(childHash) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 139 | childRev := childNode.Latest() |
| 140 | children = append(children, childRev) |
| 141 | } |
| 142 | assembledChildren[fieldName] = children |
| 143 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 144 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 145 | rev := NewPersistedRevision(branch, configData, assembledChildren) |
| 146 | return rev |
| 147 | } |
| 148 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 149 | // storeConfig saves the data associated to a revision in the persistent storage |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 150 | func (pr *PersistedRevision) storeConfig() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 151 | if ok, _ := pr.kvStore.Get(pr.GetConfig().Hash); ok != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 152 | log.Debugf("Config already exists - hash:%s, stack: %s", pr.GetConfig().Hash, string(debug.Stack())) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 153 | return |
| 154 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 155 | if blob, err := proto.Marshal(pr.GetConfig().Data.(proto.Message)); err != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 156 | // TODO report error |
| 157 | } else { |
| 158 | if pr.Compress { |
| 159 | var b bytes.Buffer |
| 160 | w := gzip.NewWriter(&b) |
| 161 | w.Write(blob) |
| 162 | w.Close() |
| 163 | blob = b.Bytes() |
| 164 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 165 | |
| 166 | if err := pr.kvStore.Put(pr.GetConfig().Hash, blob); err != nil { |
| 167 | log.Warnf("Problem storing revision config - error: %s, hash: %s, data: %+v", err.Error(), |
| 168 | pr.GetConfig().Hash, |
| 169 | pr.GetConfig().Data) |
| 170 | } else { |
| 171 | log.Debugf("Stored config - hash:%s, blob: %+v, stack: %s", pr.GetConfig().Hash, pr.GetConfig().Data, |
| 172 | string(debug.Stack())) |
| 173 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 177 | // loadConfig restores the data associated to a revision from the persistent storage |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 178 | func (pr *PersistedRevision) loadConfig(kvStore *Backend, msgClass interface{}, hash string) interface{} { |
| 179 | blob, _ := kvStore.Get(hash) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 180 | start := time.Now() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 181 | output := blob.Value.([]byte) |
| 182 | |
| 183 | if pr.Compress { |
| 184 | b := bytes.NewBuffer(blob.Value.([]byte)) |
| 185 | if r, err := gzip.NewReader(b); err != nil { |
| 186 | // TODO : report error |
| 187 | } else { |
| 188 | if output, err = ioutil.ReadAll(r); err != nil { |
| 189 | // TODO report error |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | var data reflect.Value |
| 195 | if msgClass != nil { |
| 196 | data = reflect.New(reflect.TypeOf(msgClass).Elem()) |
| 197 | if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil { |
| 198 | // TODO report error |
| 199 | } |
| 200 | } |
| 201 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 202 | stop := time.Now() |
| 203 | |
| 204 | GetProfiling().AddToInMemoryModelTime(stop.Sub(start).Seconds()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 205 | return data.Interface() |
| 206 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 207 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 208 | // 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] | 209 | func (pr *PersistedRevision) UpdateData(data interface{}, branch *Branch) Revision { |
| 210 | newNPR := pr.Revision.UpdateData(data, branch) |
| 211 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 212 | log.Debugf("Updating data %+v", data) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 213 | newPR := &PersistedRevision{ |
| 214 | Revision: newNPR, |
| 215 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 216 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | newPR.Finalize() |
| 220 | |
| 221 | return newPR |
| 222 | } |
| 223 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 224 | // 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] | 225 | func (pr *PersistedRevision) UpdateChildren(name string, children []Revision, branch *Branch) Revision { |
| 226 | newNPR := pr.Revision.UpdateChildren(name, children, branch) |
| 227 | |
| 228 | newPR := &PersistedRevision{ |
| 229 | Revision: newNPR, |
| 230 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 231 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | newPR.Finalize() |
| 235 | |
| 236 | return newPR |
| 237 | } |
| 238 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 239 | // 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] | 240 | func (pr *PersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
| 241 | newNPR := pr.Revision.UpdateAllChildren(children, branch) |
| 242 | |
| 243 | newPR := &PersistedRevision{ |
| 244 | Revision: newNPR, |
| 245 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 246 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | newPR.Finalize() |
| 250 | |
| 251 | return newPR |
| 252 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 253 | |
| 254 | // Drop takes care of eliminating a revision hash that is no longer needed |
| 255 | // and its associated config when required |
| 256 | func (pr *PersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 257 | pr.mutex.Lock() |
| 258 | defer pr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 259 | if pr.kvStore != nil && txid == "" { |
| 260 | if includeConfig { |
| 261 | log.Debugf("removing rev config - hash: %s", pr.GetConfig().Hash) |
| 262 | if err := pr.kvStore.Delete(pr.GetConfig().Hash); err != nil { |
| 263 | log.Errorf( |
| 264 | "failed to remove rev config - hash: %s, err: %s", |
| 265 | pr.GetConfig().Hash, |
| 266 | err.Error(), |
| 267 | ) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | log.Debugf("removing rev - hash: %s", pr.GetHash()) |
| 272 | if err := pr.kvStore.Delete(pr.GetHash()); err != nil { |
| 273 | log.Errorf("failed to remove rev - hash: %s, err: %s", pr.GetHash(), err.Error()) |
| 274 | } |
| 275 | } else { |
| 276 | if includeConfig { |
| 277 | log.Debugf("Attempted to remove revision config:%s linked to transaction:%s", pr.GetConfig().Hash, txid) |
| 278 | } |
| 279 | log.Debugf("Attempted to remove revision:%s linked to transaction:%s", pr.GetHash(), txid) |
| 280 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 281 | |
| 282 | pr.Revision.Drop(txid, includeConfig) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 283 | } |