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.4 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
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "os"
  6. "gitlink.org.cn/cloudream/common/pkgs/ioswitch/exec"
  7. log "gitlink.org.cn/cloudream/common/pkgs/logger"
  8. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  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/connectivity"
  13. "gitlink.org.cn/cloudream/storage/common/pkgs/distlock"
  14. "gitlink.org.cn/cloudream/storage/common/pkgs/downloader"
  15. agtrpc "gitlink.org.cn/cloudream/storage/common/pkgs/grpc/agent"
  16. "google.golang.org/grpc"
  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/grpc"
  20. cmdsvc "gitlink.org.cn/cloudream/storage/agent/internal/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. // 启动网络连通性检测,并就地检测一次
  42. conCol := connectivity.NewCollector(&config.Cfg().Connectivity, func(collector *connectivity.Collector) {
  43. log := log.WithField("Connectivity", "")
  44. coorCli, err := stgglb.CoordinatorMQPool.Acquire()
  45. if err != nil {
  46. log.Warnf("acquire coordinator mq failed, err: %s", err.Error())
  47. return
  48. }
  49. defer stgglb.CoordinatorMQPool.Release(coorCli)
  50. cons := collector.GetAll()
  51. nodeCons := make([]cdssdk.NodeConnectivity, 0, len(cons))
  52. for _, con := range cons {
  53. var delay *float32
  54. if con.Delay != nil {
  55. v := float32(con.Delay.Microseconds()) / 1000
  56. delay = &v
  57. }
  58. nodeCons = append(nodeCons, cdssdk.NodeConnectivity{
  59. FromNodeID: *stgglb.Local.NodeID,
  60. ToNodeID: con.ToNodeID,
  61. Delay: delay,
  62. TestTime: con.TestTime,
  63. })
  64. }
  65. _, err = coorCli.UpdateNodeConnectivities(coormq.ReqUpdateNodeConnectivities(nodeCons))
  66. if err != nil {
  67. log.Warnf("update node connectivities: %v", err)
  68. }
  69. })
  70. conCol.CollectInPlace()
  71. distlock, err := distlock.NewService(&config.Cfg().DistLock)
  72. if err != nil {
  73. log.Fatalf("new ipfs failed, err: %s", err.Error())
  74. }
  75. sw := exec.NewWorker()
  76. dlder := downloader.NewDownloader(config.Cfg().Downloader, &conCol)
  77. taskMgr := task.NewManager(distlock, &conCol, &dlder)
  78. // 启动命令服务器
  79. // TODO 需要设计AgentID持久化机制
  80. agtSvr, err := agtmq.NewServer(cmdsvc.NewService(&taskMgr), config.Cfg().ID, &config.Cfg().RabbitMQ)
  81. if err != nil {
  82. log.Fatalf("new agent server failed, err: %s", err.Error())
  83. }
  84. agtSvr.OnError(func(err error) {
  85. log.Warnf("agent server err: %s", err.Error())
  86. })
  87. go serveAgentServer(agtSvr)
  88. //面向客户端收发数据
  89. listenAddr := config.Cfg().GRPC.MakeListenAddress()
  90. lis, err := net.Listen("tcp", listenAddr)
  91. if err != nil {
  92. log.Fatalf("listen on %s failed, err: %s", listenAddr, err.Error())
  93. }
  94. s := grpc.NewServer()
  95. agtrpc.RegisterAgentServer(s, grpcsvc.NewService(&sw))
  96. go serveGRPC(s, lis)
  97. go serveDistLock(distlock)
  98. foever := make(chan struct{})
  99. <-foever
  100. }
  101. func serveAgentServer(server *agtmq.Server) {
  102. log.Info("start serving command server")
  103. err := server.Serve()
  104. if err != nil {
  105. log.Errorf("command server stopped with error: %s", err.Error())
  106. }
  107. log.Info("command server stopped")
  108. // TODO 仅简单结束了程序
  109. os.Exit(1)
  110. }
  111. func serveGRPC(s *grpc.Server, lis net.Listener) {
  112. log.Info("start serving grpc")
  113. err := s.Serve(lis)
  114. if err != nil {
  115. log.Errorf("grpc stopped with error: %s", err.Error())
  116. }
  117. log.Info("grpc stopped")
  118. // TODO 仅简单结束了程序
  119. os.Exit(1)
  120. }
  121. func serveDistLock(svc *distlock.Service) {
  122. log.Info("start serving distlock")
  123. err := svc.Serve()
  124. if err != nil {
  125. log.Errorf("distlock stopped with error: %s", err.Error())
  126. }
  127. log.Info("distlock stopped")
  128. // TODO 仅简单结束了程序
  129. os.Exit(1)
  130. }

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