This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
/
ccache.go
137 lines (121 loc) · 3.44 KB
/
ccache.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
package api
import (
"context"
"encoding/json"
"net/http"
"sync"
"github.com/grafana/metrictank/api/middleware"
"github.com/grafana/metrictank/api/models"
"github.com/grafana/metrictank/api/response"
"github.com/grafana/metrictank/cluster"
"github.com/grafana/metrictank/expr/tagquery"
"github.com/grafana/metrictank/idx"
log "github.com/sirupsen/logrus"
)
func (s *Server) ccacheDelete(ctx *middleware.Context, req models.CCacheDelete) {
res := models.CCacheDeleteResp{}
code := http.StatusOK
if req.Propagate {
res.Peers = s.ccacheDeletePropagate(ctx.Req.Context(), &req)
for _, peer := range res.Peers {
if peer.Errors > 0 {
code = http.StatusInternalServerError
}
}
}
// nothing to do on query nodes. they have no index or chunk cache
if s.MetricIndex == nil {
response.Write(ctx, response.NewJson(code, res, ""))
return
}
fullFlush := false
for _, pattern := range req.Patterns {
if pattern == "**" {
fullFlush = true
}
}
if fullFlush {
delSeries, delArchives := s.Cache.Reset()
res.DeletedSeries += delSeries
res.DeletedArchives += delArchives
} else {
var toClear []idx.Node
if len(req.Patterns) > 0 {
for _, pattern := range req.Patterns {
nodes, err := s.MetricIndex.Find(req.OrgId, pattern, 0)
if err != nil {
res.AddError(err)
code = http.StatusInternalServerError
} else {
toClear = append(toClear, nodes...)
}
}
}
if len(req.Expr) > 0 {
expressions, err := tagquery.ParseExpressions(req.Expr)
if err != nil {
res.AddError(err)
code = http.StatusBadRequest
} else {
query, err := tagquery.NewQuery(expressions, 0)
if err != nil {
res.AddError(err)
code = http.StatusInternalServerError
} else {
nodes := s.MetricIndex.FindByTag(req.OrgId, query)
toClear = append(toClear, nodes...)
}
}
}
if len(toClear) > 0 {
for _, node := range toClear {
for _, def := range node.Defs {
delSeries, delArchives := s.Cache.DelMetric(def.Id)
res.DeletedSeries += delSeries
res.DeletedArchives += delArchives
}
}
}
}
response.Write(ctx, response.NewJson(code, res, ""))
}
func (s *Server) ccacheDeletePropagate(ctx context.Context, req *models.CCacheDelete) map[string]models.CCacheDeleteResp {
// we never want to propagate more than once to avoid loops
req.Propagate = false
peers := cluster.Manager.MemberList(false, true)
peerResults := make(map[string]models.CCacheDeleteResp)
var mu sync.Mutex
var wg sync.WaitGroup
for _, peer := range peers {
if peer.IsLocal() {
continue
}
wg.Add(1)
go func(peer cluster.Node) {
mu.Lock()
defer mu.Unlock()
peerResults[peer.GetName()] = s.ccacheDeleteRemote(ctx, req, peer)
wg.Done()
}(peer)
}
wg.Wait()
return peerResults
}
func (s *Server) ccacheDeleteRemote(ctx context.Context, req *models.CCacheDelete, peer cluster.Node) models.CCacheDeleteResp {
var res models.CCacheDeleteResp
log.Debugf("HTTP metricDelete calling %s/ccache/delete", peer.GetName())
buf, err := peer.Post(ctx, "ccacheDeleteRemote", "/ccache/delete", *req)
if err != nil {
log.Errorf("HTTP ccacheDelete error querying %s/ccache/delete: %q", peer.GetName(), err.Error())
res.FirstError = err.Error()
res.Errors++
return res
}
err = json.Unmarshal(buf, &res)
if err != nil {
log.Errorf("HTTP ccacheDelete error unmarshaling body from %s/ccache/delete: %q", peer.GetName(), err.Error())
res.FirstError = err.Error()
res.Errors++
}
return res
}