common

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package common provides core data structures and utilities shared across the distributed key-value store system. It defines fundamental types, configuration structures, and protocol elements used by other packages.

The package focuses on:

  • Message protocol definition for inter-component communication
  • Configuration structures for client and server components
  • Custom logging implementation integrated with Dragonboat
  • Utilities for Dragonboat (RAFT) integration

Key Components:

  • Message: Core data structure for all RPC communication between components, with a flexible structure that adapts to different operation types. Includes factory methods for creating various request and response messages.

  • MessageType: Enumeration defining all supported operation types in the system, categorized into key-value operations, lock operations, and control messages.

  • ServerConfig: Comprehensive configuration for server nodes, including RAFT parameters, storage settings, network configuration, and operation modes. Provides utilities for converting to Dragonboat-specific configurations.

  • ClientConfig: Configuration for client components, controlling connection parameters, timeouts, and retry behavior.

  • Logger: Custom logging implementation that integrates with Dragonboat's logging system while providing consistent formatting across the application.

Package util provides logging utilities for the application

Index

Constants

View Source
const (
	ShardTypeLocalIStore        ServerShardType = "local store"
	ShardTypeRemoteIStore                       = "remote store"
	ShardTypeLocalILockManager                  = "local lock manager"
	ShardTypeRemoteILockManager                 = "remote lock manager"
)

Variables

This section is empty.

Functions

func CreateLogger

func CreateLogger(pkgName string) logger.ILogger

CreateLogger implements the Factory interface - note the error return value

func InitLoggers

func InitLoggers(config ServerConfig)

InitLoggers initializes all loggers with the custom format

Types

type ClientConfig

type ClientConfig struct {
	Transport     ClientTransportConfig
	TimeoutSecond int
}

func (*ClientConfig) String

func (c *ClientConfig) String() string

String returns a formatted string representation of the client configuration

type ClientTransportConfig

type ClientTransportConfig struct {
	TCPConf
	SocketConf
	// Client specific settings
	ConnectionsPerEndpoint int
	Endpoints              []string
	RetryCount             int
}

func (*ClientTransportConfig) String

func (c *ClientTransportConfig) String() string

type Message

type Message struct {
	// Type of message
	MsgType MessageType `json:"msg_type"`

	// General fields
	Key      string `json:"key,omitempty"`      // Used for: Set, Get, Has, Expire, Delete, Acquire, Release
	ExpireIn uint64 `json:"expireIn,omitempty"` // Used for: Set operations
	DeleteIn uint64 `json:"deleteIn,omitempty"` // Used for: Set, Acquire operations
	Value    []byte `json:"value,omitempty"`    // Used for: Set (request), Get (response), Acquire (response)

	// Response only fields
	Ok  bool   `json:"ok,omitempty"`  // Used for: Get, Has, Acquire, Release responses
	Err string `json:"err,omitempty"` // Empty if no error, otherwise contains the error message

	// Meta information
	Meta []byte `json:"meta,omitempty"` // Unused, can be used for additional Adapters
}

Message represents a single message used for both requests and responses. Which fields are used depends on the type of message.

func NewAcquireRequest

func NewAcquireRequest(key string, deleteIn uint64) *Message

NewAcquireRequest creates a new Acquire request

func NewAcquireResponse

func NewAcquireResponse(ok bool, value []byte, err error) *Message

NewAcquireResponse creates a new Acquire response

func NewCustomRequest

func NewCustomRequest(meta []byte) *Message

NewCustomRequest creates a new Custom request

func NewCustomResponse

func NewCustomResponse(meta []byte, err error) *Message

NewCustomResponse creates a new Custom response

func NewDeleteRequest

func NewDeleteRequest(key string) *Message

NewDeleteRequest creates a new Delete request

func NewDeleteResponse

func NewDeleteResponse(err error) *Message

NewDeleteResponse creates a new Delete response

func NewErrorResponse

func NewErrorResponse(err string) *Message

NewErrorResponse creates a new Error response

func NewExpireRequest

func NewExpireRequest(key string) *Message

NewExpireRequest creates a new Expire request

func NewExpireResponse

func NewExpireResponse(err error) *Message

NewExpireResponse creates a new Expire response

func NewGetRequest

func NewGetRequest(key string) *Message

NewGetRequest creates a new Get request

func NewGetResponse

func NewGetResponse(value []byte, ok bool, err error) *Message

NewGetResponse creates a new Get response

func NewHasRequest

func NewHasRequest(key string) *Message

NewHasRequest creates a new Has request

func NewHasResponse

func NewHasResponse(ok bool, err error) *Message

