aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/persistenceengine/persistenceengine.cpp
blob: 57e26f77cca89585aec226e0cc308f540e8e87b8 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "persistenceengine.h"
#include "ipersistenceengineowner.h"
#include "transport_latch.h"
#include <vespa/metrics/loadmetric.h>
#include <vespa/vespalib/stllike/hash_set.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/datatype/documenttype.h>
#include <vespa/document/update/documentupdate.h>
#include <vespa/document/base/exceptions.h>


#include <vespa/log/log.h>
LOG_SETUP(".proton.persistenceengine.persistenceengine");

using document::Document;
using document::DocumentId;
using storage::spi::BucketChecksum;
using storage::spi::BucketIdListResult;
using storage::spi::BucketInfo;
using storage::spi::BucketInfoResult;
using storage::spi::IncludedVersions;
using storage::spi::PartitionState;
using storage::spi::PartitionStateList;
using storage::spi::Result;
using vespalib::IllegalStateException;
using vespalib::Sequence;
using vespalib::make_string;
using std::make_unique;

using namespace std::chrono_literals;

namespace proton {

namespace {

class ResultHandlerBase {
protected:
    std::mutex               _lock;
    vespalib::CountDownLatch _latch;
public:
    ResultHandlerBase(uint32_t waitCnt);
    ~ResultHandlerBase();
    void await() { _latch.await(); }
};

ResultHandlerBase::ResultHandlerBase(uint32_t waitCnt)
    : _lock(),
      _latch(waitCnt)
{}
ResultHandlerBase::~ResultHandlerBase() = default;

class GenericResultHandler : public ResultHandlerBase, public IGenericResultHandler {
private:
    Result _result;
public:
    GenericResultHandler(uint32_t waitCnt) :
        ResultHandlerBase(waitCnt),
        _result()
    { }
    ~GenericResultHandler();
    void handle(const Result &result) override {
        if (result.hasError()) {
            std::lock_guard<std::mutex> guard(_lock);
            if (_result.hasError()) {
                _result = TransportLatch::mergeErrorResults(_result, result);
            } else {
                _result = result;
            }
        }
        _latch.countDown();
    }
    const Result &getResult() const { return _result; }
};

GenericResultHandler::~GenericResultHandler() = default;

class BucketIdListResultHandler : public IBucketIdListResultHandler
{
private:
    typedef vespalib::hash_set<document::BucketId, document::BucketId::hash> BucketIdSet;
    BucketIdSet _bucketSet;
public:
    BucketIdListResultHandler()
        : _bucketSet()
    { }
    ~BucketIdListResultHandler() override;
    void handle(const BucketIdListResult &result) override {
        const BucketIdListResult::List &buckets = result.getList();
        for (size_t i = 0; i < buckets.size(); ++i) {
            _bucketSet.insert(buckets[i]);
        }
    }
    BucketIdListResult getResult() const {
        BucketIdListResult::List buckets;
        buckets.reserve(_bucketSet.size());
        for (document::BucketId bucketId : _bucketSet) {
            buckets.push_back(bucketId);
        }
        return BucketIdListResult(buckets);
    }
};


BucketIdListResultHandler::~BucketIdListResultHandler() = default;

class SynchronizedBucketIdListResultHandler : public ResultHandlerBase,
                                              public BucketIdListResultHandler
{
public:
    SynchronizedBucketIdListResultHandler(uint32_t waitCnt)
        : ResultHandlerBase(waitCnt),
          BucketIdListResultHandler()
    { }
    ~SynchronizedBucketIdListResultHandler() override;
    void handle(const BucketIdListResult &result) override {
        {
            std::lock_guard<std::mutex> guard(_lock);
            BucketIdListResultHandler::handle(result);
        }
        _latch.countDown();
    }
};

SynchronizedBucketIdListResultHandler::~SynchronizedBucketIdListResultHandler() = default;

class BucketInfoResultHandler : public IBucketInfoResultHandler {
private:
    BucketInfoResult _result;
    bool             _first;
public:
    BucketInfoResultHandler() :
        _result(BucketInfo()),
        _first(true)
    {
    }
    ~BucketInfoResultHandler() override;
    void handle(const BucketInfoResult &result) override {
        if (_first) {
            _result = result;
            _first = false;
        } else {
            BucketInfo b1 = _result.getBucketInfo();
            BucketInfo b2 = result.getBucketInfo();
            BucketInfo::ReadyState ready =
                    (b1.getReady() == b2.getReady() ? b1.getReady() : BucketInfo::NOT_READY);
            BucketInfo::ActiveState active =
                    (b1.getActive() == b2.getActive() ? b1.getActive() : BucketInfo::NOT_ACTIVE);
            _result = BucketInfoResult(
                    BucketInfo(BucketChecksum(b1.getChecksum() + b2.getChecksum()),
                            b1.getDocumentCount() + b2.getDocumentCount(),
                            b1.getDocumentSize() + b2.getDocumentSize(),
                            b1.getEntryCount() + b2.getEntryCount(),
                            b1.getUsedSize() + b2.getUsedSize(),
                            ready, active));
        }
    }
    const BucketInfoResult &getResult() const { return _result; }
};

BucketInfoResultHandler::~BucketInfoResultHandler() = default;

}

PersistenceEngine::HandlerSnapshot::UP
PersistenceEngine::getHandlerSnapshot() const
{
    std::lock_guard<std::mutex> guard(_lock);
    return _handlers.getHandlerSnapshot();
}

PersistenceEngine::HandlerSnapshot::UP
PersistenceEngine::getHandlerSnapshot(document::BucketSpace bucketSpace) const
{
    std::lock_guard<std::mutex> guard(_lock);
    return _handlers.getHandlerSnapshot(bucketSpace);
}

PersistenceEngine::PersistenceEngine(IPersistenceEngineOwner &owner, const IResourceWriteFilter &writeFilter,
                                     ssize_t defaultSerializedSize, bool ignoreMaxBytes)
    : AbstractPersistenceProvider(),
      _defaultSerializedSize(defaultSerializedSize),
      _ignoreMaxBytes(ignoreMaxBytes),
      _handlers(),
      _lock(),
      _iterators(),
      _iterators_lock(),
      _owner(owner),
      _writeFilter(writeFilter),
      _clusterStates(),
      _extraModifiedBuckets(),
      _rwMutex()
{
}


PersistenceEngine::~PersistenceEngine()
{
    destroyIterators();
}


IPersistenceHandler::SP
PersistenceEngine::putHandler(document::BucketSpace bucketSpace, const DocTypeName &docType,
                              const IPersistenceHandler::SP &handler)
{
    std::lock_guard<std::mutex> guard(_lock);
    return _handlers.putHandler(bucketSpace, docType, handler);
}


IPersistenceHandler::SP
PersistenceEngine::getHandler(document::BucketSpace bucketSpace, const DocTypeName &docType) const
{
    std::lock_guard<std::mutex> guard(_lock);
    return _handlers.getHandler(bucketSpace, docType);
}


IPersistenceHandler::SP
PersistenceEngine::removeHandler(document::BucketSpace bucketSpace, const DocTypeName &docType)
{
    // TODO: Grab bucket list and treat them as modified
    std::lock_guard<std::mutex> guard(_lock);
    return _handlers.removeHandler(bucketSpace, docType);
}


Result
PersistenceEngine::initialize()
{
    std::unique_lock<std::shared_timed_mutex> wguard(getWLock());
    LOG(debug, "Begin initializing persistence handlers");
    HandlerSnapshot::UP snap = getHandlerSnapshot();
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->initialize();
    }
    LOG(debug, "Done initializing persistence handlers");
    return Result();
}


