summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/balancer/cluster_chunks_resize_policy_test.cpp
blob: 563178f83a7dcb866959d21c06af2a36bc9c4ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
 *    Copyright (C) 2022-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/db/dbdirectclient.h"
#include "mongo/db/s/balancer/cluster_chunks_resize_policy_impl.h"
#include "mongo/db/s/config/config_server_test_fixture.h"
namespace mongo {
namespace {

class ClusterChunksResizePolicyTest : public ConfigServerTestFixture {
protected:
    const NamespaceString kNss{"testDb.testColl"};
    const UUID kUuid = UUID::gen();
    const ChunkVersion kCollectionVersion = ChunkVersion(1, 1, OID::gen(), Timestamp(10));

    const ShardId kShardId0 = ShardId("shard0");
    const ShardId kShardId1 = ShardId("shard1");
    const HostAndPort kShardHost0 = HostAndPort("TestHost0", 12345);
    const HostAndPort kShardHost1 = HostAndPort("TestHost1", 12346);
    const std::vector<ShardType> kShardList{
        ShardType(kShardId0.toString(), kShardHost0.toString()),
        ShardType(kShardId1.toString(), kShardHost1.toString())};

    const KeyPattern kShardKeyPattern = KeyPattern(BSON("x" << 1));
    const BSONObj kKeyAtMin = BSONObjBuilder().appendMinKey("x").obj();
    const BSONObj kKeyAtZero = BSON("x" << 0);
    const BSONObj kKeyAtTen = BSON("x" << 10);
    const BSONObj kKeyAtTwenty = BSON("x" << 20);
    const BSONObj kKeyAtThirty = BSON("x" << 30);
    const BSONObj kKeyAtForty = BSON("x" << 40);
    const BSONObj kKeyAtMax = BSONObjBuilder().appendMaxKey("x").obj();

    const int64_t kDefaultMaxChunksSizeBytes = 1024;

    OperationContext* _opCtx;

    ClusterChunksResizePolicyTest() : _clusterChunksResizePolicy([] {}) {}

    void setUp() override {
        ConfigServerTestFixture::setUp();
        _opCtx = operationContext();
        setupShards(kShardList);
    }

    ClusterChunksResizePolicyImpl _clusterChunksResizePolicy;

    CollectionType buildShardedCollection(
        const NamespaceString nss,
        const KeyPattern& shardKeyPattern,
        const std::vector<ChunkType>& chunkList,
        bool markAsAlreadyProcessed = false,
        boost::optional<int64_t> maxChunkSizeBytes = boost::none) {
        setupCollection(nss, shardKeyPattern, chunkList);
        if (markAsAlreadyProcessed || maxChunkSizeBytes.is_initialized()) {
            BSONObjBuilder updateQueryBuilder;
            BSONObjBuilder setObj(updateQueryBuilder.subobjStart("$set"));
            if (markAsAlreadyProcessed) {
                setObj.append(CollectionType::kChunksAlreadySplitForDowngradeFieldName, true);
            }
            if (maxChunkSizeBytes.is_initialized()) {
                setObj.append(CollectionType::kMaxChunkSizeBytesFieldName, *maxChunkSizeBytes);
            }
            setObj.done();
            ASSERT_OK(updateToConfigCollection(_opCtx,
                                               CollectionType::ConfigNS,
                                               BSON(CollectionType::kNssFieldName << nss.ns()),
                                               updateQueryBuilder.obj(),
                                               false));
        }

        return Grid::get(_opCtx)->catalogClient()->getCollection(_opCtx, nss);
    }
};

TEST_F(ClusterChunksResizePolicyTest, ThePolicyCanBeDestroyedWhileActive) {
    ASSERT_FALSE(_clusterChunksResizePolicy.isActive());
    auto _ = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    ASSERT_TRUE(_clusterChunksResizePolicy.isActive());
    // as _clusterChunksResizePolicy goes out of scope, no exception is expected to be raised.
}

TEST_F(ClusterChunksResizePolicyTest, ThePolicyCanBeStopped) {
    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    ASSERT_TRUE(_clusterChunksResizePolicy.isActive());
    ASSERT_FALSE(completionFuture.isReady());

    _clusterChunksResizePolicy.stop();

    ASSERT_FALSE(_clusterChunksResizePolicy.isActive());
    ASSERT_TRUE(completionFuture.isReady());
}

TEST_F(ClusterChunksResizePolicyTest, ResizeAClusterWithNoChunksEndsImmediately) {
    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    ASSERT_TRUE(_clusterChunksResizePolicy.isActive());
    ASSERT_FALSE(completionFuture.isReady());

    // The policy requires at least one call to getNextStreamingAction() for its state to be
    // evaluated/updated.
    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);

