aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/distributor/operations/idealstate/mergeoperation.cpp
blob: 0a11a8233aac386c3f5c7c706e7e7328c62dd938 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "mergeoperation.h"
#include <vespa/document/bucket/fixed_bucket_spaces.h>
#include <vespa/storage/distributor/idealstatemanager.h>
#include <vespa/storage/distributor/idealstatemetricsset.h>
#include <vespa/storage/distributor/distributor_bucket_space.h>
#include <vespa/storage/distributor/node_supported_features_repo.h>
#include <vespa/storage/distributor/pendingmessagetracker.h>
#include <vespa/storageframework/generic/clock/clock.h>
#include <vespa/vdslib/distribution/distribution.h>
#include <vespa/vdslib/state/clusterstate.h>
#include <array>

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

using vespalib::to_utc;
using vespalib::to_string;
using vespalib::make_string_short::fmt;
namespace storage::distributor {

MergeOperation::~MergeOperation() = default;

std::string
MergeOperation::getStatus() const
{
    return
        Operation::getStatus() +
        fmt(" . Sent MergeBucketCommand at %s", to_string(to_utc(_sentMessageTime)).c_str());
}

void
MergeOperation::addIdealNodes(
        const std::vector<uint16_t>& idealNodes,
        const std::vector<MergeMetaData>& nodes,
        std::vector<MergeMetaData>& result)
{
    // Add all ideal nodes first. These are never marked source-only.
    for (uint16_t idealNode : idealNodes) {
        const MergeMetaData* entry = nullptr;
        for (const auto & node : nodes) {
            if (idealNode == node._nodeIndex) {
                entry = &node;
                break;
            }
        }

        if (entry != nullptr) {
            result.push_back(*entry);
            result.back()._sourceOnly = false;
        }
    }
}

void
MergeOperation::addCopiesNotAlreadyAdded(uint16_t redundancy,
                                         const std::vector<MergeMetaData>& nodes,
                                         std::vector<MergeMetaData>& result)
{
    for (const auto & node : nodes) {
        bool found = false;
        for (const auto & mergeData : result) {
            if (mergeData._nodeIndex == node._nodeIndex) {
                found = true;
            }
        }

        if (!found) {
            result.push_back(node);
            result.back()._sourceOnly = (result.size() > redundancy);
        }
    }
}

void
MergeOperation::generateSortedNodeList(
        const lib::Distribution& distribution,
        const lib::ClusterState& state,
        const document::BucketId& bucketId,
        MergeLimiter& limiter,
        std::vector<MergeMetaData>& nodes)
{
    std::vector<uint16_t> idealNodes(distribution.getIdealStorageNodes(state, bucketId, "ui"));

    std::vector<MergeMetaData> result;
    const uint16_t redundancy = distribution.getRedundancy();
    addIdealNodes(idealNodes, nodes, result);
    addCopiesNotAlreadyAdded(redundancy, nodes, result);
    // TODO optimization: when merge case is obviously a replica move (all existing N replicas
    // are in sync and new replicas are empty), could prune away N-1 lowest indexed replicas
    // from the node list. This would minimize the number of nodes involved in the merge without
    // sacrificing the end result. Avoiding the lower indexed nodes would take pressure off the
    // merge throttling "locks" and could potentially greatly speed up node retirement in the common
    // case. Existing replica could also be marked as source-only if it's not in the ideal state.
    limiter.limitMergeToMaxNodes(result);
    result.swap(nodes);
}

namespace {

struct NodeIndexComparator
{
    bool operator()(const storage::api::MergeBucketCommand::Node& a,
                    const storage::api::MergeBucketCommand::Node& b) const
    {
        return a.index < b.index;
    }
};

}

void
MergeOperation::onStart(DistributorStripeMessageSender& sender)
{
    BucketDatabase::Entry entry = _bucketSpace->getBucketDatabase().get(getBucketId());
    if (!entry.valid()) {
        LOGBP(debug, "Unable to merge nonexisting bucket %s", getBucketId().toString().c_str());
        _ok = false;
        done();
        return;
    }

    const lib::ClusterState& clusterState(_bucketSpace->getClusterState());
    std::vector<std::unique_ptr<BucketCopy> > newCopies;
    std::vector<MergeMetaData> nodes;

    for (uint16_t node : getNodes()) {
        const BucketCopy* copy = entry->getNode(node);
        if (copy == nullptr) { // New copies?
            newCopies.emplace_back(std::make_unique<BucketCopy>(BucketCopy::recentlyCreatedCopy(0, node)));
            copy = newCopies.back().get();
        }
        nodes.emplace_back(node, *copy);
    }
    _infoBefore = entry.getBucketInfo();

    generateSortedNodeList(_bucketSpace->getDistribution(), clusterState, getBucketId(), _limiter, nodes);
    for (const auto& node : nodes) {
        _mnodes.emplace_back(node._nodeIndex, node._sourceOnly);
    }

    if (_mnodes.size() > 1) {
        auto msg = std::make_shared<api::MergeBucketCommand>(getBucket(), _mnodes,
                                                             _manager->operation_context().generate_unique_timestamp(),
                                                             clusterState.getVersion());
        const bool may_send_unordered = (_manager->operation_context().distributor_config().use_unordered_merge_chaining()
                                         && all_involved_nodes_support_unordered_merge_chaining());
        if (!may_send_unordered) {
            // Due to merge forwarding/chaining semantics, we must always send
            // the merge command to the lowest indexed storage node involved in
            // the merge in order to avoid deadlocks.
            std::sort(_mnodes.begin(), _mnodes.end(), NodeIndexComparator());
        } else {
            msg->set_use_unordered_forwarding(true);
        }

        LOG(debug, "Sending %s to storage node %u", msg->toString().c_str(), _mnodes[0].index);

        // Set timeout to one hour to prevent hung nodes that manage to keep
        // connections open from stalling merges in the cluster indefinitely.
        msg->setTimeout(3600s);
        setCommandMeta(*msg);

        sender.sendToNode(lib::NodeType::STORAGE, _mnodes[0].index, msg);

        _sentMessageTime = _manager->node_context().clock().getMonotonicTime();
    } else {
        LOGBP(debug, "Unable to merge bucket %s, since only one copy is available. System state %s",
              getBucketId().toString().c_str(), clusterState.toString().c_str());
        _ok = false;
        done();
    }
}

bool
MergeOperation::sourceOnlyCopyChangedDuringMerge(
        const BucketDatabase::Entry& currentState) const
{
    assert(currentState.valid());
    for (const auto& mnode : _mnodes) {
        const BucketCopy* copyBefore(_infoBefore.getNode(mnode.index));
        if (!copyBefore) {
            continue;
        }
        const BucketCopy* copyAfter(currentState->getNode(mnode.index));
        if (!copyAfter) {
            LOG(debug, "Copy of %s on node %u removed during merge. Was %s",
                getBucketId().toString().c_str(), mnode.index, copyBefore->toString().c_str());
            continue;
        }
        if (mnode.sourceOnly && !copyBefore->consistentWith(*copyAfter)){
            LOG(debug, "Source-only copy of %s on node %u changed from %s to %s during the course of the merge. Failing it.",
                getBucketId().toString().c_str(), mnode.index, copyBefore->toString().c_str(), copyAfter->toString().c_str());
            return true;
        }
    }

    return false;
}

void
MergeOperation::deleteSourceOnlyNodes(
        const BucketDatabase::Entry& currentState,
        DistributorStripeMessageSender& sender)
{
    assert(currentState.valid());
    std::vector<uint16_t> sourceOnlyNodes;
    for (const auto& mnode : _mnodes) {
        const uint16_t nodeIndex = mnode.index;
        const BucketCopy* copy = currentState->getNode(nodeIndex);
        if (!copy) {
            continue; // No point in deleting what's not even there now.
        }
        if (mnode.sourceOnly) {
            sourceOnlyNodes.push_back(nodeIndex);
        }
    }

    LOG(debug, "Attempting to delete %zu source only copies for %s",
        sourceOnlyNodes.size(), getBucketId().toString().c_str());

    if (!sourceOnlyNodes.empty()) {
        _removeOperation = std::make_unique<RemoveBucketOperation>(_manager->node_context(), BucketAndNodes(getBucket(), sourceOnlyNodes));
        // Must not send removes to source only copies if something has caused
        // pending load to the copy after the merge was sent!
        if (_removeOperation->isBlocked(_manager->operation_context(), sender.operation_sequencer())) {
            LOG(debug, "Source only removal for %s was blocked by a pending operation",
                getBucketId().toString().c_str());
            _ok = false;
            auto merge_metrics = get_merge_metrics();
            if (merge_metrics) {
                merge_metrics->source_only_copy_delete_blocked.inc(1);
            }
            done();
            return;
        }
        _removeOperation->setIdealStateManager(_manager);
        // We cap the DeleteBucket pri so that it FIFOs with the default feed priority (120).
        // Not doing this risks preempting feed ops with deletes, elevating latencies.
        // TODO less magical numbers, but the priority mapping is technically config...
        _removeOperation->setPriority(std::max(api::StorageMessage::Priority(120), getPriority()));
        
        if (_removeOperation->onStartInternal(sender)) {
            _ok = _removeOperation->ok();
            done();
        }
    } else {
        done();
    }
}

void
MergeOperation::onReceive(DistributorStripeMessageSender& sender, const std::shared_ptr<api::StorageReply> & msg)
{
    if (_removeOperation) {
        if (_removeOperation->onReceiveInternal(msg)) {
            _ok = _removeOperation->ok();
            if (!_ok) {
                auto merge_metrics = get_merge_metrics();
                if (merge_metrics) {
                    merge_metrics->source_only_copy_delete_failed.inc(1);
                }
            }
            done();
        }

        return;
    }

    auto& reply = dynamic_cast<api::MergeBucketReply&>(*msg);
    LOG(debug, "Merge operation for bucket %s finished", getBucketId().toString().c_str());

    api::ReturnCode result = reply.getResult();
    _ok = result.success();
    // We avoid replica deletion entirely if _any_ aspect of the merge has been cancelled.
    // It is, for instance, possible that a node that was previously considered source-only
    // now is part of the #redundancy ideal copies because another node became unavailable.
    // Leave it up to the maintenance state checkers to figure this out.
    if (_cancel_scope.is_cancelled()) {
        LOG(debug, "Merge operation for %s has been cancelled", getBucketId().toString().c_str());
        _ok = false;
    } else if (_ok) {
        BucketDatabase::Entry entry(_bucketSpace->getBucketDatabase().get(getBucketId()));
        if (!entry.valid()) {
            LOG(debug, "Bucket %s no longer exists after merge", getBucketId().toString().c_str());
            done(); // Nothing more we can do.
            return;
        }
        if (sourceOnlyCopyChangedDuringMerge(entry)) {
            _ok = false;
            auto merge_metrics = get_merge_metrics();
            if (merge_metrics) {
                merge_metrics->source_only_copy_changed.inc(1);
            }
            done();
            return;
        }
        deleteSourceOnlyNodes(entry, sender);
        return;
    } else if (result.isBusy()) {
    } else if (result.isCriticalForMaintenance()) {
        LOGBP(warning, "Merging failed for %s: %s with error '%s'",
              getBucketId().toString().c_str(), msg->toString().c_str(), msg->getResult().toString().c_str());
    } else {
        LOG(debug, "Merge failed for %s with non-critical failure: %s",
            getBucketId().toString().c_str(), result.toString().c_str());
    }
    done();
}

namespace {

constexpr std::array<uint32_t, 7> WRITE_FEED_MESSAGE_TYPES {{
    api::MessageType::PUT_ID,
    api::MessageType::REMOVE_ID,
    api::MessageType::UPDATE_ID,
    api::MessageType::REMOVELOCATION_ID
}};

}

bool MergeOperation::shouldBlockThisOperation(uint32_t messageType, uint16_t node, uint8_t pri) const {
    for (auto blocking_type : WRITE_FEED_MESSAGE_TYPES) {
        if (messageType == blocking_type) {
            return true;
        }
    }

    return IdealStateOperation::shouldBlockThisOperation(messageType, node, pri);
}

bool MergeOperation::isBlocked(const DistributorStripeOperationContext& ctx,
                               const OperationSequencer& op_seq) const {
    // To avoid starvation of high priority global bucket merges, we do not consider
    // these for blocking due to a node being "busy" (usually caused by a full merge
    // throttler queue).
    //
    // This is for two reasons:
    //  1. When an ideal state op is blocked, it is still removed from the internal
    //     maintenance priority queue. This means a blocked high pri operation will
    //     not be retried until the next DB pass (at which point the node is likely
    //     to still be marked as busy when there's heavy merge traffic).
    //  2. Global bucket merges have high priority and will most likely be allowed
    //     to enter the merge throttler queues, displacing lower priority merges.
    if (!is_global_bucket_merge()) {
        const auto& node_info = ctx.pending_message_tracker().getNodeInfo();
        for (uint16_t node : getNodes()) {
            if (node_info.isBusy(node)) {
                return true;
            }
        }
    }
    return IdealStateOperation::isBlocked(ctx, op_seq);
}

bool MergeOperation::is_global_bucket_merge() const noexcept {
    return getBucket().getBucketSpace() == document::FixedBucketSpaces::global_space();
}

bool MergeOperation::all_involved_nodes_support_unordered_merge_chaining() const noexcept {
    const auto& features_repo = _manager->operation_context().node_supported_features_repo();
    for (uint16_t node : getNodes()) {
        if (!features_repo.node_supported_features(node).unordered_merge_chaining) {
            return false;
        }
    }
    return true;
}

MergeBucketMetricSet*
MergeOperation::get_merge_metrics()
{
    return (_manager)
        ? dynamic_cast<MergeBucketMetricSet *>(_manager->getMetrics().operations[getType()].get())
        : nullptr;
}

}