@@ -4594,6 +4594,96 @@ Y_UNIT_TEST_SUITE(THiveTest) {
45944594 UNIT_ASSERT_LE (movedToFirstNode, TABLETS_PER_NODE / 2 );
45954595 }
45964596
4597+ Y_UNIT_TEST (TestHiveNoBalancingWithLowResourceUsage) {
4598+ static constexpr ui64 NUM_NODES = 5 ;
4599+ static constexpr ui64 NUM_TABLETS = 100 ;
4600+ TTestBasicRuntime runtime (NUM_NODES, false );
4601+ Setup (runtime, true , 1 , [](TAppPrepare& app) {
4602+ app.HiveConfig .SetTabletKickCooldownPeriod (0 );
4603+ app.HiveConfig .SetResourceChangeReactionPeriod (0 );
4604+ app.HiveConfig .SetMetricsWindowSize (1 );
4605+ });
4606+ const int nodeBase = runtime.GetNodeId (0 );
4607+ TActorId senderA = runtime.AllocateEdgeActor ();
4608+ const ui64 hiveTablet = MakeDefaultHiveID ();
4609+ const ui64 testerTablet = MakeTabletID (false , 1 );
4610+
4611+ auto getDistribution = [hiveTablet, nodeBase, senderA, &runtime]() -> std::array<std::vector<ui64>, NUM_NODES> {
4612+ std::array<std::vector<ui64>, NUM_NODES> nodeTablets = {};
4613+ {
4614+ runtime.SendToPipe (hiveTablet, senderA, new TEvHive::TEvRequestHiveInfo ());
4615+ TAutoPtr<IEventHandle> handle;
4616+ TEvHive::TEvResponseHiveInfo* response = runtime.GrabEdgeEventRethrow <TEvHive::TEvResponseHiveInfo>(handle);
4617+ for (const NKikimrHive::TTabletInfo& tablet : response->Record .GetTablets ()) {
4618+ UNIT_ASSERT_C (((int )tablet.GetNodeID () - nodeBase >= 0 ) && (tablet.GetNodeID () - nodeBase < NUM_NODES),
4619+ " nodeId# " << tablet.GetNodeID () << " nodeBase# " << nodeBase);
4620+ nodeTablets[tablet.GetNodeID () - nodeBase].push_back (tablet.GetTabletID ());
4621+ }
4622+ }
4623+ return nodeTablets;
4624+ };
4625+
4626+ CreateTestBootstrapper (runtime, CreateTestTabletInfo (hiveTablet, TTabletTypes::Hive), &CreateDefaultHive);
4627+
4628+ // wait for creation of nodes
4629+ {
4630+ TDispatchOptions options;
4631+ options.FinalEvents .emplace_back (TEvLocal::EvStatus, NUM_NODES);
4632+ runtime.DispatchEvents (options);
4633+ }
4634+
4635+ TTabletTypes::EType tabletType = TTabletTypes::Dummy;
4636+ std::vector<ui64> tablets;
4637+ tablets.reserve (NUM_TABLETS);
4638+ for (size_t i = 0 ; i < NUM_TABLETS; ++i) {
4639+ THolder<TEvHive::TEvCreateTablet> ev (new TEvHive::TEvCreateTablet (testerTablet, 100500 + i, tabletType, BINDED_CHANNELS));
4640+ ev->Record .SetObjectId (i);
4641+ ui64 tabletId = SendCreateTestTablet (runtime, hiveTablet, testerTablet, std::move (ev), 0 , true );
4642+ MakeSureTabletIsUp (runtime, tabletId, 0 );
4643+ tablets.push_back (tabletId);
4644+ }
4645+
4646+ auto initialDistribution = getDistribution ();
4647+
4648+ // report small metrics for some tablets
4649+ auto rand = CreateDeterministicRandomProvider (777 );
4650+ for (auto tablet : tablets) {
4651+ THolder<TEvHive::TEvTabletMetrics> metrics = MakeHolder<TEvHive::TEvTabletMetrics>();
4652+ NKikimrHive::TTabletMetrics* metric = metrics->Record .AddTabletMetrics ();
4653+ metric->SetTabletID (tablet);
4654+ if (rand->GenRand () % 2 ) {
4655+ metric->MutableResourceUsage ()->SetCPU (1001 ); // 1% core
4656+ metric->MutableResourceUsage ()->SetMemory (150'000 ); // 150kb
4657+ } else {
4658+ metric->MutableResourceUsage ()->SetCPU (999 );
4659+ metric->MutableResourceUsage ()->SetMemory (100'000 );
4660+ }
4661+
4662+ runtime.SendToPipe (hiveTablet, senderA, metrics.Release ());
4663+ }
4664+
4665+ {
4666+ TDispatchOptions options;
4667+ options.FinalEvents .emplace_back (NHive::TEvPrivate::EvBalancerOut);
4668+ runtime.DispatchEvents (options, TDuration::Seconds (10 ));
4669+ }
4670+
4671+ // Check that balancer moved no tablets
4672+ auto newDistribution = getDistribution ();
4673+
4674+ UNIT_ASSERT_EQUAL (initialDistribution, newDistribution);
4675+
4676+ {
4677+ auto request = std::make_unique<TEvHive::TEvRequestHiveDomainStats>();
4678+ request->Record .SetReturnMetrics (true );
4679+ runtime.SendToPipe (hiveTablet, senderA, request.release ());
4680+ TAutoPtr<IEventHandle> handle;
4681+ TEvHive::TEvResponseHiveDomainStats* response = runtime.GrabEdgeEventRethrow <TEvHive::TEvResponseHiveDomainStats>(handle);
4682+ ui64 totalCounter = response->Record .GetDomainStats (0 ).GetMetrics ().GetCounter ();
4683+ UNIT_ASSERT_VALUES_EQUAL (totalCounter, 0 );
4684+ }
4685+ }
4686+
45974687 Y_UNIT_TEST (TestHiveBalancerWithImmovableTablets) {
45984688 static constexpr ui64 TABLETS_PER_NODE = 10 ;
45994689 TTestBasicRuntime runtime (3 , false );
0 commit comments