Skip to content

Commit

Permalink
bugfix: no set tables on start migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxin45 committed Aug 1, 2017
1 parent 3d2e826 commit 18c77a4
Show file tree
Hide file tree
Showing 19 changed files with 47 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ operator/operator

*.tar.gz

# docker/migrator/pkg/*.pyc

# swagger lib
swagger/css
swagger/fonts
Expand Down
Binary file removed docker/migrator/logs.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion docker/migrator/pkg/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, db, host, port, user, password, tables=None):
self.tables = tables

def getDataDir(self):
return ROOT + "/" + self.db
return ROOT + self.db

def getDumpedMeta(self):
return self.getDataDir() + "/metadata"
Expand Down
Binary file removed docker/migrator/pkg/config.pyc
Binary file not shown.
Binary file removed docker/migrator/pkg/logs.pyc
Binary file not shown.
7 changes: 2 additions & 5 deletions docker/migrator/pkg/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def dump(self):
# load local data to tidb
def load(self):
if not path.isfile(self.src.getDumpedMeta()):
self.dump(self.notice)
self.dump()

rest.sync_stat(self.notice, 'Loading')
cmds = self.dest.toLoader()
Expand All @@ -56,9 +56,6 @@ def load(self):
logs.critical("load error")

def sync(self):
if not path.isfile(self.src.getCheckpoint()):
self.load(self.notice)

self.src.genSyncConfigFile(self.dest)
if not path.isfile(self.src.getMeta()):
self.src.genSyncMetaFile()
Expand Down Expand Up @@ -151,4 +148,4 @@ def main(argv):
if __name__ == "__main__":
main(sys.argv[1:])

# migrator.py --database xinyang1 --src-host 10.213.125.4 --src-port 13306 --src-user root --src-password EJq4dspojdY3FmVF?TYVBkEMB --dest-host 10.213.44.128 --dest-port 14051 --dest-user xinyang1 --dest-password xinyang1 --operator sync --notice http://10.213.44.128:12808/tidb/api/v1/tidbs/006-xinyang1 --tables t1,t2
# migrator.py --database xinyang1 --src-host 10.213.125.85 --src-port 13306 --src-user root --src-password EJq4dspojdY3FmVF?TYVBkEMB --dest-host 10.213.44.128 --dest-port 14988 --dest-user xinyang1 --dest-password xinyang1 --operator sync --notice http://10.213.44.128:12808/tidb/api/v1/tidbs/006-xinyang1 --tables t1,t2
Binary file removed docker/migrator/pkg/rest.pyc
Binary file not shown.
Binary file removed docker/migrator/pkg/shell.pyc
Binary file not shown.
Binary file removed docker/migrator/pkg/sync.pyc
Binary file not shown.
Binary file removed docker/migrator/sync.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions kubernetes/migrator-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ spec:
--dest-user {{du}} \
--dest-password {{dp}} \
--operator {{op}} \
--tables "{{tables}}" \
--notice "{{api}}"
env:
- name: TZ
Expand Down
10 changes: 7 additions & 3 deletions operator/controllers/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,17 @@ func (dc *TidbController) Migrate() {
}
m := &Migrator{
Include: true,
Notify: true,
}
if err := json.Unmarshal(b, m); err != nil {
dc.CustomAbort(400, fmt.Sprintf("Parse body error: %v", err))
}
db, err := operator.GetDb(cell)
errHandler(dc.Controller, err, "get db "+cell)

