Skip to content

Commit 9e072f7

Browse files
committed
[Rollup] Better error message when trying to set non-rollup index (#32965)
We don't allow the user to configure a rollup index against an existing index, but the exceptions that we return are not clear about that. They indicate issues with metadata, instead of stating the real reason (not allowed to use a non-rollup index to store rollup data). This makes the exception better, and adds a bit more testing
1 parent 1e110c9 commit 9e072f7

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/action/TransportPutRollupJobAction.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,18 @@ static void updateMapping(RollupJob job, ActionListener<AcknowledgedResponse> li
161161
MappingMetaData mappings = getMappingResponse.getMappings().get(indexName).get(RollupField.TYPE_NAME);
162162
Object m = mappings.getSourceAsMap().get("_meta");
163163
if (m == null) {
164-
String msg = "Expected to find _meta key in mapping of rollup index [" + indexName + "] but not found.";
164+
String msg = "Rollup data cannot be added to existing indices that contain non-rollup data (expected " +
165+
"to find _meta key in mapping of rollup index [" + indexName + "] but not found).";
165166
logger.error(msg);
166167
listener.onFailure(new RuntimeException(msg));
167168
return;
168169
}
169170

170171
Map<String, Object> metadata = (Map<String, Object>) m;
171172
if (metadata.get(RollupField.ROLLUP_META) == null) {
172-
String msg = "Expected to find rollup meta key [" + RollupField.ROLLUP_META + "] in mapping of rollup index [" + indexName
173-
+ "] but not found.";
173+
String msg = "Rollup data cannot be added to existing indices that contain non-rollup data (expected " +
174+
"to find rollup meta key [" + RollupField.ROLLUP_META + "] in mapping of rollup index ["
175+
+ indexName + "] but not found).";
174176
logger.error(msg);
175177
listener.onFailure(new RuntimeException(msg));
176178
return;

x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/action/PutJobStateMachineTests.java

+41-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@ public void testNoMetadataInMapping() {
180180
ActionListener<AcknowledgedResponse> testListener = ActionListener.wrap(response -> {
181181
fail("Listener success should not have been triggered.");
182182
}, e -> {
183-
assertThat(e.getMessage(), equalTo("Expected to find _meta key in mapping of rollup index ["
184-
+ job.getConfig().getRollupIndex() + "] but not found."));
183+
assertThat(e.getMessage(), equalTo("Rollup data cannot be added to existing indices that contain " +
184+
"non-rollup data (expected to find _meta key in mapping of rollup index ["
185+
+ job.getConfig().getRollupIndex() + "] but not found)."));
185186
});
186187

187188
Logger logger = mock(Logger.class);
@@ -206,6 +207,44 @@ public void testNoMetadataInMapping() {
206207
verify(client).execute(eq(GetMappingsAction.INSTANCE), any(GetMappingsRequest.class), any());
207208
}
208209

210+
@SuppressWarnings("unchecked")
211+
public void testMetadataButNotRollup() {
212+
RollupJob job = new RollupJob(ConfigTestHelpers.randomRollupJobConfig(random()), Collections.emptyMap());
213+
214+
ActionListener<AcknowledgedResponse> testListener = ActionListener.wrap(response -> {
215+
fail("Listener success should not have been triggered.");
216+
}, e -> {
217+
assertThat(e.getMessage(), equalTo("Rollup data cannot be added to existing indices that contain " +
218+
"non-rollup data (expected to find rollup meta key [_rollup] in mapping of rollup index ["
219+
+ job.getConfig().getRollupIndex() + "] but not found)."));
220+
});
221+
222+
Logger logger = mock(Logger.class);
223+
Client client = mock(Client.class);
224+
225+
ArgumentCaptor<ActionListener> requestCaptor = ArgumentCaptor.forClass(ActionListener.class);
226+
doAnswer(invocation -> {
227+
GetMappingsResponse response = mock(GetMappingsResponse.class);
228+
Map<String, Object> m = new HashMap<>(2);
229+
m.put("random",
230+
Collections.singletonMap(job.getConfig().getId(), job.getConfig()));
231+
MappingMetaData meta = new MappingMetaData(RollupField.TYPE_NAME,
232+
Collections.singletonMap("_meta", m));
233+
ImmutableOpenMap.Builder<String, MappingMetaData> builder = ImmutableOpenMap.builder(1);
234+
builder.put(RollupField.TYPE_NAME, meta);
235+
236+
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> builder2 = ImmutableOpenMap.builder(1);
237+
builder2.put(job.getConfig().getRollupIndex(), builder.build());
238+
239+
when(response.getMappings()).thenReturn(builder2.build());
240+
requestCaptor.getValue().onResponse(response);
241+
return null;
242+
}).when(client).execute(eq(GetMappingsAction.INSTANCE), any(GetMappingsRequest.class), requestCaptor.capture());
243+
244+
TransportPutRollupJobAction.updateMapping(job, testListener, mock(PersistentTasksService.class), client, logger);
245+
verify(client).execute(eq(GetMappingsAction.INSTANCE), any(GetMappingsRequest.class), any());
246+
}
247+
209248
@SuppressWarnings("unchecked")
210249
public void testNoMappingVersion() {
211250
RollupJob job = new RollupJob(ConfigTestHelpers.randomRollupJobConfig(random()), Collections.emptyMap());

x-pack/plugin/src/test/resources/rest-api-spec/test/rollup/put_job.yml

+32
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,38 @@ setup:
128128
]
129129
}
130130
131+
---
132+
"Test put_job in non-rollup index":
133+
- do:
134+
indices.create:
135+
index: non-rollup
136+
- do:
137+
catch: /foo/
138+
headers:
139+
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
140+
xpack.rollup.put_job:
141+
id: foo
142+
body: >
143+
{
144+
"index_pattern": "foo",
145+
"rollup_index": "non-rollup",
146+
"cron": "*/30 * * * * ?",
147+
"page_size" :10,
148+
"groups" : {
149+
"date_histogram": {
150+
"field": "the_field",
151+
"interval": "1h"
152+
}
153+
},
154+
"metrics": [
155+
{
156+
"field": "value_field",
157+
"metrics": ["min", "max", "sum"]
158+
}
159+
]
160+
}
161+
162+
131163
---
132164
"Try to include headers":
133165

0 commit comments

Comments
 (0)