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

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-ac/hpcacclient"
  21. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/config"
  22. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler"
  23. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/database"
  24. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/service"
  25. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
  26. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  27. "gitlink.org.cn/JointCloud/pcm-coordinator/rpc/client/participantservice"
  28. "gitlink.org.cn/JointCloud/pcm-kubernetes/kubernetesclient"
  29. "gitlink.org.cn/JointCloud/pcm-octopus/octopusclient"
  30. "gitlink.org.cn/JointCloud/pcm-openstack/openstackclient"
  31. slurmClient "gitlink.org.cn/JointCloud/pcm-slurm/slurmclient"
  32. "gitlink.org.cn/jcce-pcm/pcm-participant-ceph/cephclient"
  33. "gitlink.org.cn/jcce-pcm/pcm-participant-modelarts/client/imagesservice"
  34. "gitlink.org.cn/jcce-pcm/pcm-participant-modelarts/client/modelartsservice"
  35. "gorm.io/driver/mysql"
  36. "gorm.io/gorm"
  37. "gorm.io/gorm/logger"
  38. "gorm.io/gorm/schema"
  39. "time"
  40. )
  41. type ServiceContext struct {
  42. Config config.Config
  43. RedisClient *redis.Client
  44. Cron *cron.Cron
  45. ModelArtsRpc modelartsservice.ModelArtsService
  46. ModelArtsImgRpc imagesservice.ImagesService
  47. DbEngin *gorm.DB
  48. ACRpc hpcacclient.HpcAC
  49. THRpc slurmClient.Slurm
  50. OctopusRpc octopusclient.Octopus
  51. CephRpc cephclient.Ceph
  52. OpenstackRpc openstackclient.Openstack
  53. K8sRpc kubernetesclient.Kubernetes
  54. MonitorClient map[int64]tracker.Prometheus
  55. ParticipantRpc participantservice.ParticipantService
  56. PromClient tracker.Prometheus
  57. AlertClient *alert.AlertmanagerAPI
  58. HttpClient *resty.Client
  59. Scheduler *scheduler.Scheduler
  60. }
  61. func NewServiceContext(c config.Config) *ServiceContext {
  62. promClient, err := tracker.NewPrometheus(c.Monitoring.PromUrl)
  63. if err != nil {
  64. logx.Errorf("InitPrometheus err: %v", err)
  65. panic("InitSnowflake err")
  66. }
  67. httpClient := resty.New()
  68. alertClient := tracker.NewAlertClient(c.Monitoring.AlertUrl)
  69. if err != nil {
  70. logx.Errorf("InitPrometheus err: %v", err)
  71. panic("InitSnowflake err")
  72. }
  73. //添加snowflake支持
  74. err = utils.InitSnowflake(c.SnowflakeConf.MachineId)
  75. if err != nil {
  76. logx.Errorf("InitSnowflake err: %v", err)
  77. panic("InitSnowflake err")
  78. }
  79. //启动Gorm支持
  80. dbEngin, err := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{
  81. NamingStrategy: schema.NamingStrategy{
  82. SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user`
  83. },
  84. Logger: logger.Default.LogMode(logger.Info),
  85. })
  86. if err != nil {
  87. logx.Errorf("数据库连接失败, err%v", err)
  88. panic(err)
  89. }
  90. sqlDB, err := dbEngin.DB()
  91. // SetMaxIdleConns 设置空闲连接池中连接的最大数量
  92. sqlDB.SetMaxIdleConns(10)
  93. // SetMaxOpenConns 设置打开数据库连接的最大数量。
  94. sqlDB.SetMaxOpenConns(50)
  95. // SetConnMaxLifetime 设置了连接可复用的最大时间。
  96. sqlDB.SetConnMaxLifetime(time.Hour)
  97. if err != nil {
  98. logx.Error(err.Error())
  99. return nil
  100. }
  101. redisClient := redis.NewClient(&redis.Options{
  102. Addr: c.Redis.Host,
  103. Password: c.Redis.Pass,
  104. })
  105. // scheduler
  106. //octopusRpc := octopusclient.NewOctopus(zrpc.MustNewClient(c.OctopusRpcConf))
  107. //aCRpc := hpcacclient.NewHpcAC(zrpc.MustNewClient(c.ACRpcConf))
  108. //modelArtsRpc := modelartsservice.NewModelArtsService(zrpc.MustNewClient(c.ModelArtsRpcConf))
  109. //modelArtsImgRpc := imagesservice.NewImagesService(zrpc.MustNewClient(c.ModelArtsImgRpcConf))
  110. storage := &database.AiStorage{DbEngin: dbEngin}
  111. aiService, err := service.NewAiService(&c, storage)
  112. if err != nil {
  113. logx.Error(err.Error())
  114. return nil
  115. }
  116. scheduler := scheduler.NewSchdlr(aiService, storage)
  117. return &ServiceContext{
  118. Cron: cron.New(cron.WithSeconds()),
  119. DbEngin: dbEngin,
  120. Config: c,
  121. RedisClient: redisClient,
  122. ModelArtsRpc: modelartsservice.NewModelArtsService(zrpc.MustNewClient(c.ModelArtsRpcConf)),
  123. ModelArtsImgRpc: imagesservice.NewImagesService(zrpc.MustNewClient(c.ModelArtsImgRpcConf)),
  124. CephRpc: cephclient.NewCeph(zrpc.MustNewClient(c.CephRpcConf)),
  125. ACRpc: hpcacclient.NewHpcAC(zrpc.MustNewClient(c.ACRpcConf)),
  126. OctopusRpc: octopusclient.NewOctopus(zrpc.MustNewClient(c.OctopusRpcConf)),
  127. OpenstackRpc: openstackclient.NewOpenstack(zrpc.MustNewClient(c.OpenstackRpcConf)),
  128. K8sRpc: kubernetesclient.NewKubernetes(zrpc.MustNewClient(c.K8sNativeConf)),
  129. MonitorClient: make(map[int64]tracker.Prometheus),
  130. ParticipantRpc: participantservice.NewParticipantService(zrpc.MustNewClient(c.PcmCoreRpcConf)),
  131. PromClient: promClient,
  132. AlertClient: alertClient,
  133. HttpClient: httpClient,
  134. Scheduler: scheduler,
  135. }
  136. }

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.