Skip to content

Commit 36c29d3

Browse files
authored
[ML] checking if p-tasks metadata is null before updating state (#41091) (#41124)
* [ML] checking if p-tasks metadata is null before updating state * Adding test that validates fix * removing debug println
1 parent 77f1ac6 commit 36c29d3

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/MlConfigMigrator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,14 @@ public ClusterState execute(ClusterState currentState) {
246246
currentState.metaData().custom(PersistentTasksCustomMetaData.TYPE), currentState.nodes());
247247

248248
ClusterState.Builder newState = ClusterState.builder(currentState);
249-
newState.metaData(MetaData.builder(currentState.getMetaData())
250-
.putCustom(MlMetadata.TYPE, removed.mlMetadata)
251-
.putCustom(PersistentTasksCustomMetaData.TYPE, updatedTasks)
252-
.build());
249+
MetaData.Builder metaDataBuilder = MetaData.builder(currentState.getMetaData())
250+
.putCustom(MlMetadata.TYPE, removed.mlMetadata);
251+
252+
// If there are no tasks in the cluster state metadata to begin with, this could be null.
253+
if (updatedTasks != null) {
254+
metaDataBuilder = metaDataBuilder.putCustom(PersistentTasksCustomMetaData.TYPE, updatedTasks);
255+
}
256+
newState.metaData(metaDataBuilder.build());
253257
return newState.build();
254258
}
255259

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/integration/MlConfigMigratorIT.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.ml.integration;
77

8+
import com.carrotsearch.hppc.cursors.ObjectCursor;
89
import org.elasticsearch.Version;
910
import org.elasticsearch.action.DocWriteRequest;
1011
import org.elasticsearch.action.index.IndexRequest;
@@ -47,10 +48,12 @@
4748

4849
import java.io.IOException;
4950
import java.io.InputStream;
51+
import java.util.ArrayList;
5052
import java.util.Arrays;
5153
import java.util.Collections;
5254
import java.util.HashSet;
5355
import java.util.List;
56+
import java.util.Objects;
5457
import java.util.Set;
5558
import java.util.SortedMap;
5659
import java.util.TreeMap;
@@ -148,9 +151,13 @@ public void testMigrateConfigs() throws InterruptedException, IOException {
148151
.routingTable(routingTable.build())
149152
.build();
150153
when(clusterService.state()).thenReturn(clusterState);
151-
154+
List<MetaData.Custom> customs = new ArrayList<>();
152155
doAnswer(invocation -> {
153156
ClusterStateUpdateTask listener = (ClusterStateUpdateTask) invocation.getArguments()[1];
157+
ClusterState result = listener.execute(clusterState);
158+
for (ObjectCursor<MetaData.Custom> value : result.metaData().customs().values()){
159+
customs.add(value.value);
160+
}
154161
listener.clusterStateProcessed("source", mock(ClusterState.class), mock(ClusterState.class));
155162
return null;
156163
}).when(clusterService).submitStateUpdateTask(eq("remove-migrated-ml-configs"), any());
@@ -164,6 +171,9 @@ public void testMigrateConfigs() throws InterruptedException, IOException {
164171
blockingCall(actionListener -> mlConfigMigrator.migrateConfigs(clusterState, actionListener),
165172
responseHolder, exceptionHolder);
166173

174+
// Verify that we have custom values in the new cluster state and that none of them is null
175+
assertThat(customs.size(), greaterThan(0));
176+
assertThat(customs.stream().anyMatch(Objects::isNull), is(false));
167177
assertNull(exceptionHolder.get());
168178
assertTrue(responseHolder.get());
169179
assertSnapshot(mlMetadata.build());

0 commit comments

Comments
 (0)