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.

servicecontext.go 5.2 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package svc
  13. import (
  14. "github.com/go-redis/redis/v8"
  15. "github.com/go-resty/resty/v2"
  16. alert "github.com/prometheus/alertmanager/api/v2/client"
  17. "github.com/robfig/cron/v3"
  18. "github.com/zeromicro/go-zero/core/logx"
  19. "github.com/zeromicro/go-zero/zrpc"
  20. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/config"
  21. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler"
  22. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
  23. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  24. "gitlink.org.cn/JointCloud/pcm-coordinator/rpc/client/participantservice"
  25. "gitlink.org.cn/JointCloud/pcm-kubernetes/kubernetesclient"
  26. "gitlink.org.cn/JointCloud/pcm-openstack/openstackclient"
  27. slurmClient "gitlink.org.cn/JointCloud/pcm-slurm/slurmclient"
  28. "gitlink.org.cn/jcce-pcm/pcm-ac/hpcacclient"
  29. "gitlink.org.cn/jcce-pcm/pcm-participant-ceph/cephclient"
  30. "gitlink.org.cn/jcce-pcm/pcm-participant-modelarts/client/imagesservice"
  31. "gitlink.org.cn/jcce-pcm/pcm-participant-modelarts/client/modelartsservice"
  32. "gitlink.org.cn/jcce-pcm/pcm-participant-octopus/octopusclient"
  33. "gorm.io/driver/mysql"
  34. "gorm.io/gorm"
  35. "gorm.io/gorm/logger"
  36. "gorm.io/gorm/schema"
  37. "time"
  38. )
  39. type ServiceContext struct {
  40. Config config.Config
  41. RedisClient *redis.Client
  42. Cron *cron.Cron
  43. ModelArtsRpc modelartsservice.ModelArtsService
  44. ModelArtsImgRpc imagesservice.ImagesService
  45. DbEngin *gorm.DB
  46. ACRpc hpcacclient.HpcAC
  47. THRpc slurmClient.Slurm
  48. OctopusRpc octopusclient.Octopus
  49. CephRpc cephclient.Ceph
  50. OpenstackRpc openstackclient.Openstack
  51. K8sRpc kubernetesclient.Kubernetes
  52. MonitorClient map[int64]tracker.Prometheus
  53. ParticipantRpc participantservice.ParticipantService
  54. PromClient tracker.Prometheus
  55. AlertClient *alert.AlertmanagerAPI
  56. HttpClient *resty.Client
  57. Scheduler *scheduler.Scheduler
  58. }
  59. func NewServiceContext(c config.Config) *ServiceContext {
  60. promClient, err := tracker.NewPrometheus(c.Monitoring.PromUrl)
  61. if err != nil {
  62. logx.Errorf("InitPrometheus err: %v", err)
  63. panic("InitSnowflake err")
  64. }
  65. httpClient := resty.New()
  66. alertClient := tracker.NewAlertClient(c.Monitoring.AlertUrl)
  67. if err != nil {
  68. logx.Errorf("InitPrometheus err: %v", err)
  69. panic("InitSnowflake err")
  70. }
  71. //添加snowflake支持
  72. err = utils.InitSnowflake(c.SnowflakeConf.MachineId)
  73. if err != nil {
  74. logx.Errorf("InitSnowflake err: %v", err)
  75. panic("InitSnowflake err")
  76. }
  77. //启动Gorm支持
  78. dbEngin, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{
  79. NamingStrategy: schema.NamingStrategy{
  80. SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user`
  81. },
  82. Logger: logger.Default.LogMode(logger.Info),
  83. })
  84. if err != nil {
  85. logx.Errorf("数据库连接失败, err%v", err)
  86. panic(err)
  87. }
  88. sqlDB, err := dbEngin.DB()
  89. // SetMaxIdleConns 设置空闲连接池中连接的最大数量
  90. sqlDB.SetMaxIdleConns(10)
  91. // SetMaxOpenConns 设置打开数据库连接的最大数量。
  92. sqlDB.SetMaxOpenConns(50)
  93. // SetConnMaxLifetime 设置了连接可复用的最大时间。
  94. sqlDB.SetConnMaxLifetime(time.Hour)
  95. if err != nil {
  96. logx.Error(err.Error())
  97. return nil
  98. }
  99. redisClient := redis.NewClient(&redis.Options{
  100. Addr: c.Redis.Host,
  101. Password: c.Redis.Pass,
  102. })
  103. // scheduler
  104. octopusRpc := octopusclient.NewOctopus(zrpc.MustNewClient(c.OctopusRpcConf))
  105. aCRpc := hpcacclient.NewHpcAC(zrpc.MustNewClient(c.ACRpcConf))
  106. modelArtsRpc := modelartsservice.NewModelArtsService(zrpc.MustNewClient(c.ModelArtsRpcConf))
  107. modelArtsImgRpc := imagesservice.NewImagesService(zrpc.MustNewClient(c.ModelArtsImgRpcConf))
  108. //aiExecutor, resourceCollector := service2.InitAiClusterMap(octopusRpc, modelArtsRpc, modelArtsImgRpc, aCRpc)
  109. //storage := &database.AiStorage{DbEngin: dbEngin}
  110. scheduler := scheduler.NewSchdlr(nil, nil, nil)
  111. return &ServiceContext{
  112. Cron: cron.New(cron.WithSeconds()),
  113. DbEngin: dbEngin,
  114. Config: c,
  115. RedisClient: redisClient,
  116. ModelArtsRpc: modelArtsRpc,
  117. ModelArtsImgRpc: modelArtsImgRpc,
  118. CephRpc: cephclient.NewCeph(zrpc.MustNewClient(c.CephRpcConf)),
  119. ACRpc: aCRpc,
  120. OctopusRpc: octopusRpc,
  121. OpenstackRpc: openstackclient.NewOpenstack(zrpc.MustNewClient(c.OpenstackRpcConf)),
  122. K8sRpc: kubernetesclient.NewKubernetes(zrpc.MustNewClient(c.K8sNativeConf)),
  123. MonitorClient: make(map[int64]tracker.Prometheus),
  124. ParticipantRpc: participantservice.NewParticipantService(zrpc.MustNewClient(c.PcmCoreRpcConf)),
  125. PromClient: promClient,
  126. AlertClient: alertClient,
  127. HttpClient: httpClient,
  128. Scheduler: scheduler,
  129. }
  130. }

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.