Skip to content

Commit

Permalink
adding big ddl command
Browse files Browse the repository at this point in the history
  • Loading branch information
bo-er committed Nov 19, 2024
1 parent 4f17a95 commit 2be74a4
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 7 deletions.
77 changes: 77 additions & 0 deletions backend/big_ddl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package backend

import (
"fmt"

"github.com/bo-er/corrupt-mysql/pkg"
"github.com/sirupsen/logrus"

Check failure on line 7 in backend/big_ddl.go

View workflow job for this annotation

GitHub Actions / build

cannot find module providing package github.com/sirupsen/logrus: import lookup disabled by -mod=vendor
)

const enablePerformanceSchemaSQL = `
UPDATE performance_schema.setup_instruments
SET ENABLED='YES', TIMED='YES'
WHERE NAME LIKE 'stage/innodb/alter%';
UPDATE performance_schema.setup_consumers
SET ENABLED='YES'
WHERE NAME LIKE '%stage%';
`

const createBigDDLSQL = `
CREATE DATABASE IF NOT EXISTS TESTDB;
USE TESTDB;
CREATE TABLE IF NOT EXISTS t1(x int primary key auto_increment);
INSERT INTO t1() VALUES (),(),(),();
INSERT INTO t1(x) SELECT x+(SELECT COUNT(*) FROM t1) FROM t1;
`

// why using this COPY algorithm?
// see https://dev.mysql.com/blog-archive/mysql-8-0-innodb-now-supports-instant-add-column/
const triggerBigDDLSQL = `
ALTER TABLE testdb.t1 ADD COLUMN col1 INT NOT NULL, ALGORITHM=COPY;
`

func CreateBigDDL(c pkg.Connect) error {
db, err := pkg.GetDB(c)
if err != nil {
return err
}

// Execute preparatory SQL to enable performance schema monitoring
err = pkg.BatchExec(db, enablePerformanceSchemaSQL)
if err != nil {
return fmt.Errorf("error enabling performance schema: %w", err)
}
logrus.Info("Performance schema monitoring enabled")

err = pkg.BatchExec(db, createBigDDLSQL)
if err != nil {
return err
}
defer func() {
// Drop the TESTDB database
_, err = db.Exec("DROP DATABASE TESTDB")
if err != nil {
logrus.Errorf("error dropping database: %v", err)
}
}()

// Execute the doubling logic in a loop
for i := 0; i < 23; i++ {
logrus.Infof("inserting data, round %d/%d", i, 23)
err = pkg.Exec(db, "INSERT INTO t1(x) SELECT x + (SELECT COUNT(*) FROM t1) FROM t1")
if err != nil {
return fmt.Errorf("error doubling data: %w", err)
}
}

// Trigger the big DDL
fmt.Println("Triggering big DDL...")
_, err = db.Exec(triggerBigDDLSQL)
if err != nil {
return fmt.Errorf("error triggering big DDL: %w", err)
}

return nil
}
3 changes: 2 additions & 1 deletion backend/big_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/bo-er/corrupt-mysql/pkg"
"github.com/inhies/go-bytesize"
"github.com/sirupsen/logrus"
)

