7
7
8
8
import org .apache .logging .log4j .LogManager ;
9
9
import org .apache .logging .log4j .Logger ;
10
+ import org .elasticsearch .Version ;
11
+ import org .elasticsearch .action .ActionListener ;
12
+ import org .elasticsearch .client .Client ;
10
13
import org .elasticsearch .cluster .ClusterChangedEvent ;
14
+ import org .elasticsearch .cluster .ClusterState ;
11
15
import org .elasticsearch .cluster .ClusterStateListener ;
12
16
import org .elasticsearch .cluster .LocalNodeMasterListener ;
13
17
import org .elasticsearch .cluster .node .DiscoveryNode ;
@@ -30,12 +34,23 @@ public class MlAssignmentNotifier implements ClusterStateListener, LocalNodeMast
30
34
31
35
private final Auditor auditor ;
32
36
private final ClusterService clusterService ;
33
-
37
+ private final MlConfigMigrator mlConfigMigrator ;
38
+ private final ThreadPool threadPool ;
34
39
private final AtomicBoolean enabled = new AtomicBoolean (false );
35
40
36
- MlAssignmentNotifier (Auditor auditor , ClusterService clusterService ) {
41
+ MlAssignmentNotifier (Auditor auditor , ThreadPool threadPool , Client client , ClusterService clusterService ) {
42
+ this .auditor = auditor ;
43
+ this .clusterService = clusterService ;
44
+ this .mlConfigMigrator = new MlConfigMigrator (client , clusterService );
45
+ this .threadPool = threadPool ;
46
+ clusterService .addLocalNodeMasterListener (this );
47
+ }
48
+
49
+ MlAssignmentNotifier (Auditor auditor , ThreadPool threadPool , MlConfigMigrator mlConfigMigrator , ClusterService clusterService ) {
37
50
this .auditor = auditor ;
38
51
this .clusterService = clusterService ;
52
+ this .mlConfigMigrator = mlConfigMigrator ;
53
+ this .threadPool = threadPool ;
39
54
clusterService .addLocalNodeMasterListener (this );
40
55
}
41
56
@@ -72,6 +87,25 @@ public void clusterChanged(ClusterChangedEvent event) {
72
87
return ;
73
88
}
74
89
90
+ Version minNodeVersion = event .state ().nodes ().getMinNodeVersion ();
91
+ if (minNodeVersion .onOrAfter (Version .V_6_6_0 )) {
92
+ // ok to migrate
93
+ mlConfigMigrator .migrateConfigsWithoutTasks (event .state (), ActionListener .wrap (
94
+ response -> threadPool .executor (executorName ()).execute (() -> auditChangesToMlTasks (current , previous , event .state ())),
95
+ e -> {
96
+ logger .error ("error migrating ml configurations" , e );
97
+ threadPool .executor (executorName ()).execute (() -> auditChangesToMlTasks (current , previous , event .state ()));
98
+ }
99
+ ));
100
+ } else {
101
+ threadPool .executor (executorName ()).execute (() -> auditChangesToMlTasks (current , previous , event .state ()));
102
+ }
103
+
104
+ }
105
+
106
+ private void auditChangesToMlTasks (PersistentTasksCustomMetaData current , PersistentTasksCustomMetaData previous ,
107
+ ClusterState state ) {
108
+
75
109
for (PersistentTask <?> currentTask : current .tasks ()) {
76
110
Assignment currentAssignment = currentTask .getAssignment ();
77
111
PersistentTask <?> previousTask = previous != null ? previous .getTask (currentTask .getId ()) : null ;
@@ -84,7 +118,7 @@ public void clusterChanged(ClusterChangedEvent event) {
84
118
if (currentAssignment .getExecutorNode () == null ) {
85
119
auditor .warning (jobId , "No node found to open job. Reasons [" + currentAssignment .getExplanation () + "]" );
86
120
} else {
87
- DiscoveryNode node = event . state () .nodes ().get (currentAssignment .getExecutorNode ());
121
+ DiscoveryNode node = state .nodes ().get (currentAssignment .getExecutorNode ());
88
122
auditor .info (jobId , "Opening job on node [" + node .toString () + "]" );
89
123
}
90
124
} else if (MlTasks .DATAFEED_TASK_NAME .equals (currentTask .getTaskName ())) {
@@ -98,7 +132,7 @@ public void clusterChanged(ClusterChangedEvent event) {
98
132
auditor .warning (jobId , msg );
99
133
}
100
134
} else {
101
- DiscoveryNode node = event . state () .nodes ().get (currentAssignment .getExecutorNode ());
135
+ DiscoveryNode node = state .nodes ().get (currentAssignment .getExecutorNode ());
102
136
if (jobId != null ) {
103
137
auditor .info (jobId , "Starting datafeed [" + datafeedParams .getDatafeedId () + "] on node [" + node + "]" );
104
138
}
0 commit comments