|
6 | 6 |
|
7 | 7 | package org.elasticsearch.xpack.ccr;
|
8 | 8 |
|
| 9 | +import org.elasticsearch.ExceptionsHelper; |
9 | 10 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
10 | 11 | import org.elasticsearch.common.bytes.BytesReference;
|
11 | 12 | import org.elasticsearch.common.settings.Settings;
|
|
28 | 29 | import static java.util.Collections.singletonMap;
|
29 | 30 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
30 | 31 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
| 32 | +import static org.hamcrest.Matchers.containsString; |
31 | 33 | import static org.hamcrest.Matchers.equalTo;
|
| 34 | +import static org.hamcrest.Matchers.hasSize; |
32 | 35 | import static org.hamcrest.Matchers.nullValue;
|
33 | 36 |
|
34 | 37 | public class LocalIndexFollowingIT extends CcrSingleNodeTestCase {
|
@@ -141,6 +144,50 @@ public void testRemoveRemoteConnection() throws Exception {
|
141 | 144 | });
|
142 | 145 | }
|
143 | 146 |
|
| 147 | + public void testChangeLeaderIndex() throws Exception { |
| 148 | + final String settings = getIndexSettings(1, 0, singletonMap(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "true")); |
| 149 | + |
| 150 | + // First, let index-1 is writable and index-2 follows index-1 |
| 151 | + assertAcked(client().admin().indices().prepareCreate("index-1").setSource(settings, XContentType.JSON)); |
| 152 | + ensureGreen("index-1"); |
| 153 | + int numDocs = between(1, 100); |
| 154 | + for (int i = 0; i < numDocs; i++) { |
| 155 | + client().prepareIndex("index-1", "doc").setSource("{}", XContentType.JSON).get(); |
| 156 | + } |
| 157 | + client().execute(PutFollowAction.INSTANCE, getPutFollowRequest("index-1", "index-2")).get(); |
| 158 | + assertBusy(() -> assertThat(client().prepareSearch("index-2").get().getHits().totalHits, equalTo((long) numDocs))); |
| 159 | + |
| 160 | + // Then switch index-1 to be a follower of index-0 |
| 161 | + assertAcked(client().admin().indices().prepareCreate("index-0").setSource(settings, XContentType.JSON)); |
| 162 | + final int newDocs; |
| 163 | + if (randomBoolean()) { |
| 164 | + newDocs = randomIntBetween(0, numDocs); |
| 165 | + } else { |
| 166 | + newDocs = numDocs + randomIntBetween(1, 100); |
| 167 | + } |
| 168 | + for (int i = 0; i < newDocs; i++) { |
| 169 | + client().prepareIndex("index-0", "doc").setSource("{}", XContentType.JSON).get(); |
| 170 | + } |
| 171 | + if (randomBoolean()) { |
| 172 | + client().admin().indices().prepareFlush("index-0").get(); |
| 173 | + } |
| 174 | + assertAcked(client().admin().indices().prepareClose("index-1")); |
| 175 | + client().execute(PutFollowAction.INSTANCE, getPutFollowRequest("index-0", "index-1")).get(); |
| 176 | + |
| 177 | + // index-2 should detect that the leader index has changed |
| 178 | + assertBusy(() -> { |
| 179 | + FollowStatsAction.StatsRequest statsRequest = new FollowStatsAction.StatsRequest(); |
| 180 | + statsRequest.setIndices(new String[]{"index-2"}); |
| 181 | + FollowStatsAction.StatsResponses resp = client().execute(FollowStatsAction.INSTANCE, statsRequest).actionGet(); |
| 182 | + assertThat(resp.getStatsResponses(), hasSize(1)); |
| 183 | + FollowStatsAction.StatsResponse stats = resp.getStatsResponses().get(0); |
| 184 | + assertNotNull(stats.status().getFatalException()); |
| 185 | + Throwable unwrapped = ExceptionsHelper.unwrap(stats.status().getFatalException(), IllegalStateException.class); |
| 186 | + assertNotNull(unwrapped); |
| 187 | + assertThat(unwrapped.getMessage(), containsString("unexpected history uuid")); |
| 188 | + }); |
| 189 | + } |
| 190 | + |
144 | 191 | public static String getIndexSettings(final int numberOfShards,
|
145 | 192 | final int numberOfReplicas,
|
146 | 193 | final Map<String, String> additionalIndexSettings) throws IOException {
|
|
0 commit comments