    ASSERT_FALSE(nextAction.is_initialized());
    ASSERT_TRUE(completionFuture.isReady());
    ASSERT_FALSE(_clusterChunksResizePolicy.isActive());
}

TEST_F(ClusterChunksResizePolicyTest,
       ThePolicyFirstGeneratesASplitVectorActionForAnUnprocessedChunk) {
    auto collectionChunk =
        ChunkType(kUuid, ChunkRange(kKeyAtMin, kKeyAtMax), kCollectionVersion, kShardId0);
    std::vector<ChunkType> collectionChunks{collectionChunk};
    auto coll = buildShardedCollection(kNss, kShardKeyPattern, collectionChunks);

    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    ASSERT_TRUE(_clusterChunksResizePolicy.isActive());
    ASSERT_FALSE(completionFuture.isReady());

    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    ASSERT_TRUE(_clusterChunksResizePolicy.isActive());
    ASSERT_FALSE(completionFuture.isReady());

    // the action holds the expected type and content
    auto autoSplitVectorAction = stdx::get<AutoSplitVectorInfo>(*nextAction);
    ASSERT_BSONOBJ_EQ(autoSplitVectorAction.keyPattern, kShardKeyPattern.toBSON());
    ASSERT_BSONOBJ_EQ(autoSplitVectorAction.minKey, collectionChunk.getMin());
    ASSERT_BSONOBJ_EQ(autoSplitVectorAction.maxKey, collectionChunk.getMax());
    ASSERT_EQ(autoSplitVectorAction.nss, kNss);
    ASSERT_EQ(autoSplitVectorAction.shardId, kShardId0);
    ASSERT_EQ(autoSplitVectorAction.uuid, kUuid);
    ASSERT_EQ(autoSplitVectorAction.collectionVersion, kCollectionVersion);
    ASSERT_EQ(autoSplitVectorAction.maxChunkSizeBytes, kDefaultMaxChunksSizeBytes);
}

TEST_F(ClusterChunksResizePolicyTest,
       ThePolicyGeneratesASplitChunkActionAfterReceivingACompleteSplitVector) {
    auto collectionChunk =
        ChunkType(kUuid, ChunkRange(kKeyAtMin, kKeyAtMax), kCollectionVersion, kShardId0);
    std::vector<ChunkType> collectionChunks{collectionChunk};
    auto coll = buildShardedCollection(kNss, kShardKeyPattern, collectionChunks);

    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    auto autoSplitVectorAction = stdx::get<AutoSplitVectorInfo>(*nextAction);

    std::vector<BSONObj> splitPoints{kKeyAtZero};
    DefragmentationActionResponse splitVectorResult = AutoSplitVectorResponse(splitPoints);

    _clusterChunksResizePolicy.applyActionResult(_opCtx, *nextAction, splitVectorResult);

    nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);

    // the action holds the expected type and content
    auto splitChunkAction = stdx::get<SplitInfoWithKeyPattern>(*nextAction);
    ASSERT_BSONOBJ_EQ(splitChunkAction.keyPattern, kShardKeyPattern.toBSON());
    ASSERT_EQ(splitChunkAction.uuid, kUuid);
    ASSERT_BSONOBJ_EQ(splitChunkAction.info.minKey, collectionChunk.getMin());
    ASSERT_BSONOBJ_EQ(splitChunkAction.info.maxKey, collectionChunk.getMax());
    ASSERT_EQ(splitChunkAction.info.nss, kNss);
    ASSERT_EQ(splitChunkAction.info.shardId, kShardId0);
    ASSERT_EQ(splitChunkAction.info.collectionVersion, kCollectionVersion);
    ASSERT_EQ(splitChunkAction.info.splitKeys.size(), 1);
    ASSERT_BSONOBJ_EQ(splitChunkAction.info.splitKeys.front(), kKeyAtZero);
}