PersistenceEngine::PartitionStateListResult
PersistenceEngine::getPartitionStates() const
{
    PartitionStateList list(1);
    return PartitionStateListResult(list);
}


BucketIdListResult
PersistenceEngine::listBuckets(BucketSpace bucketSpace, PartitionId id) const
{
    // Runs in SPI thread.
    // No handover to write threads in persistence handlers.
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    if (id != 0) {
        BucketIdListResult::List emptyList;
        return BucketIdListResult(emptyList);
    }
    HandlerSnapshot::UP snap = getHandlerSnapshot(bucketSpace);
    BucketIdListResultHandler resultHandler;
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleListBuckets(resultHandler);
    }
    return resultHandler.getResult();
}


Result
PersistenceEngine::setClusterState(BucketSpace bucketSpace, const ClusterState &calc)
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    saveClusterState(bucketSpace, calc);
    HandlerSnapshot::UP snap = getHandlerSnapshot(bucketSpace);
    GenericResultHandler resultHandler(snap->size());
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleSetClusterState(calc, resultHandler);
    }
    resultHandler.await();
    _owner.setClusterState(bucketSpace, calc);
    return resultHandler.getResult();
}


Result
PersistenceEngine::setActiveState(const Bucket& bucket,
                                  storage::spi::BucketInfo::ActiveState newState)
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    HandlerSnapshot::UP snap = getHandlerSnapshot(bucket.getBucketSpace());
    GenericResultHandler resultHandler(snap->size());
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleSetActiveState(bucket, newState, resultHandler);
    }
    resultHandler.await();
    return resultHandler.getResult();
}


