21
21
22
22
import org .elasticsearch .action .OriginalIndices ;
23
23
import org .elasticsearch .cluster .routing .PlainShardIterator ;
24
- import org .elasticsearch .cluster .routing .ShardIterator ;
25
24
import org .elasticsearch .cluster .routing .ShardRouting ;
26
25
import org .elasticsearch .common .Nullable ;
27
26
import org .elasticsearch .common .unit .TimeValue ;
27
+ import org .elasticsearch .common .util .Countable ;
28
+ import org .elasticsearch .common .util .PlainIterator ;
28
29
import org .elasticsearch .index .shard .ShardId ;
29
30
import org .elasticsearch .search .SearchShardTarget ;
30
31
import org .elasticsearch .search .internal .ShardSearchContextId ;
31
32
33
+ import java .util .Comparator ;
32
34
import java .util .List ;
33
35
import java .util .Objects ;
36
+ import java .util .stream .Collectors ;
34
37
35
38
/**
36
39
* Extension of {@link PlainShardIterator} used in the search api, which also holds the {@link OriginalIndices}
37
40
* of the search request (useful especially with cross-cluster search, as each cluster has its own set of original indices) as well as
38
41
* the cluster alias.
39
42
* @see OriginalIndices
40
43
*/
41
- public final class SearchShardIterator extends PlainShardIterator {
44
+ public final class SearchShardIterator implements Comparable < SearchShardIterator >, Countable {
42
45
43
46
private final OriginalIndices originalIndices ;
44
47
private final String clusterAlias ;
48
+ private final ShardId shardId ;
45
49
private boolean skip = false ;
46
50
47
51
private final ShardSearchContextId searchContextId ;
48
52
private final TimeValue searchContextKeepAlive ;
53
+ private final PlainIterator <String > targetNodesIterator ;
49
54
50
55
/**
51
56
* Creates a {@link PlainShardIterator} instance that iterates over a subset of the given shards
52
57
* this the a given <code>shardId</code>.
53
58
*
54
- * @param clusterAlias the alias of the cluster where the shard is located
55
- * @param shardId shard id of the group
56
- * @param shards shards to iterate
59
+ * @param clusterAlias the alias of the cluster where the shard is located
60
+ * @param shardId shard id of the group
61
+ * @param shards shards to iterate
57
62
* @param originalIndices the indices that the search request originally related to (before any rewriting happened)
58
63
*/
59
64
public SearchShardIterator (@ Nullable String clusterAlias , ShardId shardId , List <ShardRouting > shards , OriginalIndices originalIndices ) {
60
- this (clusterAlias , shardId , shards , originalIndices , null , null );
65
+ this (clusterAlias , shardId , shards .stream ().map (ShardRouting ::currentNodeId ).collect (Collectors .toList ()),
66
+ originalIndices , null , null );
61
67
}
62
68
63
69
public SearchShardIterator (@ Nullable String clusterAlias , ShardId shardId ,
64
- List <ShardRouting > shards , OriginalIndices originalIndices ,
70
+ List <String > targetNodeIds , OriginalIndices originalIndices ,
65
71
ShardSearchContextId searchContextId , TimeValue searchContextKeepAlive ) {
66
- super (shardId , shards );
72
+ this .shardId = shardId ;
73
+ this .targetNodesIterator = new PlainIterator <>(targetNodeIds );
67
74
this .originalIndices = originalIndices ;
68
75
this .clusterAlias = clusterAlias ;
69
76
this .searchContextId = searchContextId ;
@@ -86,12 +93,16 @@ public String getClusterAlias() {
86
93
return clusterAlias ;
87
94
}
88
95
89
- /**
90
- * Creates a new shard target from this iterator, pointing at the node identified by the provided identifier.
91
- * @see SearchShardTarget
92
- */
93
- SearchShardTarget newSearchShardTarget (String nodeId ) {
94
- return new SearchShardTarget (nodeId , shardId (), clusterAlias , originalIndices );
96
+ SearchShardTarget nextOrNull () {
97
+ final String nodeId = targetNodesIterator .nextOrNull ();
98
+ if (nodeId != null ) {
99
+ return new SearchShardTarget (nodeId , shardId , clusterAlias , originalIndices );
100
+ }
101
+ return null ;
102
+ }
103
+
104
+ int remaining () {
105
+ return targetNodesIterator .remaining ();
95
106
}
96
107
97
108
/**
@@ -105,6 +116,10 @@ TimeValue getSearchContextKeepAlive() {
105
116
return searchContextKeepAlive ;
106
117
}
107
118
119
+ List <String > getTargetNodeIds () {
120
+ return targetNodesIterator .asList ();
121
+ }
122
+
108
123
/**
109
124
* Reset the iterator and mark it as skippable
110
125
* @see #skip()
@@ -114,49 +129,44 @@ void resetAndSkip() {
114
129
skip = true ;
115
130
}
116
131
132
+ void reset () {
133
+ targetNodesIterator .reset ();
134
+ }
135
+
117
136
/**
118
137
* Returns <code>true</code> if the search execution should skip this shard since it can not match any documents given the query.
119
138
*/
120
139
boolean skip () {
121
140
return skip ;
122
141
}
123
142
143
+
144
+ @ Override
145
+ public int size () {
146
+ return targetNodesIterator .size ();
147
+ }
148
+
149
+ ShardId shardId () {
150
+ return shardId ;
151
+ }
152
+
124
153
@ Override
125
154
public boolean equals (Object o ) {
126
- if (this == o ) {
127
- return true ;
128
- }
129
- if (o == null || getClass () != o .getClass ()) {
130
- return false ;
131
- }
132
- if (super .equals (o ) == false ) {
133
- return false ;
134
- }
155
+ if (this == o ) return true ;
156
+ if (o == null || getClass () != o .getClass ()) return false ;
135
157
SearchShardIterator that = (SearchShardIterator ) o ;
136
- return Objects .equals (clusterAlias , that .clusterAlias );
158
+ return shardId . equals ( that . shardId ) && Objects .equals (clusterAlias , that .clusterAlias );
137
159
}
138
160
139
161
@ Override
140
162
public int hashCode () {
141
- return Objects .hash (super . hashCode (), clusterAlias );
163
+ return Objects .hash (clusterAlias , shardId );
142
164
}
143
165
144
166
@ Override
145
- public int compareTo (ShardIterator o ) {
146
- int superCompareTo = super .compareTo (o );
147
- if (superCompareTo != 0 || (o instanceof SearchShardIterator == false )) {
148
- return superCompareTo ;
149
- }
150
- SearchShardIterator searchShardIterator = (SearchShardIterator )o ;
151
- if (clusterAlias == null && searchShardIterator .getClusterAlias () == null ) {
152
- return 0 ;
153
- }
154
- if (clusterAlias == null ) {
155
- return -1 ;
156
- }
157
- if (searchShardIterator .getClusterAlias () == null ) {
158
- return 1 ;
159
- }
160
- return clusterAlias .compareTo (searchShardIterator .getClusterAlias ());
167
+ public int compareTo (SearchShardIterator o ) {
168
+ return Comparator .comparing (SearchShardIterator ::shardId )
169
+ .thenComparing (SearchShardIterator ::getClusterAlias , Comparator .nullsFirst (String ::compareTo ))
170
+ .compare (this , o );
161
171
}
162
172
}
0 commit comments