-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathobserving_clone.go
512 lines (387 loc) · 13.2 KB
/
observing_clone.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
2020 © Postgres.ai
*/
// Package observer provides clone monitoring.
package observer
import (
"context"
"fmt"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
"github.com/jackc/pgx/v4"
"github.com/pkg/errors"
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/resources"
"gitlab.com/postgres-ai/database-lab/v3/pkg/client/dblabapi/types"
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
"gitlab.com/postgres-ai/database-lab/v3/pkg/models"
)
const (
csvFields = "log_time,user_name,database_name,process_id,connection_from,session_id,session_line_num,command_tag," +
"session_start_time,virtual_transaction_id,transaction_id,error_severity,sql_state_code,message,detail,hint," +
"internal_query,internal_query_pos,context,query,query_pos,location,application_name"
)
// maskedFields contains list of the fields which should be filtered if replacement rules are defined.
var maskedFields = map[string]struct{}{
"message": {},
"detail": {},
"hint": {},
"internal_query": {},
"query": {},
}
// ObservingClone describes an entity containing observability sessions.
type ObservingClone struct {
pool *resources.Pool
cloneID string
port uint
superUserDB *pgx.Conn
config types.Config
ctx context.Context
cancel context.CancelFunc
done chan struct{}
db *pgx.Conn
csvFields string
maskedIndexes []int
session *Session
// TODO: add lock to prevent running of several session simultaneously.
registryMu *sync.Mutex
sessionRegistry map[uint64]struct{}
}
// Session returns the current observability session.
func (c *ObservingClone) Session() *Session {
if c.session == nil {
return nil
}
session := *c.session
return &session
}
// NewObservingClone creates a new observing clone.
func NewObservingClone(config types.Config, sudb *pgx.Conn) *ObservingClone {
if config.ObservationInterval == 0 {
config.ObservationInterval = defaultIntervalSeconds
}
if config.MaxLockDuration == 0 {
config.MaxLockDuration = defaultMaxLockDurationSeconds
}
if config.MaxDuration == 0 {
config.MaxDuration = defaultMaxDurationSeconds
}
ctx, cancel := context.WithCancel(context.Background())
observingClone := &ObservingClone{
config: config,
ctx: ctx,
cancel: cancel,
done: make(chan struct{}, 1),
csvFields: csvFields,
registryMu: &sync.Mutex{},
sessionRegistry: make(map[uint64]struct{}),
superUserDB: sudb,
}
observingClone.fillMaskedIndexes()
return observingClone
}
// Config returns config of the observing clone.
func (c *ObservingClone) Config() types.Config {
return c.config
}
// CsvFields returns a comma-separated list of available csv fields.
func (c *ObservingClone) CsvFields() string {
return c.csvFields
}
// fillMaskedIndexes discovers indexes of the fields which should be masked.
func (c *ObservingClone) fillMaskedIndexes() {
c.maskedIndexes = make([]int, 0, len(maskedFields))
for i, csvField := range strings.Split(c.csvFields, ",") {
if _, ok := maskedFields[csvField]; ok {
c.maskedIndexes = append(c.maskedIndexes, i)
}
}
}
// AddArtifact adds a new observation session to storage.
func (c *ObservingClone) AddArtifact(sessionID uint64) {
c.registryMu.Lock()
defer c.registryMu.Unlock()
c.sessionRegistry[sessionID] = struct{}{}
}
// GetArtifactList returns available artifact session IDs for the requested clone.
func (c *ObservingClone) GetArtifactList() []uint64 {
sessionIDs := []uint64{}
c.registryMu.Lock()
defer c.registryMu.Unlock()
for v := range c.sessionRegistry {
sessionIDs = append(sessionIDs, v)
}
return sessionIDs
}
// IsExistArtifacts checks if observation session artifacts exist.
func (c *ObservingClone) IsExistArtifacts(sessionID uint64) bool {
c.registryMu.Lock()
defer c.registryMu.Unlock()
_, ok := c.sessionRegistry[sessionID]
return ok
}
// ReadSummary reads summary file.
func (c *ObservingClone) ReadSummary(sessionID uint64) ([]byte, error) {
return c.readFileStats(sessionID, summaryFilename)
}
// Init initializes observation session.
func (c *ObservingClone) Init(clone *models.Clone, sessionID uint64, startedAt time.Time, tags map[string]string) error {
c.session = NewSession(sessionID, startedAt, c.config, tags)
log.Dbg("Init observation for SessionID: ", c.session.SessionID)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := c.createObservationDir(); err != nil {
return errors.Wrap(err, "failed to create the observation directory")
}
db, err := InitConnection(clone, c.pool.SocketDir())
if err != nil {
return errors.Wrap(err, "cannot connect to database")
}
c.db = db
if err := c.discoverLogFields(ctx); err != nil {
return errors.Wrap(err, "failed to discover available log fields")
}
if err := c.getDBSize(ctx, &c.session.state.InitialDBSize); err != nil {
return errors.Wrap(err, "failed to get the initial database size")
}
if err := c.resetStat(ctx); err != nil {
return errors.Wrap(err, "failed to reset clone statistics")
}
return nil
}
// RunSession runs observing session.
func (c *ObservingClone) RunSession() error {
if c.session == nil || c.db.IsClosed() {
return errors.New("failed to run session because it has not been initialized")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
timestamp := time.Now()
sleepDuration := time.Duration(c.session.Config.ObservationInterval) * time.Second
timer := time.NewTimer(sleepDuration)
defer timer.Stop()
defer func() {
if err := c.db.Close(ctx); err != nil {
log.Err("Failed to close a database connection after observation for SessionID: ", c.session.SessionID)
}
}()
c.session.Result = &models.ObservationResult{}
for {
select {
case <-c.ctx.Done():
case <-timer.C:
}
dangerousLocks, err := runQuery(ctx, c.db, buildLocksMetricQuery(observerApplicationName, c.session.Config.MaxLockDuration))
if err != nil {
return errors.Wrap(err, "cannot query metrics")
}
c.session.Result.Summary.TotalIntervals++
c.session.Result.Summary.TotalDuration = time.Since(c.session.StartedAt).Seconds()
if dangerousLocks != "" {
c.session.Result.Summary.WarningIntervals++
}
interval := models.Interval{
StartedAt: timestamp,
Duration: time.Since(timestamp).Seconds(),
Warning: dangerousLocks,
}
c.session.Result.Intervals = append(c.session.Result.Intervals, interval)
timestamp = time.Now()
if err := c.ctx.Err(); err != nil {
log.Dbg("Stop observation for SessionID: ", c.session.SessionID)
if err := c.storeArtifacts(); err != nil {
log.Err("Failed to store artifacts: ", err)
}
c.done <- struct{}{}
return nil
}
timer.Reset(sleepDuration)
}
}
func (c *ObservingClone) createObservationDir() error {
sessionPath := c.currentArtifactsSessionPath()
artifactsPath := path.Join(sessionPath, artifactsSubDir)
if err := os.RemoveAll(artifactsPath); err != nil {
return err
}
if err := os.MkdirAll(artifactsPath, 0777); err != nil {
return err
}
log.Dbg("Artifacts path", artifactsPath)
return nil
}
func (c *ObservingClone) resetStat(ctx context.Context) error {
if _, err := c.superUserDB.Exec(ctx, `create extension if not exists pg_stat_statements`); err != nil {
return errors.Wrap(err, "failed to reset statistics counters for the current database")
}
if _, err := c.superUserDB.Exec(ctx, `create extension if not exists logerrors`); err != nil {
return errors.Wrap(err, "failed to create the logerrors extension")
}
if _, err := c.superUserDB.Exec(ctx, `create extension if not exists pg_stat_kcache`); err != nil {
return errors.Wrap(err, "failed to create the pg_stat_kcache extension")
}
if _, err := c.superUserDB.Exec(ctx,
`select
pg_stat_reset(),
pg_stat_reset_shared('bgwriter')`); err != nil {
return errors.Wrap(err, "failed to reset statistics counters for the current database")
}
if _, err := c.superUserDB.Exec(ctx, `select pg_stat_statements_reset()`); err != nil {
return errors.Wrap(err, "failed to reset statement statistics for the current database")
}
if _, err := c.superUserDB.Exec(ctx, `select pg_stat_kcache_reset()`); err != nil {
return errors.Wrap(err, "failed to reset kcache statistics for the current database")
}
// TODO: uncomment for Postgres 13+
// if _, err := c.superUserDB.Exec(ctx, `select pg_stat_reset_slru()`); err != nil {
// return errors.Wrap(err, "failed to reset slru statistics for the current database")
// }
if _, err := c.superUserDB.Exec(ctx, `select pg_log_errors_reset()`); err != nil {
return errors.Wrap(err, "failed to reset log errors statistics for the current database")
}
log.Dbg("Stats have been reset")
return nil
}
func (c *ObservingClone) storeArtifacts() error {
log.Dbg("Store observation artifacts for SessionID: ", c.session.SessionID)
dstPath := path.Join(c.currentArtifactsSessionPath(), artifactsSubDir)
if err := os.MkdirAll(dstPath, 0666); err != nil {
return errors.Wrapf(err, "cannot create an artifact directory %s", dstPath)
}
ctx := context.Background()
if err := c.dumpStatementsStats(ctx); err != nil {
return err
}
if err := c.dumpDatabaseStats(ctx); err != nil {
return err
}
if err := c.dumpDatabaseErrors(ctx); err != nil {
return err
}
if err := c.dumpBGWriterStats(ctx); err != nil {
return err
}
if err := c.dumpKCacheStats(ctx); err != nil {
return err
}
if err := c.dumpIndexStats(ctx); err != nil {
return err
}
if err := c.dumpAllTablesStats(ctx); err != nil {
return err
}
if err := c.dumpIOIndexesStats(ctx); err != nil {
return err
}
if err := c.dumpIOTablesStats(ctx); err != nil {
return err
}
if err := c.dumpIOSequencesStats(ctx); err != nil {
return err
}
if err := c.dumpUserFunctionsStats(ctx); err != nil {
return err
}
// TODO: uncomment for Postgres 13+
// if err := c.dumpSLRUStats(ctx); err != nil {
// return err
// }
if err := c.dumpRelationsSize(ctx); err != nil {
return err
}
if err := c.dumpObjectsSize(ctx); err != nil {
return err
}
if err := c.collectCurrentState(ctx); err != nil {
return errors.Wrap(err, "failed to collect current state")
}
return nil
}
func (c *ObservingClone) collectCurrentState(ctx context.Context) error {
if err := c.getMaxQueryTime(ctx, &c.session.state.MaxDBQueryTimeMS); err != nil {
return err
}
if err := c.getDBSize(ctx, &c.session.state.CurrentDBSize); err != nil {
return err
}
if err := c.getObjectsSizeStats(ctx, &c.session.state.ObjectStat); err != nil {
return err
}
return c.countLogErrors(ctx, &c.session.state.LogErrors)
}
func (c *ObservingClone) discoverLogFields(ctx context.Context) error {
row := c.db.QueryRow(ctx, `select coalesce(string_agg(column_name, ','), '')
from information_schema.columns
where table_name = 'postgres_log'`)
fields := ""
if err := row.Scan(&fields); err != nil {
return err
}
if fields != "" {
c.csvFields = fields
c.fillMaskedIndexes()
}
return nil
}
// Stop stops an observation session.
func (c *ObservingClone) Stop() error {
log.Msg(fmt.Sprintf("Observation session %v is stopping...", c.session.SessionID))
c.cancel()
// Waiting for the observation process stops.
<-c.done
if c.session == nil {
return errors.New("failed to summarize session because it has not been initialized")
}
c.summarize()
if err := c.storeSummary(); err != nil {
log.Err(err)
}
c.AddArtifact(c.session.SessionID)
log.Msg(fmt.Sprintf("Observation session %v has been stopped.", c.session.SessionID))
return nil
}
// summarize calculates an observation result.
func (c *ObservingClone) summarize() {
c.session.Result.Status = statusFailed
c.session.FinishedAt = time.Now()
c.session.Result.Summary.Checklist.Duration = c.CheckDuration()
c.session.Result.Summary.Checklist.Locks = c.CheckLocks()
c.session.Result.Summary.Checklist.Success = c.CheckOverallSuccess()
if c.session.Result.Summary.Checklist.Duration &&
c.session.Result.Summary.Checklist.Locks &&
c.session.Result.Summary.Checklist.Success {
c.session.Result.Status = statusPassed
}
}
func (c *ObservingClone) currentArtifactsSessionPath() string {
return c.artifactsSessionPath(c.session.SessionID)
}
func (c *ObservingClone) artifactsSessionPath(sessionID uint64) string {
return path.Join(c.pool.ObserverDir(c.port), c.cloneID, strconv.FormatUint(sessionID, 10))
}
// CheckPerformanceRequirements checks monitoring data and returns an error if any of performance requires was not satisfied.
func (c *ObservingClone) CheckPerformanceRequirements() error {
if !c.CheckDuration() || !c.CheckLocks() {
return errors.New("performance requirements not satisfied")
}
return nil
}
// CheckDuration checks duration of the operation.
func (c *ObservingClone) CheckDuration() bool {
return uint64(c.session.Result.Summary.TotalDuration) < c.session.Config.MaxDuration
}
// CheckLocks checks long-lasting locks during the operation.
func (c *ObservingClone) CheckLocks() bool {
return c.session.Result.Summary.WarningIntervals == 0
}
// CheckOverallSuccess checks overall success of queries.
func (c *ObservingClone) CheckOverallSuccess() bool {
return !c.session.state.OverallError
}
// SetOverallError notes the presence of errors during the session.
func (c *ObservingClone) SetOverallError(overallErrors bool) {
c.session.state.OverallError = overallErrors
}