Documentation
¶
Overview ¶
Package gridkv provides a distributed key-value cache with eventual consistency.
Features: consistent hashing, batched replication, SWIM failure detection, adaptive LAN/WAN networking, Prometheus/OTLP metrics.
Thread-safe: all public methods are safe for concurrent access.
Index ¶
- Constants
- Variables
- type ClusterStats
- type Config
- type GridKV
- func (g *GridKV) Close(timeout ...time.Duration) error
- func (g *GridKV) Delete(ctx context.Context, key string) (err error)
- func (g *GridKV) Get(ctx context.Context, key string) (value []byte, err error)
- func (g *GridKV) HealthCheck() error
- func (g *GridKV) Set(ctx context.Context, key string, value []byte, ttl ...time.Duration) (err error)
- func (g *GridKV) Stats() Stats
- func (g *GridKV) WaitReady(timeout time.Duration) error
- type GridKVOptions
- type LogConfig
- type LoggerOptions
- type NetworkConfig
- type NetworkOptions
- type NetworkType
- type Option
- func WithBatchThreshold(threshold int) Option
- func WithBatchWindow(window time.Duration) Option
- func WithDisableAuth(disable bool) Option
- func WithFailureTimeout(timeout time.Duration) Option
- func WithGossipInterval(interval time.Duration) Option
- func WithHotCacheSize(size int) Option
- func WithHotReadCacheTTL(ttl time.Duration) Option
- func WithLocalAddress(addr string) Option
- func WithLocalNodeID(nodeID string) Option
- func WithLogger(log interface{}) Option
- func WithLoggerOptions(opts LoggerOptions) Option
- func WithNetworkBindAddr(addr string) Option
- func WithNetworkMaxConns(maxConns int) Option
- func WithNetworkMaxIdle(maxIdle int) Option
- func WithNetworkReadTimeout(timeout time.Duration) Option
- func WithNetworkTimeout(timeout time.Duration) Option
- func WithNetworkType(netType NetworkType) Option
- func WithNetworkWriteTimeout(timeout time.Duration) Option
- func WithReadRepairRateLimit(limit int64) Option
- func WithReadTimeout(timeout time.Duration) Option
- func WithReplicaCount(count int) Option
- func WithReplicationTimeout(timeout time.Duration) Option
- func WithSeedAddrs(addrs ...string) Option
- func WithStartupGracePeriod(period time.Duration) Option
- func WithStorageMaxMemoryMB(mb int64) Option
- func WithStorageShardCount(count int) Option
- func WithSuspectTimeout(timeout time.Duration) Option
- func WithTTL(ttl time.Duration) Option
- func WithVirtualNodes(count int) Option
- type Options
- type Stats
- type StorageConfig
- type StorageOptions
Constants ¶
const ( LogLevelDebug = logging.LevelDebug LogLevelInfo = logging.LevelInfo LogLevelWarn = logging.LevelWarn LogLevelError = logging.LevelError LogLevelFatal = logging.LevelFatal )
Logging level constants
const ( LogFormatText = logging.FormatText LogFormatJSON = logging.FormatJSON LogFormatCompact = logging.FormatCompact )
Logging format constants
const Version = "v0.3.8"
Version represents the current version of GridKV
Variables ¶
var ( // ErrShuttingDown indicates the node has begun graceful shutdown ErrShuttingDown = errors.New("gridkv shutting down") // ErrVersionMismatch indicates a version conflict ErrVersionMismatch = errors.New("version mismatch") // ErrMemoryLimitExceeded indicates memory limit reached ErrMemoryLimitExceeded = errors.New("memory limit exceeded") // ErrEmptyKey indicates an empty key was provided ErrEmptyKey = mem_storage.ErrEmptyKey )
Error constants exported for application error handling
Functions ¶
This section is empty.
Types ¶
type ClusterStats ¶ added in v0.3.4
type ClusterStats struct {
Ready bool
ClusterSize int
HealthyNodes int
ReplicaFactor int
LocalNodeID string
PubkeysReady bool
PubkeyCount int
PeerCount int
}
ClusterStats represents cluster health and readiness statistics.
type Config ¶ added in v0.4.0
type Config struct {
LocalNodeID string `json:"local_node_id"`
LocalAddress string `json:"local_address"`
SeedAddrs []string `json:"seed_addrs,omitempty"`
TTL string `json:"ttl,omitempty"` // Duration string (e.g., "5m")
HotReadCacheTTL string `json:"hot_read_cache_ttl,omitempty"`
HotCacheSize int `json:"hot_cache_size,omitempty"`
VirtualNodes int `json:"virtual_nodes,omitempty"`
ReplicaCount int `json:"replica_count,omitempty"`
ReadRepairRateLimitPerSec int64 `json:"read_repair_rate_limit_per_sec,omitempty"`
FailureTimeout string `json:"failure_timeout,omitempty"`
SuspectTimeout string `json:"suspect_timeout,omitempty"`
GossipInterval string `json:"gossip_interval,omitempty"`
ReplicationTimeout string `json:"replication_timeout,omitempty"`
ReadTimeout string `json:"read_timeout,omitempty"`
StartupGracePeriod string `json:"startup_grace_period,omitempty"`
DisableAuth bool `json:"disable_auth,omitempty"`
BatchThreshold int `json:"batch_threshold,omitempty"`
BatchWindow string `json:"batch_window,omitempty"`
Network *NetworkConfig `json:"network,omitempty"`
Storage *StorageConfig `json:"storage,omitempty"`
Log *LogConfig `json:"log,omitempty"`
}
Config is used for JSON/YAML configuration file parsing with snake_case field names.
type GridKV ¶
type GridKV struct {
// contains filtered or unexported fields
}
GridKV is the distributed key-value cache instance.
Components: cluster (membership/replication), storage backend, network transport. Thread-safe.
func NewGridKV ¶
NewGridKV initializes a GridKV instance with the provided options.
Example:
kv, err := gridkv.NewGridKV(
gridkv.WithLocalNodeID("node-1"),
gridkv.WithLocalAddress("localhost:8080"),
gridkv.WithSeedAddrs("localhost:8081"),
)
Required fields: LocalNodeID, LocalAddress
func (*GridKV) Close ¶
Close shuts down GridKV: stops cluster (which manages all components via lifecycle). Uses 30s default timeout if none provided. Idempotent. Thread-safe. All resources (network, storage, executor, cache) are managed by cluster's lifecycle manager.
func (*GridKV) Delete ¶
Delete removes a key-value pair.
Writes tombstone locally, enqueues async replication. Idempotent. Panic-safe, thread-safe.
func (*GridKV) Get ¶
Get retrieves a value by key.
Reads locally if available, otherwise forwards to coordinator with retries. Returns freshest value, triggers read-repair on version mismatch. Returns deep copy.
If the key is not found, returns (nil, nil). Only returns an error for real failures (network errors, timeouts, etc.).
Panic-safe, thread-safe.
func (*GridKV) HealthCheck ¶ added in v0.1.2
HealthCheck verifies GridKV is initialized and cluster has healthy nodes.
func (*GridKV) Set ¶
func (g *GridKV) Set(ctx context.Context, key string, value []byte, ttl ...time.Duration) (err error)
Set stores a key-value pair with eventual replication.
Computes replica set via consistent hashing, writes locally, enqueues async replication. Returns immediately. TTL overrides default (0 = no expiration). Panic-safe, thread-safe.
type GridKVOptions ¶
type GridKVOptions = Options
GridKVOptions is deprecated, use Options or functional options instead. Kept for backward compatibility.
type LogConfig ¶ added in v0.4.0
type LogConfig struct {
Level string `json:"level,omitempty"`
Format string `json:"format,omitempty"`
TimeFormat string `json:"time_format,omitempty"`
NoCaller bool `json:"no_caller,omitempty"`
NoTime bool `json:"no_time,omitempty"`
}
LogConfig is used for JSON/YAML configuration file parsing.
type LoggerOptions ¶ added in v0.3.0
type LoggerOptions struct {
Level string
Format string
Output io.Writer
TimeFormat string
NoCaller bool
NoTime bool
}
LoggerOptions configures logging (internal use).
type NetworkConfig ¶ added in v0.4.0
type NetworkConfig struct {
Type string `json:"type,omitempty"` // "tcp" or "quic"
BindAddr string `json:"bind_addr,omitempty"`
MaxIdle int `json:"max_idle,omitempty"`
MaxConns int `json:"max_conns,omitempty"`
Timeout string `json:"timeout,omitempty"` // Duration string
ReadTimeout string `json:"read_timeout,omitempty"` // Duration string
WriteTimeout string `json:"write_timeout,omitempty"` // Duration string
}
NetworkConfig is used for JSON/YAML configuration file parsing.
type NetworkOptions ¶
type NetworkOptions struct {
Type NetworkType
BindAddr string
MaxIdle int
MaxConns int
Timeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
}
NetworkOptions configures networking (internal use).
type NetworkType ¶
type NetworkType int
NetworkType represents the transport protocol.
const ( TCP NetworkType = 1 // Reliable transport QUIC NetworkType = 3 // QUIC )
type Option ¶ added in v0.4.0
type Option func(*Options)
Option is a functional option for configuring GridKV.
func WithBatchThreshold ¶ added in v0.4.0
WithBatchThreshold sets the batch threshold.
func WithBatchWindow ¶ added in v0.4.0
WithBatchWindow sets the batch window.
func WithDisableAuth ¶ added in v0.4.0
WithDisableAuth disables authentication.
func WithFailureTimeout ¶ added in v0.4.0
WithFailureTimeout sets the failure timeout.
func WithGossipInterval ¶ added in v0.4.0
WithGossipInterval sets the gossip interval.
func WithHotCacheSize ¶ added in v0.4.0
WithHotCacheSize sets the hot read cache size.
func WithHotReadCacheTTL ¶ added in v0.4.0
WithHotReadCacheTTL sets the hot read cache TTL.
func WithLocalAddress ¶ added in v0.4.0
WithLocalAddress sets the local address.
func WithLocalNodeID ¶ added in v0.4.0
WithLocalNodeID sets the local node ID.
func WithLogger ¶ added in v0.4.0
func WithLogger(log interface{}) Option
WithLogger sets the logger.
func WithLoggerOptions ¶ added in v0.4.0
func WithLoggerOptions(opts LoggerOptions) Option
WithLoggerOptions sets the logger options.
func WithNetworkBindAddr ¶ added in v0.4.0
WithNetworkBindAddr sets the network bind address.
func WithNetworkMaxConns ¶ added in v0.4.0
WithNetworkMaxConns sets the maximum total connections per peer.
func WithNetworkMaxIdle ¶ added in v0.4.0
WithNetworkMaxIdle sets the maximum idle connections per peer.
func WithNetworkReadTimeout ¶ added in v0.4.0
WithNetworkReadTimeout sets the network read timeout.
func WithNetworkTimeout ¶ added in v0.4.0
WithNetworkTimeout sets the network dial timeout.
func WithNetworkType ¶ added in v0.4.0
func WithNetworkType(netType NetworkType) Option
WithNetworkType sets the network type.
func WithNetworkWriteTimeout ¶ added in v0.4.0
WithNetworkWriteTimeout sets the network write timeout.
func WithReadRepairRateLimit ¶ added in v0.4.0
WithReadRepairRateLimit sets the read repair rate limit per second.
func WithReadTimeout ¶ added in v0.4.0
WithReadTimeout sets the read timeout.
func WithReplicaCount ¶ added in v0.4.0
WithReplicaCount sets the replica count.
func WithReplicationTimeout ¶ added in v0.4.0
WithReplicationTimeout sets the replication timeout.
func WithSeedAddrs ¶ added in v0.4.0
WithSeedAddrs sets the seed addresses.
func WithStartupGracePeriod ¶ added in v0.4.0
WithStartupGracePeriod sets the startup grace period.
func WithStorageMaxMemoryMB ¶ added in v0.4.0
WithStorageMaxMemoryMB sets the maximum memory in MB.
func WithStorageShardCount ¶ added in v0.4.0
WithStorageShardCount sets the shard count.
func WithSuspectTimeout ¶ added in v0.4.0
WithSuspectTimeout sets the suspect timeout.
func WithVirtualNodes ¶ added in v0.4.0
WithVirtualNodes sets the number of virtual nodes.
type Options ¶ added in v0.4.0
type Options struct {
LocalNodeID string
LocalAddress string
SeedAddrs []string
TTL time.Duration
HotReadCacheTTL time.Duration
HotCacheSize int
VirtualNodes int
ReplicaCount int
ReadRepairRateLimitPerSec int64
FailureTimeout time.Duration
SuspectTimeout time.Duration
GossipInterval time.Duration
ReplicationTimeout time.Duration
ReadTimeout time.Duration
StartupGracePeriod time.Duration
DisableAuth bool
BatchThreshold int
BatchWindow time.Duration
Network *NetworkOptions
Storage *StorageOptions
Log interface{} // LoggerOptions, logging.Opts, or *logging.Logger
}
Options is the internal configuration structure used for GridKV initialization.
func ParseConfig ¶ added in v0.4.0
ParseConfig parses a JSON configuration with snake_case field names into Options.
type Stats ¶ added in v0.3.4
type Stats struct {
// Cluster stats
Cluster ClusterStats
// Network stats
Network network.NetworkSnapshot
// Storage stats
Storage mem_storage.Stats
// Version information
Version string
}
Stats represents complete GridKV statistics including cluster, network and storage stats.
type StorageConfig ¶ added in v0.4.0
type StorageConfig struct {
MaxMemoryMB int64 `json:"max_memory_mb,omitempty"`
ShardCount int `json:"shard_count,omitempty"`
}
StorageConfig is used for JSON/YAML configuration file parsing.
type StorageOptions ¶
StorageOptions configures storage (internal use).
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
utils/hlc
Package hlc implements Hybrid Logical Clock (HLC) for distributed timestamps.
|
Package hlc implements Hybrid Logical Clock (HLC) for distributed timestamps. |
|
tests
|
|
|
simulator
Package simulator provides test configurations for GridKV testing.
|
Package simulator provides test configurations for GridKV testing. |