TEST_F(ClusterChunksResizePolicyTest,
       ThePolicyGeneratesASplitVectorActionAfterReceivingAnIncompleteSplitVector) {
    auto collectionChunk =
        ChunkType(kUuid, ChunkRange(kKeyAtMin, kKeyAtMax), kCollectionVersion, kShardId0);
    std::vector<ChunkType> collectionChunks{collectionChunk};
    auto coll = buildShardedCollection(kNss, kShardKeyPattern, collectionChunks);

    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    auto autoSplitVectorAction = stdx::get<AutoSplitVectorInfo>(*nextAction);

    std::vector<BSONObj> splitPoints{kKeyAtZero};
    AutoSplitVectorResponse splitVectorResult(splitPoints);
    splitVectorResult.setContinuation(true);
    _clusterChunksResizePolicy.applyActionResult(_opCtx, *nextAction, splitVectorResult);

    nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);

    // the action holds the expected type and content
    autoSplitVectorAction = stdx::get<AutoSplitVectorInfo>(*nextAction);
    ASSERT_BSONOBJ_EQ(autoSplitVectorAction.keyPattern, kShardKeyPattern.toBSON());
    ASSERT_BSONOBJ_EQ(autoSplitVectorAction.minKey, splitPoints.front());
    ASSERT_BSONOBJ_EQ(autoSplitVectorAction.maxKey, collectionChunk.getMax());
    ASSERT_EQ(autoSplitVectorAction.nss, kNss);
    ASSERT_EQ(autoSplitVectorAction.shardId, kShardId0);
    ASSERT_EQ(autoSplitVectorAction.uuid, kUuid);
    ASSERT_EQ(autoSplitVectorAction.collectionVersion, kCollectionVersion);
    ASSERT_EQ(autoSplitVectorAction.maxChunkSizeBytes, kDefaultMaxChunksSizeBytes);
}

TEST_F(ClusterChunksResizePolicyTest, ThePolicyGeneratesNoActionAfterReceivingAnEmptySplitVector) {
    auto collectionChunk =
        ChunkType(kUuid, ChunkRange(kKeyAtMin, kKeyAtMax), kCollectionVersion, kShardId0);
    std::vector<ChunkType> collectionChunks{collectionChunk};
    auto coll = buildShardedCollection(kNss, kShardKeyPattern, collectionChunks);

    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    auto autoSplitVectorAction = stdx::get<AutoSplitVectorInfo>(*nextAction);

    std::vector<BSONObj> splitPoints{};
    AutoSplitVectorResponse splitVectorResult(splitPoints);
    _clusterChunksResizePolicy.applyActionResult(_opCtx, *nextAction, splitVectorResult);

    nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);

    ASSERT_FALSE(nextAction.is_initialized());
    // The process of the chunk is completed; being the only entry in config.chunks, the process of
    // the whole cluster should also be complete
    ASSERT_TRUE(completionFuture.isReady());
    ASSERT_FALSE(_clusterChunksResizePolicy.isActive());
}

TEST_F(ClusterChunksResizePolicyTest,
       ThePolicyReissuesSplitVectorWhenAcknowledgedWithARetriableError) {
    auto collectionChunk =
        ChunkType(kUuid, ChunkRange(kKeyAtMin, kKeyAtMax), kCollectionVersion, kShardId0);
    std::vector<ChunkType> collectionChunks{collectionChunk};
    auto coll = buildShardedCollection(kNss, kShardKeyPattern, collectionChunks);

    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    auto originalAction = stdx::get<AutoSplitVectorInfo>(*nextAction);
    _clusterChunksResizePolicy.applyActionResult(
        _opCtx,
        *nextAction,
        StatusWith<AutoSplitVectorResponse>(ErrorCodes::NetworkTimeout, "Testing error response"));

    nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);

    // The original and replayed actions are expected to match in type and content.
    auto reissuedAction = stdx::get<AutoSplitVectorInfo>(*nextAction);
    ASSERT_BSONOBJ_EQ(originalAction.keyPattern, reissuedAction.keyPattern);
    ASSERT_BSONOBJ_EQ(originalAction.minKey, reissuedAction.minKey);
    ASSERT_BSONOBJ_EQ(originalAction.maxKey, reissuedAction.maxKey);
    ASSERT_EQ(originalAction.nss, reissuedAction.nss);
    ASSERT_EQ(originalAction.shardId, reissuedAction.shardId);
    ASSERT_EQ(originalAction.uuid, reissuedAction.uuid);
    ASSERT_EQ(originalAction.collectionVersion, reissuedAction.collectionVersion);
    ASSERT_EQ(originalAction.maxChunkSizeBytes, reissuedAction.maxChunkSizeBytes);
}

