aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/statecheckers.cpp
blob: 97641ae86a6be1c9ca7912e78654583a0366d082 (plain) (blame)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "statecheckers.h"
#include "activecopy.h"
#include "node_supported_features_repo.h"
#include <vespa/document/bucket/fixed_bucket_spaces.h>
#include <vespa/storage/distributor/operations/idealstate/splitoperation.h>
#include <vespa/storage/distributor/operations/idealstate/joinoperation.h>
#include <vespa/storage/distributor/operations/idealstate/removebucketoperation.h>
#include <vespa/storage/distributor/operations/idealstate/setbucketstateoperation.h>
#include <vespa/storage/distributor/operations/idealstate/mergeoperation.h>
#include <vespa/storage/distributor/operations/idealstate/garbagecollectionoperation.h>
#include <vespa/storageframework/generic/clock/clock.h>
#include <vespa/vdslib/distribution/distribution.h>
#include <vespa/vdslib/state/clusterstate.h>
#include <vespa/vespalib/stllike/asciistream.h>

#include <vespa/log/log.h>
LOG_SETUP(".distributor.operation.checkers");

using document::BucketSpace;

namespace storage::distributor {

bool
SplitBucketStateChecker::validForSplit(const Context& c)
{
    // Can't split if we have no nodes.
    if (c.entry()->getNodeCount() == 0) {
        LOG(spam, "Can't split bucket %s, since it has no copies", c.bucket.toString().c_str());
        return false;
    }

    // Can't split anymore if we already used 58 bits.
    if (c.getBucketId().getUsedBits() >= 58) {
        return false;
    }

    return true;
}

double
SplitBucketStateChecker::getBucketSizeRelativeToMax(const Context& c)
{
    auto highest = c.entry().getBucketInfo().getHighest();

    if (highest._documentCount < 2) {
        return 0;
    }

    double byteSplitRatio = 0;
    if (c.distributorConfig.getSplitSize() > 0) {
        byteSplitRatio = static_cast<double>(highest._totalDocumentSize) / c.distributorConfig.getSplitSize();
    }

    double docSplitRatio = 0;
    if (c.distributorConfig.getSplitCount() > 0) {
        docSplitRatio = static_cast<double>(highest._documentCount) / c.distributorConfig.getSplitCount();
    }

    double fileSizeRatio = 0;
    if (c.distributorConfig.getSplitSize() > 0) {
        fileSizeRatio = static_cast<double>(highest._usedFileSize) / (2 * c.distributorConfig.getSplitSize());
    }

    double metaSplitRatio = 0;
    if (c.distributorConfig.getSplitCount() > 0) {
        metaSplitRatio = static_cast<double>(highest._metaCount) / (2 * c.distributorConfig.getSplitCount());
    }

    return std::max(std::max(byteSplitRatio, docSplitRatio),
                    std::max(fileSizeRatio, metaSplitRatio));
}

StateChecker::Result
SplitBucketStateChecker::generateMinimumBucketSplitOperation(const Context& c)
{
    auto so = std::make_unique<SplitOperation>(c.node_ctx, BucketAndNodes(c.getBucket(), c.entry()->getNodes()),
                                               c.distributorConfig.getMinimalBucketSplit(), 0, 0);

    so->setPriority(c.distributorConfig.getMaintenancePriorities().splitDistributionBits);
    so->setDetailedReason("[Splitting bucket because the current system size requires a higher minimum split bit]");
    return Result::createStoredResult(std::move(so), MaintenancePriority::MEDIUM);
}

StateChecker::Result
SplitBucketStateChecker::generateMaxSizeExceededSplitOperation(const Context& c)
{
    auto so = std::make_unique<SplitOperation>(c.node_ctx, BucketAndNodes(c.getBucket(), c.entry()->getNodes()), 58,
                                               c.distributorConfig.getSplitCount(), c.distributorConfig.getSplitSize());

    so->setPriority(c.distributorConfig.getMaintenancePriorities().splitLargeBucket);

    auto highest = c.entry().getBucketInfo().getHighest();
    vespalib::asciistream ost;
    ost << "[Splitting bucket because its maximum size ("
        << highest._totalDocumentSize << " b, "
        << highest._documentCount << " docs, "
        << highest._metaCount << " meta, "
        << highest._usedFileSize << " b total"
        << ") is higher than the configured limit of ("
        << c.distributorConfig.getSplitSize()
        << ", " << c.distributorConfig.getSplitCount() << ")]";

    so->setDetailedReason(ost.str());
    return Result::createStoredResult(std::move(so), MaintenancePriority::HIGH);

}

StateChecker::Result
SplitBucketStateChecker::check(const Context &c) const {
    if (!validForSplit(c)) {
        return StateChecker::Result::noMaintenanceNeeded();
    }

    double splitRatio(getBucketSizeRelativeToMax(c));
    if (splitRatio > 1.0) {
        return generateMaxSizeExceededSplitOperation(c);
    }

    // Always split it if it has less used bits than the minimum.
    if (c.getBucketId().getUsedBits() < c.distributorConfig.getMinimalBucketSplit()) {
        return generateMinimumBucketSplitOperation(c);
    }
    return Result::noMaintenanceNeeded();
}

bool
JoinBucketsStateChecker::isFirstSibling(const document::BucketId& bucketId)
{
    return (bucketId.getId() & (1ULL << (bucketId.getUsedBits() - 1))) == 0;
}

namespace {

using ConstNodesRef = IdealServiceLayerNodesBundle::ConstNodesRef;
using Node2Index = IdealServiceLayerNodesBundle::Node2Index;

bool
equalNodeSet(const Node2Index & node2Index, ConstNodesRef idealState, const BucketDatabase::Entry& dbEntry)
{
    if (idealState.size() != dbEntry->getNodeCount()) {
        return false;
    }
    for (uint16_t i = 0; i < dbEntry->getNodeCount(); i++) {
        const BucketCopy & info = dbEntry->getNodeRef(i);
        if ( ! node2Index.lookup(info.getNode()).valid() ) {
            return false;
        }
    }
    return true;
}

bool
equalNodeSet(ConstNodesRef idealState, const BucketDatabase::Entry& dbEntry)
{
    if (idealState.size() != dbEntry->getNodeCount()) {
        return false;
    }
    // Note: no assumptions are made on the ordering of the elements in
    // either vector.
    for (uint16_t node : idealState) {
        if (!dbEntry->getNode(node)) {
            return false;
        }
    }
    return true;
}

bool
bucketAndSiblingReplicaLocationsEqualIdealState(const StateChecker::Context& context)
{
    if (!equalNodeSet(context.idealStateBundle.nonretired_or_maintenance_to_index(), context.idealState(), context.entry())) {
        return false;
    }
    std::vector<uint16_t> siblingIdealState = context.distribution.getIdealStorageNodes(context.systemState, context.siblingBucket);
    if (!equalNodeSet(siblingIdealState, context.siblingEntry)) {
        return false;
    }
    return true;
}

bool
inconsistentJoinIsEnabled(const StateChecker::Context& context)
{
    return context.distributorConfig.getEnableInconsistentJoin();
}

bool
inconsistentJoinIsAllowed(const StateChecker::Context& context)
{
    return (inconsistentJoinIsEnabled(context)
            && bucketAndSiblingReplicaLocationsEqualIdealState(context));
}

bool
isInconsistentlySplit(const StateChecker::Context& c)
{
    return (c.entries.size() > 1);
}

// We don't want to invoke joins on buckets that have more replicas than
// required. This is in particular because joins cause ideal states to change
// for the target buckets and trigger merges. Since the removal of the non-
// ideal replicas is done by the DeleteBuckets state-checker, it will become
// preempted by potential follow-up joins unless we explicitly avoid these.
bool
contextBucketHasTooManyReplicas(const StateChecker::Context& c)
{
    return (c.entry()->getNodeCount() > c.distribution.getRedundancy());
}

bool
bucketAtDistributionBitLimit(const document::BucketId& bucket, const StateChecker::Context& c)
{
    return (bucket.getUsedBits() <= std::max(uint32_t(c.systemState.getDistributionBitCount()),
                                             c.distributorConfig.getMinimalBucketSplit()));
}

bool
legalBucketSplitLevel(const document::BucketId& bucket, const StateChecker::Context& c)
{
    return bucket.getUsedBits() >= c.distributorConfig.getMinimalBucketSplit();
}

bool
bucketHasMultipleChildren(const document::BucketId& bucket, const StateChecker::Context& c)
{
    return c.db.childCount(bucket) > 1;
}

} // anon ns

bool
JoinBucketsStateChecker::siblingsAreInSync(const Context& context)
{
    const auto& entry(context.entry());
    const auto& siblingEntry(context.siblingEntry);

    if (entry->getNodeCount() != siblingEntry->getNodeCount()) {
        LOG(spam, "Not joining bucket %s because sibling bucket %s had different node count",
            context.bucket.toString().c_str(), context.siblingBucket.toString().c_str());
        return false;
    }

    bool siblingsCoLocated = true;
    for (uint32_t i = 0; i < entry->getNodeCount(); ++i) {
        if (entry->getNodeRef(i).getNode() != siblingEntry->getNodeRef(i).getNode()) {
            siblingsCoLocated = false;
            break;
        }
    }

    if (!siblingsCoLocated && !inconsistentJoinIsAllowed(context)) {
        LOG(spam, "Not joining bucket %s because sibling bucket %s does not have the same node set, or inconsistent "
                  "joins cannot be performed either due to config or because replicas were not in their ideal location",
            context.bucket.toString().c_str(), context.siblingBucket.toString().c_str());
        return false;
    }

    if (!entry->validAndConsistent() || !siblingEntry->validAndConsistent()) {
        LOG(spam, "Not joining bucket %s because it or %s is out of sync and syncing it may cause it to become too large",
            context.bucket.toString().c_str(), context.siblingBucket.toString().c_str());
        return false;
    }

    return true;
}

bool
JoinBucketsStateChecker::singleBucketJoinIsConsistent(const Context& c)
{
    document::BucketId joinTarget(c.getBucketId().getUsedBits() - 1,
                                  c.getBucketId().getRawId());
    // If there are 2 children under the potential join target bucket, joining
    // would cause the bucket tree to become inconsistent. The reason for this
    // being that "moving" a bucket one bit up in the tree (and into
    // joinedBucket) would create a new parent bucket for the bucket(s)
    // already present in the other child tree, thus causing it to become
    // inconsistent. After all, we desire a bucket tree with only leaves
    // being actually present.
    return (c.db.childCount(joinTarget) == 1);
}

bool
JoinBucketsStateChecker::singleBucketJoinIsEnabled(const Context& c)
{
    return c.distributorConfig.getEnableJoinForSiblingLessBuckets();
}

bool
JoinBucketsStateChecker::shouldJoin(const Context& c)
{
    if (c.entry()->getNodeCount() == 0) {
        LOG(spam, "Not joining bucket %s because it has no nodes", c.bucket.toString().c_str());
        return false;
    }

    if (contextBucketHasTooManyReplicas(c)) {
        LOG(spam, "Not joining %s because it has too high replication level", c.bucket.toString().c_str());
        return false;
    }

    if (c.distributorConfig.getJoinSize() == 0 && c.distributorConfig.getJoinCount() == 0) {
        LOG(spam, "Not joining bucket %s because join is disabled", c.bucket.toString().c_str());
        return false;
    }

    if (bucketAtDistributionBitLimit(c.getBucketId(), c)) {
        LOG(spam, "Not joining bucket %s because it is below the min split count (config: %u, cluster state: %u, bucket has: %u)",
            c.bucket.toString().c_str(), c.distributorConfig.getMinimalBucketSplit(), c.systemState.getDistributionBitCount(), c.getBucketId().getUsedBits());
        return false;
    }

    if (c.entry()->hasRecentlyCreatedEmptyCopy()) {
        return false;
    }

    if (c.getSiblingEntry().valid()) {
        if (!isFirstSibling(c.getBucketId())) {
            LOG(spam, "Not joining bucket %s because it is the second sibling of %s and not the first",
                c.bucket.toString().c_str(), c.siblingBucket.toString().c_str());
            return false;
        }
        if (!siblingsAreInSync(c)) {
            return false;
        }
        return smallEnoughToJoin(c);
    }

    if (!singleBucketJoinIsEnabled(c)) {
        return false;
    }

    if (!smallEnoughToJoin(c)) {
        return false;
    }

    // No sibling and bucket has more bits than the minimum number of split
    // bits. If joining the bucket with itself into a bucket with 1 less
    // bit does _not_ introduce any inconsistencies in the bucket tree, do
    // so in order to gradually compact away sparse buckets.
    return singleBucketJoinIsConsistent(c);
}

/**
 * Compute sum(for each sibling(max(for each replica(used file size)))).
 * If sibling does not exist, treats its highest used file size as 0.
 */
uint64_t
JoinBucketsStateChecker::getTotalUsedFileSize(const Context& c)
{
    return (c.entry().getBucketInfo().getHighestUsedFileSize()
            + c.getSiblingEntry().getBucketInfo().getHighestUsedFileSize());
}

/**
 * Compute sum(for each sibling(max(for each replica(meta count)))).
 * If sibling does not exist, treats its highest meta count as 0.
 */
uint64_t
JoinBucketsStateChecker::getTotalMetaCount(const Context& c)
{
    return (c.entry().getBucketInfo().getHighestMetaCount()
            + c.getSiblingEntry().getBucketInfo().getHighestMetaCount());
}

bool
JoinBucketsStateChecker::smallEnoughToJoin(const Context& c)
{
    if (c.distributorConfig.getJoinSize() != 0) {
        if (getTotalUsedFileSize(c) >= c.distributorConfig.getJoinSize()) {
            return false;
        }
    }
    if (c.distributorConfig.getJoinCount() != 0) {
        if (getTotalMetaCount(c) >= c.distributorConfig.getJoinCount()) {
            return false;
        }
    }
    return true;
}

document::Bucket
JoinBucketsStateChecker::computeJoinBucket(const Context& c)
{
    // Always decrease by at least 1 bit, as we could not get here unless this
    // were a valid outcome.
    unsigned int level = c.getBucketId().getUsedBits() - 1;
    document::BucketId target(level, c.getBucketId().getRawId());

    // Push bucket up the tree as long as it gets no siblings. This means
    // joins involving 2 source buckets will currently only be decreased by 1
    // bit (mirroring the legacy behavior), but sparse (single) buckets may
    // be decreased by multiple bits. We may want to optimize joins for cases
    // with 2 source buckets in the future.
    while (true) {
        document::BucketId candidate(level, c.getBucketId().getRawId());
        if (bucketHasMultipleChildren(candidate, c)
            || !legalBucketSplitLevel(candidate, c))
        {
            break;
        }
        --level;
        target = candidate;
    }
    return {c.getBucket().getBucketSpace(), target};
}

StateChecker::Result
JoinBucketsStateChecker::check(const Context &c) const
{
    // At this point in time, bucket is consistently split as the state checker
    // would otherwise be pre-empted by the inconsistent state checker.
    if (!shouldJoin(c)) {
        return Result::noMaintenanceNeeded();
    }
    
    document::Bucket joinedBucket(computeJoinBucket(c));
    assert(joinedBucket.getBucketId().getUsedBits() < c.getBucketId().getUsedBits());

    std::vector<document::BucketId> sourceBuckets;
    if (c.getSiblingEntry().valid()) {
        sourceBuckets.push_back(c.siblingBucket);
    } else {
        sourceBuckets.push_back(c.getBucketId());
    }
    sourceBuckets.push_back(c.getBucketId());
    auto op = std::make_unique<JoinOperation>(c.node_ctx, BucketAndNodes(joinedBucket, c.entry()->getNodes()), sourceBuckets);
    op->setPriority(c.distributorConfig.getMaintenancePriorities().joinBuckets);
    vespalib::asciistream ost;
    ost << "[Joining buckets " << sourceBuckets[1].toString() << " and " << sourceBuckets[0].toString()
        << " because their size (" << getTotalUsedFileSize(c) << " bytes, "
        << getTotalMetaCount(c) << " docs) is less than the configured limit of ("
        << c.distributorConfig.getJoinSize() << ", " << c.distributorConfig.getJoinCount() << ")";

    op->setDetailedReason(ost.str());

    return Result::createStoredResult(std::move(op), MaintenancePriority::VERY_LOW);
}

bool
SplitInconsistentStateChecker::isLeastSplitBucket(const document::BucketId& bucket, const std::vector<BucketDatabase::Entry>& entries)
{
    // Figure out if any other buckets are less split than the current one.
    for (const auto & e : entries) {
        assert(e.valid());

        if (e.getBucketId().getUsedBits() < bucket.getUsedBits()) {
            return false;
        }
    }

    return true;
}

uint32_t
SplitInconsistentStateChecker::getHighestUsedBits(const std::vector<BucketDatabase::Entry>& entries)
{
    uint32_t highestUsedBits = 0;
    for (const auto & e : entries) {
        highestUsedBits = std::max(e.getBucketId().getUsedBits(), highestUsedBits);
    }
    return highestUsedBits;
}

vespalib::string
SplitInconsistentStateChecker::getReason(const document::BucketId& bucketId, const std::vector<BucketDatabase::Entry>& entries)
{
    vespalib::asciistream reason;
    reason << "[Bucket is inconsistently split (list includes " << vespalib::hex << "0x" << bucketId.getId();

    for (uint32_t i = 0, found = 0; i < entries.size() && found < 3; i++) {
        if (!(entries[i].getBucketId() == bucketId)) {
            reason << ", 0x" << vespalib::hex << entries[i].getBucketId().getId();
            ++found;
        }
    }

    if (entries.size() > 4) {
        reason << " and " << vespalib::dec << entries.size() - 4 << " others";
    }

    reason << ") Splitting it to improve the problem (max used bits " << vespalib::dec << getHighestUsedBits(entries) << ")]";

    return reason.str();
}

StateChecker::Result
SplitInconsistentStateChecker::check(const Context &c) const
{
    if (!isInconsistentlySplit(c)) {
        return Result::noMaintenanceNeeded();
    }

    if (!isLeastSplitBucket(c.getBucketId(), c.entries)) {
        return Result::noMaintenanceNeeded();
    }
    
    auto op = std::make_unique<SplitOperation>(c.node_ctx, BucketAndNodes(c.getBucket(), c.entry()->getNodes()),
                                               getHighestUsedBits(c.entries), 0, 0);

    op->setPriority(c.distributorConfig.getMaintenancePriorities().splitInconsistentBucket);
    op->setDetailedReason(getReason(c.getBucketId(), c.entries));
    return Result::createStoredResult(std::move(op), MaintenancePriority::HIGH);
}

namespace {

bool
containsMaintenanceNode(ConstNodesRef ideal, const StateChecker::Context& c)
{
    for (uint16_t n : ideal) {
        if (c.systemState.getNodeState(lib::Node(lib::NodeType::STORAGE, n)).getState() == lib::State::MAINTENANCE) {
            return true;
        }
    }
    return false;
}

bool
ideal_node_is_unavailable_in_pending_state(const StateChecker::Context& c) {
    if (!c.pending_cluster_state) {
        return false;
    }
    for (uint16_t n : c.idealState()) {
        if (!c.pending_cluster_state->getNodeState(lib::Node(lib::NodeType::STORAGE, n)).getState().oneOf("uir")){
            return true;
        }
    }
    return false;
}

bool
consistentApartFromEmptyBucketsInNonIdealLocationAndInvalidEntries(ConstNodesRef idealNodes, const BucketInfo& entry)
{
    api::BucketInfo info;
    for (uint32_t i=0, n=entry.getNodeCount(); i<n; ++i) {
        const BucketCopy& copy(entry.getNodeRef(i));
        bool onIdealNode = false;
        for (const auto & idealNode : idealNodes) {
            if (copy.getNode() == idealNode) {
                onIdealNode = true;
                break;
            }
        }
            // Ignore empty buckets on non-ideal nodes
        if (!onIdealNode && copy.empty()) {
            continue;
        }
            // Ignore invalid entries.
        if (!copy.valid()) {
            continue;
        }
        if (info.valid()) {
            if (info.getChecksum() != copy.getChecksum()) {
                return false;
            }
        } else {
            info = copy.getBucketInfo();
        }
    }
    return true;
}

class MergeNodes
{
public:
    MergeNodes() noexcept;
    explicit MergeNodes(const BucketDatabase::Entry& entry);
    MergeNodes(MergeNodes && rhs) noexcept = default;
    MergeNodes & operator =(MergeNodes && rhs) noexcept = delete;
    MergeNodes(const MergeNodes & rhs) = delete;
    MergeNodes & operator =(const MergeNodes & rhs) = delete;

