|
10 | 10 | import org.elasticsearch.client.RestClient;
|
11 | 11 | import org.elasticsearch.common.Strings;
|
12 | 12 | import org.elasticsearch.common.settings.Settings;
|
| 13 | +import org.elasticsearch.common.xcontent.ObjectPath; |
13 | 14 | import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
14 | 15 |
|
15 | 16 | import java.io.IOException;
|
@@ -88,6 +89,128 @@ public void testIndexFollowing() throws Exception {
|
88 | 89 | }
|
89 | 90 | }
|
90 | 91 |
|
| 92 | + public void testAutoFollowing() throws Exception { |
| 93 | + final Settings indexSettings = Settings.builder() |
| 94 | + .put("index.soft_deletes.enabled", true) |
| 95 | + .put("index.number_of_shards", 1) |
| 96 | + .build(); |
| 97 | + |
| 98 | + String leaderIndex1 = "logs-20200101"; |
| 99 | + String leaderIndex2 = "logs-20200102"; |
| 100 | + String leaderIndex3 = "logs-20200103"; |
| 101 | + |
| 102 | + if (clusterName == ClusterName.LEADER) { |
| 103 | + switch (upgradeState) { |
| 104 | + case NONE: |
| 105 | + case ONE_THIRD: |
| 106 | + case TWO_THIRD: |
| 107 | + break; |
| 108 | + case ALL: |
| 109 | + index(leaderClient(), leaderIndex1, 64); |
| 110 | + assertBusy(() -> { |
| 111 | + String followerIndex = "copy-" + leaderIndex1; |
| 112 | + assertTotalHitCount(followerIndex, 320, followerClient()); |
| 113 | + }); |
| 114 | + index(leaderClient(), leaderIndex2, 64); |
| 115 | + assertBusy(() -> { |
| 116 | + String followerIndex = "copy-" + leaderIndex2; |
| 117 | + assertTotalHitCount(followerIndex, 256, followerClient()); |
| 118 | + }); |
| 119 | + index(leaderClient(), leaderIndex3, 64); |
| 120 | + assertBusy(() -> { |
| 121 | + String followerIndex = "copy-" + leaderIndex3; |
| 122 | + assertTotalHitCount(followerIndex, 192, followerClient()); |
| 123 | + }); |
| 124 | + |
| 125 | + deleteAutoFollowPattern(followerClient(), "test_pattern"); |
| 126 | + stopIndexFollowing(followerClient(), "copy-" + leaderIndex1); |
| 127 | + stopIndexFollowing(followerClient(), "copy-" + leaderIndex2); |
| 128 | + stopIndexFollowing(followerClient(), "copy-" + leaderIndex3); |
| 129 | + break; |
| 130 | + default: |
| 131 | + throw new AssertionError("unexpected upgrade_state [" + upgradeState + "]"); |
| 132 | + } |
| 133 | + } else if (clusterName == ClusterName.FOLLOWER) { |
| 134 | + switch (upgradeState) { |
| 135 | + case NONE: |
| 136 | + putAutoFollowPattern(followerClient(), "test_pattern", "leader", "logs-*"); |
| 137 | + createIndex(leaderIndex1, indexSettings); |
| 138 | + index(leaderClient(), leaderIndex1, 64); |
| 139 | + assertBusy(() -> { |
| 140 | + String followerIndex = "copy-" + leaderIndex1; |
| 141 | + assertThat(getNumberOfSuccessfulFollowedIndices(), equalTo(1)); |
| 142 | + assertTotalHitCount(followerIndex, 64, followerClient()); |
| 143 | + }); |
| 144 | + break; |
| 145 | + case ONE_THIRD: |
| 146 | + index(leaderClient(), leaderIndex1, 64); |
| 147 | + assertBusy(() -> { |
| 148 | + String followerIndex = "copy-" + leaderIndex1; |
| 149 | + assertTotalHitCount(followerIndex, 128, followerClient()); |
| 150 | + }); |
| 151 | + // Auto follow stats are kept in-memory on master elected node |
| 152 | + // and if this node get updated then auto follow stats are reset |
| 153 | + { |
| 154 | + int previousNumberOfSuccessfulFollowedIndices = getNumberOfSuccessfulFollowedIndices(); |
| 155 | + createIndex(leaderIndex2, indexSettings); |
| 156 | + index(leaderClient(), leaderIndex2, 64); |
| 157 | + assertBusy(() -> { |
| 158 | + String followerIndex = "copy-" + leaderIndex2; |
| 159 | + assertThat(getNumberOfSuccessfulFollowedIndices(), equalTo(previousNumberOfSuccessfulFollowedIndices + 1)); |
| 160 | + assertTotalHitCount(followerIndex, 64, followerClient()); |
| 161 | + }); |
| 162 | + } |
| 163 | + break; |
| 164 | + case TWO_THIRD: |
| 165 | + index(leaderClient(), leaderIndex1, 64); |
| 166 | + assertBusy(() -> { |
| 167 | + String followerIndex = "copy-" + leaderIndex1; |
| 168 | + assertTotalHitCount(followerIndex, 192, followerClient()); |
| 169 | + }); |
| 170 | + index(leaderClient(), leaderIndex2, 64); |
| 171 | + assertBusy(() -> { |
| 172 | + String followerIndex = "copy-" + leaderIndex2; |
| 173 | + assertTotalHitCount(followerIndex, 128, followerClient()); |
| 174 | + }); |
| 175 | + |
| 176 | + // Auto follow stats are kept in-memory on master elected node |
| 177 | + // and if this node get updated then auto follow stats are reset |
| 178 | + { |
| 179 | + int previousNumberOfSuccessfulFollowedIndices = getNumberOfSuccessfulFollowedIndices(); |
| 180 | + createIndex(leaderIndex3, indexSettings); |
| 181 | + index(leaderClient(), leaderIndex3, 64); |
| 182 | + assertBusy(() -> { |
| 183 | + String followerIndex = "copy-" + leaderIndex3; |
| 184 | + assertThat(getNumberOfSuccessfulFollowedIndices(), equalTo(previousNumberOfSuccessfulFollowedIndices + 1)); |
| 185 | + assertTotalHitCount(followerIndex, 64, followerClient()); |
| 186 | + }); |
| 187 | + } |
| 188 | + break; |
| 189 | + case ALL: |
| 190 | + index(leaderClient(), leaderIndex1, 64); |
| 191 | + assertBusy(() -> { |
| 192 | + String followerIndex = "copy-" + leaderIndex1; |
| 193 | + assertTotalHitCount(followerIndex, 256, followerClient()); |
| 194 | + }); |
| 195 | + index(leaderClient(), leaderIndex2, 64); |
| 196 | + assertBusy(() -> { |
| 197 | + String followerIndex = "copy-" + leaderIndex2; |
| 198 | + assertTotalHitCount(followerIndex, 192, followerClient()); |
| 199 | + }); |
| 200 | + index(leaderClient(), leaderIndex3, 64); |
| 201 | + assertBusy(() -> { |
| 202 | + String followerIndex = "copy-" + leaderIndex3; |
| 203 | + assertTotalHitCount(followerIndex, 128, followerClient()); |
| 204 | + }); |
| 205 | + break; |
| 206 | + default: |
| 207 | + throw new UnsupportedOperationException("unexpected upgrade state [" + upgradeState + "]"); |
| 208 | + } |
| 209 | + } else { |
| 210 | + throw new AssertionError("unexpected cluster_name [" + clusterName + "]"); |
| 211 | + } |
| 212 | + } |
| 213 | + |
91 | 214 | public void testCannotFollowLeaderInUpgradedCluster() throws Exception {
|
92 | 215 | assumeTrue("Tests only runs with upgrade_state [all]", upgradeState == UpgradeState.ALL);
|
93 | 216 |
|
@@ -134,6 +257,29 @@ private static void followIndex(RestClient client, String leaderCluster, String
|
134 | 257 | assertOK(client.performRequest(request));
|
135 | 258 | }
|
136 | 259 |
|
| 260 | + private static void putAutoFollowPattern(RestClient client, String name, String remoteCluster, String pattern) throws IOException { |
| 261 | + Request request = new Request("PUT", "/_ccr/auto_follow/" + name); |
| 262 | + request.setJsonEntity("{\"leader_index_patterns\": [\"" + pattern + "\"], \"remote_cluster\": \"" + remoteCluster + "\"," + |
| 263 | + "\"follow_index_pattern\": \"copy-{{leader_index}}\", \"read_poll_timeout\": \"10ms\"}"); |
| 264 | + assertOK(client.performRequest(request)); |
| 265 | + } |
| 266 | + |
| 267 | + private static void deleteAutoFollowPattern(RestClient client, String patternName) throws IOException { |
| 268 | + Request request = new Request("DELETE", "/_ccr/auto_follow/" + patternName); |
| 269 | + assertOK(client.performRequest(request)); |
| 270 | + } |
| 271 | + |
| 272 | + private int getNumberOfSuccessfulFollowedIndices() throws IOException { |
| 273 | + Request statsRequest = new Request("GET", "/_ccr/stats"); |
| 274 | + Map<?, ?> response = toMap(client().performRequest(statsRequest)); |
| 275 | + Integer actualSuccessfulFollowedIndices = ObjectPath.eval("auto_follow_stats.number_of_successful_follow_indices", response); |
| 276 | + if (actualSuccessfulFollowedIndices != null) { |
| 277 | + return actualSuccessfulFollowedIndices; |
| 278 | + } else { |
| 279 | + return -1; |
| 280 | + } |
| 281 | + } |
| 282 | + |
137 | 283 | private static void index(RestClient client, String index, int numDocs) throws IOException {
|
138 | 284 | for (int i = 0; i < numDocs; i++) {
|
139 | 285 | final Request request = new Request("POST", "/" + index + "/_doc/");
|
@@ -162,4 +308,10 @@ private static void verifyTotalHitCount(final String index,
|
162 | 308 | assertThat(totalHits, equalTo(expectedTotalHits));
|
163 | 309 | }
|
164 | 310 |
|
| 311 | + private static void stopIndexFollowing(RestClient client, String followerIndex) throws IOException { |
| 312 | + assertOK(client.performRequest(new Request("POST", "/" + followerIndex + "/_ccr/pause_follow"))); |
| 313 | + assertOK(client.performRequest(new Request("POST", "/" + followerIndex + "/_close"))); |
| 314 | + assertOK(client.performRequest(new Request("POST", "/" + followerIndex + "/_ccr/unfollow"))); |
| 315 | + } |
| 316 | + |
165 | 317 | }
|
0 commit comments