Skip to content

Commit 353112a

Browse files
authored
[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 5f0f990 commit 353112a

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,18 @@ static void updateMapping(RollupJob job, ActionListener<AcknowledgedResponse> li
158158
MappingMetaData mappings = getMappingResponse.getMappings().get(indexName).get(RollupField.TYPE_NAME);
159159
Object m = mappings.getSourceAsMap().get("_meta");
160160
if (m == null) {
161-
String msg = "Expected to find _meta key in mapping of rollup index [" + indexName + "] but not found.";
161+
String msg = "Rollup data cannot be added to existing indices that contain non-rollup data (expected " +
162+
"to find _meta key in mapping of rollup index [" + indexName + "] but not found).";
162163
logger.error(msg);
163164
listener.onFailure(new RuntimeException(msg));
164165
return;
165166
}
166167

167168
Map<String, Object> metadata = (Map<String, Object>) m;
168169
if (metadata.get(RollupField.ROLLUP_META) == null) {
169-
String msg = "Expected to find rollup meta key [" + RollupField.ROLLUP_META + "] in mapping of rollup index [" + indexName
170-
+ "] but not found.";
170+
String msg = "Rollup data cannot be added to existing indices that contain non-rollup data (expected " +
171+
"to find rollup meta key [" + RollupField.ROLLUP_META + "] in mapping of rollup index ["
172+
+ indexName + "] but not found).";
171173
logger.error(msg);
172174
listener.onFailure(new RuntimeException(msg));
173175
return;

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

Lines changed: 41 additions & 2 deletions
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

Lines changed: 32 additions & 0 deletions
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)