aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/messqueue.cpp
blob: 9b55f9061dfc174c0fec660117046e4f7d6324c1 (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
#include "key_value_printer.h"
#include "mb_lwtrace.h"
#include "remote_client_session.h"
#include "remote_server_session.h"
#include "ybus.h"

#include <util/generic/singleton.h>

using namespace NBus;
using namespace NBus::NPrivate;
using namespace NActor;

TBusMessageQueuePtr NBus::CreateMessageQueue(const TBusQueueConfig& config, TExecutorPtr executor, TBusLocator* locator, const char* name) {
    return new TBusMessageQueue(config, executor, locator, name);
}

TBusMessageQueuePtr NBus::CreateMessageQueue(const TBusQueueConfig& config, TBusLocator* locator, const char* name) {
    TExecutor::TConfig executorConfig;
    executorConfig.WorkerCount = config.NumWorkers;
    executorConfig.Name = name;
    TExecutorPtr executor = new TExecutor(executorConfig);
    return CreateMessageQueue(config, executor, locator, name);
}

TBusMessageQueuePtr NBus::CreateMessageQueue(const TBusQueueConfig& config, const char* name) {
    return CreateMessageQueue(config, new TBusLocator, name);
}

TBusMessageQueuePtr NBus::CreateMessageQueue(TExecutorPtr executor, const char* name) {
    return CreateMessageQueue(TBusQueueConfig(), executor, new TBusLocator, name);
}

TBusMessageQueuePtr NBus::CreateMessageQueue(const char* name) {
    TBusQueueConfig config;
    return CreateMessageQueue(config, name);
}

namespace {
    TBusQueueConfig QueueConfigFillDefaults(const TBusQueueConfig& orig, const TString& name) {
        TBusQueueConfig patched = orig;
        if (!patched.Name) {
            patched.Name = name;
        }
        return patched;
    }
}

TBusMessageQueue::TBusMessageQueue(const TBusQueueConfig& config, TExecutorPtr executor, TBusLocator* locator, const char* name)
    : Config(QueueConfigFillDefaults(config, name))
    , Locator(locator)
    , WorkQueue(executor)
    , Running(1)
{
    InitBusLwtrace();
    InitNetworkSubSystem();
}

TBusMessageQueue::~TBusMessageQueue() {
    Stop();
}

void TBusMessageQueue::Stop() {
    if (!AtomicCas(&Running, 0, 1)) {
        ShutdownComplete.WaitI();
        return;
    }

    Scheduler.Stop();

    DestroyAllSessions();

    WorkQueue->Stop();

    ShutdownComplete.Signal();
}

bool TBusMessageQueue::IsRunning() {
    return AtomicGet(Running);
}

TBusMessageQueueStatus TBusMessageQueue::GetStatusRecordInternal() const {
    TBusMessageQueueStatus r;
    r.ExecutorStatus = WorkQueue->GetStatusRecordInternal();
    r.Config = Config;
    return r;
}

TString TBusMessageQueue::GetStatusSelf() const {
    return GetStatusRecordInternal().PrintToString();
}

TString TBusMessageQueue::GetStatusSingleLine() const {
    return WorkQueue->GetStatusSingleLine();
}

TString TBusMessageQueue::GetStatus(ui16 flags) const {
    TStringStream ss;

    ss << GetStatusSelf();

    TList<TIntrusivePtr<TBusSessionImpl>> sessions;
    {
        TGuard<TMutex> scope(Lock);
        sessions = Sessions;
    }

    for (TList<TIntrusivePtr<TBusSessionImpl>>::const_iterator session = sessions.begin();
         session != sessions.end(); ++session) {
        ss << Endl;
        ss << (*session)->GetStatus(flags);
    }

    ss << Endl;
    ss << "object counts (not necessarily owned by this message queue):" << Endl;
    TKeyValuePrinter p;
    p.AddRow("TRemoteClientConnection", TObjectCounter<TRemoteClientConnection>::ObjectCount(), false);
    p.AddRow("TRemoteServerConnection", TObjectCounter<TRemoteServerConnection>::ObjectCount(), false);
    p.AddRow("TRemoteClientSession", TObjectCounter<TRemoteClientSession>::ObjectCount(), false);
    p.AddRow("TRemoteServerSession", TObjectCounter<TRemoteServerSession>::ObjectCount(), false);
    p.AddRow("NEventLoop::TEventLoop", TObjectCounter<NEventLoop::TEventLoop>::ObjectCount(), false);
    p.AddRow("NEventLoop::TChannel", TObjectCounter<NEventLoop::TChannel>::ObjectCount(), false);
    ss << p.PrintToString();

    return ss.Str();
}

TBusClientSessionPtr TBusMessageQueue::CreateSource(TBusProtocol* proto, IBusClientHandler* handler, const TBusClientSessionConfig& config, const TString& name) {
    TRemoteClientSessionPtr session(new TRemoteClientSession(this, proto, handler, config, name));
    Add(session.Get());
    return session.Get();
}

TBusServerSessionPtr TBusMessageQueue::CreateDestination(TBusProtocol* proto, IBusServerHandler* handler, const TBusClientSessionConfig& config, const TString& name) {
    TRemoteServerSessionPtr session(new TRemoteServerSession(this, proto, handler, config, name));
    try {
        int port = config.ListenPort;
        if (port == 0) {
            port = Locator->GetLocalPort(proto->GetService());
        }
        if (port == 0) {
            port = proto->GetPort();
        }

        session->Listen(port, this);

        Add(session.Get());
        return session.Release();
    } catch (...) {
        Y_FAIL("create destination failure: %s", CurrentExceptionMessage().c_str());
    }
}

TBusServerSessionPtr TBusMessageQueue::CreateDestination(TBusProtocol* proto, IBusServerHandler* handler, const TBusServerSessionConfig& config, const TVector<TBindResult>& bindTo, const TString& name) {
    TRemoteServerSessionPtr session(new TRemoteServerSession(this, proto, handler, config, name));
    try {
        session->Listen(bindTo, this);
        Add(session.Get());
        return session.Release();
    } catch (...) {
        Y_FAIL("create destination failure: %s", CurrentExceptionMessage().c_str());
    }
}

void TBusMessageQueue::Add(TIntrusivePtr<TBusSessionImpl> session) {
    TGuard<TMutex> scope(Lock);
    Sessions.push_back(session);
}

void TBusMessageQueue::Remove(TBusSession* session) {
    TGuard<TMutex> scope(Lock);
    TList<TIntrusivePtr<TBusSessionImpl>>::iterator it = std::find(Sessions.begin(), Sessions.end(), session);
    Y_VERIFY(it != Sessions.end(), "do not destroy session twice"); 
    Sessions.erase(it);
}

void TBusMessageQueue::Destroy(TBusSession* session) {
    session->Shutdown();
}

void TBusMessageQueue::DestroyAllSessions() {
    TList<TIntrusivePtr<TBusSessionImpl>> sessions;
    {
        TGuard<TMutex> scope(Lock);
        sessions = Sessions;
    }

    for (auto& session : sessions) { 
        Y_VERIFY(session->IsDown(), "Session must be shut down prior to queue shutdown"); 
    }
}

void TBusMessageQueue::Schedule(IScheduleItemAutoPtr i) {
    Scheduler.Schedule(i);
}

TString TBusMessageQueue::GetNameInternal() const {
    return Config.Name;
}