Private
Public Access
1
0

feat(db): migrate from MySQL to PostgreSQL
All checks were successful
continuous-integration/drone/push Build is passing

Replace MySQL driver with pgx/v5 and pgxpool:
- Update sqlc to use postgresql engine
- Convert query.sql to PostgreSQL syntax ($1 params, CASE WHEN,
  ANY() arrays)
- Replace sql.DB with pgxpool.Pool throughout
- Change nullable types from sql.Null* to pgtype.*
- Update ID types from uint32 to int64 for PostgreSQL compatibility
- Delete MySQL-specific dynamic_connect.go
- Add opentelemetry.gowrap template for tracing
This commit is contained in:
2025-11-29 10:59:15 -08:00
parent 85d86bc837
commit c9481d12c6
22 changed files with 3293 additions and 1309 deletions

View File

@@ -6,14 +6,15 @@ package ntpdb
import (
"context"
"database/sql"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
@@ -24,7 +25,7 @@ type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}

View File

@@ -1,85 +1,15 @@
package ntpdb
//go:generate go tool github.com/hexdigest/gowrap/cmd/gowrap gen -t ./opentelemetry.gowrap -g -i QuerierTx -p . -o otel.go
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"os"
"time"
"github.com/go-sql-driver/mysql"
"go.ntppool.org/common/logger"
"gopkg.in/yaml.v3"
"github.com/jackc/pgx/v5/pgxpool"
"go.ntppool.org/common/database/pgdb"
)
type Config struct {
MySQL DBConfig `yaml:"mysql"`
}
type DBConfig struct {
DSN string `default:"" flag:"dsn" usage:"Database DSN"`
User string `default:"" flag:"user"`
Pass string `default:"" flag:"pass"`
}
func OpenDB(ctx context.Context, configFile string) (*sql.DB, error) {
log := logger.FromContext(ctx)
dbconn := sql.OpenDB(Driver{CreateConnectorFunc: createConnector(ctx, configFile)})
dbconn.SetConnMaxLifetime(time.Minute * 3)
dbconn.SetMaxOpenConns(8)
dbconn.SetMaxIdleConns(3)
err := dbconn.Ping()
if err != nil {
log.DebugContext(ctx, "could not connect to database: %s", "err", err)
return nil, err
}
return dbconn, nil
}
func createConnector(ctx context.Context, configFile string) CreateConnectorFunc {
log := logger.FromContext(ctx)
return func() (driver.Connector, error) {
log.DebugContext(ctx, "opening db config file", "filename", configFile)
dbFile, err := os.Open(configFile)
if err != nil {
return nil, err
}
dec := yaml.NewDecoder(dbFile)
cfg := Config{}
err = dec.Decode(&cfg)
if err != nil {
return nil, err
}
// log.Printf("db cfg: %+v", cfg)
dsn := cfg.MySQL.DSN
if len(dsn) == 0 {
return nil, fmt.Errorf("--database.dsn flag or DATABASE_DSN environment variable required")
}
dbcfg, err := mysql.ParseDSN(dsn)
if err != nil {
return nil, err
}
if user := cfg.MySQL.User; len(user) > 0 {
dbcfg.User = user
}
if pass := cfg.MySQL.Pass; len(pass) > 0 {
dbcfg.Passwd = pass
}
return mysql.NewConnector(dbcfg)
}
// OpenDB opens a PostgreSQL connection pool using the specified config file
func OpenDB(ctx context.Context, configFile string) (*pgxpool.Pool, error) {
return pgdb.OpenPoolWithConfigFile(ctx, configFile)
}

View File

@@ -1,33 +0,0 @@
package ntpdb
import (
"context"
"database/sql/driver"
"errors"
"fmt"
)
// from https://github.com/Boostport/dynamic-database-config
type CreateConnectorFunc func() (driver.Connector, error)
type Driver struct {
CreateConnectorFunc CreateConnectorFunc
}
func (d Driver) Driver() driver.Driver {
return d
}
func (d Driver) Connect(ctx context.Context) (driver.Conn, error) {
connector, err := d.CreateConnectorFunc()
if err != nil {
return nil, fmt.Errorf("error creating connector from function: %w", err)
}
return connector.Connect(ctx)
}
func (d Driver) Open(name string) (driver.Conn, error) {
return nil, errors.New("open is not supported")
}

View File

@@ -5,11 +5,10 @@
package ntpdb
import (
"database/sql"
"database/sql/driver"
"fmt"
"time"
"github.com/jackc/pgx/v5/pgtype"
"go.ntppool.org/common/types"
)
@@ -271,73 +270,73 @@ func (ns NullZoneServerCountsIpVersion) Value() (driver.Value, error) {
}
type LogScore struct {
ID uint64 `db:"id" json:"id"`
MonitorID sql.NullInt32 `db:"monitor_id" json:"monitor_id"`
ServerID uint32 `db:"server_id" json:"server_id"`
Ts time.Time `db:"ts" json:"ts"`
ID int64 `db:"id" json:"id"`
MonitorID pgtype.Int8 `db:"monitor_id" json:"monitor_id"`
ServerID int64 `db:"server_id" json:"server_id"`
Ts pgtype.Timestamptz `db:"ts" json:"ts"`
Score float64 `db:"score" json:"score"`
Step float64 `db:"step" json:"step"`
Offset sql.NullFloat64 `db:"offset" json:"offset"`
Rtt sql.NullInt32 `db:"rtt" json:"rtt"`
Offset pgtype.Float8 `db:"offset" json:"offset"`
Rtt pgtype.Int4 `db:"rtt" json:"rtt"`
Attributes types.LogScoreAttributes `db:"attributes" json:"attributes"`
}
type Monitor struct {
ID uint32 `db:"id" json:"id"`
IDToken sql.NullString `db:"id_token" json:"id_token"`
ID int64 `db:"id" json:"id"`
IDToken pgtype.Text `db:"id_token" json:"id_token"`
Type MonitorsType `db:"type" json:"type"`
UserID sql.NullInt32 `db:"user_id" json:"user_id"`
AccountID sql.NullInt32 `db:"account_id" json:"account_id"`
UserID pgtype.Int8 `db:"user_id" json:"user_id"`
AccountID pgtype.Int8 `db:"account_id" json:"account_id"`
Hostname string `db:"hostname" json:"hostname"`
Location string `db:"location" json:"location"`
Ip sql.NullString `db:"ip" json:"ip"`
Ip pgtype.Text `db:"ip" json:"ip"`
IpVersion NullMonitorsIpVersion `db:"ip_version" json:"ip_version"`
TlsName sql.NullString `db:"tls_name" json:"tls_name"`
ApiKey sql.NullString `db:"api_key" json:"api_key"`
TlsName pgtype.Text `db:"tls_name" json:"tls_name"`
ApiKey pgtype.Text `db:"api_key" json:"api_key"`
Status MonitorsStatus `db:"status" json:"status"`
Config string `db:"config" json:"config"`
ClientVersion string `db:"client_version" json:"client_version"`
LastSeen sql.NullTime `db:"last_seen" json:"last_seen"`
LastSubmit sql.NullTime `db:"last_submit" json:"last_submit"`
CreatedOn time.Time `db:"created_on" json:"created_on"`
DeletedOn sql.NullTime `db:"deleted_on" json:"deleted_on"`
IsCurrent sql.NullBool `db:"is_current" json:"is_current"`
LastSeen pgtype.Timestamptz `db:"last_seen" json:"last_seen"`
LastSubmit pgtype.Timestamptz `db:"last_submit" json:"last_submit"`
CreatedOn pgtype.Timestamptz `db:"created_on" json:"created_on"`
DeletedOn pgtype.Timestamptz `db:"deleted_on" json:"deleted_on"`
IsCurrent pgtype.Bool `db:"is_current" json:"is_current"`
}
type Server struct {
ID uint32 `db:"id" json:"id"`
Ip string `db:"ip" json:"ip"`
IpVersion ServersIpVersion `db:"ip_version" json:"ip_version"`
UserID sql.NullInt32 `db:"user_id" json:"user_id"`
AccountID sql.NullInt32 `db:"account_id" json:"account_id"`
Hostname sql.NullString `db:"hostname" json:"hostname"`
Stratum sql.NullInt16 `db:"stratum" json:"stratum"`
InPool uint8 `db:"in_pool" json:"in_pool"`
InServerList uint8 `db:"in_server_list" json:"in_server_list"`
Netspeed uint32 `db:"netspeed" json:"netspeed"`
NetspeedTarget uint32 `db:"netspeed_target" json:"netspeed_target"`
CreatedOn time.Time `db:"created_on" json:"created_on"`
UpdatedOn time.Time `db:"updated_on" json:"updated_on"`
ScoreTs sql.NullTime `db:"score_ts" json:"score_ts"`
ScoreRaw float64 `db:"score_raw" json:"score_raw"`
DeletionOn sql.NullTime `db:"deletion_on" json:"deletion_on"`
Flags string `db:"flags" json:"flags"`
ID int64 `db:"id" json:"id"`
Ip string `db:"ip" json:"ip"`
IpVersion ServersIpVersion `db:"ip_version" json:"ip_version"`
UserID pgtype.Int8 `db:"user_id" json:"user_id"`
AccountID pgtype.Int8 `db:"account_id" json:"account_id"`
Hostname pgtype.Text `db:"hostname" json:"hostname"`
Stratum pgtype.Int2 `db:"stratum" json:"stratum"`
InPool int16 `db:"in_pool" json:"in_pool"`
InServerList int16 `db:"in_server_list" json:"in_server_list"`
Netspeed int64 `db:"netspeed" json:"netspeed"`
NetspeedTarget int64 `db:"netspeed_target" json:"netspeed_target"`
CreatedOn pgtype.Timestamptz `db:"created_on" json:"created_on"`
UpdatedOn pgtype.Timestamptz `db:"updated_on" json:"updated_on"`
ScoreTs pgtype.Timestamptz `db:"score_ts" json:"score_ts"`
ScoreRaw float64 `db:"score_raw" json:"score_raw"`
DeletionOn pgtype.Date `db:"deletion_on" json:"deletion_on"`
Flags string `db:"flags" json:"flags"`
}
type Zone struct {
ID uint32 `db:"id" json:"id"`
Name string `db:"name" json:"name"`
Description sql.NullString `db:"description" json:"description"`
ParentID sql.NullInt32 `db:"parent_id" json:"parent_id"`
Dns bool `db:"dns" json:"dns"`
ID int64 `db:"id" json:"id"`
Name string `db:"name" json:"name"`
Description pgtype.Text `db:"description" json:"description"`
ParentID pgtype.Int8 `db:"parent_id" json:"parent_id"`
Dns bool `db:"dns" json:"dns"`
}
type ZoneServerCount struct {
ID uint32 `db:"id" json:"id"`
ZoneID uint32 `db:"zone_id" json:"zone_id"`
ID int64 `db:"id" json:"id"`
ZoneID int64 `db:"zone_id" json:"zone_id"`
IpVersion ZoneServerCountsIpVersion `db:"ip_version" json:"ip_version"`
Date time.Time `db:"date" json:"date"`
CountActive uint32 `db:"count_active" json:"count_active"`
CountRegistered uint32 `db:"count_registered" json:"count_registered"`
Date pgtype.Date `db:"date" json:"date"`
CountActive int32 `db:"count_active" json:"count_active"`
CountRegistered int32 `db:"count_registered" json:"count_registered"`
NetspeedActive int `db:"netspeed_active" json:"netspeed_active"`
}

View File

@@ -0,0 +1,55 @@
import (
"context"
_codes "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)
{{ $decorator := (or .Vars.DecoratorName (printf "%sWithTracing" .Interface.Name)) }}
{{ $spanNameType := (or .Vars.SpanNamePrefix .Interface.Name) }}
// {{$decorator}} implements {{.Interface.Name}} interface instrumented with open telemetry spans
type {{$decorator}} struct {
{{.Interface.Type}}
_instance string
_spanDecorator func(span trace.Span, params, results map[string]interface{})
}
// New{{$decorator}} returns {{$decorator}}
func New{{$decorator}} (base {{.Interface.Type}}, instance string, spanDecorator ...func(span trace.Span, params, results map[string]interface{})) {{$decorator}} {
d := {{$decorator}} {
{{.Interface.Name}}: base,
_instance: instance,
}
if len(spanDecorator) > 0 && spanDecorator[0] != nil {
d._spanDecorator = spanDecorator[0]
}
return d
}
{{range $method := .Interface.Methods}}
{{if $method.AcceptsContext}}
// {{$method.Name}} implements {{$.Interface.Name}}
func (_d {{$decorator}}) {{$method.Declaration}} {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "{{$spanNameType}}.{{$method.Name}}")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, {{$method.ParamsMap}}, {{$method.ResultsMap}})
}{{- if $method.ReturnsError}} else if err != nil {
_span.RecordError(err)
_span.SetStatus(_codes.Error, err.Error())
_span.SetAttributes(
attribute.String("event", "error"),
attribute.String("message", err.Error()),
)
}
{{end}}
_span.End()
}()
{{$method.Pass (printf "_d.%s." $.Interface.Name) }}
}
{{end}}
{{end}}

