|
5 | 5 | */ |
6 | 6 | package org.elasticsearch.xpack.ml.integration; |
7 | 7 |
|
8 | | -import org.elasticsearch.action.admin.cluster.node.tasks.list.ListTasksRequest; |
9 | 8 | import org.elasticsearch.action.search.SearchResponse; |
10 | 9 | import org.elasticsearch.action.support.master.AcknowledgedResponse; |
11 | | -import org.elasticsearch.client.Client; |
12 | | -import org.elasticsearch.cluster.ClusterModule; |
13 | | -import org.elasticsearch.cluster.ClusterState; |
14 | | -import org.elasticsearch.cluster.metadata.MetaData; |
15 | 10 | import org.elasticsearch.common.Strings; |
16 | 11 | import org.elasticsearch.common.bytes.BytesArray; |
17 | | -import org.elasticsearch.common.io.PathUtils; |
18 | | -import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
19 | | -import org.elasticsearch.common.network.NetworkModule; |
20 | | -import org.elasticsearch.common.settings.Settings; |
21 | 12 | import org.elasticsearch.common.unit.TimeValue; |
22 | 13 | import org.elasticsearch.common.xcontent.DeprecationHandler; |
23 | 14 | import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
|
26 | 17 | import org.elasticsearch.common.xcontent.XContentType; |
27 | 18 | import org.elasticsearch.common.xcontent.json.JsonXContent; |
28 | 19 | import org.elasticsearch.index.query.QueryBuilders; |
29 | | -import org.elasticsearch.index.reindex.ReindexPlugin; |
30 | | -import org.elasticsearch.persistent.PersistentTaskParams; |
31 | | -import org.elasticsearch.persistent.PersistentTaskState; |
32 | | -import org.elasticsearch.plugins.Plugin; |
33 | 20 | import org.elasticsearch.search.SearchHit; |
34 | 21 | import org.elasticsearch.search.SearchHits; |
35 | | -import org.elasticsearch.search.SearchModule; |
36 | 22 | import org.elasticsearch.search.sort.SortBuilders; |
37 | 23 | import org.elasticsearch.search.sort.SortOrder; |
38 | | -import org.elasticsearch.test.ESIntegTestCase; |
39 | | -import org.elasticsearch.test.SecuritySettingsSourceField; |
40 | | -import org.elasticsearch.transport.Netty4Plugin; |
41 | | -import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; |
42 | | -import org.elasticsearch.xpack.core.XPackClientPlugin; |
43 | | -import org.elasticsearch.xpack.core.XPackSettings; |
44 | | -import org.elasticsearch.xpack.core.ml.MlMetadata; |
45 | | -import org.elasticsearch.xpack.core.ml.MlTasks; |
46 | 24 | import org.elasticsearch.xpack.core.ml.action.CloseJobAction; |
47 | 25 | import org.elasticsearch.xpack.core.ml.action.DeleteDatafeedAction; |
48 | 26 | import org.elasticsearch.xpack.core.ml.action.DeleteJobAction; |
|
70 | 48 | import org.elasticsearch.xpack.core.ml.calendars.Calendar; |
71 | 49 | import org.elasticsearch.xpack.core.ml.calendars.ScheduledEvent; |
72 | 50 | import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig; |
73 | | -import org.elasticsearch.xpack.core.ml.datafeed.DatafeedState; |
74 | 51 | import org.elasticsearch.xpack.core.ml.job.config.Job; |
75 | 52 | import org.elasticsearch.xpack.core.ml.job.config.JobState; |
76 | | -import org.elasticsearch.xpack.core.ml.job.config.JobTaskState; |
77 | 53 | import org.elasticsearch.xpack.core.ml.job.config.JobUpdate; |
78 | 54 | import org.elasticsearch.xpack.core.ml.job.config.MlFilter; |
79 | 55 | import org.elasticsearch.xpack.core.ml.job.persistence.AnomalyDetectorsIndex; |
|
85 | 61 | import org.elasticsearch.xpack.core.ml.job.results.Forecast; |
86 | 62 | import org.elasticsearch.xpack.core.ml.job.results.ForecastRequestStats; |
87 | 63 | import org.elasticsearch.xpack.core.ml.job.results.Result; |
88 | | -import org.elasticsearch.xpack.core.security.SecurityField; |
89 | | -import org.elasticsearch.xpack.core.security.authc.TokenMetaData; |
90 | 64 |
|
91 | 65 | import java.io.IOException; |
92 | | -import java.net.URISyntaxException; |
93 | | -import java.nio.file.Path; |
94 | 66 | import java.util.ArrayList; |
95 | | -import java.util.Arrays; |
96 | | -import java.util.Collection; |
97 | | -import java.util.Collections; |
98 | 67 | import java.util.HashMap; |
99 | 68 | import java.util.List; |
100 | 69 | import java.util.Map; |
101 | 70 | import java.util.concurrent.TimeUnit; |
102 | 71 | import java.util.function.Function; |
103 | 72 |
|
104 | | -import static org.elasticsearch.test.XContentTestUtils.convertToMap; |
105 | | -import static org.elasticsearch.test.XContentTestUtils.differenceBetweenMapsIgnoringArrayOrder; |
106 | 73 | import static org.hamcrest.Matchers.equalTo; |
107 | 74 | import static org.hamcrest.Matchers.is; |
108 | 75 | import static org.hamcrest.Matchers.notNullValue; |
109 | 76 |
|
110 | 77 | /** |
111 | 78 | * Base class of ML integration tests that use a native autodetect process |
112 | 79 | */ |
113 | | -abstract class MlNativeAutodetectIntegTestCase extends ESIntegTestCase { |
| 80 | +abstract class MlNativeAutodetectIntegTestCase extends MlNativeIntegTestCase { |
114 | 81 |
|
115 | 82 | private List<Job.Builder> jobs = new ArrayList<>(); |
116 | 83 | private List<DatafeedConfig> datafeeds = new ArrayList<>(); |
117 | | - @Override |
118 | | - protected Collection<Class<? extends Plugin>> nodePlugins() { |
119 | | - return Arrays.asList(LocalStateCompositeXPackPlugin.class, Netty4Plugin.class); |
120 | | - } |
121 | 84 |
|
122 | 85 | @Override |
123 | | - protected Collection<Class<? extends Plugin>> transportClientPlugins() { |
124 | | - return Arrays.asList(XPackClientPlugin.class, Netty4Plugin.class, ReindexPlugin.class); |
125 | | - } |
126 | | - |
127 | | - @Override |
128 | | - protected Settings externalClusterClientSettings() { |
129 | | - Path key; |
130 | | - Path certificate; |
131 | | - try { |
132 | | - key = PathUtils.get(getClass().getResource("/testnode.pem").toURI()); |
133 | | - certificate = PathUtils.get(getClass().getResource("/testnode.crt").toURI()); |
134 | | - } catch (URISyntaxException e) { |
135 | | - throw new IllegalStateException("error trying to get keystore path", e); |
136 | | - } |
137 | | - Settings.Builder builder = Settings.builder(); |
138 | | - builder.put(NetworkModule.TRANSPORT_TYPE_KEY, SecurityField.NAME4); |
139 | | - builder.put(SecurityField.USER_SETTING.getKey(), "x_pack_rest_user:" + SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING); |
140 | | - builder.put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), true); |
141 | | - builder.put("xpack.security.transport.ssl.enabled", true); |
142 | | - builder.put("xpack.security.transport.ssl.key", key.toAbsolutePath().toString()); |
143 | | - builder.put("xpack.security.transport.ssl.certificate", certificate.toAbsolutePath().toString()); |
144 | | - builder.put("xpack.security.transport.ssl.key_passphrase", "testnode"); |
145 | | - builder.put("xpack.security.transport.ssl.verification_mode", "certificate"); |
146 | | - return builder.build(); |
147 | | - } |
148 | | - |
149 | | - protected void cleanUp() { |
| 86 | + protected void cleanUpResources() { |
150 | 87 | cleanUpDatafeeds(); |
151 | 88 | cleanUpJobs(); |
152 | | - waitForPendingTasks(); |
153 | 89 | } |
154 | 90 |
|
155 | 91 | private void cleanUpDatafeeds() { |
@@ -182,18 +118,6 @@ private void cleanUpJobs() { |
182 | 118 | } |
183 | 119 | } |
184 | 120 |
|
185 | | - private void waitForPendingTasks() { |
186 | | - ListTasksRequest listTasksRequest = new ListTasksRequest(); |
187 | | - listTasksRequest.setWaitForCompletion(true); |
188 | | - listTasksRequest.setDetailed(true); |
189 | | - listTasksRequest.setTimeout(TimeValue.timeValueSeconds(10)); |
190 | | - try { |
191 | | - admin().cluster().listTasks(listTasksRequest).get(); |
192 | | - } catch (Exception e) { |
193 | | - throw new AssertionError("Failed to wait for pending tasks to complete", e); |
194 | | - } |
195 | | - } |
196 | | - |
197 | 121 | protected void registerJob(Job.Builder job) { |
198 | 122 | if (jobs.add(job) == false) { |
199 | 123 | throw new IllegalArgumentException("job [" + job.getId() + "] is already registered"); |
@@ -441,56 +365,6 @@ protected PersistJobAction.Response persistJob(String jobId) { |
441 | 365 | return client().execute(PersistJobAction.INSTANCE, request).actionGet(); |
442 | 366 | } |
443 | 367 |
|
444 | | - @Override |
445 | | - protected void ensureClusterStateConsistency() throws IOException { |
446 | | - if (cluster() != null && cluster().size() > 0) { |
447 | | - List<NamedWriteableRegistry.Entry> entries = new ArrayList<>(ClusterModule.getNamedWriteables()); |
448 | | - entries.addAll(new SearchModule(Settings.EMPTY, true, Collections.emptyList()).getNamedWriteables()); |
449 | | - entries.add(new NamedWriteableRegistry.Entry(MetaData.Custom.class, "ml", MlMetadata::new)); |
450 | | - entries.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.DATAFEED_TASK_NAME, |
451 | | - StartDatafeedAction.DatafeedParams::new)); |
452 | | - entries.add(new NamedWriteableRegistry.Entry(PersistentTaskParams.class, MlTasks.JOB_TASK_NAME, |
453 | | - OpenJobAction.JobParams::new)); |
454 | | - entries.add(new NamedWriteableRegistry.Entry(PersistentTaskState.class, JobTaskState.NAME, JobTaskState::new)); |
455 | | - entries.add(new NamedWriteableRegistry.Entry(PersistentTaskState.class, DatafeedState.NAME, DatafeedState::fromStream)); |
456 | | - entries.add(new NamedWriteableRegistry.Entry(ClusterState.Custom.class, TokenMetaData.TYPE, TokenMetaData::new)); |
457 | | - final NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(entries); |
458 | | - ClusterState masterClusterState = client().admin().cluster().prepareState().all().get().getState(); |
459 | | - byte[] masterClusterStateBytes = ClusterState.Builder.toBytes(masterClusterState); |
460 | | - // remove local node reference |
461 | | - masterClusterState = ClusterState.Builder.fromBytes(masterClusterStateBytes, null, namedWriteableRegistry); |
462 | | - Map<String, Object> masterStateMap = convertToMap(masterClusterState); |
463 | | - int masterClusterStateSize = ClusterState.Builder.toBytes(masterClusterState).length; |
464 | | - String masterId = masterClusterState.nodes().getMasterNodeId(); |
465 | | - for (Client client : cluster().getClients()) { |
466 | | - ClusterState localClusterState = client.admin().cluster().prepareState().all().setLocal(true).get().getState(); |
467 | | - byte[] localClusterStateBytes = ClusterState.Builder.toBytes(localClusterState); |
468 | | - // remove local node reference |
469 | | - localClusterState = ClusterState.Builder.fromBytes(localClusterStateBytes, null, namedWriteableRegistry); |
470 | | - final Map<String, Object> localStateMap = convertToMap(localClusterState); |
471 | | - final int localClusterStateSize = ClusterState.Builder.toBytes(localClusterState).length; |
472 | | - // Check that the non-master node has the same version of the cluster state as the master and |
473 | | - // that the master node matches the master (otherwise there is no requirement for the cluster state to match) |
474 | | - if (masterClusterState.version() == localClusterState.version() && |
475 | | - masterId.equals(localClusterState.nodes().getMasterNodeId())) { |
476 | | - try { |
477 | | - assertEquals("clusterstate UUID does not match", masterClusterState.stateUUID(), localClusterState.stateUUID()); |
478 | | - // We cannot compare serialization bytes since serialization order of maps is not guaranteed |
479 | | - // but we can compare serialization sizes - they should be the same |
480 | | - assertEquals("clusterstate size does not match", masterClusterStateSize, localClusterStateSize); |
481 | | - // Compare JSON serialization |
482 | | - assertNull("clusterstate JSON serialization does not match", |
483 | | - differenceBetweenMapsIgnoringArrayOrder(masterStateMap, localStateMap)); |
484 | | - } catch (AssertionError error) { |
485 | | - logger.error("Cluster state from master:\n{}\nLocal cluster state:\n{}", |
486 | | - masterClusterState.toString(), localClusterState.toString()); |
487 | | - throw error; |
488 | | - } |
489 | | - } |
490 | | - } |
491 | | - } |
492 | | - } |
493 | | - |
494 | 368 | protected List<String> generateData(long timestamp, TimeValue bucketSpan, int bucketCount, |
495 | 369 | Function<Integer, Integer> timeToCountFunction) throws IOException { |
496 | 370 | List<String> data = new ArrayList<>(); |
|
0 commit comments