Browse Source

snowflake sequencer need an unique id

fix https://github.com/chrislusf/seaweedfs/issues/2213
pull/2218/head
Chris Lu 3 years ago
parent
commit
ac28611817
  1. 2
      weed/command/scaffold/master.toml
  2. 5
      weed/sequence/snowflake_sequencer.go
  3. 4
      weed/server/master_server.go

2
weed/command/scaffold/master.toml

@ -26,6 +26,8 @@ type = "raft" # Choose [raft|etcd|snowflake] type for storing the file id se
# when sequencer.type = etcd, set listen client urls of etcd cluster that store file id sequence
# example : http://127.0.0.1:2379,http://127.0.0.1:2389
sequencer_etcd_urls = "http://127.0.0.1:2379"
# when sequencer.type = snowflake, the snowflake id must be different from other masters
sequencer_snowflake_id = 0 # any number between 1~1023
# configurations for tiered cloud storage

5
weed/sequence/snowflake_sequencer.go

@ -13,8 +13,11 @@ type SnowflakeSequencer struct {
node *snowflake.Node
}
func NewSnowflakeSequencer(nodeid string) (*SnowflakeSequencer, error) {
func NewSnowflakeSequencer(nodeid string, snowflakeId int) (*SnowflakeSequencer, error) {
nodeid_hash := hash(nodeid) & 0x3ff
if snowflakeId != 0 {
nodeid_hash = uint32(snowflakeId)
}
glog.V(0).Infof("use snowflake seq id generator, nodeid:%s hex_of_nodeid: %x", nodeid, nodeid_hash)
node, err := snowflake.NewNode(int64(nodeid_hash))
if err != nil {

4
weed/server/master_server.go

@ -28,6 +28,7 @@ import (
const (
SequencerType = "master.sequencer.type"
SequencerEtcdUrls = "master.sequencer.sequencer_etcd_urls"
SequencerSnowflakeId = "master.sequencer.sequencer_snowflake_id"
)
type MasterOption struct {
@ -293,7 +294,8 @@ func (ms *MasterServer) createSequencer(option *MasterOption) sequence.Sequencer
}
case "snowflake":
var err error
seq, err = sequence.NewSnowflakeSequencer(fmt.Sprintf("%s:%d", option.Host, option.Port))
snowflakeId := v.GetInt(SequencerSnowflakeId)
seq, err = sequence.NewSnowflakeSequencer(fmt.Sprintf("%s:%d", option.Host, option.Port), snowflakeId)
if err != nil {
glog.Error(err)
seq = nil

Loading…
Cancel
Save