View File

@@ -1,5 +1,5 @@
// Code generated by gowrap. DO NOT EDIT.
// template: https://raw.githubusercontent.com/hexdigest/gowrap/6bd1bc023b4d2a619f30020924f258b8ff665a7a/templates/opentelemetry
// template: opentelemetry.gowrap
// gowrap: http://github.com/hexdigest/gowrap
package ntpdb
@@ -7,10 +7,11 @@ package ntpdb
import (
"context"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
_codes "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
// QuerierTxWithTracing implements QuerierTx interface instrumented with open telemetry spans
@@ -104,7 +105,7 @@ func (_d QuerierTxWithTracing) GetMonitorByNameAndIPVersion(ctx context.Context,
}
// GetMonitorsByID implements QuerierTx
func (_d QuerierTxWithTracing) GetMonitorsByID(ctx context.Context, monitorids []uint32) (ma1 []Monitor, err error) {
func (_d QuerierTxWithTracing) GetMonitorsByID(ctx context.Context, monitorids []int64) (ma1 []Monitor, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetMonitorsByID")
defer func() {
if _d._spanDecorator != nil {
@@ -128,7 +129,7 @@ func (_d QuerierTxWithTracing) GetMonitorsByID(ctx context.Context, monitorids [
}
// GetServerByID implements QuerierTx
func (_d QuerierTxWithTracing) GetServerByID(ctx context.Context, id uint32) (s1 Server, err error) {
func (_d QuerierTxWithTracing) GetServerByID(ctx context.Context, id int64) (s1 Server, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerByID")
defer func() {
if _d._spanDecorator != nil {
@@ -224,14 +225,14 @@ func (_d QuerierTxWithTracing) GetServerLogScoresByMonitorID(ctx context.Context
}
// GetServerNetspeed implements QuerierTx
func (_d QuerierTxWithTracing) GetServerNetspeed(ctx context.Context, ip string) (u1 uint32, err error) {
func (_d QuerierTxWithTracing) GetServerNetspeed(ctx context.Context, ip string) (i1 int64, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetServerNetspeed")
defer func() {
if _d._spanDecorator != nil {
_d._spanDecorator(_span, map[string]interface{}{
"ctx": ctx,
"ip": ip}, map[string]interface{}{
"u1": u1,
"i1": i1,
"err": err})
} else if err != nil {
_span.RecordError(err)
@@ -296,7 +297,7 @@ func (_d QuerierTxWithTracing) GetZoneByName(ctx context.Context, name string) (
}
// GetZoneCounts implements QuerierTx
func (_d QuerierTxWithTracing) GetZoneCounts(ctx context.Context, zoneID uint32) (za1 []ZoneServerCount, err error) {
func (_d QuerierTxWithTracing) GetZoneCounts(ctx context.Context, zoneID int64) (za1 []ZoneServerCount, err error) {
ctx, _span := otel.Tracer(_d._instance).Start(ctx, "QuerierTx.GetZoneCounts")
defer func() {
if _d._spanDecorator != nil {

View File

@@ -10,15 +10,15 @@ import (
type Querier interface {
GetMonitorByNameAndIPVersion(ctx context.Context, arg GetMonitorByNameAndIPVersionParams) (Monitor, error)
GetMonitorsByID(ctx context.Context, monitorids []uint32) ([]Monitor, error)
GetServerByID(ctx context.Context, id uint32) (Server, error)
GetMonitorsByID(ctx context.Context, monitorids []int64) ([]Monitor, error)
GetServerByID(ctx context.Context, id int64) (Server, error)
GetServerByIP(ctx context.Context, ip string) (Server, error)
GetServerLogScores(ctx context.Context, arg GetServerLogScoresParams) ([]LogScore, error)
GetServerLogScoresByMonitorID(ctx context.Context, arg GetServerLogScoresByMonitorIDParams) ([]LogScore, error)
GetServerNetspeed(ctx context.Context, ip string) (uint32, error)
GetServerNetspeed(ctx context.Context, ip string) (int64, error)
GetServerScores(ctx context.Context, arg GetServerScoresParams) ([]GetServerScoresRow, error)
GetZoneByName(ctx context.Context, name string) (Zone, error)
GetZoneCounts(ctx context.Context, zoneID uint32) ([]ZoneServerCount, error)
GetZoneCounts(ctx context.Context, zoneID int64) ([]ZoneServerCount, error)
GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow, error)
GetZoneStatsV2(ctx context.Context, ip string) ([]GetZoneStatsV2Row, error)
}

View File

@@ -7,28 +7,27 @@ package ntpdb
import (
"context"
"database/sql"
"strings"
"time"
"github.com/jackc/pgx/v5/pgtype"
)
const getMonitorByNameAndIPVersion = `-- name: GetMonitorByNameAndIPVersion :one
select id, id_token, type, user_id, account_id, hostname, location, ip, ip_version, tls_name, api_key, status, config, client_version, last_seen, last_submit, created_on, deleted_on, is_current from monitors
where
tls_name like ? AND
(ip_version = ? OR (type = 'score' AND ip_version IS NULL)) AND
is_current = 1
tls_name like $1 AND
(ip_version = $2 OR (type = 'score' AND ip_version IS NULL)) AND
is_current = true
order by id
limit 1
`
type GetMonitorByNameAndIPVersionParams struct {
TlsName sql.NullString `db:"tls_name" json:"tls_name"`
TlsName pgtype.Text `db:"tls_name" json:"tls_name"`
IpVersion NullMonitorsIpVersion `db:"ip_version" json:"ip_version"`
}
func (q *Queries) GetMonitorByNameAndIPVersion(ctx context.Context, arg GetMonitorByNameAndIPVersionParams) (Monitor, error) {
row := q.db.QueryRowContext(ctx, getMonitorByNameAndIPVersion, arg.TlsName, arg.IpVersion)
row := q.db.QueryRow(ctx, getMonitorByNameAndIPVersion, arg.TlsName, arg.IpVersion)
var i Monitor
err := row.Scan(
&i.ID,
@@ -56,21 +55,11 @@ func (q *Queries) GetMonitorByNameAndIPVersion(ctx context.Context, arg GetMonit
const getMonitorsByID = `-- name: GetMonitorsByID :many
select id, id_token, type, user_id, account_id, hostname, location, ip, ip_version, tls_name, api_key, status, config, client_version, last_seen, last_submit, created_on, deleted_on, is_current from monitors
where id in (/*SLICE:MonitorIDs*/?)
where id = ANY($1::bigint[])
`
func (q *Queries) GetMonitorsByID(ctx context.Context, monitorids []uint32) ([]Monitor, error) {
query := getMonitorsByID
var queryParams []interface{}
if len(monitorids) > 0 {
for _, v := range monitorids {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:MonitorIDs*/?", strings.Repeat(",?", len(monitorids))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:MonitorIDs*/?", "NULL", 1)
}
rows, err := q.db.QueryContext(ctx, query, queryParams...)
func (q *Queries) GetMonitorsByID(ctx context.Context, monitorids []int64) ([]Monitor, error) {
rows, err := q.db.Query(ctx, getMonitorsByID, monitorids)
if err != nil {
return nil, err
}
@@ -103,9 +92,6 @@ func (q *Queries) GetMonitorsByID(ctx context.Context, monitorids []uint32) ([]M
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
@@ -115,11 +101,11 @@ func (q *Queries) GetMonitorsByID(ctx context.Context, monitorids []uint32) ([]M
const getServerByID = `-- name: GetServerByID :one
select id, ip, ip_version, user_id, account_id, hostname, stratum, in_pool, in_server_list, netspeed, netspeed_target, created_on, updated_on, score_ts, score_raw, deletion_on, flags from servers
where
id = ?
id = $1
`
func (q *Queries) GetServerByID(ctx context.Context, id uint32) (Server, error) {
row := q.db.QueryRowContext(ctx, getServerByID, id)
func (q *Queries) GetServerByID(ctx context.Context, id int64) (Server, error) {
row := q.db.QueryRow(ctx, getServerByID, id)
var i Server
err := row.Scan(
&i.ID,
@@ -146,11 +132,11 @@ func (q *Queries) GetServerByID(ctx context.Context, id uint32) (Server, error)
const getServerByIP = `-- name: GetServerByIP :one
select id, ip, ip_version, user_id, account_id, hostname, stratum, in_pool, in_server_list, netspeed, netspeed_target, created_on, updated_on, score_ts, score_raw, deletion_on, flags from servers
where
ip = ?
ip = $1
`
func (q *Queries) GetServerByIP(ctx context.Context, ip string) (Server, error) {
row := q.db.QueryRowContext(ctx, getServerByIP, ip)
row := q.db.QueryRow(ctx, getServerByIP, ip)
var i Server
err := row.Scan(
&i.ID,
@@ -175,20 +161,20 @@ func (q *Queries) GetServerByIP(ctx context.Context, ip string) (Server, error)
}
const getServerLogScores = `-- name: GetServerLogScores :many
select id, monitor_id, server_id, ts, score, step, offset, rtt, attributes from log_scores
select id, monitor_id, server_id, ts, score, step, "offset", rtt, attributes from log_scores
where
server_id = ?
server_id = $1
order by ts desc
limit ?
limit $2
`
type GetServerLogScoresParams struct {
ServerID uint32 `db:"server_id" json:"server_id"`
Limit int32 `db:"limit" json:"limit"`
ServerID int64 `db:"server_id" json:"server_id"`
Limit int32 `db:"limit" json:"limit"`
}
func (q *Queries) GetServerLogScores(ctx context.Context, arg GetServerLogScoresParams) ([]LogScore, error) {
rows, err := q.db.QueryContext(ctx, getServerLogScores, arg.ServerID, arg.Limit)
rows, err := q.db.Query(ctx, getServerLogScores, arg.ServerID, arg.Limit)
if err != nil {
return nil, err
}
@@ -211,9 +197,6 @@ func (q *Queries) GetServerLogScores(ctx context.Context, arg GetServerLogScores
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
@@ -221,22 +204,22 @@ func (q *Queries) GetServerLogScores(ctx context.Context, arg GetServerLogScores
}
const getServerLogScoresByMonitorID = `-- name: GetServerLogScoresByMonitorID :many
select id, monitor_id, server_id, ts, score, step, offset, rtt, attributes from log_scores
select id, monitor_id, server_id, ts, score, step, "offset", rtt, attributes from log_scores
where
server_id = ? AND
monitor_id = ?
server_id = $1 AND
monitor_id = $2
order by ts desc
limit ?
limit $3
`
type GetServerLogScoresByMonitorIDParams struct {
ServerID uint32 `db:"server_id" json:"server_id"`
MonitorID sql.NullInt32 `db:"monitor_id" json:"monitor_id"`
Limit int32 `db:"limit" json:"limit"`
ServerID int64 `db:"server_id" json:"server_id"`
MonitorID pgtype.Int8 `db:"monitor_id" json:"monitor_id"`
Limit int32 `db:"limit" json:"limit"`
}
func (q *Queries) GetServerLogScoresByMonitorID(ctx context.Context, arg GetServerLogScoresByMonitorIDParams) ([]LogScore, error) {
rows, err := q.db.QueryContext(ctx, getServerLogScoresByMonitorID, arg.ServerID, arg.MonitorID, arg.Limit)
rows, err := q.db.Query(ctx, getServerLogScoresByMonitorID, arg.ServerID, arg.MonitorID, arg.Limit)
if err != nil {
return nil, err
}
@@ -259,9 +242,6 @@ func (q *Queries) GetServerLogScoresByMonitorID(ctx context.Context, arg GetServ
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
@@ -269,12 +249,12 @@ func (q *Queries) GetServerLogScoresByMonitorID(ctx context.Context, arg GetServ
}
const getServerNetspeed = `-- name: GetServerNetspeed :one
select netspeed from servers where ip = ?
select netspeed from servers where ip = $1
`
func (q *Queries) GetServerNetspeed(ctx context.Context, ip string) (uint32, error) {
row := q.db.QueryRowContext(ctx, getServerNetspeed, ip)
var netspeed uint32
func (q *Queries) GetServerNetspeed(ctx context.Context, ip string) (int64, error) {
row := q.db.QueryRow(ctx, getServerNetspeed, ip)
var netspeed int64
err := row.Scan(&netspeed)
return netspeed, err
}
@@ -287,39 +267,28 @@ select
inner join monitors m
on (m.id=ss.monitor_id)
where
server_id = ? AND
monitor_id in (/*SLICE:MonitorIDs*/?)
server_id = $1 AND
monitor_id = ANY($2::bigint[])
`
type GetServerScoresParams struct {
ServerID uint32 `db:"server_id" json:"server_id"`
MonitorIDs []uint32 `db:"MonitorIDs" json:"MonitorIDs"`
ServerID int64 `db:"server_id" json:"server_id"`
MonitorIDs []int64 `db:"MonitorIDs" json:"MonitorIDs"`
}
type GetServerScoresRow struct {
ID uint32 `db:"id" json:"id"`
ID int64 `db:"id" json:"id"`
Hostname string `db:"hostname" json:"hostname"`
TlsName sql.NullString `db:"tls_name" json:"tls_name"`
TlsName pgtype.Text `db:"tls_name" json:"tls_name"`
Location string `db:"location" json:"location"`
Type MonitorsType `db:"type" json:"type"`
ScoreRaw float64 `db:"score_raw" json:"score_raw"`
ScoreTs sql.NullTime `db:"score_ts" json:"score_ts"`
ScoreTs pgtype.Timestamptz `db:"score_ts" json:"score_ts"`
Status ServerScoresStatus `db:"status" json:"status"`
}
func (q *Queries) GetServerScores(ctx context.Context, arg GetServerScoresParams) ([]GetServerScoresRow, error) {
query := getServerScores
var queryParams []interface{}
queryParams = append(queryParams, arg.ServerID)
if len(arg.MonitorIDs) > 0 {
for _, v := range arg.MonitorIDs {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:MonitorIDs*/?", strings.Repeat(",?", len(arg.MonitorIDs))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:MonitorIDs*/?", "NULL", 1)
}
rows, err := q.db.QueryContext(ctx, query, queryParams...)
rows, err := q.db.Query(ctx, getServerScores, arg.ServerID, arg.MonitorIDs)
if err != nil {
return nil, err
}
@@ -341,9 +310,6 @@ func (q *Queries) GetServerScores(ctx context.Context, arg GetServerScoresParams
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
@@ -353,11 +319,11 @@ func (q *Queries) GetServerScores(ctx context.Context, arg GetServerScoresParams
const getZoneByName = `-- name: GetZoneByName :one
select id, name, description, parent_id, dns from zones
where
name = ?
name = $1
`
func (q *Queries) GetZoneByName(ctx context.Context, name string) (Zone, error) {
row := q.db.QueryRowContext(ctx, getZoneByName, name)
row := q.db.QueryRow(ctx, getZoneByName, name)
var i Zone
err := row.Scan(
&i.ID,
@@ -371,12 +337,12 @@ func (q *Queries) GetZoneByName(ctx context.Context, name string) (Zone, error)
const getZoneCounts = `-- name: GetZoneCounts :many
select id, zone_id, ip_version, date, count_active, count_registered, netspeed_active from zone_server_counts
where zone_id = ?
where zone_id = $1
order by date
`
func (q *Queries) GetZoneCounts(ctx context.Context, zoneID uint32) ([]ZoneServerCount, error) {
rows, err := q.db.QueryContext(ctx, getZoneCounts, zoneID)
func (q *Queries) GetZoneCounts(ctx context.Context, zoneID int64) ([]ZoneServerCount, error) {
rows, err := q.db.Query(ctx, getZoneCounts, zoneID)
if err != nil {
return nil, err
}
@@ -397,9 +363,6 @@ func (q *Queries) GetZoneCounts(ctx context.Context, zoneID uint32) ([]ZoneServe
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
@@ -408,7 +371,7 @@ func (q *Queries) GetZoneCounts(ctx context.Context, zoneID uint32) ([]ZoneServe
const getZoneStatsData = `-- name: GetZoneStatsData :many
SELECT zc.date, z.name, zc.ip_version, count_active, count_registered, netspeed_active
FROM zone_server_counts zc USE INDEX (date_idx)
FROM zone_server_counts zc
INNER JOIN zones z
ON(zc.zone_id=z.id)
WHERE date IN (SELECT max(date) from zone_server_counts)
@@ -416,16 +379,16 @@ ORDER BY name
`
type GetZoneStatsDataRow struct {
Date time.Time `db:"date" json:"date"`
Date pgtype.Date `db:"date" json:"date"`
Name string `db:"name" json:"name"`
IpVersion ZoneServerCountsIpVersion `db:"ip_version" json:"ip_version"`
CountActive uint32 `db:"count_active" json:"count_active"`
CountRegistered uint32 `db:"count_registered" json:"count_registered"`
CountActive int32 `db:"count_active" json:"count_active"`
CountRegistered int32 `db:"count_registered" json:"count_registered"`
NetspeedActive int `db:"netspeed_active" json:"netspeed_active"`
}
func (q *Queries) GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow, error) {
rows, err := q.db.QueryContext(ctx, getZoneStatsData)
rows, err := q.db.Query(ctx, getZoneStatsData)
if err != nil {
return nil, err
}
@@ -445,9 +408,6 @@ func (q *Queries) GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow,
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
@@ -455,15 +415,14 @@ func (q *Queries) GetZoneStatsData(ctx context.Context) ([]GetZoneStatsDataRow,
}
const getZoneStatsV2 = `-- name: GetZoneStatsV2 :many
select zone_name, netspeed_active+0 as netspeed_active FROM (
SELECT
z.name as zone_name,
SUM(
IF (deletion_on IS NULL AND score_raw > 10,
netspeed,
0
)
) AS netspeed_active
CAST(SUM(
CASE WHEN deletion_on IS NULL AND score_raw > 10
THEN netspeed
ELSE 0
END
) AS int) AS netspeed_active
FROM
servers s
INNER JOIN server_zones sz ON (sz.server_id = s.id)
@@ -472,23 +431,22 @@ FROM
select zone_id, s.ip_version
from server_zones sz
inner join servers s on (s.id=sz.server_id)
where s.ip=?
where s.ip=$1
) as srvz on (srvz.zone_id=z.id AND srvz.ip_version=s.ip_version)
WHERE
(deletion_on IS NULL OR deletion_on > NOW())
AND in_pool = 1
AND netspeed > 0
GROUP BY z.name)
AS server_netspeed
GROUP BY z.name
`
type GetZoneStatsV2Row struct {
ZoneName string `db:"zone_name" json:"zone_name"`
NetspeedActive int `db:"netspeed_active" json:"netspeed_active"`
NetspeedActive int32 `db:"netspeed_active" json:"netspeed_active"`
}
func (q *Queries) GetZoneStatsV2(ctx context.Context, ip string) ([]GetZoneStatsV2Row, error) {
rows, err := q.db.QueryContext(ctx, getZoneStatsV2, ip)
rows, err := q.db.Query(ctx, getZoneStatsV2, ip)
if err != nil {
return nil, err
}
@@ -501,9 +459,6 @@ func (q *Queries) GetZoneStatsV2(ctx context.Context, ip string) ([]GetZoneStats
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}

View File

@@ -2,7 +2,11 @@ package ntpdb
import (
"context"
"database/sql"
"errors"
"github.com/jackc/pgx/v5"
"go.ntppool.org/common/logger"
"go.opentelemetry.io/otel/trace"
)
type QuerierTx interface {
@@ -11,14 +15,17 @@ type QuerierTx interface {
Begin(ctx context.Context) (QuerierTx, error)
Commit(ctx context.Context) error
Rollback(ctx context.Context) error
// Conn returns the connection used by this transaction
Conn() *pgx.Conn
}
type Beginner interface {
Begin(context.Context) (sql.Tx, error)
Begin(context.Context) (pgx.Tx, error)
}
type Tx interface {
Begin(context.Context) (sql.Tx, error)
Begin(context.Context) (pgx.Tx, error)
Commit(ctx context.Context) error
Rollback(ctx context.Context) error
}
@@ -28,21 +35,33 @@ func (q *Queries) Begin(ctx context.Context) (QuerierTx, error) {
if err != nil {
return nil, err
}
return &Queries{db: &tx}, nil
return &Queries{db: tx}, nil
}
func (q *Queries) Commit(ctx context.Context) error {
tx, ok := q.db.(Tx)
if !ok {
return sql.ErrTxDone
// Commit called on Queries with dbpool, so treat as transaction already committed
return pgx.ErrTxClosed
}
return tx.Commit(ctx)
}
func (q *Queries) Conn() *pgx.Conn {
// pgx.Tx is an interface that has Conn() method
tx, ok := q.db.(pgx.Tx)
if !ok {
logger.Setup().Error("could not get connection from QuerierTx")
return nil
}
return tx.Conn()
}
func (q *Queries) Rollback(ctx context.Context) error {
tx, ok := q.db.(Tx)
if !ok {
return sql.ErrTxDone
// Rollback called on Queries with dbpool, so treat as transaction already committed
return pgx.ErrTxClosed
}
return tx.Rollback(ctx)
}
@@ -62,3 +81,41 @@ func (wq *WrappedQuerier) Begin(ctx context.Context) (QuerierTx, error) {
}
return NewWrappedQuerier(q), nil
}
func (wq *WrappedQuerier) Conn() *pgx.Conn {
return wq.QuerierTxWithTracing.Conn()
}
// LogRollback logs and performs a rollback if the transaction is still active
func LogRollback(ctx context.Context, tx QuerierTx) {
if !isInTransaction(tx) {
return
}
log := logger.FromContext(ctx)
log.WarnContext(ctx, "transaction rollback called on an active transaction")
// if caller ctx is done we still need rollback to happen
// so Rollback gets a fresh context with span copied over
rbCtx := context.Background()
if span := trace.SpanFromContext(ctx); span != nil {
rbCtx = trace.ContextWithSpan(rbCtx, span)
}
if err := tx.Rollback(rbCtx); err != nil && !errors.Is(err, pgx.ErrTxClosed) {
log.ErrorContext(ctx, "rollback failed", "err", err)
}
}
func isInTransaction(tx QuerierTx) bool {
if tx == nil {
return false
}
conn := tx.Conn()
if conn == nil {
return false
}
// 'I' means idle, so if it's not idle, we're in a transaction
return conn.PgConn().TxStatus() != 'I'
}