Skip to content

Commit 79f209a

Browse files
committed
make DateMathIndexExpressionsIntegrationIT more resilient
the internal cluster calls System.nanoTime() and System.currentTimeMillis() during evaluations of requests that need date-math index resolution. These are not mockable in these tests. As is, executing requests as-is in these test cases can potentially result in invalid responses when day-boundaries are hit mid test run. This change makes the test framework ignore failures due to day changes. Closes #31067.
1 parent aef5775 commit 79f209a

File tree

1 file changed

+65
-32
lines changed

1 file changed

+65
-32
lines changed

server/src/test/java/org/elasticsearch/indices/DateMathIndexExpressionsIntegrationIT.java

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
package org.elasticsearch.indices;
2121

22+
import org.elasticsearch.action.ActionRequest;
23+
import org.elasticsearch.action.ActionRequestBuilder;
24+
import org.elasticsearch.action.ActionResponse;
2225
import org.elasticsearch.action.DocWriteResponse;
2326
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
2427
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
@@ -29,10 +32,14 @@
2932
import org.elasticsearch.cluster.ClusterState;
3033
import org.elasticsearch.cluster.metadata.IndexMetaData;
3134
import org.elasticsearch.common.xcontent.XContentType;
35+
import org.elasticsearch.index.IndexNotFoundException;
3236
import org.elasticsearch.test.ESIntegTestCase;
33-
import org.joda.time.DateTime;
34-
import org.joda.time.DateTimeZone;
35-
import org.joda.time.format.DateTimeFormat;
37+
import org.junit.Before;
38+
39+
import java.time.ZoneOffset;
40+
import java.time.ZonedDateTime;
41+
import java.time.format.DateTimeFormatter;
42+
import java.util.Locale;
3643

3744
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
3845
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
@@ -41,15 +48,42 @@
4148
import static org.hamcrest.Matchers.notNullValue;
4249

4350
public class DateMathIndexExpressionsIntegrationIT extends ESIntegTestCase {
51+
private ZonedDateTime now;
52+
53+
@Before
54+
public void setNow() {
55+
now = ZonedDateTime.now(ZoneOffset.UTC);
56+
}
57+
58+
/**
59+
* the internal cluster calls System.nanoTime() and System.currentTimeMillis() during evaluations of requests
60+
* that need date-math index resolution. These are not mockable in these tests. As is, executing requests as-is
61+
* in these test cases can potentially result in invalid responses when day-boundaries are hit mid test run. Instead
62+
* of failing when index resolution with `now` is one day off, this method wraps calls with the assumption that
63+
* the day did not change during the test run.
64+
*/
65+
public <Q extends ActionRequest, R extends ActionResponse> R dateSensitiveGet(ActionRequestBuilder<Q, R> builder) {
66+
Runnable dayChangeAssumption = () -> assumeTrue("day changed between requests",
67+
ZonedDateTime.now(ZoneOffset.UTC).getDayOfYear() == now.getDayOfYear());
68+
R response;
69+
try {
70+
response = builder.get();
71+
} catch (IndexNotFoundException e) {
72+
// index resolver throws this if it does not find the exact index due to day changes
73+
dayChangeAssumption.run();
74+
throw e;
75+
}
76+
dayChangeAssumption.run();
77+
return response;
78+
}
4479

4580
public void testIndexNameDateMathExpressions() {
46-
DateTime now = new DateTime(DateTimeZone.UTC);
47-
String index1 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now);
48-
String index2 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(1));
49-
String index3 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(2));
81+
String index1 = ".marvel-" + DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT).format(now);
82+
String index2 = ".marvel-" + DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT).format(now.minusDays(1));
83+
String index3 = ".marvel-" + DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT).format(now.minusDays(2));
5084
createIndex(index1, index2, index3);
5185