BucketInfoResult
PersistenceEngine::getBucketInfo(const Bucket& b) const
{
    // Runs in SPI thread.
    // No handover to write threads in persistence handlers.
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    HandlerSnapshot::UP snap = getHandlerSnapshot(b.getBucketSpace());
    BucketInfoResultHandler resultHandler;
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleGetBucketInfo(b, resultHandler);
    }
    return resultHandler.getResult();
}


Result
PersistenceEngine::put(const Bucket& b, Timestamp t, const document::Document::SP& doc, Context&)
{
    if (!_writeFilter.acceptWriteOperation()) {
        IResourceWriteFilter::State state = _writeFilter.getAcceptState();
        if (!state.acceptWriteOperation()) {
            return Result(Result::RESOURCE_EXHAUSTED,
                          make_string("Put operation rejected for document '%s': '%s'",
                                      doc->getId().toString().c_str(), state.message().c_str()));
        }
    }
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    DocTypeName docType(doc->getType());
    LOG(spam, "put(%s, %" PRIu64 ", (\"%s\", \"%s\"))", b.toString().c_str(), static_cast<uint64_t>(t.getValue()),
        docType.toString().c_str(), doc->getId().toString().c_str());
    if (!doc->getId().hasDocType()) {
        return Result(Result::PERMANENT_ERROR,
                      make_string("Old id scheme not supported in elastic mode (%s)", doc->getId().toString().c_str()));
    }
    IPersistenceHandler::SP handler = getHandler(b.getBucketSpace(), docType);
    if (!handler) {
        return Result(Result::PERMANENT_ERROR,
                      make_string("No handler for document type '%s'", docType.toString().c_str()));
    }
    TransportLatch latch(1);
    handler->handlePut(feedtoken::make(latch), b, t, doc);
    latch.await();
    return latch.getResult();
}

