func (self *ClientPriorityList) SelectWrite() *ClientWeightWrapper {
	if self.num == 1 {
		logger.LogDebug("select writer ", self.clients[0].GetClient())
		return self.clients[0]
	}
	for {
		self.i = (self.i + 1) % self.num
		if self.i == 0 {
			self.cw -= self.gcd
			if self.cw <= 0 {
				self.cw = self.w_max
				if self.cw == 0 {
					return nil
				}
			}
		}

		if weight := self.clients[self.i].w; weight >= self.cw {
			logger.LogDebug("select writer ", self.clients[self.i].GetClient())
			return self.clients[self.i]
		}
	}
	if self.lowerPriority != nil {
		return self.lowerPriority.SelectWrite()
	}
	return nil
}
Example #2
0
func (self *Selector) EndBalance() {
	self.lock.Lock()
	defer self.lock.Unlock()
	self.scaling = false
	self.runtime = self.scale
	logger.LogDebug("balance finish")
}
Example #3
0
// 有可能多写,所以这里group返回的是数组,那么上层应该双写
func (self *Selector) Shard(key string, isWrite bool) []*datasource.Group {
	data := []byte(key)
	ds := make([]*datasource.Group, 1)
	if !isWrite || !self.scaling {
		i := int(crc32.ChecksumIEEE(data)) % len(self.runtime)
		ds[0] = self.runtime[i]
		logger.LogDebug("selector runtime", self.runtime[i], "at", i)
	} else {
		oldIdx := int(crc32.ChecksumIEEE(data)) % len(self.runtime)
		newIdx := int(crc32.ChecksumIEEE(data)) % len(self.scale)
		// 实际上这部分扩容期间的写入是冗余的
		ds[0] = self.runtime[oldIdx]
		logger.LogDebug("selector runtime", self.runtime[oldIdx], "at", oldIdx)
		if oldIdx != newIdx {
			ds = append(ds, self.scale[newIdx])
			logger.LogDebug("selector scale ", self.scale[newIdx], "at", newIdx)
		}
	}
	return ds
}
Example #4
0
func (self *DBAdapter) GenKey(key string, keyType int, client *mysql.MysqlClient) (int64, error) {
	logger.LogDebug("gen key use client", client)
	db := client.GetDB()
	id := int64(-1)
	db.QueryRow("SELECT `db`.`id` from `db` WHERE `db`.`key`=?", key).Scan(&id)
	if id < 0 {
		stmt, err := db.Prepare("INSERT INTO `db` (`id`, `type`, `key`) VALUES (NULL, ?, ?)")
		defer stmt.Close()
		if err != nil {
			return -1, err
		}
		result, err := stmt.Exec(keyType, key)
		if err != nil {
			return -1, err
		}
		id, err = result.LastInsertId()
		if err != nil {
			return -1, err
		}
	}
	return id, nil
}
Example #5
0
func (self *MedisHandler) Mget(keys []string) ([][]byte, error) {
	logger.LogDebug("mget", keys)
	return self.stringAdapter.MGet(keys)
}
Example #6
0
func (self *Selector) Balance() {
	self.lock.Lock()
	defer self.lock.Unlock()
	self.scaling = true
	logger.LogDebug("balancing datasource groups")
}