aboutsummaryrefslogtreecommitdiffstats
path: root/mdbc-server/src/main/java/org/onap/music/mdbc/ownership/Dag.java
blob: 142cb34631b84ace05e6356933c2dc6fe9cca584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * ============LICENSE_START====================================================
 * org.onap.music.mdbc
 * =============================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END======================================================
 */

package org.onap.music.mdbc.ownership;

import java.util.*;
import java.util.stream.Collectors;

import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.music.exceptions.MDBCServiceException;
import org.onap.music.logging.EELFLoggerDelegate;
import org.onap.music.mdbc.DatabasePartition;
import org.onap.music.mdbc.Range;
import org.onap.music.mdbc.tables.MriReference;
import org.onap.music.mdbc.tables.MriRowComparator;
import org.onap.music.mdbc.tables.MusicRangeInformationRow;
import org.onap.music.mdbc.tables.MusicTxDigestId;

public class Dag {

    private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(Dag.class);

    private boolean valid;
    private boolean ownInit;
    private boolean readyInit;
    private Map<UUID,DagNode> nodes;
    private Queue<DagNode> readyNodes;
    private Queue<DagNode> toApplyNodes;
    private Map<Range,Set<DagNode>> rowsPerLatestRange;
    private List<Range> ranges;

    public Dag(){
        this(false);
    }


    public Dag(boolean isValid){
        valid=isValid;
        ranges=null;
        readyNodes = new LinkedList<>();
        toApplyNodes = new LinkedList<>();
        nodes = new HashMap<>();
        ownInit = false;
        readyInit = false;
        rowsPerLatestRange = null;
    }

    private void createDag(List<MusicRangeInformationRow> rows, Set<Range> ranges){
        this.ranges = new ArrayList<>(ranges);
        Map<Range,DagNode> latestRow = new HashMap<>();
        //sort to make sure rows are in chronological order
        Collections.sort(rows, new MriRowComparator());
        for(MusicRangeInformationRow row : rows){
            if(!nodes.containsKey(row.getPartitionIndex())){
                DagNode node = new DagNode(row);
                nodes.put(row.getPartitionIndex(),node);
                for(Range range : ranges){
                    Set<Range> nodeRanges = row.getDBPartition().getSnapshot();
                    for(Range nRange : nodeRanges){
                        if(nRange.overlaps(range)){
                            if(latestRow.containsKey(range)){
                                final DagNode dagNode = latestRow.get(range);
                                dagNode.addOutgoingEdge(node);
                                node.addIncomingEdge(dagNode);
                            }
                            latestRow.put(range,node);
                        }
                    }
                }
            }
        }
    }

    public static Dag getDag(List<MusicRangeInformationRow> rows, Set<Range> ranges){
        Dag newDag = new Dag(true);
        newDag.createDag(rows,ranges);
        return newDag;
    }

    public void setRowsPerLatestRange(Map<Range, Set<DagNode>> rowsPerLatestRange) {
        this.rowsPerLatestRange = rowsPerLatestRange;
    }

    private void initApplyDatastructures(){
        readyInit=true;
        nodes.forEach((id, node) -> {
            if(node.hasNotIncomingEdges()) {
                toApplyNodes.add(node);
            }
        });
    }

    private void initOwnDatastructures(){
        ownInit = true;
        nodes.forEach((id, node) -> {
            if(node.hasNotIncomingEdges()) {
                readyNodes.add(node);
            }
        });
    }

    public DagNode getNode(UUID rowId) {
        if(!nodes.containsKey(rowId)){
            return null;
        }
        return nodes.get(rowId);
    }

    public synchronized boolean hasNextToOwn(){
        if(!ownInit){
            initOwnDatastructures();
        }
        return !readyNodes.isEmpty();
    }

    public synchronized DagNode nextToOwn() throws MDBCServiceException {
        if(!ownInit){
            initOwnDatastructures();
        }
        DagNode nextNode = readyNodes.poll();
        if(nextNode == null){
            throw new MDBCServiceException("Next To Own was call without checking has next to own");
        }
        return nextNode;
    }

    public synchronized DagNode nextToApply(Set<Range> ranges){
        if(!readyInit){
            initApplyDatastructures();
        }
        while(!toApplyNodes.isEmpty()){
            DagNode nextNode = toApplyNodes.poll();
            List<DagNode> outgoing = nextNode.getOutgoingEdges();
            for(DagNode out : outgoing){
                out.setApplyDependencyReady(nextNode);
                if(out.areApplyDependenciesReady()){
                    toApplyNodes.add(out);
                }
            }
            if(!nextNode.wasApplied(ranges)){
                return nextNode;
            }
        }
        return null;
    }

