-
Notifications
You must be signed in to change notification settings - Fork 487
/
cluster.go
179 lines (146 loc) · 4.8 KB
/
cluster.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
177
178
179
package cluster
import (
"context"
"fmt"
"sync"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/protobuf/ptypes/empty"
"github.com/gorilla/mux"
"github.com/grafana/agent/pkg/agentproto"
"github.com/grafana/agent/pkg/metrics/instance"
"github.com/grafana/agent/pkg/metrics/instance/configstore"
"github.com/grafana/agent/pkg/util"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
)
// Cluster connects an Agent to other Agents and allows them to distribute
// workload.
type Cluster struct {
mut sync.RWMutex
log log.Logger
cfg Config
baseValidation ValidationFunc
//
// Internally, Cluster glues together four separate pieces of logic.
// See comments below to get an understanding of what is going on.
//
// node manages membership in the cluster and performs cluster-wide reshards.
node *node
// store connects to a configstore for changes. storeAPI is an HTTP API for it.
store *configstore.Remote
storeAPI *configstore.API
// watcher watches the store and applies changes to an instance.Manager,
// triggering metrics to be collected and sent. configWatcher also does a
// complete refresh of its state on an interval.
watcher *configWatcher
}
// New creates a new Cluster.
func New(
l log.Logger,
reg prometheus.Registerer,
cfg Config,
im instance.Manager,
validate ValidationFunc,
) (*Cluster, error) {
l = log.With(l, "component", "cluster")
var (
c = &Cluster{log: l, cfg: cfg, baseValidation: validate}
err error
)
// Hold the lock for the initialization. This is necessary since newNode will
// eventually call Reshard, and we want c.watcher to be initialized when that
// happens.
c.mut.Lock()
defer c.mut.Unlock()
c.node, err = newNode(reg, l, cfg, c)
if err != nil {
return nil, fmt.Errorf("failed to initialize node membership: %w", err)
}
c.store, err = configstore.NewRemote(l, reg, cfg.KVStore.Config, cfg.Enabled)
if err != nil {
return nil, fmt.Errorf("failed to initialize configstore: %w", err)
}
c.storeAPI = configstore.NewAPI(l, c.store, c.storeValidate, cfg.APIEnableGetConfiguration)
reg.MustRegister(c.storeAPI)
c.watcher, err = newConfigWatcher(l, cfg, c.store, im, c.node.Owns, validate)
if err != nil {
return nil, fmt.Errorf("failed to initialize configwatcher: %w", err)
}
// NOTE(rfratto): ApplyConfig isn't necessary for the initialization but must
// be called for any changes to the configuration.
return c, nil
}
func (c *Cluster) storeValidate(cfg *instance.Config) error {
c.mut.RLock()
defer c.mut.RUnlock()
if err := c.baseValidation(cfg); err != nil {
return err
}
if c.cfg.DangerousAllowReadingFiles {
return nil
}
// If configs aren't allowed to read from the store, we need to make sure no
// configs coming in from the API set files for passwords.
return validateNofiles(cfg)
}
// Reshard implements agentproto.ScrapingServiceServer, and syncs the state of
// configs with the configstore.
func (c *Cluster) Reshard(ctx context.Context, _ *agentproto.ReshardRequest) (*empty.Empty, error) {
c.mut.RLock()
defer c.mut.RUnlock()
level.Info(c.log).Log("msg", "received reshard notification, requesting refresh")
c.watcher.RequestRefresh()
return &empty.Empty{}, nil
}
// ApplyConfig applies configuration changes to Cluster.
func (c *Cluster) ApplyConfig(cfg Config) error {
c.mut.Lock()
defer c.mut.Unlock()
if util.CompareYAML(c.cfg, cfg) {
return nil
}
if err := c.node.ApplyConfig(cfg); err != nil {
return fmt.Errorf("failed to apply config to node membership: %w", err)
}
if err := c.store.ApplyConfig(cfg.Lifecycler.RingConfig.KVStore, cfg.Enabled); err != nil {
return fmt.Errorf("failed to apply config to config store: %w", err)
}
if err := c.watcher.ApplyConfig(cfg); err != nil {
return fmt.Errorf("failed to apply config to watcher: %w", err)
}
c.cfg = cfg
// Force a refresh so all the configs get updated with new defaults.
level.Info(c.log).Log("msg", "cluster config changed, queueing refresh")
c.watcher.RequestRefresh()
return nil
}
// WireAPI injects routes into the provided mux router for the config
// management API.
func (c *Cluster) WireAPI(r *mux.Router) {
c.storeAPI.WireAPI(r)
c.node.WireAPI(r)
}
// WireGRPC injects gRPC server handlers into the provided gRPC server.
func (c *Cluster) WireGRPC(srv *grpc.Server) {
agentproto.RegisterScrapingServiceServer(srv, c)
}
// Stop stops the cluster and all of its dependencies.
func (c *Cluster) Stop() {
c.mut.Lock()
defer c.mut.Unlock()
deps := []struct {
name string
closer func() error
}{
{"node", c.node.Stop},
{"config store", c.store.Close},
{"config watcher", c.watcher.Stop},
}
for _, dep := range deps {
err := dep.closer()
if err != nil {
level.Error(c.log).Log("msg", "failed to stop dependency", "dependency", dep.name, "err", err)
}
}
}