Skip to content

Commit

Permalink
vtorc: make SQL formatting consistent
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Nov 5, 2024
1 parent 9ed8ee2 commit a90de33
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 286 deletions.
18 changes: 8 additions & 10 deletions go/vt/vtorc/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ func OpenVTOrc() (db *sql.DB, err error) {

// registerVTOrcDeployment updates the vtorc_db_deployments table upon successful deployment
func registerVTOrcDeployment(db *sql.DB) error {
query := `
replace into vtorc_db_deployments (
deployed_version, deployed_timestamp
) values (
?, datetime('now')
)
`
query := `REPLACE INTO vtorc_db_deployments (
deployed_version,
deployed_timestamp
) VALUES (
?,
DATETIME('now')
)`
if _, err := execInternal(db, query, ""); err != nil {
log.Fatalf("Unable to write to vtorc_db_deployments: %+v", err)
}
Expand Down Expand Up @@ -116,9 +116,7 @@ func initVTOrcDB(db *sql.DB) error {

// execInternal
func execInternal(db *sql.DB, query string, args ...any) (sql.Result, error) {
var err error
res, err := sqlutils.ExecNoPrepare(db, query, args...)
return res, err
return sqlutils.ExecNoPrepare(db, query, args...)
}

// ExecVTOrc will execute given query on the vtorc backend database.
Expand Down
62 changes: 33 additions & 29 deletions go/vt/vtorc/inst/analysis_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func GetReplicationAnalysis(keyspace string, shard string, hints *ReplicationAna

// TODO(sougou); deprecate ReduceReplicationAnalysisCount
args := sqlutils.Args(config.Config.ReasonableReplicationLagSeconds, ValidSecondsFromSeenToLastAttemptedCheck(), config.Config.ReasonableReplicationLagSeconds, keyspace, shard)
query := `
SELECT
query := `SELECT
vitess_tablet.info AS tablet_info,
vitess_tablet.tablet_type,
vitess_tablet.primary_timestamp,
Expand All @@ -91,13 +90,13 @@ func GetReplicationAnalysis(keyspace string, shard string, hints *ReplicationAna
IFNULL(
primary_instance.binary_log_file = database_instance_stale_binlog_coordinates.binary_log_file
AND primary_instance.binary_log_pos = database_instance_stale_binlog_coordinates.binary_log_pos
AND database_instance_stale_binlog_coordinates.first_seen < datetime('now', printf('-%d second', ?)),
AND database_instance_stale_binlog_coordinates.first_seen < DATETIME('now', PRINTF('-%d SECOND', ?)),
0
)
) AS is_stale_binlog_coordinates,
MIN(
primary_instance.last_checked <= primary_instance.last_seen
and primary_instance.last_attempted_check <= datetime(primary_instance.last_seen, printf('+%d second', ?))
and primary_instance.last_attempted_check <= DATETIME(primary_instance.last_seen, PRINTF('+%d SECOND', ?))
) = 1 AS is_last_check_valid,
/* To be considered a primary, traditional async replication must not be present/valid AND the host should either */
/* not be a replication group member OR be the primary of the replication group */
Expand Down Expand Up @@ -655,13 +654,13 @@ func auditInstanceAnalysisInChangelog(tabletAlias string, analysisCode AnalysisC
// Find if the lastAnalysisHasChanged or not while updating the row if it has.
lastAnalysisChanged := false
{
sqlResult, err := db.ExecVTOrc(`
update database_instance_last_analysis set
sqlResult, err := db.ExecVTOrc(`UPDATE update database_instance_last_analysis
SET
analysis = ?,
analysis_timestamp = datetime('now')
where
analysis_timestamp = DATETIME('now')
WHERE
alias = ?
and analysis != ?
AND analysis != ?
`,
string(analysisCode), tabletAlias, string(analysisCode),
)
Expand All @@ -682,13 +681,16 @@ func auditInstanceAnalysisInChangelog(tabletAlias string, analysisCode AnalysisC
firstInsertion := false
if !lastAnalysisChanged {
// The insert only returns more than 1 row changed if this is the first insertion.
sqlResult, err := db.ExecVTOrc(`
insert or ignore into database_instance_last_analysis (
alias, analysis_timestamp, analysis
) values (
?, datetime('now'), ?
)
`,
sqlResult, err := db.ExecVTOrc(`INSERT OR IGNORE
INTO database_instance_last_analysis (
alias,
analysis_timestamp,
analysis
) VALUES (
?,
DATETIME('now'),
?
)`,
tabletAlias, string(analysisCode),
)
if err != nil {
Expand All @@ -708,13 +710,16 @@ func auditInstanceAnalysisInChangelog(tabletAlias string, analysisCode AnalysisC
return nil
}

_, err := db.ExecVTOrc(`
insert into database_instance_analysis_changelog (
alias, analysis_timestamp, analysis
) values (
?, datetime('now'), ?
)
`,
_, err := db.ExecVTOrc(`INSERT
INTO database_instance_analysis_changelog (
alias,
analysis_timestamp,
analysis
) VALUES (
?,
DATETIME('now'),
?
)`,
tabletAlias, string(analysisCode),
)
if err == nil {
Expand All @@ -727,12 +732,11 @@ func auditInstanceAnalysisInChangelog(tabletAlias string, analysisCode AnalysisC

// ExpireInstanceAnalysisChangelog removes old-enough analysis entries from the changelog
func ExpireInstanceAnalysisChangelog() error {
_, err := db.ExecVTOrc(`
delete
from database_instance_analysis_changelog
where
analysis_timestamp < datetime('now', printf('-%d hour', ?))
`,
_, err := db.ExecVTOrc(`DELETE
FROM database_instance_analysis_changelog
WHERE
analysis_timestamp < DATETIME('now', PRINTF('-%d HOUR', ?))
`,
config.UnseenInstanceForgetHours,
)
if err != nil {
Expand Down
24 changes: 16 additions & 8 deletions go/vt/vtorc/inst/audit_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,22 @@ func AuditOperation(auditType string, tabletAlias string, message string) error
}()
}
if config.Config.AuditToBackendDB {
_, err := db.ExecVTOrc(`
insert
into audit (
audit_timestamp, audit_type, alias, keyspace, shard, message
) VALUES (
datetime('now'), ?, ?, ?, ?, ?
)
`,
_, err := db.ExecVTOrc(`INSERT
INTO audit (
audit_timestamp,
audit_type,
alias,
keyspace,
shard,
message
) VALUES (
DATETIME('now'),
?,
?,
?,
?,
?
)`,
auditType,
tabletAlias,
keyspace,
Expand Down
Loading

0 comments on commit a90de33

Please sign in to comment.