forked from mtusi2026/mtusi2026lab2
114 lines
3.2 KiB
Go
114 lines
3.2 KiB
Go
// Package valkey implements sharded cache and admission infrastructure.
|
|
package valkey
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab2/internal/hashring"
|
|
)
|
|
|
|
const virtualNodes = 256
|
|
|
|
// Options bounds each independent Valkey connection pool.
|
|
type Options struct {
|
|
// Nodes maps stable shard identities to network addresses.
|
|
Nodes map[string]string
|
|
// Coordinator identifies the stable global admission shard.
|
|
Coordinator string
|
|
// Timeout bounds operations and pool acquisition.
|
|
Timeout time.Duration
|
|
// PoolSize bounds connections to each shard.
|
|
PoolSize int
|
|
}
|
|
|
|
// Client owns a fixed topology; callers must drain before changing it.
|
|
type Client struct {
|
|
nodes map[string]*redis.Client
|
|
ring *hashring.Ring
|
|
coordinator *redis.Client
|
|
timeout time.Duration
|
|
topology string
|
|
}
|
|
|
|
// New prepares lazy clients without opening network connections.
|
|
func New(options Options) (*Client, error) {
|
|
if len(options.Nodes) < 1 || options.PoolSize < 1 || options.Timeout <= 0 {
|
|
return nil, fmt.Errorf("valkey nodes, pool bound and timeout are required")
|
|
}
|
|
ids := make([]string, 0, len(options.Nodes))
|
|
for id, address := range options.Nodes {
|
|
if address == "" {
|
|
return nil, fmt.Errorf("valkey address is required")
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
ring, err := hashring.New(ids, virtualNodes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, ok := options.Nodes[options.Coordinator]; !ok {
|
|
return nil, fmt.Errorf("coordinator must identify a configured node")
|
|
}
|
|
sort.Strings(ids)
|
|
topologyHash := sha256.Sum256([]byte(strings.Join(ids, ",")))
|
|
client := &Client{nodes: make(map[string]*redis.Client), ring: ring, timeout: options.Timeout}
|
|
for id, address := range options.Nodes {
|
|
client.nodes[id] = redis.NewClient(&redis.Options{
|
|
Addr: address,
|
|
PoolSize: options.PoolSize,
|
|
MaxActiveConns: options.PoolSize,
|
|
PoolTimeout: options.Timeout,
|
|
DialTimeout: options.Timeout,
|
|
ReadTimeout: options.Timeout,
|
|
WriteTimeout: options.Timeout,
|
|
ContextTimeoutEnabled: true,
|
|
MaxRetries: -1,
|
|
})
|
|
}
|
|
client.topology = hex.EncodeToString(topologyHash[:])
|
|
client.coordinator = client.nodes[options.Coordinator]
|
|
return client, nil
|
|
}
|
|
|
|
// Ping checks every shard required for safe admission.
|
|
func (c *Client) Ping(ctx context.Context) error {
|
|
ctx, cancel := context.WithTimeout(ctx, c.timeout)
|
|
defer cancel()
|
|
for _, node := range c.nodes {
|
|
if err := node.Ping(ctx).Err(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
stored, err := c.coordinator.Eval(ctx, topologyScript, []string{"admission:topology"}, c.topology).Text()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if stored != c.topology {
|
|
return fmt.Errorf("valkey topology differs: stop admission and drain before changing the ring")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
const topologyScript = `
|
|
redis.call('SET',KEYS[1],ARGV[1],'NX')
|
|
return redis.call('GET',KEYS[1])
|
|
`
|
|
|
|
// Close releases every connection pool owned by the client.
|
|
func (c *Client) Close() error {
|
|
var result error
|
|
for _, node := range c.nodes {
|
|
result = errors.Join(result, node.Close())
|
|
}
|
|
return result
|
|
}
|