    ~MergeNodes();

    bool shouldMerge() const noexcept {
        return _problemFlags != 0;
    }

    void operator+=(const MergeNodes& other);
    void markMoveToIdealLocation(uint16_t node, uint8_t msgPriority);
    void markOutOfSync(const StateChecker::Context& c, uint8_t msgPriority);
    void markMissingReplica(uint16_t node, uint8_t msgPriority);

    bool needsMoveOnly() const noexcept {
        return _problemFlags == NON_IDEAL_LOCATION;
    }

    void addNode(uint16_t node) {
        _nodes.push_back(node);
    }

    const std::vector<uint16_t>& nodes() const noexcept { return _nodes; }
    uint8_t priority() const noexcept { return _priority; }
    std::string reason() const { return _reason.str(); }

private:
    void updatePriority(uint8_t pri) noexcept {
        _priority = std::min(pri, _priority);
    }

    void addProblem(uint8_t newProblem) noexcept {
        _problemFlags |= newProblem;
    }

    enum Problem {
        OUT_OF_SYNC = 1,
        MISSING_REPLICA = 2,
        NON_IDEAL_LOCATION = 4
    };
    vespalib::asciistream _reason;
    std::vector<uint16_t> _nodes;
    uint8_t               _problemFlags;
    uint8_t               _priority;
};

MergeNodes::MergeNodes() noexcept
    : _reason(),
      _nodes(),
      _problemFlags(0),
      _priority(255)
{}

MergeNodes::MergeNodes(const BucketDatabase::Entry& entry)
    : _reason(),
      _nodes(),
      _problemFlags(0),
      _priority(255)
{
    _nodes.reserve(entry->getNodeCount());
    for (uint16_t i = 0; i < entry->getNodeCount(); i++) {
        addNode(entry->getNodeRef(i).getNode());
    }
}

MergeNodes::~MergeNodes() = default;


void
MergeNodes::operator+=(const MergeNodes& other) {
    _reason << other._reason.str();
    _problemFlags |= other._problemFlags;
    _nodes.reserve(_nodes.size() + other._nodes.size());
    _nodes.insert(_nodes.end(), other._nodes.begin(), other._nodes.end());
    updatePriority(other._priority);
}

void
MergeNodes::markMoveToIdealLocation(uint16_t node, uint8_t msgPriority) {
    _reason << "[Moving bucket to ideal node " << node << "]";
    addProblem(NON_IDEAL_LOCATION);
    addNode(node);
    updatePriority(msgPriority);
}

void
MergeNodes::markOutOfSync(const StateChecker::Context& c, uint8_t msgPriority) {
    _reason << "[Synchronizing buckets with different checksums " << c.entry()->toString() << "]";
    addProblem(OUT_OF_SYNC);
    updatePriority(msgPriority);
}

void
MergeNodes::markMissingReplica(uint16_t node, uint8_t msgPriority) {
    _reason << "[Adding missing node " << node << "]";
    addProblem(MISSING_REPLICA);
    addNode(node);
    updatePriority(msgPriority);
}

bool
presentInIdealState(const StateChecker::Context& c, uint16_t node) noexcept
{
    return c.idealStateBundle.is_nonretired_or_maintenance(node);
}

void
addStatisticsForNonIdealNodes(const StateChecker::Context& c, bool missingReplica)
{
    // Common case is that ideal state == actual state with no missing replicas.
    // If so, do nothing.
    if (!missingReplica && (c.idealState().size() == c.entry()->getNodeCount())) {
        return;
    }
    for (uint32_t j = 0; j < c.entry()->getNodeCount(); ++j) {
        const uint16_t node(c.entry()->getNodeRef(j).getNode());
        if (!presentInIdealState(c, node)) {
            c.stats.incMovingOut(node, c.getBucketSpace());
        } else if (missingReplica) {
            // Copy is in ideal location and we're missing a replica. Thus
            // we treat all ideal copies as sources to copy from.
            c.stats.incCopyingOut(node, c.getBucketSpace());
        }
    }
}

MergeNodes checkForNodesMissingFromIdealState(const StateChecker::Context& c) __attribute__((noinline));
MergeNodes checkIfBucketsAreOutOfSyncAndNeedMerging(const StateChecker::Context& c) __attribute__((noinline));

MergeNodes
checkForNodesMissingFromIdealState(const StateChecker::Context& c)
{
    MergeNodes ret;

    // Check if we need to add copies to get to ideal state.
    if (!c.entry()->emptyAndConsistent()) {
        bool hasMissingReplica = false;
        for (uint16_t node : c.idealState()) {
            bool found = false;
            for (uint32_t j = 0; j < c.entry()->getNodeCount(); j++) {
                if (c.entry()->getNodeRef(j).getNode() == node) {
                    found = true;
                    break;
                }
            }

            if (!found) {
                const auto & mp = c.distributorConfig.getMaintenancePriorities();
                if (c.idealState().size() > c.entry()->getNodeCount()) {
                    ret.markMissingReplica(node, mp.mergeTooFewCopies);
                } else {
                    ret.markMoveToIdealLocation(node,mp.mergeMoveToIdealNode);
                }
                c.stats.incCopyingIn(node, c.getBucketSpace());
                hasMissingReplica = true;
            }
        }
        addStatisticsForNonIdealNodes(c, hasMissingReplica);
    }
    return ret;
}

void
addStatisticsForOutOfSyncCopies(const StateChecker::Context& c)
{
    const uint32_t n = c.entry()->getNodeCount();
    for (uint32_t i = 0; i < n; ++i) {
        const BucketCopy& cp(c.entry()->getNodeRef(i));
        c.stats.incSyncing(cp.getNode(), c.getBucketSpace());
    }
}

MergeNodes
checkIfBucketsAreOutOfSyncAndNeedMerging(const StateChecker::Context& c)
{
    MergeNodes ret;
    if (!consistentApartFromEmptyBucketsInNonIdealLocationAndInvalidEntries(c.idealState(),c.entry().getBucketInfo())) {
        auto pri(c.distributorConfig.getMaintenancePriorities().mergeOutOfSyncCopies);
        ret.markOutOfSync(c, pri);
        addStatisticsForOutOfSyncCopies(c);
    }
    return ret;
}

bool
allCopiesAreInvalid(const StateChecker::Context& c)
{
    const uint32_t n = c.entry()->getNodeCount();
    for (uint32_t i = 0; i < n; ++i) {
        const BucketCopy& cp(c.entry()->getNodeRef(i));
        if (cp.valid()) {
            return false;
        }
    }
    return true;
}

bool
merging_effectively_disabled_for_state_checker(const StateChecker::Context& c) noexcept
{
    return (c.distributorConfig.merge_operations_disabled()
            || (c.distributorConfig.inhibit_default_merges_when_global_merges_pending()
                && c.merges_inhibited_in_bucket_space));
}

}

StateChecker::Result
SynchronizeAndMoveStateChecker::check(const Context &c) const
{
    if (merging_effectively_disabled_for_state_checker(c)) {
        return Result::noMaintenanceNeeded();
    }
    if (isInconsistentlySplit(c)) {
        return Result::noMaintenanceNeeded();
    }
    if (containsMaintenanceNode(c.idealState(), c)) {
        return Result::noMaintenanceNeeded();
    }
    if (ideal_node_is_unavailable_in_pending_state(c)) {
        return Result::noMaintenanceNeeded();
    }
    if (allCopiesAreInvalid(c)) {
        return Result::noMaintenanceNeeded();
    }

    assert(c.entry()->getNodeCount() > 0);

    MergeNodes result(c.entry());
    result += checkForNodesMissingFromIdealState(c);
    result += checkIfBucketsAreOutOfSyncAndNeedMerging(c);

    if (result.shouldMerge()) {
        auto op = std::make_unique<MergeOperation>(BucketAndNodes(c.getBucket(), result.nodes()),
                                                   c.distributorConfig.getMaxNodesPerMerge());
        op->setDetailedReason(result.reason());
        MaintenancePriority::Priority schedPri;
        if ((c.getBucketSpace() == document::FixedBucketSpaces::default_space())
            || !c.distributorConfig.prioritize_global_bucket_merges())
        {
            schedPri = (result.needsMoveOnly() ? MaintenancePriority::LOW : MaintenancePriority::MEDIUM);
            op->setPriority(result.priority());
        } else {
            // Since the default bucket space has a dependency on the global bucket space,
            // we prioritize scheduling of merges to global buckets over those for default buckets.
            // We also prioritize these above bucket deletions for the default space to avoid starvation.
            schedPri = MaintenancePriority::VERY_HIGH;
            op->setPriority(c.distributorConfig.getMaintenancePriorities().mergeGlobalBuckets);
        }

        return Result::createStoredResult(std::move(op), schedPri);
    } else {
        LOG(spam, "Bucket %s: No need for merge, as bucket is in consistent state (or inconsistent buckets are empty) %s",
            c.bucket.toString().c_str(), c.entry()->toString().c_str());
        return Result::noMaintenanceNeeded();
    }
}

bool
DeleteExtraCopiesStateChecker::bucketHasNoData(const Context& c)
{
    return (c.entry()->getHighestMetaCount() == 0
            && !c.entry()->hasRecentlyCreatedEmptyCopy());
}

bool
DeleteExtraCopiesStateChecker::copyIsInIdealState(const BucketCopy& cp, const Context& c)
{
    return c.idealStateBundle.is_nonretired_or_maintenance(cp.getNode());
}

bool
DeleteExtraCopiesStateChecker::enoughCopiesKept(uint32_t keptIdealCopies, uint32_t keptNonIdealCopies, const Context& c)
{
    return ((keptIdealCopies + keptNonIdealCopies) >= c.distribution.getRedundancy());
}

void
DeleteExtraCopiesStateChecker::addToRemoveSet(
        const BucketCopy& copyToRemove,
        const char* reasonForRemoval,
        std::vector<uint16_t>& removedCopies,
        vespalib::asciistream& reasons)
{
    reasons << "[Removing " << reasonForRemoval << " from node " << copyToRemove.getNode() << ']';
    removedCopies.push_back(copyToRemove.getNode());
}

uint32_t
DeleteExtraCopiesStateChecker::numberOfIdealCopiesPresent(const Context& c)
{
    const uint32_t cnt = c.entry()->getNodeCount();
    uint32_t idealCopies = 0;
    for (uint32_t i = 0; i < cnt; ++i) {
        const BucketCopy& cp(c.entry()->getNodeRef(i));
        if (copyIsInIdealState(cp, c)) {
            ++idealCopies;
        }
    }
    return idealCopies;
}

/**
 * Delete copies that are not in ideal state and either:
 *  - in sync with all other copies AND redundant, or
 *  - empty
 *
 * Assumes that no other method has removed copies before this.
 */
void
DeleteExtraCopiesStateChecker::removeRedundantEmptyOrConsistentCopies(
        const Context& c,
        std::vector<uint16_t>& removedCopies,
        vespalib::asciistream& reasons)
{
    assert(removedCopies.empty());
    const bool copiesAreConsistent = c.entry()->validAndConsistent();
    const uint32_t cnt = c.entry()->getNodeCount();
    // Always keep all ideal copies
    uint32_t keptIdealCopies = numberOfIdealCopiesPresent(c);
    uint32_t keptNonIdealCopies = 0;

    for (uint32_t i = 0; i < cnt; ++i) {
        const BucketCopy& cp(c.entry()->getNodeRef(i));
        if (copyIsInIdealState(cp, c)) {
            continue;
        }
        // Caller already checked for recently created/invalid copies, so
        // any empty copies not in ideal state are pending for a bending,
        // no matter if bucket is consistent or not.
        if (cp.empty()) {
            addToRemoveSet(cp, "empty copy", removedCopies, reasons);
        } else if (copiesAreConsistent
                   && enoughCopiesKept(keptIdealCopies, keptNonIdealCopies, c)
                   && !cp.active())
        {
            addToRemoveSet(cp, "redundant in-sync copy", removedCopies, reasons);
        } else {
            ++keptNonIdealCopies;
        }
    }
}

StateChecker::Result
DeleteExtraCopiesStateChecker::check(const Context &c) const
{
    if (c.entry()->hasInvalidCopy()) {
        // Don't delete anything here.
        return Result::noMaintenanceNeeded();
    }
    // Maintain symmetry with merge; don't try to mess with nodes that have an
    // ideal copy on a node set in maintenance mode.
    if (containsMaintenanceNode(c.idealState(), c)) {
        return Result::noMaintenanceNeeded();
    }

    vespalib::asciistream reasons;
    std::vector<uint16_t> removedCopies;

    if (bucketHasNoData(c)) {
        reasons << "[Removing all copies since bucket is empty:" << c.entry()->toString() << "]";

        for (uint32_t j = 0, cnt = c.entry()->getNodeCount(); j < cnt; ++j) {
            removedCopies.push_back(c.entry()->getNodeRef(j).getNode());
        }
    } else if (c.entry()->getNodeCount() <= c.distribution.getRedundancy()) {
        return Result::noMaintenanceNeeded();
    } else if (c.entry()->hasRecentlyCreatedEmptyCopy()) {
        return Result::noMaintenanceNeeded();
    } else {
        removeRedundantEmptyOrConsistentCopies(c, removedCopies, reasons);
    }

    if (!removedCopies.empty()) {
        auto ro = std::make_unique<RemoveBucketOperation>(c.node_ctx, BucketAndNodes(c.getBucket(), removedCopies));

        ro->setPriority(c.distributorConfig.getMaintenancePriorities().deleteBucketCopy);
        ro->setDetailedReason(reasons.str());
        return Result::createStoredResult(std::move(ro), MaintenancePriority::HIGH);
    }

    return Result::noMaintenanceNeeded();
}

namespace {

bool
shouldSkipActivationDueToMaintenanceOrGatherOperationNodes(const ActiveList &activeNodes,
                                                           const StateChecker::Context &c,
                                                           std::vector<uint16_t> & operationNodes) {
    for (uint32_t i = 0; i < activeNodes.size(); ++i) {
        const ActiveCopy & active = activeNodes[i];
        if ( ! active.entryIndex().valid()) continue;
        const BucketCopy & cp(c.entry()->getNodeRef(active.entryIndex()));
        if (cp.active()) continue;

        const auto node_index = active.nodeIndex();
        if (!cp.ready()) {
            if (!c.op_ctx.node_supported_features_repo().node_supported_features(node_index).no_implicit_indexing_of_active_buckets) {
                // If copy is not ready, we don't want to activate it if a node
                // is set in maintenance. Doing so would imply that we want proton
                // to start background indexing.
                if (containsMaintenanceNode(c.idealState(), c)) return true;
            } // else: activation does not imply indexing, so we can safely do it at any time.
        }
        operationNodes.push_back(node_index);
    }
    return false;
}

}

/**
 * The copy we want to set active is, in prioritized order:
 *  1. The first ideal state copy that is trusted and ready
 *  2. The first non-ideal state copy that is ready
 *  3. The first ideal state copy that is trusted
 *  4. The first available copy that is trusted
 *  5. The first ideal state copy
 *  6. Any existing active copy (i.e. do not alter active state)
 *  7. Any valid copy if no copies are active
 */
StateChecker::Result
BucketStateStateChecker::check(const Context &c) const
{
    if (c.distributorConfig.isBucketActivationDisabled()) {
        return Result::noMaintenanceNeeded();
    }

    if (isInconsistentlySplit(c)) {
        return Result::noMaintenanceNeeded();
    }

    ActiveList activeNodes = ActiveCopy::calculate(c.idealStateBundle.nonretired_or_maintenance_to_index(),
                                                   c.distribution, c.entry(),
                                                   c.distributorConfig.max_activation_inhibited_out_of_sync_groups());
    if (activeNodes.empty()) {
        return Result::noMaintenanceNeeded();
    }
    std::vector<uint16_t> operationNodes;
    if (shouldSkipActivationDueToMaintenanceOrGatherOperationNodes(activeNodes, c, operationNodes)) {
        return Result::noMaintenanceNeeded();
    }
    vespalib::asciistream reason;
    for (uint16_t nodeIndex : operationNodes) { // Most of the time empty
        for (uint32_t i = 0; i < activeNodes.size(); ++i) {
            const ActiveCopy &active = activeNodes[i];
            if (nodeIndex == active.nodeIndex()) {
                reason << "[Setting node " << active.nodeIndex() << " as active: " << active.getReason() << "]";
            }
        }
    }

    // Deactivate all copies that are currently marked as active.
    for (uint32_t i = 0; i < c.entry()->getNodeCount(); ++i) {
        const BucketCopy& cp = c.entry()->getNodeRef(i);
        if (!cp.active()) {
            continue;
        }
        bool shouldBeActive = false;
        for (uint32_t j=0; j<activeNodes.size(); ++j) {
            if (activeNodes[j].nodeIndex() == cp.getNode()) {
                shouldBeActive = true;
            }
        }
        if (!shouldBeActive) {
            reason << "[Setting node " << cp.getNode() << " as inactive]";
            operationNodes.push_back(cp.getNode());
        }
    }

    if (operationNodes.empty()) {
        return Result::noMaintenanceNeeded();
    }

    std::vector<uint16_t> activeNodeIndexes;
    for (uint32_t i=0; i<activeNodes.size(); ++i) {
        activeNodeIndexes.push_back(activeNodes[i].nodeIndex());
    }
    auto op = std::make_unique<SetBucketStateOperation>(c.node_ctx, BucketAndNodes(c.getBucket(), operationNodes), activeNodeIndexes);

    // If activeNodes > 1, we're dealing with a active-per-leaf group case and
    // we currently always send high pri activations.
    // Otherwise, only > 1 operationNodes if we have copies to deactivate.
    if (activeNodes.size() > 1 || operationNodes.size() == 1) {
        op->setPriority(c.distributorConfig.getMaintenancePriorities().activateNoExistingActive);
    } else {
        op->setPriority(c.distributorConfig.getMaintenancePriorities().activateWithExistingActive);
    }
    op->setDetailedReason(reason.str());
    return Result::createStoredResult(std::move(op), MaintenancePriority::HIGHEST);
}

bool
GarbageCollectionStateChecker::garbage_collection_disabled(const Context& c) noexcept
{
    return (c.distributorConfig.getGarbageCollectionInterval() == vespalib::duration::zero());
}

bool
GarbageCollectionStateChecker::needs_garbage_collection(const Context& c, vespalib::duration time_since_epoch)
{
    if (c.entry()->getNodeCount() == 0) {
        return false;
    }
    if (containsMaintenanceNode(c.idealState(), c)) {
        return false;
    }
    std::chrono::seconds lastRunAt(c.entry()->getLastGarbageCollectionTime());
    return c.gcTimeCalculator.shouldGc(c.getBucketId(), time_since_epoch, lastRunAt);
}

StateChecker::Result
GarbageCollectionStateChecker::check(const Context &c) const
{
    if (garbage_collection_disabled(c)) {
        return Result::noMaintenanceNeeded();
    }
    const vespalib::duration now(c.node_ctx.clock().getSystemTime().time_since_epoch());
    const std::chrono::seconds last_run_at(c.entry()->getLastGarbageCollectionTime());
    c.stats.update_observed_time_since_last_gc(now - last_run_at);

    if (needs_garbage_collection(c, now)) {
        auto op = std::make_unique<GarbageCollectionOperation>(c.node_ctx, BucketAndNodes(c.getBucket(), c.entry()->getNodes()));

        vespalib::asciistream reason;
        reason << "[Needs garbage collection: Last check at "
               << c.entry()->getLastGarbageCollectionTime()
               << ", current time "
               << vespalib::count_s(now)
               << ", configured interval "
               << vespalib::to_s(c.distributorConfig.getGarbageCollectionInterval()) << "]";

        op->setPriority(c.distributorConfig.getMaintenancePriorities().garbageCollection);
        op->setDetailedReason(reason.c_str());
        return Result::createStoredResult(std::move(op), MaintenancePriority::VERY_LOW);
    } else {
        return Result::noMaintenanceNeeded();
    }
}

}