-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathmount_registry.cpp
More file actions
199 lines (165 loc) · 5.1 KB
/
Copy pathmount_registry.cpp
File metadata and controls
199 lines (165 loc) · 5.1 KB
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
#include "mount_registry.h"
#include <cloud/storage/core/libs/common/error.h>
#include <cloud/storage/core/libs/common/task_queue.h>
#include <cloud/storage/core/libs/common/thread_pool.h>
#include <util/generic/algorithm.h>
namespace NCloud::NBlockStore::NStorage {
////////////////////////////////////////////////////////////////////////////////
TMountRegistry::TMountRegistry(TLog log)
: Log(std::move(log))
// a single thread on purpose, see the comment on the class
, Queue(CreateThreadPool("RDMA_REG", 1))
{}
TMountRegistry::~TMountRegistry() = default;
void TMountRegistry::Start()
{
Queue->Start();
}
void TMountRegistry::Stop()
{
Queue->Stop();
}
////////////////////////////////////////////////////////////////////////////////
void TMountRegistry::Enqueue(std::function<void()> update) noexcept
{
auto task = [Log = Log, update = std::move(update)]
{
// the thread pool runs the task in a noexcept context
auto error = SafeExecute<NProto::TError>(
[&]
{
update();
return NProto::TError{};
});
if (HasError(error)) {
STORAGE_WARN(
"Can't update the mount registry: %s",
FormatError(error).c_str());
}
};
// called from the transport threads, nothing may escape into them
auto error = SafeExecute<NProto::TError>(
[&]
{
Queue->ExecuteSimple(std::move(task));
return NProto::TError{};
});
if (HasError(error)) {
STORAGE_WARN(
"Can't enqueue a mount registry update: %s",
FormatError(error).c_str());
}
}
////////////////////////////////////////////////////////////////////////////////
void TMountRegistry::AddConnection(
ui64 sessionId,
TString peer,
TInstant startTs) noexcept
{
Enqueue(
[this, sessionId, peer = std::move(peer), startTs]
{ DoAddConnection(sessionId, peer, startTs); });
}
void TMountRegistry::RemoveConnection(ui64 sessionId) noexcept
{
Enqueue([this, sessionId] { DoRemoveConnection(sessionId); });
}
void TMountRegistry::AddMount(ui64 sessionId, TMountInfo info) noexcept
{
Enqueue(
[this, sessionId, info = std::move(info)]() mutable
{ DoAddMount(sessionId, std::move(info)); });
}
void TMountRegistry::RemoveMount(
ui64 sessionId,
TString diskId,
TString clientId) noexcept
{
Enqueue(
[this,
sessionId,
diskId = std::move(diskId),
clientId = std::move(clientId)]
{ DoRemoveMount(sessionId, diskId, clientId); });
}
////////////////////////////////////////////////////////////////////////////////
void TMountRegistry::DoAddConnection(
ui64 sessionId,
TString peer,
TInstant startTs)
{
with_lock (Lock) {
auto& connection = Connections[sessionId];
connection.SessionId = sessionId;
connection.Peer = std::move(peer);
connection.StartTs = startTs;
}
}
void TMountRegistry::DoRemoveConnection(ui64 sessionId)
{
with_lock (Lock) {
Connections.erase(sessionId);
}
}
void TMountRegistry::DoAddMount(ui64 sessionId, TMountInfo info)
{
with_lock (Lock) {
auto* connection = Connections.FindPtr(sessionId);
if (!connection) {
// the connection is announced before it can serve anything and
// forgotten only after everything it delivered has been answered,
// so there is no mount to record here - and recording one would
// bring a closed connection back for good
return;
}
auto it = FindIf(
connection->Mounts,
[&](const auto& mount)
{
return mount.DiskId == info.DiskId &&
mount.ClientId == info.ClientId;
});
if (it != connection->Mounts.end()) {
*it = std::move(info);
} else {
connection->Mounts.push_back(std::move(info));
}
}
}
void TMountRegistry::DoRemoveMount(
ui64 sessionId,
const TString& diskId,
const TString& clientId)
{
with_lock (Lock) {
auto* connection = Connections.FindPtr(sessionId);
if (!connection) {
return;
}
EraseIf(
connection->Mounts,
[&](const auto& mount)
{
return mount.DiskId == diskId && mount.ClientId == clientId;
});
}
}
TVector<TConnectionInfo> TMountRegistry::GetConnections() const
{
TVector<TConnectionInfo> result;
with_lock (Lock) {
result.reserve(Connections.size());
for (const auto& [sessionId, connection]: Connections) {
result.push_back(connection);
}
}
SortBy(result, [](const auto& connection) { return connection.StartTs; });
return result;
}
////////////////////////////////////////////////////////////////////////////////
TMountRegistryPtr CreateMountRegistry(ILoggingServicePtr logging)
{
return std::make_shared<TMountRegistry>(
logging->CreateLog("BLOCKSTORE_SERVER"));
}
} // namespace NCloud::NBlockStore::NStorage