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.

main.go 4.7 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "os"
  6. "sync"
  7. "time"
  8. log "gitlink.org.cn/cloudream/common/pkgs/logger"
  9. "gitlink.org.cn/cloudream/storage/agent/internal/config"
  10. "gitlink.org.cn/cloudream/storage/agent/internal/task"
  11. stgglb "gitlink.org.cn/cloudream/storage/common/globals"
  12. "gitlink.org.cn/cloudream/storage/common/pkgs/distlock"
  13. agtrpc "gitlink.org.cn/cloudream/storage/common/pkgs/grpc/agent"
  14. "google.golang.org/grpc"
  15. "gitlink.org.cn/cloudream/storage/common/consts"
  16. "gitlink.org.cn/cloudream/storage/common/utils"
  17. agtmq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/agent"
  18. coormq "gitlink.org.cn/cloudream/storage/common/pkgs/mq/coordinator"
  19. grpcsvc "gitlink.org.cn/cloudream/storage/agent/internal/services/grpc"
  20. cmdsvc "gitlink.org.cn/cloudream/storage/agent/internal/services/mq"
  21. )
  22. // TODO 此数据是否在运行时会发生变化?
  23. var AgentIpList []string
  24. func main() {
  25. // TODO 放到配置里读取
  26. AgentIpList = []string{"pcm01", "pcm1", "pcm2"}
  27. err := config.Init()
  28. if err != nil {
  29. fmt.Printf("init config failed, err: %s", err.Error())
  30. os.Exit(1)
  31. }
  32. err = log.Init(&config.Cfg().Logger)
  33. if err != nil {
  34. fmt.Printf("init logger failed, err: %s", err.Error())
  35. os.Exit(1)
  36. }
  37. stgglb.InitLocal(&config.Cfg().Local)
  38. stgglb.InitMQPool(&config.Cfg().RabbitMQ)
  39. stgglb.InitAgentRPCPool(&agtrpc.PoolConfig{})
  40. stgglb.InitIPFSPool(&config.Cfg().IPFS)
  41. distlock, err := distlock.NewService(&config.Cfg().DistLock)
  42. if err != nil {
  43. log.Fatalf("new ipfs failed, err: %s", err.Error())
  44. }
  45. //处置协调端、客户端命令(可多建几个)
  46. wg := sync.WaitGroup{}
  47. wg.Add(5)
  48. taskMgr := task.NewManager(distlock)
  49. // 启动命令服务器
  50. // TODO 需要设计AgentID持久化机制
  51. agtSvr, err := agtmq.NewServer(cmdsvc.NewService(&taskMgr), config.Cfg().ID, &config.Cfg().RabbitMQ)
  52. if err != nil {
  53. log.Fatalf("new agent server failed, err: %s", err.Error())
  54. }
  55. agtSvr.OnError(func(err error) {
  56. log.Warnf("agent server err: %s", err.Error())
  57. })
  58. go serveAgentServer(agtSvr, &wg)
  59. go reportStatus(&wg) //网络延迟感知
  60. //面向客户端收发数据
  61. listenAddr := config.Cfg().GRPC.MakeListenAddress()
  62. lis, err := net.Listen("tcp", listenAddr)
  63. if err != nil {
  64. log.Fatalf("listen on %s failed, err: %s", listenAddr, err.Error())
  65. }
  66. s := grpc.NewServer()
  67. agtrpc.RegisterAgentServer(s, grpcsvc.NewService())
  68. go serveGRPC(s, lis, &wg)
  69. go serveDistLock(distlock)
  70. wg.Wait()
  71. }
  72. func serveAgentServer(server *agtmq.Server, wg *sync.WaitGroup) {
  73. log.Info("start serving command server")
  74. err := server.Serve()
  75. if err != nil {
  76. log.Errorf("command server stopped with error: %s", err.Error())
  77. }
  78. log.Info("command server stopped")
  79. wg.Done()
  80. }
  81. func serveGRPC(s *grpc.Server, lis net.Listener, wg *sync.WaitGroup) {
  82. log.Info("start serving grpc")
  83. err := s.Serve(lis)
  84. if err != nil {
  85. log.Errorf("grpc stopped with error: %s", err.Error())
  86. }
  87. log.Info("grpc stopped")
  88. wg.Done()
  89. }
  90. func serveDistLock(svc *distlock.Service) {
  91. log.Info("start serving distlock")
  92. err := svc.Serve()
  93. if err != nil {
  94. log.Errorf("distlock stopped with error: %s", err.Error())
  95. }
  96. log.Info("distlock stopped")
  97. }
  98. func reportStatus(wg *sync.WaitGroup) {
  99. coorCli, err := coormq.NewClient(&config.Cfg().RabbitMQ)
  100. if err != nil {
  101. wg.Done()
  102. log.Error("new coordinator client failed, err: %w", err)
  103. return
  104. }
  105. // TODO 增加退出死循环的方法
  106. for {
  107. //挨个ping其他agent(AgentIpList),记录延迟到AgentDelay
  108. // TODO AgentIP考虑放到配置文件里或者启动时从coor获取
  109. ips := utils.GetAgentIps()
  110. agentDelay := make([]int, len(ips))
  111. waitG := sync.WaitGroup{}
  112. waitG.Add(len(ips))
  113. for i := 0; i < len(ips); i++ {
  114. go func(i int, wg *sync.WaitGroup) {
  115. connStatus, err := utils.GetConnStatus(ips[i])
  116. if err != nil {
  117. wg.Done()
  118. log.Warnf("ping %s failed, err: %s", ips[i], err.Error())
  119. return
  120. }
  121. log.Debugf("connection status to %s: %+v", ips[i], connStatus)
  122. if connStatus.IsReachable {
  123. agentDelay[i] = int(connStatus.Delay.Milliseconds()) + 1
  124. } else {
  125. agentDelay[i] = -1
  126. }
  127. wg.Done()
  128. }(i, &waitG)
  129. }
  130. waitG.Wait()
  131. //TODO: 查看本地IPFS daemon是否正常,记录到ipfsStatus
  132. ipfsStatus := consts.IPFSStateOK
  133. //TODO:访问自身资源目录(配置文件中获取路径),记录是否正常,记录到localDirStatus
  134. localDirStatus := consts.StorageDirectoryStateOK
  135. //发送心跳
  136. // TODO 由于数据结构未定,暂时不发送真实数据
  137. coorCli.AgentStatusReport(coormq.NewAgentStatusReportBody(config.Cfg().ID, []int64{}, []int{}, ipfsStatus, localDirStatus))
  138. time.Sleep(time.Minute * 5)
  139. }
  140. coorCli.Close()
  141. wg.Done()
  142. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。