|
15 | 15 | import org.elasticsearch.common.settings.SecureString;
|
16 | 16 | import org.elasticsearch.common.settings.Settings;
|
17 | 17 | import org.elasticsearch.common.util.concurrent.ThreadContext;
|
| 18 | +import org.elasticsearch.index.IndexSettings; |
| 19 | +import org.elasticsearch.index.mapper.DateFieldMapper; |
18 | 20 | import org.elasticsearch.repositories.fs.FsRepository;
|
19 | 21 | import org.elasticsearch.rest.RestStatus;
|
20 | 22 |
|
|
23 | 25 | import java.util.Map;
|
24 | 26 | import java.util.concurrent.TimeUnit;
|
25 | 27 |
|
| 28 | +import static io.github.nik9000.mapmatcher.MapMatcher.assertMap; |
| 29 | +import static io.github.nik9000.mapmatcher.MapMatcher.matchesMap; |
26 | 30 | import static org.hamcrest.Matchers.containsString;
|
27 | 31 | import static org.hamcrest.Matchers.emptyOrNullString;
|
28 | 32 | import static org.hamcrest.Matchers.equalTo;
|
@@ -235,6 +239,140 @@ public void testFollowSearchableSnapshotsFails() throws Exception {
|
235 | 239 | }
|
236 | 240 | }
|
237 | 241 |
|
| 242 | + public void testFollowTsdbIndex() throws Exception { |
| 243 | + final int numDocs = 128; |
| 244 | + final String leaderIndexName = "tsdb_leader"; |
| 245 | + long basetime = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis("2021-01-01T00:00:00Z"); |
| 246 | + if ("leader".equals(targetCluster)) { |
| 247 | + logger.info("Running against leader cluster"); |
| 248 | + createIndex( |
| 249 | + leaderIndexName, |
| 250 | + Settings.builder().put(IndexSettings.MODE.getKey(), "time_series").build(), |
| 251 | + "\"properties\": {\"@timestamp\": {\"type\": \"date\"}, \"dim\": {\"type\": \"keyword\", \"time_series_dimension\": true}}" |
| 252 | + ); |
| 253 | + for (int i = 0; i < numDocs; i++) { |
| 254 | + logger.info("Indexing doc [{}]", i); |
| 255 | + index( |
| 256 | + client(), |
| 257 | + leaderIndexName, |
| 258 | + Integer.toString(i), |
| 259 | + "@timestamp", |
| 260 | + basetime + TimeUnit.SECONDS.toMillis(i * 10), |
| 261 | + "dim", |
| 262 | + "foobar" |
| 263 | + ); |
| 264 | + } |
| 265 | + refresh(leaderIndexName); |
| 266 | + verifyDocuments(client(), leaderIndexName, numDocs); |
| 267 | + } else if ("follow".equals(targetCluster)) { |
| 268 | + logger.info("Running against follow cluster"); |
| 269 | + final String followIndexName = "tsdb_follower"; |
| 270 | + final boolean overrideNumberOfReplicas = randomBoolean(); |
| 271 | + if (overrideNumberOfReplicas) { |
| 272 | + followIndex( |
| 273 | + client(), |
| 274 | + "leader_cluster", |
| 275 | + leaderIndexName, |
| 276 | + followIndexName, |
| 277 | + Settings.builder().put("index.number_of_replicas", 0).build() |
| 278 | + ); |
| 279 | + } else { |
| 280 | + followIndex(leaderIndexName, followIndexName); |
| 281 | + } |
| 282 | + assertBusy(() -> { |
| 283 | + verifyDocuments(client(), followIndexName, numDocs); |
| 284 | + if (overrideNumberOfReplicas) { |
| 285 | + assertMap( |
| 286 | + getIndexSettingsAsMap(followIndexName), |
| 287 | + matchesMap().extraOk().entry("index.mode", "time_series").entry("index.number_of_replicas", "0") |
| 288 | + ); |
| 289 | + } else { |
| 290 | + assertMap( |
| 291 | + getIndexSettingsAsMap(followIndexName), |
| 292 | + matchesMap().extraOk().entry("index.mode", "time_series").entry("index.number_of_replicas", "1") |
| 293 | + ); |
| 294 | + } |
| 295 | + }); |
| 296 | + // unfollow and then follow and then index a few docs in leader index: |
| 297 | + pauseFollow(followIndexName); |
| 298 | + resumeFollow(followIndexName); |
| 299 | + try (RestClient leaderClient = buildLeaderClient()) { |
| 300 | + int id = numDocs; |
| 301 | + index( |
| 302 | + leaderClient, |
| 303 | + leaderIndexName, |
| 304 | + Integer.toString(id), |
| 305 | + "@timestamp", |
| 306 | + basetime + TimeUnit.SECONDS.toMillis(id * 10), |
| 307 | + "dim", |
| 308 | + "foobar" |
| 309 | + ); |
| 310 | + index( |
| 311 | + leaderClient, |
| 312 | + leaderIndexName, |
| 313 | + Integer.toString(id + 1), |
| 314 | + "@timestamp", |
| 315 | + basetime + TimeUnit.SECONDS.toMillis(id * 10 + 10), |
| 316 | + "dim", |
| 317 | + "foobar" |
| 318 | + ); |
| 319 | + index( |
| 320 | + leaderClient, |
| 321 | + leaderIndexName, |
| 322 | + Integer.toString(id + 2), |
| 323 | + "@timestamp", |
| 324 | + basetime + TimeUnit.SECONDS.toMillis(id * 10 + 20), |
| 325 | + "dim", |
| 326 | + "foobar" |
| 327 | + ); |
| 328 | + } |
| 329 | + assertBusy(() -> verifyDocuments(client(), followIndexName, numDocs + 3)); |
| 330 | + assertBusy(() -> verifyCcrMonitoring(leaderIndexName, followIndexName), 30, TimeUnit.SECONDS); |
| 331 | + |
| 332 | + pauseFollow(followIndexName); |
| 333 | + closeIndex(followIndexName); |
| 334 | + assertOK(client().performRequest(new Request("POST", "/" + followIndexName + "/_ccr/unfollow"))); |
| 335 | + Exception e = expectThrows(ResponseException.class, () -> resumeFollow(followIndexName)); |
| 336 | + assertThat(e.getMessage(), containsString("follow index [" + followIndexName + "] does not have ccr metadata")); |
| 337 | + } |
| 338 | + } |
| 339 | + |
| 340 | + public void testFollowTsdbIndexCanNotOverrideMode() throws Exception { |
| 341 | + if (false == "follow".equals(targetCluster)) { |
| 342 | + return; |
| 343 | + } |
| 344 | + logger.info("Running against follow cluster"); |
| 345 | + Exception e = expectThrows(ResponseException.class, () -> followIndex( |
| 346 | + client(), |
| 347 | + "leader_cluster", |
| 348 | + "tsdb_leader", |
| 349 | + "tsdb_follower_bad", |
| 350 | + Settings.builder().put("index.mode", "standard").build() |
| 351 | + )); |
| 352 | + assertThat( |
| 353 | + e.getMessage(), |
| 354 | + containsString("can not put follower index that could override leader settings {\\\"index.mode\\\":\\\"standard\\\"}") |
| 355 | + ); |
| 356 | + } |
| 357 | + |
| 358 | + public void testFollowStandardIndexCanNotOverrideMode() throws Exception { |
| 359 | + if (false == "follow".equals(targetCluster)) { |
| 360 | + return; |
| 361 | + } |
| 362 | + logger.info("Running against follow cluster"); |
| 363 | + Exception e = expectThrows(ResponseException.class, () -> followIndex( |
| 364 | + client(), |
| 365 | + "leader_cluster", |
| 366 | + "test_index1", |
| 367 | + "tsdb_follower_bad", |
| 368 | + Settings.builder().put("index.mode", "time_series").build() |
| 369 | + )); |
| 370 | + assertThat( |
| 371 | + e.getMessage(), |
| 372 | + containsString("can not put follower index that could override leader settings {\\\"index.mode\\\":\\\"time_series\\\"}") |
| 373 | + ); |
| 374 | + } |
| 375 | + |
238 | 376 | @Override
|
239 | 377 | protected Settings restClientSettings() {
|
240 | 378 | String token = basicAuthHeaderValue("admin", new SecureString("admin-password".toCharArray()));
|
|
0 commit comments