27
27
import org .elasticsearch .common .xcontent .XContentBuilder ;
28
28
import org .elasticsearch .common .xcontent .XContentParser ;
29
29
import org .elasticsearch .rest .RestStatus ;
30
+ import org .elasticsearch .tasks .TaskId ;
30
31
31
32
import java .io .IOException ;
32
33
33
34
public class ReindexTaskStateDoc implements ToXContentObject {
34
35
35
36
public static final ConstructingObjectParser <ReindexTaskStateDoc , Void > PARSER =
36
37
new ConstructingObjectParser <>("reindex/index_state" , a -> new ReindexTaskStateDoc ((ReindexRequest ) a [0 ], (Long ) a [1 ],
37
- (BulkByScrollResponse ) a [2 ], (ElasticsearchException ) a [3 ], (Integer ) a [4 ], (ScrollableHitSource .Checkpoint ) a [5 ]));
38
+ toTaskId ((String ) a [2 ]), (BulkByScrollResponse ) a [3 ], (ElasticsearchException ) a [4 ], (Integer ) a [5 ],
39
+ (ScrollableHitSource .Checkpoint ) a [6 ],
40
+ (float ) a [7 ]));
38
41
39
42
private static final String REINDEX_REQUEST = "request" ;
40
43
private static final String ALLOCATION = "allocation" ;
44
+ private static final String EPHEMERAL_TASK_ID = "ephemeral_task_id" ;
41
45
private static final String REINDEX_RESPONSE = "response" ;
42
46
private static final String REINDEX_EXCEPTION = "exception" ;
43
47
private static final String FAILURE_REST_STATUS = "failure_rest_status" ;
44
48
private static final String REINDEX_CHECKPOINT = "checkpoint" ;
49
+ private static final String REQUESTS_PER_SECOND = "requests_per_second" ;
45
50
46
51
static {
47
52
PARSER .declareObject (ConstructingObjectParser .constructorArg (), (p , c ) -> ReindexRequest .fromXContentWithParams (p ),
48
53
new ParseField (REINDEX_REQUEST ));
49
54
PARSER .declareLong (ConstructingObjectParser .optionalConstructorArg (), new ParseField (ALLOCATION ));
55
+ PARSER .declareString (ConstructingObjectParser .optionalConstructorArg (), new ParseField (EPHEMERAL_TASK_ID ));
50
56
PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> BulkByScrollResponse .fromXContent (p ),
51
57
new ParseField (REINDEX_RESPONSE ));
52
58
PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> ElasticsearchException .fromXContent (p ),
53
59
new ParseField (REINDEX_EXCEPTION ));
54
60
PARSER .declareInt (ConstructingObjectParser .optionalConstructorArg (), new ParseField (FAILURE_REST_STATUS ));
55
61
PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> ScrollableHitSource .Checkpoint .fromXContent (p ),
56
62
new ParseField (REINDEX_CHECKPOINT ));
63
+ PARSER .declareFloat (ConstructingObjectParser .constructorArg (), new ParseField (REQUESTS_PER_SECOND ));
57
64
}
58
65
59
66
private final ReindexRequest reindexRequest ;
60
67
private final Long allocationId ;
68
+ private final TaskId ephemeralTaskId ;
61
69
private final BulkByScrollResponse reindexResponse ;
62
70
private final ElasticsearchException exception ;
63
71
private final RestStatus failureStatusCode ;
64
72
private final ScrollableHitSource .Checkpoint checkpoint ;
73
+ private final float requestsPerSecond ;
65
74
66
75
public ReindexTaskStateDoc (ReindexRequest reindexRequest ) {
67
- this (reindexRequest , null , null , null , (RestStatus ) null , null );
76
+ this (reindexRequest , null , null , null , null , (RestStatus ) null , null , reindexRequest . getRequestsPerSecond () );
68
77
}
69
78
70
79
public ReindexTaskStateDoc (ReindexRequest reindexRequest , @ Nullable Long allocationId ,
71
- @ Nullable BulkByScrollResponse reindexResponse , @ Nullable ElasticsearchException exception ,
72
- @ Nullable Integer failureStatusCode , ScrollableHitSource .Checkpoint checkpoint ) {
73
- this (reindexRequest , allocationId , reindexResponse , exception ,
74
- failureStatusCode == null ? null : RestStatus .fromCode (failureStatusCode ), checkpoint );
80
+ @ Nullable TaskId ephemeralTaskId , @ Nullable BulkByScrollResponse reindexResponse ,
81
+ @ Nullable ElasticsearchException exception ,
82
+ @ Nullable Integer failureStatusCode , ScrollableHitSource .Checkpoint checkpoint , float requestsPerSecond ) {
83
+ this (reindexRequest , allocationId , ephemeralTaskId , reindexResponse , exception ,
84
+ failureStatusCode == null ? null : RestStatus .fromCode (failureStatusCode ), checkpoint , requestsPerSecond );
75
85
}
76
86
77
87
public ReindexTaskStateDoc (ReindexRequest reindexRequest , @ Nullable Long allocationId ,
78
- @ Nullable BulkByScrollResponse reindexResponse , @ Nullable ElasticsearchException exception ,
79
- @ Nullable ScrollableHitSource .Checkpoint checkpoint ) {
80
- this (reindexRequest , allocationId , reindexResponse , exception , exception != null ? exception .status () : null , checkpoint );
88
+ @ Nullable TaskId ephemeralTaskId , @ Nullable BulkByScrollResponse reindexResponse ,
89
+ @ Nullable ElasticsearchException exception ,
90
+ @ Nullable ScrollableHitSource .Checkpoint checkpoint , float requestsPerSecond ) {
91
+ this (reindexRequest , allocationId , ephemeralTaskId , reindexResponse , exception , exception != null ? exception .status () : null ,
92
+ checkpoint , requestsPerSecond );
81
93
}
82
94
83
95
private ReindexTaskStateDoc (ReindexRequest reindexRequest , @ Nullable Long allocationId ,
84
- @ Nullable BulkByScrollResponse reindexResponse , @ Nullable ElasticsearchException exception ,
85
- @ Nullable RestStatus failureStatusCode , @ Nullable ScrollableHitSource .Checkpoint checkpoint ) {
96
+ @ Nullable TaskId ephemeralTaskId , @ Nullable BulkByScrollResponse reindexResponse ,
97
+ @ Nullable ElasticsearchException exception ,
98
+ @ Nullable RestStatus failureStatusCode , @ Nullable ScrollableHitSource .Checkpoint checkpoint ,
99
+ float requestsPerSecond ) {
100
+ assert (allocationId == null ) == (ephemeralTaskId == null );
86
101
this .allocationId = allocationId ;
102
+ this .ephemeralTaskId = ephemeralTaskId ;
103
+ assert Float .isNaN (requestsPerSecond ) == false && requestsPerSecond >= 0 ;
104
+ this .requestsPerSecond = requestsPerSecond ;
87
105
assert (reindexResponse == null ) || (exception == null ) : "Either response or exception must be null" ;
88
106
this .reindexRequest = reindexRequest ;
89
107
this .reindexResponse = reindexResponse ;
@@ -100,6 +118,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
100
118
if (allocationId != null ) {
101
119
builder .field (ALLOCATION , allocationId );
102
120
}
121
+ if (ephemeralTaskId != null ) {
122
+ builder .field (EPHEMERAL_TASK_ID , ephemeralTaskId .toString ());
123
+ }
103
124
if (reindexResponse != null ) {
104
125
builder .field (REINDEX_RESPONSE );
105
126
builder .startObject ();
@@ -117,13 +138,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
117
138
builder .field (REINDEX_CHECKPOINT );
118
139
checkpoint .toXContent (builder , params );
119
140
}
141
+ builder .field (REQUESTS_PER_SECOND , requestsPerSecond );
120
142
return builder .endObject ();
121
143
}
122
144
123
145
public static ReindexTaskStateDoc fromXContent (XContentParser parser ) {
124
146
return PARSER .apply (parser , null );
125
147
}
126
148
149
+ private static TaskId toTaskId (String s ) {
150
+ return s != null ? new TaskId (s ) : null ;
151
+ }
152
+
127
153
public ReindexRequest getReindexRequest () {
128
154
return reindexRequest ;
129
155
}
@@ -148,17 +174,33 @@ public Long getAllocationId() {
148
174
return allocationId ;
149
175
}
150
176
177
+ public TaskId getEphemeralTaskId () {
178
+ return ephemeralTaskId ;
179
+ }
180
+
181
+ public float getRequestsPerSecond () {
182
+ return requestsPerSecond ;
183
+ }
184
+
151
185
public ReindexTaskStateDoc withCheckpoint (ScrollableHitSource .Checkpoint checkpoint , BulkByScrollTask .Status status ) {
152
186
// todo: also store and resume from status.
153
- return new ReindexTaskStateDoc (reindexRequest , allocationId , reindexResponse , exception , failureStatusCode , checkpoint );
187
+ return new ReindexTaskStateDoc (reindexRequest , allocationId , ephemeralTaskId , reindexResponse , exception , failureStatusCode ,
188
+ checkpoint , requestsPerSecond );
154
189
}
155
190
156
- public ReindexTaskStateDoc withNewAllocation (long newAllocationId ) {
157
- return new ReindexTaskStateDoc (reindexRequest , newAllocationId , reindexResponse , exception , failureStatusCode , checkpoint );
191
+ public ReindexTaskStateDoc withNewAllocation (long newAllocationId , TaskId ephemeralTaskId ) {
192
+ return new ReindexTaskStateDoc (reindexRequest , newAllocationId , ephemeralTaskId , reindexResponse , exception , failureStatusCode ,
193
+ checkpoint , requestsPerSecond );
158
194
}
159
195
160
196
public ReindexTaskStateDoc withFinishedState (@ Nullable BulkByScrollResponse reindexResponse ,
161
197
@ Nullable ElasticsearchException exception ) {
162
- return new ReindexTaskStateDoc (reindexRequest , allocationId , reindexResponse , exception , checkpoint );
198
+ return new ReindexTaskStateDoc (reindexRequest , allocationId , ephemeralTaskId , reindexResponse , exception , checkpoint ,
199
+ requestsPerSecond );
200
+ }
201
+
202
+ public ReindexTaskStateDoc withRequestsPerSecond (float requestsPerSecond ) {
203
+ return new ReindexTaskStateDoc (reindexRequest , allocationId , ephemeralTaskId , reindexResponse , exception , checkpoint ,
204
+ requestsPerSecond );
163
205
}
164
206
}
0 commit comments