aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/top_level_distributor_test_util.cpp
blob: 6bbe7a47da2061cbd9ad12c43cf76c3b70c0cbf5 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "top_level_distributor_test_util.h"
#include <vespa/config-stor-distribution.h>
#include <vespa/document/test/make_bucket_space.h>
#include <vespa/document/test/make_document_bucket.h>
#include <vespa/storage/distributor/top_level_distributor.h>
#include <vespa/storage/distributor/distributor_bucket_space.h>
#include <vespa/storage/distributor/distributor_stripe.h>
#include <vespa/storage/distributor/distributor_stripe_component.h>
#include <vespa/storage/distributor/distributor_stripe_pool.h>
#include <vespa/storage/distributor/distributor_stripe_thread.h>
#include <vespa/storage/distributor/distributor_total_metrics.h>
#include <vespa/storage/storageutil/utils.h>
#include <vespa/storage/common/bucket_stripe_utils.h>
#include <vespa/vdslib/distribution/distribution.h>
#include <vespa/vespalib/text/stringtokenizer.h>

using document::test::makeBucketSpace;
using document::test::makeDocumentBucket;

namespace storage::distributor {

TopLevelDistributorTestUtil::TopLevelDistributorTestUtil()
    : _message_sender(_sender, _sender_down),
      _num_distributor_stripes(4)
{
    _config = getStandardConfig(false);
}

TopLevelDistributorTestUtil::~TopLevelDistributorTestUtil() = default;

void
TopLevelDistributorTestUtil::create_links()
{
    _node = std::make_unique<TestDistributorApp>(_config.getConfigId());
    _thread_pool = framework::TickingThreadPool::createDefault("distributor", 100ms);
    _stripe_pool = DistributorStripePool::make_non_threaded_pool_for_testing();
    _distributor.reset(new TopLevelDistributor(
            _node->getComponentRegister(),
            _node->node_identity(),
            *_thread_pool,
            *_stripe_pool,
            *this,
            _num_distributor_stripes,
            _host_info,
            &_message_sender));
    _component = std::make_unique<storage::DistributorComponent>(_node->getComponentRegister(), "distrtestutil");
};

void
TopLevelDistributorTestUtil::setup_distributor(int redundancy,
                                               int node_count,
                                               const std::string& cluster_state,
                                               uint32_t early_return,
                                               bool require_primary_to_be_written)
{
    setup_distributor(redundancy, node_count, lib::ClusterStateBundle(lib::ClusterState(cluster_state)),
                      early_return, require_primary_to_be_written);
}

void
TopLevelDistributorTestUtil::setup_distributor(int redundancy,
                                               int node_count,
                                               const lib::ClusterStateBundle& state,
                                               uint32_t early_return,
                                               bool require_primary_to_be_written)
{
    lib::Distribution::DistributionConfigBuilder config(
            lib::Distribution::getDefaultDistributionConfig(redundancy, node_count).get());
    config.redundancy = redundancy;
    config.initialRedundancy = early_return;
    config.ensurePrimaryPersisted = require_primary_to_be_written;
    auto distribution = std::make_shared<lib::Distribution>(config);
    _node->getComponentRegister().setDistribution(distribution);
    // This is for all intents and purposes a hack to avoid having the
    // distributor treat setting the distribution explicitly as a signal that
    // it should send RequestBucketInfo to all configured nodes.
    // If we called storage_distribution_changed followed by enableDistribution
    // explicitly (which is what happens in "real life"), that is what would
    // take place.
    // The inverse case of this can be explicitly accomplished by calling
    // triggerDistributionChange().
    // This isn't pretty, folks, but it avoids breaking the world for now,
    // as many tests have implicit assumptions about this being the behavior.
    _distributor->propagate_default_distribution_thread_unsafe(distribution);
    // Explicitly init the stripe pool since onOpen isn't called during testing
    _distributor->start_stripe_pool();
    enable_distributor_cluster_state(state);
}

size_t
TopLevelDistributorTestUtil::stripe_index_of_bucket(const document::BucketId& id) const noexcept
{
    return stripe_of_bucket_key(id.toKey(), _distributor->_n_stripe_bits);
}

size_t
TopLevelDistributorTestUtil::stripe_index_of_bucket(const document::Bucket& bucket) const noexcept
{
    return stripe_of_bucket_key(bucket.getBucketId().toKey(), _distributor->_n_stripe_bits);
}

void
TopLevelDistributorTestUtil::receive_set_system_state_command(const vespalib::string& state_str)
{
    auto state_cmd = std::make_shared<api::SetSystemStateCommand>(lib::ClusterState(state_str));
    handle_top_level_message(state_cmd); // TODO move semantics
}

bool
TopLevelDistributorTestUtil::handle_top_level_message(const std::shared_ptr<api::StorageMessage>& msg)
{
    return _distributor->onDown(msg);
}

void
TopLevelDistributorTestUtil::close()
{
    _component.reset();
    if (_distributor) {
        _stripe_pool->stop_and_join(); // Must be tagged as stopped prior to onClose
        _distributor->onClose();
    }
    _sender.clear();
    _node.reset();
    _config = getStandardConfig(false);
}

void
TopLevelDistributorTestUtil::add_nodes_to_stripe_bucket_db(const document::Bucket& bucket,
                                                           const std::string& nodeStr)
{
    BucketDatabase::Entry entry = get_bucket(bucket);

    if (!entry.valid()) {
        entry = BucketDatabase::Entry(bucket.getBucketId());
    }

    entry->clear();

    vespalib::StringTokenizer tokenizer(nodeStr, ",");
    for (uint32_t i = 0; i < tokenizer.size(); ++i) {
        vespalib::StringTokenizer tok2(tokenizer[i], "=");
        vespalib::StringTokenizer tok3(tok2[1], "/");

        api::BucketInfo info(atoi(tok3[0].data()),
                             atoi(tok3.size() > 1 ? tok3[1].data() : tok3[0].data()),
                             atoi(tok3.size() > 2 ? tok3[2].data() : tok3[0].data()));

        size_t flagsIdx = 3;

        // Meta info override? For simplicity, require both meta count and size
        if (tok3.size() > 4 && (!tok3[3].empty() && isdigit(tok3[3][0]))) {
            info.setMetaCount(atoi(tok3[3].data()));
            info.setUsedFileSize(atoi(tok3[4].data()));
            flagsIdx = 5;
        }

        if ((tok3.size() > flagsIdx + 1) && tok3[flagsIdx + 1] == "a") {
            info.setActive();
        } else {
            info.setActive(false);
        }
        if ((tok3.size() > flagsIdx + 2) && tok3[flagsIdx + 2] == "r") {
            info.setReady();
        } else {
            info.setReady(false);
        }

        uint16_t idx = atoi(tok2[0].data());
        BucketCopy node(0, idx, info);

        // Allow user to manually override trusted and active.
        if (tok3.size() > flagsIdx && tok3[flagsIdx] == "t") {
            node.setTrusted();
        }

        entry->addNodeManual(node);
    }

    stripe_bucket_database(stripe_index_of_bucket(bucket), bucket.getBucketSpace()).update(entry);
}

std::string
TopLevelDistributorTestUtil::get_ideal_str(document::BucketId id, const lib::ClusterState& state)
{
    if (!distributor_bucket_space(id).owns_bucket_in_state(state, id)) {
        return id.toString();
    }
    std::vector<uint16_t> nodes;
    _component->getDistribution()->getIdealNodes(lib::NodeType::STORAGE, state, id, nodes, "uim");
    std::sort(nodes.begin(), nodes.end());
    std::ostringstream ost;
    ost << id << ": " << dumpVector(nodes);
    return ost.str();
}

void
TopLevelDistributorTestUtil::add_ideal_nodes(const lib::ClusterState& state, const document::BucketId& id)
{
    BucketDatabase::Entry entry = get_bucket(id);

    if (!entry.valid()) {
        entry = BucketDatabase::Entry(id);
    }

    std::vector<uint16_t> res;
    assert(_component.get());
    _component->getDistribution()->getIdealNodes(lib::NodeType::STORAGE, state, id, res, "uim");

    for (uint32_t i = 0; i < res.size(); ++i) {
        if (state.getNodeState(lib::Node(lib::NodeType::STORAGE, res[i])).getState() != lib::State::MAINTENANCE) {
            entry->addNode(BucketCopy(0, res[i], api::BucketInfo(1,1,1)), toVector<uint16_t>(0));
        }
    }

    stripe_bucket_database(stripe_index_of_bucket(id)).update(entry);
}

void
TopLevelDistributorTestUtil::add_ideal_nodes(const document::BucketId& id)
{
    // TODO STRIPE good way of getting current active cluster state on top-level distributor
    // We assume that all stripes have the same cluster state internally, so just use the first.
    assert(_distributor->_stripes[0]);
    const auto& bundle = _distributor->_stripes[0]->getClusterStateBundle();
    add_ideal_nodes(*bundle.getBaselineClusterState(), id);
}

std::string
TopLevelDistributorTestUtil::get_nodes(document::BucketId id)
{
    BucketDatabase::Entry entry = get_bucket(id);

    if (!entry.valid()) {
        return id.toString();
    } else {
        std::vector<uint16_t> nodes = entry->getNodes();
        std::sort(nodes.begin(), nodes.end());

        std::ostringstream ost;
        ost << id << ": " << dumpVector(nodes);
        return ost.str();
    }
}

void
TopLevelDistributorTestUtil::add_nodes_to_stripe_bucket_db(const document::BucketId& id,
                                                           const std::string& nodeStr)
{
    add_nodes_to_stripe_bucket_db(document::Bucket(makeBucketSpace(), id), nodeStr);
}

BucketDatabase::Entry
TopLevelDistributorTestUtil::get_bucket(const document::Bucket& bucket) const
{
    return stripe_bucket_database(stripe_index_of_bucket(bucket), bucket.getBucketSpace()).get(bucket.getBucketId());
}

BucketDatabase::Entry
TopLevelDistributorTestUtil::get_bucket(const document::BucketId& bId) const
{
    return stripe_bucket_database(stripe_index_of_bucket(bId)).get(bId);
}

BucketSpaceStateMap&
TopLevelDistributorTestUtil::bucket_space_states() noexcept
{
    return _distributor->_component.bucket_space_states();
}

const BucketSpaceStateMap&
TopLevelDistributorTestUtil::bucket_space_states() const noexcept
{
    return _distributor->_component.bucket_space_states();
}

std::unique_ptr<StripeAccessGuard>
TopLevelDistributorTestUtil::acquire_stripe_guard()
{
    // Note: this won't actually interact with any threads, as the pool is running in single-threaded test mode.
    return _distributor->_stripe_accessor->rendezvous_and_hold_all();
}

TopLevelBucketDBUpdater&
TopLevelDistributorTestUtil::bucket_db_updater() {
    return *_distributor->_bucket_db_updater;
}

const IdealStateMetricSet&
TopLevelDistributorTestUtil::total_ideal_state_metrics() const
{
    assert(_distributor->_ideal_state_total_metrics);
    return *_distributor->_ideal_state_total_metrics;
}

const DistributorMetricSet&
TopLevelDistributorTestUtil::total_distributor_metrics() const
{
    assert(_distributor->_total_metrics);
    return *_distributor->_total_metrics;
}

DistributorBucketSpace&
TopLevelDistributorTestUtil::distributor_bucket_space(const document::BucketId& id)
{
    return stripe_of_bucket(id).getBucketSpaceRepo().get(makeBucketSpace());
}

const DistributorBucketSpace&
TopLevelDistributorTestUtil::distributor_bucket_space(const document::BucketId& id) const
{
    return stripe_of_bucket(id).getBucketSpaceRepo().get(makeBucketSpace());
}

DistributorStripe&
TopLevelDistributorTestUtil::stripe_of_bucket(const document::BucketId& id) noexcept
{
    return *_distributor->_stripes[stripe_index_of_bucket(id)];
}

const DistributorStripe&
TopLevelDistributorTestUtil::stripe_of_bucket(const document::BucketId& id) const noexcept
{
    return *_distributor->_stripes[stripe_index_of_bucket(id)];
}

DistributorStripe&
TopLevelDistributorTestUtil::stripe_of_bucket(const document::Bucket& bucket) noexcept
{
    return *_distributor->_stripes[stripe_index_of_bucket(bucket.getBucketId())];
}

const DistributorStripe&
TopLevelDistributorTestUtil::stripe_of_bucket(const document::Bucket& bucket) const noexcept
{
    return *_distributor->_stripes[stripe_index_of_bucket(bucket.getBucketId())];
}

bool
TopLevelDistributorTestUtil::tick(bool only_tick_top_level) {
    framework::ThreadWaitInfo res(
            framework::ThreadWaitInfo::NO_MORE_CRITICAL_WORK_KNOWN);
    {
        framework::TickingLockGuard lock(_distributor->_threadPool.freezeCriticalTicks());
        res.merge(_distributor->doCriticalTick(0));
    }
    res.merge(_distributor->doNonCriticalTick(0));
    bool did_work = !res.waitWanted();
    if (!only_tick_top_level) {
        for (auto& s : *_stripe_pool) {
            did_work |= s->stripe().tick();
        }
    }
    return did_work;
}

const DistributorConfig&
TopLevelDistributorTestUtil::current_distributor_config() const
{
    return _component->getDistributorConfig();
}

void
TopLevelDistributorTestUtil::reconfigure(const DistributorConfig& cfg)
{
    _node->getComponentRegister().setDistributorConfig(cfg);
    tick(); // Config is propagated upon next top-level tick
}

framework::MetricUpdateHook&
TopLevelDistributorTestUtil::distributor_metric_update_hook() {
    return _distributor->_metricUpdateHook;
}

BucketDatabase&
TopLevelDistributorTestUtil::stripe_bucket_database(uint16_t stripe_idx) {
    assert(stripe_idx < _distributor->_stripes.size());
    return _distributor->_stripes[stripe_idx]->getBucketSpaceRepo().get(makeBucketSpace()).getBucketDatabase();
}

BucketDatabase&
TopLevelDistributorTestUtil::stripe_bucket_database(uint16_t stripe_idx, document::BucketSpace space) {
    assert(stripe_idx < _distributor->_stripes.size());
    return _distributor->_stripes[stripe_idx]->getBucketSpaceRepo().get(space).getBucketDatabase();
}

const BucketDatabase&
TopLevelDistributorTestUtil::stripe_bucket_database(uint16_t stripe_idx) const {
    assert(stripe_idx < _distributor->_stripes.size());
    return _distributor->_stripes[stripe_idx]->getBucketSpaceRepo().get(makeBucketSpace()).getBucketDatabase();
}

const BucketDatabase&
TopLevelDistributorTestUtil::stripe_bucket_database(uint16_t stripe_idx, document::BucketSpace space) const {
    assert(stripe_idx < _distributor->_stripes.size());
    return _distributor->_stripes[stripe_idx]->getBucketSpaceRepo().get(space).getBucketDatabase();
}

// Hide how the sausages are made when directly accessing internal stripes
std::vector<DistributorStripe*>
TopLevelDistributorTestUtil::distributor_stripes() const {
    std::vector<DistributorStripe*> stripes;
    stripes.reserve(_distributor->_stripes.size());
    for (auto& s : _distributor->_stripes) {
        stripes.emplace_back(s.get());
    }
    return stripes;
}

bool
TopLevelDistributorTestUtil::all_distributor_stripes_are_in_recovery_mode() const {
    for (auto* s : distributor_stripes()) {
        if (!s->isInRecoveryMode()) {
            return false;
        }
    }
    return true;
}

void
TopLevelDistributorTestUtil::enable_distributor_cluster_state(vespalib::stringref state,
                                                              bool has_bucket_ownership_transfer)
{
    bucket_db_updater().simulate_cluster_state_bundle_activation(
            lib::ClusterStateBundle(lib::ClusterState(state)),
            has_bucket_ownership_transfer);
}

void
TopLevelDistributorTestUtil::enable_distributor_cluster_state(const lib::ClusterStateBundle& state)
{
    bucket_db_updater().simulate_cluster_state_bundle_activation(state);
}

std::vector<document::BucketSpace>
TopLevelDistributorTestUtil::bucket_spaces()
{
    return {document::FixedBucketSpaces::default_space(), document::FixedBucketSpaces::global_space()};
}

void
TopLevelDistributorTestUtil::trigger_distribution_change(std::shared_ptr<lib::Distribution> distr)
{
    _node->getComponentRegister().setDistribution(std::move(distr));
    _distributor->storageDistributionChanged();
    _distributor->enable_next_distribution_if_changed();
}

const lib::ClusterStateBundle&
TopLevelDistributorTestUtil::current_cluster_state_bundle() const
{
    // We assume that all stripes have the same cluster state internally, so just use the first.
    assert(_distributor->_stripes[0]);
    const auto& bundle = _distributor->_stripes[0]->getClusterStateBundle();
    // ... but sanity-check just to make sure...
    for (size_t i = 1; i < _num_distributor_stripes; ++i) {
        assert(_distributor->_stripes[i]->getClusterStateBundle() == bundle);
    }
    return bundle;
}

void
TopLevelDistributorTestUtil::tick_distributor_and_stripes_n_times(uint32_t n)
{
    for (uint32_t i = 0; i < n; ++i) {
        tick(false);
    }
}

void
TopLevelDistributorTestUtil::tick_top_level_distributor_n_times(uint32_t n)
{
    for (uint32_t i = 0; i < n; ++i) {
        tick(true);
    }
}

void
TopLevelDistributorTestUtil::complete_recovery_mode_on_all_stripes()
{
    for (auto* s : distributor_stripes()) {
        s->scanAllBuckets();
    }
}

}