aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/interconnect/interconnect_channel.cpp
blob: 9aee8fd5a2fd923742b369c3ff22e97acdd9fa47 (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
#include "interconnect_channel.h"

#include <library/cpp/actors/core/events.h>
#include <library/cpp/actors/core/executor_thread.h>
#include <library/cpp/actors/core/log.h>
#include <library/cpp/actors/core/probes.h> 
#include <library/cpp/actors/protos/services_common.pb.h>
#include <library/cpp/actors/prof/tag.h>
#include <library/cpp/digest/crc32c/crc32c.h>

LWTRACE_USING(ACTORLIB_PROVIDER); 
 
namespace NActors {
    DECLARE_WILSON_EVENT(EventSentToSocket);
    DECLARE_WILSON_EVENT(EventReceivedFromSocket);

    bool TEventOutputChannel::FeedDescriptor(TTcpPacketOutTask& task, TEventHolder& event, ui64 *weightConsumed) {
        const size_t amount = sizeof(TChannelPart) + sizeof(TEventDescr);
        if (task.GetVirtualFreeAmount() < amount) {
            return false;
        }

        NWilson::TTraceId traceId(event.Descr.TraceId);
//        if (ctx) {
//            WILSON_TRACE(*ctx, &traceId, EventSentToSocket);
//        }
        traceId.Serialize(&event.Descr.TraceId);
        LWTRACK(SerializeToPacketEnd, event.Orbit, PeerNodeId, ChannelId, OutputQueueSize, task.GetDataSize()); 
        task.Orbit.Take(event.Orbit); 

        event.Descr.Flags = (event.Descr.Flags & ~IEventHandle::FlagForwardOnNondelivery) |
            (ExtendedFormat ? IEventHandle::FlagExtendedFormat : 0);

        TChannelPart *part = static_cast<TChannelPart*>(task.GetFreeArea());
        part->Channel = ChannelId | TChannelPart::LastPartFlag;
        part->Size = sizeof(TEventDescr);
        memcpy(part + 1, &event.Descr, sizeof(TEventDescr));
        task.AppendBuf(part, amount);
        *weightConsumed += amount;
        OutputQueueSize -= part->Size;
        Metrics->UpdateOutputChannelEvents(ChannelId);

        return true;
    }

    void TEventOutputChannel::DropConfirmed(ui64 confirm) {
        LOG_DEBUG_IC_SESSION("ICOCH98", "Dropping confirmed messages");
        for (auto it = NotYetConfirmed.begin(); it != NotYetConfirmed.end() && it->Serial <= confirm; ) {
            Pool.Release(NotYetConfirmed, it++);
        }
    }

    bool TEventOutputChannel::FeedBuf(TTcpPacketOutTask& task, ui64 serial, ui64 *weightConsumed) {
        for (;;) {
            Y_VERIFY(!Queue.empty());
            TEventHolder& event = Queue.front();

            switch (State) {
                case EState::INITIAL:
                    event.InitChecksum();
                    LWTRACK(SerializeToPacketBegin, event.Orbit, PeerNodeId, ChannelId, OutputQueueSize); 
                    if (event.Event) {
                        State = EState::CHUNKER;
                        IEventBase *base = event.Event.Get();
                        Chunker.SetSerializingEvent(base);
                        ExtendedFormat = base->IsExtendedFormat();
                    } else if (event.Buffer) {
                        State = EState::BUFFER;
                        Iter = event.Buffer->GetBeginIter();
                        ExtendedFormat = event.Buffer->IsExtendedFormat();
                    } else {
                        State = EState::DESCRIPTOR;
                        ExtendedFormat = false;
                    }
                    break;

                case EState::CHUNKER:
                case EState::BUFFER: {
                    size_t maxBytes = task.GetVirtualFreeAmount();
                    if (maxBytes <= sizeof(TChannelPart)) {
                        return false;
                    }

                    TChannelPart *part = static_cast<TChannelPart*>(task.GetFreeArea());
                    part->Channel = ChannelId;
                    part->Size = 0;
                    task.AppendBuf(part, sizeof(TChannelPart));
                    maxBytes -= sizeof(TChannelPart);
                    Y_VERIFY(maxBytes);

                    auto addChunk = [&](const void *data, size_t len) {
                        event.UpdateChecksum(Params, data, len);
                        task.AppendBuf(data, len);
                        part->Size += len;
                        Y_VERIFY_DEBUG(maxBytes >= len);
                        maxBytes -= len;

                        event.EventActuallySerialized += len;
                        if (event.EventActuallySerialized > MaxSerializedEventSize) {
                            throw TExSerializedEventTooLarge(event.Descr.Type);
                        }
                    };

                    bool complete = false;
                    if (State == EState::CHUNKER) {
                        Y_VERIFY_DEBUG(task.GetFreeArea() == part + 1);
                        while (!complete && maxBytes) {
                            const auto [first, last] = Chunker.FeedBuf(task.GetFreeArea(), maxBytes);
                            for (auto p = first; p != last; ++p) {
                                addChunk(p->first, p->second);
                            }
                            complete = Chunker.IsComplete();
                        }
                        Y_VERIFY(!complete || Chunker.IsSuccessfull());
                        Y_VERIFY_DEBUG(complete || !maxBytes);
                    } else { // BUFFER
                        while (const size_t numb = Min(maxBytes, Iter.ContiguousSize())) {
                            const char *obuf = Iter.ContiguousData();
                            addChunk(obuf, numb);
                            Iter += numb;
                        }
                        complete = !Iter.Valid();
                    }
                    if (complete) {
                        Y_VERIFY(event.EventActuallySerialized == event.EventSerializedSize,
                            "EventActuallySerialized# %" PRIu32 " EventSerializedSize# %" PRIu32 " Type# 0x%08" PRIx32,
                            event.EventActuallySerialized, event.EventSerializedSize, event.Descr.Type);
                    }

                    if (!part->Size) {
                        task.Undo(sizeof(TChannelPart));
                    } else {
                        *weightConsumed += sizeof(TChannelPart) + part->Size;
                        OutputQueueSize -= part->Size;
                    }
                    if (complete) {
                        State = EState::DESCRIPTOR;
                    }
                    break;
                }

                case EState::DESCRIPTOR:
                    if (!FeedDescriptor(task, event, weightConsumed)) {
                        return false;
                    }
                    event.Serial = serial;
                    NotYetConfirmed.splice(NotYetConfirmed.end(), Queue, Queue.begin()); // move event to not-yet-confirmed queue
                    State = EState::INITIAL;
                    return true; // we have processed whole event, signal to the caller
            }
        }
    }

    void TEventOutputChannel::NotifyUndelivered() {
        LOG_DEBUG_IC_SESSION("ICOCH89", "Notyfying about Undelivered messages! NotYetConfirmed size: %zu, Queue size: %zu", NotYetConfirmed.size(), Queue.size());
        if (State == EState::CHUNKER) {
            Y_VERIFY(!Chunker.IsComplete()); // chunk must have an event being serialized
            Y_VERIFY(!Queue.empty()); // this event must be the first event in queue
            TEventHolder& event = Queue.front();
            Y_VERIFY(Chunker.GetCurrentEvent() == event.Event.Get()); // ensure the event is valid
            Chunker.Abort(); // stop serializing current event
            Y_VERIFY(Chunker.IsComplete());
        }
        for (auto& item : NotYetConfirmed) {
            if (item.Descr.Flags & IEventHandle::FlagGenerateUnsureUndelivered) { // notify only when unsure flag is set
                item.ForwardOnNondelivery(true);
            }
        }
        Pool.Release(NotYetConfirmed);
        for (auto& item : Queue) {
            item.ForwardOnNondelivery(false);
        }
        Pool.Release(Queue);
    }

}