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