Skip to content
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

Remove type from Watcher IndexAction #47986

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ public static EmailAction.Builder emailAction(EmailTemplate email) {
return EmailAction.builder(email);
}

/**
* Types are deprecated and should not be used. use {@link #indexAction(String)}
*/
@Deprecated
public static IndexAction.Builder indexAction(String index, String type) {
return IndexAction.builder(index, type);
}

public static IndexAction.Builder indexAction(String index) {
return IndexAction.builder(index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.xpack.core.ClientHelper;
import org.elasticsearch.xpack.core.watcher.actions.Action;
import org.elasticsearch.xpack.core.watcher.actions.Action.Result.Status;
Expand Down Expand Up @@ -92,7 +91,7 @@ public Action.Result execute(String actionId, WatchExecutionContext ctx, Payload
}

if (ctx.simulateAction(actionId)) {
return new IndexAction.Simulated(indexRequest.index(), MapperService.SINGLE_MAPPING_NAME, indexRequest.id(),
return new IndexAction.Simulated(indexRequest.index(), indexRequest.id(),
action.refreshPolicy, new XContentSource(indexRequest.source(), XContentType.JSON));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
*/
package org.elasticsearch.xpack.watcher.actions.index;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.support.WriteRequest.RefreshPolicy;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -29,31 +27,17 @@ public class IndexAction implements Action {

public static final String TYPE = "index";

@Nullable @Deprecated final String docType;
@Nullable final String index;
@Nullable final String docId;
@Nullable final String executionTimeField;
@Nullable final TimeValue timeout;
@Nullable final ZoneId dynamicNameTimeZone;
@Nullable final RefreshPolicy refreshPolicy;

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(IndexAction.class));
public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Specifying types in a watcher index action is deprecated.";

public IndexAction(@Nullable String index, @Nullable String docId,
@Nullable String executionTimeField,
@Nullable TimeValue timeout, @Nullable ZoneId dynamicNameTimeZone, @Nullable RefreshPolicy refreshPolicy) {
this(index, null, docId, executionTimeField, timeout, dynamicNameTimeZone, refreshPolicy);
}
/**
* Document types are deprecated, use constructor without docType
*/
@Deprecated
public IndexAction(@Nullable String index, @Nullable String docType, @Nullable String docId,
@Nullable String executionTimeField,
@Nullable TimeValue timeout, @Nullable ZoneId dynamicNameTimeZone, @Nullable RefreshPolicy refreshPolicy) {
this.index = index;
this.docType = docType;
this.docId = docId;
this.executionTimeField = executionTimeField;
this.timeout = timeout;
Expand All @@ -70,10 +54,6 @@ public String getIndex() {
return index;
}

public String getDocType() {
return docType;
}

public String getDocId() {
return docId;
}
Expand All @@ -97,7 +77,7 @@ public boolean equals(Object o) {

IndexAction that = (IndexAction) o;

return Objects.equals(index, that.index) && Objects.equals(docType, that.docType) && Objects.equals(docId, that.docId)
return Objects.equals(index, that.index) && Objects.equals(docId, that.docId)
&& Objects.equals(executionTimeField, that.executionTimeField)
&& Objects.equals(timeout, that.timeout)
&& Objects.equals(dynamicNameTimeZone, that.dynamicNameTimeZone)
Expand All @@ -106,7 +86,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(index, docType, docId, executionTimeField, timeout, dynamicNameTimeZone, refreshPolicy);
return Objects.hash(index, docId, executionTimeField, timeout, dynamicNameTimeZone, refreshPolicy);
}

@Override
Expand All @@ -115,9 +95,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (index != null) {
builder.field(Field.INDEX.getPreferredName(), index);
}
if (docType != null) {
builder.field(Field.DOC_TYPE.getPreferredName(), docType);
}
if (docId != null) {
builder.field(Field.DOC_ID.getPreferredName(), docId);
}
Expand All @@ -138,7 +115,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

public static IndexAction parse(String watchId, String actionId, XContentParser parser) throws IOException {
String index = null;
String docType = null;
String docId = null;
String executionTimeField = null;
TimeValue timeout = null;
Expand All @@ -165,10 +141,7 @@ public static IndexAction parse(String watchId, String actionId, XContentParser
watchId, actionId, currentFieldName);
}
} else if (token == XContentParser.Token.VALUE_STRING) {
if (Field.DOC_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
deprecationLogger.deprecatedAndMaybeLog("watcher_index_action", TYPES_DEPRECATION_MESSAGE);
docType = parser.text();
} else if (Field.DOC_ID.match(currentFieldName, parser.getDeprecationHandler())) {
if (Field.DOC_ID.match(currentFieldName, parser.getDeprecationHandler())) {
docId = parser.text();
} else if (Field.EXECUTION_TIME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
executionTimeField = parser.text();
Expand All @@ -194,15 +167,7 @@ public static IndexAction parse(String watchId, String actionId, XContentParser
}
}

return new IndexAction(index, docType, docId, executionTimeField, timeout, dynamicNameTimeZone, refreshPolicy);
}

/**
* Document types are deprecated, use {@link #builder(java.lang.String)}
*/
@Deprecated
public static Builder builder(String index, String docType) {
return new Builder(index, docType);
return new IndexAction(index, docId, executionTimeField, timeout, dynamicNameTimeZone, refreshPolicy);
}

public static Builder builder(String index) {
Expand Down Expand Up @@ -233,16 +198,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
static class Simulated extends Action.Result {

private final String index;
private final String docType;
@Nullable private final String docId;
@Nullable private final RefreshPolicy refreshPolicy;
private final XContentSource source;

protected Simulated(String index, String docType, @Nullable String docId, @Nullable RefreshPolicy refreshPolicy,
protected Simulated(String index, @Nullable String docId, @Nullable RefreshPolicy refreshPolicy,
XContentSource source) {
super(TYPE, Status.SIMULATED);
this.index = index;
this.docType = docType;
this.docId = docId;
this.source = source;
this.refreshPolicy = refreshPolicy;
Expand All @@ -252,10 +215,6 @@ public String index() {
return index;
}

public String docType() {
return docType;
}

public String docId() {
return docId;
}
Expand All @@ -268,8 +227,7 @@ public XContentSource source() {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(type)
.startObject(Field.REQUEST.getPreferredName())
.field(Field.INDEX.getPreferredName(), index)
.field(Field.DOC_TYPE.getPreferredName(), docType);
.field(Field.INDEX.getPreferredName(), index);

if (docId != null) {
builder.field(Field.DOC_ID.getPreferredName(), docId);
Expand All @@ -288,25 +246,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
public static class Builder implements Action.Builder<IndexAction> {

final String index;
final String docType;
String docId;
String executionTimeField;
TimeValue timeout;
ZoneId dynamicNameTimeZone;
RefreshPolicy refreshPolicy;

/**
* Document types are deprecated and should not be used. Use: {@link Builder#Builder(java.lang.String)}
*/
@Deprecated
private Builder(String index, String docType) {
this.index = index;
this.docType = docType;
}

private Builder(String index) {
this.index = index;
this.docType = null;
}

public Builder setDocId(String docId) {
Expand Down Expand Up @@ -336,13 +283,12 @@ public Builder setRefreshPolicy(RefreshPolicy refreshPolicy) {

@Override
public IndexAction build() {
return new IndexAction(index, docType, docId, executionTimeField, timeout, dynamicNameTimeZone, refreshPolicy);
return new IndexAction(index, docId, executionTimeField, timeout, dynamicNameTimeZone, refreshPolicy);
}
}

interface Field {
ParseField INDEX = new ParseField("index");
ParseField DOC_TYPE = new ParseField("doc_type");
ParseField DOC_ID = new ParseField("doc_id");
ParseField EXECUTION_TIME_FIELD = new ParseField("execution_time_field");
ParseField SOURCE = new ParseField("source");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void testCanUseAnyConcreteIndexName() throws Exception {
.trigger(schedule(interval("3s")))
.input(noneInput())
.condition(InternalAlwaysCondition.INSTANCE)
.addAction("indexer", indexAction(watchResultsIndex, "_doc")))
.addAction("indexer", indexAction(watchResultsIndex)))
.get();

assertTrue(putWatchResponse.isCreated());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testErrorInAction() throws Exception {
// adding an action that throws an error and is associated with a 60 minute throttle period
// with such a period, on successful execution we other executions of the watch will be
// throttled within the hour... but on failed execution there should be no throttling
.addAction("_action", TimeValue.timeValueMinutes(60), IndexAction.builder("foo", "bar")))
.addAction("_action", TimeValue.timeValueMinutes(60), IndexAction.builder("foo")))
.get();

assertThat(putWatchResponse.isCreated(), is(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,8 @@ public void testParser() throws Exception {
assertThat(executable.action().timeout, equalTo(writeTimeout));
}

public void testDeprecationTypes() throws Exception {
XContentBuilder builder = jsonBuilder();
builder.startObject();
builder.field(IndexAction.Field.DOC_TYPE.getPreferredName(), "test-type");
builder.endObject();
IndexActionFactory actionParser = new IndexActionFactory(Settings.EMPTY, client);
XContentParser parser = createParser(builder);
parser.nextToken();
ExecutableIndexAction executable = actionParser.parseExecutable(randomAlphaOfLength(5), randomAlphaOfLength(3), parser);
assertThat(executable.action().docType, equalTo("test-type"));
assertWarnings(IndexAction.TYPES_DEPRECATION_MESSAGE);
}

public void testParserFailure() throws Exception {
// wrong type for field
expectParseFailure(jsonBuilder()
.startObject()
.field(IndexAction.Field.DOC_TYPE.getPreferredName(), 1234)
.endObject());

expectParseFailure(jsonBuilder()
.startObject()
.field(IndexAction.Field.TIMEOUT.getPreferredName(), "1234")
Expand Down Expand Up @@ -161,7 +143,7 @@ private void expectFailure(Class clazz, XContentBuilder builder) throws Exceptio
}

public void testUsingParameterIdWithBulkOrIdFieldThrowsIllegalState() {
final IndexAction action = new IndexAction("test-index", "test-type", "123", null, null, null, refreshPolicy);
final IndexAction action = new IndexAction("test-index", "123", null, null, null, refreshPolicy);
final ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client,
TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30));
final Map<String, Object> docWithId = Map.of(
Expand Down Expand Up @@ -196,23 +178,18 @@ public void testUsingParameterIdWithBulkOrIdFieldThrowsIllegalState() {

public void testThatIndexTypeIdDynamically() throws Exception {
boolean configureIndexDynamically = randomBoolean();
boolean configureTypeDynamically = randomBoolean();
boolean configureIdDynamically = (configureTypeDynamically == false && configureIndexDynamically == false) || randomBoolean();
boolean configureIdDynamically = configureIndexDynamically == false || randomBoolean();

var entries = new ArrayList<Map.Entry<String, Object>>(4);
entries.add(entry("foo", "bar"));
if (configureIdDynamically) {
entries.add(entry("_id", "my_dynamic_id"));
}
if (configureTypeDynamically) {
entries.add(entry("_type", "my_dynamic_type"));
}
if (configureIndexDynamically) {
entries.add(entry("_index", "my_dynamic_index"));
}

final IndexAction action = new IndexAction(configureIndexDynamically ? null : "my_index",
configureTypeDynamically ? null : "my_type",
configureIdDynamically ? null : "my_id",
null, null, null, refreshPolicy);
final ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client,
Expand All @@ -234,7 +211,7 @@ public void testThatIndexTypeIdDynamically() throws Exception {
}

public void testThatIndexActionCanBeConfiguredWithDynamicIndexNameAndBulk() throws Exception {
final IndexAction action = new IndexAction(null, "my-type", null, null, null, null, refreshPolicy);
final IndexAction action = new IndexAction(null, null, null, null, null, refreshPolicy);
final ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client,
TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30));

Expand Down Expand Up @@ -281,7 +258,7 @@ public void testIndexActionExecuteSingleDoc() throws Exception {
String docId = randomAlphaOfLength(5);
String timestampField = randomFrom("@timestamp", null);

IndexAction action = new IndexAction("test-index", "test-type", docIdAsParam ? docId : null, timestampField, null, null,
IndexAction action = new IndexAction("test-index", docIdAsParam ? docId : null, timestampField, null, null,
refreshPolicy);
ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client, TimeValue.timeValueSeconds(30),
TimeValue.timeValueSeconds(30));
Expand Down Expand Up @@ -331,7 +308,7 @@ public void testIndexActionExecuteSingleDoc() throws Exception {
}

public void testFailureResult() throws Exception {
IndexAction action = new IndexAction("test-index", "test-type", null, "@timestamp", null, null, refreshPolicy);
IndexAction action = new IndexAction("test-index", null, "@timestamp", null, null, refreshPolicy);
ExecutableIndexAction executable = new ExecutableIndexAction(action, logger, client,
TimeValue.timeValueSeconds(30), TimeValue.timeValueSeconds(30));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public String type() {
INDEX {
@Override
public Action.Builder<IndexAction> action() throws Exception {
return IndexAction.builder("test_index", "test_type");
return IndexAction.builder("test_index");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void testQueuedStats() throws ExecutionException, InterruptedException {
.addAction(
"action",
TimeValue.timeValueSeconds(1),
IndexAction.builder("test_index", "acknowledgement").setDocId("id")))
IndexAction.builder("test_index").setDocId("id")))
.get();

final int numberOfIterations = 128 - scaledRandomIntBetween(0, 128);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void testSearchTransform() throws Exception {
.trigger(schedule(interval("5s")))
.input(searchInput(inputRequest))
.transform(searchTransform(transformRequest))
.addAction("_id", indexAction("output1", "result"))
.addAction("_id", indexAction("output1"))
).get();
assertThat(putWatchResponse.isCreated(), is(true));
putWatchResponse = new PutWatchRequestBuilder(client(), "_id2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,14 @@ private List<ActionWrapper> randomActions() {
randomFrom(DataAttachment.JSON, DataAttachment.YAML), EmailAttachments.EMPTY_ATTACHMENTS);
list.add(new ActionWrapper("_email_" + randomAlphaOfLength(8), randomThrottler(),
AlwaysConditionTests.randomCondition(scriptService), randomTransform(),
new ExecutableEmailAction(action, logger, emailService, templateEngine, htmlSanitizer,
new ExecutableEmailAction(action, logger, emailService, templateEngine, htmlSanitizer,
Collections.emptyMap()), null, null));
}
if (randomBoolean()) {
ZoneOffset timeZone = randomBoolean() ? ZoneOffset.UTC : null;
TimeValue timeout = randomBoolean() ? timeValueSeconds(between(1, 10000)) : null;
WriteRequest.RefreshPolicy refreshPolicy = randomBoolean() ? null : randomFrom(WriteRequest.RefreshPolicy.values());
IndexAction action = new IndexAction("_index", null, randomBoolean() ? "123" : null, null, timeout, timeZone,
IndexAction action = new IndexAction("_index", randomBoolean() ? "123" : null, null, timeout, timeZone,
refreshPolicy);
list.add(new ActionWrapper("_index_" + randomAlphaOfLength(8), randomThrottler(),
AlwaysConditionTests.randomCondition(scriptService), randomTransform(),
Expand Down