34
34
35
35
import org .apache .logging .log4j .LogManager ;
36
36
import org .apache .logging .log4j .Logger ;
37
+ import org .opensearch .cluster .node .DiscoveryNode ;
37
38
import org .opensearch .cluster .routing .RecoverySource ;
38
39
import org .opensearch .cluster .routing .RoutingNode ;
40
+ import org .opensearch .cluster .routing .RoutingNodes ;
39
41
import org .opensearch .cluster .routing .ShardRouting ;
40
42
import org .opensearch .cluster .routing .allocation .AllocateUnassignedDecision ;
41
43
import org .opensearch .cluster .routing .allocation .AllocationDecision ;
42
44
import org .opensearch .cluster .routing .allocation .ExistingShardsAllocator ;
43
45
import org .opensearch .cluster .routing .allocation .NodeAllocationResult ;
44
46
import org .opensearch .cluster .routing .allocation .RoutingAllocation ;
45
47
import org .opensearch .cluster .routing .allocation .decider .Decision ;
48
+ import org .opensearch .indices .replication .checkpoint .ReplicationCheckpoint ;
46
49
47
50
import java .util .ArrayList ;
51
+ import java .util .Comparator ;
52
+ import java .util .HashMap ;
53
+ import java .util .Iterator ;
48
54
import java .util .List ;
55
+ import java .util .Set ;
56
+ import java .util .TreeMap ;
57
+ import java .util .concurrent .ConcurrentMap ;
58
+ import java .util .stream .Collectors ;
49
59
50
60
/**
51
61
* An abstract class that implements basic functionality for allocating
52
62
* shards to nodes based on shard copies that already exist in the cluster.
53
- *
63
+ * <p>
54
64
* Individual implementations of this class are responsible for providing
55
65
* the logic to determine to which nodes (if any) those shards are allocated.
56
66
*
@@ -64,8 +74,9 @@ public abstract class BaseGatewayShardAllocator {
64
74
* Allocate an unassigned shard to nodes (if any) where valid copies of the shard already exist.
65
75
* It is up to the individual implementations of {@link #makeAllocationDecision(ShardRouting, RoutingAllocation, Logger)}
66
76
* to make decisions on assigning shards to nodes.
67
- * @param shardRouting the shard to allocate
68
- * @param allocation the allocation state container object
77
+ *
78
+ * @param shardRouting the shard to allocate
79
+ * @param allocation the allocation state container object
69
80
* @param unassignedAllocationHandler handles the allocation of the current shard
70
81
*/
71
82
public void allocateUnassigned (
@@ -109,9 +120,9 @@ protected long getExpectedShardSize(ShardRouting shardRouting, RoutingAllocation
109
120
* {@link #allocateUnassigned(ShardRouting, RoutingAllocation, ExistingShardsAllocator.UnassignedAllocationHandler)} to make decisions
110
121
* about whether or not the shard can be allocated by this allocator and if so, to which node it will be allocated.
111
122
*
112
- * @param unassignedShard the unassigned shard to allocate
113
- * @param allocation the current routing state
114
- * @param logger the logger
123
+ * @param unassignedShard the unassigned shard to allocate
124
+ * @param allocation the current routing state
125
+ * @param logger the logger
115
126
* @return an {@link AllocateUnassignedDecision} with the final decision of whether to allocate and details of the decision
116
127
*/
117
128
public abstract AllocateUnassignedDecision makeAllocationDecision (
@@ -132,4 +143,68 @@ protected static List<NodeAllocationResult> buildDecisionsForAllNodes(ShardRouti
132
143
}
133
144
return results ;
134
145
}
146
+
147
+ protected static class NodeShardState {
148
+ private final String allocationId ;
149
+ private final boolean primary ;
150
+ private final Exception storeException ;
151
+ private final ReplicationCheckpoint replicationCheckpoint ;
152
+ private final DiscoveryNode node ;
153
+
154
+ public NodeShardState (DiscoveryNode node ,
155
+ String allocationId ,
156
+ boolean primary ,
157
+ ReplicationCheckpoint replicationCheckpoint ,
158
+ Exception storeException ) {
159
+ this .node = node ;
160
+ this .allocationId = allocationId ;
161
+ this .primary = primary ;
162
+ this .replicationCheckpoint = replicationCheckpoint ;
163
+ this .storeException = storeException ;
164
+ }
165
+
166
+ public String allocationId () {
167
+ return this .allocationId ;
168
+ }
169
+
170
+ public boolean primary () {
171
+ return this .primary ;
172
+ }
173
+
174
+ public ReplicationCheckpoint replicationCheckpoint () {
175
+ return this .replicationCheckpoint ;
176
+ }
177
+
178
+ public Exception storeException () {
179
+ return this .storeException ;
180
+ }
181
+
182
+ public DiscoveryNode getNode () {
183
+ return this .node ;
184
+ }
185
+ }
186
+
187
+ protected static class NodeShardStates {
188
+ TreeMap <NodeShardState , DiscoveryNode > nodeShardStates ;
189
+
190
+ public NodeShardStates (Comparator <NodeShardState > comparator ) {
191
+ this .nodeShardStates = new TreeMap <>(comparator );
192
+ }
193
+
194
+ public void add (NodeShardState key , DiscoveryNode value ) {
195
+ this .nodeShardStates .put (key , value );
196
+ }
197
+
198
+ public DiscoveryNode get (NodeShardState key ) {
199
+ return this .nodeShardStates .get (key );
200
+ }
201
+
202
+ public int size () {
203
+ return this .nodeShardStates .size ();
204
+ }
205
+
206
+ public Iterator <NodeShardState > iterator () {
207
+ return this .nodeShardStates .keySet ().iterator ();
208
+ }
209
+ }
135
210
}
0 commit comments