Skip to content

Commit

Permalink
vtorc: Cleanup more unused code (#13354)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbussink committed Jun 30, 2023
1 parent 4826310 commit 5356ef5
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 547 deletions.
57 changes: 1 addition & 56 deletions go/vt/external/golib/sqlutils/sqlutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,10 @@ func (this *RowData) MarshalJSON() ([]byte, error) {
return json.Marshal(cells)
}

func (this *RowData) Args() []any {
result := make([]any, len(*this))
for i := range *this {
result[i] = (*(*this)[i].NullString())
}
return result
}

// ResultData is an ordered row set of RowData
type ResultData []RowData
type NamedResultData struct {
Columns []string
Data ResultData
}

var EmptyResultData = ResultData{}

func (this *RowMap) GetString(key string) string {
return (*this)[key].String
}

// GetStringD returns a string from the map, or a default value if the key does not exist
func (this *RowMap) GetStringD(key string, def string) string {
if cell, ok := (*this)[key]; ok {
return cell.String
}
return def
}

func (this *RowMap) GetInt64(key string) int64 {
res, _ := strconv.ParseInt(this.GetString(key), 10, 64)
return res
Expand All @@ -130,40 +105,16 @@ func (this *RowMap) GetInt(key string) int {
return res
}

func (this *RowMap) GetIntD(key string, def int) int {
res, err := strconv.Atoi(this.GetString(key))
if err != nil {
return def
}
return res
}

func (this *RowMap) GetUint(key string) uint {
res, _ := strconv.ParseUint(this.GetString(key), 10, 0)
return uint(res)
}

func (this *RowMap) GetUintD(key string, def uint) uint {
res, err := strconv.ParseUint(this.GetString(key), 10, 0)
if err != nil {
return def
}
return uint(res)
}

func (this *RowMap) GetUint64(key string) uint64 {
res, _ := strconv.ParseUint(this.GetString(key), 10, 64)
return res
}

func (this *RowMap) GetUint64D(key string, def uint64) uint64 {
res, err := strconv.ParseUint(this.GetString(key), 10, 64)
if err != nil {
return def
}
return res
}

func (this *RowMap) GetUint32(key string) uint32 {
res, _ := strconv.ParseUint(this.GetString(key), 10, 32)
return uint32(res)
Expand All @@ -181,7 +132,7 @@ func (this *RowMap) GetTime(key string) time.Time {
}

// knownDBs is a DB cache by uri
var knownDBs map[string]*sql.DB = make(map[string]*sql.DB)
var knownDBs = make(map[string]*sql.DB)
var knownDBsMutex = &sync.Mutex{}

// GetGenericDB returns a DB instance based on uri.
Expand All @@ -203,12 +154,6 @@ func GetGenericDB(driverName, dataSourceName string) (*sql.DB, bool, error) {
return knownDBs[dataSourceName], exists, nil
}

// GetDB returns a MySQL DB instance based on uri.
// bool result indicates whether the DB was returned from cache; err
func GetDB(mysql_uri string) (*sql.DB, bool, error) {
return GetGenericDB("mysql", mysql_uri)
}

// GetSQLiteDB returns a SQLite DB instance based on DB file name.
// bool result indicates whether the DB was returned from cache; err
func GetSQLiteDB(dbFile string) (*sql.DB, bool, error) {
Expand Down
7 changes: 0 additions & 7 deletions go/vt/vtorc/collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ func init() {
namedCollection = make(map[string](*Collection))
}

// StopMonitoring stops monitoring all the collections
func StopMonitoring() {
for _, q := range namedCollection {
q.StopAutoExpiration()
}
}

// CreateOrReturnCollection allows for creation of a new collection or
// returning a pointer to an existing one given the name. This allows access
// to the data structure from the api interface (http/api.go) and also when writing (inst).
Expand Down
4 changes: 0 additions & 4 deletions go/vt/vtorc/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@ var configurationLoaded = make(chan bool)
const (
HealthPollSeconds = 1
ActiveNodeExpireSeconds = 5
MaintenanceOwner = "vtorc"
AuditPageSize = 20
MaintenancePurgeDays = 7
MaintenanceExpireMinutes = 10
DebugMetricsIntervalSeconds = 10
StaleInstanceCoordinatesExpireSeconds = 60
DiscoveryMaxConcurrency = 300 // Number of goroutines doing hosts discovery
DiscoveryQueueCapacity = 100000
DiscoveryQueueMaxStatisticsSize = 120
DiscoveryCollectionRetentionSeconds = 120
UnseenInstanceForgetHours = 240 // Number of hours after which an unseen instance is forgotten
CandidateInstanceExpireMinutes = 60 // Minutes after which a suggestion to use an instance as a candidate replica (to be preferably promoted on primary failover) is expired.
FailureDetectionPeriodBlockMinutes = 60 // The time for which an instance's failure discovery is kept "active", so as to avoid concurrent "discoveries" of the instance's failure; this preceeds any recovery process, if any.
)

Expand Down
5 changes: 1 addition & 4 deletions go/vt/vtorc/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,11 @@ func execInternal(db *sql.DB, query string, args ...any) (sql.Result, error) {

// ExecVTOrc will execute given query on the vtorc backend database.
func ExecVTOrc(query string, args ...any) (sql.Result, error) {
var err error
query = translateStatement(query)
db, err := OpenVTOrc()
if err != nil {
return nil, err
}
res, err := sqlutils.ExecNoPrepare(db, query, args...)
return res, err
return execInternal(db, query, args...)
}

// QueryVTOrcRowsMap
Expand Down
13 changes: 0 additions & 13 deletions go/vt/vtorc/inst/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package inst

import (
"encoding/json"
"strings"
"time"

topodatapb "vitess.io/vitess/go/vt/proto/topodata"
Expand Down Expand Up @@ -156,18 +155,6 @@ func (replicationAnalysis *ReplicationAnalysis) MarshalJSON() ([]byte, error) {
return json.Marshal(i)
}

// AnalysisString returns a human friendly description of all analysis issues
func (replicationAnalysis *ReplicationAnalysis) AnalysisString() string {
result := []string{}
if replicationAnalysis.Analysis != NoProblem {
result = append(result, string(replicationAnalysis.Analysis))
}
for _, structureAnalysis := range replicationAnalysis.StructureAnalysis {
result = append(result, string(structureAnalysis))
}
return strings.Join(result, ", ")
}

// Get a string description of the analyzed instance type (primary? co-primary? intermediate-primary?)
func (replicationAnalysis *ReplicationAnalysis) GetAnalysisInstanceType() AnalysisInstanceType {
if replicationAnalysis.IsCoPrimary {
Expand Down
26 changes: 0 additions & 26 deletions go/vt/vtorc/inst/audit.go

This file was deleted.

45 changes: 0 additions & 45 deletions go/vt/vtorc/inst/audit_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"
"time"

"vitess.io/vitess/go/vt/external/golib/sqlutils"
"vitess.io/vitess/go/vt/log"

"github.com/rcrowley/go-metrics"
Expand Down Expand Up @@ -109,50 +108,6 @@ func AuditOperation(auditType string, tabletAlias string, message string) error
return nil
}

// ReadRecentAudit returns a list of audit entries order chronologically descending, using page number.
func ReadRecentAudit(tabletAlias string, page int) ([]Audit, error) {
res := []Audit{}
args := sqlutils.Args()
whereCondition := ``
if tabletAlias != "" {
whereCondition = `where alias=?`
args = append(args, tabletAlias)
}
query := fmt.Sprintf(`
select
audit_id,
audit_timestamp,
audit_type,
alias,
message
from
audit
%s
order by
audit_timestamp desc
limit ?
offset ?
`, whereCondition)
args = append(args, config.AuditPageSize, page*config.AuditPageSize)
err := db.QueryVTOrc(query, args, func(m sqlutils.RowMap) error {
audit := Audit{}
audit.AuditID = m.GetInt64("audit_id")
audit.AuditTimestamp = m.GetString("audit_timestamp")
audit.AuditType = m.GetString("audit_type")
audit.AuditTabletAlias = m.GetString("alias")
audit.Message = m.GetString("message")

res = append(res, audit)
return nil
})

if err != nil {
log.Error(err)
}
return res, err

}

// ExpireAudit removes old rows from the audit table
func ExpireAudit() error {
return ExpireTableData("audit", "audit_timestamp")
Expand Down
58 changes: 54 additions & 4 deletions go/vt/vtorc/inst/audit_dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package inst

import (
"fmt"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/vt/external/golib/sqlutils"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/vtorc/config"
Expand Down Expand Up @@ -75,7 +77,7 @@ func TestAuditOperation(t *testing.T) {
auditType := "test-audit-operation"
message := "test-message"

t.Run("Audit to backend", func(t *testing.T) {
t.Run("audit to backend", func(t *testing.T) {
config.Config.AuditLogFile = ""
config.Config.AuditToSyslog = false
config.Config.AuditToBackendDB = true
Expand All @@ -85,7 +87,7 @@ func TestAuditOperation(t *testing.T) {
require.NoError(t, err)

// Check that we can read the recent audits
audits, err := ReadRecentAudit(tab100Alias, 0)
audits, err := readRecentAudit(tab100Alias, 0)
require.NoError(t, err)
require.Len(t, audits, 1)
require.EqualValues(t, 1, audits[0].AuditID)
Expand All @@ -94,7 +96,7 @@ func TestAuditOperation(t *testing.T) {
require.EqualValues(t, tab100Alias, audits[0].AuditTabletAlias)

// Check the same for no-filtering
audits, err = ReadRecentAudit("", 0)
audits, err = readRecentAudit("", 0)
require.NoError(t, err)
require.Len(t, audits, 1)
require.EqualValues(t, 1, audits[0].AuditID)
Expand All @@ -103,7 +105,7 @@ func TestAuditOperation(t *testing.T) {
require.EqualValues(t, tab100Alias, audits[0].AuditTabletAlias)
})

t.Run("Audit to File", func(t *testing.T) {
t.Run("audit to File", func(t *testing.T) {
config.Config.AuditToBackendDB = false
config.Config.AuditToSyslog = false

Expand All @@ -124,3 +126,51 @@ func TestAuditOperation(t *testing.T) {
require.Contains(t, string(fileContent), "\ttest-audit-operation\tzone-1-0000000100\t[ks:0]\ttest-message")
})
}

// audit presents a single audit entry (namely in the database)
type audit struct {
AuditID int64
AuditTimestamp string
AuditType string
AuditTabletAlias string
Message string
}

// readRecentAudit returns a list of audit entries order chronologically descending, using page number.
func readRecentAudit(tabletAlias string, page int) ([]audit, error) {
res := []audit{}
var args []any
whereCondition := ``
if tabletAlias != "" {
whereCondition = `where alias=?`
args = append(args, tabletAlias)
}
query := fmt.Sprintf(`
select
audit_id,
audit_timestamp,
audit_type,
alias,
message
from
audit
%s
order by
audit_timestamp desc
limit ?
offset ?
`, whereCondition)
args = append(args, config.AuditPageSize, page*config.AuditPageSize)
err := db.QueryVTOrc(query, args, func(m sqlutils.RowMap) error {
a := audit{}
a.AuditID = m.GetInt64("audit_id")
a.AuditTimestamp = m.GetString("audit_timestamp")
a.AuditType = m.GetString("audit_type")
a.AuditTabletAlias = m.GetString("alias")
a.Message = m.GetString("message")

res = append(res, a)
return nil
})
return res, err
}
Loading

0 comments on commit 5356ef5

Please sign in to comment.