8
8
9
9
import org .elasticsearch .cluster .AbstractDiffable ;
10
10
import org .elasticsearch .cluster .Diffable ;
11
+ import org .elasticsearch .common .Nullable ;
11
12
import org .elasticsearch .common .ParseField ;
12
13
import org .elasticsearch .common .io .stream .StreamInput ;
13
14
import org .elasticsearch .common .io .stream .StreamOutput ;
21
22
import java .time .Instant ;
22
23
import java .time .ZoneOffset ;
23
24
import java .time .ZonedDateTime ;
25
+ import java .util .HashMap ;
24
26
import java .util .Map ;
25
27
import java .util .Objects ;
28
+ import java .util .Optional ;
26
29
27
30
/**
28
31
* {@code SnapshotLifecyclePolicyMetadata} encapsulates a {@link SnapshotLifecyclePolicy} as well as
@@ -37,18 +40,34 @@ public class SnapshotLifecyclePolicyMetadata extends AbstractDiffable<SnapshotLi
37
40
static final ParseField VERSION = new ParseField ("version" );
38
41
static final ParseField MODIFIED_DATE = new ParseField ("modified_date" );
39
42
static final ParseField MODIFIED_DATE_STRING = new ParseField ("modified_date_string" );
43
+ static final ParseField LAST_SUCCESS = new ParseField ("last_success" );
44
+ static final ParseField LAST_FAILURE = new ParseField ("last_failure" );
40
45
41
46
private final SnapshotLifecyclePolicy policy ;
42
47
private final Map <String , String > headers ;
43
48
private final long version ;
44
49
private final long modifiedDate ;
50
+ @ Nullable
51
+ private final SnapshotInvocationRecord lastSuccess ;
52
+ @ Nullable
53
+ private final SnapshotInvocationRecord lastFailure ;
45
54
46
55
@ SuppressWarnings ("unchecked" )
47
56
public static final ConstructingObjectParser <SnapshotLifecyclePolicyMetadata , String > PARSER =
48
57
new ConstructingObjectParser <>("snapshot_policy_metadata" ,
49
58
a -> {
50
59
SnapshotLifecyclePolicy policy = (SnapshotLifecyclePolicy ) a [0 ];
51
- return new SnapshotLifecyclePolicyMetadata (policy , (Map <String , String >) a [1 ], (long ) a [2 ], (long ) a [3 ]);
60
+ SnapshotInvocationRecord lastSuccess = (SnapshotInvocationRecord ) a [5 ];
61
+ SnapshotInvocationRecord lastFailure = (SnapshotInvocationRecord ) a [6 ];
62
+
63
+ return builder ()
64
+ .setPolicy (policy )
65
+ .setHeaders ((Map <String , String >) a [1 ])
66
+ .setVersion ((long ) a [2 ])
67
+ .setModifiedDate ((long ) a [3 ])
68
+ .setLastSuccess (lastSuccess )
69
+ .setLastFailure (lastFailure )
70
+ .build ();
52
71
});
53
72
54
73
static {
@@ -57,17 +76,22 @@ public class SnapshotLifecyclePolicyMetadata extends AbstractDiffable<SnapshotLi
57
76
PARSER .declareLong (ConstructingObjectParser .constructorArg (), VERSION );
58
77
PARSER .declareLong (ConstructingObjectParser .constructorArg (), MODIFIED_DATE );
59
78
PARSER .declareString (ConstructingObjectParser .constructorArg (), MODIFIED_DATE_STRING );
79
+ PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), SnapshotInvocationRecord ::parse , LAST_SUCCESS );
80
+ PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), SnapshotInvocationRecord ::parse , LAST_FAILURE );
60
81
}
61
82
62
83
public static SnapshotLifecyclePolicyMetadata parse (XContentParser parser , String name ) {
63
84
return PARSER .apply (parser , name );
64
85
}
65
86
66
- public SnapshotLifecyclePolicyMetadata (SnapshotLifecyclePolicy policy , Map <String , String > headers , long version , long modifiedDate ) {
87
+ SnapshotLifecyclePolicyMetadata (SnapshotLifecyclePolicy policy , Map <String , String > headers , long version , long modifiedDate ,
88
+ SnapshotInvocationRecord lastSuccess , SnapshotInvocationRecord lastFailure ) {
67
89
this .policy = policy ;
68
90
this .headers = headers ;
69
91
this .version = version ;
70
92
this .modifiedDate = modifiedDate ;
93
+ this .lastSuccess = lastSuccess ;
94
+ this .lastFailure = lastFailure ;
71
95
}
72
96
73
97
@ SuppressWarnings ("unchecked" )
@@ -76,6 +100,8 @@ public SnapshotLifecyclePolicyMetadata(SnapshotLifecyclePolicy policy, Map<Strin
76
100
this .headers = (Map <String , String >) in .readGenericValue ();
77
101
this .version = in .readVLong ();
78
102
this .modifiedDate = in .readVLong ();
103
+ this .lastSuccess = in .readOptionalWriteable (SnapshotInvocationRecord ::new );
104
+ this .lastFailure = in .readOptionalWriteable (SnapshotInvocationRecord ::new );
79
105
}
80
106
81
107
@ Override
@@ -84,6 +110,25 @@ public void writeTo(StreamOutput out) throws IOException {
84
110
out .writeGenericValue (this .headers );
85
111
out .writeVLong (this .version );
86
112
out .writeVLong (this .modifiedDate );
113
+ out .writeOptionalWriteable (this .lastSuccess );
114
+ out .writeOptionalWriteable (this .lastFailure );
115
+ }
116
+
117
+ public static Builder builder () {
118
+ return new Builder ();
119
+ }
120
+
121
+ public static Builder builder (SnapshotLifecyclePolicyMetadata metadata ) {
122
+ if (metadata == null ) {
123
+ return builder ();
124
+ }
125
+ return new Builder ()
126
+ .setHeaders (metadata .getHeaders ())
127
+ .setPolicy (metadata .getPolicy ())
128
+ .setVersion (metadata .getVersion ())
129
+ .setModifiedDate (metadata .getModifiedDate ())
130
+ .setLastSuccess (metadata .getLastSuccess ())
131
+ .setLastFailure (metadata .getLastFailure ());
87
132
}
88
133
89
134
public Map <String , String > getHeaders () {
@@ -106,9 +151,24 @@ public long getModifiedDate() {
106
151
return modifiedDate ;
107
152
}
108
153
154
+ private String dateToDateString (Long date ) {
155
+ if (Objects .isNull (date )) {
156
+ return null ;
157
+ }
158
+ ZonedDateTime dateTime = ZonedDateTime .ofInstant (Instant .ofEpochMilli (date ), ZoneOffset .UTC );
159
+ return dateTime .toString ();
160
+ }
161
+
109
162
public String getModifiedDateString () {
110
- ZonedDateTime modifiedDateTime = ZonedDateTime .ofInstant (Instant .ofEpochMilli (modifiedDate ), ZoneOffset .UTC );
111
- return modifiedDateTime .toString ();
163
+ return dateToDateString (modifiedDate );
164
+ }
165
+
166
+ public SnapshotInvocationRecord getLastSuccess () {
167
+ return lastSuccess ;
168
+ }
169
+
170
+ public SnapshotInvocationRecord getLastFailure () {
171
+ return lastFailure ;
112
172
}
113
173
114
174
@ Override
@@ -119,13 +179,19 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
119
179
builder .field (VERSION .getPreferredName (), version );
120
180
builder .field (MODIFIED_DATE .getPreferredName (), modifiedDate );
121
181
builder .field (MODIFIED_DATE_STRING .getPreferredName (), getModifiedDateString ());
182
+ if (Objects .nonNull (lastSuccess )) {
183
+ builder .field (LAST_SUCCESS .getPreferredName (), lastSuccess );
184
+ }
185
+ if (Objects .nonNull (lastFailure )) {
186
+ builder .field (LAST_FAILURE .getPreferredName (), lastFailure );
187
+ }
122
188
builder .endObject ();
123
189
return builder ;
124
190
}
125
191
126
192
@ Override
127
193
public int hashCode () {
128
- return Objects .hash (policy , headers , version , modifiedDate );
194
+ return Objects .hash (policy , headers , version , modifiedDate , lastSuccess , lastFailure );
129
195
}
130
196
131
197
@ Override
@@ -140,7 +206,9 @@ public boolean equals(Object obj) {
140
206
return Objects .equals (policy , other .policy ) &&
141
207
Objects .equals (headers , other .headers ) &&
142
208
Objects .equals (version , other .version ) &&
143
- Objects .equals (modifiedDate , other .modifiedDate );
209
+ Objects .equals (modifiedDate , other .modifiedDate ) &&
210
+ Objects .equals (lastSuccess , other .lastSuccess ) &&
211
+ Objects .equals (lastFailure , other .lastFailure );
144
212
}
145
213
146
214
@ Override
@@ -150,4 +218,58 @@ public String toString() {
150
218
// should not emit them in case it accidentally gets logged.
151
219
return super .toString ();
152
220
}
221
+
222
+ public static class Builder {
223
+
224
+ private Builder () {
225
+ }
226
+
227
+ private SnapshotLifecyclePolicy policy ;
228
+ private Map <String , String > headers ;
229
+ private long version = 1L ;
230
+ private Long modifiedDate ;
231
+ private SnapshotInvocationRecord lastSuccessDate ;
232
+ private SnapshotInvocationRecord lastFailureDate ;
233
+
234
+ public Builder setPolicy (SnapshotLifecyclePolicy policy ) {
235
+ this .policy = policy ;
236
+ return this ;
237
+ }
238
+
239
+ public Builder setHeaders (Map <String , String > headers ) {
240
+ this .headers = headers ;
241
+ return this ;
242
+ }
243
+
244
+ public Builder setVersion (long version ) {
245
+ this .version = version ;
246
+ return this ;
247
+ }
248
+
249
+ public Builder setModifiedDate (long modifiedDate ) {
250
+ this .modifiedDate = modifiedDate ;
251
+ return this ;
252
+ }
253
+
254
+ public Builder setLastSuccess (SnapshotInvocationRecord lastSuccessDate ) {
255
+ this .lastSuccessDate = lastSuccessDate ;
256
+ return this ;
257
+ }
258
+
259
+ public Builder setLastFailure (SnapshotInvocationRecord lastFailureDate ) {
260
+ this .lastFailureDate = lastFailureDate ;
261
+ return this ;
262
+ }
263
+
264
+ public SnapshotLifecyclePolicyMetadata build () {
265
+ return new SnapshotLifecyclePolicyMetadata (
266
+ Objects .requireNonNull (policy ),
267
+ Optional .ofNullable (headers ).orElse (new HashMap <>()),
268
+ version ,
269
+ Objects .requireNonNull (modifiedDate , "modifiedDate must be set" ),
270
+ lastSuccessDate ,
271
+ lastFailureDate );
272
+ }
273
+ }
274
+
153
275
}
0 commit comments