Skip to content

Commit 877c148

Browse files
authored
Optimize index sorting in BSC SysView (#15448)
1 parent 3c66116 commit 877c148

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

ydb/core/mind/bscontroller/sys_view.cpp

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ void CalculateGroupUsageStats(NKikimrSysView::TGroupInfo *info, const std::vecto
7676

7777
class TSystemViewsCollector : public TActorBootstrapped<TSystemViewsCollector> {
7878
TControllerSystemViewsState State;
79-
std::vector<std::pair<TPDiskId, const NKikimrSysView::TPDiskInfo*>> PDiskIndex;
80-
std::vector<std::pair<TVSlotId, const NKikimrSysView::TVSlotInfo*>> VSlotIndex;
81-
std::vector<std::pair<TGroupId, const NKikimrSysView::TGroupInfo*>> GroupIndex;
82-
std::vector<std::pair<TBoxStoragePoolId, const NKikimrSysView::TStoragePoolInfo*>> StoragePoolIndex;
79+
std::map<TPDiskId, const NKikimrSysView::TPDiskInfo*> PDiskIndex;
80+
std::map<TVSlotId, const NKikimrSysView::TVSlotInfo*> VSlotIndex;
81+
std::map<TGroupId, const NKikimrSysView::TGroupInfo*> GroupIndex;
82+
std::map<TBoxStoragePoolId, const NKikimrSysView::TStoragePoolInfo*> StoragePoolIndex;
8383
TBlobStorageController::THostRecordMap HostRecords;
8484
ui32 GroupReserveMin = 0;
8585
ui32 GroupReservePart = 0;
@@ -143,52 +143,49 @@ class TSystemViewsCollector : public TActorBootstrapped<TSystemViewsCollector> {
143143

144144
template<typename TDest, typename TSrc, typename TDeleted, typename TIndex>
145145
void Merge(TDest& dest, TSrc& src, const TDeleted& deleted, TIndex& index) {
146-
if (!src.empty() || !deleted.empty()) {
147-
index.clear();
148-
}
149146
for (const auto& key : deleted) {
150147
dest.erase(key);
148+
index.erase(key);
151149
}
152-
for (auto& [key, _] : src) {
150+
for (auto& [key, value] : src) {
153151
dest.erase(key);
152+
index[key] = &value;
154153
}
155154
dest.merge(std::move(src));
155+
156+
#ifndef NDEBUG
157+
for (const auto& [key, value] : dest) {
158+
Y_DEBUG_ABORT_UNLESS(index.contains(key) && index[key] == &value);
159+
}
160+
Y_DEBUG_ABORT_UNLESS(index.size() == dest.size());
161+
#endif
156162
}
157163

158-
template <typename TResponse, typename TRequest, typename TMap, typename TIndex>
159-
void Reply(TRequest& request, const TMap& entries, TIndex& index) {
164+
template <typename TResponse, typename TRequest, typename TIndex>
165+
void Reply(TRequest& request, TIndex& index) {
160166
const auto& record = request->Get()->Record;
161167
auto response = MakeHolder<TResponse>();
162168

163-
if (index.empty() && !entries.empty()) {
164-
index.reserve(entries.size());
165-
for (const auto& [key, value] : entries) {
166-
index.emplace_back(key, &value);
167-
}
168-
std::sort(index.begin(), index.end());
169-
}
170-
171169
auto begin = index.begin();
172170
auto end = index.end();
173-
auto comp = [](const auto& kv, const auto& key) { return kv.first < key; };
174171

175172
if (record.HasFrom()) {
176173
auto from = TransformKey(record.GetFrom());
177-
begin = std::lower_bound(index.begin(), index.end(), from, comp);
174+
begin = index.lower_bound(from);
178175
if (begin != index.end() && begin->first == from && record.HasInclusiveFrom() && !record.GetInclusiveFrom()) {
179176
++begin;
180177
}
181178
}
182179

183180
if (record.HasTo()) {
184181
auto to = TransformKey(record.GetTo());
185-
end = std::lower_bound(index.begin(), index.end(), to, comp);
182+
end = index.lower_bound(to);
186183
if (end != index.end() && end->first == to && record.GetInclusiveTo()) {
187184
++end;
188185
}
189186
}
190187

191-
for (; begin < end; ++begin) {
188+
for (; begin != end; ++begin) {
192189
auto* entry = response->Record.AddEntries();
193190
FillKey(entry->MutableKey(), begin->first);
194191
entry->MutableInfo()->CopyFrom(*begin->second);
@@ -198,19 +195,19 @@ class TSystemViewsCollector : public TActorBootstrapped<TSystemViewsCollector> {
198195
}
199196

200197
void Handle(TEvSysView::TEvGetPDisksRequest::TPtr& ev) {
201-
Reply<TEvSysView::TEvGetPDisksResponse>(ev, State.PDisks, PDiskIndex);
198+
Reply<TEvSysView::TEvGetPDisksResponse>(ev, PDiskIndex);
202199
}
203200

204201
void Handle(TEvSysView::TEvGetVSlotsRequest::TPtr& ev) {
205-
Reply<TEvSysView::TEvGetVSlotsResponse>(ev, State.VSlots, VSlotIndex);
202+
Reply<TEvSysView::TEvGetVSlotsResponse>(ev, VSlotIndex);
206203
}
207204

208205
void Handle(TEvSysView::TEvGetGroupsRequest::TPtr& ev) {
209-
Reply<TEvSysView::TEvGetGroupsResponse>(ev, State.Groups, GroupIndex);
206+
Reply<TEvSysView::TEvGetGroupsResponse>(ev, GroupIndex);
210207
}
211208

212209
void Handle(TEvSysView::TEvGetStoragePoolsRequest::TPtr& ev) {
213-
Reply<TEvSysView::TEvGetStoragePoolsResponse>(ev, State.StoragePools, StoragePoolIndex);
210+
Reply<TEvSysView::TEvGetStoragePoolsResponse>(ev, StoragePoolIndex);
214211
}
215212

216213
void Handle(TEvSysView::TEvGetStorageStatsRequest::TPtr& ev) {

0 commit comments

Comments
 (0)