|
| 1 | +#include "mount_registry.h" |
| 2 | + |
| 3 | +#include <cloud/storage/core/libs/common/error.h> |
| 4 | +#include <cloud/storage/core/libs/common/task_queue.h> |
| 5 | +#include <cloud/storage/core/libs/common/thread_pool.h> |
| 6 | + |
| 7 | +#include <util/generic/algorithm.h> |
| 8 | + |
| 9 | +namespace NCloud::NBlockStore::NStorage { |
| 10 | + |
| 11 | +//////////////////////////////////////////////////////////////////////////////// |
| 12 | + |
| 13 | +TMountRegistry::TMountRegistry(TLog log) |
| 14 | + : Log(std::move(log)) |
| 15 | + // a single thread on purpose, see the comment on the class |
| 16 | + , Queue(CreateThreadPool("RDMA_REG", 1)) |
| 17 | +{} |
| 18 | + |
| 19 | +TMountRegistry::~TMountRegistry() = default; |
| 20 | + |
| 21 | +void TMountRegistry::Start() |
| 22 | +{ |
| 23 | + Queue->Start(); |
| 24 | +} |
| 25 | + |
| 26 | +void TMountRegistry::Stop() |
| 27 | +{ |
| 28 | + Queue->Stop(); |
| 29 | +} |
| 30 | + |
| 31 | +//////////////////////////////////////////////////////////////////////////////// |
| 32 | + |
| 33 | +void TMountRegistry::Enqueue(std::function<void()> update) noexcept |
| 34 | +{ |
| 35 | + auto task = [Log = Log, update = std::move(update)] |
| 36 | + { |
| 37 | + // the thread pool runs the task in a noexcept context |
| 38 | + auto error = SafeExecute<NProto::TError>( |
| 39 | + [&] |
| 40 | + { |
| 41 | + update(); |
| 42 | + return NProto::TError{}; |
| 43 | + }); |
| 44 | + |
| 45 | + if (HasError(error)) { |
| 46 | + STORAGE_WARN( |
| 47 | + "Can't update the mount registry: %s", |
| 48 | + FormatError(error).c_str()); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + // called from the transport threads, nothing may escape into them |
| 53 | + auto error = SafeExecute<NProto::TError>( |
| 54 | + [&] |
| 55 | + { |
| 56 | + Queue->ExecuteSimple(std::move(task)); |
| 57 | + return NProto::TError{}; |
| 58 | + }); |
| 59 | + |
| 60 | + if (HasError(error)) { |
| 61 | + STORAGE_WARN( |
| 62 | + "Can't enqueue a mount registry update: %s", |
| 63 | + FormatError(error).c_str()); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +//////////////////////////////////////////////////////////////////////////////// |
| 68 | + |
| 69 | +void TMountRegistry::AddConnection( |
| 70 | + ui64 sessionId, |
| 71 | + TString peer, |
| 72 | + TInstant startTs) noexcept |
| 73 | +{ |
| 74 | + Enqueue( |
| 75 | + [this, sessionId, peer = std::move(peer), startTs] |
| 76 | + { DoAddConnection(sessionId, peer, startTs); }); |
| 77 | +} |
| 78 | + |
| 79 | +void TMountRegistry::RemoveConnection(ui64 sessionId) noexcept |
| 80 | +{ |
| 81 | + Enqueue([this, sessionId] { DoRemoveConnection(sessionId); }); |
| 82 | +} |
| 83 | + |
| 84 | +void TMountRegistry::AddMount(ui64 sessionId, TMountInfo info) noexcept |
| 85 | +{ |
| 86 | + Enqueue( |
| 87 | + [this, sessionId, info = std::move(info)]() mutable |
| 88 | + { DoAddMount(sessionId, std::move(info)); }); |
| 89 | +} |
| 90 | + |
| 91 | +void TMountRegistry::RemoveMount( |
| 92 | + ui64 sessionId, |
| 93 | + TString diskId, |
| 94 | + TString clientId) noexcept |
| 95 | +{ |
| 96 | + Enqueue( |
| 97 | + [this, |
| 98 | + sessionId, |
| 99 | + diskId = std::move(diskId), |
| 100 | + clientId = std::move(clientId)] |
| 101 | + { DoRemoveMount(sessionId, diskId, clientId); }); |
| 102 | +} |
| 103 | + |
| 104 | +//////////////////////////////////////////////////////////////////////////////// |
| 105 | + |
| 106 | +void TMountRegistry::DoAddConnection( |
| 107 | + ui64 sessionId, |
| 108 | + TString peer, |
| 109 | + TInstant startTs) |
| 110 | +{ |
| 111 | + with_lock (Lock) { |
| 112 | + auto& connection = Connections[sessionId]; |
| 113 | + connection.SessionId = sessionId; |
| 114 | + connection.Peer = std::move(peer); |
| 115 | + connection.StartTs = startTs; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void TMountRegistry::DoRemoveConnection(ui64 sessionId) |
| 120 | +{ |
| 121 | + with_lock (Lock) { |
| 122 | + Connections.erase(sessionId); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void TMountRegistry::DoAddMount(ui64 sessionId, TMountInfo info) |
| 127 | +{ |
| 128 | + with_lock (Lock) { |
| 129 | + auto* connection = Connections.FindPtr(sessionId); |
| 130 | + if (!connection) { |
| 131 | + // the connection is announced before it can serve anything and |
| 132 | + // forgotten only after everything it delivered has been answered, |
| 133 | + // so there is no mount to record here - and recording one would |
| 134 | + // bring a closed connection back for good |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + auto it = FindIf( |
| 139 | + connection->Mounts, |
| 140 | + [&](const auto& mount) |
| 141 | + { |
| 142 | + return mount.DiskId == info.DiskId && |
| 143 | + mount.ClientId == info.ClientId; |
| 144 | + }); |
| 145 | + |
| 146 | + if (it != connection->Mounts.end()) { |
| 147 | + *it = std::move(info); |
| 148 | + } else { |
| 149 | + connection->Mounts.push_back(std::move(info)); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +void TMountRegistry::DoRemoveMount( |
| 155 | + ui64 sessionId, |
| 156 | + const TString& diskId, |
| 157 | + const TString& clientId) |
| 158 | +{ |
| 159 | + with_lock (Lock) { |
| 160 | + auto* connection = Connections.FindPtr(sessionId); |
| 161 | + if (!connection) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + EraseIf( |
| 166 | + connection->Mounts, |
| 167 | + [&](const auto& mount) |
| 168 | + { |
| 169 | + return mount.DiskId == diskId && mount.ClientId == clientId; |
| 170 | + }); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +TVector<TConnectionInfo> TMountRegistry::GetConnections() const |
| 175 | +{ |
| 176 | + TVector<TConnectionInfo> result; |
| 177 | + |
| 178 | + with_lock (Lock) { |
| 179 | + result.reserve(Connections.size()); |
| 180 | + |
| 181 | + for (const auto& [sessionId, connection]: Connections) { |
| 182 | + result.push_back(connection); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + SortBy(result, [](const auto& connection) { return connection.StartTs; }); |
| 187 | + |
| 188 | + return result; |
| 189 | +} |
| 190 | + |
| 191 | +//////////////////////////////////////////////////////////////////////////////// |
| 192 | + |
| 193 | +TMountRegistryPtr CreateMountRegistry(ILoggingServicePtr logging) |
| 194 | +{ |
| 195 | + return std::make_shared<TMountRegistry>( |
| 196 | + logging->CreateLog("BLOCKSTORE_SERVER")); |
| 197 | +} |
| 198 | + |
| 199 | +} // namespace NCloud::NBlockStore::NStorage |
0 commit comments