PersistenceEngine::RemoveResult
PersistenceEngine::remove(const Bucket& b, Timestamp t, const DocumentId& did, Context&)
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    LOG(spam, "remove(%s, %" PRIu64 ", \"%s\")", b.toString().c_str(),
        static_cast<uint64_t>(t.getValue()), did.toString().c_str());
    if (!did.hasDocType()) {
        return RemoveResult(Result::PERMANENT_ERROR,
                            make_string("Old id scheme not supported in elastic mode (%s)", did.toString().c_str()));
    }
    DocTypeName docType(did.getDocType());
    IPersistenceHandler::SP handler = getHandler(b.getBucketSpace(), docType);
    if (!handler) {
        return RemoveResult(Result::PERMANENT_ERROR,
                            make_string("No handler for document type '%s'", docType.toString().c_str()));
    }
    TransportLatch latch(1);
    handler->handleRemove(feedtoken::make(latch), b, t, did);
    latch.await();
    return latch.getRemoveResult();
}


PersistenceEngine::UpdateResult
PersistenceEngine::update(const Bucket& b, Timestamp t, const DocumentUpdate::SP& upd, Context&)
{
    if (!_writeFilter.acceptWriteOperation()) {
        IResourceWriteFilter::State state = _writeFilter.getAcceptState();
        if (!state.acceptWriteOperation()) {
            return UpdateResult(Result::RESOURCE_EXHAUSTED,
                                make_string("Update operation rejected for document '%s': '%s'",
                                            upd->getId().toString().c_str(), state.message().c_str()));
        }
    }
    try {
        upd->eagerDeserialize();
    } catch (document::FieldNotFoundException & e) {
        return UpdateResult(Result::TRANSIENT_ERROR,
                            make_string("Update operation rejected for document '%s' of type '%s': 'Field not found'",
                                        upd->getId().toString().c_str(), upd->getType().getName().c_str()));
    } catch (document::DocumentTypeNotFoundException & e) {
        return UpdateResult(Result::TRANSIENT_ERROR,
                            make_string("Update operation rejected for document '%s' of type '%s'.",
                                        upd->getId().toString().c_str(), e.getDocumentTypeName().c_str()));

    } catch (document::WrongTensorTypeException &e) {
        return UpdateResult(Result::TRANSIENT_ERROR,
                            make_string("Update operation rejected for document '%s' of type '%s': 'Wrong tensor type: %s'",
                                        upd->getId().toString().c_str(),
                                        upd->getType().getName().c_str(),
                                        e.getMessage().c_str()));
    }
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    DocTypeName docType(upd->getType());
    LOG(spam, "update(%s, %" PRIu64 ", (\"%s\", \"%s\"), createIfNonExistent='%s')",
        b.toString().c_str(), static_cast<uint64_t>(t.getValue()), docType.toString().c_str(),
        upd->getId().toString().c_str(), (upd->getCreateIfNonExistent() ? "true" : "false"));
    if (!upd->getId().hasDocType()) {
        return UpdateResult(Result::PERMANENT_ERROR,
                            make_string("Old id scheme not supported in elastic mode (%s)", upd->getId().toString().c_str()));
    }
    if (upd->getId().getDocType() != docType.getName()) {
        return UpdateResult(Result::PERMANENT_ERROR,
                            make_string("Update operation rejected due to bad id (%s, %s)", upd->getId().toString().c_str(), docType.getName().c_str()));
    }
    IPersistenceHandler::SP handler = getHandler(b.getBucketSpace(), docType);

    if (handler) {
        TransportLatch latch(1);
        LOG(debug, "update = %s", upd->toXml().c_str());
        handler->handleUpdate(feedtoken::make(latch), b, t, upd);
        latch.await();
        return latch.getUpdateResult();
    } else {
        return UpdateResult(Result::PERMANENT_ERROR, make_string("No handler for document type '%s'", docType.toString().c_str()));
    }
}


