Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip index creation during loading MySQL snapshot #278

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions catalog/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,8 @@ func (d *Database) createAllTable(ctx *sql.Context, name string, schema sql.Prim
}

// https://github.com/apecloud/myduckserver/issues/272
TianyuZhang1214 marked this conversation as resolved.
Show resolved Hide resolved
if !(mycontext.IsReplicationQuery(ctx) && configuration.IsReplicationWithoutIndex()) {
if len(primaryKeys) > 0 {
b.WriteString(fmt.Sprintf(", PRIMARY KEY (%s)", strings.Join(primaryKeys, ", ")))
}
if len(primaryKeys) > 0 && !isIndexCreationDisabled(ctx) {
b.WriteString(fmt.Sprintf(", PRIMARY KEY (%s)", strings.Join(primaryKeys, ", ")))
}

b.WriteString(")")
Expand Down Expand Up @@ -205,6 +203,23 @@ func (d *Database) createAllTable(ctx *sql.Context, name string, schema sql.Prim
return nil
}

func isIndexCreationDisabled(ctx *sql.Context) bool {
if !configuration.IsReplicationWithoutIndex() {
return false
}
if mycontext.IsReplicationQuery(ctx) {
return true
}
_, vv, ok := sql.SystemVariables.GetGlobal("replica_is_loading_snapshot")
if !ok {
return false
}
if b, ok := vv.(int8); ok {
return b != 0
}
return false
}

// CreateTable implements sql.TableCreator.
func (d *Database) CreateTable(ctx *sql.Context, name string, schema sql.PrimaryKeySchema, collation sql.CollationID, comment string) error {
d.mu.Lock()
Expand Down
3 changes: 1 addition & 2 deletions catalog/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/apecloud/myduckserver/adapter"
"github.com/apecloud/myduckserver/configuration"
"github.com/apecloud/myduckserver/mycontext"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/marcboeker/go-duckdb"
Expand Down Expand Up @@ -362,7 +361,7 @@ func (t *Table) CreateIndex(ctx *sql.Context, indexDef sql.IndexDef) error {
defer t.mu.Unlock()

// https://github.com/apecloud/myduckserver/issues/272
if mycontext.IsReplicationQuery(ctx) && configuration.IsReplicationWithoutIndex() {
if isIndexCreationDisabled(ctx) {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion configuration/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (

func IsReplicationWithoutIndex() bool {
switch strings.ToLower(os.Getenv(replicationWithoutIndex)) {
case "", "t", "1", "true":
case "", "y", "t", "1", "on", "yes", "true":
return true
}
return false
Expand Down
1 change: 1 addition & 0 deletions devtools/replica-setup-mysql/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ echo "Setting local_infile and server_id..."
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=root --no-password <<EOF
SET GLOBAL local_infile = 1;
SET GLOBAL server_id = ${MYDUCK_SERVER_ID};
SET GLOBAL replica_is_loading_snapshot = ON;
EOF
7 changes: 6 additions & 1 deletion devtools/replica-setup-mysql/snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@ else
fi


echo "Snapshot completed successfully."
echo "Snapshot completed successfully."

echo "Reset replica_is_loading_snapshot..."
mysqlsh --sql --host=${MYDUCK_HOST} --port=${MYDUCK_PORT} --user=root --no-password <<EOF
SET GLOBAL replica_is_loading_snapshot = OFF;
EOF
9 changes: 9 additions & 0 deletions replica/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,14 @@ func RegisterReplicaOptions(options *ReplicaOptions) {
Type: types.NewSystemStringType("report_password"),
Default: options.ReportPassword,
},
// MyDuck-specific system variables
&sql.MysqlSystemVariable{
Name: "replica_is_loading_snapshot",
Scope: sql.GetMysqlScope(sql.SystemVariableScope_Global),
Dynamic: true,
SetVarHintApplies: false,
Type: types.NewSystemBoolType("replica_is_loading_snapshot"),
Default: false,
},
})
}
Loading