-
Notifications
You must be signed in to change notification settings - Fork 90
/
ColocationGroup.java
258 lines (220 loc) · 9.43 KB
/
ColocationGroup.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.ignite.internal.sql.engine.metadata;
import static org.apache.ignite.internal.util.ArrayUtils.asList;
import static org.apache.ignite.internal.util.CollectionUtils.first;
import static org.apache.ignite.internal.util.CollectionUtils.nullOrEmpty;
import static org.apache.ignite.internal.util.IgniteUtils.firstNotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.apache.ignite.internal.sql.engine.rel.IgniteRel;
import org.apache.ignite.internal.sql.engine.util.Commons;
import org.apache.ignite.internal.util.IgniteIntList;
import org.jetbrains.annotations.NotNull;
/**
* ColocationGroup.
* TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
*/
public class ColocationGroup implements Serializable {
private static final int SYNTHETIC_PARTITIONS_COUNT = 512;
// TODO: IgniteSystemProperties.getInteger("IGNITE_CALCITE_SYNTHETIC_PARTITIONS_COUNT", 512);
private final List<Long> sourceIds;
private final List<String> nodeNames;
private final List<List<String>> assignments;
/**
* ForNodes.
* TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
*/
public static ColocationGroup forNodes(List<String> nodeNames) {
return new ColocationGroup(null, nodeNames, null);
}
/**
* ForAssignments.
* TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
*/
public static ColocationGroup forAssignments(List<List<String>> assignments) {
return new ColocationGroup(null, null, assignments);
}
/**
* ForSourceId.
* TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
*/
public static ColocationGroup forSourceId(long sourceId) {
return new ColocationGroup(Collections.singletonList(sourceId), null, null);
}
/**
* Constructor.
* TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
*/
private ColocationGroup(List<Long> sourceIds, List<String> nodeNames, List<List<String>> assignments) {
this.sourceIds = sourceIds;
this.nodeNames = nodeNames;
this.assignments = assignments;
}
/**
* Get lists of colocation group sources.
*/
public List<Long> sourceIds() {
return sourceIds == null ? Collections.emptyList() : sourceIds;
}
/**
* Get lists of nodes capable to execute a query fragment for what the mapping is calculated.
*/
public List<String> nodeNames() {
return nodeNames == null ? Collections.emptyList() : nodeNames;
}
/**
* Get list of partitions (index) and nodes (items) having an appropriate partition in OWNING state, calculated for
* distributed tables, involved in query execution.
*/
public List<List<String>> assignments() {
return assignments == null ? Collections.emptyList() : assignments;
}
/**
* Prunes involved partitions (hence nodes, involved in query execution) on the basis of filter, its distribution,
* query parameters and original nodes mapping.
*
* @param rel Filter.
* @return Resulting nodes mapping.
*/
public ColocationGroup prune(IgniteRel rel) {
return this; // TODO https://issues.apache.org/jira/browse/IGNITE-12455
}
/**
* Belongs.
* TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
*/
public boolean belongs(long sourceId) {
return sourceIds != null && sourceIds.contains(sourceId);
}
/**
* Merges this mapping with given one.
*
* @param other Mapping to merge with.
* @return Merged nodes mapping.
* @throws ColocationMappingException If involved nodes intersection is empty, hence there is no nodes capable to
* execute being calculated fragment.
*/
public ColocationGroup colocate(ColocationGroup other) throws ColocationMappingException {
List<Long> sourceIds;
if (this.sourceIds == null || other.sourceIds == null) {
sourceIds = firstNotNull(this.sourceIds, other.sourceIds);
} else {
sourceIds = Commons.combine(this.sourceIds, other.sourceIds);
}
List<String> nodeNames;
if (this.nodeNames == null || other.nodeNames == null) {
nodeNames = firstNotNull(this.nodeNames, other.nodeNames);
} else {
nodeNames = Commons.intersect(other.nodeNames, this.nodeNames);
}
if (nodeNames != null && nodeNames.isEmpty()) {
throw new ColocationMappingException("Failed to map fragment to location. "
+ "Replicated query parts are not co-located on all nodes");
}
List<List<String>> assignments;
if (this.assignments == null || other.assignments == null) {
assignments = firstNotNull(this.assignments, other.assignments);
if (assignments != null && nodeNames != null) {
Set<String> filter = new HashSet<>(nodeNames);
List<List<String>> assignments0 = new ArrayList<>(assignments.size());
for (int i = 0; i < assignments.size(); i++) {
List<String> assignment = Commons.intersect(filter, assignments.get(i));
if (assignment.isEmpty()) { // TODO check with partition filters
throw new ColocationMappingException("Failed to map fragment to location. "
+ "Partition mapping is empty [part=" + i + "]");
}
assignments0.add(assignment);
}
assignments = assignments0;
}
} else {
assert this.assignments.size() == other.assignments.size();
assignments = new ArrayList<>(this.assignments.size());
Set<String> filter = nodeNames == null ? null : new HashSet<>(nodeNames);
for (int i = 0; i < this.assignments.size(); i++) {
List<String> assignment = Commons.intersect(this.assignments.get(i), other.assignments.get(i));
if (filter != null) {
assignment.retainAll(filter);
}
if (assignment.isEmpty()) { // TODO check with partition filters
throw new ColocationMappingException("Failed to map fragment to location. Partition mapping is empty [part=" + i + "]");
}
assignments.add(assignment);
}
}
return new ColocationGroup(sourceIds, nodeNames, assignments);
}
/**
* Constructor.
* TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
*/
public ColocationGroup complete() {
if (assignments == null && nodeNames == null) {
return this;
}
if (assignments != null) {
List<List<String>> assignments = new ArrayList<>(this.assignments.size());
Set<String> nodes = new HashSet<>();
for (List<String> assignment : this.assignments) {
String first = first(assignment);
if (first != null) {
nodes.add(first);
}
assignments.add(first != null ? Collections.singletonList(first) : Collections.emptyList());
}
return new ColocationGroup(sourceIds, new ArrayList<>(nodes), assignments);
}
return forNodes0(nodeNames);
}
/**
* MapToNodes.
* TODO Documentation https://issues.apache.org/jira/browse/IGNITE-15859
*/
public ColocationGroup mapToNodes(List<String> nodeNames) {
return !nullOrEmpty(this.nodeNames) ? this : forNodes0(nodeNames);
}
@NotNull
private ColocationGroup forNodes0(List<String> nodeNames) {
List<List<String>> assignments = new ArrayList<>(SYNTHETIC_PARTITIONS_COUNT);
for (int i = 0; i < SYNTHETIC_PARTITIONS_COUNT; i++) {
assignments.add(asList(nodeNames.get(i % nodeNames.size())));
}
return new ColocationGroup(sourceIds, nodeNames, assignments);
}
/**
* Returns List of partitions to scan on the given node.
*
* @param nodeNames Cluster node consistent ID.
* @return List of partitions to scan on the given node.
*/
public int[] partitions(String nodeNames) {
IgniteIntList parts = new IgniteIntList(assignments.size());
for (int i = 0; i < assignments.size(); i++) {
List<String> assignment = assignments.get(i);
if (Objects.equals(nodeNames, first(assignment))) {
parts.add(i);
}
}
return parts.array();
}
}