本文整理匯總了Golang中github.com/pingcap/tidb/model.Job.State方法的典型用法代碼示例。如果您正苦於以下問題:Golang Job.State方法的具體用法?Golang Job.State怎麽用?Golang Job.State使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pingcap/tidb/model.Job
的用法示例。
在下文中一共展示了Job.State方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: onDropForeignKey
func (d *ddl) onDropForeignKey(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tblInfo, err := d.getTableInfo(t, job)
if err != nil {
return errors.Trace(err)
}
var (
fkName model.CIStr
found bool
fkInfo model.FKInfo
)
err = job.DecodeArgs(&fkName)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
for _, fk := range tblInfo.ForeignKeys {
if fk.Name.L == fkName.L {
found = true
fkInfo = *fk
}
}
if !found {
return infoschema.ErrForeignKeyNotExists.Gen("foreign key doesn't exist", fkName)
}
nfks := tblInfo.ForeignKeys[:0]
for _, fk := range tblInfo.ForeignKeys {
if fk.Name.L != fkName.L {
nfks = append(nfks, fk)
}
}
tblInfo.ForeignKeys = nfks
_, err = t.GenSchemaVersion()
if err != nil {
return errors.Trace(err)
}
switch fkInfo.State {
case model.StatePublic:
// We just support record the foreign key, so we just make it none.
// public -> none
job.SchemaState = model.StateNone
fkInfo.State = model.StateNone
err = t.UpdateTable(schemaID, tblInfo)
if err != nil {
return errors.Trace(err)
}
// finish this job
job.State = model.JobDone
return nil
default:
return ErrInvalidForeignKeyState.Gen("invalid fk state %v", fkInfo.State)
}
}
示例2: onModifyColumn
func (d *ddl) onModifyColumn(t *meta.Meta, job *model.Job) error {
tblInfo, err := d.getTableInfo(t, job)
if err != nil {
return errors.Trace(err)
}
newCol := &model.ColumnInfo{}
oldColName := &model.CIStr{}
err = job.DecodeArgs(newCol, oldColName)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
oldCol := findCol(tblInfo.Columns, oldColName.L)
if oldCol == nil || oldCol.State != model.StatePublic {
job.State = model.JobCancelled
return infoschema.ErrColumnNotExists.GenByArgs(newCol.Name, tblInfo.Name)
}
*oldCol = *newCol
err = t.UpdateTable(job.SchemaID, tblInfo)
if err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
job.SchemaState = model.StatePublic
job.State = model.JobDone
addTableHistoryInfo(job, ver, tblInfo)
return nil
}
示例3: delReorgSchema
func (d *ddl) delReorgSchema(t *meta.Meta, job *model.Job) error {
dbInfo := &model.DBInfo{}
if err := job.DecodeArgs(dbInfo); err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
tables, err := t.ListTables(dbInfo.ID)
if terror.ErrorEqual(meta.ErrDBNotExists, err) {
job.State = model.JobDone
return nil
}
if err != nil {
return errors.Trace(err)
}
if err = d.dropSchemaData(dbInfo, tables); err != nil {
return errors.Trace(err)
}
// finish this background job
job.SchemaState = model.StateNone
job.State = model.JobDone
return nil
}
示例4: delReorgTable
func (d *ddl) delReorgTable(t *meta.Meta, job *model.Job) error {
tblInfo := &model.TableInfo{}
err := job.DecodeArgs(tblInfo)
if err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
tblInfo.State = model.StateDeleteReorganization
tbl, err := d.getTable(job.SchemaID, tblInfo)
if err != nil {
return errors.Trace(err)
}
err = d.dropTableData(tbl)
if err != nil {
return errors.Trace(err)
}
// finish this background job
job.SchemaState = model.StateNone
job.State = model.JobDone
return nil
}
示例5: runDDLJob
// runDDLJob runs a DDL job.
func (d *ddl) runDDLJob(t *meta.Meta, job *model.Job) {
log.Infof("[ddl] run DDL job %s", job)
if job.IsFinished() {
return
}
if job.State != model.JobRollback {
job.State = model.JobRunning
}
var err error
switch job.Type {
case model.ActionCreateSchema:
err = d.onCreateSchema(t, job)
case model.ActionDropSchema:
err = d.onDropSchema(t, job)
case model.ActionCreateTable:
err = d.onCreateTable(t, job)
case model.ActionDropTable:
err = d.onDropTable(t, job)
case model.ActionAddColumn:
err = d.onAddColumn(t, job)
case model.ActionDropColumn:
err = d.onDropColumn(t, job)
case model.ActionModifyColumn:
err = d.onModifyColumn(t, job)
case model.ActionAddIndex:
err = d.onCreateIndex(t, job)
case model.ActionDropIndex:
err = d.onDropIndex(t, job)
case model.ActionAddForeignKey:
err = d.onCreateForeignKey(t, job)
case model.ActionDropForeignKey:
err = d.onDropForeignKey(t, job)
case model.ActionTruncateTable:
err = d.onTruncateTable(t, job)
default:
// Invalid job, cancel it.
job.State = model.JobCancelled
err = errInvalidDDLJob.Gen("invalid ddl job %v", job)
}
// Save errors in job, so that others can know errors happened.
if err != nil {
// If job is not cancelled, we should log this error.
if job.State != model.JobCancelled {
log.Errorf("[ddl] run ddl job err %v", errors.ErrorStack(err))
} else {
log.Infof("[ddl] the job is normal to cancel because %v", errors.ErrorStack(err))
}
job.Error = toTError(err)
job.ErrorCount++
}
}
示例6: onCreateSchema
func (d *ddl) onCreateSchema(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
var name model.CIStr
if err := job.DecodeArgs(&name); err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
dbInfo := &model.DBInfo{
ID: schemaID,
Name: name,
State: model.StateNone,
}
dbs, err := t.ListDatabases()
if err != nil {
return errors.Trace(err)
}
for _, db := range dbs {
if db.Name.L == name.L {
if db.ID != schemaID {
// database exists, can't create, we should cancel this job now.
job.State = model.JobCancelled
return errors.Trace(ErrExists)
}
dbInfo = db
}
}
_, err = t.GenSchemaVersion()
if err != nil {
return errors.Trace(err)
}
switch dbInfo.State {
case model.StateNone:
// none -> public
job.SchemaState = model.StatePublic
dbInfo.State = model.StatePublic
err = t.CreateDatabase(dbInfo)
if err != nil {
return errors.Trace(err)
}
// finish this job
job.State = model.JobDone
return nil
default:
// we can't enter here.
return errors.Errorf("invalid db state %v", dbInfo.State)
}
}
示例7: onCreateTable
func (d *ddl) onCreateTable(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tbInfo := &model.TableInfo{}
if err := job.DecodeArgs(tbInfo); err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
tbInfo.State = model.StateNone
tables, err := t.ListTables(schemaID)
if terror.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseNotExists)
} else if err != nil {
return errors.Trace(err)
}
for _, tbl := range tables {
if tbl.Name.L == tbInfo.Name.L {
if tbl.ID != tbInfo.ID {
// table exists, can't create, we should cancel this job now.
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrTableExists)
}
tbInfo = tbl
}
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
switch tbInfo.State {
case model.StateNone:
// none -> public
job.SchemaState = model.StatePublic
tbInfo.State = model.StatePublic
err = t.CreateTable(schemaID, tbInfo)
if err != nil {
return errors.Trace(err)
}
// finish this job
job.State = model.JobDone
addTableHistoryInfo(job, ver, tbInfo)
return nil
default:
return ErrInvalidTableState.Gen("invalid table state %v", tbInfo.State)
}
}
示例8: onDropTable
func (d *ddl) onDropTable(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tableID := job.TableID
// Check this table's database.
tblInfo, err := t.GetTable(schemaID, tableID)
if terror.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseNotExists)
} else if err != nil {
return errors.Trace(err)
}
// Check the table.
if tblInfo == nil {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrTableNotExists)
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
switch tblInfo.State {
case model.StatePublic:
// public -> write only
job.SchemaState = model.StateWriteOnly
tblInfo.State = model.StateWriteOnly
err = t.UpdateTable(schemaID, tblInfo)
case model.StateWriteOnly:
// write only -> delete only
job.SchemaState = model.StateDeleteOnly
tblInfo.State = model.StateDeleteOnly
err = t.UpdateTable(schemaID, tblInfo)
case model.StateDeleteOnly:
tblInfo.State = model.StateNone
err = t.UpdateTable(schemaID, tblInfo)
if err = t.DropTable(job.SchemaID, job.TableID); err != nil {
break
}
// Finish this job.
job.State = model.JobDone
job.SchemaState = model.StateNone
addTableHistoryInfo(job, ver, tblInfo)
startKey := tablecodec.EncodeTablePrefix(tableID)
job.Args = append(job.Args, startKey)
default:
err = ErrInvalidTableState.Gen("invalid table state %v", tblInfo.State)
}
return errors.Trace(err)
}
示例9: onDropSchema
func (d *ddl) onDropSchema(t *meta.Meta, job *model.Job) error {
dbInfo, err := t.GetDatabase(job.SchemaID)
if err != nil {
return errors.Trace(err)
}
if dbInfo == nil {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseDropExists)
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
switch dbInfo.State {
case model.StatePublic:
// public -> write only
job.SchemaState = model.StateWriteOnly
dbInfo.State = model.StateWriteOnly
err = t.UpdateDatabase(dbInfo)
case model.StateWriteOnly:
// write only -> delete only
job.SchemaState = model.StateDeleteOnly
dbInfo.State = model.StateDeleteOnly
err = t.UpdateDatabase(dbInfo)
case model.StateDeleteOnly:
dbInfo.State = model.StateNone
tables, err := t.ListTables(job.SchemaID)
if err != nil {
return errors.Trace(err)
}
err = t.UpdateDatabase(dbInfo)
if err = t.DropDatabase(dbInfo.ID); err != nil {
break
}
// finish this job
addDBHistoryInfo(job, ver, dbInfo)
if len(tables) > 0 {
job.Args = append(job.Args, getIDs(tables))
}
job.State = model.JobDone
job.SchemaState = model.StateNone
default:
// we can't enter here.
err = errors.Errorf("invalid db state %v", dbInfo.State)
}
return errors.Trace(err)
}
示例10: onCreateSchema
func (d *ddl) onCreateSchema(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
dbInfo := &model.DBInfo{}
if err := job.DecodeArgs(dbInfo); err != nil {
// arg error, cancel this job.
job.State = model.JobCancelled
return errors.Trace(err)
}
dbInfo.ID = schemaID
dbInfo.State = model.StateNone
dbs, err := t.ListDatabases()
if err != nil {
return errors.Trace(err)
}
for _, db := range dbs {
if db.Name.L == dbInfo.Name.L {
if db.ID != schemaID {
// database exists, can't create, we should cancel this job now.
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseExists)
}
dbInfo = db
}
}
ver, err := updateSchemaVersion(t, job)
if err != nil {
return errors.Trace(err)
}
switch dbInfo.State {
case model.StateNone:
// none -> public
job.SchemaState = model.StatePublic
dbInfo.State = model.StatePublic
err = t.CreateDatabase(dbInfo)
if err != nil {
return errors.Trace(err)
}
// finish this job
job.State = model.JobDone
addDBHistoryInfo(job, ver, dbInfo)
return nil
default:
// we can't enter here.
return errors.Errorf("invalid db state %v", dbInfo.State)
}
}
示例11: onDropTable
func (d *ddl) onDropTable(t *meta.Meta, job *model.Job) error {
schemaID := job.SchemaID
tableID := job.TableID
tblInfo, err := t.GetTable(schemaID, tableID)
if terror.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseNotExists)
} else if err != nil {
return errors.Trace(err)
}
if tblInfo == nil {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrTableNotExists)
}
_, err = t.GenSchemaVersion()
if err != nil {
return errors.Trace(err)
}
switch tblInfo.State {
case model.StatePublic:
// public -> write only
job.SchemaState = model.StateWriteOnly
tblInfo.State = model.StateWriteOnly
err = t.UpdateTable(schemaID, tblInfo)
case model.StateWriteOnly:
// write only -> delete only
job.SchemaState = model.StateDeleteOnly
tblInfo.State = model.StateDeleteOnly
err = t.UpdateTable(schemaID, tblInfo)
case model.StateDeleteOnly:
tblInfo.State = model.StateNone
err = t.UpdateTable(schemaID, tblInfo)
if err = t.DropTable(job.SchemaID, job.TableID); err != nil {
break
}
// finish this job
job.Args = []interface{}{tblInfo}
job.State = model.JobDone
job.SchemaState = model.StateNone
default:
err = ErrInvalidTableState.Gen("invalid table state %v", tblInfo.State)
}
return errors.Trace(err)
}
示例12: runDDLJob
func (d *ddl) runDDLJob(t *meta.Meta, job *model.Job) {
if job.IsFinished() {
return
}
job.State = model.JobRunning
var err error
switch job.Type {
case model.ActionCreateSchema:
err = d.onCreateSchema(t, job)
case model.ActionDropSchema:
err = d.onDropSchema(t, job)
case model.ActionCreateTable:
err = d.onCreateTable(t, job)
case model.ActionDropTable:
err = d.onDropTable(t, job)
case model.ActionAddColumn:
err = d.onAddColumn(t, job)
case model.ActionDropColumn:
err = d.onDropColumn(t, job)
case model.ActionAddIndex:
err = d.onCreateIndex(t, job)
case model.ActionDropIndex:
err = d.onDropIndex(t, job)
case model.ActionAddForeignKey:
err = d.onCreateForeignKey(t, job)
case model.ActionDropForeignKey:
err = d.onDropForeignKey(t, job)
default:
// invalid job, cancel it.
job.State = model.JobCancelled
err = errInvalidDDLJob.Gen("invalid ddl job %v", job)
}
// saves error in job, so that others can know error happens.
if err != nil {
// if job is not cancelled, we should log this error.
if job.State != model.JobCancelled {
log.Errorf("run ddl job err %v", errors.ErrorStack(err))
}
job.Error = err.Error()
job.ErrorCount++
}
}
示例13: onDropSchema
func (d *ddl) onDropSchema(t *meta.Meta, job *model.Job) error {
dbInfo, err := t.GetDatabase(job.SchemaID)
if err != nil {
return errors.Trace(err)
}
if dbInfo == nil {
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseNotExists)
}
_, err = t.GenSchemaVersion()
if err != nil {
return errors.Trace(err)
}
switch dbInfo.State {
case model.StatePublic:
// public -> write only
job.SchemaState = model.StateWriteOnly
dbInfo.State = model.StateWriteOnly
err = t.UpdateDatabase(dbInfo)
case model.StateWriteOnly:
// write only -> delete only
job.SchemaState = model.StateDeleteOnly
dbInfo.State = model.StateDeleteOnly
err = t.UpdateDatabase(dbInfo)
case model.StateDeleteOnly:
dbInfo.State = model.StateDeleteReorganization
err = t.UpdateDatabase(dbInfo)
if err = t.DropDatabase(dbInfo.ID); err != nil {
break
}
// finish this job
job.Args = []interface{}{dbInfo}
job.State = model.JobDone
job.SchemaState = model.StateNone
default:
// we can't enter here.
err = errors.Errorf("invalid db state %v", dbInfo.State)
}
return errors.Trace(err)
}
示例14: delReorgTable
func (d *ddl) delReorgTable(t *meta.Meta, job *model.Job) error {
var startKey kv.Key
if err := job.DecodeArgs(&startKey); err != nil {
job.State = model.JobCancelled
return errors.Trace(err)
}
limit := reorgTableDeleteLimit
delCount, err := d.dropTableData(startKey, job, limit)
if err != nil {
return errors.Trace(err)
}
// Finish this background job.
if delCount < limit {
job.SchemaState = model.StateNone
job.State = model.JobDone
}
return nil
}
示例15: getTableInfo
func (d *ddl) getTableInfo(t *meta.Meta, job *model.Job) (*model.TableInfo, error) {
schemaID := job.SchemaID
tableID := job.TableID
tblInfo, err := t.GetTable(schemaID, tableID)
if terror.ErrorEqual(err, meta.ErrDBNotExists) {
job.State = model.JobCancelled
return nil, errors.Trace(infoschema.ErrDatabaseNotExists)
} else if err != nil {
return nil, errors.Trace(err)
} else if tblInfo == nil {
job.State = model.JobCancelled
return nil, errors.Trace(infoschema.ErrTableNotExists)
}
if tblInfo.State != model.StatePublic {
job.State = model.JobCancelled
return nil, ErrInvalidTableState.Gen("table %s is not in public, but %s", tblInfo.Name.L, tblInfo.State)
}
return tblInfo, nil
}