Renamed from arcdb: module path, cmd/archer, internal/archererr, docs, build scripts and ARCHER_* environment variables. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
145 lines
3.2 KiB
Go
145 lines
3.2 KiB
Go
// Package lag checks replica delay before each fetch.
|
|
package lag
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
// Checker waits until a replica's delay is below Max and replication is
|
|
// running.
|
|
type Checker struct {
|
|
db *sql.DB
|
|
Max time.Duration
|
|
Interval time.Duration
|
|
OnWait func(reason string)
|
|
}
|
|
|
|
// Open connects to the replica DSN.
|
|
func Open(dsn string, max, interval time.Duration) (*Checker, error) {
|
|
db, err := sql.Open("mysql", dsn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
db.SetMaxOpenConns(1)
|
|
if err := db.Ping(); err != nil {
|
|
db.Close()
|
|
return nil, fmt.Errorf("connect replica: %w", err)
|
|
}
|
|
return &Checker{db: db, Max: max, Interval: interval}, nil
|
|
}
|
|
|
|
func (c *Checker) Close() error { return c.db.Close() }
|
|
|
|
// Wait blocks until the replica lag is acceptable or ctx is cancelled.
|
|
func (c *Checker) Wait(ctx context.Context) error {
|
|
for {
|
|
reason, ok, err := c.status(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if ok {
|
|
return nil
|
|
}
|
|
if c.OnWait != nil {
|
|
c.OnWait(reason)
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-time.After(c.Interval):
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Checker) status(ctx context.Context) (string, bool, error) {
|
|
fields, err := c.showStatus(ctx, "SHOW REPLICA STATUS")
|
|
if err != nil || len(fields) == 0 {
|
|
fields, err = c.showStatus(ctx, "SHOW SLAVE STATUS")
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
}
|
|
if len(fields) == 0 {
|
|
return "replication is not configured", false, nil
|
|
}
|
|
sqlRunning := yes(fields["Replica_SQL_Running"], fields["Slave_SQL_Running"])
|
|
ioRunning := yes(fields["Replica_IO_Running"], fields["Slave_IO_Running"])
|
|
if !sqlRunning || !ioRunning {
|
|
return "replication threads are not running", false, nil
|
|
}
|
|
raw := firstNonEmpty(fields["Seconds_Behind_Source"], fields["Seconds_Behind_Master"])
|
|
if raw == "" || strings.EqualFold(raw, "NULL") {
|
|
return "replica lag is unknown", false, nil
|
|
}
|
|
secs, err := strconv.ParseFloat(raw, 64)
|
|
if err != nil {
|
|
return fmt.Sprintf("cannot parse replica lag %q", raw), false, nil
|
|
}
|
|
lag := time.Duration(secs * float64(time.Second))
|
|
if lag > c.Max {
|
|
return fmt.Sprintf("replica lag %s exceeds %s", lag, c.Max), false, nil
|
|
}
|
|
return "", true, nil
|
|
}
|
|
|
|
func (c *Checker) showStatus(ctx context.Context, q string) (map[string]string, error) {
|
|
rows, err := c.db.QueryContext(ctx, q)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
cols, err := rows.Columns()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !rows.Next() {
|
|
if err := rows.Err(); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return nil, err
|
|
}
|
|
return nil, nil
|
|
}
|
|
vals := make([]sql.RawBytes, len(cols))
|
|
ptrs := make([]any, len(cols))
|
|
for i := range vals {
|
|
ptrs[i] = &vals[i]
|
|
}
|
|
if err := rows.Scan(ptrs...); err != nil {
|
|
return nil, err
|
|
}
|
|
out := make(map[string]string, len(cols))
|
|
for i, name := range cols {
|
|
if vals[i] == nil {
|
|
out[name] = "NULL"
|
|
} else {
|
|
out[name] = string(vals[i])
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func yes(vals ...string) bool {
|
|
for _, v := range vals {
|
|
if strings.EqualFold(v, "Yes") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func firstNonEmpty(vals ...string) string {
|
|
for _, v := range vals {
|
|
if v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|