diff --git a/weed/command/scaffold/master.toml b/weed/command/scaffold/master.toml index f550f0ad6..60a7dbcfc 100644 --- a/weed/command/scaffold/master.toml +++ b/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 diff --git a/weed/sequence/snowflake_sequencer.go b/weed/sequence/snowflake_sequencer.go index 300449fa0..381933b3a 100644 --- a/weed/sequence/snowflake_sequencer.go +++ b/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 { diff --git a/weed/server/master_server.go b/weed/server/master_server.go index 11dc95ded..9d222a342 100644 --- a/weed/server/master_server.go +++ b/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