sharding

package
v0.0.0-...-dac5809 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package sharding NOTES

Index

Constants

This section is empty.

Variables

View Source
var ErrRetryTransaction = errors.New("RETRY TRANSACTION ERROR")

ErrRetryTransaction defines errors that need to retry transaction, like deadlock error in upsert scenario

Functions

func InitAuditorSharding

func InitAuditorSharding(adminDB *gorm.DB) (*gorm.DB, error)

InitAuditorSharding 审计表分库

func InitBizSharding

func InitBizSharding(db *gorm.DB) error

InitBizSharding 按业务ID分表

func MustShardingAuditor

func MustShardingAuditor(adminDB *gorm.DB) *gorm.DB

MustShardingAuditor auditorDB 不存在,使用 adminDB

func URI

func URI(opt cc.Database) string

URI generate the standard db connection string format uri.

Types

type One

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

One represent one mysql sharding instance. Note: You must call Err() to test the error BEFORE you use the 'One' to do anything.

func (*One) AutoTxn

func (o *One) AutoTxn(kit *kit.Kit, run TxnFunc) error

AutoTxn is a wrapper to do all the transaction operations as follows: 1. auto launch the transaction 2. process the logics, which is a callback run function 3. rollback the transaction if 'run' hit an error automatically. 4. commit the transaction if no error happens.

func (*One) BeginTx

func (o *One) BeginTx(kit *kit.Kit) (*Tx, error)

BeginTx return DB instance's transaction.

func (*One) DB

func (o *One) DB() *sqlx.DB

DB return DB instance

func (*One) Err

func (o *One) Err() error

Err return am error if something wrong happens, then DO NOT USE the 'One' to do anything.

func (*One) ShardingUid

func (o *One) ShardingUid() string

ShardingUid return uid

type Sharding

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

Sharding is used to manage all the mysql instances which works for all the biz and admin resources.

func InitSharding

func InitSharding(sd *cc.Sharding) (*Sharding, error)

InitSharding initialize a sharding management instance.

func (*Sharding) Admin

func (s *Sharding) Admin() *One

Admin get the admin db instance

func (*Sharding) Audit

func (s *Sharding) Audit() *One

Audit get the audit db instance

func (*Sharding) Event

func (s *Sharding) Event() *One

Event get the event db instance

func (*Sharding) Healthz

func (s *Sharding) Healthz() error

Healthz check mysql healthz.

func (*Sharding) MustSharding

func (s *Sharding) MustSharding(biz uint32) *sqlx.DB

MustSharding get a db instance with biz id. It does not check the biz's value, caller should to guarantee that biz is > 0; Otherwise, it will panic.

func (*Sharding) ShardingOne

func (s *Sharding) ShardingOne(biz uint32) *One

ShardingOne get a db instance with biz id.

type Tx

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

Tx db affairs.

func (*Tx) Commit

func (t *Tx) Commit(kit *kit.Kit) error

Commit commit tx.

func (*Tx) Rollback

func (t *Tx) Rollback(kit *kit.Kit) error

Rollback rollback tx.

func (*Tx) ShardingUid

func (t *Tx) ShardingUid() string

ShardingUid return sharding uid

func (*Tx) Tx

func (t *Tx) Tx() *sqlx.Tx

Tx return tx.

type TxnFunc

type TxnFunc func(txn *sqlx.Tx, opt *TxnOption) error

TxnFunc is a callback function to process logic tasks between a transaction.

type TxnOption

type TxnOption struct {
	// ShardingUid is the unique id of a mysql sharding instance.
	// which means a same sharding instance have the same uid.
	// It is used to test if the mysql instance is the same instance
	// in a distributed transaction, such as create an app and save
	// its audit log.
	ShardingUid string
}

TxnOption defines all the options to do distributed transaction in the AutoTxn processes.

func (TxnOption) Validate

func (t TxnOption) Validate() error

Validate transaction option

Jump to

Keyboard shortcuts

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