aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/blobstorage/dsproxy/dsproxy_multiget.cpp
blob: 4337aa72cc12bb1e3f00f3022d17688dc8a35603 (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
#include "dsproxy.h"
#include "dsproxy_mon.h"

#include <ydb/core/blobstorage/base/wilson_events.h>
#include <ydb/core/blobstorage/vdisk/query/query_spacetracker.h>

#include <util/generic/set.h>

namespace NKikimr {

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GET request
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class TBlobStorageGroupMultiGetRequest : public TBlobStorageGroupRequestActor<TBlobStorageGroupMultiGetRequest> {
    struct TRequestInfo {
        ui64 BeginIdx;
        ui64 EndIdx;
        bool IsReplied;
    };

    const ui64 QuerySize;
    const TArrayHolder<TEvBlobStorage::TEvGet::TQuery> Queries;
    const TInstant Deadline;
    const bool IsInternal;
    TArrayHolder<TEvBlobStorage::TEvGetResult::TResponse> Responses;

    const TInstant StartTime;
    const bool MustRestoreFirst;
    const NKikimrBlobStorage::EGetHandleClass GetHandleClass;
    const ui32 ForceBlockedGeneration;

    static constexpr ui64 MaxRequestsInFlight = 3;
    ui64 RequestsInFlight = 0;
    std::deque<std::pair<std::unique_ptr<TEvBlobStorage::TEvGet>, ui64>> PendingGets;

    TStackVec<TRequestInfo, TypicalDisksInGroup> RequestInfos;

    void Handle(TEvBlobStorage::TEvGetResult::TPtr &ev) {
        RequestsInFlight--;

        WILSON_TRACE_FROM_ACTOR(*TlsActivationContext, *this, &TraceId, EvGetResultReceived, MergedNode = std::move(ev->TraceId));

        const TEvBlobStorage::TEvGetResult &res = *ev->Get();
        if (res.Status != NKikimrProto::OK) {
            R_LOG_ERROR_S("BPMG1", "Handle TEvGetResult status# " << NKikimrProto::EReplyStatus_Name(res.Status));
            ReplyAndDie(res.Status);
            return;
        }

        Y_VERIFY(ev->Cookie < RequestInfos.size()); 
        TRequestInfo &info = RequestInfos[ev->Cookie];
        Y_VERIFY(!info.IsReplied); 
        info.IsReplied = true;
        Y_VERIFY(res.ResponseSz == info.EndIdx - info.BeginIdx); 

        for (ui64 offset = 0; offset < res.ResponseSz; ++offset) {
            Responses[info.BeginIdx + offset] = res.Responses[offset];
        }

        SendRequests();
    }

    friend class TBlobStorageGroupRequestActor<TBlobStorageGroupMultiGetRequest>;
    void ReplyAndDie(NKikimrProto::EReplyStatus status) {
        std::unique_ptr<TEvBlobStorage::TEvGetResult> ev(new TEvBlobStorage::TEvGetResult(status, QuerySize, Info->GroupID));
        for (ui32 i = 0, e = QuerySize; i != e; ++i) {
            const TEvBlobStorage::TEvGet::TQuery &query = Queries[i];
            TEvBlobStorage::TEvGetResult::TResponse &x = ev->Responses[i];
            x.Status = NKikimrProto::UNKNOWN;
            x.Id = query.Id;
        }
        ev->ErrorReason = ErrorReason;
        Mon->CountGetResponseTime(Info->GetDeviceType(), GetHandleClass, ev->PayloadSizeBytes(), TActivationContext::Now() - StartTime);
        WILSON_TRACE_FROM_ACTOR(*TlsActivationContext, *this, &TraceId, MultiGetResultSent);
        Y_VERIFY(status != NKikimrProto::OK);
        SendResponseAndDie(std::move(ev));
    }

    std::unique_ptr<IEventBase> RestartQuery(ui32) {
        Y_FAIL();
    }

public:
    static constexpr NKikimrServices::TActivity::EType ActorActivityType() {
        return NKikimrServices::TActivity::BS_PROXY_MULTIGET_ACTOR;
    }

    static const auto& ActiveCounter(const TIntrusivePtr<TBlobStorageGroupProxyMon>& mon) {
        return mon->ActiveMultiGet;
    }

    TBlobStorageGroupMultiGetRequest(const TIntrusivePtr<TBlobStorageGroupInfo> &info,
            const TIntrusivePtr<TGroupQueues> &state, const TActorId &source,
            const TIntrusivePtr<TBlobStorageGroupProxyMon> &mon, TEvBlobStorage::TEvGet *ev, ui64 cookie,
            NWilson::TTraceId traceId, TMaybe<TGroupStat::EKind> latencyQueueKind, TInstant now,
            TIntrusivePtr<TStoragePoolCounters> &storagePoolCounters)
        : TBlobStorageGroupRequestActor(info, state, mon, source, cookie, std::move(traceId),
                NKikimrServices::BS_PROXY_MULTIGET, false, latencyQueueKind, now, storagePoolCounters, 0)
        , QuerySize(ev->QuerySize)
        , Queries(ev->Queries.Release())
        , Deadline(ev->Deadline)
        , IsInternal(ev->IsInternal)
        , Responses(new TEvBlobStorage::TEvGetResult::TResponse[QuerySize])
        , StartTime(now)
        , MustRestoreFirst(ev->MustRestoreFirst)
        , GetHandleClass(ev->GetHandleClass)
        , ForceBlockedGeneration(ev->ForceBlockedGeneration)
    {}

    void PrepareRequest(ui32 beginIdx, ui32 endIdx) {
        Y_VERIFY(endIdx > beginIdx);
        ui64 cookie = RequestInfos.size();
        RequestInfos.push_back({beginIdx, endIdx, false});
        TArrayHolder<TEvBlobStorage::TEvGet::TQuery> queries(new TEvBlobStorage::TEvGet::TQuery[endIdx - beginIdx]);
        for (ui32 idx = beginIdx; idx < endIdx; ++idx) {
            queries[idx - beginIdx] = Queries[idx];
        }
        auto ev = std::make_unique<TEvBlobStorage::TEvGet>(queries, endIdx - beginIdx, Deadline, GetHandleClass,
            MustRestoreFirst, false, ForceBlockedGeneration);
        ev->IsInternal = IsInternal;
        PendingGets.emplace_back(std::move(ev), cookie);
    }

    void SendRequests() {
        for (; RequestsInFlight < MaxRequestsInFlight && !PendingGets.empty(); ++RequestsInFlight, PendingGets.pop_front()) {
            auto& [ev, cookie] = PendingGets.front();
            WILSON_TRACE_FROM_ACTOR(*TlsActivationContext, *this, &TraceId, EvGetSent);
            SendToBSProxy(SelfId(), Info->GroupID, ev.release(), cookie, TraceId.SeparateBranch());
        }
        if (!RequestsInFlight && PendingGets.empty()) {
            auto ev = std::make_unique<TEvBlobStorage::TEvGetResult>(NKikimrProto::OK, 0, Info->GroupID);
            ev->ResponseSz = QuerySize;
            ev->Responses = std::move(Responses);
            Mon->CountGetResponseTime(Info->GetDeviceType(), GetHandleClass, ev->PayloadSizeBytes(), TActivationContext::Now() - StartTime);
            WILSON_TRACE_FROM_ACTOR(*TlsActivationContext, *this, &TraceId, MultiGetResultSent);
            SendResponseAndDie(std::move(ev));
        }
    }

    void Bootstrap() {
        WILSON_TRACE_FROM_ACTOR(*TlsActivationContext, *this, &TraceId, MultiGetReceived);

        auto dumpQuery = [this] {
            TStringStream str;
            str << "{";
            for (ui32 i = 0; i < QuerySize; ++i) {
                str << (i ? " " : "")
                    << Queries[i].Id
                    << "@" << Queries[i].Shift
                    << ":" << Queries[i].Size;
            }
            str << "}";
            return str.Str();
        };
        A_LOG_INFO_S("BPMG3", "bootstrap"
            << " ActorId# " << SelfId()
            << " Group# " << Info->GroupID
            << " Query# " << dumpQuery()
            << " Deadline# " << Deadline);

        Y_VERIFY(QuerySize != 0); // reply with error? 
        ui32 beginIdx = 0;
        TLogoBlobID lastBlobId;
        TQueryResultSizeTracker resultSize;
        resultSize.Init();

        for (ui32 queryIdx = 0; queryIdx < QuerySize; ++queryIdx) {
            const TEvBlobStorage::TEvGet::TQuery &query = Queries[queryIdx];
            if (lastBlobId == query.Id && queryIdx != 0) {
                continue;
            }
            resultSize.AddAllPartsOfLogoBlob(Info->Type, query.Id);

            if (queryIdx != beginIdx) {
                if (resultSize.IsOverflow() || queryIdx - beginIdx == 10000) {
                    PrepareRequest(beginIdx, queryIdx);
                    beginIdx = queryIdx;
                    resultSize.Init();
                    resultSize.AddAllPartsOfLogoBlob(Info->Type, query.Id);
                }
            }
        }
        PrepareRequest(beginIdx, QuerySize);

        SendRequests();

        Become(&TThis::StateWait);
    }

    STATEFN(StateWait) {
        if (ProcessEvent(ev)) {
            return;
        }
        switch (ev->GetTypeRewrite()) {
            hFunc(TEvBlobStorage::TEvGetResult, Handle);
        }
    }
};

IActor* CreateBlobStorageGroupMultiGetRequest(const TIntrusivePtr<TBlobStorageGroupInfo> &info,
        const TIntrusivePtr<TGroupQueues> &state, const TActorId &source,
        const TIntrusivePtr<TBlobStorageGroupProxyMon> &mon, TEvBlobStorage::TEvGet *ev,
        ui64 cookie, NWilson::TTraceId traceId, TMaybe<TGroupStat::EKind> latencyQueueKind,
        TInstant now, TIntrusivePtr<TStoragePoolCounters> &storagePoolCounters) {
    return new TBlobStorageGroupMultiGetRequest(info, state, source, mon, ev, cookie, std::move(traceId),
        latencyQueueKind, now, storagePoolCounters);
}

}//NKikimr