aboutsummaryrefslogtreecommitdiffstats
path: root/kikimr/persqueue/sdk/deprecated/cpp/v2/ut_utils/data_writer.cpp
blob: 6f201f39f50c2cd7c8fb93d8983fdb96fa90f7da (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
#include "data_writer.h"

namespace NPersQueue::NTests {
using namespace NKikimr;

void TPQDataWriter::Read(
        const TString& topic, const TString& clientId, const TString& ticket, bool error, bool checkACL,
        bool useBatching, bool onlyCreate
) {

    grpc::ClientContext context;

    auto stream = StubP_->ReadSession(&context);
    UNIT_ASSERT(stream);

    // Send initial request.
    TReadRequest  req;
    TReadResponse resp;

    req.MutableInit()->AddTopics(topic);

    req.MutableInit()->SetClientId(clientId);
    req.MutableInit()->SetProxyCookie(ProxyCookie);
    if (!ticket.empty()) {
        req.MutableCredentials()->SetTvmServiceTicket(ticket);
    }

    if (useBatching) {
        req.MutableInit()->SetProtocolVersion(TReadRequest::Batching);
    }

    if (!stream->Write(req)) {
        ythrow yexception() << "write fail";
    }
    //TODO[komels]: why this leads to timeout?
    //Server.AnnoyingClient->GetClientInfo({topic}, clientId, true);
    UNIT_ASSERT(stream->Read(&resp));
    if (error) {
        UNIT_ASSERT(resp.HasError());
        return;
    }
    UNIT_ASSERT_C(resp.HasInit(), resp);

    if (onlyCreate)
        return;

    for (ui32 i = 0; i < 11; ++i) {
        TReadRequest req;

        req.MutableRead()->SetMaxCount(1);

        if (!stream->Write(req)) {
            ythrow yexception() << "write fail";
        }
        Server.AnnoyingClient->AlterTopic(FullTopicName, i < 10 ? 2 : 3);

    }

    if (checkACL) {
        NACLib::TDiffACL acl;
        acl.RemoveAccess(NACLib::EAccessType::Allow, NACLib::SelectRow, clientId + "@" BUILTIN_ACL_DOMAIN);
        Server.AnnoyingClient->ModifyACL("/Root/PQ", FullTopicName, acl.SerializeAsString());

        TReadRequest req;
        req.MutableRead()->SetMaxCount(1);
        if (!stream->Write(req)) {
            ythrow yexception() << "write fail";
        }

        UNIT_ASSERT(stream->Read(&resp));
        UNIT_ASSERT(resp.HasError() && resp.GetError().GetCode() == NPersQueue::NErrorCode::EErrorCode::ACCESS_DENIED);
        return;
    }
    Server.AnnoyingClient->GetClientInfo({FullTopicName}, clientId, true);
    for (ui32 i = 0; i < 11; ++i) {
        TReadResponse resp;

        UNIT_ASSERT(stream->Read(&resp));

        if (useBatching) {
            UNIT_ASSERT(resp.HasBatchedData());
            UNIT_ASSERT_VALUES_EQUAL(resp.GetBatchedData().PartitionDataSize(), 1);
            UNIT_ASSERT_VALUES_EQUAL(resp.GetBatchedData().GetPartitionData(0).BatchSize(), 1);
            UNIT_ASSERT_VALUES_EQUAL(resp.GetBatchedData().GetPartitionData(0).GetBatch(0).MessageDataSize(), 1);
            UNIT_ASSERT_VALUES_EQUAL(resp.GetBatchedData().GetPartitionData(0).GetBatch(0).GetMessageData(0).GetOffset(), i);
        } else {
            UNIT_ASSERT(resp.HasData());
            UNIT_ASSERT_VALUES_EQUAL(resp.GetData().MessageBatchSize(), 1);
            UNIT_ASSERT_VALUES_EQUAL(resp.GetData().GetMessageBatch(0).MessageSize(), 1);
            UNIT_ASSERT_VALUES_EQUAL(resp.GetData().GetMessageBatch(0).GetMessage(0).GetOffset(), i);
        }
    }
    //TODO: check here that read will never done        UNIT_ASSERT(!stream->Read(&resp));
    {
        for (ui32 i = 1; i < 11; ++i) {
            TReadRequest req;

            req.MutableCommit()->AddCookie(i);

            if (!stream->Write(req)) {
                ythrow yexception() << "write fail";
            }
        }
        ui32 i = 1;
        while (i <= 10) {
            TReadResponse resp;

            UNIT_ASSERT(stream->Read(&resp));
            Cerr << resp << "\n";
            UNIT_ASSERT(resp.HasCommit());
            UNIT_ASSERT(resp.GetCommit().CookieSize() > 0);
            for (ui32 j = 0; j < resp.GetCommit().CookieSize(); ++j) {
                UNIT_ASSERT( resp.GetCommit().GetCookie(j) == i);
                ++i;
                UNIT_ASSERT(i <= 11);
            }
        }
        Server.AnnoyingClient->GetClientInfo({FullTopicName}, clientId, true);
    }
}

void TPQDataWriter::WaitWritePQServiceInitialization() {
    TWriteRequest  req;
    TWriteResponse resp;
    while (true) {
        grpc::ClientContext context;

        auto stream = StubP_->WriteSession(&context);
        UNIT_ASSERT(stream);
        req.MutableInit()->SetTopic(ShortTopicName);
        req.MutableInit()->SetSourceId("12345678");
        req.MutableInit()->SetProxyCookie(ProxyCookie);

        if (!stream->Write(req)) {
            continue;
        }
        UNIT_ASSERT(stream->Read(&resp));
        if (resp.GetError().GetCode() == NPersQueue::NErrorCode::INITIALIZING) {
            Sleep(TDuration::MilliSeconds(50));
        } else {
            break;
        }
    }
}

ui32 TPQDataWriter::InitSession(const TString& sourceId, ui32 pg, bool success, ui32 step) {
    TWriteRequest  req;
    TWriteResponse resp;

    grpc::ClientContext context;

    auto stream = StubP_->WriteSession(&context);

    UNIT_ASSERT(stream);
    req.MutableInit()->SetTopic(ShortTopicName);
    req.MutableInit()->SetSourceId(sourceId);
    req.MutableInit()->SetPartitionGroup(pg);
    req.MutableInit()->SetProxyCookie(ProxyCookie);

    UNIT_ASSERT(stream->Write(req));
    UNIT_ASSERT(stream->Read(&resp));
    Cerr << "Init result: " << resp << "\n";
    //TODO: ensure topic creation - proxy already knows about new partitions, but tablet - no!
    if (!success) {
        UNIT_ASSERT(resp.HasError());
        return 0;
    } else {
        if (!resp.HasInit() && step < 5) {
            Sleep(TDuration::MilliSeconds(100));
            return InitSession(sourceId, pg, success, step + 1);
        }
        UNIT_ASSERT(resp.HasInit());
        return resp.GetInit().GetPartition();
    }
    return 0;
}

ui32 TPQDataWriter::WriteImpl(
        const TString& topic, const TVector<TString>& data, bool error, const TString& ticket, bool batch
) {
    grpc::ClientContext context;

    auto stream = StubP_->WriteSession(&context);
    UNIT_ASSERT(stream);

    // Send initial request.
    TWriteRequest  req;
    TWriteResponse resp;

    req.MutableInit()->SetTopic(topic);
    req.MutableInit()->SetSourceId(SourceId_);
    req.MutableInit()->SetProxyCookie(ProxyCookie);
    if (!ticket.empty())
        req.MutableCredentials()->SetTvmServiceTicket(ticket);
    auto item = req.MutableInit()->MutableExtraFields()->AddItems();
    item->SetKey("key");
    item->SetValue("value");

    if (!stream->Write(req)) {
        ythrow yexception() << "write fail";
    }

    ui32 part = 0;

    UNIT_ASSERT(stream->Read(&resp));

    if (!error) {
        UNIT_ASSERT_C(resp.HasInit(), resp);
        UNIT_ASSERT_C(!resp.GetInit().GetSessionId().empty(), resp);
        part = resp.GetInit().GetPartition();
    } else {
        Cerr << resp << "\n";
        UNIT_ASSERT(resp.HasError());
        return 0;
    }

    // Send data requests.
    Flush(data, stream, ticket, batch);

    Flush(data, stream, ticket, batch);

    Flush(data, stream, ticket, batch);

    Flush(data, stream, ticket, batch);

    //will cause only 4 answers in stream->Read - third call will fail, not blocks
    stream->WritesDone();

    UNIT_ASSERT(!stream->Read(&resp));

    auto status = stream->Finish();
    UNIT_ASSERT(status.ok());
    return part;
}

ui64 TPQDataWriter::ReadCookieFromMetadata(const std::multimap<grpc::string_ref, grpc::string_ref>& meta) const {
    auto ci = meta.find("cookie");
    if (ci == meta.end()) {
        ythrow yexception() << "server didn't provide the cookie";
    } else {
        return FromString<ui64>(TStringBuf(ci->second.data(), ci->second.size()));
    }
}

void TPQDataWriter::InitializeChannel() {
    Channel_ = grpc::CreateChannel("[::1]:" + ToString(Server.GrpcPort), grpc::InsecureChannelCredentials());
    Stub_ = NKikimrClient::TGRpcServer::NewStub(Channel_);

    grpc::ClientContext context;
    NKikimrClient::TChooseProxyRequest request;
    NKikimrClient::TResponse response;
    auto status = Stub_->ChooseProxy(&context, request, &response);
    UNIT_ASSERT(status.ok());
    Cerr << response << "\n";
    UNIT_ASSERT(response.GetStatus() == NMsgBusProxy::MSTATUS_OK);
    ProxyCookie = response.GetProxyCookie();
    Channel_ = grpc::CreateChannel(
            "[" + response.GetProxyName() + "]:" + ToString(Server.GrpcPort),
            grpc::InsecureChannelCredentials()
    );
    StubP_ = NPersQueue::PersQueueService::NewStub(Channel_);
}
} // namespace NKikimr::NPersQueueTests