aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/interconnect/ut/dynamic_proxy_ut.cpp
blob: 3c474979dce930e503f00d6041c9219fc3345738 (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
#include <library/cpp/actors/interconnect/ut/lib/node.h>
#include <library/cpp/actors/interconnect/ut/lib/ic_test_cluster.h>
#include <library/cpp/testing/unittest/registar.h>

TActorId MakeResponderServiceId(ui32 nodeId) {
    return TActorId(nodeId, TStringBuf("ResponderAct", 12));
}

class TArriveQueue {
    struct TArrivedItem {
        ui32 QueueId;
        ui32 Index;
        bool Success;
    };

    TMutex Lock;
    std::size_t Counter = 0;
    std::vector<TArrivedItem> Items;

public:
    TArriveQueue(size_t capacity)
        : Items(capacity)
    {}

    bool Done() const {
        with_lock (Lock) {
            return Counter == Items.size();
        }
    }

    void Push(ui64 cookie, bool success) {
        with_lock (Lock) {
            const size_t pos = Counter++;
            TArrivedItem item{.QueueId = static_cast<ui32>(cookie >> 32), .Index = static_cast<ui32>(cookie & 0xffff'ffff),
                .Success = success};
            memcpy(&Items[pos], &item, sizeof(TArrivedItem));
        }
    }

    void Check() {
        struct TPerQueueState {
            std::vector<ui32> Ok, Error;
        };
        std::unordered_map<ui32, TPerQueueState> state;
        for (const TArrivedItem& item : Items) {
            auto& st = state[item.QueueId];
            auto& v = item.Success ? st.Ok : st.Error;
            v.push_back(item.Index);
        }
        for (const auto& [queueId, st] : state) {
            ui32 expected = 0;
            for (const ui32 index : st.Ok) {
                Y_VERIFY(index == expected);
                ++expected;
            }
            for (const ui32 index : st.Error) {
                Y_VERIFY(index == expected);
                ++expected;
            }
            if (st.Error.size()) {
                Cerr << "Error.size# " << st.Error.size() << Endl;
            }
        }
    }
};

class TResponder : public TActor<TResponder> {
    TArriveQueue& ArriveQueue;

public:
    TResponder(TArriveQueue& arriveQueue)
        : TActor(&TResponder::StateFunc)
        , ArriveQueue(arriveQueue)
    {}

    STRICT_STFUNC(StateFunc,
        hFunc(TEvents::TEvPing, Handle);
    )

    void Handle(TEvents::TEvPing::TPtr ev) {
        ArriveQueue.Push(ev->Cookie, true);
    }
};

class TSender : public TActor<TSender> {
    TArriveQueue& ArriveQueue;

public:
    TSender(TArriveQueue& arriveQueue)
        : TActor(&TThis::StateFunc)
        , ArriveQueue(arriveQueue)
    {}

    STRICT_STFUNC(StateFunc,
        hFunc(TEvents::TEvUndelivered, Handle);
    )

    void Handle(TEvents::TEvUndelivered::TPtr ev) {
        ArriveQueue.Push(ev->Cookie, false);
    }
};

void SenderThread(TMutex& lock, TActorSystem *as, ui32 nodeId, ui32 queueId, ui32 count, TArriveQueue& arriveQueue) {
    const TActorId sender = as->Register(new TSender(arriveQueue));
    with_lock(lock) {}
    const TActorId target = MakeResponderServiceId(nodeId);
    for (ui32 i = 0; i < count; ++i) {
        const ui32 flags = IEventHandle::FlagTrackDelivery;
        as->Send(new IEventHandle(TEvents::THelloWorld::Ping, flags, target, sender, nullptr, ((ui64)queueId << 32) | i));
    }
}

void RaceTestIter(ui32 numThreads, ui32 count) {
    TPortManager portman;
    THashMap<ui32, ui16> nodeToPort;
    const ui32 numNodes = 6; // total
    const ui32 numDynamicNodes = 3;
    for (ui32 i = 1; i <= numNodes; ++i) {
        nodeToPort.emplace(i, portman.GetPort());
    }

    NMonitoring::TDynamicCounterPtr counters = new NMonitoring::TDynamicCounters;
    std::list<TNode> nodes;
    for (ui32 i = 1; i <= numNodes; ++i) {
        nodes.emplace_back(i, numNodes, nodeToPort, "127.1.0.0", counters->GetSubgroup("nodeId", TStringBuilder() << i),
            TDuration::Seconds(10), TChannelsConfig(), numDynamicNodes, numThreads);
    }

    const ui32 numSenders = 10;
    TArriveQueue arriveQueue(numSenders * numNodes * (numNodes - 1) * count);
    for (TNode& node : nodes) {
        node.RegisterServiceActor(MakeResponderServiceId(node.GetActorSystem()->NodeId), new TResponder(arriveQueue));
    }

    TMutex lock;
    std::list<TThread> threads;
    ui32 queueId = 0;
    with_lock(lock) {
        for (TNode& from : nodes) {
            for (ui32 toId = 1; toId <= numNodes; ++toId) {
                if (toId == from.GetActorSystem()->NodeId) {
                    continue;
                }
                for (ui32 i = 0; i < numSenders; ++i) {
                    threads.emplace_back([=, &lock, &from, &arriveQueue] {
                        SenderThread(lock, from.GetActorSystem(), toId, queueId, count, arriveQueue);
                    });
                    ++queueId;
                }
            }
        }
        for (auto& thread : threads) {
            thread.Start();
        }
    }
    for (auto& thread : threads) {
        thread.Join();
    }

    for (THPTimer timer; !arriveQueue.Done(); TDuration::MilliSeconds(10)) {
        Y_VERIFY(timer.Passed() < 10);
    }

    nodes.clear();
    arriveQueue.Check();
}

Y_UNIT_TEST_SUITE(DynamicProxy) {
    Y_UNIT_TEST(RaceCheck1) {
        for (ui32 iteration = 0; iteration < 100; ++iteration) {
            RaceTestIter(1 + iteration % 5, 1);
        }
    }
    Y_UNIT_TEST(RaceCheck10) {
        for (ui32 iteration = 0; iteration < 100; ++iteration) {
            RaceTestIter(1 + iteration % 5, 10);
        }
    }
}