TEST_F(ClusterChunksResizePolicyTest,
       ThePolicyReissuesSplitChunkWhenAcknowledgedWithARetriableError) {
    auto collectionChunk =
        ChunkType(kUuid, ChunkRange(kKeyAtMin, kKeyAtMax), kCollectionVersion, kShardId0);
    std::vector<ChunkType> collectionChunks{collectionChunk};
    auto coll = buildShardedCollection(kNss, kShardKeyPattern, collectionChunks);

    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);

    std::vector<BSONObj> splitPoints{kKeyAtZero};
    DefragmentationActionResponse splitVectorResult = AutoSplitVectorResponse(splitPoints);

    _clusterChunksResizePolicy.applyActionResult(_opCtx, *nextAction, splitVectorResult);

    nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    auto originalSplitAction = stdx::get<SplitInfoWithKeyPattern>(*nextAction);
    _clusterChunksResizePolicy.applyActionResult(
        _opCtx, *nextAction, Status(ErrorCodes::NetworkTimeout, "Testing error response"));

    nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);

    // The original and replayed actions are expected to match in type and content.
    auto reissuedSplitAction = stdx::get<SplitInfoWithKeyPattern>(*nextAction);
    ASSERT_BSONOBJ_EQ(originalSplitAction.keyPattern, reissuedSplitAction.keyPattern);
    ASSERT_EQ(originalSplitAction.uuid, reissuedSplitAction.uuid);
    ASSERT_BSONOBJ_EQ(originalSplitAction.info.minKey, reissuedSplitAction.info.minKey);
    ASSERT_BSONOBJ_EQ(originalSplitAction.info.maxKey, reissuedSplitAction.info.maxKey);
    ASSERT_EQ(originalSplitAction.info.nss, reissuedSplitAction.info.nss);
    ASSERT_EQ(originalSplitAction.info.shardId, reissuedSplitAction.info.shardId);
    ASSERT_EQ(originalSplitAction.info.collectionVersion,
              reissuedSplitAction.info.collectionVersion);
    ASSERT_EQ(originalSplitAction.info.splitKeys.size(), reissuedSplitAction.info.splitKeys.size());
    ASSERT_EQ(originalSplitAction.info.splitKeys.size(), 1);
    ASSERT_BSONOBJ_EQ(originalSplitAction.info.splitKeys.front(),
                      reissuedSplitAction.info.splitKeys.front());
}

TEST_F(ClusterChunksResizePolicyTest,
       ThePolicyRestartsTheCollectionProcessingWhenANonRetriableErrorIsReceived) {
    auto collectionChunk =
        ChunkType(kUuid, ChunkRange(kKeyAtMin, kKeyAtMax), kCollectionVersion, kShardId0);
    std::vector<ChunkType> collectionChunks{collectionChunk};
    auto coll = buildShardedCollection(kNss, kShardKeyPattern, collectionChunks);

    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    auto originalSplitVectorAction = stdx::get<AutoSplitVectorInfo>(*nextAction);

    std::vector<BSONObj> splitPoints{kKeyAtZero};
    DefragmentationActionResponse splitVectorResult = AutoSplitVectorResponse(splitPoints);

    _clusterChunksResizePolicy.applyActionResult(_opCtx, *nextAction, splitVectorResult);

    nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    _clusterChunksResizePolicy.applyActionResult(
        _opCtx, *nextAction, Status(ErrorCodes::OperationFailed, "Testing nonRetriable error"));

    nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    ASSERT_TRUE(nextAction.is_initialized());

    auto reissuedSplitVectorAction = stdx::get<AutoSplitVectorInfo>(*nextAction);
    ASSERT_BSONOBJ_EQ(originalSplitVectorAction.keyPattern, reissuedSplitVectorAction.keyPattern);
    ASSERT_BSONOBJ_EQ(originalSplitVectorAction.minKey, reissuedSplitVectorAction.minKey);
    ASSERT_BSONOBJ_EQ(originalSplitVectorAction.maxKey, reissuedSplitVectorAction.maxKey);
    ASSERT_EQ(originalSplitVectorAction.nss, reissuedSplitVectorAction.nss);
    ASSERT_EQ(originalSplitVectorAction.shardId, reissuedSplitVectorAction.shardId);
    ASSERT_EQ(originalSplitVectorAction.uuid, reissuedSplitVectorAction.uuid);
    ASSERT_EQ(originalSplitVectorAction.collectionVersion,
              reissuedSplitVectorAction.collectionVersion);
    ASSERT_EQ(originalSplitVectorAction.maxChunkSizeBytes,
              reissuedSplitVectorAction.maxChunkSizeBytes);
}

