Пример #1
0
// Internal control command.
// Migrate moves one slot data to another server on the fly.
func (c *CtrlContext) Migrate(host string, slotId uint16) error {
	call := c.cli.newCall(proto.CmdMigrate, nil)
	if call.err != nil {
		return call.err
	}

	var p ctrl.PkgMigrate
	p.ClientReq = true
	p.MasterAddr = host
	p.SlotId = slotId

	pkg, err := ctrl.Encode(call.cmd, c.dbId, call.seq, &p)
	if err != nil {
		c.cli.errCall(call, err)
		return err
	}

	call.pkg = pkg
	c.cli.sending <- call

	r, err := (<-call.Done).Reply()
	if err != nil {
		return err
	}

	t := r.(*ctrl.PkgMigrate)
	if t.ErrMsg != "" {
		return errors.New(t.ErrMsg)
	}
	return nil
}
Пример #2
0
func (srv *Server) replyMigrate(req *Request, msg string) {
	ps := ctrl.PkgMigrate{}
	ps.ErrMsg = msg
	pkg, err := ctrl.Encode(req.Cmd, req.DbId, req.Seq, &ps)
	if err == nil {
		srv.sendResp(false, req, pkg)
	}
}
Пример #3
0
func (slv *slave) SendSlaveOfToMaster() error {
	slv.mtx.Lock()
	var mi = slv.mi
	var cli = slv.cli
	slv.mtx.Unlock()

	if cli == nil {
		return nil
	}

	var err error
	var pkg []byte
	if mi.Migration {
		var p ctrl.PkgMigrate
		p.ClientReq = false
		p.MasterAddr = mi.MasterAddr
		p.SlaveAddr = mi.SlaveAddr
		p.SlotId = mi.SlotId

		pkg, err = ctrl.Encode(proto.CmdMigrate, 0, 0, &p)
		if err != nil {
			return err
		}
	} else {
		lastSeq, valid := slv.bin.GetMasterSeq()
		if !valid {
			slv.mc.SetStatus(ctrl.SlaveNeedClear)
			// Any better solution?
			log.Fatalf("Slave lastSeq %d is out of sync, please clear old data! "+
				"(Restart may fix this issue)", lastSeq)
		}

		var p ctrl.PkgSlaveOf
		p.ClientReq = false
		p.MasterAddr = mi.MasterAddr
		p.SlaveAddr = mi.SlaveAddr
		p.LastSeq = lastSeq
		log.Printf("Connect to master %s with lastSeq %d\n",
			mi.MasterAddr, p.LastSeq)

		pkg, err = ctrl.Encode(proto.CmdSlaveOf, 0, 0, &p)
		if err != nil {
			return err
		}
	}

	cli.AddResp(pkg)
	return slv.mc.SetStatus(ctrl.SlaveFullSync)
}
Пример #4
0
func (srv *Server) migrate(req *Request) {
	var cliType uint32 = ClientTypeNormal
	if req.Cli != nil {
		cliType = req.Cli.ClientType()
	}

	var p ctrl.PkgMigrate
	var err = ctrl.Decode(req.Pkg, nil, &p)
	if err == nil && !p.ClientReq {
		srv.newMigMaster(req, &p)
		return
	}

	switch cliType {
	case ClientTypeNormal:
		if err != nil {
			log.Printf("Failed to Decode pkg: %s\n", err)
			srv.replyMigrate(req, fmt.Sprintf("decode failed(%s)", err))
			return
		}

		if !req.Cli.IsAuth(proto.AdminDbId) {
			log.Printf("Not authorized!\n")
			srv.replyMigrate(req, "no priviledge")
			return
		}

		if len(p.SlaveAddr) == 0 {
			p.SlaveAddr = req.Cli.LocalAddr().String()
		}
		if sameAddress(p.MasterAddr, p.SlaveAddr, req) {
			log.Printf("Master and slave addresses are the same!\n")
			srv.replyMigrate(req, "master and slave addresses are the same")
			return
		}

		err = srv.mc.SetMigration(p.MasterAddr, p.SlaveAddr, p.SlotId)
		if err != nil {
			log.Printf("Failed to update migration config: %s\n", err)
			srv.replyMigrate(req,
				fmt.Sprintf("update migration config failed(%s)", err))
			return
		}

		srv.rwMtx.Lock()
		slv := srv.slv
		srv.slv = nil
		srv.rwMtx.Unlock()

		if len(p.MasterAddr) > 0 {
			if slv != nil {
				slv.Close()
			}
			if srv.tbl.HasSlotData(p.SlotId) {
				err = srv.mc.SetStatus(ctrl.SlaveNeedClear)
				if err != nil {
					log.Printf("Failed to set migration status: %s\n", err)
					srv.replyMigrate(req,
						fmt.Sprintf("failed to set migration status(%s)", err))
					return
				}
			} else {
				srv.connectToMaster(srv.mc)
			}
		} else {
			if slv != nil {
				go slv.DelayClose()
			}
			srv.bin.AsMaster()
		}

		srv.replyMigrate(req, "") // Success
	case ClientTypeSlave:
		fallthrough
	case ClientTypeMaster:
		if err != nil {
			log.Printf("Failed to Decode pkg: %s\n", err)
		} else {
			log.Println("Invalid client type %d for Migrate command, close now!",
				cliType)
		}
		req.Cli.Close()
	}
}