add a snowflake sequencer as more robust fid generator, but less compressable than small auto-inc id

This commit is contained in:
李海 2021-03-25 18:49:26 +08:00
parent 82dfe06066
commit 69b2dab9c6
5 changed files with 55 additions and 1 deletions

1
go.mod
View file

@ -13,6 +13,7 @@ require (
github.com/Shopify/sarama v1.23.1
github.com/aws/aws-sdk-go v1.34.30
github.com/buraksezer/consistent v0.0.0-20191006190839-693edf70fd72
github.com/bwmarrin/snowflake v0.3.0
github.com/cespare/xxhash v1.1.0
github.com/chrislusf/raft v1.0.4
github.com/coreos/go-semver v0.3.0 // indirect

2
go.sum
View file

@ -141,6 +141,8 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4Yn
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/buraksezer/consistent v0.0.0-20191006190839-693edf70fd72 h1:fUmDBbSvv1uOzo/t8WaxZMVb7BxJ8JECo5lGoR9c5bA=
github.com/buraksezer/consistent v0.0.0-20191006190839-693edf70fd72/go.mod h1:OEE5igu/CDjGegM1Jn6ZMo7R6LlV/JChAkjfQQIRLpg=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.0 h1:LzQXZOgg4CQfE6bFvXGM30YZL1WW/M337pXml+GrcZ4=

View file

@ -506,7 +506,7 @@ default = "localhost:8888" # used by maintenance scripts if the scripts needs
[master.sequencer]
type = "raft" # Choose [raft|etcd] type for storing the file id sequence
type = "raft" # Choose [raft|etcd|snowflake] type for storing the file id sequence
# 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"

View file

@ -0,0 +1,43 @@
package sequence
import (
"fmt"
"hash/fnv"
"github.com/bwmarrin/snowflake"
)
// a simple snowflake Sequencer
type SnowflakeSequencer struct {
node *snowflake.Node
}
func NewSnowflakeSequencer(nodeid string) (*SnowflakeSequencer, error) {
node, err := snowflake.NewNode(int64(hash(nodeid) & 0x3ff))
if err != nil {
fmt.Println(err)
return nil, err
}
sequencer := &SnowflakeSequencer{node: node}
return sequencer, nil
}
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
func (m *SnowflakeSequencer) NextFileId(count uint64) uint64 {
return uint64(m.node.Generate().Int64())
}
// ignore setmax as we are snowflake
func (m *SnowflakeSequencer) SetMax(seenValue uint64) {
}
// return a new id as no Peek is stored
func (m *SnowflakeSequencer) Peek() uint64 {
return uint64(m.node.Generate().Int64())
}

View file

@ -277,6 +277,14 @@ func (ms *MasterServer) createSequencer(option *MasterOption) sequence.Sequencer
glog.Error(err)
seq = nil
}
case "snowflake":
var err error
glog.V(0).Infof("use a snowfalke seq id, nodeid %s:%d", option.Host, option.Port)
seq, err = sequence.NewSnowflakeSequencer(fmt.Sprintf("%s:%d", option.Host, option.Port))
if err != nil {
glog.Error(err)
seq = nil
}
default:
seq = sequence.NewMemorySequencer()
}