52-
GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings(index1, index2, index3).get();
86+
GetSettingsResponse getSettingsResponse = dateSensitiveGet(client().admin().indices().prepareGetSettings(index1, index2, index3));
5387
assertEquals(index1, getSettingsResponse.getSetting(index1, IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
5488
assertEquals(index2, getSettingsResponse.getSetting(index2, IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
5589
assertEquals(index3, getSettingsResponse.getSetting(index3, IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
@@ -63,56 +97,56 @@ public void testIndexNameDateMathExpressions() {
6397
client().prepareIndex(dateMathExp3, "type", "3").setSource("{}", XContentType.JSON).get();
6498
refresh();
6599

66-
SearchResponse searchResponse = client().prepareSearch(dateMathExp1, dateMathExp2, dateMathExp3).get();
100+
SearchResponse searchResponse = dateSensitiveGet(client().prepareSearch(dateMathExp1, dateMathExp2, dateMathExp3));
67101
assertHitCount(searchResponse, 3);
68102
assertSearchHits(searchResponse, "1", "2", "3");
69103

70-
GetResponse getResponse = client().prepareGet(dateMathExp1, "type", "1").get();
104+
GetResponse getResponse = dateSensitiveGet(client().prepareGet(dateMathExp1, "type", "1"));
71105
assertThat(getResponse.isExists(), is(true));
72106
assertThat(getResponse.getId(), equalTo("1"));
73107

74-
getResponse = client().prepareGet(dateMathExp2, "type", "2").get();
108+
getResponse = dateSensitiveGet(client().prepareGet(dateMathExp2, "type", "2"));
75109
assertThat(getResponse.isExists(), is(true));
76110
assertThat(getResponse.getId(), equalTo("2"));
77111

78-
getResponse = client().prepareGet(dateMathExp3, "type", "3").get();
112+
getResponse = dateSensitiveGet(client().prepareGet(dateMathExp3, "type", "3"));
79113
assertThat(getResponse.isExists(), is(true));
80114
assertThat(getResponse.getId(), equalTo("3"));
81115

82-
MultiGetResponse mgetResponse = client().prepareMultiGet()
116+
MultiGetResponse mgetResponse = dateSensitiveGet(client().prepareMultiGet()
83117
.add(dateMathExp1, "type", "1")
84118
.add(dateMathExp2, "type", "2")
85-
.add(dateMathExp3, "type", "3").get();
119+
.add(dateMathExp3, "type", "3"));
86120
assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true));
87121
assertThat(mgetResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
88122
assertThat(mgetResponse.getResponses()[1].getResponse().isExists(), is(true));
89123
assertThat(mgetResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
90124
assertThat(mgetResponse.getResponses()[2].getResponse().isExists(), is(true));
91125
assertThat(mgetResponse.getResponses()[2].getResponse().getId(), equalTo("3"));
92126

93-
IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(dateMathExp1, dateMathExp2, dateMathExp3).get();
127+
IndicesStatsResponse indicesStatsResponse =
128+
dateSensitiveGet(client().admin().indices().prepareStats(dateMathExp1, dateMathExp2, dateMathExp3));
94129
assertThat(indicesStatsResponse.getIndex(index1), notNullValue());
95130
assertThat(indicesStatsResponse.getIndex(index2), notNullValue());
96131
assertThat(indicesStatsResponse.getIndex(index3), notNullValue());
97132

98-
DeleteResponse deleteResponse = client().prepareDelete(dateMathExp1, "type", "1").get();
133+
DeleteResponse deleteResponse = dateSensitiveGet(client().prepareDelete(dateMathExp1, "type", "1"));
99134
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
100135
assertThat(deleteResponse.getId(), equalTo("1"));
101136

102-
deleteResponse = client().prepareDelete(dateMathExp2, "type", "2").get();
137+
deleteResponse = dateSensitiveGet(client().prepareDelete(dateMathExp2, "type", "2"));
103138
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
104139
assertThat(deleteResponse.getId(), equalTo("2"));
105140

106-
deleteResponse = client().prepareDelete(dateMathExp3, "type", "3").get();
141+
deleteResponse = dateSensitiveGet(client().prepareDelete(dateMathExp3, "type", "3"));
107142
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
108143
assertThat(deleteResponse.getId(), equalTo("3"));
109144
}
110145

111-
public void testAutoCreateIndexWithDateMathExpression() throws Exception {
112-
DateTime now = new DateTime(DateTimeZone.UTC);
113-
String index1 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now);
114-
String index2 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(1));
115-
String index3 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(2));
146+
public void testAutoCreateIndexWithDateMathExpression() {
147+
String index1 = ".marvel-" + DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT).format(now);
148+
String index2 = ".marvel-" + DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT).format(now.minusDays(1));
149+
String index3 = ".marvel-" + DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT).format(now.minusDays(2));
116150

117151
String dateMathExp1 = "<.marvel-{now/d}>";
118152
String dateMathExp2 = "<.marvel-{now/d-1d}>";
@@ -122,29 +156,28 @@ public void testAutoCreateIndexWithDateMathExpression() throws Exception {
122156
client().prepareIndex(dateMathExp3, "type", "3").setSource("{}", XContentType.JSON).get();
123157
refresh();
124158

125-
SearchResponse searchResponse = client().prepareSearch(dateMathExp1, dateMathExp2, dateMathExp3).get();
159+
SearchResponse searchResponse = dateSensitiveGet(client().prepareSearch(dateMathExp1, dateMathExp2, dateMathExp3));
126160
assertHitCount(searchResponse, 3);
127161
assertSearchHits(searchResponse, "1", "2", "3");
128162

129-
IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(dateMathExp1, dateMathExp2, dateMathExp3).get();
163+
IndicesStatsResponse indicesStatsResponse =
164+
dateSensitiveGet(client().admin().indices().prepareStats(dateMathExp1, dateMathExp2, dateMathExp3));
130165
assertThat(indicesStatsResponse.getIndex(index1), notNullValue());
131166
assertThat(indicesStatsResponse.getIndex(index2), notNullValue());
132167
assertThat(indicesStatsResponse.getIndex(index3), notNullValue());
133168
}
134169

135-
public void testCreateIndexWithDateMathExpression() throws Exception {
136-
DateTime now = new DateTime(DateTimeZone.UTC);
137-
String index1 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now);
138-
String index2 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(1));
139-
String index3 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(2));
170+
public void testCreateIndexWithDateMathExpression() {
171+
String index1 = ".marvel-" + DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT).format(now);
172+
String index2 = ".marvel-" + DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT).format(now.minusDays(1));
173+
String index3 = ".marvel-" + DateTimeFormatter.ofPattern("yyyy.MM.dd", Locale.ROOT).format(now.minusDays(2));
140174

141175
String dateMathExp1 = "<.marvel-{now/d}>";
142176
String dateMathExp2 = "<.marvel-{now/d-1d}>";
143177
String dateMathExp3 = "<.marvel-{now/d-2d}>";
144178
createIndex(dateMathExp1, dateMathExp2, dateMathExp3);
145179

146-
147-
GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings(index1, index2, index3).get();
180+
GetSettingsResponse getSettingsResponse = dateSensitiveGet(client().admin().indices().prepareGetSettings(index1, index2, index3));
148181
assertEquals(dateMathExp1, getSettingsResponse.getSetting(index1, IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
149182
assertEquals(dateMathExp2, getSettingsResponse.getSetting(index2, IndexMetaData.SETTING_INDEX_PROVIDED_NAME));
150183
assertEquals(dateMathExp3, getSettingsResponse.getSetting(index3, IndexMetaData.SETTING_INDEX_PROVIDED_NAME));

0 commit comments

Comments
 (0)