const prepareBigTransactionSQL = `
Expand Down Expand Up @@ -55,7 +56,7 @@ func getPower(size string) (int, error) {
if kb < 10 {
return 0, errors.New("please enter a number that's bigger than 10 kilobytes")
}
fmt.Println("bytes: ", kb)
logrus.Info("bytes: ", kb)
ratio := (kb / 10240) * 20
return int(math.Ceil(ratio)), nil
}
Expand Down
32 changes: 31 additions & 1 deletion backend/deadlock.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package backend

import (
"fmt"
"sync"

"github.com/bo-er/corrupt-mysql/pkg"
"github.com/sirupsen/logrus"
)

const prepareDeadlockSQL = `
Expand Down Expand Up @@ -43,21 +45,49 @@ func CreateDeadlock(c pkg.Connect) error {
if err != nil {
return err
}

// Get the original innodb_lock_wait_timeout
var (
variableName string
originalTimeout int
)
err = db.QueryRow("SHOW VARIABLES LIKE 'innodb_lock_wait_timeout'").Scan(&variableName, &originalTimeout)
if err != nil {
return fmt.Errorf("error getting original timeout: %w", err)
}
logrus.Infof("original timeout is %d", originalTimeout)
newTimeout := 60
err = pkg.Exec(db, "SET GLOBAL innodb_lock_wait_timeout = ?", newTimeout)
if err != nil {
return fmt.Errorf("error setting new timeout: %w", err)
}
defer func() {
// Restore the original innodb_lock_wait_timeout
err = pkg.Exec(db, "SET GLOBAL innodb_lock_wait_timeout = ?", originalTimeout)
if err != nil {
logrus.Errorf("error restoring original timeout: %v", err)
}
}()

err = pkg.BatchExec(db, prepareDeadlockSQL)
if err != nil {
return err
}

var wg sync.WaitGroup
wg.Add(2)
for _, sql := range []string{Thread1, Thread2} {
sql := sql
go func() {
defer wg.Done()
err = pkg.BatchExec(db, sql)
if err != nil {
panic(err.Error())
// Handle the deadlock error (e.g., log it)
logrus.Errorf("Error: %v", err)
}
}()
}
wg.Wait()

return nil
}
29 changes: 29 additions & 0 deletions cmd/big_ddl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"github.com/bo-er/corrupt-mysql/backend"
"github.com/bo-er/corrupt-mysql/pkg"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// bigDDLCmd creates a big DDL in a MySQL instance.
var bigDDLCmd = &cobra.Command{
Use: "bigddl",
Short: "creating a big DDL in a MySQL instance.",
Long: `./corrupt-mysql bigddl -H10.186.62.63 -P25690 -uuniverse_udb -p123`,
Run: func(cmd *cobra.Command, args []string) {
connect := pkg.Connect{
User: user,
Host: host,
Password: password,
DBName: "mysql",
Port: port,
}
err := backend.CreateBigDDL(connect)
if err != nil {
logrus.Error(err)
}
logrus.Info("operation is done")
},
}
4 changes: 3 additions & 1 deletion cmd/big_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/bo-er/corrupt-mysql/backend"
"github.com/bo-er/corrupt-mysql/pkg"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -25,7 +26,8 @@ var bigTransaction = &cobra.Command{

err := backend.CreatesBigTransactions(connect, args[0])
if err != nil {
panic(err.Error())
logrus.Error(err)
}
logrus.Info("operation is done")
},
}
4 changes: 3 additions & 1 deletion cmd/deadlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/bo-er/corrupt-mysql/backend"
"github.com/bo-er/corrupt-mysql/pkg"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -21,7 +22,8 @@ var deadlockCmd = &cobra.Command{
}
err := backend.CreateDeadlock(connect)
if err != nil {
panic(err.Error())
logrus.Error(err)
}
logrus.Info("operation is done")
},
}
4 changes: 3 additions & 1 deletion cmd/high_cpu_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/bo-er/corrupt-mysql/backend"
"github.com/bo-er/corrupt-mysql/pkg"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -21,7 +22,8 @@ var highCPUCmd = &cobra.Command{
}
err := backend.MakeCPUUsageHigh(connect)
if err != nil {
panic(err.Error())
logrus.Error(err)
}
logrus.Info("operation is done")
},
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
rootCmd.AddCommand(deadlockCmd)
rootCmd.AddCommand(slowlogCmd)
rootCmd.AddCommand(bigTransaction)
rootCmd.AddCommand(bigDDLCmd)

rootCmd.PersistentFlags().StringVarP(&host, "host", "H", "", "host of the mysql server")
rootCmd.PersistentFlags().IntVarP(&port, "port", "P", 3306, "port of the mysql server")
Expand Down
4 changes: 3 additions & 1 deletion cmd/slow_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/bo-er/corrupt-mysql/backend"
"github.com/bo-er/corrupt-mysql/pkg"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -21,7 +22,8 @@ var slowlogCmd = &cobra.Command{
}
err := backend.CreateSlowlog(connect)
if err != nil {
panic(err.Error())
logrus.Error(err)
}
logrus.Info("operation is done")
},
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package main

import "github.com/bo-er/corrupt-mysql/cmd"
import (
"os"

"github.com/bo-er/corrupt-mysql/cmd"
"github.com/sirupsen/logrus"
)

func main() {
logrus.SetOutput(os.Stdout)
cmd.Execute()
}
9 changes: 9 additions & 0 deletions pkg/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ func BatchExec(db *sql.DB, sqls string) error {
}
return nil
}

func Exec(db *sql.DB, sql string, args ...any) error {
_, err := db.Exec(sql, args...)
if err != nil {
return fmt.Errorf("sql: %s,initializing databases: %s", sql, err.Error())
}
fmt.Println(sql)
return nil
}

0 comments on commit 2be74a4

Please sign in to comment.