    public synchronized boolean isDifferent(Dag other){
        Set<DagNode> thisSet = new HashSet<>(nodes.values());
        Set<DagNode> otherSet = new HashSet<>(other.nodes.values());
        return !(thisSet.size()==otherSet.size() &&
            thisSet.containsAll(otherSet));
    }

    public synchronized boolean isOwned(){
        if(!valid){
            return false;
        }
        else if(nodes.isEmpty()){
            return true;
        }
        for(Map.Entry<UUID,DagNode> pair : nodes.entrySet()){
            if(!pair.getValue().isOwned()){
                return false;
            }
        }
        return true;
    }

    public void setOwn(DagNode node) throws MDBCServiceException {
        if(node == null){
            throw new MDBCServiceException("Set Own was call with a null node");
        }
        final DagNode dagNode = nodes.get(node.getId());
        if(dagNode == null){
            throw new MDBCServiceException("Set Own was call with a node that is not in the DAG");
        }
        dagNode.setOwned();
        for(DagNode next: dagNode.getOutgoingEdges()){
            next.setOwnDependencyReady(dagNode);
            if (next.areOwnDependenciesReady()) {
               readyNodes.add(next);
            }
        }
    }

    public void setReady(DagNode node, Range range) throws MDBCServiceException {
        if(node == null){
            throw new MDBCServiceException("Set Ready was call with a null node");
        }
        final DagNode dagNode = nodes.get(node.getId());
        if(dagNode == null){
            throw new MDBCServiceException("Set Ready was call with a node that is not in the DAG");
        }
        dagNode.addReady(range);
    }

    public void setPartiallyReady(DagNode node, Range range, int index) throws MDBCServiceException {
        if(node == null){
            throw new MDBCServiceException("Set Ready was call with a null node");
        }
        final DagNode dagNode = nodes.get(node.getId());
        if(dagNode == null){
            throw new MDBCServiceException("Set Ready was call with a node that is not in the DAG");
        }
        dagNode.addPartiallyReady(range,index);
    }

    public synchronized boolean applied(){
        if(!valid) {
            return false;
        }
        if(!readyInit){
            initApplyDatastructures();
        }
        return toApplyNodes.isEmpty();
    }

    public void setAlreadyApplied(Map<Range, Pair<MriReference,MusicTxDigestId>> alreadyApplied, Set<Range> ranges)
        throws MDBCServiceException {
        for (DagNode node: nodes.values()) {
            Set<Range> intersection = new HashSet<>(ranges);
            intersection.retainAll(node.getRangeSet());
            for(Range r : intersection){
                if(alreadyApplied.containsKey(r)){
                    final Pair<MriReference, MusicTxDigestId> appliedPair = alreadyApplied.get(r);
                    final MriReference appliedRow = appliedPair.getKey();
                    final int index = appliedPair.getValue().index;
                    final long appliedTimestamp = appliedRow.getTimestamp();
                    final long nodeTimestamp = node.getTimestamp();
                    if(appliedTimestamp > nodeTimestamp){
                        setReady(node,r);
                    }
                    else if(appliedTimestamp == nodeTimestamp){
                        setPartiallyReady(node,r,index);
                    }
                }
            }
        }
    }

    public void addNewNode(MusicRangeInformationRow row, List<DagNode> dependencies) throws MDBCServiceException {
        boolean found=false;
        if (ranges != null) {
            DatabasePartition dbPartition = row.getDBPartition();
            for(Range range : dbPartition.getSnapshot()){
                for(Range dagRange : ranges){
                    if(dagRange.overlaps(range)){
                        found = true;
                        break;
                    }
                }
                if(found) break;
            }
            if(!found) {
                return;
            }
        }

        DagNode newNode = new DagNode(row);
        nodes.put(row.getPartitionIndex(),newNode);
        for(DagNode dependency : dependencies) {
            newNode.addIncomingEdge(dependency);
            DagNode localNode = getNode(dependency.getId());
            localNode.addOutgoingEdge(newNode);
        }
    }

