Skip to content

Commit 469ccb4

Browse files
Merge pull request #38 from kamil-holubicki/DISTMYSQL-421
DISTMYSQL-421: Remove master-slave terminologies in Orchestrator
2 parents b3863ba + 27785ce commit 469ccb4

File tree

19 files changed

+541
-111
lines changed

19 files changed

+541
-111
lines changed

docker/Dockerfile.system

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ RUN git clone -b $ci_env_branch $ci_env_repo # cache
1919

2020
# Setup dbdeployer
2121
RUN mkdir /dbdeployer
22-
RUN (cd /dbdeployer && wget https://github.com/datacharmer/dbdeployer/releases/download/v1.64.0/dbdeployer-1.64.0.linux.tar.gz)
23-
RUN (cd /dbdeployer && tar -xf dbdeployer-1.64.0.linux.tar.gz)
24-
RUN (cd /dbdeployer && ln -s dbdeployer-1.64.0.linux dbdeployer)
22+
RUN (cd /dbdeployer && wget https://github.com/datacharmer/dbdeployer/releases/download/v1.73.0/dbdeployer-1.73.0.linux.tar.gz)
23+
RUN (cd /dbdeployer && tar -xf dbdeployer-1.73.0.linux.tar.gz)
24+
RUN (cd /dbdeployer && ln -s dbdeployer-1.73.0.linux dbdeployer)
2525
RUN (cd /dbdeployer && ./dbdeployer defaults update reserved-ports '0')
2626
RUN (cd /orchestrator/orchestrator-ci-env/bin/linux && ln -s /dbdeployer/dbdeployer)
2727

28+
# Temporary solution:
29+
# dbdeployer patched to work with PS 8.4
30+
COPY docker/resources/dbdeployer /dbdeployer/dbdeployer84
31+
RUN (cd /dbdeployer && ./dbdeployer84 defaults update reserved-ports '0')
32+
RUN (cd /orchestrator/orchestrator-ci-env/bin/linux && ln -s /dbdeployer/dbdeployer84)
33+
2834
# For dev purposes only, just to avoid downloading over and over via download-mysql script
2935
# RUN (mkdir /orchestrator/orchestrator-ci-env/mysql-tarballs-downloaded)
30-
# COPY docker/Percona-Server-8.0.30-22-Linux.x86_64.glibc2.17-minimal.tar.gz /orchestrator/orchestrator-ci-env/mysql-tarballs-downloaded/
36+
# COPY docker/mysql-8.4.0-linux-glibc2.17-x86_64-minimal.tar.xz /orchestrator/orchestrator-ci-env/mysql-tarballs-downloaded/
3137

3238
RUN (cd /orchestrator/orchestrator-ci-env && cp bin/linux/systemctl.py /usr/bin/systemctl)
3339
RUN (cd /orchestrator/orchestrator-ci-env && script/deploy-haproxy)

docker/Dockerfile.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM golang:1.20.3-bullseye
22
LABEL maintainer="[email protected]"
33

44
RUN apt-get update
5-
RUN apt-get install -y lsb-release rsync libaio1 numactl sqlite3
5+
RUN apt-get install -y lsb-release rsync libaio1 numactl sqlite3 libncurses5 libtinfo5
66
RUN rm -rf /var/lib/apt/lists/*
77

88
ENV WORKPATH /go/src/github.com/openark/orchestrator

docker/resources/dbdeployer

13.1 MB
Binary file not shown.

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab h1:xveKWz2iauee
2323
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AEU963A2AYjv4d1V5eVL1CQbEJq6aCNHDDjibzu8=
2424
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
2525
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
26+
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
2627
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
2728
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
2829
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=

go/cmd/orchestrator/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func main() {
136136

137137
if len(flag.Args()) == 0 && *command == "" {
138138
// No command, no argument: just prompt
139-
fmt.Println(app.AppPrompt)
139+
fmt.Print(app.AppPrompt)
140140
return
141141
}
142142

go/db/db.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func openTopology(host string, port int, readTimeout int) (db *sql.DB, err error
107107
return nil, err
108108
}
109109
}
110-
sqlUtilsLogger := SqlUtilsLogger{client_context: host + ":" + strconv.Itoa(port)}
110+
sqlUtilsLogger := SqlUtilsLogger{client_context: host + ":" + strconv.Itoa(port), backend_connection: false}
111111
if db, _, err = sqlutils.GetDB(mysql_uri, sqlUtilsLogger); err != nil {
112112
return nil, err
113113
}
@@ -131,7 +131,7 @@ func openOrchestratorMySQLGeneric() (db *sql.DB, fromCache bool, err error) {
131131
if config.Config.MySQLOrchestratorUseMutualTLS {
132132
uri, _ = SetupMySQLOrchestratorTLS(uri)
133133
}
134-
sqlUtilsLogger := SqlUtilsLogger{client_context: config.Config.MySQLOrchestratorHost + ":" + strconv.FormatUint(uint64(config.Config.MySQLOrchestratorPort), 10)}
134+
sqlUtilsLogger := SqlUtilsLogger{client_context: config.Config.MySQLOrchestratorHost + ":" + strconv.FormatUint(uint64(config.Config.MySQLOrchestratorPort), 10), backend_connection: true}
135135
return sqlutils.GetDB(uri, sqlUtilsLogger)
136136
}
137137

@@ -200,7 +200,7 @@ func OpenOrchestrator() (db *sql.DB, err error) {
200200
}
201201
}
202202
dsn := getMySQLURI()
203-
sqlUtilsLogger := SqlUtilsLogger{client_context: config.Config.MySQLOrchestratorHost + ":" + strconv.FormatUint(uint64(config.Config.MySQLOrchestratorPort), 10)}
203+
sqlUtilsLogger := SqlUtilsLogger{client_context: config.Config.MySQLOrchestratorHost + ":" + strconv.FormatUint(uint64(config.Config.MySQLOrchestratorPort), 10), backend_connection: true}
204204
db, fromCache, err = sqlutils.GetDB(dsn, sqlUtilsLogger)
205205
if err == nil && !fromCache {
206206
log.Debugf("Connected to orchestrator backend: %v", safeMySQLURI(dsn))

go/db/tls.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,48 @@ func init() {
5757
}
5858

5959
type SqlUtilsLogger struct {
60-
client_context string
60+
client_context string
61+
backend_connection bool
6162
}
6263

6364
func (logger SqlUtilsLogger) OnError(caller_context string, query string, err error) error {
64-
query = strings.Join(strings.Fields(query), " ") // trim whitespaces
65-
query = strings.Replace(query, "%", "%%", -1) // escape %
65+
query = strings.Join(strings.Fields(query), " ") // trim whitespaces
66+
query = strings.Replace(query, "%", "%%", -1) // escape %
6667

6768
msg := fmt.Sprintf("%+v(%+v) %+v: %+v",
68-
caller_context,
69-
logger.client_context,
70-
query,
71-
err)
69+
caller_context,
70+
logger.client_context,
71+
query,
72+
err)
7273

7374
return log.Errorf(msg)
7475
}
7576

77+
// This validator is for dev purposes only. Call of this validator is
78+
// disabled in sqlutils.go
79+
var query_whitelist = []string{
80+
"substring_index(host, ':', 1) as slave_hostname",
81+
}
82+
83+
func (logger SqlUtilsLogger) ValidateQuery(query string) {
84+
if logger.backend_connection {
85+
return
86+
}
87+
88+
// check if whitelisted
89+
for i := 0; i < len(query_whitelist); i++ {
90+
if strings.Contains(query, query_whitelist[i]) {
91+
return
92+
}
93+
}
94+
95+
lquery := strings.ToLower(query)
96+
if strings.Contains(lquery, "master") || strings.Contains(lquery, "slave") {
97+
log.Error("QUERY CONTAINS MASTER / SLAVE: ")
98+
// panic("Query contains master/slave: " + query)
99+
}
100+
}
101+
76102
func requiresTLS(host string, port int, mysql_uri string) bool {
77103
cacheKey := fmt.Sprintf("%s:%d", host, port)
78104

@@ -82,7 +108,7 @@ func requiresTLS(host string, port int, mysql_uri string) bool {
82108
}
83109

84110
required := false
85-
sqlUtilsLogger := SqlUtilsLogger{client_context: host + ":" + strconv.Itoa(port)}
111+
sqlUtilsLogger := SqlUtilsLogger{client_context: host + ":" + strconv.Itoa(port), backend_connection: false}
86112
db, _, _ := sqlutils.GetDB(mysql_uri, sqlUtilsLogger)
87113
if err := db.Ping(); err != nil && (strings.Contains(err.Error(), Error3159) || strings.Contains(err.Error(), Error1045)) {
88114
required = true

go/golib/sqlutils/sqlutils.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,34 @@ func (this *RowMap) GetTime(key string) time.Time {
170170
return time.Time{}
171171
}
172172

173+
func validateQuery(query string, db *sql.DB) {
174+
// dev purposes only. Remove this return to call query validator function.
175+
return
176+
177+
knownDBsMutex.RLock()
178+
defer knownDBsMutex.RUnlock()
179+
180+
if logger, exists := DB2logger[db]; exists && logger != nil {
181+
logger.ValidateQuery((query))
182+
}
183+
}
184+
173185
// knownDBs is a DB cache by uri
174186
var knownDBs map[string]*sql.DB = make(map[string]*sql.DB)
175187
var knownDBsMutex = &sync.RWMutex{}
176188

177189
type Logger interface {
178190
OnError(context string, query string, err error) error
191+
ValidateQuery(query string)
179192
}
193+
180194
// it is also protected by knownDBsMutex
181195
var DB2logger map[*sql.DB]Logger = make(map[*sql.DB]Logger)
182196

183197
// GetDB returns a DB instance based on uri.
184198
// logger parameter is optional. If nil, internal logging will be used.
185199
// bool result indicates whether the DB was returned from cache; err
186-
func GetGenericDB(driverName, dataSourceName string ,logger Logger) (*sql.DB, bool, error) {
200+
func GetGenericDB(driverName, dataSourceName string, logger Logger) (*sql.DB, bool, error) {
187201
knownDBsMutex.Lock()
188202
defer func() {
189203
knownDBsMutex.Unlock()
@@ -221,7 +235,7 @@ func GetSQLiteDB(dbFile string, logger Logger) (*sql.DB, bool, error) {
221235
func RowToArray(rows *sql.Rows, columns []string) []CellData {
222236
buff := make([]interface{}, len(columns))
223237
data := make([]CellData, len(columns))
224-
for i, _ := range buff {
238+
for i := range buff {
225239
buff[i] = data[i].NullString()
226240
}
227241
rows.Scan(buff...)
@@ -265,12 +279,10 @@ func ScanRowsToMaps(rows *sql.Rows, on_row func(RowMap) error) error {
265279
return err
266280
}
267281

268-
func logErrorInternal(context string, db *sql.DB, query string, err error) error{
282+
func logErrorInternal(context string, db *sql.DB, query string, err error) error {
269283
// find logger registered by the client
270284
knownDBsMutex.RLock()
271-
defer func() {
272-
knownDBsMutex.RUnlock()
273-
}()
285+
defer knownDBsMutex.RUnlock()
274286

275287
if logger, exists := DB2logger[db]; exists && logger != nil {
276288
return logger.OnError(context, query, err)
@@ -282,6 +294,7 @@ func logErrorInternal(context string, db *sql.DB, query string, err error) error
282294
// QueryRowsMap is a convenience function allowing querying a result set while poviding a callback
283295
// function activated per read row.
284296
func QueryRowsMap(db *sql.DB, query string, on_row func(RowMap) error, args ...interface{}) (err error) {
297+
validateQuery(query, db)
285298
defer func() {
286299
if derr := recover(); derr != nil {
287300
err = fmt.Errorf("QueryRowsMap unexpected error: %+v", derr)
@@ -302,6 +315,7 @@ func QueryRowsMap(db *sql.DB, query string, on_row func(RowMap) error, args ...i
302315

303316
// queryResultData returns a raw array of rows for a given query, optionally reading and returning column names
304317
func queryResultData(db *sql.DB, query string, retrieveColumns bool, args ...interface{}) (resultData ResultData, columns []string, err error) {
318+
validateQuery(query, db)
305319
defer func() {
306320
if derr := recover(); derr != nil {
307321
err = errors.New(fmt.Sprintf("QueryRowsMap unexpected error: %+v", derr))
@@ -358,6 +372,7 @@ func QueryRowsMapBuffered(db *sql.DB, query string, on_row func(RowMap) error, a
358372

359373
// ExecNoPrepare executes given query using given args on given DB, without using prepared statements.
360374
func ExecNoPrepare(db *sql.DB, query string, args ...interface{}) (res sql.Result, err error) {
375+
validateQuery(query, db)
361376
defer func() {
362377
if derr := recover(); derr != nil {
363378
err = errors.New(fmt.Sprintf("ExecNoPrepare unexpected error: %+v", derr))
@@ -374,6 +389,7 @@ func ExecNoPrepare(db *sql.DB, query string, args ...interface{}) (res sql.Resul
374389
// ExecQuery executes given query using given args on given DB. It will safele prepare, execute and close
375390
// the statement.
376391
func execInternal(silent bool, db *sql.DB, query string, args ...interface{}) (res sql.Result, err error) {
392+
validateQuery(query, db)
377393
defer func() {
378394
if derr := recover(); derr != nil {
379395
err = errors.New(fmt.Sprintf("execInternal unexpected error: %+v", derr))

go/inst/cluster_alias_dao.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func UpdateClusterAliases() error {
206206
// MySQL backend (Orchestrator supports only SQLite and MySQL backends)
207207
// INSERT ON DUPLICATE KEY UPDATE is more performant than REPLACE in MySQL
208208
err = updateClusterAliasesUsingInsert()
209-
if (err != nil) {
209+
if err != nil {
210210
// Fallback to the original, safe implementation
211211
err = updateClusterAliasesUsingReplace()
212212
}

go/inst/instance.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ type Instance struct {
148148

149149
// Primary of the replication group
150150
ReplicationGroupPrimaryInstanceKey InstanceKey
151+
152+
// Query string provider
153+
QSP QueryStringProvider
151154
}
152155

153156
// NewInstance creates a new, empty instance

0 commit comments

Comments
 (0)