Skip to content

Commit

Permalink
meta, util: add unit tests (#10054)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored and zz-jason committed Apr 11, 2019
1 parent ee99570 commit e90f3b9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 73 deletions.
95 changes: 24 additions & 71 deletions meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ var (
mTablePrefix = "Table"
mTableIDPrefix = "TID"
mBootstrapKey = []byte("BootstrapKey")
mTableStatsPrefix = "TStats"
mSchemaDiffPrefix = "Diff"
)

Expand Down Expand Up @@ -128,16 +127,6 @@ func (m *Meta) dbKey(dbID int64) []byte {
return []byte(fmt.Sprintf("%s:%d", mDBPrefix, dbID))
}

func (m *Meta) parseDatabaseID(key string) (int64, error) {
seps := strings.Split(key, ":")
if len(seps) != 2 {
return 0, errors.Errorf("invalid db key %s", key)
}

n, err := strconv.ParseInt(seps[1], 10, 64)
return n, errors.Trace(err)
}

func (m *Meta) autoTableIDKey(tableID int64) []byte {
return []byte(fmt.Sprintf("%s:%d", mTableIDPrefix, tableID))
}
Expand All @@ -146,18 +135,8 @@ func (m *Meta) tableKey(tableID int64) []byte {
return []byte(fmt.Sprintf("%s:%d", mTablePrefix, tableID))
}

func (m *Meta) parseTableID(key string) (int64, error) {
seps := strings.Split(key, ":")
if len(seps) != 2 {
return 0, errors.Errorf("invalid table meta key %s", key)
}

n, err := strconv.ParseInt(seps[1], 10, 64)
return n, errors.Trace(err)
}

// GenAutoTableIDIDKeyValue generate meta key by dbID, tableID and corresponding value by autoID.
func (m *Meta) GenAutoTableIDIDKeyValue(dbID, tableID, autoID int64) (key, value []byte) {
// GenAutoTableIDKeyValue generates meta key by dbID, tableID and corresponding value by autoID.
func (m *Meta) GenAutoTableIDKeyValue(dbID, tableID, autoID int64) (key, value []byte) {
dbKey := m.dbKey(dbID)
autoTableIDKey := m.autoTableIDKey(tableID)
return m.txn.EncodeHashAutoIDKeyValue(dbKey, autoTableIDKey, autoID)
Expand Down Expand Up @@ -196,52 +175,34 @@ func (m *Meta) GenSchemaVersion() (int64, error) {

func (m *Meta) checkDBExists(dbKey []byte) error {
v, err := m.txn.HGet(mDBs, dbKey)
if err != nil {
return errors.Trace(err)
} else if v == nil {
return ErrDBNotExists
if err == nil && v == nil {
err = ErrDBNotExists
}

return nil
return errors.Trace(err)
}

func (m *Meta) checkDBNotExists(dbKey []byte) error {
v, err := m.txn.HGet(mDBs, dbKey)
if err != nil {
return errors.Trace(err)
if err == nil && v != nil {
err = ErrDBExists
}

if v != nil {
return ErrDBExists
}

return nil
return errors.Trace(err)
}

func (m *Meta) checkTableExists(dbKey []byte, tableKey []byte) error {
v, err := m.txn.HGet(dbKey, tableKey)
if err != nil {
return errors.Trace(err)
}

if v == nil {
return ErrTableNotExists
if err == nil && v == nil {
err = ErrTableNotExists
}

return nil
return errors.Trace(err)
}

func (m *Meta) checkTableNotExists(dbKey []byte, tableKey []byte) error {
v, err := m.txn.HGet(dbKey, tableKey)
if err != nil {
return errors.Trace(err)
}

if v != nil {
return ErrTableExists
if err == nil && v != nil {
err = ErrTableExists
}

return nil
return errors.Trace(err)
}

// CreateDatabase creates a database with db info.
Expand Down Expand Up @@ -484,10 +445,10 @@ var (

func (m *Meta) enQueueDDLJob(key []byte, job *model.Job) error {
b, err := job.Encode(true)
if err != nil {
return errors.Trace(err)
if err == nil {
err = m.txn.RPush(key, b)
}
return m.txn.RPush(key, b)
return errors.Trace(err)
}

// EnQueueDDLJob adds a DDL job to the list.
Expand Down Expand Up @@ -550,10 +511,10 @@ func (m *Meta) GetDDLJobByIdx(index int64, jobListKeys ...JobListKeyType) (*mode
// updateRawArgs is used to determine whether to update the raw args when encode the job.
func (m *Meta) updateDDLJob(index int64, job *model.Job, key []byte, updateRawArgs bool) error {
b, err := job.Encode(updateRawArgs)
if err != nil {
return errors.Trace(err)
if err == nil {
err = m.txn.LSet(key, index, b)
}
return m.txn.LSet(key, index, b)
return errors.Trace(err)
}

// UpdateDDLJob updates the DDL job with index.
Expand Down Expand Up @@ -640,11 +601,10 @@ func (m *Meta) reorgJobPhysicalTableID(id int64) []byte {

func (m *Meta) addHistoryDDLJob(key []byte, job *model.Job) error {
b, err := job.Encode(true)
if err != nil {
return errors.Trace(err)
if err == nil {
err = m.txn.HSet(key, m.jobIDKey(job.ID), b)
}

return m.txn.HSet(key, m.jobIDKey(job.ID), b)
return errors.Trace(err)
}

// AddHistoryDDLJob adds DDL job to history.
Expand Down Expand Up @@ -790,10 +750,6 @@ func (m *Meta) GetDDLReorgHandle(job *model.Job) (startHandle, endHandle, physic
return
}

func (m *Meta) tableStatsKey(tableID int64) []byte {
return []byte(fmt.Sprintf("%s:%d", mTableStatsPrefix, tableID))
}

func (m *Meta) schemaDiffKey(schemaVersion int64) []byte {
return []byte(fmt.Sprintf("%s:%d", mSchemaDiffPrefix, schemaVersion))
}
Expand All @@ -804,12 +760,9 @@ func (m *Meta) GetSchemaDiff(schemaVersion int64) (*model.SchemaDiff, error) {
startTime := time.Now()
data, err := m.txn.Get(diffKey)
metrics.MetaHistogram.WithLabelValues(metrics.GetSchemaDiff, metrics.RetLabel(err)).Observe(time.Since(startTime).Seconds())
if err != nil {
if err != nil || len(data) == 0 {
return nil, errors.Trace(err)
}
if len(data) == 0 {
return nil, nil
}
diff := &model.SchemaDiff{}
err = json.Unmarshal(data, diff)
return diff, errors.Trace(err)
Expand Down
52 changes: 51 additions & 1 deletion meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package meta_test
import (
"context"
"math"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -44,7 +45,6 @@ func (s *testSuite) TestMeta(c *C) {

txn, err := store.Begin()
c.Assert(err, IsNil)

defer txn.Rollback()

t := meta.NewMeta(txn)
Expand Down Expand Up @@ -179,6 +179,20 @@ func (s *testSuite) TestMeta(c *C) {
currentDBID = nonExistentID
_, err = t.GenAutoTableID(currentDBID, tid, 10)
c.Assert(err, NotNil)
// Test case for CreateTableAndSetAutoID.
tbInfo3 := &model.TableInfo{
ID: 3,
Name: model.NewCIStr("tbl3"),
}
err = t.CreateTableAndSetAutoID(1, tbInfo3, 123)
c.Assert(err, IsNil)
id, err := t.GetAutoTableID(1, tbInfo3.ID)
c.Assert(err, IsNil)
c.Assert(id, Equals, int64(123))
// Test case for GenAutoTableIDKeyValue.
key, val := t.GenAutoTableIDKeyValue(1, tbInfo3.ID, 1234)
c.Assert(val, DeepEquals, []byte(strconv.FormatInt(1234, 10)))
c.Assert(key, DeepEquals, []byte{0x6d, 0x44, 0x42, 0x3a, 0x31, 0x0, 0x0, 0x0, 0x0, 0xfb, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x68, 0x54, 0x49, 0x44, 0x3a, 0x33, 0x0, 0x0, 0x0, 0xfc})

err = t.DropDatabase(1)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -313,6 +327,10 @@ func (s *testSuite) TestDDL(c *C) {
c.Assert(j, Equals, int64(math.MaxInt64))
c.Assert(k, Equals, int64(0))

// Test GetDDLReorgHandle failed.
_, _, _, err = t.GetDDLReorgHandle(job)
c.Assert(err, IsNil)

v, err = t.DeQueueDDLJob()
c.Assert(err, IsNil)
c.Assert(v, DeepEquals, job)
Expand All @@ -323,6 +341,13 @@ func (s *testSuite) TestDDL(c *C) {
c.Assert(err, IsNil)
c.Assert(v, DeepEquals, job)

// Add multiple history jobs.
historyJob1 := &model.Job{ID: 1234}
err = t.AddHistoryDDLJob(historyJob1)
c.Assert(err, IsNil)
historyJob2 := &model.Job{ID: 123}
err = t.AddHistoryDDLJob(historyJob2)
c.Assert(err, IsNil)
all, err := t.GetAllHistoryDDLJobs()
c.Assert(err, IsNil)
var lastID int64
Expand All @@ -344,4 +369,29 @@ func (s *testSuite) TestDDL(c *C) {

err = txn.Commit(context.Background())
c.Assert(err, IsNil)

// Test for add index job.
txn1, err := store.Begin()
c.Assert(err, IsNil)
defer txn1.Rollback()

m := meta.NewMeta(txn1, meta.AddIndexJobListKey)
err = m.EnQueueDDLJob(job)
c.Assert(err, IsNil)
job.ID = 123
err = m.UpdateDDLJob(0, job, true, meta.AddIndexJobListKey)
c.Assert(err, IsNil)
v, err = m.GetDDLJobByIdx(0, meta.AddIndexJobListKey)
c.Assert(err, IsNil)
c.Assert(v, DeepEquals, job)
l, err := m.DDLJobQueueLen(meta.AddIndexJobListKey)
c.Assert(err, IsNil)
c.Assert(l, Equals, int64(1))
jobs, err = m.GetAllDDLJobsInQueue(meta.AddIndexJobListKey)
c.Assert(err, IsNil)
expectJobs = []*model.Job{job}
c.Assert(jobs, DeepEquals, expectJobs)

err = txn1.Commit(context.Background())
c.Assert(err, IsNil)
}
2 changes: 1 addition & 1 deletion util/kvencoder/kv_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (e *kvEncoder) EncodePrepareStmt(tableID int64, stmtID uint32, param ...int
func (e *kvEncoder) EncodeMetaAutoID(dbID, tableID, autoID int64) (KvPair, error) {
mockTxn := kv.NewMockTxn()
m := meta.NewMeta(mockTxn)
k, v := m.GenAutoTableIDIDKeyValue(dbID, tableID, autoID)
k, v := m.GenAutoTableIDKeyValue(dbID, tableID, autoID)
return KvPair{Key: k, Val: v}, nil
}

Expand Down

0 comments on commit e90f3b9

Please sign in to comment.