Esempio n. 1
0
func (t *MigrateTask) migrateSingleSlot(slotId int, to int) error {
	// set slot status
	s, err := models.GetSlot(t.coordConn, t.productName, slotId)
	if err != nil {
		log.Error(err)
		return err
	}
	if s.State.Status != models.SLOT_STATUS_ONLINE && s.State.Status != models.SLOT_STATUS_MIGRATE {
		log.Warning("status is not online && migrate", s)
		return nil
	}

	from := s.GroupId
	if s.State.Status == models.SLOT_STATUS_MIGRATE {
		from = s.State.MigrateStatus.From
	}

	// cannot migrate to itself, just ignore
	if from == to {
		log.Warning("from == to, ignore", s)
		return nil
	}

	// make sure from group & target group exists
	exists, err := models.GroupExists(t.coordConn, t.productName, from)
	if err != nil {
		return errors.Trace(err)
	}
	if !exists {
		log.Errorf("src group %d not exist when migrate from %d to %d", from, from, to)
		return errors.NotFoundf("group %d", from)
	}

	exists, err = models.GroupExists(t.coordConn, t.productName, to)
	if err != nil {
		return errors.Trace(err)
	}
	if !exists {
		return errors.NotFoundf("group %d", to)
	}

	// modify slot status
	if err := s.SetMigrateStatus(t.coordConn, from, to); err != nil {
		log.Error(err)
		return err
	}

	err = t.slotMigrator.Migrate(s, from, to, t, func(p SlotMigrateProgress) {
		// on migrate slot progress
		if p.Remain%500 == 0 {
			log.Info(p)
		}
	})
	if err != nil {
		log.Error(err)
		return err
	}

	// migrate done, change slot status back
	s.State.Status = models.SLOT_STATUS_ONLINE
	s.State.MigrateStatus.From = models.INVALID_ID
	s.State.MigrateStatus.To = models.INVALID_ID
	if err := s.Update(t.coordConn); err != nil {
		log.Error(err)
		return err
	}

	return nil
}
Esempio n. 2
0
func (t *MigrateTask) migrateSingleSlot(slotId int, to int) error {
	// set slot status
	s, err := models.GetSlot(t.coordConn, t.productName, slotId)
	if err != nil {
		log.Error(err)
		return errors.Trace(err)
	}
	if s.State.Status != models.SLOT_STATUS_ONLINE && s.State.Status != models.SLOT_STATUS_MIGRATE {
		log.Warning("status is not online && migrate", s)
		return nil
	}

	from := s.GroupId
	if s.State.Status == models.SLOT_STATUS_MIGRATE {
		from = s.State.MigrateStatus.From
	}

	// cannot migrate to itself, just ignore
	if from == to {
		log.Warning("from == to, ignore", s)
		return nil
	}

	// make sure from group & target group exists
	exists, err := models.GroupExists(t.coordConn, t.productName, from)
	if err != nil {
		return errors.Trace(err)
	}
	if !exists {
		log.Errorf("src group %d not exist when migrate from %d to %d", from, from, to)
		return errors.NotFoundf("group %d", from)
	}

	exists, err = models.GroupExists(t.coordConn, t.productName, to)
	if err != nil {
		return errors.Trace(err)
	}
	if !exists {
		return errors.NotFoundf("group %d", to)
	}

	/***************************************************************/
	// make sure to group has at least one master server ---zjp 20160711

	log.Warning("Get group %d has no master master not found")
	groupTo, err := models.GetGroup(t.coordConn, t.productName, to)
	if err != nil {
		log.Warning(err)
		return errors.Trace(err)
	}
	log.Warning("Get to master ")
	toMaster, err := groupTo.Master(t.coordConn)
	if err != nil {
		log.Warning(err)
		return errors.Trace(err)
	}

	if toMaster == nil {
		log.Warning("to master not found")
		return errors.NotFoundf("group %d has no master", to)
	}
	log.Warning("Get to master  != nil")

	var perr error
	for i := 0; i < haMaxRetryNum; i++ {
		if perr = utils.Ping(toMaster.Addr, globalEnv.StoreAuth()); perr == nil {
			break
		}

		perr = errors.Trace(perr)
		time.Sleep(time.Duration(haRetryDelay) * time.Second)
	}

	if perr != nil {
		log.Warning(perr)
		log.Warning("To master is not online")
		return errors.Trace(perr)
	}
	groupFrom, err := models.GetGroup(t.coordConn, t.productName, from)
	if err != nil {
		log.Warning(err)
		return errors.Trace(err)
	}
	log.Warning("Get from master ")
	fromMaster, err := groupFrom.Master(t.coordConn)
	if err != nil {
		log.Warning(err)
		return errors.Trace(err)
	}

	if fromMaster == nil {
		log.Warning(" from master not found")
		return errors.NotFoundf("group %d has no master", to)
	}
	log.Warning("Get from master  != nil")

	var Fmerr error
	for i := 0; i < haMaxRetryNum; i++ {
		if Fmerr = utils.Ping(fromMaster.Addr, globalEnv.StoreAuth()); Fmerr == nil {
			break
		}

		Fmerr = errors.Trace(Fmerr)
		time.Sleep(time.Duration(haRetryDelay) * time.Second)
	}

	if Fmerr != nil {
		log.Warning(Fmerr)
		log.Warning("From master is not online")
		return errors.Trace(Fmerr)
	}
	groupFrom, gerr := models.GetGroup(t.coordConn, t.productName, from)
	if gerr != nil {
		log.Warning(gerr)
		return errors.Trace(gerr)
	}

	/***************************************************************/

	// modify slot status
	if err := s.SetMigrateStatus(t.coordConn, from, to); err != nil {
		log.Error(err)
		return errors.Trace(err)
	}

	err = t.slotMigrator.Migrate(s, from, to, t, func(p SlotMigrateProgress) {
		// on migrate slot progress
		if p.Remain%500 == 0 {
			log.Info(p)
		}
	})
	if err != nil {
		log.Error(err)
		return errors.Trace(err)
	}

	// migrate done, change slot status back
	s.State.Status = models.SLOT_STATUS_ONLINE
	s.State.MigrateStatus.From = models.INVALID_ID
	s.State.MigrateStatus.To = models.INVALID_ID
	if err := s.Update(t.coordConn); err != nil {
		log.Error(err)
		return errors.Trace(err)
	}

	return nil
}