跳到主要内容

Sequencer API设计文档

本文档讨论"生成分布式唯一(自增)id"的API

1. 需求​

1.1. 生成全局唯一id​

Q: 什么时候需要生成全局唯一id?

A: db不帮你自动生成的时候。比如:

  • db做了分库分表,没帮你自动生成id,你又需要一个全局唯一的业务id
  • 没走db,比如请求到了后端要生成一个traceId

1.2. 对该id有递增的需求。具体来说有很多种:​

  • 不需要递增。这种情况UUID能解决,虽然缺点是比较长。本API暂时不考虑这种情况
  • “趋势递增”。不追求一定递增,大部分情况在递增就行。

Q: 什么场景需要趋势递增?

  1. 对b+树类的db(例如MYSQL)来说,趋势递增的主键能更好的利用缓存(cache friendly)。

  2. 拿来排序查最新数据。比如需求是查最新的100条消息,开发者不想新增个时间戳字段、建索引,如果id本身是递增的,那么查最新的100条消息时直接按id排序即可:

select * from message order by message-id limit 100

这在使用nosql的时候很常见,因为nosql在时间戳字段上加索引很难

  • sharding内单调递增。比如Tidb的自增id 能保证单台服务器上生成的id递增,没法保证全局(在多台服务器上)单调递增

  • 全局单调递增

希望生成的id一定递增,没有任何倒退的情况。

1.3. 可能会有自定义id schema的需求​

比如要求id的格式为"前8位是uid,后8位是自增id"这样的需求

1.4. 可能有信息安全相关需求​

如果ID是连续的,恶意用户的扒取工作就非常容易做了,直接按照顺序下载指定URL即可;

如果是订单号就更危险了,竞对可以直接知道用户一天的单量。所以在一些应用场景下,会需要ID无规则、不规则。

2. 产品调研​

系统能否保证生成的id唯一趋势递增严格递增可用性信息安全
单机redisyes.需要特殊配置redis服务器,把两种落盘策略都打开、每次写操作都写磁盘 ,避免丢数据yesyes.前提是宕机重启不丢数据有单点故障风险
redis 主从复制+sentinelno.复制是异步的,即使用Wait命令等待同步复制还是可能在fo后丢数据,见文档yes取决于会不会丢数据
redis cluster同上yes同上
snowflakeno.(时钟回拨等情况可能导致id重复;需要依赖外部存储;或者声明必须关闭NTP或使用防止回拨的可靠NTP)yesnogood
Leaf snowflakeyesyesnogood
Leaf segmentyesyesno
Leaf segment只部署一台Leaf服务器yesyesyes有单点故障风险
zookeeperyesyesyes
etcdyesyesyes
mysql单库单表yesyesyes有单点故障风险

3. grpc API设计​

3.1. proto定义​

// Sequencer API
rpc GetNextId(GetNextIdRequest )returns (GetNextIdResponse) {}


message GetNextIdRequest {
string store_name = 1;
// key is the identifier of a sequencer.
string key = 2;

SequencerOptions options = 3;
// The metadata which will be sent to the component.
map<string, string> metadata = 4;
}

// SequencerOptions configures requirements for incremental and uniqueness guarantee
message SequencerOptions {
enum AutoIncrement {
// WEAK means a "best effort" incrementing service.But there is no strict guarantee
WEAK = 0;
// STRONG means a strict guarantee of global monotonically increasing
STRONG = 1;
}

// enum Uniqueness{
// // WEAK means a "best effort" unqueness guarantee.
// // But it might duplicate in some corner cases.
// WEAK = 0;
// // STRONG means a strict guarantee of global uniqueness
// STRONG = 1;
// }

AutoIncrement increment = 1;
// Uniqueness uniqueness=2;
}

message GetNextIdResponse{
int64 next_id=1;
}

解释:

其实就一个GetNextId接口,需要传key作为命名空间(比如传某个订单表表名,"order_table"),sequencer保证生成的id在该命名空间内唯一、递增。

SequencerOptions.AutoIncrement用于声明用户对递增性的需求,是趋势递增(WEAK)还是要求严格全局递增(STRONG)

Q: 是否由Layotto帮用户按需拼id?