NewHasResponse creates a new Has response

func NewReleaseRequest

func NewReleaseRequest(key string, ownerId []byte) *Message

NewReleaseRequest creates a new Release request

func NewReleaseResponse

func NewReleaseResponse(ok bool, err error) *Message

NewReleaseResponse creates a new Release response

func NewSetEIfUnsetRequest

func NewSetEIfUnsetRequest(key string, value []byte, expireIn, deleteIn uint64) *Message

NewSetEIfUnsetRequest creates a new SetEIfUnset request

func NewSetEIfUnsetResponse

func NewSetEIfUnsetResponse(err error) *Message

NewSetEIfUnsetResponse creates a new SetEIfUnset response

func NewSetERequest

func NewSetERequest(key string, value []byte, expireIn, deleteIn uint64) *Message

NewSetERequest creates a new SetE request

func NewSetEResponse

func NewSetEResponse(err error) *Message

NewSetEResponse creates a new SetE response

func NewSetRequest

func NewSetRequest(key string, value []byte) *Message

NewSetRequest creates a new Set request

func NewSetResponse

func NewSetResponse(err error) *Message

NewSetResponse creates a new Set response

type MessageType

type MessageType uint8

MessageType defines the type of message used in RPC communication.

const (
	MsgTUnknown MessageType = iota
	MsgTSuccess             // Indicates a successful operation
	MsgTError               // Indicates an error occurred

	MsgTKVSet         // Set a key-value pair
	MsgTKVSetE        // Set a key-value pair with expiration
	MsgTKVSetEIfUnset // Set a key-value pair if not already set
	MsgTKVExpire      // Expire a key
	MsgTKVDelete      // Delete a key-value pair
	MsgTKVGet         // Get a value by key
	MsgTKVHas         // Check if a key exists

	MsgTLCKAcquire // Acquire a lock
	MsgTLCKRelease // Release a lock

	MsgTCustom // Custom operation type
)

func (MessageType) MarshalJSON

func (t MessageType) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for MessageType. This allows MessageType to be serialized as a string in JSON.

func (MessageType) String

func (t MessageType) String() string

String returns the string representation of a MessageType.

func (*MessageType) UnmarshalJSON

func (t *MessageType) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for MessageType. This allows MessageType to be deserialized from a string in JSON.

type ServerConfig

type ServerConfig struct {
	// whether to start the server in single node mode or in a cluster
	Shards []ServerShard

	// Dragenboat parameters
	RTTMillisecond     uint64
	ElectionRTTFactor  uint64
	HeartbeatRTTFactor uint64
	SnapshotEntries    uint64
	CompactionOverhead uint64
	DataDir            string
	ReplicaID          uint64
	ClusterMembers     map[uint64]string

	// remote kvStore parameters
	TimeoutSecond int64

	// Logging configuration
	LogLevel string

	// Transport configuration
	Transport ServerTransportConfig
}

ServerConfig holds all configuration parameters for the RAFT cluster.

func (*ServerConfig) HasRemoteShard

func (c *ServerConfig) HasRemoteShard() bool

HasRemoteShard checks if the configuration contains any remote shards

func (*ServerConfig) String

func (c *ServerConfig) String() string

String returns a formatted string representation of the configuration

func (*ServerConfig) ToDragonboatConfig

func (c *ServerConfig) ToDragonboatConfig(shardId uint64) config.Config

ToDragonboatConfig converts the ServerConfig to Dragonboat Config

func (*ServerConfig) ToNodeHostConfig

func (c *ServerConfig) ToNodeHostConfig() config.NodeHostConfig

ToNodeHostConfig creates a NodeHostConfig for Dragonboat

type ServerShard

type ServerShard struct {
	// ShardID is the ID of the shard
	ShardID uint64
	// Store is the store for the shard
	Type ServerShardType
}

type ServerShardType

type ServerShardType string

type ServerTransportConfig

type ServerTransportConfig struct {
	TCPConf
	SocketConf
	// Server specific settings
	WorkersPerConn int
	Endpoint       string
}

func (*ServerTransportConfig) String

func (c *ServerTransportConfig) String() string

type SocketConf added in v1.0.1

type SocketConf struct {
	WriteBufferSize int
	ReadBufferSize  int
}

func (*SocketConf) String added in v1.0.1

func (c *SocketConf) String() string

type TCPConf added in v1.0.1

type TCPConf struct {
	TCPNoDelay      bool
	TCPKeepAliveSec int
	TCPLingerSec    int
}

func (*TCPConf) String added in v1.0.1

func (c *TCPConf) String() string

Jump to

Keyboard shortcuts

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