shardid

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 10 Imported by: 1

README

shardid

64 BITS

signed 1 millis (39) worker(2) db-sharding(10) table-rotate(2) sequence(10)
39 = 17 years 2 = 4 10=1024 0: none :table 10=1024
1: monthly :table-[YYYYMM]
2: weekly :table-[YYYY0XX]
3: daily :table-[YYYYMMDD]
  • signed(1): sid is always positive number
  • millis(39): 2^39 (17years) unix milliseconds since 2024-02-19 00:00:00
  • workers(2): 2^2(4) workers
  • db-sharding(10): 2^10 (1024) database instances
  • table-rotate(2): 2^2(4) table rotate: none/by year/by month/by day
  • sequence(10): 2^10(1024) ids per milliseconds

TPS:

  • ID: 1000(ms)*1024(seq)4 = 4096000 409.6W/s 10001024 = 1024000 102.4W/s

  • DB : 10 * 1000 = 10000 1W/s 1024 * 1000 = 1024000 102.4W/s

    10   * 2000   =   20000       2W/s
    1024 * 2000   = 2048000   204.8W/s
    
    10   * 3000   =   30000       3W/s
    1024 * 3000   = 3072000   307.2W/s
    

mysql-benchmark

issues

  • Overflow capacity waiting for next microsecond.

  • System Clock Dependency You should use NTP to keep your system clock accurate.

  • Time move backwards

    • if sequence doesn't overflow, let's use last timestamp and next sequence. system clock might moves forward and greater than last timestamp on next id generation
    • if sequence overflows, and has to be reset. let's built-in clock to get timestamp till system clock moves forward and greater than built-in clock
  • Built-in clock record last timestamp in memory/database, increase it when it is requested to send current timestamp instead of system clock

Documentation

Index

Constants

View Source
const (

	// TimeEpoch : 2024-2-19
	TimeEpoch int64 = 1708300800000
	// TimeEnd : 2041-2-19
	TimeEnd int64 = 2244844800000

	// TimeMillisBits milliseconds since TimeEpoch
	TimeMillisBits = 39
	// WorkerBits worker id: 0-3
	WorkerBits = 2
	// DatabaseBits  database id: 0-1023
	DatabaseBits = 10
	// TableBits table sharding: 0=none/1=yyyyMM/2=yyyy0WW/3=yyyyMMDD
	TableBits = 2
	// SequenceBits sequence: 0-1023
	SequenceBits = 10

	TimeNowShift  = WorkerBits + DatabaseBits + TableBits + SequenceBits
	WorkerShift   = DatabaseBits + TableBits + SequenceBits
	DatabaseShift = TableBits + SequenceBits
	TableShift    = SequenceBits

	MaxSequence   int16 = -1 ^ (-1 << SequenceBits) // 1023
	MaxTableShard int8  = -1 ^ (-1 << TableBits)
	MaxDatabaseID int16 = -1 ^ (-1 << DatabaseBits)
	MaxWorkerID   int8  = -1 ^ (-1 << WorkerBits)
	MaxTimeMillis int64 = -1 ^ (-1 << TimeMillisBits)
)

Variables

View Source
var (
	ErrDataItemIsBusy = errors.New("sqle: data_item_is_busy")
	ErrNilDHT         = errors.New("sqle: dht_is_nil")
)

Functions

func FormatDay added in v1.3.0

func FormatDay(t time.Time) string

func FormatMonth added in v1.3.0

func FormatMonth(t time.Time) string

func FormatWeek added in v1.3.0

func FormatWeek(t time.Time) string

Types

type DHT added in v1.4.0

type DHT struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

DHT distributed hash table

func NewDHT added in v1.4.0

func NewDHT(dbs ...int) *DHT

NewDHT create a distributed hash table between databases

func (*DHT) Add added in v1.4.0

func (m *DHT) Add(dbs ...int) []int

Add dynamically add databases, and return affected database

func (*DHT) Done added in v1.4.0

func (m *DHT) Done()

Done dbs are added, then reset current/next HashRing

func (*DHT) On added in v1.4.0

func (m *DHT) On(v string) (int, int, error)

On locate database with v from current/next HashRing, return ErrItemIsBusy if it is on affected database

type Generator

type Generator struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(options ...Option) *Generator

func (*Generator) Next

func (g *Generator) Next() ID

type HashRing added in v1.4.0

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

HashRing implement consistent hashing for database sharding with hash key

func NewHR added in v1.4.0

func NewHR(n int, options ...HashRingOption) *HashRing

NewHR create HashRing with n dbs and virtual nodes

func (*HashRing) On added in v1.4.0

func (r *HashRing) On(v string) (int, uint32)

On locate db and vNode for data v

type HashRingOption added in v1.4.0

type HashRingOption func(r *HashRing)

func WithReplicas added in v1.4.0

func WithReplicas(nodes ...string) HashRingOption

type ID

type ID struct {
	Time       time.Time
	Int64      int64
	TimeMillis int64

	Sequence   int16
	DatabaseID int16

	WorkerID    int8
	TableRotate TableRotate
}

ID shardid info

func Build

func Build(timeNow int64, workerID int8, databaseID int16, tr TableRotate, sequence int16) ID

func Parse

func Parse(id int64) ID

func (ID) MarshalJSON added in v1.4.6

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (*ID) RotateName

func (i *ID) RotateName() string

RotateName format time parts as rotated table name suffix

func (*ID) Scan added in v1.2.1

func (b *ID) Scan(src interface{}) error

Scan implements the sql.Scanner interface,

func (*ID) UnmarshalJSON added in v1.4.6

func (id *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface

func (ID) Value

func (b ID) Value() (driver.Value, error)

Value implements the driver.Valuer interface

type Option

type Option func(g *Generator)

func WithDailyRotate added in v1.2.1

func WithDailyRotate() Option

func WithDatabase

func WithDatabase(total int16) Option

func WithMonthlyRotate added in v1.2.1

func WithMonthlyRotate() Option

func WithRotate added in v1.2.1

func WithRotate(ts TableRotate) Option

func WithTimeNow

func WithTimeNow(now func() time.Time) Option

func WithWeeklyRotate added in v1.2.1

func WithWeeklyRotate() Option

func WithWorkerID

func WithWorkerID(i int8) Option

type TableRotate

type TableRotate int8

TableRotate table rotation option

var (
	NoRotate      TableRotate = 0
	MonthlyRotate TableRotate = 1
	WeeklyRotate  TableRotate = 2
	DailyRotate   TableRotate = 3
)

Jump to

Keyboard shortcuts

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