diff options
| author | Chris Lu <chrislusf@users.noreply.github.com> | 2021-03-25 10:29:07 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-25 10:29:07 -0700 |
| commit | b8d3f11e92b1c7efe6f208452a349be9e085783b (patch) | |
| tree | 27157bafcd013483e815c00fddb7ae6e3e16e901 /weed/sequence | |
| parent | 82dfe06066a4fbd6d8acc35040d32880643d9a26 (diff) | |
| parent | 06be5dc6c3cd50c461656e35a53585dd44d94793 (diff) | |
| download | seaweedfs-b8d3f11e92b1c7efe6f208452a349be9e085783b.tar.xz seaweedfs-b8d3f11e92b1c7efe6f208452a349be9e085783b.zip | |
Merge pull request #1945 from bash99/master
Add a snowflake sequencer as more robust fid generator, but less compressable than small auto-inc id
Diffstat (limited to 'weed/sequence')
| -rw-r--r-- | weed/sequence/snowflake_sequencer.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/weed/sequence/snowflake_sequencer.go b/weed/sequence/snowflake_sequencer.go new file mode 100644 index 000000000..679588120 --- /dev/null +++ b/weed/sequence/snowflake_sequencer.go @@ -0,0 +1,46 @@ +package sequence + +import ( + "fmt" + "hash/fnv" + + "github.com/bwmarrin/snowflake" + "github.com/chrislusf/seaweedfs/weed/glog" +) + +// a simple snowflake Sequencer +type SnowflakeSequencer struct { + node *snowflake.Node +} + +func NewSnowflakeSequencer(nodeid string) (*SnowflakeSequencer, error) { + nodeid_hash := hash(nodeid) & 0x3ff + glog.V(0).Infof("use snowfalke seq id generator, nodeid:%s hex_of_nodeid: %x", nodeid, nodeid_hash) + node, err := snowflake.NewNode(int64(nodeid_hash)) + 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()) +} |
