diff --git a/cli/util/uploader.go b/cli/util/uploader.go index 70b44e92f8..c6f502ff4f 100644 --- a/cli/util/uploader.go +++ b/cli/util/uploader.go @@ -227,9 +227,9 @@ type searchResult struct { // starting from the currentHeight and going backwards. func fetchLatestMissingBlockIndex(ctx context.Context, p *pool.Pool, containerID cid.ID, priv *keys.PrivateKey, attributeKey string, currentHeight int) (int, error) { var ( - wg sync.WaitGroup - numBatches = currentHeight/searchBatchSize + 1 - emptyBatchFound bool + wg sync.WaitGroup + numBatches = currentHeight/searchBatchSize + 1 + lastFullBatchEnd = 0 ) for batch := numBatches; batch > -maxParallelSearches; batch -= maxParallelSearches { @@ -266,21 +266,17 @@ func fetchLatestMissingBlockIndex(ctx context.Context, p *pool.Pool, containerID }(i, startIndex, endIndex) } wg.Wait() - + //results is sorted so we can iterate from the end to find the last full batch. for i := len(results) - 1; i >= 0; i-- { if results[i].err != nil { return 0, fmt.Errorf("search of index files failed for batch with indexes from %d to %d: %w", results[i].startIndex, results[i].endIndex-1, results[i].err) } - if results[i].numOIDs < searchBatchSize { - emptyBatchFound = true - continue - } - if emptyBatchFound || (batch == numBatches && i == len(results)-1) { - return results[i].startIndex + searchBatchSize, nil + if results[i].numOIDs == searchBatchSize { + lastFullBatchEnd = max(lastFullBatchEnd, results[i].endIndex) } } } - return 0, nil + return lastFullBatchEnd, nil } // updateIndexFiles updates the index files in the container.