Skip to content

Commit bc54710

Browse files
committed
rollover info test
1 parent b0a593f commit bc54710

File tree

1 file changed

+125
-6
lines changed

1 file changed

+125
-6
lines changed

x-pack/plugin/migrate/src/internalClusterTest/java/org/elasticsearch/xpack/migrate/action/CopyIndexMetadataTransportActionIT.java

Lines changed: 125 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,35 @@
77

88
package org.elasticsearch.xpack.migrate.action;
99

10+
import org.elasticsearch.action.DocWriteRequest;
11+
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
1012
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
13+
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
14+
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
15+
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
1116
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
17+
import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction;
18+
import org.elasticsearch.action.datastreams.CreateDataStreamAction;
19+
import org.elasticsearch.action.datastreams.GetDataStreamAction;
20+
import org.elasticsearch.action.index.IndexRequest;
21+
import org.elasticsearch.action.support.IndicesOptions;
22+
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
1223
import org.elasticsearch.cluster.metadata.IndexMetadata;
24+
import org.elasticsearch.cluster.metadata.Template;
25+
import org.elasticsearch.common.compress.CompressedXContent;
1326
import org.elasticsearch.datastreams.DataStreamsPlugin;
1427
import org.elasticsearch.ingest.common.IngestCommonPlugin;
1528
import org.elasticsearch.plugins.Plugin;
1629
import org.elasticsearch.reindex.ReindexPlugin;
1730
import org.elasticsearch.test.ESIntegTestCase;
1831
import org.elasticsearch.test.transport.MockTransportService;
32+
import org.elasticsearch.xcontent.json.JsonXContent;
1933
import org.elasticsearch.xpack.migrate.MigratePlugin;
2034

2135
import java.util.Collection;
2236
import java.util.List;
2337
import java.util.Locale;
38+
import java.util.concurrent.TimeUnit;
2439

2540
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
2641

@@ -58,12 +73,8 @@ public void testCreationDate() throws Exception {
5873
assertNotEquals(sourceDate, destDate);
5974
}
6075

61-
assertAcked(
62-
client().execute(
63-
CopyIndexMetadataAction.INSTANCE,
64-
new CopyIndexMetadataAction.Request(TEST_REQUEST_TIMEOUT, sourceIndex, destIndex)
65-
)
66-
);
76+
// copy over the metadata
77+
copyMetadata(sourceIndex, destIndex);
6778

