Skip to content

Commit 595a103

Browse files
committed
[common] Acquire machine-id for Sonyflake generator
1 parent 4ed8b75 commit 595a103

3 files changed

Lines changed: 48 additions & 5 deletions

File tree

common/utils/uid/uid.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,60 @@
2525
package uid
2626

2727
import (
28+
"encoding/binary"
2829
"time"
2930

3031
"github.com/AliceO2Group/Control/common/logger"
32+
"github.com/denisbrodbeck/machineid"
3133
"github.com/osamingo/indigo"
34+
"github.com/pborman/uuid"
3235
"github.com/rs/xid"
3336
"github.com/sirupsen/logrus"
3437
)
3538

39+
type ID string
3640
var (
3741
log = logger.New(logrus.StandardLogger(),"utils")
38-
uidGen = indigo.New(nil, indigo.StartTime(
39-
time.Unix(1257894000, 0)),
40-
indigo.MachineID(func() (uint16, error){return 42, nil}))
42+
uidGen *indigo.Generator
4143
)
4244

43-
type ID string
45+
46+
func init() {
47+
// In order to correctly seed ID generation and ensure that all generated IDs
48+
// are reasonably unique to a given machine, we need to provide a uint16
49+
// that represents the current machine.
50+
// By default, Sonyflake/Indigo attempt to acquire such an ID from the machine's
51+
// private IP address, but this doesn't always work (e.g. on some Docker
52+
// instances).
53+
// So we use denisbrodbeck/machineid to read in the standard machine-id in a
54+
// cross-platform way. On CentOS and similar Linuxes, this means that we read
55+
// the file /etc/machine-id.
56+
// This file contains a standard UUID as string, so we need to parse it into
57+
// a []byte, and fetch 2 of these bytes to generate the uint16 ID. If this
58+
// fails, the uint16 ID defaults to 42.
59+
var machineId uint16 = 42
60+
61+
id, err := machineid.ID()
62+
if err == nil {
63+
parsed := uuid.Parse(id)
64+
if parsed != nil {
65+
// The NodeID consists of the last 6 bytes of the full UUID.
66+
// We use the first 2 bytes of this 6-byte block instead of the full
67+
// uuid.Array because the first 10 bits are clock-dependent.
68+
array := parsed.NodeID()
69+
machineId = binary.BigEndian.Uint16(array[0:2])
70+
}
71+
} else {
72+
id = "<not available>"
73+
}
74+
log.Infof("machine UUID: %s generator machine ID: %d", id, machineId)
75+
76+
uidGen = indigo.New(
77+
nil,
78+
indigo.StartTime(time.Unix(1257894000, 0)), // Go epoch
79+
indigo.MachineID(func() (uint16, error){return machineId, nil}),
80+
)
81+
}
4482

4583
func (u ID) String() string {
4684
return string(u)

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/coreos/etcd v3.3.19+incompatible // indirect
2626
github.com/coreos/go-semver v0.3.0 // indirect
2727
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
28+
github.com/denisbrodbeck/machineid v1.0.1
2829
github.com/fatih/color v1.9.0
2930
github.com/fsnotify/fsnotify v1.4.9 // indirect
3031
github.com/gdamore/tcell v1.3.0
@@ -99,7 +100,7 @@ require (
99100
go.uber.org/zap v1.14.1 // indirect
100101
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
101102
golang.org/x/net v0.0.0-20200707034311-ab3426394381
102-
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642
103+
golang.org/x/sys v0.0.0-20201014080544-cc95f250f6bc
103104
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
104105
google.golang.org/grpc v1.31.0
105106
google.golang.org/grpc/examples v0.0.0-20200826230536-d31b6710005d // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
151151
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
152152
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
153153
github.com/deckarep/golang-set v1.7.1/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
154+
github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ=
155+
github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI=
154156
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
155157
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
156158
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
@@ -854,6 +856,8 @@ golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zr
854856
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
855857
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 h1:B6caxRw+hozq68X2MY7jEpZh/cr4/aHLv9xU8Kkadrw=
856858
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
859+
golang.org/x/sys v0.0.0-20201014080544-cc95f250f6bc h1:HVFDs9bKvTxP6bh1Rj9MCSo+UmafQtI8ZWDPVwVk9g4=
860+
golang.org/x/sys v0.0.0-20201014080544-cc95f250f6bc/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
857861
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
858862
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
859863
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)