6565 */
6666public class RevertModelSnapshotIT extends MlNativeAutodetectIntegTestCase {
6767
68+ private static final long DATA_START_TIME = 1761955200000L ;
69+
6870 @ After
6971 public void tearDownData () {
7072 cleanUp ();
@@ -75,7 +77,32 @@ public void testRevertModelSnapshot() throws Exception {
7577 }
7678
7779 public void testRevertModelSnapshot_DeleteInterveningResults () throws Exception {
80+ // Create and run a unrelated job to chech it is not affected by reverting a different job
81+ String jobId = "revert-snapshot-delete-intervening-unrelated-job" ;
82+
83+ TimeValue bucketSpan = TimeValue .timeValueHours (1 );
84+ long startTime = DATA_START_TIME - (bucketSpan .getMillis () * 2 );
85+ String data = String .join ("" , generateData (startTime , bucketSpan , 23 , List .of ("foo" ), (bucketIndex , series ) -> 10.0 ));
86+
87+ Job .Builder job = buildAndRegisterJob (jobId , bucketSpan );
88+ openJob (job .getId ());
89+ postData (job .getId (), data );
90+ flushJob (job .getId (), true );
91+ closeJob (job .getId ());
92+
93+ String snapShotId = getJob (jobId ).get (0 ).getModelSnapshotId ();
94+ assertThat (snapShotId , is (notNullValue ()));
95+ List <Bucket > buckets = getBuckets (jobId );
96+ assertThat (buckets .size (), greaterThan (0 ));
97+
98+ // Run another job and revert to an previous snapshot
7899 testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion ("revert-model-snapshot-it-job-delete-intervening-results" , true );
100+
101+ // Check snapshot Id and buckets have not changed
102+ assertThat (getJob (jobId ).getFirst ().getModelSnapshotId (), is (snapShotId ));
103+ List <Bucket > bucketsAfterRevert = getBuckets (jobId );
104+ assertThat (bucketsAfterRevert .size (), is (buckets .size ()));
105+ assertThat (bucketsAfterRevert , is (buckets ));
79106 }
80107
81108 public void testRevertToEmptySnapshot () throws Exception {
@@ -126,13 +153,13 @@ public void testRevertToEmptySnapshot() throws Exception {
126153
127154 private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion (String jobId , boolean deleteInterveningResults ) throws Exception {
128155 TimeValue bucketSpan = TimeValue .timeValueHours (1 );
129- long startTime = 1491004800000L ;
130156
131157 Job .Builder job = buildAndRegisterJob (jobId , bucketSpan );
132158 openJob (job .getId ());
133159 postData (
134160 job .getId (),
135- generateData (startTime , bucketSpan , 10 , Arrays .asList ("foo" ), (bucketIndex , series ) -> bucketIndex == 5 ? 100.0 : 10.0 ).stream ()
161+ generateData (DATA_START_TIME , bucketSpan , 10 , Arrays .asList ("foo" ), (bucketIndex , series ) -> bucketIndex == 5 ? 100.0 : 10.0 )
162+ .stream ()
136163 .collect (Collectors .joining ())
137164 );
138165 flushJob (job .getId (), true );
@@ -156,7 +183,7 @@ private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion(String jobI
156183 postData (
157184 job .getId (),
158185 generateData (
159- startTime + 10 * bucketSpan .getMillis (),
186+ DATA_START_TIME + 10 * bucketSpan .getMillis (),
160187 bucketSpan ,
161188 10 ,
162189 Arrays .asList ("foo" , "bar" ),
@@ -187,15 +214,15 @@ private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion(String jobI
187214 ModelSnapshot revertSnapshot = modelSnapshots .get (1 );
188215
189216 // Check there are 2 annotations (one per model snapshot)
190- assertThatNumberOfAnnotationsIsEqualTo (2 );
217+ assertThatNumberOfAnnotationsIsEqualTo (jobId , 2 );
191218
192219 // Add 3 new annotations...
193220 Instant lastResultTimestamp = revertSnapshot .getLatestResultTimeStamp ().toInstant ();
194221 client ().index (randomAnnotationIndexRequest (job .getId (), lastResultTimestamp .plusSeconds (10 ), Event .DELAYED_DATA )).actionGet ();
195222 client ().index (randomAnnotationIndexRequest (job .getId (), lastResultTimestamp .plusSeconds (20 ), Event .MODEL_CHANGE )).actionGet ();
196223 client ().index (randomAnnotationIndexRequest (job .getId (), lastResultTimestamp .minusSeconds (10 ), Event .MODEL_CHANGE )).actionGet ();
197224 // ... and check there are 5 annotations in total now
198- assertThatNumberOfAnnotationsIsEqualTo (5 );
225+ assertThatNumberOfAnnotationsIsEqualTo (jobId , 5 );
199226
200227 GetJobsStatsAction .Response .JobStats statsBeforeRevert = getJobStats (jobId ).get (0 );
201228 Instant timeBeforeRevert = Instant .now ();
@@ -219,7 +246,7 @@ private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion(String jobI
219246 assertThat (getQuantiles (job .getId ()).getTimestamp (), equalTo (revertSnapshot .getLatestResultTimeStamp ()));
220247
221248 // Check annotations with event type from {delayed_data, model_change} have been removed if deleteInterveningResults flag is set
222- assertThatNumberOfAnnotationsIsEqualTo (deleteInterveningResults ? 3 : 5 );
249+ assertThatNumberOfAnnotationsIsEqualTo (jobId , deleteInterveningResults ? 3 : 5 );
223250
224251 // Reverting should not have deleted any forecast docs
225252 assertThat (countForecastDocs (job .getId (), forecastId ), is (numForecastDocs ));
@@ -229,7 +256,7 @@ private void testRunJobInTwoPartsAndRevertSnapshotAndRunToCompletion(String jobI
229256 postData (
230257 job .getId (),
231258 generateData (
232- startTime + 10 * bucketSpan .getMillis (),
259+ DATA_START_TIME + 10 * bucketSpan .getMillis (),
233260 bucketSpan ,
234261 10 ,
235262 Arrays .asList ("foo" , "bar" ),
0 commit comments