/
sync.go
252 lines (204 loc) · 6.32 KB
/
sync.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package overdue
import (
"context"
_ "embed"
"fmt"
"github.com/google/uuid"
"github.com/icinga/icingadb/internal"
"github.com/icinga/icingadb/pkg/com"
"github.com/icinga/icingadb/pkg/contracts"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/icingadb/v1"
"github.com/icinga/icingadb/pkg/icingadb/v1/overdue"
"github.com/icinga/icingadb/pkg/icingaredis"
"github.com/icinga/icingadb/pkg/icingaredis/telemetry"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/icingadb/pkg/periodic"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
"golang.org/x/sync/errgroup"
"regexp"
"strconv"
"strings"
"time"
)
// Sync specifies the source and destination of an overdue sync.
type Sync struct {
db *icingadb.DB
redis *icingaredis.Client
logger *logging.Logger
}
// NewSync creates a new Sync.
func NewSync(db *icingadb.DB, redis *icingaredis.Client, logger *logging.Logger) *Sync {
return &Sync{
db: db,
redis: redis,
logger: logger,
}
}
// factory abstracts overdue.NewHostState and overdue.NewServiceState.
type factory = func(id string, overdue bool) (contracts.Entity, error)
// Sync synchronizes Redis overdue sets from s.redis to s.db.
func (s Sync) Sync(ctx context.Context) error {
{
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return s.initSync(ctx, "host")
})
g.Go(func() error {
return s.initSync(ctx, "service")
})
if err := g.Wait(); err != nil {
return errors.Wrap(err, "can't sync overdue indicators")
}
}
g, ctx := errgroup.WithContext(ctx)
var hostCounter com.Counter
defer s.log(ctx, "host", &hostCounter).Stop()
var serviceCounter com.Counter
defer s.log(ctx, "service", &serviceCounter).Stop()
g.Go(func() error {
return s.sync(ctx, "host", overdue.NewHostState, &hostCounter)
})
g.Go(func() error {
return s.sync(ctx, "service", overdue.NewServiceState, &serviceCounter)
})
return g.Wait()
}
// initSync initializes icingadb:overdue:objectType from the database.
func (s Sync) initSync(ctx context.Context, objectType string) error {
s.logger.Debugf("Refreshing already synced %s overdue indicators", objectType)
start := time.Now()
var rows []v1.IdMeta
query := fmt.Sprintf("SELECT id FROM %s_state WHERE is_overdue='y'", objectType)
if err := s.db.SelectContext(ctx, &rows, query); err != nil {
return internal.CantPerformQuery(err, query)
}
_, err := s.redis.Pipelined(ctx, func(pipe redis.Pipeliner) error {
key := "icingadb:overdue:" + objectType
pipe.Del(ctx, key)
var ids []interface{}
for _, row := range rows {
ids = append(ids, row.Id.String())
if len(ids) == 100 {
pipe.SAdd(ctx, key, ids...)
ids = nil
}
}
if len(ids) > 0 {
pipe.SAdd(ctx, key, ids...)
}
return nil
})
if err == nil {
s.logger.Debugf(
"Refreshing %d already synced %s overdue indicators took %s",
len(rows), objectType, time.Since(start),
)
} else {
err = errors.Wrap(err, "can't execute Redis pipeline")
}
return err
}
// log periodically logs sync's workload.
func (s Sync) log(ctx context.Context, objectType string, counter *com.Counter) periodic.Stopper {
return periodic.Start(ctx, s.logger.Interval(), func(_ periodic.Tick) {
if count := counter.Reset(); count > 0 {
s.logger.Infof("Synced %d %s overdue indicators", count, objectType)
}
})
}
//go:embed get_overdues.lua
var getOverduesLua string
var luaGetOverdues = redis.NewScript(strings.TrimSpace(
regexp.MustCompile(`(?m)^--.*?$`).ReplaceAllString(getOverduesLua, ""),
))
// sync synchronizes Redis overdue sets from s.redis to s.db for objectType.
func (s Sync) sync(ctx context.Context, objectType string, factory factory, counter *com.Counter) error {
s.logger.Debugf("Syncing %s overdue indicators", objectType)
keys := [3]string{"icinga:nextupdate:" + objectType, "icingadb:overdue:" + objectType, ""}
if rand, err := uuid.NewRandom(); err == nil {
keys[2] = rand.String()
} else {
return errors.Wrap(err, "can't create random UUID")
}
const period = 2 * time.Second
periodically := time.NewTicker(period)
defer periodically.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-periodically.C:
overdues, err := luaGetOverdues.Run(
ctx, s.redis, keys[:], strconv.FormatInt(time.Now().Unix(), 10),
).Result()
if err != nil {
return errors.Wrap(err, "can't execute Redis script")
}
root := overdues.([]interface{})
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return s.updateOverdue(ctx, objectType, factory, counter, root[0].([]interface{}), true)
})
g.Go(func() error {
return s.updateOverdue(ctx, objectType, factory, counter, root[1].([]interface{}), false)
})
if err := g.Wait(); err != nil {
return errors.Wrap(err, "can't update overdue indicators")
}
// For the case that syncing has taken some time, delay the next sync.
periodically.Reset(period)
select {
case <-periodically.C: // Clean up periodically.C after reset...
default: // ... unless it's already clean.
}
}
}
}
// updateOverdue sets objectType_state#is_overdue for ids to overdue
// and updates icingadb:overdue:objectType respectively.
func (s Sync) updateOverdue(
ctx context.Context, objectType string, factory factory, counter *com.Counter, ids []interface{}, overdue bool,
) error {
if len(ids) < 1 {
return nil
}
if err := s.updateDb(ctx, factory, ids, overdue); err != nil {
return errors.Wrap(err, "can't update overdue indicators")
}
counter.Add(uint64(len(ids)))
telemetry.Stats.Overdue.Add(uint64(len(ids)))
var op func(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
if overdue {
op = s.redis.SAdd
} else {
op = s.redis.SRem
}
_, err := op(ctx, "icingadb:overdue:"+objectType, ids...).Result()
return err
}
// updateDb sets objectType_state#is_overdue for ids to overdue.
func (s Sync) updateDb(ctx context.Context, factory factory, ids []interface{}, overdue bool) error {
g, ctx := errgroup.WithContext(ctx)
ch := make(chan contracts.Entity, 1<<10)
g.Go(func() error {
defer close(ch)
for _, id := range ids {
e, err := factory(id.(string), overdue)
if err != nil {
return errors.Wrap(err, "can't create entity")
}
select {
case <-ctx.Done():
return ctx.Err()
case ch <- e:
}
}
return nil
})
g.Go(func() error {
return s.db.UpdateStreamed(ctx, ch)
})
return g.Wait()
}