func makeShards(src []*shardSource) ([]*shard.Shard, error) { out := make([]*shard.Shard, len(src)) for i, sh := range src { if strings.Index(sh.Pivot, "0x") != 0 { return nil, errors.New("invalid pivot format") } pivot, err := strconv.ParseUint(sh.Pivot[2:], 16, 64) if err != nil { return nil, err } out[i] = &shard.Shard{ Pivot: xor.Key(pivot), Addr: x.Addr(sh.Addr), HTTP: sh.HTTP, } } return out, nil }
// Sum sends a SUM request to the sumr database and returns the value underlying key, or zero otherwise func (cli *Client) Sum(key sumr.Key) (result float64) { cli.lmtr.Open() defer cli.lmtr.Close() cli.lk.Lock() server := cli.metric.Nearest(xor.Key(key), 1)[0].(*shard).Server cli.lk.Unlock() // Recover from dead shard panic defer func() { if err := recover(); err != nil { log.Printf("dead shard: %s", err) result = math.NaN() } }() retrn := server.Call("Sum", key) return retrn[0].(float64) }
// Add sends an ADD request to the database to add value to key; if key does not exist, it is created with the given value. // updateTime is the application-level timestamp of this request. // Add returns the value of the key after the update. func (cli *Client) Add(updateTime time.Time, key sumr.Key, value float64) (result float64) { // Per-client rate-limiting cli.lmtr.Open() defer cli.lmtr.Close() cli.lk.Lock() server := cli.metric.Nearest(xor.Key(key), 1)[0].(*shard).Server cli.lk.Unlock() // Recover from dead shard panic defer func() { if err := recover(); err != nil { log.Printf("dead shard: %s", err) // XXX: Take a more comprehensive action here result = math.NaN() } }() retrn := server.Call("Add", updateTime, key, value) return retrn[0].(float64) }
func (s *shard) Key() xor.Key { return xor.Key(s.ShardKey) }
// ID returns the XOR-metric ID of the shard underlying this checkpoint func (s *WorkerCheckpoint) Key() xor.Key { return xor.Key(s.ShardKey) }
func ShardKeyOf(timedashID int64) xor.Key { h := fnv.New64a() binary.Write(h, binary.BigEndian, timedashID) return xor.Key(h.Sum64()) }
func (id SpaceID) ShardKey() xor.Key { return xor.Key(id) }