31
31
*/
32
32
public class TransformCheckpointingInfo implements Writeable , ToXContentObject {
33
33
34
+ /**
35
+ * Builder for collecting checkpointing information for the purpose of _stats
36
+ */
37
+ public static class TransformCheckpointingInfoBuilder {
38
+ private TransformIndexerPosition nextCheckpointPosition ;
39
+ private TransformProgress nextCheckpointProgress ;
40
+ private TransformCheckpoint lastCheckpoint ;
41
+ private TransformCheckpoint nextCheckpoint ;
42
+ private TransformCheckpoint sourceCheckpoint ;
43
+ private Instant changesLastDetectedAt ;
44
+ private long operationsBehind ;
45
+
46
+ public TransformCheckpointingInfoBuilder () {}
47
+
48
+ public TransformCheckpointingInfo build () {
49
+ if (lastCheckpoint == null ) {
50
+ lastCheckpoint = TransformCheckpoint .EMPTY ;
51
+ }
52
+ if (nextCheckpoint == null ) {
53
+ nextCheckpoint = TransformCheckpoint .EMPTY ;
54
+ }
55
+ if (sourceCheckpoint == null ) {
56
+ sourceCheckpoint = TransformCheckpoint .EMPTY ;
57
+ }
58
+
59
+ // checkpointstats requires a non-negative checkpoint number
60
+ long lastCheckpointNumber = lastCheckpoint .getCheckpoint () > 0 ? lastCheckpoint .getCheckpoint () : 0 ;
61
+ long nextCheckpointNumber = nextCheckpoint .getCheckpoint () > 0 ? nextCheckpoint .getCheckpoint () : 0 ;
62
+
63
+ return new TransformCheckpointingInfo (
64
+ new TransformCheckpointStats (
65
+ lastCheckpointNumber ,
66
+ null ,
67
+ null ,
68
+ lastCheckpoint .getTimestamp (),
69
+ lastCheckpoint .getTimeUpperBound ()
70
+ ),
71
+ new TransformCheckpointStats (
72
+ nextCheckpointNumber ,
73
+ nextCheckpointPosition ,
74
+ nextCheckpointProgress ,
75
+ nextCheckpoint .getTimestamp (),
76
+ nextCheckpoint .getTimeUpperBound ()
77
+ ),
78
+ operationsBehind ,
79
+ changesLastDetectedAt
80
+ );
81
+ }
82
+
83
+ public TransformCheckpointingInfoBuilder setLastCheckpoint (TransformCheckpoint lastCheckpoint ) {
84
+ this .lastCheckpoint = lastCheckpoint ;
85
+ return this ;
86
+ }
87
+
88
+ public TransformCheckpoint getLastCheckpoint () {
89
+ return lastCheckpoint ;
90
+ }
91
+
92
+ public TransformCheckpointingInfoBuilder setNextCheckpoint (TransformCheckpoint nextCheckpoint ) {
93
+ this .nextCheckpoint = nextCheckpoint ;
94
+ return this ;
95
+ }
96
+
97
+ public TransformCheckpoint getNextCheckpoint () {
98
+ return nextCheckpoint ;
99
+ }
100
+
101
+ public TransformCheckpointingInfoBuilder setSourceCheckpoint (TransformCheckpoint sourceCheckpoint ) {
102
+ this .sourceCheckpoint = sourceCheckpoint ;
103
+ return this ;
104
+ }
105
+
106
+ public TransformCheckpoint getSourceCheckpoint () {
107
+ return sourceCheckpoint ;
108
+ }
109
+
110
+ public TransformCheckpointingInfoBuilder setNextCheckpointProgress (TransformProgress nextCheckpointProgress ) {
111
+ this .nextCheckpointProgress = nextCheckpointProgress ;
112
+ return this ;
113
+ }
114
+
115
+ public TransformCheckpointingInfoBuilder setNextCheckpointPosition (TransformIndexerPosition nextCheckpointPosition ) {
116
+ this .nextCheckpointPosition = nextCheckpointPosition ;
117
+ return this ;
118
+ }
119
+
120
+ public TransformCheckpointingInfoBuilder setChangesLastDetectedAt (Instant changesLastDetectedAt ) {
121
+ this .changesLastDetectedAt = changesLastDetectedAt ;
122
+ return this ;
123
+ }
124
+
125
+ public TransformCheckpointingInfoBuilder setOperationsBehind (long operationsBehind ) {
126
+ this .operationsBehind = operationsBehind ;
127
+ return this ;
128
+ }
129
+
130
+ }
131
+
34
132
public static final TransformCheckpointingInfo EMPTY = new TransformCheckpointingInfo (
35
133
TransformCheckpointStats .EMPTY ,
36
134
TransformCheckpointStats .EMPTY ,
37
135
0L ,
38
- null );
136
+ null
137
+ );
39
138
40
139
public static final ParseField LAST_CHECKPOINT = new ParseField ("last" );
41
140
public static final ParseField NEXT_CHECKPOINT = new ParseField ("next" );
@@ -44,32 +143,41 @@ public class TransformCheckpointingInfo implements Writeable, ToXContentObject {
44
143
private final TransformCheckpointStats last ;
45
144
private final TransformCheckpointStats next ;
46
145
private final long operationsBehind ;
47
- private Instant changesLastDetectedAt ;
48
-
49
- private static final ConstructingObjectParser <TransformCheckpointingInfo , Void > LENIENT_PARSER =
50
- new ConstructingObjectParser <>(
51
- "data_frame_transform_checkpointing_info" ,
52
- true ,
53
- a -> {
54
- long behind = a [2 ] == null ? 0L : (Long ) a [2 ];
55
- Instant changesLastDetectedAt = (Instant )a [3 ];
56
- return new TransformCheckpointingInfo (
57
- a [0 ] == null ? TransformCheckpointStats .EMPTY : (TransformCheckpointStats ) a [0 ],
58
- a [1 ] == null ? TransformCheckpointStats .EMPTY : (TransformCheckpointStats ) a [1 ],
59
- behind ,
60
- changesLastDetectedAt );
61
- });
146
+ private final Instant changesLastDetectedAt ;
147
+
148
+ private static final ConstructingObjectParser <TransformCheckpointingInfo , Void > LENIENT_PARSER = new ConstructingObjectParser <>(
149
+ "data_frame_transform_checkpointing_info" ,
150
+ true ,
151
+ a -> {
152
+ long behind = a [2 ] == null ? 0L : (Long ) a [2 ];
153
+ Instant changesLastDetectedAt = (Instant ) a [3 ];
154
+ return new TransformCheckpointingInfo (
155
+ a [0 ] == null ? TransformCheckpointStats .EMPTY : (TransformCheckpointStats ) a [0 ],
156
+ a [1 ] == null ? TransformCheckpointStats .EMPTY : (TransformCheckpointStats ) a [1 ],
157
+ behind ,
158
+ changesLastDetectedAt
159
+ );
160
+ }
161
+ );
62
162
63
163
static {
64
- LENIENT_PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (),
65
- TransformCheckpointStats .LENIENT_PARSER ::apply , LAST_CHECKPOINT );
66
- LENIENT_PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (),
67
- TransformCheckpointStats .LENIENT_PARSER ::apply , NEXT_CHECKPOINT );
164
+ LENIENT_PARSER .declareObject (
165
+ ConstructingObjectParser .optionalConstructorArg (),
166
+ TransformCheckpointStats .LENIENT_PARSER ::apply ,
167
+ LAST_CHECKPOINT
168
+ );
169
+ LENIENT_PARSER .declareObject (
170
+ ConstructingObjectParser .optionalConstructorArg (),
171
+ TransformCheckpointStats .LENIENT_PARSER ::apply ,
172
+ NEXT_CHECKPOINT
173
+ );
68
174
LENIENT_PARSER .declareLong (ConstructingObjectParser .optionalConstructorArg (), OPERATIONS_BEHIND );
69
- LENIENT_PARSER .declareField (ConstructingObjectParser .optionalConstructorArg (),
175
+ LENIENT_PARSER .declareField (
176
+ ConstructingObjectParser .optionalConstructorArg (),
70
177
p -> TimeUtils .parseTimeFieldToInstant (p , CHANGES_LAST_DETECTED_AT .getPreferredName ()),
71
178
CHANGES_LAST_DETECTED_AT ,
72
- ObjectParser .ValueType .VALUE );
179
+ ObjectParser .ValueType .VALUE
180
+ );
73
181
}
74
182
75
183
/**
@@ -81,28 +189,26 @@ public class TransformCheckpointingInfo implements Writeable, ToXContentObject {
81
189
* @param operationsBehind counter of operations the current checkpoint is behind source
82
190
* @param changesLastDetectedAt the last time the source indices were checked for changes
83
191
*/
84
- public TransformCheckpointingInfo (TransformCheckpointStats last ,
85
- TransformCheckpointStats next ,
86
- long operationsBehind ,
87
- Instant changesLastDetectedAt ) {
192
+ public TransformCheckpointingInfo (
193
+ TransformCheckpointStats last ,
194
+ TransformCheckpointStats next ,
195
+ long operationsBehind ,
196
+ Instant changesLastDetectedAt
197
+ ) {
88
198
this .last = Objects .requireNonNull (last );
89
199
this .next = Objects .requireNonNull (next );
90
200
this .operationsBehind = operationsBehind ;
91
201
this .changesLastDetectedAt = changesLastDetectedAt == null ? null : Instant .ofEpochMilli (changesLastDetectedAt .toEpochMilli ());
92
202
}
93
203
94
- public TransformCheckpointingInfo (TransformCheckpointStats last ,
95
- TransformCheckpointStats next ,
96
- long operationsBehind ) {
97
- this (last , next , operationsBehind , null );
98
- }
99
-
100
204
public TransformCheckpointingInfo (StreamInput in ) throws IOException {
101
205
last = new TransformCheckpointStats (in );
102
206
next = new TransformCheckpointStats (in );
103
207
operationsBehind = in .readLong ();
104
208
if (in .getVersion ().onOrAfter (Version .V_7_4_0 )) {
105
209
changesLastDetectedAt = in .readOptionalInstant ();
210
+ } else {
211
+ changesLastDetectedAt = null ;
106
212
}
107
213
}
108
214
@@ -122,23 +228,22 @@ public Instant getChangesLastDetectedAt() {
122
228
return changesLastDetectedAt ;
123
229
}
124
230
125
- public TransformCheckpointingInfo setChangesLastDetectedAt (Instant changesLastDetectedAt ) {
126
- this .changesLastDetectedAt = Instant .ofEpochMilli (Objects .requireNonNull (changesLastDetectedAt ).toEpochMilli ());
127
- return this ;
128
- }
129
-
130
231
@ Override
131
232
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
132
233
builder .startObject ();
133
234
builder .field (LAST_CHECKPOINT .getPreferredName (), last );
134
235
if (next .getCheckpoint () > 0 ) {
135
236
builder .field (NEXT_CHECKPOINT .getPreferredName (), next );
136
237
}
137
- builder .field (OPERATIONS_BEHIND .getPreferredName (), operationsBehind );
238
+ if (operationsBehind > 0 ) {
239
+ builder .field (OPERATIONS_BEHIND .getPreferredName (), operationsBehind );
240
+ }
138
241
if (changesLastDetectedAt != null ) {
139
- builder .timeField (CHANGES_LAST_DETECTED_AT .getPreferredName (),
242
+ builder .timeField (
243
+ CHANGES_LAST_DETECTED_AT .getPreferredName (),
140
244
CHANGES_LAST_DETECTED_AT .getPreferredName () + "_string" ,
141
- changesLastDetectedAt .toEpochMilli ());
245
+ changesLastDetectedAt .toEpochMilli ()
246
+ );
142
247
}
143
248
builder .endObject ();
144
249
return builder ;
@@ -175,10 +280,10 @@ public boolean equals(Object other) {
175
280
176
281
TransformCheckpointingInfo that = (TransformCheckpointingInfo ) other ;
177
282
178
- return Objects .equals (this .last , that .last ) &&
179
- Objects .equals (this .next , that .next ) &&
180
- this .operationsBehind == that .operationsBehind &&
181
- Objects .equals (this .changesLastDetectedAt , that .changesLastDetectedAt );
283
+ return Objects .equals (this .last , that .last )
284
+ && Objects .equals (this .next , that .next )
285
+ && this .operationsBehind == that .operationsBehind
286
+ && Objects .equals (this .changesLastDetectedAt , that .changesLastDetectedAt );
182
287
}
183
288
184
289
@ Override
0 commit comments