    public void addNewNodeWithSearch(MusicRangeInformationRow row, Set<Range> ranges) throws MDBCServiceException {
        Map<Range,DagNode> newestNode = new HashMap<>();
        for(DagNode node : nodes.values()){
            for(Range range : ranges) {
                if (node.getRangeSet().contains(range)){
                   if(!newestNode.containsKey(range)){
                        newestNode.put(range,node);
                   }
                   else{
                       DagNode current = newestNode.get(range);
                       if(node.getTimestamp() > current.getTimestamp()){
                           newestNode.put(range,node);
                       }
                   }
                }
            }
        }
        List<DagNode> dependencies = newestNode.values().stream().distinct().collect(Collectors.toList());
        addNewNode(row,dependencies);
    }

    /**
     * 
     * @return All ranges in every node of the DAG
     */
    public Set<Range> getAllRanges(){
        Set<Range> ranges = new HashSet<>();
        for(DagNode node : nodes.values()){
            ranges.addAll(node.getRangeSet());
        }
        return ranges;
    }

    public void setIsLatest(UUID id, boolean isLatest){
        DagNode dagNode = nodes.get(id);
        dagNode.setIsLatest(isLatest);
        if(isLatest) {
            MusicRangeInformationRow row = dagNode.getRow();
            DatabasePartition dbPartition = row.getDBPartition();
            for (Range range : dbPartition.getSnapshot()) {
                if (!rowsPerLatestRange.containsKey(range)) {
                    rowsPerLatestRange.put(range, new HashSet<>());
                }
                rowsPerLatestRange.get(range).add(dagNode);
            }
        }
        else{
            MusicRangeInformationRow row = dagNode.getRow();
            DatabasePartition dbPartition = row.getDBPartition();
            for (Range range : dbPartition.getSnapshot()) {
                if (rowsPerLatestRange.containsKey(range)) {
                    rowsPerLatestRange.get(range).remove(dagNode);
                }
            }
        }
    }

    private Map<Range,Set<DagNode>> getIsLatestPerRange(){
        if(rowsPerLatestRange == null){
            rowsPerLatestRange = new HashMap<>();
        }
        for(DagNode node : nodes.values()){
            MusicRangeInformationRow row = node.getRow();
            DatabasePartition dbPartition = row.getDBPartition();
            if (row.getIsLatest()) {
                for(Range range : dbPartition.getSnapshot()){
                    if(!rowsPerLatestRange.containsKey(range)){
                        rowsPerLatestRange.put(range,new HashSet<>());
                    }
                    rowsPerLatestRange.get(range).add(node);
                }
            }
        }
        return new HashMap<>(rowsPerLatestRange);
    }

    private List<DagNode> getOldestDoubleRows(Map<Range,Set<DagNode>> rowPerLatestRange) throws MDBCServiceException {
        Set<DagNode> oldest = new HashSet<>();
        for(Map.Entry<Range,Set<DagNode>> rangeAndNodes : rowPerLatestRange.entrySet()){
            Range range = rangeAndNodes.getKey();
            Set<DagNode> nodes = rangeAndNodes.getValue();
            if(nodes.size() > 2){
                logger.error("Range "+range.getTable()+"has more than 2 active rows");
                throw new MDBCServiceException("Range has more than 2 active rows");
            }
            else if(nodes.size()==2){
                DagNode older = null;
                long olderTimestamp = Long.MAX_VALUE;
                for(DagNode node : nodes){
                    if(olderTimestamp > node.getTimestamp()){
                        older  = node;
                        olderTimestamp=node.getTimestamp();
                    }
                }
                oldest.add(older);
            }
        }
        return new ArrayList<>(oldest);
    }

    public List<DagNode> getOldestDoubles() throws MDBCServiceException{
        Map<Range,Set<DagNode>> rowsPerLatestRange = getIsLatestPerRange();
        List<DagNode> toDisable = getOldestDoubleRows(rowsPerLatestRange);
        return toDisable;
    }

    public Pair<Set<Range>, Set<DagNode>> getIncompleteRangesAndDependents() throws MDBCServiceException {
        Set<Range> incomplete = new HashSet<>();
        Set<DagNode> dependents = new HashSet<>();
        Map<Range,Set<DagNode>> rowsPerLatestRange = getIsLatestPerRange();
        List<DagNode> toDisable = getOldestDoubleRows(rowsPerLatestRange);
        for(DagNode node : toDisable) {
            for (Range range : node.getRangeSet()) {
                rowsPerLatestRange.get(range).remove(node);
                if (rowsPerLatestRange.get(range).size() == 0) {
                    incomplete.add(range);
                    dependents.add(node);
                }
            }
        }
        return Pair.of(incomplete,dependents);
    }
}