Skip to content
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 @@ -30,10 +30,12 @@
import org.elasticsearch.client.internal.ParentTaskAssigningClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.MetadataIndexTemplateService;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.VersionType;
Expand Down Expand Up @@ -226,9 +228,20 @@ static class AsyncIndexBySearchAction extends AbstractAsyncBulkByScrollAction<Re
scriptService,
sslConfig
);
this.destinationIndexIdMapper = destinationIndexMode(state).buildNoFieldDataIdFieldMapper();
}

private IndexMode destinationIndexMode(ClusterState state) {
IndexMetadata destMeta = state.metadata().index(mainRequest.getDestination().index());
IndexMode destMode = destMeta == null ? IndexMode.STANDARD : IndexSettings.MODE.get(destMeta.getSettings());
this.destinationIndexIdMapper = destMode.buildNoFieldDataIdFieldMapper();
if (destMeta != null) {
return IndexSettings.MODE.get(destMeta.getSettings());
}
String template = MetadataIndexTemplateService.findV2Template(state.metadata(), mainRequest.getDestination().index(), false);
if (template == null) {
return IndexMode.STANDARD;
}
Settings settings = MetadataIndexTemplateService.resolveSettings(state.metadata(), template);
return IndexSettings.MODE.get(settings);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.ComponentTemplate;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.Template;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexMode;
Expand All @@ -22,27 +25,45 @@
import org.elasticsearch.index.reindex.ScrollableHitSource;
import org.elasticsearch.xcontent.XContentType;

import java.util.List;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

/**
* Reindex
* Reindex tests for picking ids.
*/
public class ReindexIdTests extends AbstractAsyncBulkByScrollActionTestCase<ReindexRequest, BulkByScrollResponse> {
public void testEmptyStateCopiesId() throws Exception {
assertThat(action(ClusterState.EMPTY_STATE).buildRequest(doc()).getId(), equalTo(doc().getId()));
}

public void testStandardIndexCopiesId() throws Exception {
Settings.Builder settings = Settings.builder().put(IndexSettings.MODE.getKey(), IndexMode.STANDARD);
assertThat(action(stateWithIndex(settings)).buildRequest(doc()).getId(), equalTo(doc().getId()));
assertThat(action(stateWithIndex(standardSettings())).buildRequest(doc()).getId(), equalTo(doc().getId()));
}

public void testTsdbIndexClearsId() throws Exception {
Settings.Builder settings = Settings.builder()
.put(IndexSettings.MODE.getKey(), IndexMode.TIME_SERIES)
.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "foo");
assertThat(action(stateWithIndex(settings)).buildRequest(doc()).getId(), nullValue());
assertThat(action(stateWithIndex(tsdbSettings())).buildRequest(doc()).getId(), nullValue());
}

public void testMissingIndexWithStandardTemplateCopiesId() throws Exception {
assertThat(action(stateWithTemplate(standardSettings())).buildRequest(doc()).getId(), equalTo(doc().getId()));
}

public void testMissingIndexWithTsdbTemplateClearsId() throws Exception {
assertThat(action(stateWithTemplate(tsdbSettings())).buildRequest(doc()).getId(), nullValue());
}

private ClusterState stateWithTemplate(Settings.Builder settings) {
Metadata.Builder metadata = Metadata.builder();
Template template = new Template(settings.build(), null, null);
if (randomBoolean()) {
metadata.put("c", new ComponentTemplate(template, null, null));
metadata.put("c", new ComposableIndexTemplate(List.of("dest_index"), null, List.of("c"), null, null, null));
} else {
metadata.put("c", new ComposableIndexTemplate(List.of("dest_index"), template, null, null, null, null));
}
return ClusterState.builder(ClusterState.EMPTY_STATE).metadata(metadata).build();
}

private ClusterState stateWithIndex(Settings.Builder settings) {
Expand All @@ -53,6 +74,19 @@ private ClusterState stateWithIndex(Settings.Builder settings) {
return ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder(Metadata.EMPTY_METADATA).put(meta)).build();
}

private Settings.Builder standardSettings() {
if (randomBoolean()) {
return Settings.builder();
}
return Settings.builder().put(IndexSettings.MODE.getKey(), IndexMode.STANDARD);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe randomly return an empty setting? Since standard is the default.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

private Settings.Builder tsdbSettings() {
return Settings.builder()
.put(IndexSettings.MODE.getKey(), IndexMode.TIME_SERIES)
.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "foo");
}

private ScrollableHitSource.BasicHit doc() {
return new ScrollableHitSource.BasicHit("index", "id", -1).setSource(new BytesArray("{}"), XContentType.JSON);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ protected TestAction action() {

@Override
protected ReindexRequest request() {
return new ReindexRequest();
ReindexRequest request = new ReindexRequest();
request.getDestination().index("test");
return request;
}

private class TestAction extends Reindexer.AsyncIndexBySearchAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public void testSetRouting() throws Exception {

@Override
protected ReindexRequest request() {
return new ReindexRequest();
ReindexRequest request = new ReindexRequest();
request.getDestination().index("test");
return request;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,96 @@ from tsdb to tsdb modifying dimension:
- match: {hits.hits.1._source.@timestamp: 2021-04-28T18:50:23.142Z}
- match: {hits.hits.2._source.@timestamp: 2021-04-28T18:50:53.142Z}
- match: {hits.hits.3._source.@timestamp: 2021-04-28T18:51:03.142Z}

---
from tsdb to tsdb created by template while modifying dimension:
- skip:
version: " - 8.2.99"
reason: introduced in 8.3.0

- do:
cluster.put_component_template:
name: test-component-template
body:
template:
settings:
index:
mode: time_series
routing_path: [metricset, k8s.pod.uid]
number_of_replicas: 0
number_of_shards: 2
mappings:
properties:
"@timestamp":
type: date
metricset:
type: keyword
time_series_dimension: true
k8s:
properties:
pod:
properties:
uid:
type: keyword
time_series_dimension: true
name:
type: keyword
ip:
type: ip
network:
properties:
tx:
type: long
rx:
type: long
- do:
indices.put_index_template:
name: test-composable-1
body:
index_patterns:
- tsdb_templated_*
composed_of:
- test-component-template

- do:
reindex:
refresh: true
body:
source:
index: tsdb
dest:
index: tsdb_templated_new
script:
source: ctx._source["metricset"] = "bubbles"
- match: {created: 4}
- match: {updated: 0}
- match: {version_conflicts: 0}
- match: {batches: 1}
- match: {failures: []}
- match: {throttled_millis: 0}
- gte: { took: 0 }
- is_false: task
- is_false: deleted

- do:
indices.refresh: {}

- do:
search:
index: tsdb_templated_new
body:
sort: '@timestamp'
aggs:
tsids:
terms:
field: _tsid
order:
_key: asc

- match: {hits.total.value: 4}
- match: {aggregations.tsids.buckets.0.key: {k8s.pod.uid: 1c4fc7b8-93b7-4ba8-b609-2a48af2f8e39, metricset: bubbles}}
- match: {aggregations.tsids.buckets.0.doc_count: 4}
- match: {hits.hits.0._source.@timestamp: 2021-04-28T18:50:03.142Z}
- match: {hits.hits.1._source.@timestamp: 2021-04-28T18:50:23.142Z}
- match: {hits.hits.2._source.@timestamp: 2021-04-28T18:50:53.142Z}
- match: {hits.hits.3._source.@timestamp: 2021-04-28T18:51:03.142Z}