aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/interconnect/ut_huge_cluster/huge_cluster.cpp
blob: cb46a62ed96a80b08f80aebce298207c4026597e (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
#include <library/cpp/actors/interconnect/ut/lib/ic_test_cluster.h>
#include <library/cpp/actors/interconnect/ut/lib/test_events.h>
#include <library/cpp/actors/interconnect/ut/lib/test_actors.h>

#include <library/cpp/testing/unittest/registar.h>

#include <vector>

Y_UNIT_TEST_SUITE(HugeCluster) {
    using namespace NActors;

    class TPoller: public TActor<TPoller> {
        const std::vector<TActorId>& Targets;
        std::unordered_map<TActorId, TManualEvent>& Connected;

    public:
        TPoller(const std::vector<TActorId>& targets, std::unordered_map<TActorId, TManualEvent>& events)
            : TActor(&TPoller::StateFunc)
            , Targets(targets)
            , Connected(events)
        {}

        void Handle(TEvTestStartPolling::TPtr /*ev*/, const TActorContext& ctx) {
            for (ui32 i = 0; i < Targets.size(); ++i) {
                ctx.Send(Targets[i], new TEvTest(), IEventHandle::FlagTrackDelivery, i);
            }
        }

        void Handle(TEvents::TEvUndelivered::TPtr ev, const TActorContext& ctx) {
            const ui32 cookie = ev->Cookie;
            // Cerr << "TEvUndelivered ping from node# " << SelfId().NodeId() << " to node# " << cookie + 1 << Endl;
            ctx.Send(Targets[cookie], new TEvTest(), IEventHandle::FlagTrackDelivery, cookie);
        }

        void Handle(TEvTest::TPtr ev, const TActorContext& /*ctx*/) {
            // Cerr << "Polled from " << ev->Sender.ToString() << Endl;
            Connected[ev->Sender].Signal();
        }

        void Handle(TEvents::TEvPoisonPill::TPtr& /*ev*/, const TActorContext& ctx) {
            Die(ctx);
        }

        STRICT_STFUNC(StateFunc,
            HFunc(TEvents::TEvUndelivered, Handle)
            HFunc(TEvTestStartPolling, Handle)
            HFunc(TEvTest, Handle)
            HFunc(TEvents::TEvPoisonPill, Handle)
        )
    };

    class TStartPollers : public TActorBootstrapped<TStartPollers> {
        const std::vector<TActorId>& Pollers;

    public:
        TStartPollers(const std::vector<TActorId>& pollers)
            : Pollers(pollers)
        {}

        void Bootstrap(const TActorContext& ctx) {
            Become(&TThis::StateFunc);
            for (ui32 i = 0; i < Pollers.size(); ++i) {
                ctx.Send(Pollers[i], new TEvTestStartPolling(), IEventHandle::FlagTrackDelivery, i);
            }
        }

        void Handle(TEvents::TEvUndelivered::TPtr ev, const TActorContext& ctx) {
            const ui32 cookie = ev->Cookie;
            // Cerr << "TEvUndelivered start poller message to node# " << cookie + 1 << Endl;
            ctx.Send(Pollers[cookie], new TEvTestStartPolling(), IEventHandle::FlagTrackDelivery, cookie);
        }

        void Handle(TEvents::TEvPoisonPill::TPtr& /*ev*/, const TActorContext& ctx) {
            Die(ctx);
        }

        STRICT_STFUNC(StateFunc,
            HFunc(TEvents::TEvUndelivered, Handle)
            HFunc(TEvents::TEvPoisonPill, Handle)
        )
    };

    TIntrusivePtr<NLog::TSettings> MakeLogConfigs(NLog::EPriority priority) {
        // custom logger settings
        auto loggerSettings = MakeIntrusive<NLog::TSettings>(
                TActorId(0, "logger"),
                NActorsServices::LOGGER,
                priority,
                priority,
                0U);

        loggerSettings->Append(
            NActorsServices::EServiceCommon_MIN,
            NActorsServices::EServiceCommon_MAX,
            NActorsServices::EServiceCommon_Name
        );

        constexpr ui32 WilsonComponentId = 430; // NKikimrServices::WILSON
        static const TString WilsonComponentName = "WILSON";

        loggerSettings->Append(
            (NLog::EComponent)WilsonComponentId,
            (NLog::EComponent)WilsonComponentId + 1,
            [](NLog::EComponent) -> const TString & { return WilsonComponentName; });

        return loggerSettings;
    }

    Y_UNIT_TEST(AllToAll) {
        ui32 nodesNum = 120;
        std::vector<TActorId> pollers(nodesNum);
        std::vector<std::unordered_map<TActorId, TManualEvent>> events(nodesNum);

        // Must destroy actor system before shared arrays
        {
            TTestICCluster testCluster(nodesNum, NActors::TChannelsConfig(), nullptr, MakeLogConfigs(NLog::PRI_EMERG));

            for (ui32 i = 0; i < nodesNum; ++i) {
                pollers[i] = testCluster.RegisterActor(new TPoller(pollers, events[i]), i + 1);
            }

            for (ui32 i = 0; i < nodesNum; ++i) {
                for (const auto& actor : pollers) {
                    events[i][actor] = TManualEvent();
                }
            }

            testCluster.RegisterActor(new TStartPollers(pollers), 1);

            for (ui32 i = 0; i < nodesNum; ++i) {
                for (auto& [_, ev] : events[i]) {
                    ev.WaitI();
                }
            }
        }
    }


    Y_UNIT_TEST(AllToOne) {
        ui32 nodesNum = 500;
        std::vector<TActorId> listeners;
        std::vector<TActorId> pollers(nodesNum - 1);
        std::unordered_map<TActorId, TManualEvent> events;
        std::unordered_map<TActorId, TManualEvent> emptyEventList;

        // Must destroy actor system before shared arrays
        {
            TTestICCluster testCluster(nodesNum, NActors::TChannelsConfig(), nullptr, MakeLogConfigs(NLog::PRI_EMERG));

            const TActorId listener = testCluster.RegisterActor(new TPoller({}, events), nodesNum);
            listeners = { listener };
            for (ui32 i = 0; i < nodesNum - 1; ++i) {
                pollers[i] = testCluster.RegisterActor(new TPoller(listeners, emptyEventList), i + 1);
            }

            for (const auto& actor : pollers) {
                events[actor] = TManualEvent();
            }

            testCluster.RegisterActor(new TStartPollers(pollers), 1);

            for (auto& [_, ev] : events) {
                ev.WaitI();
            }
        }
    }
}