api := fmt.Sprintf(statAPI, beego.BConfig.Listen.HTTPAddr, beego.BConfig.Listen.HTTPPort, cell)
api := ""
if m.Notify {
api = fmt.Sprintf(statAPI, beego.BConfig.Listen.HTTPAddr, beego.BConfig.Listen.HTTPPort, cell)
}
errHandler(
dc.Controller,
db.Migrate(m.Mysql, api, m.Sync, m.Include, m.Tables),
Expand All @@ -261,9 +264,10 @@ func (dc *TidbController) Migrate() {
// Migrator a migrated target
type Migrator struct {
mysqlutil.Mysql `json:",inline"`
Include bool `json:"include,omitempty"`
Include bool `json:"include"`
Tables []string `json:"tables,omitempty"`
Sync bool `json:"sync,omitempty"`
Notify bool `json:"notify"`
}

func errHandler(c beego.Controller, err error, msg string) {
Expand Down
4 changes: 2 additions & 2 deletions operator/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestDb_Migrate(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err = db.stopMigrator(); err != nil {
if err = db.StopMigrator(); err != nil {
t.Error(err)
}
time.Sleep(6 * time.Second)
Expand All @@ -26,7 +26,7 @@ func TestDb_Migrate(t *testing.T) {
User: "root",
Password: "EJq4dspojdY3FmVF?TYVBkEMB",
}
if err = db.Migrate(src, "", true); err != nil {
if err = db.Migrate(src, "", true, true, nil); err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Second)
Expand Down
1 change: 1 addition & 0 deletions operator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (db *Db) startMigrator(my *tsql.Migration) (err error) {
"{{dh}}", my.Dest.IP, "{{dP}}", fmt.Sprintf("%v", my.Dest.Port),
"{{du}}", my.Dest.User, "{{dp}}", my.Dest.Password,
"{{op}}", sync,
"{{tables}}", strings.Join(my.Tables, ","),
"{{api}}", my.NotifyAPI)
s := r.Replace(mysqlMigrateYaml)
var j []byte
Expand Down
12 changes: 12 additions & 0 deletions operator/migrator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package operator

import "testing"

func TestDb_StopMigrator(t *testing.T) {
db := NewDb()
db.Metadata.Name = "006-xinyang1"
err := db.StopMigrator()
if err != nil {
t.Fatal(err)
}
}
1 change: 1 addition & 0 deletions operator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ spec:
--dest-user {{du}} \
--dest-password {{dp}} \
--operator {{op}} \
--tables "{{tables}}" \
--notice "{{api}}"
env:
- name: TZ
Expand Down
8 changes: 6 additions & 2 deletions pkg/util/k8sutil/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ func CreateAndWaitJob(job *v1.Job, timeout time.Duration) (*v1.Job, error) {
// DeleteJob delete a job by name
func DeleteJob(name string) error {
err := kubecli.BatchV1().Jobs(Namespace).Delete(name, &metav1.DeleteOptions{})
if !apierrors.IsNotFound(err) {
if err != nil && !apierrors.IsNotFound(err) {
return err
}
return DeletePodsByLabel(map[string]string{"job-name": name})
err = DeletePodsByLabel(map[string]string{"job-name": name})
if err != nil {
return err
}
return nil
}

// GetJob get a job by name
Expand Down
1 change: 1 addition & 0 deletions pkg/util/mysqlutil/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (m *Migration) Check() error {
}
}
}
m.Tables = tables
cmd += (" " + strings.Join(tables, " "))
}
o, err := execShell(cmd)
Expand Down
12 changes: 11 additions & 1 deletion test/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,28 @@ func Test_DeleteDB(t *testing.T) {
}
}

/*
CREATE TABLE t1 (id INT, age INT, PRIMARY KEY(id)) ENGINE=InnoDB;
CREATE TABLE t2 (id INT, name VARCHAR(256), PRIMARY KEY(id)) ENGINE=InnoDB;
CREATE TABLE t_error (
c timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3);
INSERT INTO t2 VALUES (1, "a"), (2, "b"), (3, "c");
*/
func Test_Migrate(t *testing.T) {
m := controllers.Migrator{
Mysql: mysqlutil.Mysql{
Database: "xinyang1",
IP: "10.213.125.4",
IP: "10.213.125.85",
Port: 13306,
User: "xinyang1",
Password: "xinyang1",
},
Include: true,
Tables: []string{"t1", "t2"},
Sync: false,
Notify: false,
}
body, _ := json.Marshal(m)
fmt.Printf("%s", body)
Expand Down

0 comments on commit 18c77a4

Please sign in to comment.