A: API和Layotto运行时不管这事,由sdk或者用户自己处理,或者某个特殊组件想实现这个feature也可以。

Q: 返回类型是string还是int64

如果返回string,假如用户用了某个返回int64的实现,在用户代码中把返回的string转成了int64,那他迁移到别的组件,这个转换过程可能报错

如果返回int64,组件就没法帮用户做一些定制拼id的事情了

为了可移植性,选择int64。拼id的事情在sdk里做

Q: int64溢出问题怎么处理?

暂不考虑

3.2. 关于唯一性的争议​

API中原先定义了用户传参SequencerOptions.Uniqueness枚举值 ,其中WEAK代表"尽量保证全局唯一,但是极小概率可能重复",要求业务代码如果拿到id去写库时,做好id重复、重试的心理准备;而STRONG代表严格全局唯一,用户代码不考虑id重复、重试之类的事情。

  • 定义这个枚举值的原因(好处)

如果要保证严格唯一,组件实现起来会比较重。比如单机redis常用的落盘策略就不行,可能导致宕机重启之后丢数据、生成一个重复id;比如直接在sidecar里写一个snowflake算法就不行,因为可能会有时钟回拨导致id重复的问题(NTP时钟同步、闰秒等情况都有可能导致时钟回拨)。Leaf的snowflake实现 依赖zookeeper判断时钟回拨;

  • 定义这个枚举值的缺点

给用户带来更多理解成本

  • 结论

存在争议,本期先不添加该枚举值。默认返回的结果一定能保证全局唯一(STRONG)。

4. 组件API​

package sequencer

type Store interface {
// Init this component.
//
// The number 'BiggerThan' means that the id generated by this component must be bigger than this number.
//
// If the component find that currently the storage can't guarantee this,
// it can do some initialization like inserting a new id equal to or bigger than this 'BiggerThan' into the storage,
// or just return an error
Init(config Configuration) error

GetNextId(*GetNextIdRequest) (*GetNextIdResponse, error)

// GetSegment returns a range of id.
// 'support' indicates whether this method is supported by the component.
// Layotto runtime will cache the result if this method is supported.
GetSegment(*GetSegmentRequest) (support bool, result *GetSegmentResponse, err error)
}

type GetNextIdRequest struct {
Key string
Options SequencerOptions
Metadata map[string]string
}

type SequencerOptions struct {
AutoIncrement AutoIncrementOption
}

type AutoIncrementOption string

const (
WEAK AutoIncrementOption = "weak"
STRONG AutoIncrementOption = "strong"
)

type GetNextIdResponse struct {
NextId int64
}

type GetSegmentRequest struct {
Size int
Key string
Options SequencerOptions
Metadata map[string]string
}

type GetSegmentResponse struct {
Segment []int64
}

type Configuration struct {
BiggerThan map[string]int64
Properties map[string]string
}

Q: BiggerThan字段是啥?

要求组件生成的所有id都得比"biggerThan"大。

设计这个配置项是为了方便用户做移植。比如系统原先使用mysql做发号服务,id已经生成到了1000,后来迁移到PostgreSQL上,需要配置biggerThan为1000,这样PostgreSQL组件在初始化的时候会进行设置、强制id在1000以上,或者发现id没法满足要求、直接启动时报错。

Q: BiggerThan为啥是个map?

因为每个key可能有自己的biggerThan.

比如原先app1做了分库分表、用某种发号服务来生成订单表和商品表的id,订单表id达到了1000,商品表id达到2000.

后来app1想换个存储做sequencer,那么他要声明订单表id在1000以上,商品表id在2000以上。

另一个例子是Leaf的设计 ,也是每个biz_tag对应一个max_id(Leaf的max_id就是我们的biggerThan)

leaf_max_id.png

Q: 要不要在runtime层实现缓存?

如果runtime做缓存,需要组件实现方法:

GetSegment(*GetSegmentRequest) (support bool, result *GetSegmentResponse, err error)

可以先定义接口,组件先不实现,以后有性能需求再实现

参考资料​

设计分布式唯一id生成

架构 细聊分布式ID生成方法

Leaf——美团点评分布式ID生成系统