aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/operations/external/twophaseupdateoperation.h
blob: 84bcf2beff8c6c40b352549e7872fd41be694e88 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <set>
#include <vespa/storageapi/messageapi/returncode.h>
#include <vespa/storage/distributor/persistencemessagetracker.h>
#include <vespa/storage/distributor/operations/sequenced_operation.h>
#include <vespa/document/update/documentupdate.h>

namespace document {
class Document;
}

namespace storage {

namespace api {
class UpdateCommand;
class CreateBucketReply;
}

class UpdateMetricSet;

namespace distributor {

class DistributorBucketSpace;

/*
 * General functional outline:
 *
 * if bucket is consistent and all copies are in sync
 *   send updates directly to nodes
 * else
 *   start safe (slow) path
 *
 * Slow path:
 *
 * send Get for document to update to inconsistent copies
 * if get reply has document
 *   apply updates and send new put
 * else if create-if-non-existing set on update
 *   create new blank document
 *   apply updates and send new put
 * else
 *   reply with not found
 *
 * Note that the above case also implicitly handles the case in which a
 * bucket does not exist.
*/


class TwoPhaseUpdateOperation : public SequencedOperation
{
public:
    TwoPhaseUpdateOperation(DistributorComponent& manager,
                            DistributorBucketSpace &bucketSpace,
                            const std::shared_ptr<api::UpdateCommand> & msg,
                            DistributorMetricSet& metrics,
                            SequencingHandle sequencingHandle = SequencingHandle());
    ~TwoPhaseUpdateOperation();

    void onStart(DistributorMessageSender& sender) override;

    const char* getName() const override { return "twophaseupdate"; }

    std::string getStatus() const override { return ""; }

    void onReceive(DistributorMessageSender&,
                   const std::shared_ptr<api::StorageReply>&) override;

    void onClose(DistributorMessageSender& sender) override;

    bool canSendHeaderOnly() const;

private:
    enum class SendState {
        NONE_SENT,
        UPDATES_SENT,
        GETS_SENT,
        PUTS_SENT,
    };

    enum class Mode {
        FAST_PATH,
        SLOW_PATH
    };

    void transitionTo(SendState newState);
    const char* stateToString(SendState);

    void sendReply(DistributorMessageSender&,
                   std::shared_ptr<api::StorageReply>&);
    void sendReplyWithResult(DistributorMessageSender&, const api::ReturnCode&);
    void ensureUpdateReplyCreated();

    bool isFastPathPossible() const;
    void startFastPathUpdate(DistributorMessageSender&);
    void startSafePathUpdate(DistributorMessageSender&);
    bool lostBucketOwnershipBetweenPhases() const;
    void sendLostOwnershipTransientErrorReply(DistributorMessageSender&);
    void schedulePutsWithUpdatedDocument(
            std::shared_ptr<document::Document>,
            api::Timestamp,
            DistributorMessageSender&);
    void applyUpdateToDocument(document::Document&) const;
    std::shared_ptr<document::Document> createBlankDocument() const;
    void setUpdatedForTimestamp(api::Timestamp);
    void handleFastPathReceive(DistributorMessageSender&,
                               const std::shared_ptr<api::StorageReply>&);
    void handleSafePathReceive(DistributorMessageSender&,
                               const std::shared_ptr<api::StorageReply>&);
    void handleSafePathReceivedGet(DistributorMessageSender&,
                                   api::GetReply&);
    void handleSafePathReceivedPut(DistributorMessageSender&,
                                   const api::PutReply&);
    bool shouldCreateIfNonExistent() const;
    bool processAndMatchTasCondition(
            DistributorMessageSender& sender,
            const document::Document& candidateDoc);
    bool satisfiesUpdateTimestampConstraint(api::Timestamp) const;
    void addTraceFromReply(const api::StorageReply& reply);
    bool hasTasCondition() const noexcept;
    void replyWithTasFailure(DistributorMessageSender& sender,
                             vespalib::stringref message);
    bool may_restart_with_fast_path(const api::GetReply& reply);
    void restart_with_fast_path_due_to_consistent_get_timestamps(DistributorMessageSender& sender);

    UpdateMetricSet& _updateMetric;
    PersistenceOperationMetricSet& _putMetric;
    PersistenceOperationMetricSet& _getMetric;
    std::shared_ptr<api::UpdateCommand> _updateCmd;
    std::shared_ptr<api::StorageReply> _updateReply;
    DistributorComponent& _manager;
    DistributorBucketSpace &_bucketSpace;
    SentMessageMap _sentMessageMap;
    SendState _sendState;
    Mode _mode;
    mbus::TraceNode _trace;
    document::BucketId _updateDocBucketId;
    bool _replySent;
};

}

}