PersistenceEngine::GetResult
PersistenceEngine::get(const Bucket& b, const document::FieldSet& fields, const DocumentId& did, Context& context) const
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    HandlerSnapshot::UP snapshot = getHandlerSnapshot(b.getBucketSpace());

    for (PersistenceHandlerSequence & handlers = snapshot->handlers(); handlers.valid(); handlers.next()) {
        BucketGuard::UP bucket_guard = handlers.get()->lockBucket(b);
        IPersistenceHandler::RetrieversSP retrievers = handlers.get()->getDocumentRetrievers(context.getReadConsistency());
        for (size_t i = 0; i < retrievers->size(); ++i) {
            IDocumentRetriever &retriever = *(*retrievers)[i];
            search::DocumentMetaData meta = retriever.getDocumentMetaData(did);
            if (meta.timestamp != 0 && meta.bucketId == b.getBucketId()) {
                if (meta.removed) {
                    return GetResult();
                }
                document::Document::UP doc = retriever.getDocument(meta.lid);
                if (!doc || doc->getId().getGlobalId() != meta.gid) {
                    return GetResult();
                }
                document::FieldSet::stripFields(*doc, fields);
                return GetResult(std::move(doc), meta.timestamp);
            }
        }
    }
    return GetResult();
}


PersistenceEngine::CreateIteratorResult
PersistenceEngine::createIterator(const Bucket &bucket, const document::FieldSet& fields, const Selection &selection,
                                  IncludedVersions versions, Context & context)
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    HandlerSnapshot::UP snapshot = getHandlerSnapshot(bucket.getBucketSpace());

    auto entry = std::make_unique<IteratorEntry>(context.getReadConsistency(), bucket, fields, selection,
                                                 versions, _defaultSerializedSize, _ignoreMaxBytes);
    entry->bucket_guards.reserve(snapshot->size());
    for (PersistenceHandlerSequence & handlers = snapshot->handlers(); handlers.valid(); handlers.next()) {
        entry->bucket_guards.push_back(handlers.get()->lockBucket(bucket));
        IPersistenceHandler::RetrieversSP retrievers = handlers.get()->getDocumentRetrievers(context.getReadConsistency());
        for (size_t i = 0; i < retrievers->size(); ++i) {
            entry->it.add((*retrievers)[i]);
        }
    }
    entry->handler_sequence = HandlerSnapshot::release(std::move(*snapshot));

    std::lock_guard<std::mutex> guard(_iterators_lock);
    static IteratorId id_counter(0);
    IteratorId id(++id_counter);
    _iterators[id] = entry.release();
    return CreateIteratorResult(id);
}


PersistenceEngine::IterateResult
PersistenceEngine::iterate(IteratorId id, uint64_t maxByteSize, Context&) const
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    IteratorEntry *iteratorEntry;
    {
        std::lock_guard<std::mutex> guard(_iterators_lock);
        auto it = _iterators.find(id);
        if (it == _iterators.end()) {
            return IterateResult(Result::PERMANENT_ERROR, make_string("Unknown iterator with id %" PRIu64, id.getValue()));
        }
        iteratorEntry = it->second;
        if (iteratorEntry->in_use) {
            return IterateResult(Result::TRANSIENT_ERROR, make_string("Iterator with id %" PRIu64 " is already in use", id.getValue()));
        }
        iteratorEntry->in_use = true;
    }

    DocumentIterator &iterator = iteratorEntry->it;
    try {
        IterateResult result = iterator.iterate(maxByteSize);
        std::lock_guard<std::mutex> guard(_iterators_lock);
        iteratorEntry->in_use = false;
        return result;
    } catch (const std::exception & e) {
        IterateResult result(Result::PERMANENT_ERROR, make_string("Caught exception during visitor iterator.iterate() = '%s'", e.what()));
        LOG(warning, "Caught exception during visitor iterator.iterate() = '%s'", e.what());
        std::lock_guard<std::mutex> guard(_iterators_lock);
        iteratorEntry->in_use = false;
        return result;
    }
}


Result
PersistenceEngine::destroyIterator(IteratorId id, Context&)
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    std::lock_guard<std::mutex> guard(_iterators_lock);
    auto it = _iterators.find(id);
    if (it == _iterators.end()) {
        return Result();
    }
    if (it->second->in_use) {
        return Result(Result::TRANSIENT_ERROR, make_string("Iterator with id %" PRIu64 " is currently in use", id.getValue()));
    }
    delete it->second;
    _iterators.erase(it);
    return Result();
}


