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

#include "newest_replica.h"
#include <vespa/storage/distributor/persistencemessagetracker.h>
#include <vespa/storage/distributor/operations/sequenced_operation.h>
#include <vespa/document/update/documentupdate.h>
#include <set>
#include <optional>

namespace document { class Document; }

namespace storage::api {
class UpdateCommand;
class UpdateReply;
class CreateBucketReply;
class ReturnCode;
}

namespace storage::distributor {

class DistributorBucketSpace;
class GetOperation;
class UpdateMetricSet;

/*
 * 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(const DistributorNodeContext& node_ctx,
                            DistributorStripeOperationContext& op_ctx,
                            const DocumentSelectionParser& parser,
                            DistributorBucketSpace& bucketSpace,
                            std::shared_ptr<api::UpdateCommand> msg,
                            DistributorMetricSet& metrics,
                            SequencingHandle sequencingHandle = SequencingHandle());
    ~TwoPhaseUpdateOperation() override;

    void onStart(DistributorStripeMessageSender& sender) override;

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

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

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

    void onClose(DistributorStripeMessageSender& sender) override;

    void on_cancel(DistributorStripeMessageSender& sender, const CancelScope& cancel_scope) override;

private:
    enum class SendState {
        NONE_SENT,
        UPDATES_SENT,
        METADATA_GETS_SENT,
        SINGLE_GET_SENT,
        FULL_GETS_SENT,
        PUTS_SENT,
    };

    enum class Mode {
        FAST_PATH,
        SLOW_PATH
    };

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

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

    [[nodiscard]] std::vector<BucketDatabase::Entry> get_bucket_database_entries() const;
    [[nodiscard]] static bool isFastPathPossible(const std::vector<BucketDatabase::Entry>& entries);
    void startFastPathUpdate(DistributorStripeMessageSender& sender, std::vector<BucketDatabase::Entry> entries);
    void startSafePathUpdate(DistributorStripeMessageSender&);
    [[nodiscard]] bool lostBucketOwnershipBetweenPhases() const;
    void sendLostOwnershipTransientErrorReply(DistributorStripeMessageSender&);
    void send_operation_cancelled_reply(DistributorStripeMessageSender& sender);
    void send_feed_blocked_error_reply(DistributorStripeMessageSender& sender);
    void schedulePutsWithUpdatedDocument(
            std::shared_ptr<document::Document>,
            api::Timestamp,
            DistributorStripeMessageSender&);
    void applyUpdateToDocument(document::Document&) const;
    [[nodiscard]] std::shared_ptr<document::Document> createBlankDocument() const;
    void setUpdatedForTimestamp(api::Timestamp);
    void handleFastPathReceive(DistributorStripeMessageSender&,
                               const std::shared_ptr<api::StorageReply>&);
    void handleSafePathReceive(DistributorStripeMessageSender&,
                               const std::shared_ptr<api::StorageReply>&);
    std::shared_ptr<GetOperation> create_initial_safe_path_get_operation();
    void handle_safe_path_received_metadata_get(DistributorStripeMessageSender&,
                                                api::GetReply&,
                                                const std::optional<NewestReplica>&,
                                                bool any_replicas_failed);
    void handle_safe_path_received_single_full_get(DistributorStripeMessageSender&, api::GetReply&);
    void handleSafePathReceivedGet(DistributorStripeMessageSender&, api::GetReply&);
    void handleSafePathReceivedPut(DistributorStripeMessageSender&, const api::PutReply&);
    [[nodiscard]] bool shouldCreateIfNonExistent() const;
    bool processAndMatchTasCondition(
            DistributorStripeMessageSender& sender,
            const document::Document& candidateDoc);
    [[nodiscard]] bool satisfiesUpdateTimestampConstraint(api::Timestamp) const;
    void addTraceFromReply(api::StorageReply& reply);
    [[nodiscard]] bool hasTasCondition() const noexcept;
    void replyWithTasFailure(DistributorStripeMessageSender& sender,
                             vespalib::stringref message);
    bool may_restart_with_fast_path(const api::GetReply& reply);
    [[nodiscard]] bool replica_set_unchanged_after_get_operation() const;
    void restart_with_fast_path_due_to_consistent_get_timestamps(DistributorStripeMessageSender& sender);
    // Precondition: reply has not yet been sent.
    [[nodiscard]] vespalib::string update_doc_id() const;

    using ReplicaState = std::vector<std::pair<document::BucketId, uint16_t>>;

    UpdateMetricSet&                    _updateMetric;
    PersistenceOperationMetricSet&      _putMetric;
    PersistenceOperationMetricSet&      _put_condition_probe_metrics;
    PersistenceOperationMetricSet&      _getMetric;
    PersistenceOperationMetricSet&      _metadata_get_metrics;
    std::shared_ptr<api::UpdateCommand> _updateCmd;
    std::shared_ptr<api::UpdateReply>   _updateReply;
    const DistributorNodeContext&       _node_ctx;
    DistributorStripeOperationContext&  _op_ctx;
    const DocumentSelectionParser&      _parser;
    DistributorBucketSpace&             _bucketSpace;
    SentMessageMap                      _sentMessageMap;
    SendState                           _sendState;
    Mode                                _mode;
    mbus::Trace                         _trace;
    document::BucketId                  _updateDocBucketId;
    ReplicaState                        _replicas_at_get_send_time;
    std::optional<framework::MilliSecTimer> _single_get_latency_timer;
    uint16_t                            _fast_path_repair_source_node;
    bool                                _use_initial_cheap_metadata_fetch_phase;
    bool                                _replySent;
};

}