store

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Router             *Router
	PeerBuilder        PeerBuilder
	LocalMeta          *raftmeta.Store
	WorkDir            string
	Scheduler          SchedulerClient
	HeartbeatInterval  time.Duration
	StoreID            uint64
	OperationQueueSize int
	OperationCooldown  time.Duration
	OperationInterval  time.Duration
	OperationBurst     int
	CommandApplier     func(*pb.RaftCmdRequest) (*pb.RaftCmdResponse, error)
	CommandTimeout     time.Duration
}

Config configures Store construction. Only the Router field is optional; the store fills in a default router when omitted.

type Operation added in v0.7.1

type Operation struct {
	Type   OperationType
	Region uint64
	Source uint64
	Target uint64
}

Operation represents a scheduling decision to be executed by store runtime.

type OperationType added in v0.7.1

type OperationType uint8

OperationType identifies the scheduler operation kind.

const (
	OperationNone OperationType = iota
	OperationLeaderTransfer
)

func (OperationType) String added in v0.7.1

func (t OperationType) String() string

type PeerBuilder

type PeerBuilder func(meta raftmeta.RegionMeta) (*peer.Config, error)

PeerBuilder constructs peer configuration for the provided region metadata. It allows the store to spawn new peers for splits without external callers wiring the configuration manually.

type PeerHandle

type PeerHandle struct {
	ID     uint64
	Peer   *peer.Peer
	Region *raftmeta.RegionMeta
}

PeerHandle is a lightweight view of a peer registered with the store. It is designed for diagnostics and scheduling components so they can iterate over the cluster topology without touching the internal map directly.

type RegionSnapshot

type RegionSnapshot struct {
	Regions []raftmeta.RegionMeta `json:"regions"`
}

RegionSnapshot provides an external view of the tracked Region metadata.

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router mimics TinyKV's raftstore router by providing an addressable abstraction for driving peer state machines. Each peer registers itself with the router so other components (store loops, RPC handlers, tests) can deliver raft traffic or administrative ticks without holding the peer reference directly.

func NewRouter

func NewRouter() *Router

NewRouter creates an empty router instance.

func (*Router) BroadcastFlush

func (r *Router) BroadcastFlush() error

BroadcastFlush forces processReady on all registered peers. This mirrors TinyKV's behavior of draining the ready queue when necessary.

func (*Router) BroadcastTick

func (r *Router) BroadcastTick() error

BroadcastTick invokes Tick on every registered peer. The first error is returned to the caller.

func (*Router) Deregister

func (r *Router) Deregister(id uint64)

Deregister removes a peer mapping. The caller is responsible for closing the peer after deregistration.

func (*Router) Peer

func (r *Router) Peer(id uint64) (*peer.Peer, bool)

Peer returns a peer handle by ID.

func (*Router) Register

func (r *Router) Register(p *peer.Peer) error

Register wires a peer into the router. Register must only be called once per peer ID.

func (*Router) SendCommand added in v0.2.0

func (r *Router) SendCommand(id uint64, req *pb.RaftCmdRequest) error

SendCommand encodes the provided raft command and submits it to the peer.

func (*Router) SendPropose

func (r *Router) SendPropose(id uint64, data []byte) error

SendPropose submits an application proposal to the peer.

func (*Router) SendRaft

func (r *Router) SendRaft(id uint64, msg myraft.Message) error

SendRaft delivers a raft protocol message to the registered peer.

func (*Router) SendTick

func (r *Router) SendTick(id uint64) error

SendTick drives a single logical clock tick for the target peer.

type SchedulerClient added in v0.7.1

type SchedulerClient interface {
	PublishRegion(context.Context, raftmeta.RegionMeta)
	RemoveRegion(context.Context, uint64)
	StoreHeartbeat(context.Context, StoreStats) []Operation
	Status() SchedulerStatus
	Close() error
}

SchedulerClient publishes store state to the control plane and returns any scheduling decisions that should be applied locally.

type SchedulerMode added in v0.7.2

type SchedulerMode string

SchedulerStatus captures local/control-plane scheduler health. It is a diagnostic view only; stores must not treat it as routing authority.

const (
	SchedulerModeHealthy     SchedulerMode = "healthy"
	SchedulerModeDegraded    SchedulerMode = "degraded"
	SchedulerModeUnavailable SchedulerMode = "unavailable"
)

type SchedulerStatus added in v0.7.1

type SchedulerStatus struct {
	Mode              SchedulerMode `json:"mode"`
	Degraded          bool          `json:"degraded"`
	LastError         string        `json:"last_error,omitempty"`
	LastErrorAt       time.Time     `json:"last_error_at"`
	DroppedOperations uint64        `json:"dropped_operations"`
}

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store hosts a collection of peers and exposes the concrete runtime helpers used by raftstore. It owns peer registration, region metadata, optional control-plane heartbeats, and command application.

func NewStore

