@@ -57,24 +57,28 @@ public class MlMetadata implements XPackPlugin.XPackMetaDataCustom {
5757 public static final String TYPE = "ml" ;
5858 private static final ParseField JOBS_FIELD = new ParseField ("jobs" );
5959 private static final ParseField DATAFEEDS_FIELD = new ParseField ("datafeeds" );
60+ private static final ParseField LAST_MEMORY_REFRESH_VERSION_FIELD = new ParseField ("last_memory_refresh_version" );
6061
61- public static final MlMetadata EMPTY_METADATA = new MlMetadata (Collections .emptySortedMap (), Collections .emptySortedMap ());
62+ public static final MlMetadata EMPTY_METADATA = new MlMetadata (Collections .emptySortedMap (), Collections .emptySortedMap (), null );
6263 // This parser follows the pattern that metadata is parsed leniently (to allow for enhancements)
6364 public static final ObjectParser <Builder , Void > LENIENT_PARSER = new ObjectParser <>("ml_metadata" , true , Builder ::new );
6465
6566 static {
6667 LENIENT_PARSER .declareObjectArray (Builder ::putJobs , (p , c ) -> Job .LENIENT_PARSER .apply (p , c ).build (), JOBS_FIELD );
6768 LENIENT_PARSER .declareObjectArray (Builder ::putDatafeeds ,
6869 (p , c ) -> DatafeedConfig .LENIENT_PARSER .apply (p , c ).build (), DATAFEEDS_FIELD );
70+ LENIENT_PARSER .declareLong (Builder ::setLastMemoryRefreshVersion , LAST_MEMORY_REFRESH_VERSION_FIELD );
6971 }
7072
7173 private final SortedMap <String , Job > jobs ;
7274 private final SortedMap <String , DatafeedConfig > datafeeds ;
75+ private final Long lastMemoryRefreshVersion ;
7376 private final GroupOrJobLookup groupOrJobLookup ;
7477
75- private MlMetadata (SortedMap <String , Job > jobs , SortedMap <String , DatafeedConfig > datafeeds ) {
78+ private MlMetadata (SortedMap <String , Job > jobs , SortedMap <String , DatafeedConfig > datafeeds , Long lastMemoryRefreshVersion ) {
7679 this .jobs = Collections .unmodifiableSortedMap (jobs );
7780 this .datafeeds = Collections .unmodifiableSortedMap (datafeeds );
81+ this .lastMemoryRefreshVersion = lastMemoryRefreshVersion ;
7882 this .groupOrJobLookup = new GroupOrJobLookup (jobs .values ());
7983 }
8084
@@ -112,6 +116,10 @@ public Set<String> expandDatafeedIds(String expression, boolean allowNoDatafeeds
112116 .expand (expression , allowNoDatafeeds );
113117 }
114118
119+ public Long getLastMemoryRefreshVersion () {
120+ return lastMemoryRefreshVersion ;
121+ }
122+
115123 @ Override
116124 public Version getMinimalSupportedVersion () {
117125 return Version .V_5_4_0 ;
@@ -145,14 +153,21 @@ public MlMetadata(StreamInput in) throws IOException {
145153 datafeeds .put (in .readString (), new DatafeedConfig (in ));
146154 }
147155 this .datafeeds = datafeeds ;
148-
156+ if (in .getVersion ().onOrAfter (Version .V_6_6_0 )) {
157+ lastMemoryRefreshVersion = in .readOptionalLong ();
158+ } else {
159+ lastMemoryRefreshVersion = null ;
160+ }
149161 this .groupOrJobLookup = new GroupOrJobLookup (jobs .values ());
150162 }
151163
152164 @ Override
153165 public void writeTo (StreamOutput out ) throws IOException {
154166 writeMap (jobs , out );
155167 writeMap (datafeeds , out );
168+ if (out .getVersion ().onOrAfter (Version .V_6_6_0 )) {
169+ out .writeOptionalLong (lastMemoryRefreshVersion );
170+ }
156171 }
157172
158173 private static <T extends Writeable > void writeMap (Map <String , T > map , StreamOutput out ) throws IOException {
@@ -169,6 +184,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
169184 new DelegatingMapParams (Collections .singletonMap (ToXContentParams .FOR_INTERNAL_STORAGE , "true" ), params );
170185 mapValuesToXContent (JOBS_FIELD , jobs , builder , extendedParams );
171186 mapValuesToXContent (DATAFEEDS_FIELD , datafeeds , builder , extendedParams );
187+ if (lastMemoryRefreshVersion != null ) {
188+ builder .field (LAST_MEMORY_REFRESH_VERSION_FIELD .getPreferredName (), lastMemoryRefreshVersion );
189+ }
172190 return builder ;
173191 }
174192
@@ -185,30 +203,46 @@ public static class MlMetadataDiff implements NamedDiff<MetaData.Custom> {
185203
186204 final Diff <Map <String , Job >> jobs ;
187205 final Diff <Map <String , DatafeedConfig >> datafeeds ;
206+ final Long lastMemoryRefreshVersion ;
188207
189208 MlMetadataDiff (MlMetadata before , MlMetadata after ) {
190209 this .jobs = DiffableUtils .diff (before .jobs , after .jobs , DiffableUtils .getStringKeySerializer ());
191210 this .datafeeds = DiffableUtils .diff (before .datafeeds , after .datafeeds , DiffableUtils .getStringKeySerializer ());
211+ this .lastMemoryRefreshVersion = after .lastMemoryRefreshVersion ;
192212 }
193213
194214 public MlMetadataDiff (StreamInput in ) throws IOException {
195215 this .jobs = DiffableUtils .readJdkMapDiff (in , DiffableUtils .getStringKeySerializer (), Job ::new ,
196216 MlMetadataDiff ::readJobDiffFrom );
197217 this .datafeeds = DiffableUtils .readJdkMapDiff (in , DiffableUtils .getStringKeySerializer (), DatafeedConfig ::new ,
198- MlMetadataDiff ::readSchedulerDiffFrom );
218+ MlMetadataDiff ::readDatafeedDiffFrom );
219+ if (in .getVersion ().onOrAfter (Version .V_6_6_0 )) {
220+ lastMemoryRefreshVersion = in .readOptionalLong ();
221+ } else {
222+ lastMemoryRefreshVersion = null ;
223+ }
199224 }
200225
226+ /**
227+ * Merge the diff with the ML metadata.
228+ * @param part The current ML metadata.
229+ * @return The new ML metadata.
230+ */
201231 @ Override
202232 public MetaData .Custom apply (MetaData .Custom part ) {
203233 TreeMap <String , Job > newJobs = new TreeMap <>(jobs .apply (((MlMetadata ) part ).jobs ));
204234 TreeMap <String , DatafeedConfig > newDatafeeds = new TreeMap <>(datafeeds .apply (((MlMetadata ) part ).datafeeds ));
205- return new MlMetadata (newJobs , newDatafeeds );
235+ // lastMemoryRefreshVersion always comes from the diff - no need to merge with the old value
236+ return new MlMetadata (newJobs , newDatafeeds , lastMemoryRefreshVersion );
206237 }
207238
208239 @ Override
209240 public void writeTo (StreamOutput out ) throws IOException {
210241 jobs .writeTo (out );
211242 datafeeds .writeTo (out );
243+ if (out .getVersion ().onOrAfter (Version .V_6_6_0 )) {
244+ out .writeOptionalLong (lastMemoryRefreshVersion );
245+ }
212246 }
213247
214248 @ Override
@@ -220,7 +254,7 @@ static Diff<Job> readJobDiffFrom(StreamInput in) throws IOException {
220254 return AbstractDiffable .readDiffFrom (Job ::new , in );
221255 }
222256
223- static Diff <DatafeedConfig > readSchedulerDiffFrom (StreamInput in ) throws IOException {
257+ static Diff <DatafeedConfig > readDatafeedDiffFrom (StreamInput in ) throws IOException {
224258 return AbstractDiffable .readDiffFrom (DatafeedConfig ::new , in );
225259 }
226260 }
@@ -233,7 +267,8 @@ public boolean equals(Object o) {
233267 return false ;
234268 MlMetadata that = (MlMetadata ) o ;
235269 return Objects .equals (jobs , that .jobs ) &&
236- Objects .equals (datafeeds , that .datafeeds );
270+ Objects .equals (datafeeds , that .datafeeds ) &&
271+ Objects .equals (lastMemoryRefreshVersion , that .lastMemoryRefreshVersion );
237272 }
238273
239274 @ Override
@@ -243,13 +278,14 @@ public final String toString() {
243278
244279 @ Override
245280 public int hashCode () {
246- return Objects .hash (jobs , datafeeds );
281+ return Objects .hash (jobs , datafeeds , lastMemoryRefreshVersion );
247282 }
248283
249284 public static class Builder {
250285
251286 private TreeMap <String , Job > jobs ;
252287 private TreeMap <String , DatafeedConfig > datafeeds ;
288+ private Long lastMemoryRefreshVersion ;
253289
254290 public Builder () {
255291 jobs = new TreeMap <>();
@@ -263,6 +299,7 @@ public Builder(@Nullable MlMetadata previous) {
263299 } else {
264300 jobs = new TreeMap <>(previous .jobs );
265301 datafeeds = new TreeMap <>(previous .datafeeds );
302+ lastMemoryRefreshVersion = previous .lastMemoryRefreshVersion ;
266303 }
267304 }
268305
@@ -382,8 +419,13 @@ private Builder putDatafeeds(Collection<DatafeedConfig> datafeeds) {
382419 return this ;
383420 }
384421
422+ public Builder setLastMemoryRefreshVersion (Long lastMemoryRefreshVersion ) {
423+ this .lastMemoryRefreshVersion = lastMemoryRefreshVersion ;
424+ return this ;
425+ }
426+
385427 public MlMetadata build () {
386- return new MlMetadata (jobs , datafeeds );
428+ return new MlMetadata (jobs , datafeeds , lastMemoryRefreshVersion );
387429 }
388430
389431 public void markJobAsDeleting (String jobId , PersistentTasksCustomMetaData tasks , boolean allowDeleteOpenJob ) {
@@ -420,8 +462,6 @@ void checkJobHasNoDatafeed(String jobId) {
420462 }
421463 }
422464
423-
424-
425465 public static MlMetadata getMlMetadata (ClusterState state ) {
426466 MlMetadata mlMetadata = (state == null ) ? null : state .getMetaData ().custom (TYPE );
427467 if (mlMetadata == null ) {
0 commit comments