-
Notifications
You must be signed in to change notification settings - Fork 2k
/
v1.go
176 lines (147 loc) · 5.12 KB
/
v1.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package v1
import (
"net/http"
"sync"
"time"
"github.com/go-kit/log"
"github.com/oklog/ulid"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/common/route"
"github.com/thanos-io/objstore"
"github.com/thanos-io/thanos/pkg/api"
"github.com/thanos-io/thanos/pkg/block"
"github.com/thanos-io/thanos/pkg/block/metadata"
extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http"
"github.com/thanos-io/thanos/pkg/logging"
)
// BlocksAPI is a very simple API used by Thanos Block Viewer.
type BlocksAPI struct {
baseAPI *api.BaseAPI
logger log.Logger
globalBlocksInfo *BlocksInfo
loadedBlocksInfo *BlocksInfo
globalLock, loadedLock sync.Mutex
disableCORS bool
bkt objstore.Bucket
disableAdminOperations bool
}
type BlocksInfo struct {
Label string `json:"label"`
Blocks []metadata.Meta `json:"blocks"`
RefreshedAt time.Time `json:"refreshedAt"`
Err error `json:"err"`
}
type ActionType int32
const (
Deletion ActionType = iota
NoCompaction
Unknown
)
func parse(s string) ActionType {
switch s {
case "DELETION":
return Deletion
case "NO_COMPACTION":
return NoCompaction
default:
return Unknown
}
}
// NewBlocksAPI creates a simple API to be used by Thanos Block Viewer.
func NewBlocksAPI(logger log.Logger, disableCORS bool, label string, flagsMap map[string]string, bkt objstore.Bucket) *BlocksAPI {
disableAdminOperations := flagsMap["disable-admin-operations"] == "true"
return &BlocksAPI{
baseAPI: api.NewBaseAPI(logger, disableCORS, flagsMap),
logger: logger,
globalBlocksInfo: &BlocksInfo{
Blocks: []metadata.Meta{},
Label: label,
},
loadedBlocksInfo: &BlocksInfo{
Blocks: []metadata.Meta{},
Label: label,
},
disableCORS: disableCORS,
bkt: bkt,
disableAdminOperations: disableAdminOperations,
}
}
func (bapi *BlocksAPI) Register(r *route.Router, tracer opentracing.Tracer, logger log.Logger, ins extpromhttp.InstrumentationMiddleware, logMiddleware *logging.HTTPServerMiddleware) {
bapi.baseAPI.Register(r, tracer, logger, ins, logMiddleware)
instr := api.GetInstr(tracer, logger, ins, logMiddleware, bapi.disableCORS)
r.Get("/blocks", instr("blocks", bapi.blocks))
r.Post("/blocks/mark", instr("blocks_mark", bapi.markBlock))
}
func (bapi *BlocksAPI) markBlock(r *http.Request) (interface{}, []error, *api.ApiError, func()) {
if bapi.disableAdminOperations {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: errors.New("Admin operations are disabled")}, func() {}
}
idParam := r.FormValue("id")
actionParam := r.FormValue("action")
detailParam := r.FormValue("detail")
if idParam == "" {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: errors.New("ID cannot be empty")}, func() {}
}
if actionParam == "" {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: errors.New("Action cannot be empty")}, func() {}
}
id, err := ulid.Parse(idParam)
if err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: errors.Errorf("ULID %q is not valid: %v", idParam, err)}, func() {}
}
actionType := parse(actionParam)
switch actionType {
case Deletion:
err := block.MarkForDeletion(r.Context(), bapi.logger, bapi.bkt, id, detailParam, promauto.With(nil).NewCounter(prometheus.CounterOpts{}))
if err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
}
case NoCompaction:
err := block.MarkForNoCompact(r.Context(), bapi.logger, bapi.bkt, id, metadata.ManualNoCompactReason, detailParam, promauto.With(nil).NewCounter(prometheus.CounterOpts{}))
if err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
}
default:
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: errors.Errorf("not supported marker %v", actionParam)}, func() {}
}
return nil, nil, nil, func() {}
}
func (bapi *BlocksAPI) blocks(r *http.Request) (interface{}, []error, *api.ApiError, func()) {
viewParam := r.URL.Query().Get("view")
if viewParam == "loaded" {
bapi.loadedLock.Lock()
defer bapi.loadedLock.Unlock()
return bapi.loadedBlocksInfo, nil, nil, func() {}
}
bapi.globalLock.Lock()
defer bapi.globalLock.Unlock()
return bapi.globalBlocksInfo, nil, nil, func() {}
}
func (b *BlocksInfo) set(blocks []metadata.Meta, err error) {
if err != nil {
// Last view is maintained.
b.RefreshedAt = time.Now()
b.Err = err
return
}
b.RefreshedAt = time.Now()
b.Blocks = blocks
b.Err = err
}
// SetGlobal updates the global blocks' metadata in the API.
func (bapi *BlocksAPI) SetGlobal(blocks []metadata.Meta, err error) {
bapi.globalLock.Lock()
defer bapi.globalLock.Unlock()
bapi.globalBlocksInfo.set(blocks, err)
}
// SetLoaded updates the local blocks' metadata in the API.
func (bapi *BlocksAPI) SetLoaded(blocks []metadata.Meta, err error) {
bapi.loadedLock.Lock()
defer bapi.loadedLock.Unlock()
bapi.loadedBlocksInfo.set(blocks, err)
}