Result
PersistenceEngine::createBucket(const Bucket &b, Context &)
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    LOG(spam, "createBucket(%s)", b.toString().c_str());
    HandlerSnapshot::UP snap = getHandlerSnapshot(b.getBucketSpace());
    TransportLatch latch(snap->size());
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleCreateBucket(feedtoken::make(latch), b);
    }
    latch.await();
    return latch.getResult();
}


Result
PersistenceEngine::deleteBucket(const Bucket& b, Context&)
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    LOG(spam, "deleteBucket(%s)", b.toString().c_str());
    HandlerSnapshot::UP snap = getHandlerSnapshot(b.getBucketSpace());
    TransportLatch latch(snap->size());
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleDeleteBucket(feedtoken::make(latch), b);
    }
    latch.await();
    return latch.getResult();
}


BucketIdListResult
PersistenceEngine::getModifiedBuckets(BucketSpace bucketSpace) const
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    typedef BucketIdListResultV MBV;
    MBV extraModifiedBuckets;
    {
        std::lock_guard<std::mutex> guard(_lock);
        extraModifiedBuckets.swap(_extraModifiedBuckets[bucketSpace]);
    }
    HandlerSnapshot::UP snap = getHandlerSnapshot(bucketSpace);
    SynchronizedBucketIdListResultHandler resultHandler(snap->size() + extraModifiedBuckets.size());
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleGetModifiedBuckets(resultHandler);
    }
    for (const auto & item : extraModifiedBuckets) {
        resultHandler.handle(*item);
    }
    resultHandler.await();
    return resultHandler.getResult();
}


Result
PersistenceEngine::split(const Bucket& source, const Bucket& target1, const Bucket& target2, Context&)
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    LOG(spam, "split(%s, %s, %s)", source.toString().c_str(), target1.toString().c_str(), target2.toString().c_str());
    assert(source.getBucketSpace() == target1.getBucketSpace());
    assert(source.getBucketSpace() == target2.getBucketSpace());
    HandlerSnapshot::UP snap = getHandlerSnapshot(source.getBucketSpace());
    TransportLatch latch(snap->size());
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleSplit(feedtoken::make(latch), source, target1, target2);
    }
    latch.await();
    return latch.getResult();
}


Result
PersistenceEngine::join(const Bucket& source1, const Bucket& source2, const Bucket& target, Context&)
{
    std::shared_lock<std::shared_timed_mutex> rguard(_rwMutex);
    LOG(spam, "join(%s, %s, %s)", source1.toString().c_str(), source2.toString().c_str(), target.toString().c_str());
    assert(source1.getBucketSpace() == target.getBucketSpace());
    assert(source2.getBucketSpace() == target.getBucketSpace());
    HandlerSnapshot::UP snap = getHandlerSnapshot(target.getBucketSpace());
    TransportLatch latch(snap->size());
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleJoin(feedtoken::make(latch), source1, source2, target);
    }
    latch.await();
    return latch.getResult();
}


Result
PersistenceEngine::maintain(const Bucket& , MaintenanceLevel)
{
    return Result();
}

void
PersistenceEngine::destroyIterators()
{
    Context context(storage::spi::LoadType(0, "default"),
                    storage::spi::Priority(0x80),
                    storage::spi::Trace::TraceLevel(0));
    for (;;) {
        IteratorId id;
        {
            std::lock_guard<std::mutex> guard(_iterators_lock);
            if (_iterators.empty())
                break;
            id = _iterators.begin()->first;
        }
        Result res(destroyIterator(id, context));
        if (res.hasError()) {
            LOG(debug, "%zu iterator left. Can not destroy iterator '%" PRIu64 "'. Reason='%s'", _iterators.size(), id.getValue(), res.toString().c_str());
            std::this_thread::sleep_for(100ms);
        }
    }
}


