|
25 | 25 | import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
|
26 | 26 | import org.elasticsearch.action.admin.indices.segments.IndexSegments;
|
27 | 27 | import org.elasticsearch.action.admin.indices.segments.IndexShardSegments;
|
28 |
| -import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse; |
29 | 28 | import org.elasticsearch.action.admin.indices.segments.IndicesSegmentsRequest;
|
30 | 29 | import org.elasticsearch.action.admin.indices.segments.ShardSegments;
|
31 | 30 | import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
|
32 | 31 | import org.elasticsearch.action.bulk.BulkItemResponse;
|
| 32 | +import org.elasticsearch.action.support.DefaultShardOperationFailedException; |
33 | 33 | import org.elasticsearch.client.internal.Client;
|
34 | 34 | import org.elasticsearch.client.internal.FilterClient;
|
35 | 35 | import org.elasticsearch.client.internal.OriginSettingClient;
|
@@ -572,48 +572,82 @@ private void refreshEnrichIndex(final String destinationIndexName, final int att
|
572 | 572 | protected void ensureSingleSegment(final String destinationIndexName, final int attempt) {
|
573 | 573 | enrichOriginClient().admin()
|
574 | 574 | .indices()
|
575 |
| - .segments(new IndicesSegmentsRequest(destinationIndexName), new DelegatingActionListener<>(listener) { |
576 |
| - @Override |
577 |
| - public void onResponse(IndicesSegmentResponse indicesSegmentResponse) { |
578 |
| - IndexSegments indexSegments = indicesSegmentResponse.getIndices().get(destinationIndexName); |
579 |
| - if (indexSegments == null) { |
| 575 | + .segments(new IndicesSegmentsRequest(destinationIndexName), listener.delegateFailureAndWrap((l, indicesSegmentResponse) -> { |
| 576 | + int failedShards = indicesSegmentResponse.getFailedShards(); |
| 577 | + if (failedShards > 0) { |
| 578 | + // Encountered a problem while querying the segments for the enrich index. Try and surface the problem in the log. |
| 579 | + logger.warn( |
| 580 | + "Policy [{}]: Encountered [{}] shard level failures while querying the segments for enrich index [{}]. " |
| 581 | + + "Turn on DEBUG logging for details.", |
| 582 | + policyName, |
| 583 | + failedShards, |
| 584 | + enrichIndexName |
| 585 | + ); |
| 586 | + if (logger.isDebugEnabled()) { |
| 587 | + DefaultShardOperationFailedException[] shardFailures = indicesSegmentResponse.getShardFailures(); |
| 588 | + int failureNumber = 1; |
| 589 | + String logPrefix = "Policy [" + policyName + "]: Encountered shard failure ["; |
| 590 | + String logSuffix = " of " |
| 591 | + + shardFailures.length |
| 592 | + + "] while querying segments for enrich index [" |
| 593 | + + enrichIndexName |
| 594 | + + "]. Shard ["; |
| 595 | + for (DefaultShardOperationFailedException shardFailure : shardFailures) { |
| 596 | + logger.debug( |
| 597 | + logPrefix + failureNumber + logSuffix + shardFailure.index() + "][" + shardFailure.shardId() + "]", |
| 598 | + shardFailure.getCause() |
| 599 | + ); |
| 600 | + failureNumber++; |
| 601 | + } |
| 602 | + } |
| 603 | + } |
| 604 | + IndexSegments indexSegments = indicesSegmentResponse.getIndices().get(destinationIndexName); |
| 605 | + if (indexSegments == null) { |
| 606 | + if (indicesSegmentResponse.getShardFailures().length == 0) { |
580 | 607 | throw new ElasticsearchException(
|
581 | 608 | "Could not locate segment information for newly created index [{}]",
|
582 | 609 | destinationIndexName
|
583 | 610 | );
|
| 611 | + } else { |
| 612 | + DefaultShardOperationFailedException shardFailure = indicesSegmentResponse.getShardFailures()[0]; |
| 613 | + throw new ElasticsearchException( |
| 614 | + "Could not obtain segment information for newly created index [{}]; shard info [{}][{}]", |
| 615 | + shardFailure.getCause(), |
| 616 | + destinationIndexName, |
| 617 | + shardFailure.index(), |
| 618 | + shardFailure.shardId() |
| 619 | + ); |
584 | 620 | }
|
585 |
| - Map<Integer, IndexShardSegments> indexShards = indexSegments.getShards(); |
586 |
| - assert indexShards.size() == 1 : "Expected enrich index to contain only one shard"; |
587 |
| - ShardSegments[] shardSegments = indexShards.get(0).shards(); |
588 |
| - assert shardSegments.length == 1 : "Expected enrich index to contain no replicas at this point"; |
589 |
| - ShardSegments primarySegments = shardSegments[0]; |
590 |
| - if (primarySegments.getSegments().size() > 1) { |
591 |
| - int nextAttempt = attempt + 1; |
592 |
| - if (nextAttempt > maxForceMergeAttempts) { |
593 |
| - delegate.onFailure( |
594 |
| - new ElasticsearchException( |
595 |
| - "Force merging index [{}] attempted [{}] times but did not result in one segment.", |
596 |
| - destinationIndexName, |
597 |
| - attempt, |
598 |
| - maxForceMergeAttempts |
599 |
| - ) |
600 |
| - ); |
601 |
| - } else { |
602 |
| - logger.debug( |
603 |
| - "Policy [{}]: Force merge result contains more than one segment [{}], retrying (attempt {}/{})", |
604 |
| - policyName, |
605 |
| - primarySegments.getSegments().size(), |
606 |
| - nextAttempt, |
607 |
| - maxForceMergeAttempts |
608 |
| - ); |
609 |
| - forceMergeEnrichIndex(destinationIndexName, nextAttempt); |
610 |
| - } |
| 621 | + } |
| 622 | + Map<Integer, IndexShardSegments> indexShards = indexSegments.getShards(); |
| 623 | + assert indexShards.size() == 1 : "Expected enrich index to contain only one shard"; |
| 624 | + ShardSegments[] shardSegments = indexShards.get(0).shards(); |
| 625 | + assert shardSegments.length == 1 : "Expected enrich index to contain no replicas at this point"; |
| 626 | + ShardSegments primarySegments = shardSegments[0]; |
| 627 | + if (primarySegments.getSegments().size() > 1) { |
| 628 | + int nextAttempt = attempt + 1; |
| 629 | + if (nextAttempt > maxForceMergeAttempts) { |
| 630 | + throw new ElasticsearchException( |
| 631 | + "Force merging index [{}] attempted [{}] times but did not result in one segment.", |
| 632 | + destinationIndexName, |
| 633 | + attempt, |
| 634 | + maxForceMergeAttempts |
| 635 | + ); |
611 | 636 | } else {
|
612 |
| - // Force merge down to one segment successful |
613 |
| - setIndexReadOnly(destinationIndexName); |
| 637 | + logger.debug( |
| 638 | + "Policy [{}]: Force merge result contains more than one segment [{}], retrying (attempt {}/{})", |
| 639 | + policyName, |
| 640 | + primarySegments.getSegments().size(), |
| 641 | + nextAttempt, |
| 642 | + maxForceMergeAttempts |
| 643 | + ); |
| 644 | + forceMergeEnrichIndex(destinationIndexName, nextAttempt); |
614 | 645 | }
|
| 646 | + } else { |
| 647 | + // Force merge down to one segment successful |
| 648 | + setIndexReadOnly(destinationIndexName); |
615 | 649 | }
|
616 |
| - }); |
| 650 | + })); |
617 | 651 | }
|
618 | 652 |
|
619 | 653 | private void setIndexReadOnly(final String destinationIndexName) {
|
|
0 commit comments