aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/ZooKeeper/ZooKeeperWithFaultInjection.h
blob: 4887e896e9b9752cc9b3c154bc7c8ec75c50c411 (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
#pragma once
#include <random>
#include <Common/ZooKeeper/KeeperException.h>
#include <Common/ZooKeeper/Types.h>
#include <Common/ZooKeeper/ZooKeeper.h>
#include <Common/ZooKeeper/ZooKeeperCommon.h>
#include <Common/logger_useful.h>
#include <Common/randomSeed.h>
#include "Coordination/KeeperConstants.h"

namespace DB
{

namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
}

class RandomFaultInjection
{
public:
    bool must_fail_after_op = false;
    bool must_fail_before_op = false;

    RandomFaultInjection(double probability, UInt64 seed_) : rndgen(seed_), distribution(probability) { }

    void beforeOperation()
    {
        if (distribution(rndgen) || must_fail_before_op)
        {
            must_fail_before_op = false;
            throw zkutil::KeeperException::fromMessage(Coordination::Error::ZSESSIONEXPIRED, "Fault injection before operation");
        }
    }
    void afterOperation()
    {
        if (distribution(rndgen) || must_fail_after_op)
        {
            must_fail_after_op = false;
            throw zkutil::KeeperException::fromMessage(Coordination::Error::ZOPERATIONTIMEOUT, "Fault injection after operation");
        }
    }

private:
    std::mt19937_64 rndgen;
    std::bernoulli_distribution distribution;
};

///
/// ZooKeeperWithFaultInjection mimics ZooKeeper interface and inject failures according to failure policy if set
///
class ZooKeeperWithFaultInjection
{
    template<bool async_insert>
    friend class ReplicatedMergeTreeSinkImpl;

    using zk = zkutil::ZooKeeper;

    zk::Ptr keeper;
    zk::Ptr keeper_prev;
    std::unique_ptr<RandomFaultInjection> fault_policy;
    std::string name;
    Poco::Logger * logger = nullptr;
    UInt64 calls_total = 0;
    UInt64 calls_without_fault_injection = 0;
    const UInt64 seed = 0;

    std::vector<std::string> ephemeral_nodes;

    ZooKeeperWithFaultInjection(
        zk::Ptr const & keeper_,
        double fault_injection_probability,
        UInt64 fault_injection_seed,
        std::string name_,
        Poco::Logger * logger_)
        : keeper(keeper_), name(std::move(name_)), logger(logger_), seed(fault_injection_seed)
    {
        fault_policy = std::make_unique<RandomFaultInjection>(fault_injection_probability, fault_injection_seed);

        if (unlikely(logger))
            LOG_TRACE(
                logger,
                "ZooKeeperWithFaultInjection created: name={} seed={} fault_probability={}",
                name,
                seed,
                fault_injection_probability);
    }

public:
    using Ptr = std::shared_ptr<ZooKeeperWithFaultInjection>;

    static ZooKeeperWithFaultInjection::Ptr createInstance(
        double fault_injection_probability, UInt64 fault_injection_seed, const zk::Ptr & zookeeper, std::string name, Poco::Logger * logger)
    {
        /// validate all parameters here, constructor just accept everything

        if (fault_injection_probability < 0.0)
            fault_injection_probability = .0;
        else if (fault_injection_probability > 1.0)
            fault_injection_probability = 1.0;

        if (0 == fault_injection_seed)
            fault_injection_seed = randomSeed();

        if (fault_injection_probability > 0.0)
            return std::shared_ptr<ZooKeeperWithFaultInjection>(
                new ZooKeeperWithFaultInjection(zookeeper, fault_injection_probability, fault_injection_seed, std::move(name), logger));

        /// if no fault injection provided, create instance which will not log anything
        return std::make_shared<ZooKeeperWithFaultInjection>(zookeeper);
    }

    explicit ZooKeeperWithFaultInjection(zk::Ptr const & keeper_) : keeper(keeper_) { }

    ~ZooKeeperWithFaultInjection()
    {
        if (unlikely(logger))
            LOG_TRACE(
                logger,
                "ZooKeeperWithFaultInjection report: name={} seed={} calls_total={} calls_succeeded={} calls_failed={} failure_rate={}",
                name,
                seed,
                calls_total,
                calls_without_fault_injection,
                calls_total - calls_without_fault_injection,
                float(calls_total - calls_without_fault_injection) / calls_total);
    }

    void setKeeper(zk::Ptr const & keeper_) { keeper = keeper_; }
    bool isNull() const { return keeper.get() == nullptr; }
    bool expired() { return keeper->expired(); }

    ///
    /// mirror ZooKeeper interface
    ///

    Strings getChildren(
        const std::string & path,
        Coordination::Stat * stat = nullptr,
        const zkutil::EventPtr & watch = nullptr,
        Coordination::ListRequestType list_request_type = Coordination::ListRequestType::ALL)
    {
        return access("getChildren", path, [&]() { return keeper->getChildren(path, stat, watch, list_request_type); });
    }

    Coordination::Error tryGetChildren(
        const std::string & path,
        Strings & res,
        Coordination::Stat * stat = nullptr,
        const zkutil::EventPtr & watch = nullptr,
        Coordination::ListRequestType list_request_type = Coordination::ListRequestType::ALL)
    {
        return access("tryGetChildren", path, [&]() { return keeper->tryGetChildren(path, res, stat, watch, list_request_type); });
    }

    zk::FutureExists asyncExists(const std::string & path, Coordination::WatchCallback watch_callback = {})
    {
        return access("asyncExists", path, [&]() { return keeper->asyncExists(path, watch_callback); });
    }

    zk::FutureGet asyncTryGet(const std::string & path)
    {
        return access("asyncTryGet", path, [&]() { return keeper->asyncTryGet(path); });
    }

    bool tryGet(
        const std::string & path,
        std::string & res,
        Coordination::Stat * stat = nullptr,
        const zkutil::EventPtr & watch = nullptr,
        Coordination::Error * code = nullptr)
    {
        return access("tryGet", path, [&]() { return keeper->tryGet(path, res, stat, watch, code); });
    }

    Coordination::Error tryMulti(const Coordination::Requests & requests, Coordination::Responses & responses)
    {
        constexpr auto method = "tryMulti";
        auto error = access(
            method,
            !requests.empty() ? requests.front()->getPath() : "",
            [&]() { return keeper->tryMulti(requests, responses); },
            [&](const Coordination::Error & original_error)
            {
                if (original_error == Coordination::Error::ZOK)
                    faultInjectionPostAction(method, requests, responses);
            },
            [&]()
            {
                responses.clear();
                for (size_t i = 0; i < requests.size(); ++i)
                    responses.emplace_back(std::make_shared<Coordination::ZooKeeperErrorResponse>());
            });


        /// collect ephemeral nodes when no fault was injected (to clean up on demand)
        if (unlikely(fault_policy) && Coordination::Error::ZOK == error)
        {
            doForEachCreatedEphemeralNode(
                method, requests, responses, [&](const String & path_created) { ephemeral_nodes.push_back(path_created); });
        }
        return error;
    }

    Coordination::Error tryMultiNoThrow(const Coordination::Requests & requests, Coordination::Responses & responses)
    {
        constexpr auto method = "tryMultiNoThrow";
        constexpr auto no_throw = true;
        constexpr auto inject_failure_before_op = false;
        auto error = access<no_throw, inject_failure_before_op>(
            method,
            !requests.empty() ? requests.front()->getPath() : "",
            [&]() { return keeper->tryMultiNoThrow(requests, responses); },
            [&](const Coordination::Error & original_error)
            {
                if (original_error == Coordination::Error::ZOK)
                    faultInjectionPostAction(method, requests, responses);
            },
            [&]()
            {
                responses.clear();
                for (size_t i = 0; i < requests.size(); ++i)
                    responses.emplace_back(std::make_shared<Coordination::ZooKeeperErrorResponse>());
            });

        /// collect ephemeral nodes when no fault was injected (to clean up later)
        if (unlikely(fault_policy) && Coordination::Error::ZOK == error)
        {
            doForEachCreatedEphemeralNode(
                method, requests, responses, [&](const String & path_created) { ephemeral_nodes.push_back(path_created); });
        }
        return error;
    }

    std::string get(const std::string & path, Coordination::Stat * stat = nullptr, const zkutil::EventPtr & watch = nullptr)
    {
        return access("get", path, [&]() { return keeper->get(path, stat, watch); });
    }

    zkutil::ZooKeeper::MultiGetResponse get(const std::vector<std::string> & paths)
    {
        return access("get", !paths.empty() ? paths.front() : "", [&]() { return keeper->get(paths); });
    }

    bool exists(const std::string & path, Coordination::Stat * stat = nullptr, const zkutil::EventPtr & watch = nullptr)
    {
        return access("exists", path, [&]() { return keeper->exists(path, stat, watch); });
    }

    bool existsNoFailureInjection(const std::string & path, Coordination::Stat * stat = nullptr, const zkutil::EventPtr & watch = nullptr)
    {
        return access<false, false, false>("exists", path, [&]() { return keeper->exists(path, stat, watch); });
    }

    zkutil::ZooKeeper::MultiExistsResponse exists(const std::vector<std::string> & paths)
    {
        return access("exists", !paths.empty() ? paths.front() : "", [&]() { return keeper->exists(paths); });
    }

    std::string create(const std::string & path, const std::string & data, int32_t mode)
    {
        std::string path_created;
        auto code = tryCreate(path, data, mode, path_created);

        if (code != Coordination::Error::ZOK)
            throw zkutil::KeeperException::fromPath(code, path);

        return path_created;
    }

    Coordination::Error tryCreate(const std::string & path, const std::string & data, int32_t mode, std::string & path_created)
    {
        path_created.clear();

        auto error = access(
            "tryCreate",
            path,
            [&]() { return keeper->tryCreate(path, data, mode, path_created); },
            [&](Coordination::Error & code)
            {
                try
                {
                    if (!path_created.empty() && (mode == zkutil::CreateMode::EphemeralSequential || mode == zkutil::CreateMode::Ephemeral))
                    {
                        keeper->remove(path_created);
                        if (unlikely(logger))
                            LOG_TRACE(logger, "ZooKeeperWithFaultInjection cleanup: seed={} func={} path={} path_created={} code={}",
                                seed, "tryCreate", path, path_created, code);
                    }
                }
                catch (const zkutil::KeeperException & e)
                {
                    if (unlikely(logger))
                        LOG_TRACE(
                            logger,
                            "ZooKeeperWithFaultInjection cleanup FAILED: seed={} func={} path={} path_created={} code={} message={} ",
                            seed,
                            "tryCreate",
                            path,
                            path_created,
                            e.code,
                            e.message());
                }
            });

        /// collect ephemeral nodes when no fault was injected (to clean up later)
        if (unlikely(fault_policy))
        {
            if (!path_created.empty() && (mode == zkutil::CreateMode::EphemeralSequential || mode == zkutil::CreateMode::Ephemeral))
                ephemeral_nodes.push_back(path_created);
        }

        return error;
    }

    Coordination::Error tryCreate(const std::string & path, const std::string & data, int32_t mode)
    {
        String path_created;
        return tryCreate(path, data, mode, path_created);
    }

    void createIfNotExists(const std::string & path, const std::string & data)
    {
        std::string path_created;
        auto code = tryCreate(path, data, zkutil::CreateMode::Persistent, path_created);

        if (code == Coordination::Error::ZOK || code == Coordination::Error::ZNODEEXISTS)
            return;

        throw zkutil::KeeperException::fromPath(code, path);
    }

    Coordination::Responses multi(const Coordination::Requests & requests)
    {
        constexpr auto method = "multi";
        auto result = access(
            method,
            !requests.empty() ? requests.front()->getPath() : "",
            [&]() { return keeper->multi(requests); },
            [&](Coordination::Responses & responses) { faultInjectionPostAction(method, requests, responses); });

        /// collect ephemeral nodes to clean up
        if (unlikely(fault_policy))
        {
            doForEachCreatedEphemeralNode(
                method, requests, result, [&](const String & path_created) { ephemeral_nodes.push_back(path_created); });
        }
        return result;
    }

    void createAncestors(const std::string & path)
    {
        access("createAncestors", path, [&]() { return keeper->createAncestors(path); });
    }

    Coordination::Error tryRemove(const std::string & path, int32_t version = -1)
    {
        return access("tryRemove", path, [&]() { return keeper->tryRemove(path, version); });
    }

    void removeRecursive(const std::string & path)
    {
        return access("removeRecursive", path, [&]() { return keeper->removeRecursive(path); });
    }

    std::string sync(const std::string & path)
    {
        return access("sync", path, [&]() { return keeper->sync(path); });
    }

    Coordination::Error trySet(const std::string & path, const std::string & data, int32_t version = -1, Coordination::Stat * stat = nullptr)
    {
        return access("trySet", path, [&]() { return keeper->trySet(path, data, version, stat); });
    }

    void checkExistsAndGetCreateAncestorsOps(const std::string & path, Coordination::Requests & requests)
    {
        return access("checkExistsAndGetCreateAncestorsOps", path, [&]() { return keeper->checkExistsAndGetCreateAncestorsOps(path, requests); });
    }

    void handleEphemeralNodeExistenceNoFailureInjection(const std::string & path, const std::string & fast_delete_if_equal_value)
    {
        return access<false, false, false>("handleEphemeralNodeExistence", path, [&]() { return keeper->handleEphemeralNodeExistence(path, fast_delete_if_equal_value); });
    }

    void cleanupEphemeralNodes()
    {
        for (const auto & path : ephemeral_nodes)
        {
            try
            {
                if (keeper_prev)
                    keeper_prev->tryRemove(path);
            }
            catch (...)
            {
                if (unlikely(logger))
                    tryLogCurrentException(logger, "Exception during ephemeral nodes clean up");
            }
        }

        ephemeral_nodes.clear();
    }

    bool isFeatureEnabled(KeeperFeatureFlag feature_flag) const
    {
        return keeper->isFeatureEnabled(feature_flag);
    }

private:
    void faultInjectionBefore(std::function<void()> fault_cleanup)
    {
        try
        {
            if (unlikely(fault_policy))
                fault_policy->beforeOperation();
        }
        catch (const zkutil::KeeperException &)
        {
            fault_cleanup();
            throw;
        }
    }
    void faultInjectionAfter(std::function<void()> fault_cleanup)
    {
        try
        {
            if (unlikely(fault_policy))
                fault_policy->afterOperation();
        }
        catch (const zkutil::KeeperException &)
        {
            fault_cleanup();
            throw;
        }
    }

    void doForEachCreatedEphemeralNode(
        const char * method, const Coordination::Requests & requests, const Coordination::Responses & responses, auto && action)
    {
        if (responses.empty())
            return;

        if (responses.size() != requests.size())
            throw Exception(
                ErrorCodes::LOGICAL_ERROR,
                "Number of responses doesn't match number of requests: method={} requests={} responses={}",
                method,
                requests.size(),
                responses.size());

        /// find create request with ephemeral flag
        std::vector<std::pair<size_t, const Coordination::CreateRequest *>> create_requests;
        for (size_t i = 0; i < requests.size(); ++i)
        {
            const auto * create_req = dynamic_cast<const Coordination::CreateRequest *>(requests[i].get());
            if (create_req && create_req->is_ephemeral)
                create_requests.emplace_back(i, create_req);
        }

        for (auto && [i, req] : create_requests)
        {
            const auto * create_resp = dynamic_cast<const Coordination::CreateResponse *>(responses.at(i).get());
            if (!create_resp)
                throw Exception(
                    ErrorCodes::LOGICAL_ERROR, "Response should be CreateResponse: method={} index={} path={}", method, i, req->path);

            action(create_resp->path_created);
        }
    }

    void faultInjectionPostAction(const char * method, const Coordination::Requests & requests, Coordination::Responses & responses)
    {
        doForEachCreatedEphemeralNode(method, requests, responses, [&](const String & path_created) { keeper->remove(path_created); });
    }

    template <typename T>
    struct FaultCleanupTypeImpl
    {
        using Type = std::function<void(T &)>;
    };

    template <>
    struct FaultCleanupTypeImpl<void>
    {
        using Type = std::function<void()>;
    };

    template <typename T>
    using FaultCleanupType = typename FaultCleanupTypeImpl<T>::Type;

    template <
        bool no_throw_access = false,
        bool inject_failure_before_op = true,
        int inject_failure_after_op = true,
        typename Operation,
        typename Result = std::invoke_result_t<Operation>>
    Result access(
        const char * func_name,
        const std::string & path,
        Operation operation,
        FaultCleanupType<Result> fault_after_op_cleanup = {},
        FaultCleanupType<void> fault_before_op_cleanup = {})
    {
        try
        {
            ++calls_total;

            if (!keeper)
                throw zkutil::KeeperException::fromMessage(Coordination::Error::ZSESSIONEXPIRED,
                    "Session is considered to be expired due to fault injection");

            if constexpr (inject_failure_before_op)
            {
                faultInjectionBefore(
                    [&]
                    {
                        if (fault_before_op_cleanup)
                            fault_before_op_cleanup();
                    });
            }

            if constexpr (!std::is_same_v<Result, void>)
            {
                Result res = operation();

                /// if connectivity error occurred w/o fault injection -> just return it
                if constexpr (std::is_same_v<Coordination::Error, Result>)
                {
                    if (Coordination::isHardwareError(res))
                        return res;
                }

                if constexpr (inject_failure_after_op)
                {
                    faultInjectionAfter(
                        [&]
                        {
                            if (fault_after_op_cleanup)
                                fault_after_op_cleanup(res);
                        });
                }

                ++calls_without_fault_injection;

                if (unlikely(logger))
                    LOG_TRACE(logger, "ZooKeeperWithFaultInjection call SUCCEEDED: seed={} func={} path={}", seed, func_name, path);

                return res;
            }
            else
            {
                operation();

                if constexpr (inject_failure_after_op)
                {
                    faultInjectionAfter(
                        [&fault_after_op_cleanup]
                        {
                            if (fault_after_op_cleanup)
                                fault_after_op_cleanup();
                        });
                }

                ++calls_without_fault_injection;

                if (unlikely(logger))
                    LOG_TRACE(logger, "ZooKeeperWithFaultInjection call SUCCEEDED: seed={} func={} path={}", seed, func_name, path);
            }
        }
        catch (const zkutil::KeeperException & e)
        {
            if (unlikely(logger))
                LOG_TRACE(
                    logger,
                    "ZooKeeperWithFaultInjection call FAILED: seed={} func={} path={} code={} message={} ",
                    seed,
                    func_name,
                    path,
                    e.code,
                    e.message());

            /// save valid pointer to clean up ephemeral nodes later if necessary
            if (keeper)
                keeper_prev = keeper;
            keeper.reset();

            /// for try*NoThrow() methods
            if constexpr (no_throw_access)
                return e.code;

            if constexpr (std::is_same_v<Coordination::Error, Result>)
            {
                /// try*() methods throws at least on hardware error and return only on user errors
                /// todo: the methods return only on subset of user errors, and throw on another errors
                ///       to mimic the methods exactly - we need to specify errors on which to return for each such method
                if (Coordination::isHardwareError(e.code))
                    throw;

                return e.code;
            }

            throw;
        }
    }
};

using ZooKeeperWithFaultInjectionPtr = ZooKeeperWithFaultInjection::Ptr;
}