You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vmScheduler.go 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package schedulers
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/algorithm/providerPricing"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/database"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers/option"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy"
  10. "gitlink.org.cn/JointCloud/pcm-coordinator/api/pkg/response"
  11. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  12. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  13. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
  14. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  15. "gorm.io/gorm"
  16. )
  17. type VmScheduler struct {
  18. yamlString string
  19. storage database.Storage
  20. task *response.TaskInfo
  21. *scheduler.Scheduler
  22. option *option.VmOption
  23. ctx context.Context
  24. promClient tracker.Prometheus
  25. dbEngin *gorm.DB
  26. }
  27. type VmResult struct {
  28. TaskId string
  29. ClusterId string
  30. ClusterName string
  31. Strategy string
  32. Replica int32
  33. Msg string
  34. }
  35. func NewVmScheduler(ctx context.Context, val string, scheduler *scheduler.Scheduler, option *option.VmOption, dbEngin *gorm.DB, promClient tracker.Prometheus) (*VmScheduler, error) {
  36. return &VmScheduler{ctx: ctx, yamlString: val, Scheduler: scheduler, option: option, dbEngin: dbEngin, promClient: promClient}, nil
  37. }
  38. /*func NewCloudScheduler(ctx context.Context, val string, scheduler *scheduler.Scheduler, option *option.CloudOption, dbEngin *gorm.DB, promClient tracker.Prometheus) (*CloudScheduler, error) {
  39. return &CloudScheduler{ctx: ctx, yamlString: val, Scheduler: scheduler, option: option, dbEngin: dbEngin, promClient: promClient}, nil
  40. }*/
  41. func (vm *VmScheduler) PickOptimalStrategy() (strategy.Strategy, error) {
  42. if len(vm.option.ClusterIds) == 1 {
  43. // TODO database operation Find
  44. return &strategy.SingleAssignment{Cluster: &strategy.AssignedCluster{ClusterId: vm.option.ClusterIds[0], Replicas: 1}}, nil
  45. }
  46. //resources, err := vm.findClustersWithResources()
  47. /* if err != nil {
  48. return nil, err
  49. }*/
  50. /* if len(resources) == 0 {
  51. return nil, errors.New("no cluster has resources")
  52. }*/
  53. //
  54. //if len(resources) == 1 {
  55. // var cluster strategy.AssignedCluster
  56. // cluster.ClusterId = resources[0].ClusterId
  57. // cluster.Replicas = 1
  58. // return &strategy.SingleAssignment{Cluster: &cluster}, nil
  59. //}
  60. //params := &param.Params{Resources: resources}
  61. switch vm.option.Strategy {
  62. /* case strategy.REPLICATION:
  63. var clusterIds []string
  64. for _, resource := range resources {
  65. clusterIds = append(clusterIds, resource.ClusterId)
  66. }
  67. strategy := strategy.NewReplicationStrategy(clusterIds, 1)
  68. return strategy, nil
  69. case strategy.RESOURCES_PRICING:
  70. strategy := strategy.NewPricingStrategy(&param.ResourcePricingParams{Params: params, Replicas: 1})
  71. return strategy, nil
  72. case strategy.DYNAMIC_RESOURCES:
  73. strategy := strategy.NewDynamicResourcesStrategy(params.Resources, vm.option, 1)
  74. return strategy, nil*/
  75. case strategy.STATIC_WEIGHT:
  76. //todo resources should match cluster StaticWeightMap
  77. strategy := strategy.NewStaticWeightStrategy(vm.option.ClusterToStaticWeight, 1)
  78. return strategy, nil
  79. }
  80. /*strategy := strategy.NewPricingStrategy(&param.ResourcePricingParams{})
  81. return strategy, nil*/
  82. return nil, errors.New("no strategy has been chosen")
  83. }
  84. func (v *VmScheduler) GetNewStructForDb(task *response.TaskInfo, resource string, participantId int64) (interface{}, error) {
  85. //TODO implement me
  86. vm := models.Vm{}
  87. utils.Convert(task.Metadata, &vm)
  88. vm.Id = utils.GenSnowflakeID()
  89. vm.TaskId = vm.TaskId
  90. vm.Status = constants.Saved
  91. vm.ParticipantId = participantId
  92. return vm, nil
  93. //vm.YamlString =v.yamlString
  94. /* vm. = utils.GenSnowflakeID()
  95. vm.NsID = task.NsID
  96. vm.ParticipantId = participantId*/
  97. }
  98. func (vm *VmScheduler) genTaskAndProviders() (*providerPricing.Task, []*providerPricing.Provider, error) {
  99. proParams, err := vm.storage.GetProviderParams()
  100. if err != nil {
  101. return nil, nil, nil
  102. }
  103. var providerList []*providerPricing.Provider
  104. for _, p := range proParams {
  105. provider := providerPricing.NewProvider(p.Participant_id, p.Cpu_avail, p.Mem_avail, p.Disk_avail, 0.0, 0.0, 0.0)
  106. providerList = append(providerList, provider)
  107. }
  108. //replicas := task.Metadata.(map[string]interface{})["spec"].(map[string]interface{})["replicas"].(float64)
  109. //t := algorithm.NewTask(0, int(replicas), 2, 75120000, 301214500, 1200, 2, 6, 2000)
  110. return nil, providerList, nil
  111. }
  112. func (as *VmScheduler) AssignTask(clusters []*strategy.AssignedCluster) (interface{}, error) {
  113. //TODO implement me
  114. if clusters == nil {
  115. return nil, errors.New("clusters is nil")
  116. }
  117. for i := len(clusters) - 1; i >= 0; i-- {
  118. if clusters[i].Replicas == 0 {
  119. clusters = append(clusters[:i], clusters[i+1:]...)
  120. }
  121. }
  122. if len(clusters) == 0 {
  123. return nil, errors.New("clusters is nil")
  124. }
  125. var results []*VmResult
  126. for _, cluster := range clusters {
  127. cName := ""
  128. as.dbEngin.Table("t_cluster").Select("name").Where("id=?", cluster.ClusterId).Find(&cName)
  129. cr := VmResult{
  130. ClusterId: cluster.ClusterId,
  131. ClusterName: cName,
  132. Replica: cluster.Replicas,
  133. }
  134. cr.ClusterId = cluster.ClusterId
  135. cr.Replica = cluster.Replicas
  136. cr.ClusterName = cName
  137. results = append(results, &cr)
  138. }
  139. return results, nil
  140. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.