func NewStore(cfg Config) *Store

NewStore constructs a Store using concrete dependencies. It keeps peer construction, region tracking, and scheduler heartbeats explicit rather than routing them through callback chains.

func (*Store) Close

func (s *Store) Close()

Close stops background workers associated with the store.

func (*Store) Peer

func (s *Store) Peer(id uint64) (*peer.Peer, bool)

Peer returns the peer registered with the provided ID.

func (*Store) Peers

func (s *Store) Peers() []PeerHandle

Peers returns a snapshot describing every peer managed by the store.

func (*Store) ProposeAddPeer

func (s *Store) ProposeAddPeer(regionID uint64, meta raftmeta.PeerMeta) error

ProposeAddPeer issues a configuration change to add the provided peer to the region's raft group. Local region metadata is updated once the configuration change is committed and applied.

func (*Store) ProposeCommand added in v0.2.0

func (s *Store) ProposeCommand(ctx context.Context, req *pb.RaftCmdRequest) (*pb.RaftCmdResponse, error)

ProposeCommand submits a raft command to the leader hosting the target region. When the store is not leader or the request header is invalid the returned response includes an appropriate RegionError.

func (*Store) ProposeMerge

func (s *Store) ProposeMerge(targetRegionID, sourceRegionID uint64) error

ProposeMerge submits a merge admin command merging source region into target.

func (*Store) ProposeRemovePeer

func (s *Store) ProposeRemovePeer(regionID, peerID uint64) error

ProposeRemovePeer issues a configuration change removing the peer with the provided peer ID from the region's raft group.

func (*Store) ProposeSplit

func (s *Store) ProposeSplit(parentID uint64, childMeta raftmeta.RegionMeta, splitKey []byte) error

ProposeSplit issues a split command through the raft log of the parent region. The child metadata must describe the new region configuration.

func (*Store) ReadCommand added in v0.2.0

func (s *Store) ReadCommand(ctx context.Context, req *pb.RaftCmdRequest) (*pb.RaftCmdResponse, error)

ReadCommand executes the provided read-only raft command locally on the leader. The command must only include read operations (Get/Scan). The method returns a RegionError when the store is not leader for the target region.

func (*Store) RegionMetaByID

func (s *Store) RegionMetaByID(regionID uint64) (raftmeta.RegionMeta, bool)

RegionMetaByID returns the stored metadata for the provided region, along with a boolean indicating whether it exists.

func (*Store) RegionMetas

func (s *Store) RegionMetas() []raftmeta.RegionMeta

RegionMetas collects the known raftmeta.RegionMeta entries from registered peers. This mirrors the TinyKV store exposing region layout information to schedulers and debugging endpoints.

func (*Store) RegionMetrics

func (s *Store) RegionMetrics() *metrics.RegionMetrics

RegionMetrics returns the metrics recorder tracking region state counts.

func (*Store) RegionSnapshot

func (s *Store) RegionSnapshot() RegionSnapshot

RegionSnapshot returns a snapshot containing all region metadata currently known to the store. The resulting slice is safe for callers to modify.

func (*Store) Router

func (s *Store) Router() *Router

Router exposes the underlying router reference so transports can reuse the same registration hub.

func (*Store) SchedulerStatus added in v0.7.1

func (s *Store) SchedulerStatus() SchedulerStatus

SchedulerStatus returns the current scheduler health view by combining the local queue state with the control-plane client status.

func (*Store) StartPeer

func (s *Store) StartPeer(cfg *peer.Config, bootstrapPeers []myraft.Peer) (*peer.Peer, error)

StartPeer builds and registers a peer according to the supplied configuration. The peer is automatically registered with the Store router. When bootstrapPeers is non-empty StartPeer will call Bootstrap with those peers after the peer is registered.

func (*Store) Step added in v0.2.0

func (s *Store) Step(msg myraft.Message) error

Step forwards the provided raft message to the target peer hosted on this store.

func (*Store) StopPeer

func (s *Store) StopPeer(id uint64)

StopPeer removes the peer from the router and closes it.

func (*Store) TransferLeader

func (s *Store) TransferLeader(regionID, targetPeerID uint64) error

TransferLeader initiates leadership transfer for the specified region to the provided peer ID.

func (*Store) VisitPeers

func (s *Store) VisitPeers(fn func(*peer.Peer))

VisitPeers executes the provided callback for every peer registered with the store. The callback receives a snapshot of the peer pointer so callers can perform operations without holding the store lock for extended periods.

type StoreStats added in v0.7.1

type StoreStats struct {
	StoreID   uint64    `json:"store_id"`
	RegionNum uint64    `json:"region_num"`
	LeaderNum uint64    `json:"leader_num"`
	Capacity  uint64    `json:"capacity"`
	Available uint64    `json:"available"`
	UpdatedAt time.Time `json:"updated_at"`
}

StoreStats captures minimal store-level heartbeat information.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL