Stephane Barbarie | ec0919b | 2018-09-05 14:14: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 | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 20 | "context" |
| 21 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func revisionsAreEqual(a, b []Revision) bool { |
| 26 | // If one is nil, the other must also be nil. |
| 27 | if (a == nil) != (b == nil) { |
| 28 | return false |
| 29 | } |
| 30 | |
| 31 | if len(a) != len(b) { |
| 32 | return false |
| 33 | } |
| 34 | |
| 35 | for i := range a { |
| 36 | if a[i] != b[i] { |
| 37 | return false |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return true |
| 42 | } |
| 43 | |
| 44 | type changeAnalysis struct { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 45 | KeyMap1 map[string]int |
| 46 | KeyMap2 map[string]int |
| 47 | AddedKeys map[string]struct{} |
| 48 | RemovedKeys map[string]struct{} |
| 49 | ChangedKeys map[string]struct{} |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | func newChangeAnalysis(lst1, lst2 []Revision, keyName string) *changeAnalysis { |
| 53 | changes := &changeAnalysis{} |
| 54 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 55 | changes.KeyMap1 = make(map[string]int) |
| 56 | changes.KeyMap2 = make(map[string]int) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 57 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 58 | changes.AddedKeys = make(map[string]struct{}) |
| 59 | changes.RemovedKeys = make(map[string]struct{}) |
| 60 | changes.ChangedKeys = make(map[string]struct{}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 61 | |
| 62 | for i, rev := range lst1 { |
| 63 | _, v := GetAttributeValue(rev.GetData(), keyName, 0) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 64 | changes.KeyMap1[v.String()] = i |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 65 | } |
| 66 | for i, rev := range lst2 { |
| 67 | _, v := GetAttributeValue(rev.GetData(), keyName, 0) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 68 | changes.KeyMap2[v.String()] = i |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 69 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 70 | for v := range changes.KeyMap2 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 71 | if _, ok := changes.KeyMap1[v]; !ok { |
| 72 | changes.AddedKeys[v] = struct{}{} |
| 73 | } |
| 74 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 75 | for v := range changes.KeyMap1 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 76 | if _, ok := changes.KeyMap2[v]; !ok { |
| 77 | changes.RemovedKeys[v] = struct{}{} |
| 78 | } |
| 79 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 80 | for v := range changes.KeyMap1 { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 81 | if _, ok := changes.KeyMap2[v]; ok && lst1[changes.KeyMap1[v]].GetHash() != lst2[changes.KeyMap2[v]].GetHash() { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 82 | changes.ChangedKeys[v] = struct{}{} |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return changes |
| 87 | } |
| 88 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 89 | // Merge3Way takes care of combining the revision contents of the same data set |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 90 | func Merge3Way( |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 91 | ctx context.Context, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 92 | forkRev, srcRev, dstRev Revision, |
| 93 | mergeChildFunc func(Revision) Revision, |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 94 | dryRun bool) (rev Revision, changes []ChangeTuple) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 95 | |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 96 | logger.Debugw("3-way-merge-request", log.Fields{"dryRun": dryRun}) |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 97 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 98 | var configChanged bool |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 99 | var revsToDiscard []Revision |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 100 | |
| 101 | if dstRev.GetConfig() == forkRev.GetConfig() { |
| 102 | configChanged = dstRev.GetConfig() != srcRev.GetConfig() |
| 103 | } else { |
| 104 | if dstRev.GetConfig().Hash != srcRev.GetConfig().Hash { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 105 | logger.Error("config-collision") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 106 | } |
| 107 | configChanged = true |
| 108 | } |
| 109 | |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 110 | //newChildren := reflect.ValueOf(dstRev.GetAllChildren()).Interface().(map[string][]Revision) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 111 | newChildren := make(map[string][]Revision) |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 112 | for entryName, childrenEntry := range dstRev.GetAllChildren() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 113 | //newRev.Children[entryName] = append(newRev.Children[entryName], childrenEntry...) |
| 114 | newChildren[entryName] = make([]Revision, len(childrenEntry)) |
| 115 | copy(newChildren[entryName], childrenEntry) |
| 116 | } |
| 117 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 118 | childrenFields := ChildrenFields(forkRev.GetData()) |
| 119 | |
| 120 | for fieldName, field := range childrenFields { |
Stephane Barbarie | 3cb0122 | 2019-01-16 17:15:56 -0500 | [diff] [blame] | 121 | forkList := forkRev.GetChildren(fieldName) |
| 122 | srcList := srcRev.GetChildren(fieldName) |
| 123 | dstList := dstRev.GetChildren(fieldName) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 124 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 125 | if revisionsAreEqual(dstList, srcList) { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 126 | for _, rev := range srcList { |
| 127 | mergeChildFunc(rev) |
| 128 | } |
| 129 | continue |
| 130 | } |
| 131 | |
| 132 | if field.Key == "" { |
| 133 | if revisionsAreEqual(dstList, forkList) { |
| 134 | if !revisionsAreEqual(srcList, forkList) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 135 | logger.Error("we should not be here") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 136 | } else { |
| 137 | for _, rev := range srcList { |
| 138 | newChildren[fieldName] = append(newChildren[fieldName], mergeChildFunc(rev)) |
| 139 | } |
| 140 | if field.IsContainer { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 141 | changes = append( |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 142 | changes, ChangeTuple{PostListchange, |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 143 | NewOperationContext("", nil, fieldName, ""), nil}, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 144 | ) |
| 145 | } |
| 146 | } |
| 147 | } else { |
| 148 | if !revisionsAreEqual(srcList, forkList) { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 149 | logger.Error("cannot merge - single child node or un-keyed children list has changed") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } else { |
| 153 | if revisionsAreEqual(dstList, forkList) { |
| 154 | src := newChangeAnalysis(forkList, srcList, field.Key) |
| 155 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 156 | newList := make([]Revision, len(srcList)) |
| 157 | copy(newList, srcList) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 158 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 159 | for key := range src.AddedKeys { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 160 | idx := src.KeyMap2[key] |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 161 | newRev := mergeChildFunc(newList[idx]) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 162 | |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 163 | // FIXME: newRev may come back as nil... exclude those entries for now |
| 164 | if newRev != nil { |
| 165 | newList[idx] = newRev |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 166 | changes = append(changes, ChangeTuple{PostAdd, newList[idx].GetData(), newRev.GetData()}) |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 167 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 168 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 169 | for key := range src.RemovedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 170 | oldRev := forkList[src.KeyMap1[key]] |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 171 | revsToDiscard = append(revsToDiscard, oldRev) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 172 | changes = append(changes, ChangeTuple{PostRemove, oldRev.GetData(), nil}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 173 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 174 | for key := range src.ChangedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 175 | idx := src.KeyMap2[key] |
| 176 | newRev := mergeChildFunc(newList[idx]) |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 177 | |
| 178 | // FIXME: newRev may come back as nil... exclude those entries for now |
| 179 | if newRev != nil { |
| 180 | newList[idx] = newRev |
| 181 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 184 | if !dryRun { |
| 185 | newChildren[fieldName] = newList |
| 186 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 187 | } else { |
| 188 | src := newChangeAnalysis(forkList, srcList, field.Key) |
| 189 | dst := newChangeAnalysis(forkList, dstList, field.Key) |
| 190 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 191 | newList := make([]Revision, len(dstList)) |
| 192 | copy(newList, dstList) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 193 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 194 | for key := range src.AddedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 195 | if _, exists := dst.AddedKeys[key]; exists { |
| 196 | childDstRev := dstList[dst.KeyMap2[key]] |
| 197 | childSrcRev := srcList[src.KeyMap2[key]] |
| 198 | if childDstRev.GetHash() == childSrcRev.GetHash() { |
| 199 | mergeChildFunc(childDstRev) |
| 200 | } else { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 201 | logger.Error("conflict error - revision has been added is different") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 202 | } |
| 203 | } else { |
| 204 | newRev := mergeChildFunc(srcList[src.KeyMap2[key]]) |
| 205 | newList = append(newList, newRev) |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 206 | changes = append(changes, ChangeTuple{PostAdd, srcList[src.KeyMap2[key]], newRev.GetData()}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 207 | } |
| 208 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 209 | for key := range src.ChangedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 210 | if _, removed := dst.RemovedKeys[key]; removed { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 211 | logger.Error("conflict error - revision has been removed") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 212 | } else if _, changed := dst.ChangedKeys[key]; changed { |
| 213 | childDstRev := dstList[dst.KeyMap2[key]] |
| 214 | childSrcRev := srcList[src.KeyMap2[key]] |
| 215 | if childDstRev.GetHash() == childSrcRev.GetHash() { |
| 216 | mergeChildFunc(childSrcRev) |
| 217 | } else if childDstRev.GetConfig().Hash != childSrcRev.GetConfig().Hash { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 218 | logger.Error("conflict error - revision has been changed and is different") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 219 | } else { |
| 220 | newRev := mergeChildFunc(srcList[src.KeyMap2[key]]) |
| 221 | newList[dst.KeyMap2[key]] = newRev |
| 222 | } |
| 223 | } else { |
| 224 | newRev := mergeChildFunc(srcList[src.KeyMap2[key]]) |
| 225 | newList[dst.KeyMap2[key]] = newRev |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // TODO: how do i sort this map in reverse order? |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 230 | for key := range src.RemovedKeys { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 231 | if _, changed := dst.ChangedKeys[key]; changed { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 232 | logger.Error("conflict error - revision has changed") |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 233 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 234 | if _, removed := dst.RemovedKeys[key]; !removed { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 235 | dstIdx := dst.KeyMap2[key] |
| 236 | oldRev := newList[dstIdx] |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 237 | revsToDiscard = append(revsToDiscard, oldRev) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 238 | |
| 239 | copy(newList[dstIdx:], newList[dstIdx+1:]) |
| 240 | newList[len(newList)-1] = nil |
| 241 | newList = newList[:len(newList)-1] |
| 242 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 243 | changes = append(changes, ChangeTuple{PostRemove, oldRev.GetData(), nil}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 247 | if !dryRun { |
| 248 | newChildren[fieldName] = newList |
| 249 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 254 | if !dryRun && len(newChildren) > 0 { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 255 | if configChanged { |
| 256 | rev = srcRev |
| 257 | } else { |
| 258 | rev = dstRev |
| 259 | } |
| 260 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 261 | for _, discarded := range revsToDiscard { |
| 262 | discarded.Drop("", true) |
| 263 | } |
| 264 | |
Stephane Barbarie | 260a563 | 2019-02-26 16:12:49 -0500 | [diff] [blame] | 265 | // FIXME: Do not discard the latest value for now |
| 266 | //dstRev.GetBranch().GetLatest().Drop("", configChanged) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 267 | rev = rev.UpdateAllChildren(ctx, newChildren, dstRev.GetBranch()) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 268 | |
| 269 | if configChanged { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 270 | changes = append(changes, ChangeTuple{PostUpdate, dstRev.GetBranch().GetLatest().GetData(), rev.GetData()}) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 271 | } |
| 272 | return rev, changes |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 273 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 274 | |
| 275 | return nil, nil |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 276 | } |