Skip to content

Commit 6e5a872

Browse files
config, api: add a config to turn off traffic replay on TiDB Cloud (#738) (#800)
Signed-off-by: ti-chi-bot <[email protected]> Co-authored-by: djshow832 <[email protected]>
1 parent a69bb7c commit 6e5a872

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

lib/config/proxy.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ var (
2222
)
2323

2424
type Config struct {
25-
Proxy ProxyServer `yaml:"proxy,omitempty" toml:"proxy,omitempty" json:"proxy,omitempty"`
26-
API API `yaml:"api,omitempty" toml:"api,omitempty" json:"api,omitempty"`
27-
Advance Advance `yaml:"advance,omitempty" toml:"advance,omitempty" json:"advance,omitempty"`
28-
Workdir string `yaml:"workdir,omitempty" toml:"workdir,omitempty" json:"workdir,omitempty"`
29-
Security Security `yaml:"security,omitempty" toml:"security,omitempty" json:"security,omitempty"`
30-
Log Log `yaml:"log,omitempty" toml:"log,omitempty" json:"log,omitempty"`
31-
Balance Balance `yaml:"balance,omitempty" toml:"balance,omitempty" json:"balance,omitempty"`
32-
Labels map[string]string `yaml:"labels,omitempty" toml:"labels,omitempty" json:"labels,omitempty"`
33-
HA HA `yaml:"ha,omitempty" toml:"ha,omitempty" json:"ha,omitempty"`
25+
Proxy ProxyServer `yaml:"proxy,omitempty" toml:"proxy,omitempty" json:"proxy,omitempty"`
26+
API API `yaml:"api,omitempty" toml:"api,omitempty" json:"api,omitempty"`
27+
Advance Advance `yaml:"advance,omitempty" toml:"advance,omitempty" json:"advance,omitempty"`
28+
Workdir string `yaml:"workdir,omitempty" toml:"workdir,omitempty" json:"workdir,omitempty"`
29+
Security Security `yaml:"security,omitempty" toml:"security,omitempty" json:"security,omitempty"`
30+
Log Log `yaml:"log,omitempty" toml:"log,omitempty" json:"log,omitempty"`
31+
Balance Balance `yaml:"balance,omitempty" toml:"balance,omitempty" json:"balance,omitempty"`
32+
Labels map[string]string `yaml:"labels,omitempty" toml:"labels,omitempty" json:"labels,omitempty"`
33+
HA HA `yaml:"ha,omitempty" toml:"ha,omitempty" json:"ha,omitempty"`
34+
EnableTrafficReplay bool `yaml:"enable-traffic-replay,omitempty" toml:"enable-traffic-replay,omitempty" json:"enable-traffic-replay,omitempty"`
3435
}
3536

3637
type KeepAlive struct {
@@ -164,6 +165,8 @@ func NewConfig() *Config {
164165

165166
cfg.Balance = DefaultBalance()
166167

168+
cfg.EnableTrafficReplay = true
169+
167170
return &cfg
168171
}
169172

pkg/server/api/traffic.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ import (
1616
func (h *Server) registerTraffic(group *gin.RouterGroup) {
1717
group.POST("/capture", h.TrafficCapture)
1818
group.POST("/replay", h.TrafficReplay)
19-
group.POST("/cancel", h.TrafficStop)
19+
group.POST("/cancel", h.TrafficCancel)
2020
group.GET("/show", h.TrafficShow)
2121
}
2222

2323
func (h *Server) TrafficCapture(c *gin.Context) {
24+
globalCfg := h.mgr.CfgMgr.GetConfig()
25+
if !globalCfg.EnableTrafficReplay {
26+
c.String(http.StatusBadRequest, "traffic capture is disabled")
27+
return
28+
}
29+
2430
cfg := capture.CaptureConfig{}
2531
cfg.Output = c.PostForm("output")
2632
if durationStr := c.PostForm("duration"); durationStr != "" {
@@ -40,6 +46,12 @@ func (h *Server) TrafficCapture(c *gin.Context) {
4046
}
4147

4248
func (h *Server) TrafficReplay(c *gin.Context) {
49+
globalCfg := h.mgr.CfgMgr.GetConfig()
50+
if !globalCfg.EnableTrafficReplay {
51+
c.String(http.StatusBadRequest, "traffic replay is disabled")
52+
return
53+
}
54+
4355
cfg := replay.ReplayConfig{}
4456
cfg.Input = c.PostForm("input")
4557
if speedStr := c.PostForm("speed"); speedStr != "" {
@@ -60,12 +72,24 @@ func (h *Server) TrafficReplay(c *gin.Context) {
6072
c.String(http.StatusOK, "replay started")
6173
}
6274

63-
func (h *Server) TrafficStop(c *gin.Context) {
75+
func (h *Server) TrafficCancel(c *gin.Context) {
76+
globalCfg := h.mgr.CfgMgr.GetConfig()
77+
if !globalCfg.EnableTrafficReplay {
78+
c.String(http.StatusBadRequest, "traffic cancel is disabled")
79+
return
80+
}
81+
6482
result := h.mgr.ReplayJobMgr.Stop()
6583
c.String(http.StatusOK, result)
6684
}
6785

6886
func (h *Server) TrafficShow(c *gin.Context) {
87+
globalCfg := h.mgr.CfgMgr.GetConfig()
88+
if !globalCfg.EnableTrafficReplay {
89+
c.String(http.StatusBadRequest, "traffic show is disabled")
90+
return
91+
}
92+
6993
result := h.mgr.ReplayJobMgr.Jobs()
7094
c.String(http.StatusOK, result)
7195
}

pkg/server/api/traffic_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,42 @@ func TestTraffic(t *testing.T) {
8888
})
8989
}
9090

91+
func TestDisableTrafficReplay(t *testing.T) {
92+
server, doHTTP := createServer(t)
93+
server.mgr.CfgMgr.GetConfig().EnableTrafficReplay = false
94+
95+
doHTTP(t, http.MethodPost, "/api/traffic/capture", httpOpts{
96+
reader: cli.GetFormReader(map[string]string{"output": "/tmp", "duration": "1h"}),
97+
header: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
98+
}, func(t *testing.T, r *http.Response) {
99+
require.Equal(t, http.StatusBadRequest, r.StatusCode)
100+
all, err := io.ReadAll(r.Body)
101+
require.NoError(t, err)
102+
require.Equal(t, "traffic capture is disabled", string(all))
103+
})
104+
doHTTP(t, http.MethodPost, "/api/traffic/replay", httpOpts{
105+
reader: cli.GetFormReader(map[string]string{"input": "/tmp"}),
106+
header: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
107+
}, func(t *testing.T, r *http.Response) {
108+
require.Equal(t, http.StatusBadRequest, r.StatusCode)
109+
all, err := io.ReadAll(r.Body)
110+
require.NoError(t, err)
111+
require.Equal(t, "traffic replay is disabled", string(all))
112+
})
113+
doHTTP(t, http.MethodPost, "/api/traffic/cancel", httpOpts{}, func(t *testing.T, r *http.Response) {
114+
require.Equal(t, http.StatusBadRequest, r.StatusCode)
115+
all, err := io.ReadAll(r.Body)
116+
require.NoError(t, err)
117+
require.Equal(t, "traffic cancel is disabled", string(all))
118+
})
119+
doHTTP(t, http.MethodGet, "/api/traffic/show", httpOpts{}, func(t *testing.T, r *http.Response) {
120+
require.Equal(t, http.StatusBadRequest, r.StatusCode)
121+
all, err := io.ReadAll(r.Body)
122+
require.NoError(t, err)
123+
require.Equal(t, "traffic show is disabled", string(all))
124+
})
125+
}
126+
91127
var _ manager.JobManager = (*mockReplayJobManager)(nil)
92128

93129
type mockReplayJobManager struct {

0 commit comments

Comments
 (0)