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
|
#pragma once
#include "msgbus_server.h"
#include <ydb/public/lib/base/msgbus.h>
#include <ydb/core/base/tablet.h>
#include <ydb/core/base/tablet_pipe.h>
#include <library/cpp/actors/core/actor_bootstrapped.h>
#include <library/cpp/actors/core/log.h>
#include <library/cpp/actors/core/hfunc.h>
#include <ydb/core/protos/services.pb.h>
#include <util/generic/hash_set.h>
#include <util/generic/vector.h>
#define CHECK_PROTOBUF_FIELD_PRESENCE(pb, field, errslist) \
if (!pb.Has##field()) { \
errslist.push_back( #field );\
}
namespace NKikimr {
namespace NMsgBusProxy {
template<typename TDerived, typename TTabletReplyEvent>
class TMessageBusTabletRequest : public TActorBootstrapped<TDerived>, public TMessageBusSessionIdentHolder {
protected:
const TDuration Timeout;
const bool WithRetry;
const bool ConnectToFollower;
private:
TActorId PipeClient;
ui64 TabletId = 0;
void Handle(TEvTabletPipe::TEvClientConnected::TPtr &ev, const TActorContext &ctx) {
TEvTabletPipe::TEvClientConnected *msg = ev->Get();
if (msg->Status != NKikimrProto::OK) {
PipeClient = TActorId();
return SendReplyAndDie(CreateErrorReply(MSTATUS_ERROR, ctx,
Sprintf("Tablet pipe client connected with status# %s for tablet %" PRIu64 " Marker# MBT3",
NKikimrProto::EReplyStatus_Name(msg->Status).data(), msg->TabletId)), ctx);
}
}
void Handle(TEvTabletPipe::TEvClientDestroyed::TPtr &ev, const TActorContext &ctx) {
Y_UNUSED(ev);
PipeClient = TActorId();
SendReplyMove(CreateErrorReply(MSTATUS_ERROR, ctx, "Tablet pipe client destroyed Marker# MBT2"));
return Die(ctx);
}
void HandleTimeout(const TActorContext &ctx) {
return SendReplyAndDie(CreateErrorReply(MSTATUS_TIMEOUT, ctx, "Tablet request timed out Marker# MBT4"), ctx);
}
protected:
void Die(const TActorContext &ctx) override {
if (PipeClient) {
NTabletPipe::CloseClient(ctx, PipeClient);
PipeClient = TActorId();
}
TActorBootstrapped<TDerived>::Die(ctx);
}
virtual NBus::TBusMessage* CreateErrorReply(EResponseStatus status, const TActorContext &ctx,
const TString& text = TString()) {
LOG_ERROR_S(ctx, NKikimrServices::MSGBUS_REQUEST, "TabletRequest TabletId# " << TabletId
<< " status# " << status << " text# \"" << text << "\"" << Endl);
return new TBusResponseStatus(status, text);
}
void SendReplyAndDie(NBus::TBusMessage *reply, const TActorContext &ctx) {
SendReplyMove(reply);
return Die(ctx);
}
TMessageBusTabletRequest(TBusMessageContext &msg, bool withRetry, TDuration timeout, bool connectToFollower)
: TMessageBusSessionIdentHolder(msg)
, Timeout(timeout)
, WithRetry(withRetry)
, ConnectToFollower(connectToFollower)
{
}
public:
static constexpr NKikimrServices::TActivity::EType ActorActivityType() {
return NKikimrServices::TActivity::MSGBUS_COMMON;
}
void Bootstrap(const TActorContext &ctx) {
NTabletPipe::TClientConfig clientConfig;
if (WithRetry) {
clientConfig.RetryPolicy = NTabletPipe::TClientRetryPolicy::WithRetries();
}
if (ConnectToFollower) {
clientConfig.AllowFollower = true;
clientConfig.ForceFollower = true;
}
std::pair<ui64, TAutoPtr<IEventBase>> reqPair = static_cast<TDerived *>(this)->MakeReqPair(ctx);
TabletId = reqPair.first;
if (reqPair.first) {
PipeClient = ctx.RegisterWithSameMailbox(NTabletPipe::CreateClient(ctx.SelfID, reqPair.first, clientConfig));
NTabletPipe::SendData(ctx, PipeClient, reqPair.second.Release());
this->Become(&TDerived::StateFunc, ctx, Timeout, new TEvents::TEvWakeup());
} else {
return SendReplyAndDie(CreateErrorReply(MSTATUS_ERROR, ctx,
"Unable to obtain TabletId Marker# MBT1"), ctx);
}
}
STFUNC(StateFunc) {
switch (ev->GetTypeRewrite()) {
HTemplFunc(TTabletReplyEvent, static_cast<TDerived *>(this)->Handle);
HFunc(TEvTabletPipe::TEvClientDestroyed, Handle);
HFunc(TEvTabletPipe::TEvClientConnected, Handle);
CFunc(TEvents::TSystem::Wakeup, HandleTimeout);
}
}
};
template<typename TDerived, typename TTabletReplyEvent, NKikimrServices::TActivity::EType Activity>
class TMessageBusSimpleTabletRequest : public TMessageBusTabletRequest<TDerived, TTabletReplyEvent> {
protected:
const ui64 TabletID;
TMessageBusSimpleTabletRequest(TBusMessageContext &msg, ui64 tabletId, bool withRetry, TDuration timeout, bool connectToFollower)
: TMessageBusTabletRequest<TDerived, TTabletReplyEvent>(msg, withRetry, timeout, connectToFollower)
, TabletID(tabletId)
{}
public:
std::pair<ui64, TAutoPtr<IEventBase>> MakeReqPair(const TActorContext &ctx) {
return std::pair<ui64, TAutoPtr<IEventBase>>(TabletID, static_cast<TDerived *>(this)->MakeReq(ctx));
}
static constexpr auto ActorActivityType() {
return Activity;
}
};
}
}
|