6
6
namespace NKikimr ::NPQ::NBalancing {
7
7
8
8
9
+ struct LowLoadSessionComparator {
10
+ bool operator ()(const TSession* lhs, const TSession* rhs) const ;
11
+ };
12
+
13
+ using TLowLoadOrderedSessions = std::set<TSession*, LowLoadSessionComparator>;
14
+
15
+
16
+
9
17
//
10
18
// TPartition
11
19
//
@@ -868,6 +876,8 @@ void TConsumer::RegisterReadingSession(TSession* session, const TActorContext& c
868
876
CreateFamily ({partitionId}, ctx);
869
877
}
870
878
}
879
+ } else {
880
+ OrderedSessions.reset ();
871
881
}
872
882
}
873
883
@@ -886,6 +896,9 @@ std::vector<TPartitionFamily*> Snapshot(const std::unordered_map<size_t, const s
886
896
void TConsumer::UnregisterReadingSession (TSession* session, const TActorContext& ctx) {
887
897
auto pipe = session->Pipe ;
888
898
Sessions.erase (session->Pipe );
899
+ if (!session->WithGroups ()) {
900
+ OrderedSessions.reset ();
901
+ }
889
902
890
903
for (auto * family : Snapshot (Families)) {
891
904
auto special = family->SpecialSessions .erase (pipe);
@@ -1156,11 +1169,11 @@ void TConsumer::ScheduleBalance(const TActorContext& ctx) {
1156
1169
ctx.Send (Balancer.TopicActor .SelfId (), new TEvPQ::TEvBalanceConsumer (ConsumerName));
1157
1170
}
1158
1171
1159
- TOrderedSessions OrderSessions (
1172
+ TLowLoadOrderedSessions OrderSessions (
1160
1173
const std::unordered_map<TActorId, TSession*>& values,
1161
1174
std::function<bool (const TSession*)> predicate = [](const TSession*) { return true ; }
1162
1175
) {
1163
- TOrderedSessions result;
1176
+ TLowLoadOrderedSessions result;
1164
1177
for (auto & [_, v] : values) {
1165
1178
if (predicate (v)) {
1166
1179
result.insert (v);
@@ -1244,7 +1257,7 @@ void TConsumer::Balance(const TActorContext& ctx) {
1244
1257
}
1245
1258
}
1246
1259
1247
- TOrderedSessions commonSessions = OrderSessions (Sessions, [](auto * session) {
1260
+ TLowLoadOrderedSessions commonSessions = OrderSessions (Sessions, [](auto * session) {
1248
1261
return !session->WithGroups ();
1249
1262
});
1250
1263
@@ -1253,7 +1266,7 @@ void TConsumer::Balance(const TActorContext& ctx) {
1253
1266
auto families = OrderFamilies (UnreadableFamilies);
1254
1267
for (auto it = families.rbegin (); it != families.rend (); ++it) {
1255
1268
auto * family = *it;
1256
- TOrderedSessions specialSessions;
1269
+ TLowLoadOrderedSessions specialSessions;
1257
1270
auto & sessions = (family->IsCommon ()) ? commonSessions : (specialSessions = OrderSessions (family->SpecialSessions ));
1258
1271
1259
1272
auto sit = sessions.begin ();
@@ -1297,7 +1310,11 @@ void TConsumer::Balance(const TActorContext& ctx) {
1297
1310
GetPrefix () << " start rebalancing. familyCount=" << familyCount << " , sessionCount=" << commonSessions.size ()
1298
1311
<< " , desiredFamilyCount=" << desiredFamilyCount << " , allowPlusOne=" << allowPlusOne);
1299
1312
1300
- for (auto it = commonSessions.rbegin (); it != commonSessions.rend (); ++it) {
1313
+ if (!OrderedSessions) {
1314
+ OrderedSessions.emplace ();
1315
+ OrderedSessions->insert (commonSessions.begin (), commonSessions.end ());
1316
+ }
1317
+ for (auto it = OrderedSessions->begin (); it != OrderedSessions->end (); ++it) {
1301
1318
auto * session = *it;
1302
1319
auto targerFamilyCount = desiredFamilyCount + (allowPlusOne ? 1 : 0 );
1303
1320
auto families = OrderFamilies (session->Families );
@@ -1308,7 +1325,7 @@ void TConsumer::Balance(const TActorContext& ctx) {
1308
1325
}
1309
1326
}
1310
1327
1311
- if (allowPlusOne && session-> ActiveFamilyCount > desiredFamilyCount ) {
1328
+ if (allowPlusOne) {
1312
1329
--allowPlusOne;
1313
1330
}
1314
1331
}
@@ -1397,7 +1414,8 @@ TSession::TSession(const TActorId& pipe)
1397
1414
, InactivePartitionCount(0 )
1398
1415
, ReleasingPartitionCount(0 )
1399
1416
, ActiveFamilyCount(0 )
1400
- , ReleasingFamilyCount(0 ) {
1417
+ , ReleasingFamilyCount(0 )
1418
+ , Order(RandomNumber<size_t >()) {
1401
1419
}
1402
1420
1403
1421
bool TSession::WithGroups () const { return !Partitions.empty (); }
@@ -1850,18 +1868,23 @@ bool TPartitionFamilyComparator::operator()(const TPartitionFamily* lhs, const T
1850
1868
}
1851
1869
1852
1870
bool SessionComparator::operator ()(const TSession* lhs, const TSession* rhs) const {
1871
+ if (lhs->Order != rhs->Order ) {
1872
+ return lhs->Order < rhs->Order ;
1873
+ }
1874
+ return lhs->SessionName < rhs->SessionName ;
1875
+ }
1876
+
1877
+
1878
+ bool LowLoadSessionComparator::operator ()(const TSession* lhs, const TSession* rhs) const {
1853
1879
if (lhs->ActiveFamilyCount != rhs->ActiveFamilyCount ) {
1854
1880
return lhs->ActiveFamilyCount < rhs->ActiveFamilyCount ;
1855
1881
}
1856
- if (lhs->ActivePartitionCount != rhs->ActivePartitionCount ) {
1857
- return lhs->ActivePartitionCount < rhs->ActivePartitionCount ;
1858
- }
1859
- if (lhs->InactivePartitionCount != rhs->InactivePartitionCount ) {
1860
- return lhs->InactivePartitionCount < rhs->InactivePartitionCount ;
1861
- }
1862
1882
if (lhs->Partitions .size () != rhs->Partitions .size ()) {
1863
1883
return lhs->Partitions .size () < rhs->Partitions .size ();
1864
1884
}
1885
+ if (lhs->Order != rhs->Order ) {
1886
+ return lhs->Order < rhs->Order ;
1887
+ }
1865
1888
return lhs->SessionName < rhs->SessionName ;
1866
1889
}
1867
1890
0 commit comments