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.

commonScheduler.go 1.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package scheduler
  2. import (
  3. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
  4. "gorm.io/gorm"
  5. "math/rand"
  6. "time"
  7. )
  8. type scheduleService interface {
  9. getNewStructForDb(task *types.TaskInfo, participantIds []int64) (interface{}, error)
  10. }
  11. func MatchLabels(dbEngin *gorm.DB, task *types.TaskInfo) ([]int64, error) {
  12. var ids []int64
  13. count := 0
  14. for key := range task.MatchLabels {
  15. var participantId []int64
  16. dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, task.MatchLabels[key]).Scan(&participantId)
  17. if count == 0 {
  18. ids = participantId
  19. }
  20. if len(participantId) == 0 || len(ids) == 0 {
  21. return nil, nil
  22. }
  23. ids = intersect(ids, participantId)
  24. count++
  25. }
  26. return micsSlice(ids, 1), nil
  27. }
  28. // 求交集
  29. func intersect(slice1, slice2 []int64) []int64 {
  30. m := make(map[int64]int)
  31. nn := make([]int64, 0)
  32. for _, v := range slice1 {
  33. m[v]++
  34. }
  35. for _, v := range slice2 {
  36. times, _ := m[v]
  37. if times == 1 {
  38. nn = append(nn, v)
  39. }
  40. }
  41. return nn
  42. }
  43. func micsSlice(origin []int64, count int) []int64 {
  44. tmpOrigin := make([]int64, len(origin))
  45. copy(tmpOrigin, origin)
  46. //一定要seed
  47. rand.Seed(time.Now().Unix())
  48. rand.Shuffle(len(tmpOrigin), func(i int, j int) {
  49. tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i]
  50. })
  51. result := make([]int64, 0, count)
  52. for index, value := range tmpOrigin {
  53. if index == count {
  54. break
  55. }
  56. result = append(result, value)
  57. }
  58. return result
  59. }

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.