-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Rollup] Disallow index patterns that match rollup indices #30491
Changes from 1 commit
c660ebe
733e12d
98a47f8
2c3185c
275b594
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest; | ||
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.IndicesOptions; | ||
import org.elasticsearch.action.support.master.TransportMasterNodeAction; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.ClusterState; | ||
|
@@ -44,10 +45,12 @@ | |
import org.elasticsearch.xpack.core.XPackField; | ||
import org.elasticsearch.xpack.core.rollup.RollupField; | ||
import org.elasticsearch.xpack.core.rollup.action.PutRollupJobAction; | ||
import org.elasticsearch.xpack.core.rollup.action.RollupJobCaps; | ||
import org.elasticsearch.xpack.core.rollup.job.RollupJob; | ||
import org.elasticsearch.xpack.core.rollup.job.RollupJobConfig; | ||
import org.elasticsearch.xpack.rollup.Rollup; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
@@ -91,9 +94,16 @@ protected void masterOperation(PutRollupJobAction.Request request, ClusterState | |
return; | ||
} | ||
|
||
// Check to see if the index pattern matches any existing rollup indices | ||
ActionRequestValidationException validationException = validateIndexPattern(request, clusterState); | ||
if (validationException != null) { | ||
listener.onFailure(validationException); | ||
return; | ||
} | ||
|
||
FieldCapabilitiesRequest fieldCapsRequest = new FieldCapabilitiesRequest() | ||
.indices(request.getConfig().getIndexPattern()) | ||
.fields(request.getConfig().getAllFields().toArray(new String[0])); | ||
.indices(request.getConfig().getIndexPattern()) | ||
.fields(request.getConfig().getAllFields().toArray(new String[0])); | ||
|
||
client.fieldCaps(fieldCapsRequest, new ActionListener<FieldCapabilitiesResponse>() { | ||
@Override | ||
|
@@ -115,6 +125,26 @@ public void onFailure(Exception e) { | |
}); | ||
} | ||
|
||
/** | ||
* Ensure that the index pattern doesn't match any pre-existing rollup indices. We don't need to check | ||
* if the pattern would match it's own rollup index because the request already validated that. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can be less strict and only forbid mixing rollup and non-rollup indices in the same pattern ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point about the field name validation, although i'm still a bit worried about unintended consequences of matching against a rollup index. Barring the "rollup a rollup" usecase, it doesn't seem wise to let users match against an existing rollup if we can help it? But maybe it's not worth the extra complexity? If it matches another rollup index, that index will exist so we can validate against the fields and it should throw an exception. It also doesn't protect against future rollup indices from matching the defined pattern, so we'd be relying on field level validation there anyway. I think I'm convinced. Will make the changes, should simplify the PR a bunch :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, it turns out to be a bit more complicated. The field-level validation is broken, due to how field caps behaves. Field caps only tells you the caps for the fields that match, in the resolved indices. But it doesn't tell you about the indices that were resolved but didn't match any field. So we won't know the index pattern matched some random index without the correct fields. That's unrelated to this change to this PR, but needs to be fixed for us to simplify. Going to see what it takes, we may have to manually resolve the indices anyway to check. Will keep you updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. We can also add the matching indices in field caps's response. Could be per field : the list of index that doesn't have a definition for the field or global: the list of indices that match the pattern and then you can infer which index is missing from the field's response. I think it's a valid feature to add even if we don't use it in rollups. |
||
private static ActionRequestValidationException validateIndexPattern(PutRollupJobAction.Request request, ClusterState clusterState) { | ||
IndexNameExpressionResolver resolver = new IndexNameExpressionResolver(Settings.EMPTY); | ||
String[] indices = resolver.concreteIndexNames(clusterState, IndicesOptions.strictExpandOpenAndForbidClosed(), | ||
request.getConfig().getIndexPattern()); | ||
|
||
ActionRequestValidationException validationException = new ActionRequestValidationException(); | ||
|
||
Arrays.stream(indices) | ||
.forEach(index -> TransportGetRollupCapsAction.findRollupIndexCaps(index, clusterState.getMetaData().index(index)) | ||
.ifPresent(c -> validationException | ||
.addValidationError("Index pattern [" + request.getConfig().getIndexPattern() + "] matches existing rollup index [" | ||
+ index + "] from jobs " + c.getJobCaps().stream().map(RollupJobCaps::getJobID).collect(Collectors.toList())))); | ||
|
||
return validationException.validationErrors().size() > 0 ? validationException : null; | ||
} | ||
|
||
private static RollupJob createRollupJob(RollupJobConfig config, ThreadPool threadPool) { | ||
// ensure we only filter for the allowed headers | ||
Map<String, String> filteredHeaders = threadPool.getThreadContext().getHeaders().entrySet().stream() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch !