@@ -40,24 +40,27 @@ import (
4040)
4141
4242type Task struct {
43- taskID string
44- alias string
45- applicationID string
46- application * Application
43+ taskID string
44+ alias string
45+ applicationID string
46+ application * Application
47+ podStatus v1.PodStatus // pod status, maintained separately for efficiency reasons
48+ context * Context
49+ createTime time.Time
50+ placeholder bool
51+ originator bool
52+ sm * fsm.FSM
53+
54+ // mutable resources, require locking
4755 allocationKey string
48- resource * si.Resource
49- pod * v1.Pod
50- podStatus v1.PodStatus // pod status, maintained separately for efficiency reasons
51- context * Context
5256 nodeName string
53- createTime time.Time
5457 taskGroupName string
55- placeholder bool
5658 terminationType string
57- originator bool
5859 schedulingState TaskSchedulingState
59- sm * fsm.FSM
60- lock * locking.RWMutex
60+ resource * si.Resource
61+ pod * v1.Pod
62+
63+ lock * locking.RWMutex
6164}
6265
6366func NewTask (tid string , app * Application , ctx * Context , pod * v1.Pod ) * Task {
@@ -135,14 +138,10 @@ func (task *Task) GetTaskPod() *v1.Pod {
135138}
136139
137140func (task * Task ) GetTaskID () string {
138- task .lock .RLock ()
139- defer task .lock .RUnlock ()
140141 return task .taskID
141142}
142143
143144func (task * Task ) IsPlaceholder () bool {
144- task .lock .RLock ()
145- defer task .lock .RUnlock ()
146145 return task .placeholder
147146}
148147
@@ -157,19 +156,25 @@ func (task *Task) setTaskGroupName(groupName string) {
157156 task .taskGroupName = groupName
158157}
159158
160- func (task * Task ) setTaskTerminationType (terminationTyp string ) {
159+ func (task * Task ) setTaskTerminationType (terminationType string ) {
161160 task .lock .Lock ()
162161 defer task .lock .Unlock ()
163- task .terminationType = terminationTyp
162+ task .terminationType = terminationType
164163}
165164
166- func (task * Task ) getTaskGroupName () string {
165+ func (task * Task ) GetTaskTerminationType () string {
166+ task .lock .RLock ()
167+ defer task .lock .RUnlock ()
168+ return task .terminationType
169+ }
170+
171+ func (task * Task ) GetTaskGroupName () string {
167172 task .lock .RLock ()
168173 defer task .lock .RUnlock ()
169174 return task .taskGroupName
170175}
171176
172- func (task * Task ) getNodeName () string {
177+ func (task * Task ) GetNodeName () string {
173178 task .lock .RLock ()
174179 defer task .lock .RUnlock ()
175180 return task .nodeName
@@ -222,8 +227,6 @@ func (task *Task) initialize() {
222227}
223228
224229func (task * Task ) IsOriginator () bool {
225- task .lock .RLock ()
226- defer task .lock .RUnlock ()
227230 return task .originator
228231}
229232
@@ -286,13 +289,33 @@ func (task *Task) handleSubmitTaskEvent() {
286289 log .Log (log .ShimCacheTask ).Debug ("scheduling pod" ,
287290 zap .String ("podName" , task .pod .Name ))
288291
292+ // send update allocation event to core
293+ task .updateAllocation ()
294+
295+ if ! utils .PodAlreadyBound (task .pod ) {
296+ // if this is a new request, add events to pod
297+ events .GetRecorder ().Eventf (task .pod .DeepCopy (), nil , v1 .EventTypeNormal , "Scheduling" , "Scheduling" ,
298+ "%s is queued and waiting for allocation" , task .alias )
299+ // if this task belongs to a task group, that means the app has gang scheduling enabled
300+ // in this case, post an event to indicate the task is being gang scheduled
301+ if ! task .placeholder && task .taskGroupName != "" {
302+ events .GetRecorder ().Eventf (task .pod .DeepCopy (), nil ,
303+ v1 .EventTypeNormal , "GangScheduling" , "TaskGroupMatch" ,
304+ "Pod belongs to the taskGroup %s, it will be scheduled as a gang member" , task .taskGroupName )
305+ }
306+ }
307+ }
308+
309+ // updateAllocation updates the core scheduler when task information changes.
310+ // This function must be called with the task lock held.
311+ func (task * Task ) updateAllocation () {
289312 // build preemption policy
290313 preemptionPolicy := & si.PreemptionPolicy {
291314 AllowPreemptSelf : task .isPreemptSelfAllowed (),
292315 AllowPreemptOther : task .isPreemptOtherAllowed (),
293316 }
294317
295- // submit allocation ask
318+ // submit allocation
296319 rr := common .CreateAllocationForTask (
297320 task .applicationID ,
298321 task .taskID ,
@@ -305,22 +328,9 @@ func (task *Task) handleSubmitTaskEvent() {
305328 preemptionPolicy )
306329 log .Log (log .ShimCacheTask ).Debug ("send update request" , zap .Stringer ("request" , rr ))
307330 if err := task .context .apiProvider .GetAPIs ().SchedulerAPI .UpdateAllocation (rr ); err != nil {
308- log .Log (log .ShimCacheTask ).Debug ("failed to send scheduling request to scheduler" , zap .Error (err ))
331+ log .Log (log .ShimCacheTask ).Debug ("failed to send allocation to scheduler" , zap .Error (err ))
309332 return
310333 }
311-
312- if ! utils .PodAlreadyBound (task .pod ) {
313- // if this is a new request, add events to pod
314- events .GetRecorder ().Eventf (task .pod .DeepCopy (), nil , v1 .EventTypeNormal , "Scheduling" , "Scheduling" ,
315- "%s is queued and waiting for allocation" , task .alias )
316- // if this task belongs to a task group, that means the app has gang scheduling enabled
317- // in this case, post an event to indicate the task is being gang scheduled
318- if ! task .placeholder && task .taskGroupName != "" {
319- events .GetRecorder ().Eventf (task .pod .DeepCopy (), nil ,
320- v1 .EventTypeNormal , "GangScheduling" , "TaskGroupMatch" ,
321- "Pod belongs to the taskGroup %s, it will be scheduled as a gang member" , task .taskGroupName )
322- }
323- }
324334}
325335
326336// this is called after task reaches PENDING state,
@@ -604,20 +614,42 @@ func (task *Task) UpdatePodCondition(podCondition *v1.PodCondition) (bool, *v1.P
604614 return false , pod
605615}
606616
617+ func (task * Task ) GetAllocationKey () string {
618+ task .lock .RLock ()
619+ defer task .lock .RUnlock ()
620+ return task .allocationKey
621+ }
622+
607623func (task * Task ) setAllocationKey (allocationKey string ) {
608624 task .lock .Lock ()
609625 defer task .lock .Unlock ()
610626 task .allocationKey = allocationKey
611627}
612628
629+ func (task * Task ) FailWithEvent (errorMessage , actionReason string ) {
630+ task .lock .RLock ()
631+ defer task .lock .RUnlock ()
632+ task .failWithEvent (errorMessage , actionReason )
633+ }
634+
613635func (task * Task ) failWithEvent (errorMessage , actionReason string ) {
614636 dispatcher .Dispatch (NewFailTaskEvent (task .applicationID , task .taskID , errorMessage ))
615637 events .GetRecorder ().Eventf (task .pod .DeepCopy (),
616638 nil , v1 .EventTypeWarning , actionReason , actionReason , errorMessage )
617639}
618640
619- func (task * Task ) setTaskPod (pod * v1.Pod ) {
641+ func (task * Task ) SetTaskPod (pod * v1.Pod ) {
620642 task .lock .Lock ()
621643 defer task .lock .Unlock ()
644+
622645 task .pod = pod
646+ oldResource := task .resource
647+ newResource := common .GetPodResource (pod )
648+ if ! common .Equals (oldResource , newResource ) {
649+ // pod resources have changed
650+ task .resource = newResource
651+
652+ // update allocation in core
653+ task .updateAllocation ()
654+ }
623655}
0 commit comments