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