overlord.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package dao
  2. import (
  3. "context"
  4. "net"
  5. "strconv"
  6. "go-common/app/admin/main/cache/model"
  7. "go-common/library/log"
  8. )
  9. var (
  10. apiserverURI = "http://cache-mng.bilibili.co/api/v1/appids/%s"
  11. )
  12. // OverlordClusters get all overlord clusters.
  13. func (d *Dao) OverlordClusters(c context.Context, zone, appid string) (ocs []*model.OverlordCluster, err error) {
  14. var res struct {
  15. Data []*model.OverlordApiserver `json:"grouped_clusters"`
  16. }
  17. if err = d.client.RESTfulGet(c, apiserverURI, "", nil, &res, appid); err != nil {
  18. log.Error("overlord cluster url(%s) appid(%s) error(%v)", apiserverURI, appid, err)
  19. return
  20. }
  21. GETALL:
  22. for _, oa := range res.Data {
  23. if zone == "" || oa.Group == zone {
  24. for _, oc := range oa.Clusters {
  25. cluster := &model.OverlordCluster{
  26. Name: oc.Name,
  27. Type: oc.Type,
  28. Zone: zone,
  29. HashMethod: "fnv1a_64",
  30. HashDistribution: "ketama",
  31. HashTag: "{}",
  32. ListenProto: "tcp",
  33. ListenAddr: net.JoinHostPort("0.0.0.0", strconv.Itoa(oc.FrontEndPort)),
  34. DailTimeout: 1000,
  35. ReadTimeout: 1000,
  36. WriteTimeout: 1000,
  37. NodeConn: 2,
  38. PingFailLimit: 3,
  39. PingAutoEject: true,
  40. }
  41. for _, oci := range oc.Instances {
  42. if oc.Type == "redis_cluster" && oci.Role != "master" {
  43. continue
  44. }
  45. on := &model.OverlordNode{
  46. Alias: oci.Alias,
  47. Addr: net.JoinHostPort(oci.IP, strconv.Itoa(oci.Port)),
  48. Weight: oci.Weight,
  49. }
  50. cluster.Nodes = append(cluster.Nodes, on)
  51. }
  52. ocs = append(ocs, cluster)
  53. }
  54. }
  55. }
  56. if len(ocs) == 0 && zone != "" {
  57. zone = ""
  58. goto GETALL
  59. }
  60. return
  61. }