retry migration when key is in migrating

This commit is contained in:
siddontang 2015-03-09 10:00:29 +08:00
parent 4eeaffbc00
commit ce8e0d4fdc
1 changed files with 50 additions and 14 deletions

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/siddontang/go/hack" "github.com/siddontang/go/hack"
"github.com/siddontang/go/log"
goledis "github.com/siddontang/ledisdb/client/goledis" goledis "github.com/siddontang/ledisdb/client/goledis"
"github.com/siddontang/ledisdb/ledis" "github.com/siddontang/ledisdb/ledis"
) )
@ -110,6 +111,29 @@ func restoreCommand(c *client) error {
return nil return nil
} }
// maybe only used in xcodis for redis data port
func xrestoreCommand(c *client) error {
args := c.args
if len(args) != 4 {
return ErrCmdParams
}
// tp := strings.ToUpper(string(args[2]))
key := args[1]
ttl, err := ledis.StrInt64(args[2], nil)
if err != nil {
return err
}
data := args[3]
if err = c.db.Restore(key, ttl, data); err != nil {
return err
} else {
c.resp.writeStatus(OK)
}
return nil
}
func xdump(db *ledis.DB, tp string, key []byte) ([]byte, error) { func xdump(db *ledis.DB, tp string, key []byte) ([]byte, error) {
var err error var err error
var data []byte var data []byte
@ -393,19 +417,30 @@ func xmigrateCommand(c *client) error {
} }
defer conn.Close() defer conn.Close()
if tp == "ALL" { // if key is in migrating, we will wait 500ms and retry again
// if tp is ALL, we will migrate the key in all types for i := 0; i < 10; i++ {
// this feature is useful for xcodis RESTORE or other commands that we don't know the data type exactly if tp == "ALL" {
err = migrateAllTypeKeys(c, conn, key, timeout) // if tp is ALL, we will migrate the key in all types
} else { // this feature is useful for xcodis RESTORE or other commands that we don't know the data type exactly
err = migrateKey(c, conn, tp, key, timeout) err = migrateAllTypeKeys(c, conn, key, timeout)
if err != nil { } else {
if err == errNoKey { err = migrateKey(c, conn, tp, key, timeout)
c.resp.writeStatus(NOKEY) }
return nil
} else { if err != errKeyInMigrating {
return err break
} } else {
log.Infof("%s key %s is in migrating, wait 500ms and retry", tp, key)
time.Sleep(500 * time.Millisecond)
}
}
if err != nil {
if err == errNoKey {
c.resp.writeStatus(NOKEY)
return nil
} else {
return err
} }
} }
@ -475,7 +510,7 @@ func migrateAllTypeKeys(c *client, conn *goledis.Conn, key []byte, timeout int64
for _, tp := range TypeNames { for _, tp := range TypeNames {
err := migrateKey(c, conn, tp, key, timeout) err := migrateKey(c, conn, tp, key, timeout)
if err != nil { if err != nil {
if err == errNoKey || err == errKeyInMigrating { if err == errNoKey {
continue continue
} else { } else {
return err return err
@ -493,6 +528,7 @@ func init() {
register("sdump", sdumpCommand) register("sdump", sdumpCommand)
register("zdump", zdumpCommand) register("zdump", zdumpCommand)
register("restore", restoreCommand) register("restore", restoreCommand)
register("xrestore", xrestoreCommand)
register("xdump", xdumpCommand) register("xdump", xdumpCommand)
register("xmigrate", xmigrateCommand) register("xmigrate", xmigrateCommand)
register("xmigratedb", xmigratedbCommand) register("xmigratedb", xmigratedbCommand)