@@ -65,7 +65,10 @@ public final class SearchRequest extends ActionRequest implements IndicesRequest
6565 public static final int DEFAULT_PRE_FILTER_SHARD_SIZE = 128 ;
6666 public static final int DEFAULT_BATCHED_REDUCE_SIZE = 512 ;
6767
68+ private static final long DEFAULT_ABSOLUTE_START_MILLIS = -1 ;
69+
6870 private String localClusterAlias ;
71+ private long absoluteStartMillis ;
6972
7073 private SearchType searchType = SearchType .DEFAULT ;
7174
@@ -98,6 +101,7 @@ public final class SearchRequest extends ActionRequest implements IndicesRequest
98101
99102 public SearchRequest () {
100103 this .localClusterAlias = null ;
104+ this .absoluteStartMillis = DEFAULT_ABSOLUTE_START_MILLIS ;
101105 }
102106
103107 /**
@@ -118,6 +122,7 @@ public SearchRequest(SearchRequest searchRequest) {
118122 this .source = searchRequest .source ;
119123 this .types = searchRequest .types ;
120124 this .localClusterAlias = searchRequest .localClusterAlias ;
125+ this .absoluteStartMillis = searchRequest .absoluteStartMillis ;
121126 }
122127
123128 /**
@@ -141,12 +146,17 @@ public SearchRequest(String[] indices, SearchSourceBuilder source) {
141146 }
142147
143148 /**
144- * Creates a new search request by providing the alias of the cluster where it will be executed. Used when a {@link SearchRequest}
145- * is created and executed as part of a cross-cluster search request performing local reduction on each cluster.
146- * The coordinating CCS node provides the alias to prefix index names with in the returned search results.
149+ * Creates a new search request by providing the alias of the cluster where it will be executed, as well as the current time in
150+ * milliseconds from the epoch time. Used when a {@link SearchRequest} is created and executed as part of a cross-cluster search
151+ * request performing local reduction on each cluster. The coordinating CCS node provides the alias to prefix index names with in
152+ * the returned search results, and the current time to be used on the remote clusters to ensure that the same value is used.
147153 */
148- SearchRequest (String localClusterAlias ) {
154+ SearchRequest (String localClusterAlias , long absoluteStartMillis ) {
149155 this .localClusterAlias = Objects .requireNonNull (localClusterAlias , "cluster alias must not be null" );
156+ if (absoluteStartMillis < 0 ) {
157+ throw new IllegalArgumentException ("absoluteStartMillis must not be negative but was [" + absoluteStartMillis + "]" );
158+ }
159+ this .absoluteStartMillis = absoluteStartMillis ;
150160 }
151161
152162 @ Override
@@ -185,6 +195,17 @@ String getLocalClusterAlias() {
185195 return localClusterAlias ;
186196 }
187197
198+ /**
199+ * Returns the current time in milliseconds from the time epoch, to be used for the execution of this search request. Used to
200+ * ensure that the same value, determined by the coordinating node, is used on all nodes involved in the execution of the search
201+ * request. When created through {@link #SearchRequest(String, long)}, this method returns the provided current time, otherwise
202+ * it will return {@link System#currentTimeMillis()}.
203+ *
204+ */
205+ long getOrCreateAbsoluteStartMillis () {
206+ return absoluteStartMillis == DEFAULT_ABSOLUTE_START_MILLIS ? System .currentTimeMillis () : absoluteStartMillis ;
207+ }
208+
188209 /**
189210 * Sets the indices the search will be executed on.
190211 */
@@ -367,8 +388,7 @@ public SearchRequest allowPartialSearchResults(boolean allowPartialSearchResults
367388
368389 public Boolean allowPartialSearchResults () {
369390 return this .allowPartialSearchResults ;
370- }
371-
391+ }
372392
373393 /**
374394 * Sets the number of shard results that should be reduced at once on the coordinating node. This value should be used as a protection
@@ -496,8 +516,14 @@ public void readFrom(StreamInput in) throws IOException {
496516 }
497517 if (in .getVersion ().onOrAfter (Version .V_6_7_0 )) {
498518 localClusterAlias = in .readOptionalString ();
519+ if (localClusterAlias != null ) {
520+ absoluteStartMillis = in .readVLong ();
521+ } else {
522+ absoluteStartMillis = DEFAULT_ABSOLUTE_START_MILLIS ;
523+ }
499524 } else {
500525 localClusterAlias = null ;
526+ absoluteStartMillis = DEFAULT_ABSOLUTE_START_MILLIS ;
501527 }
502528 }
503529
@@ -526,6 +552,9 @@ public void writeTo(StreamOutput out) throws IOException {
526552 }
527553 if (out .getVersion ().onOrAfter (Version .V_6_7_0 )) {
528554 out .writeOptionalString (localClusterAlias );
555+ if (localClusterAlias != null ) {
556+ out .writeVLong (absoluteStartMillis );
557+ }
529558 }
530559 }
531560
@@ -551,14 +580,15 @@ public boolean equals(Object o) {
551580 Objects .equals (preFilterShardSize , that .preFilterShardSize ) &&
552581 Objects .equals (indicesOptions , that .indicesOptions ) &&
553582 Objects .equals (allowPartialSearchResults , that .allowPartialSearchResults ) &&
554- Objects .equals (localClusterAlias , that .localClusterAlias );
583+ Objects .equals (localClusterAlias , that .localClusterAlias ) &&
584+ absoluteStartMillis == that .absoluteStartMillis ;
555585 }
556586
557587 @ Override
558588 public int hashCode () {
559589 return Objects .hash (searchType , Arrays .hashCode (indices ), routing , preference , source , requestCache ,
560590 scroll , Arrays .hashCode (types ), indicesOptions , batchedReduceSize , maxConcurrentShardRequests , preFilterShardSize ,
561- allowPartialSearchResults , localClusterAlias );
591+ allowPartialSearchResults , localClusterAlias , absoluteStartMillis );
562592 }
563593
564594 @ Override
@@ -577,6 +607,7 @@ public String toString() {
577607 ", preFilterShardSize=" + preFilterShardSize +
578608 ", allowPartialSearchResults=" + allowPartialSearchResults +
579609 ", localClusterAlias=" + localClusterAlias +
610+ ", getOrCreateAbsoluteStartMillis=" + absoluteStartMillis +
580611 ", source=" + source + '}' ;
581612 }
582613}
0 commit comments