Skip to content

Commit

Permalink
Allow reindex with unlimited empty pages (#4663)
Browse files Browse the repository at this point in the history
* Removed limitation Gen2 reindex history groups

* fix fix infinite loop

* fix edge case
  • Loading branch information
mikaelweave authored Oct 10, 2024
1 parent e9a0162 commit df29074
Showing 1 changed file with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -926,16 +926,21 @@ protected async override Task<SearchResult> SearchForReindexInternalAsync(Search
}

var queryHints = searchOptions.QueryHints;
long startId = long.Parse(queryHints.First(_ => _.Param == KnownQueryParameterNames.StartSurrogateId).Value);
long endId = long.Parse(queryHints.First(_ => _.Param == KnownQueryParameterNames.EndSurrogateId).Value);
long globalStartId = long.Parse(queryHints.First(h => h.Param == KnownQueryParameterNames.StartSurrogateId).Value);
long globalEndId = long.Parse(queryHints.First(h => h.Param == KnownQueryParameterNames.EndSurrogateId).Value);
long queryStartId = globalStartId;

SearchResult results = null;
IReadOnlyList<(long StartId, long EndId)> ranges = await GetSurrogateIdRanges(resourceType, startId, endId, searchOptions.MaxItemCount, 50, true, cancellationToken);
IReadOnlyList<(long StartId, long EndId)> ranges;

if (ranges?.Count > 0)
do
{
// Get surrogate ID ranges
ranges = await GetSurrogateIdRanges(resourceType, queryStartId, globalEndId, searchOptions.MaxItemCount, 50, true, cancellationToken);

foreach (var range in ranges)
{
// Search within the surrogate ID range
results = await SearchBySurrogateIdRange(
resourceType,
range.StartId,
Expand All @@ -948,20 +953,24 @@ protected async override Task<SearchResult> SearchForReindexInternalAsync(Search
if (results.Results.Any())
{
results.MaxResourceSurrogateId = results.Results.Max(e => e.Resource.ResourceSurrogateId);
break;
_logger.LogInformation("For Reindex, Resource Type={ResourceType} Count={Count} MaxResourceSurrogateId={MaxResourceSurrogateId}", resourceType, results.TotalCount, results.MaxResourceSurrogateId);
return results;
}

_logger.LogInformation("For Reindex, empty data page encountered. Resource Type={ResourceType} StartId={StartId} EndId={EndId}", resourceType, range.StartId, range.EndId);
}

// If no resources are found in the group of surrogate id ranges, move forward the starting point.
if (ranges.Any())
{
queryStartId = ranges.Max(x => x.EndId) + 1;
}
}
else
{
_logger.LogInformation("For Reindex, no data pages found. Resource Type={ResourceType} StartId={StartId} EndId={EndId}", resourceType, startId, endId);
results = new SearchResult(0, []);
}
while (ranges.Any()); // Repeat until there are no more ranges to scan. Needed to advance through large contigous history.

_logger.LogInformation("For Reindex, Resource Type={ResourceType} Count={Count} MaxResourceSurrogateId={MaxResourceSurrogateId}", resourceType, results.TotalCount, results.MaxResourceSurrogateId);
return results;
// Return empty result when no resources are found in the given range provided by queryHints.
_logger.LogInformation("No surrogate ID ranges found containing data. Resource Type={ResourceType} StartId={StartId} EndId={EndId}", resourceType, globalStartId, globalEndId);
return new SearchResult(0, []);
}

/// <summary>
Expand Down

0 comments on commit df29074

Please sign in to comment.