6879
var destDate = indicesAdmin().getSettings(new GetSettingsRequest().indices(sourceIndex, destIndex))
6980
.actionGet()
@@ -72,4 +83,112 @@ public void testCreationDate() throws Exception {
7283
.getAsLong(IndexMetadata.SETTING_CREATION_DATE, 0L);
7384
assertEquals(sourceDate, destDate);
7485
}
86+
87+
public void testRolloverInfos() throws Exception {
88+
89+
var dataStream = createDataStream();
90+
91+
// rollover a few times
92+
createDocument(dataStream);
93+
assertTrue(indicesAdmin().rolloverIndex(new RolloverRequest(dataStream, null)).get().isAcknowledged());
94+
createDocument(dataStream);
95+
assertTrue(indicesAdmin().rolloverIndex(new RolloverRequest(dataStream, null)).get().isAcknowledged());
96+
createDocument(dataStream);
97+
assertTrue(indicesAdmin().rolloverIndex(new RolloverRequest(dataStream, null)).get().isAcknowledged());
98+
99+
var writeIndex = safeGet(
100+
indicesAdmin().execute(
101+
GetDataStreamAction.INSTANCE,
102+
new GetDataStreamAction.Request(TEST_REQUEST_TIMEOUT, new String[] { dataStream })
103+
)
104+
).getDataStreams().get(0).getDataStream().getWriteIndex().getName();
105+
106+
var getIndexResponse = indicesAdmin().getIndex(new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(dataStream)).get();
107+
for (var backingIndex : getIndexResponse.indices()) {
108+
109+
var destIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT);
110+
indicesAdmin().create(new CreateIndexRequest(destIndex)).get();
111+
112+
var metadataBefore = safeGet(
113+
clusterAdmin().state(new ClusterStateRequest(TEST_REQUEST_TIMEOUT).indices(backingIndex, destIndex))
114+
).getState().metadata();
115+
IndexMetadata source = metadataBefore.index(backingIndex);
116+
IndexMetadata destBefore = metadataBefore.index(destIndex);
117+
118+
// sanity check not equal before the copy
119+
if (backingIndex.equals(writeIndex)) {
120+
assertTrue(source.getRolloverInfos().isEmpty());
121+
assertTrue(destBefore.getRolloverInfos().isEmpty());
122+
} else {
123+
assertNotEquals(source.getRolloverInfos(), destBefore.getRolloverInfos());
124+
}
125+
126+
// copy over the metadata
127+
copyMetadata(backingIndex, destIndex);
128+
129+
var metadataAfter = safeGet(clusterAdmin().state(new ClusterStateRequest(TEST_REQUEST_TIMEOUT).indices(destIndex)))
130+
.getState()
131+
.metadata();
132+
IndexMetadata destAfter = metadataAfter.index(destIndex);
133+
134+
// now rollover info should be equal
135+
assertEquals(source.getRolloverInfos(), destAfter.getRolloverInfos());
136+
}
137+
}
138+
139+
private String createDataStream() throws Exception {
140+
String dataStreamName = randomAlphaOfLength(10).toLowerCase(Locale.getDefault());
141+
142+
Template idxTemplate = new Template(null, new CompressedXContent("""
143+
{"properties":{"@timestamp":{"type":"date"},"data":{"type":"keyword"}}}
144+
"""), null);
145+
146+
ComposableIndexTemplate template = ComposableIndexTemplate.builder()
147+
.indexPatterns(List.of(dataStreamName + "*"))
148+
.template(idxTemplate)
149+
.dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false))
150+
.build();
151+
152+
assertAcked(
153+
client().execute(
154+
TransportPutComposableIndexTemplateAction.TYPE,
155+
new TransportPutComposableIndexTemplateAction.Request(dataStreamName + "_template").indexTemplate(template)
156+
)
157+
);
158+
assertAcked(
159+
client().execute(
160+
CreateDataStreamAction.INSTANCE,
161+
new CreateDataStreamAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, dataStreamName)
162+
)
163+
);
164+
return dataStreamName;
165+
}
166+
167+
private long createDocument(String dataStreamName) throws Exception {
168+
// Get some randomized but reasonable timestamps on the data since not all of it is guaranteed to arrive in order.
169+
long timeSeed = System.currentTimeMillis();
170+
long timestamp = randomLongBetween(timeSeed - TimeUnit.HOURS.toMillis(5), timeSeed);
171+
client().index(
172+
new IndexRequest(dataStreamName).opType(DocWriteRequest.OpType.CREATE)
173+
.source(
174+
JsonXContent.contentBuilder()
175+
.startObject()
176+
.field("@timestamp", timestamp)
177+
.field("data", randomAlphaOfLength(25))
178+
.endObject()
179+
)
180+
).get();
181+
indicesAdmin().refresh(new RefreshRequest(".ds-" + dataStreamName + "*").indicesOptions(IndicesOptions.lenientExpandOpenHidden()))
182+
.get();
183+
return timestamp;
184+
}
185+
186+
private void copyMetadata(String sourceIndex, String destIndex) {
187+
assertAcked(
188+
client().execute(
189+
CopyIndexMetadataAction.INSTANCE,
190+
new CopyIndexMetadataAction.Request(TEST_REQUEST_TIMEOUT, sourceIndex, destIndex)
191+
)
192+
);
193+
}
75194
}

0 commit comments

Comments
 (0)