...
1
18
19 package scheduler
20
21 import (
22 "fmt"
23 "scheduler/log"
24 "scheduler/service_discovery"
25 "scheduler/types"
26 "sync"
27 "time"
28 )
29
30 const RoundRobinWithMasterSchedulerName = "RoundRobinWithMasterScheduler"
31
32
33 type RoundRobinWithMasterScheduler struct {
34
35 Master bool
36
37 MasterIP string
38
39 Loss bool
40
41 currentIndex int
42 currentIndexMutex sync.Mutex
43 }
44
45 func (s *RoundRobinWithMasterScheduler) GetFullName() string {
46 return fmt.Sprintf("%s(%t, %s, %t)", RoundRobinWithMasterSchedulerName, s.Master, s.MasterIP, s.Loss)
47 }
48
49 func (s *RoundRobinWithMasterScheduler) GetScheduler() *types.SchedulerDescriptor {
50 return &types.SchedulerDescriptor{
51 Name: RoundRobinWithMasterSchedulerName,
52 Parameters: []string{
53 fmt.Sprintf("%t", s.Master),
54 fmt.Sprintf("%s", s.MasterIP),
55 fmt.Sprintf("%t", s.Loss),
56 },
57 }
58 }
59
60 func (s *RoundRobinWithMasterScheduler) Schedule(req *types.ServiceRequest) (*JobResult, error) {
61 log.Log.Debugf("Scheduling job %s", req.ServiceName)
62 now := time.Now()
63 timingsStart := types.TimingsStart{ArrivedAt: &now}
64
65
66 if s.Master {
67
68 if !req.External {
69 return nil, JobCannotBeScheduled{}
70 }
71
72 machinesIp, err := service_discovery.GetMachinesIpsList()
73 if err != nil {
74 return nil, JobCannotBeScheduled{err.Error()}
75 }
76 if len(machinesIp) == 0 {
77 return nil, JobCannotBeScheduled{"no machine known"}
78 }
79
80
81 s.currentIndexMutex.Lock()
82
83 if s.currentIndex >= len(machinesIp) {
84 s.currentIndex = 0
85 }
86 pickedMachineIp := machinesIp[s.currentIndex]
87 s.currentIndex = (s.currentIndex + 1) % len(machinesIp)
88 s.currentIndexMutex.Unlock()
89
90 log.Log.Debugf("nextIndex is %d", s.currentIndex)
91
92
93 return executeJobExternally(req, pickedMachineIp, &timingsStart, s.GetFullName())
94 }
95
96
97
98 if !req.External {
99 return executeJobExternally(req, s.MasterIP, &timingsStart, s.GetFullName())
100 }
101
102
103 return executeJobLocally(req, &timingsStart, s.GetFullName())
104 }
105
View as plain text