@@ -121,7 +121,31 @@ double TSchedulerEntityHandle::VRuntime() {
121121}
122122
123123struct TComputeScheduler ::TImpl {
124- THashMap<TString, std::unique_ptr<TMultithreadPublisher<TSchedulerEntity::TGroupRecord>>> Groups;
124+ THashMap<TString, size_t > PoolId;
125+ std::vector<std::unique_ptr<TMultithreadPublisher<TSchedulerEntity::TGroupRecord>>> Records;
126+
127+ struct TRule {
128+ size_t Parent;
129+ double Weight;
130+
131+ TMaybe<size_t > RecordId = {};
132+ double SubRulesSum = 0 ;
133+ bool Empty = true ;
134+ };
135+ size_t RootRule;
136+ std::vector<TRule> Rules;
137+
138+ double SumCores;
139+
140+ void AssignWeights (TMonotonic now) {
141+ for (size_t i = 0 ; i < Rules.size (); ++i) {
142+ if (Rules[i].RecordId ) {
143+ if (Records[*Rules[i].RecordId ]->Next ()->EntitiesWeight < MinPriority) { // mincores
144+ }
145+ } else {
146+ }
147+ }
148+ }
125149};
126150
127151TComputeScheduler::TComputeScheduler () {
@@ -130,31 +154,100 @@ TComputeScheduler::TComputeScheduler() {
130154
131155TComputeScheduler::~TComputeScheduler () = default ;
132156
133- void TComputeScheduler::SetPriorities (THashMap<TString, double > priorities, double cores) {
134- double sum = 0 ;
135- for (auto [_, v] : priorities) {
136- sum += v;
137- }
138- for (auto & [k, v] : Impl->Groups ) {
139- auto ptr = priorities.FindPtr (k);
140- if (ptr) {
141- v->Next ()->Weight = ((*ptr) * cores) / sum;
142- v->Next ()->Disabled = false ;
157+ void TComputeScheduler::SetPriorities (TDistributionRule rule, double cores, TMonotonic now) {
158+ THashSet<TString> seenNames;
159+ std::function<void (TDistributionRule&)> exploreNames = [&](TDistributionRule& rule) {
160+ if (rule.SubRules .empty ()) {
161+ seenNames.insert (rule.Name );
143162 } else {
144- v->Next ()->Weight = 0 ;
145- v->Next ()->Disabled = true ;
163+ for (auto & subRule : rule.SubRules ) {
164+ exploreNames (subRule);
165+ }
146166 }
147- v->Publish ();
148- }
149- for (auto & [k, v] : priorities) {
150- if (!Impl->Groups .contains (k)) {
167+ };
168+ exploreNames (rule);
169+
170+ for (auto & k : seenNames) {
171+ auto ptr = Impl->PoolId .FindPtr (k);
172+ if (!ptr) {
173+ Impl->PoolId [k] = Impl->Records .size ();
151174 auto group = std::make_unique<TMultithreadPublisher<TSchedulerEntity::TGroupRecord>>();
152- group->Next ()->LastNowRecalc = TMonotonic::Now ();
153- group->Next ()->Weight = (v * cores) / sum;
175+ group->Next ()->LastNowRecalc = now;
176+ Impl->Records .push_back (std::move (group));
177+ }
178+ }
179+ for (auto & [k, v] : Impl->PoolId ) {
180+ if (!seenNames.contains (k)) {
181+ auto * group = Impl->Records [Impl->PoolId [v]].get ();
182+ group->Next ()->Weight = 0 ;
183+ group->Next ()->Disabled = true ;
154184 group->Publish ();
155- Impl->Groups [k] = std::move (group);
156185 }
157186 }
187+ Impl->SumCores = cores;
188+
189+ TVector<TImpl::TRule> rules;
190+ std::function<size_t (TDistributionRule&)> makeRules = [&](TDistributionRule& rule) {
191+ size_t result;
192+ if (rule.SubRules .empty ()) {
193+ result = rules.size ();
194+ rules.push_back (TImpl::TRule{.Weight = rule.Share , .RecordId =Impl->PoolId [rule.Name ]});
195+ } else {
196+ TVector<size_t > toAssign;
197+ for (auto & subRule : rule.SubRules ) {
198+ toAssign.push_back (makeRules (subRule));
199+ }
200+ size_t result = rules.size ();
201+ rules.push_back (TImpl::TRule{.Weight = rule.Share });
202+ for (auto i : toAssign) {
203+ rules[i].Parent = result;
204+ }
205+ return result;
206+ }
207+ return result;
208+ };
209+ Impl->RootRule = makeRules (rule);
210+ Impl->Rules .swap (rules);
211+
212+ // if (ptr) {
213+ // v->Next()->Weight = ((*ptr) * cores) / sum;
214+ // v->Next()->Disabled = false;
215+ // } else {
216+ // v->Next()->Weight = 0;
217+ // v->Next()->Disabled = true;
218+ // }
219+ // v->Publish();
220+ // }
221+
222+ Impl->AssignWeights (now);
223+ for (auto & record : Impl->Records ) {
224+ record->Publish ();
225+ }
226+
227+ // double sum = 0;
228+ // for (auto [_, v] : priorities) {
229+ // sum += v;
230+ // }
231+ // for (auto& [k, v] : Impl->Groups) {
232+ // auto ptr = priorities.FindPtr(k);
233+ // if (ptr) {
234+ // v->Next()->Weight = ((*ptr) * cores) / sum;
235+ // v->Next()->Disabled = false;
236+ // } else {
237+ // v->Next()->Weight = 0;
238+ // v->Next()->Disabled = true;
239+ // }
240+ // v->Publish();
241+ // }
242+ // for (auto& [k, v] : priorities) {
243+ // if (!Impl->Groups.contains(k)) {
244+ // auto group = ;
245+ // group->Next()->LastNowRecalc = TMonotonic::Now();
246+ // group->Next()->Weight = (v * cores) / sum;
247+ // group->Publish();
248+ // Impl->Groups[k] = std::move(group);
249+ // }
250+ // }
158251}
159252
160253
@@ -177,7 +270,8 @@ TSchedulerEntityHandle TComputeScheduler::Enroll(TString groupName, double weigh
177270}
178271
179272void TComputeScheduler::AdvanceTime (TMonotonic now) {
180- for (auto & [_, v] : Impl->Groups ) {
273+ Impl->AssignWeights ();
274+ for (auto & v : Impl->Records ) {
181275 {
182276 auto group = v.get ()->Current ();
183277 if (!group.get ()->Disabled && group.get ()->EntitiesWeight > MinPriority) {
0 commit comments