@@ -258,13 +258,17 @@ func safeGetRequirements(schedulingInfo *internaltypes.JobSchedulingInfo) map[st
258258func (jobDb * JobDb ) internJobSchedulingInfoStrings (info * internaltypes.JobSchedulingInfo ) * internaltypes.JobSchedulingInfo {
259259 info .PriorityClass = jobDb .stringInterner .Intern (info .PriorityClass )
260260 pr := info .PodRequirements
261+ newAnnotations := make (map [string ]string , len (pr .Annotations ))
261262 for k , v := range pr .Annotations {
262- pr . Annotations [jobDb .stringInterner .Intern (k )] = jobDb .stringInterner .Intern (v )
263+ newAnnotations [jobDb .stringInterner .Intern (k )] = jobDb .stringInterner .Intern (v )
263264 }
265+ pr .Annotations = newAnnotations
264266
267+ newNodeSelector := make (map [string ]string , len (pr .NodeSelector ))
265268 for k , v := range pr .NodeSelector {
266- pr . NodeSelector [jobDb .stringInterner .Intern (k )] = jobDb .stringInterner .Intern (v )
269+ newNodeSelector [jobDb .stringInterner .Intern (k )] = jobDb .stringInterner .Intern (v )
267270 }
271+ pr .NodeSelector = newNodeSelector
268272
269273 for idx , toleration := range pr .Tolerations {
270274 pr .Tolerations [idx ] = v1.Toleration {
@@ -339,6 +343,10 @@ func (jobDb *JobDb) WriteTxn() *Txn {
339343 }
340344}
341345
346+ func (jobDb * JobDb ) CumulativeInternedStringsCount () uint64 {
347+ return jobDb .stringInterner .CumulativeInsertCount ()
348+ }
349+
342350// Txn is a JobDb Transaction. Transactions provide a consistent view of the database, allowing readers to
343351// perform multiple actions without the database changing from underneath them.
344352// Write transactions also allow callers to perform write operations that will not be visible to other users
@@ -559,14 +567,32 @@ func (txn *Txn) Upsert(jobs []*Job) error {
559567 // gangs
560568 go func () {
561569 defer wg .Done ()
562- for _ , job := range jobs {
563- if job .IsInGang () {
564- key := gangKey {queue : job .Queue (), gangId : job .GetGangInfo ().Id ()}
570+ if hasJobs {
571+ for _ , job := range jobs {
572+ if job .IsInGang () {
573+ key := gangKey {queue : job .Queue (), gangId : job .GetGangInfo ().Id ()}
574+
575+ if _ , present := txn .jobsByGangKey [key ]; ! present {
576+ txn .jobsByGangKey [key ] = immutable.NewSet [string ](JobIdHasher {})
577+ }
578+ txn .jobsByGangKey [key ] = txn .jobsByGangKey [key ].Add (job .Id ())
579+ }
580+ }
581+ } else {
582+ jobsByGangKey := map [gangKey ]map [string ]bool {}
583+ for _ , job := range jobs {
584+ if job .IsInGang () {
585+ key := gangKey {queue : job .Queue (), gangId : job .GetGangInfo ().Id ()}
565586
566- if _ , present := txn .jobsByGangKey [key ]; ! present {
567- txn .jobsByGangKey [key ] = immutable.NewSet [string ](JobIdHasher {})
587+ if _ , present := jobsByGangKey [key ]; ! present {
588+ jobsByGangKey [key ] = map [string ]bool {}
589+ }
590+ jobsByGangKey [key ][job .Id ()] = true
568591 }
569- txn .jobsByGangKey [key ] = txn .jobsByGangKey [key ].Add (job .Id ())
592+ }
593+
594+ for key , jobsInGang := range jobsByGangKey {
595+ txn .jobsByGangKey [key ] = immutable .NewSet [string ](JobIdHasher {}, maps .Keys (jobsInGang )... )
570596 }
571597 }
572598 }()
@@ -575,28 +601,65 @@ func (txn *Txn) Upsert(jobs []*Job) error {
575601 // To enable iterating over them in the order they should be scheduled.
576602 go func () {
577603 defer wg .Done ()
578- for _ , job := range jobs {
579- if job .Queued () {
580- newQueue , ok := txn .jobsByQueue [job .queue ]
581- if ! ok {
582- q := emptyList
583- newQueue = q
604+ if hasJobs {
605+ for _ , job := range jobs {
606+ if job .Queued () {
607+ newQueue , ok := txn .jobsByQueue [job .queue ]
608+ if ! ok {
609+ newQueue = emptyList
610+ }
611+ txn .jobsByQueue [job .queue ] = newQueue .Add (job )
612+
613+ for _ , pool := range job .Pools () {
614+ _ , present := txn .jobsByPoolAndQueue [pool ]
615+ if ! present {
616+ queues := map [string ]immutable.SortedSet [* Job ]{}
617+ txn .jobsByPoolAndQueue [pool ] = queues
618+ }
619+ _ , present = txn.jobsByPoolAndQueue [pool ][job.queue ]
620+ if ! present {
621+ jobs := immutable.NewSortedSet [* Job ](MarketJobPriorityComparer {Pool : pool })
622+ txn.jobsByPoolAndQueue [pool ][job.queue ] = jobs
623+ }
624+ txn.jobsByPoolAndQueue [pool ][job.queue ] = txn.jobsByPoolAndQueue [pool ][job.queue ].Add (job )
625+ }
584626 }
585- newQueue = newQueue .Add (job )
586- txn .jobsByQueue [job .queue ] = newQueue
627+ }
628+ } else {
629+ jobsByQueue := map [string ]map [* Job ]bool {}
630+ jobsByPoolAndQueue := map [string ]map [string ]map [* Job ]bool {}
587631
588- for _ , pool := range job .Pools () {
589- _ , present := txn .jobsByPoolAndQueue [pool ]
590- if ! present {
591- queues := map [string ]immutable.SortedSet [* Job ]{}
592- txn .jobsByPoolAndQueue [pool ] = queues
632+ for _ , job := range jobs {
633+ if job .Queued () {
634+ if _ , ok := jobsByQueue [job .queue ]; ! ok {
635+ jobsByQueue [job .queue ] = map [* Job ]bool {}
593636 }
594- _ , present = txn.jobsByPoolAndQueue [pool ][job.queue ]
595- if ! present {
596- jobs := immutable.NewSortedSet [* Job ](MarketJobPriorityComparer {Pool : pool })
597- txn.jobsByPoolAndQueue [pool ][job.queue ] = jobs
637+ jobsByQueue [job.queue ][job ] = true
638+
639+ for _ , pool := range job .Pools () {
640+ if _ , present := jobsByPoolAndQueue [pool ]; ! present {
641+ jobsByPoolAndQueue [pool ] = map [string ]map [* Job ]bool {}
642+ }
643+ if _ , present := jobsByPoolAndQueue [pool ][job.queue ]; ! present {
644+ jobsByPoolAndQueue [pool ][job.queue ] = map [* Job ]bool {}
645+ }
646+ jobsByPoolAndQueue [pool ][job.queue ][job ] = true
647+ }
648+ }
649+ }
650+
651+ for queue , jobsForQueue := range jobsByQueue {
652+ txn .jobsByQueue [queue ] = immutable .NewSortedSet [* Job ](JobPriorityComparer {}, maps .Keys (jobsForQueue )... )
653+ }
654+
655+ for pool , jobsForPool := range jobsByPoolAndQueue {
656+ if _ , ok := txn .jobsByPoolAndQueue [pool ]; ! ok {
657+ txn .jobsByPoolAndQueue [pool ] = map [string ]immutable.SortedSet [* Job ]{}
658+ }
659+ for queue , jobsForQueueInPool := range jobsForPool {
660+ if _ , ok := txn.jobsByPoolAndQueue [pool ][queue ]; ! ok {
661+ txn.jobsByPoolAndQueue [pool ][queue ] = immutable .NewSortedSet [* Job ](MarketJobPriorityComparer {Pool : pool }, maps .Keys (jobsForQueueInPool )... )
598662 }
599- txn.jobsByPoolAndQueue [pool ][job.queue ] = txn.jobsByPoolAndQueue [pool ][job.queue ].Add (job )
600663 }
601664 }
602665 }
@@ -605,11 +668,24 @@ func (txn *Txn) Upsert(jobs []*Job) error {
605668 // Unvalidated jobs
606669 go func () {
607670 defer wg .Done ()
608- for _ , job := range jobs {
609- if ! job .Validated () {
610- unvalidatedJobs := txn .unvalidatedJobs .Add (job )
611- txn .unvalidatedJobs = & unvalidatedJobs
671+ if hasJobs {
672+ for _ , job := range jobs {
673+ if ! job .Validated () {
674+ unvalidatedJobs := txn .unvalidatedJobs .Add (job )
675+ txn .unvalidatedJobs = & unvalidatedJobs
676+ }
677+ }
678+ } else {
679+ unvalidatedJobs := map [* Job ]bool {}
680+
681+ for _ , job := range jobs {
682+ if ! job .Validated () {
683+ unvalidatedJobs [job ] = true
684+ }
612685 }
686+
687+ unvalidatedJobsImmutable := immutable .NewSet [* Job ](JobHasher {}, maps .Keys (unvalidatedJobs )... )
688+ txn .unvalidatedJobs = & unvalidatedJobsImmutable
613689 }
614690 }()
615691
0 commit comments