TEST_F(ClusterChunksResizePolicyTest, ThePolicyCompletesWhenAllActionsAreAcknowledged) {
    std::vector<ChunkType> collectionChunks{
        ChunkType(kUuid, ChunkRange(kKeyAtMin, kKeyAtTen), kCollectionVersion, kShardId0),
        ChunkType(kUuid, ChunkRange(kKeyAtTen, kKeyAtMax), kCollectionVersion, kShardId1)};
    auto coll = buildShardedCollection(kNss, kShardKeyPattern, collectionChunks);

    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    ASSERT_TRUE(_clusterChunksResizePolicy.isActive());
    ASSERT_FALSE(completionFuture.isReady());

    // Multiple SplitVector actions may be extracted in parallel
    auto splitVectorForChunk1 = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    ASSERT_TRUE(stdx::holds_alternative<AutoSplitVectorInfo>(*splitVectorForChunk1));
    auto splitVectorForChunk2 = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    ASSERT_TRUE(stdx::holds_alternative<AutoSplitVectorInfo>(*splitVectorForChunk2));

    // As long as they are incomplete, no progress may be made
    auto noAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    ASSERT_TRUE(_clusterChunksResizePolicy.isActive());
    ASSERT_FALSE(completionFuture.isReady());
    ASSERT_FALSE(noAction.is_initialized());

    // As splitVectors are acknowledged, splitChunk Actions are generated
    StatusWith<AutoSplitVectorResponse> splitVectorResult1 = AutoSplitVectorResponse({kKeyAtZero});
    _clusterChunksResizePolicy.applyActionResult(_opCtx, *splitVectorForChunk1, splitVectorResult1);
    auto splitChunkForChunk1 = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    ASSERT_TRUE(stdx::holds_alternative<SplitInfoWithKeyPattern>(*splitChunkForChunk1));

    StatusWith<AutoSplitVectorResponse> splitVectorResult2 = AutoSplitVectorResponse({kKeyAtForty});
    _clusterChunksResizePolicy.applyActionResult(_opCtx, *splitVectorForChunk2, splitVectorResult2);
    auto splitChunkForChunk2 = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    ASSERT_TRUE(stdx::holds_alternative<SplitInfoWithKeyPattern>(*splitChunkForChunk2));

    // Acknowledging the remaining actions (and pulling one more) completes the process
    ASSERT_TRUE(_clusterChunksResizePolicy.isActive());
    ASSERT_FALSE(completionFuture.isReady());
    DBDirectClient dbClient(_opCtx);
    const auto fullyProcessesCollectionsQuery =
        BSON(CollectionType::kChunksAlreadySplitForDowngradeFieldName << true);
    auto numFullyProcessedCollections =
        dbClient.count(CollectionType::ConfigNS, fullyProcessesCollectionsQuery);
    ASSERT_EQ(0, numFullyProcessedCollections);

    // After the acknowledges, the collection should be marked as fully processed on disk
    _clusterChunksResizePolicy.applyActionResult(_opCtx, *splitChunkForChunk1, Status::OK());
    _clusterChunksResizePolicy.applyActionResult(_opCtx, *splitChunkForChunk2, Status::OK());
    numFullyProcessedCollections =
        dbClient.count(CollectionType::ConfigNS, fullyProcessesCollectionsQuery);
    ASSERT_EQ(1, numFullyProcessedCollections);

    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);
    ASSERT_FALSE(nextAction.is_initialized());
    ASSERT_FALSE(_clusterChunksResizePolicy.isActive());
    ASSERT_TRUE(completionFuture.isReady());

    // After the completion of the whole process, the temporary mark should have been removed from
    // disk
    numFullyProcessedCollections =
        dbClient.count(CollectionType::ConfigNS, fullyProcessesCollectionsQuery);
    ASSERT_EQ(0, numFullyProcessedCollections);
}

TEST_F(ClusterChunksResizePolicyTest, CollectionsMarkedAsAlreadyProcessedGetIgnored) {
    std::vector<ChunkType> collectionChunks{
        ChunkType(kUuid, ChunkRange(kKeyAtMin, kKeyAtTen), kCollectionVersion, kShardId0),
        ChunkType(kUuid, ChunkRange(kKeyAtTen, kKeyAtMax), kCollectionVersion, kShardId1)};
    buildShardedCollection(kNss, kShardKeyPattern, collectionChunks, true);

    auto completionFuture = _clusterChunksResizePolicy.activate(_opCtx, kDefaultMaxChunksSizeBytes);
    ASSERT_TRUE(_clusterChunksResizePolicy.isActive());
    ASSERT_FALSE(completionFuture.isReady());
    auto nextAction = _clusterChunksResizePolicy.getNextStreamingAction(_opCtx);

    ASSERT_FALSE(nextAction.is_initialized());
    ASSERT_TRUE(completionFuture.isReady());
    ASSERT_FALSE(_clusterChunksResizePolicy.isActive());
}

}  // namespace
}  // namespace mongo