| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using Common; |
| | 11 | | using Models; |
| | 12 | | using Rest.Azure; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Exception thrown when an indexing operation only partially succeeds. |
| | 16 | | /// </summary> |
| | 17 | | public class IndexBatchException : CloudException |
| | 18 | | { |
| | 19 | | private const string MessageFormat = |
| | 20 | | "{0} of {1} indexing actions in the batch failed. The remaining actions succeeded and modified the " + |
| | 21 | | "index. Check the IndexResponse property for the status of each index action."; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes a new instance of the IndexBatchException class. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="documentIndexResult">The deserialized response from the index request.</param> |
| 32 | 27 | | public IndexBatchException(DocumentIndexResult documentIndexResult) : base(CreateMessage(documentIndexResult)) |
| | 28 | | { |
| | 29 | | // Null check in CreateMessage(). |
| 32 | 30 | | IndexingResults = documentIndexResult.Results; |
| | 31 | |
|
| 32 | 32 | | Body = new CloudError() { Code = String.Empty, Message = Message }; |
| 32 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Gets the results for the index batch that contains the status for each individual index action. |
| | 37 | | /// </summary> |
| 66 | 38 | | public IList<IndexingResult> IndexingResults { get; } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Finds all index actions in the given batch that failed and need to be retried, and returns them in a |
| | 42 | | /// new batch. |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="originalBatch">The batch that partially failed indexing.</param> |
| | 45 | | /// <param name="keyFieldName">The name of the key field from the index schema.</param> |
| | 46 | | /// <returns> |
| | 47 | | /// A new batch containing all the actions from the given batch that failed and should be retried. |
| | 48 | | /// </returns> |
| | 49 | | public IndexBatch<Document> FindFailedActionsToRetry(IndexBatch<Document> originalBatch, string keyFieldName) |
| | 50 | | { |
| 48 | 51 | | string GetKey(Document doc) => doc[keyFieldName].ToString(); |
| 12 | 52 | | return FindFailedActionsToRetry(originalBatch, GetKey); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Finds all index actions in the given batch that failed and need to be retried, and returns them in a |
| | 57 | | /// new batch. |
| | 58 | | /// </summary> |
| | 59 | | /// <typeparam name="T"> |
| | 60 | | /// The CLR type that maps to the index schema. Instances of this type can be stored as documents in the index. |
| | 61 | | /// </typeparam> |
| | 62 | | /// <param name="originalBatch">The batch that partially failed indexing.</param> |
| | 63 | | /// <param name="keySelector">A lambda that retrieves a key value from a given document of type T.</param> |
| | 64 | | /// <returns> |
| | 65 | | /// A new batch containing all the actions from the given batch that failed and should be retried. |
| | 66 | | /// </returns> |
| | 67 | | public IndexBatch<T> FindFailedActionsToRetry<T>(IndexBatch<T> originalBatch, Func<T, string> keySelector) |
| | 68 | | { |
| 24 | 69 | | IEnumerable<IndexAction<T>> failedActions = DoFindFailedActionsToRetry(originalBatch, keySelector); |
| 24 | 70 | | return IndexBatch.New(failedActions); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | private IEnumerable<IndexAction<T>> DoFindFailedActionsToRetry<T>(IndexBatch<T> originalBatch, Func<T, string> k |
| | 74 | | { |
| 148 | 75 | | IEnumerable<string> allRetriableKeys = IndexingResults.Where(r => IsRetriableStatusCode(r.StatusCode)).Selec |
| | 76 | |
|
| 24 | 77 | | var uniqueRetriableKeys = new HashSet<string>(allRetriableKeys); |
| | 78 | |
|
| | 79 | | bool ShouldRetry(IndexAction<T> action) => |
| 96 | 80 | | action.Document != null && uniqueRetriableKeys.Contains(keySelector(action.Document)); |
| | 81 | |
|
| 24 | 82 | | return originalBatch.Actions.Where(ShouldRetry); |
| | 83 | | } |
| | 84 | |
|
| | 85 | | private static string CreateMessage(DocumentIndexResult documentIndexResult) |
| | 86 | | { |
| 32 | 87 | | Throw.IfArgumentNull(documentIndexResult, "documentIndexResult"); |
| | 88 | |
|
| 32 | 89 | | return String.Format( |
| 32 | 90 | | MessageFormat, |
| 160 | 91 | | documentIndexResult.Results.Count(r => !r.Succeeded), |
| 32 | 92 | | documentIndexResult.Results.Count); |
| | 93 | | } |
| | 94 | |
|
| | 95 | | private static bool IsRetriableStatusCode(int statusCode) |
| | 96 | | { |
| | 97 | | switch (statusCode) |
| | 98 | | { |
| | 99 | | case 200: |
| | 100 | | case 201: |
| 28 | 101 | | return false; // Don't retry on success. |
| | 102 | |
|
| | 103 | | case 404: |
| | 104 | | case 400: |
| 24 | 105 | | return false; // Don't retry on user error. |
| | 106 | |
|
| | 107 | | case 500: |
| 12 | 108 | | return false; // Don't retry when something unexpected happened. |
| | 109 | |
|
| | 110 | | case 422: |
| | 111 | | case 409: |
| | 112 | | case 503: |
| 28 | 113 | | return true; // The above cases might succeed on a subsequent retry. |
| | 114 | |
|
| | 115 | | default: |
| | 116 | | // If this happens, it's a bug. Safest to assume no retry. |
| 4 | 117 | | return false; |
| | 118 | | } |
| | 119 | | } |
| | 120 | | } |
| | 121 | | } |