void
PersistenceEngine::saveClusterState(BucketSpace bucketSpace, const ClusterState &calc)
{
    auto clusterState = std::make_shared<ClusterState>(calc);
    {
        std::lock_guard<std::mutex> guard(_lock);
        clusterState.swap(_clusterStates[bucketSpace]);
    }
}

PersistenceEngine::ClusterState::SP
PersistenceEngine::savedClusterState(BucketSpace bucketSpace) const
{
    std::lock_guard<std::mutex> guard(_lock);
    auto itr(_clusterStates.find(bucketSpace));
    return ((itr != _clusterStates.end()) ? itr->second : ClusterState::SP());
}

void
PersistenceEngine::propagateSavedClusterState(BucketSpace bucketSpace, IPersistenceHandler &handler)
{
    ClusterState::SP clusterState(savedClusterState(bucketSpace));
    if (!clusterState)
        return;
    // Propagate saved cluster state.
    // TODO: Fix race with new cluster state setting.
    GenericResultHandler resultHandler(1);
    handler.handleSetClusterState(*clusterState, resultHandler);
    resultHandler.await();
}

void
PersistenceEngine::grabExtraModifiedBuckets(BucketSpace bucketSpace, IPersistenceHandler &handler)
{
    BucketIdListResultHandler resultHandler;
    handler.handleListBuckets(resultHandler);
    auto result = std::make_shared<BucketIdListResult>(resultHandler.getResult());
    std::lock_guard<std::mutex> guard(_lock);
    _extraModifiedBuckets[bucketSpace].push_back(result);
}


class ActiveBucketIdListResultHandler : public IBucketIdListResultHandler
{
private:
    using BucketIdMap = std::map<document::BucketId, size_t>;
    using IR = std::pair<BucketIdMap::iterator, bool>;
    BucketIdMap _bucketMap;
public:
    ActiveBucketIdListResultHandler() : _bucketMap() { }

    void handle(const BucketIdListResult &result) override {
        const BucketIdListResult::List &buckets = result.getList();
        for (size_t i = 0; i < buckets.size(); ++i) {
            IR ir(_bucketMap.insert(std::make_pair(buckets[i], 1u)));
            if (!ir.second) {
                ++(ir.first->second);
            }
        }
    }

    const BucketIdMap & getBucketMap() const { return _bucketMap; }
};

void
PersistenceEngine::populateInitialBucketDB(BucketSpace bucketSpace,
                                           IPersistenceHandler &targetHandler)
{
    HandlerSnapshot::UP snap = getHandlerSnapshot(bucketSpace);
    
    size_t snapSize(snap->size());
    size_t flawed = 0;

    // handleListActiveBuckets() runs in SPI thread.
    // No handover to write threads in persistence handlers.
    ActiveBucketIdListResultHandler resultHandler;
    for (; snap->handlers().valid(); snap->handlers().next()) {
        IPersistenceHandler *handler = snap->handlers().get();
        handler->handleListActiveBuckets(resultHandler);
    }
    typedef std::map<document::BucketId, size_t> BucketIdMap;
    document::BucketId::List buckets;
    const BucketIdMap &bucketMap(resultHandler.getBucketMap());

    for (const auto & item : bucketMap) {
        if (item.second != snapSize) {
            ++flawed;
        }
        buckets.push_back(item.first);
    }
    LOG(info, "Adding %zu active buckets (%zu flawed) to new bucket db", buckets.size(), flawed);
    GenericResultHandler trHandler(1);
    targetHandler.handlePopulateActiveBuckets(buckets, trHandler);
    trHandler.await();
}

std::unique_lock<std::shared_timed_mutex>
PersistenceEngine::getWLock() const
{
    return std::unique_lock<std::shared_timed_mutex>(_rwMutex);
}

} // storage