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.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. func MatchLabels(dbEngin *gorm.DB, task *types.TaskInfo) ([]int64, error) {
  9. var ids []int64
  10. count := 0
  11. for key := range task.MatchLabels {
  12. var participantId []int64
  13. dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, task.MatchLabels[key]).Scan(&participantId)
  14. if count == 0 {
  15. ids = participantId
  16. }
  17. if len(participantId) == 0 || len(ids) == 0 {
  18. return nil, nil
  19. }
  20. ids = intersect(ids, participantId)
  21. count++
  22. }
  23. return micsSlice(ids, 1), nil
  24. }
  25. // 求交集
  26. func intersect(slice1, slice2 []int64) []int64 {
  27. m := make(map[int64]int)
  28. nn := make([]int64, 0)
  29. for _, v := range slice1 {
  30. m[v]++
  31. }
  32. for _, v := range slice2 {
  33. times, _ := m[v]
  34. if times == 1 {
  35. nn = append(nn, v)
  36. }
  37. }
  38. return nn
  39. }
  40. func micsSlice(origin []int64, count int) []int64 {
  41. tmpOrigin := make([]int64, len(origin))
  42. copy(tmpOrigin, origin)
  43. //一定要seed
  44. rand.Seed(time.Now().Unix())
  45. rand.Shuffle(len(tmpOrigin), func(i int, j int) {
  46. tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i]
  47. })
  48. result := make([]int64, 0, count)
  49. for index, value := range tmpOrigin {
  50. if index == count {
  51. break
  52. }
  53. result = append(result, value)
  54. }
  55. return result
  56. }

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.