aboutsummaryrefslogtreecommitdiffstats
path: root/mdbc-server/src/main/java/org/onap/music/mdbc/ownership/DagNode.java
blob: 78c68e10a3c61657847959e0707426e21e24ab87 (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
/*
 * ============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.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.music.exceptions.MDBCServiceException;
import org.onap.music.mdbc.Range;
import org.onap.music.mdbc.tables.MusicRangeInformationRow;
import org.onap.music.mdbc.tables.MusicTxDigestId;

public class DagNode {

    private boolean owned;
    private boolean applyInit;
    private final MusicRangeInformationRow row;
    private int currentIndex;
    private Set<DagNode> dependencies;
    private Set<DagNode> outgoingEdges;
    private Set<DagNode> readyOwnDependencies;
    private Set<DagNode> readyAppliedDependencies;
    private List<Range> alreadyApplied;
    private Map<Range,Integer> partiallyApplied;
    private Map<Range,Integer> startIndex;

    public DagNode(MusicRangeInformationRow row){
        this.row = row;
        owned = false;
        applyInit = false;
        currentIndex = 0;
        dependencies = new HashSet<>();
        outgoingEdges = new HashSet<>();
        readyOwnDependencies = new HashSet<>();
        readyAppliedDependencies = new HashSet<>();
        alreadyApplied = new ArrayList<>();
        partiallyApplied = new HashMap<>();
        startIndex = new HashMap<>();
    }

    public MusicRangeInformationRow getRow() {
        return row;
    }

    public synchronized void setOwned(){
        owned = true;
    }

    public synchronized boolean isOwned(){
        return owned;
    }

    public UUID getId(){
        return row.getPartitionIndex();
    }

    public synchronized void addIncomingEdge(DagNode sourceNode){
        dependencies.add(sourceNode);
    }

    public synchronized void addOutgoingEdge(DagNode destinationNode){
        outgoingEdges.add(destinationNode);
    }

    public synchronized  boolean hasNotIncomingEdges(){
        return dependencies.isEmpty();
    }

    public synchronized List<DagNode> getOutgoingEdges(){
       return new ArrayList<>(outgoingEdges);
    }

    public synchronized  void addReady(Range r) throws MDBCServiceException {
        if(!row.getDBPartition().isContained(r)){
            throw new MDBCServiceException("Range was set ready to a node that doesn't own it");
        }
        alreadyApplied.add(r);
    }

    public synchronized void addPartiallyReady(Range r, int index){
        partiallyApplied.put(r,index);
    }

    public synchronized void setOwnDependencyReady(DagNode other){
        readyOwnDependencies.add(other);
    }

    public synchronized boolean areOwnDependenciesReady(){
        final int dSize = dependencies.size();
        final int oSize = readyOwnDependencies.size();
        return (dSize == oSize) && dependencies.containsAll(readyOwnDependencies);
    }

    public synchronized void setApplyDependencyReady(DagNode other){
        readyAppliedDependencies.add(other);
    }

    public synchronized boolean areApplyDependenciesReady(){
        final int dSize = dependencies.size();
        final int oSize = readyAppliedDependencies.size();
        return (dSize == oSize) && dependencies.containsAll(readyAppliedDependencies);
    }

    private void initializeApply(Set<Range> ranges){
        applyInit = true;
        int redoSize = row.getRedoLog().size();
        // No need to apply
        for(Range r: alreadyApplied){
            startIndex.put(r,redoSize);
        }
        // Only apply the required subsection
        partiallyApplied.forEach((r, index) -> {
            startIndex.put(r,index);
        });
        // All other ranges need to be applied completely
        Set<Range> alreadySet = new HashSet<>(alreadyApplied);
        Set<Range> partialSet = partiallyApplied.keySet();
        Set<Range> pending = new HashSet<>(ranges);
        pending.removeAll(alreadySet);
        pending.removeAll(partialSet);
        for(Range r: pending){
            startIndex.put(r,-1);
        }
        //Get the index of the redo log to begin with
        currentIndex = startIndex.values().stream().mapToInt(v->v).min().orElse(0);
        currentIndex = currentIndex+1;
    }

    public synchronized Pair<MusicTxDigestId, List<Range>> nextNotAppliedTransaction(Set<Range> ranges){
        if(row.getRedoLog().isEmpty()) return null;
        if(!applyInit){
            initializeApply(ranges);
        }
        final List<MusicTxDigestId> redoLog = row.getRedoLog();
        if(currentIndex  < redoLog.size()){
            List<Range> responseRanges= new ArrayList<>();
            startIndex.forEach((r, index) -> {
                if(index < currentIndex){
                   responseRanges.add(r);
                }
            });
            return Pair.of(redoLog.get(currentIndex++),responseRanges);
        }
        return null;
    }

    public void setIsLatest(boolean isLatest){
        row.setIsLatest(isLatest);
    }

    public synchronized Set<Range> getRangeSet(){
       return new HashSet<>(row.getDBPartition().getSnapshot());
    }

    public synchronized boolean wasApplied(Set<Range> ranges){
        if(row.getRedoLog().isEmpty()) return true;
        if(!applyInit){
            initializeApply(ranges);
        }
        return currentIndex >= row.getRedoLog().size();
    }

    public long getTimestamp(){
        return row.getTimestamp();
    }


    @Override
    public boolean equals(Object o){
        if (this == o) return true;
        if(o == null) return false;
        if(!(o instanceof DagNode)) return false;
        DagNode other = (DagNode) o;
        return other.row.getPartitionIndex().equals(this.row.getPartitionIndex());
    }

    @Override
    public int hashCode(){
        return row.getPartitionIndex().hashCode();
    }
}