forked from pingcap/tidb-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
140 lines (123 loc) · 3.95 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
// clusterinfo is a directory for ClusterInfoServer, which could load topology from pd
// using Etcd v3 interface and pd interface.
package clusterinfo
import (
"context"
"fmt"
"net/http"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/pingcap-incubator/tidb-dashboard/pkg/apiserver/user"
"github.com/pingcap-incubator/tidb-dashboard/pkg/config"
"github.com/pingcap-incubator/tidb-dashboard/pkg/pd"
"github.com/pingcap-incubator/tidb-dashboard/pkg/utils/clusterinfo"
)
type Service struct {
config *config.Config
etcdProvider pd.EtcdProvider
httpClient *http.Client
}
func NewService(config *config.Config, etcdProvider pd.EtcdProvider, httpClient *http.Client) *Service {
return &Service{config: config, etcdProvider: etcdProvider, httpClient: httpClient}
}
func (s *Service) Register(r *gin.RouterGroup, auth *user.AuthService) {
endpoint := r.Group("/topology")
endpoint.Use(auth.MWAuthRequired())
endpoint.GET("/all", s.topologyHandler)
endpoint.DELETE("/tidb/:address/", s.deleteTiDBTopologyHandler)
endpoint.GET("/alertmanager/:address/count", s.topologyGetAlertCount)
}
// @Summary Delete etcd's tidb key.
// @Description Delete etcd's TiDB key with ip:port.
// @Produce json
// @Success 204 "delete ok"
// @Failure 401 {object} utils.APIError "Unauthorized failure"
// @Router /topology/address [delete]
func (s *Service) deleteTiDBTopologyHandler(c *gin.Context) {
address := c.Param("address")
errorChannel := make(chan error, 2)
ttlKey := fmt.Sprintf("/topology/tidb/%v/ttl", address)
nonTTLKey := fmt.Sprintf("/topology/tidb/%v/info", address)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
var wg sync.WaitGroup
for _, key := range []string{ttlKey, nonTTLKey} {
wg.Add(1)
go func(toDel string) {
defer wg.Done()
if _, err := s.etcdProvider.GetEtcdClient().Delete(ctx, toDel); err != nil {
errorChannel <- err
}
}(key)
}
wg.Wait()
var err error
select {
case err = <-errorChannel:
default:
}
close(errorChannel)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, nil)
}
// @Summary Get all Dashboard topology and liveness.
// @Description Get information about the dashboard topology.
// @Produce json
// @Success 200 {object} clusterinfo.ClusterInfo
// @Router /topology/all [get]
// @Security JwtAuth
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) topologyHandler(c *gin.Context) {
var returnObject ClusterInfo
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
fetchers := []func(ctx context.Context, service *Service, info *ClusterInfo){
fillTopologyUnderEtcd,
fillTiKVTopology,
fillPDTopology,
}
var wg sync.WaitGroup
for _, fetcher := range fetchers {
wg.Add(1)
currentFetcher := fetcher
go func() {
defer wg.Done()
currentFetcher(ctx, s, &returnObject)
}()
}
wg.Wait()
c.JSON(http.StatusOK, returnObject)
}
// @Summary Get the count of alert
// @Description Get alert number of the alert manager.
// @Produce json
// @Success 200 {object} int
// @Param address path string true "ip:port"
// @Router /topology/alertmanager/{address}/count [get]
// @Security JwtAuth
// @Failure 401 {object} utils.APIError "Unauthorized failure"
func (s *Service) topologyGetAlertCount(c *gin.Context) {
address := c.Param("address")
cnt, err := clusterinfo.GetAlertCountByAddress(address, s.httpClient)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, cnt)
}