Skip to content

Commit

Permalink
wrap the functions
Browse files Browse the repository at this point in the history
  • Loading branch information
all-seeing-code committed Feb 7, 2023
1 parent df1ad84 commit dfdfb1e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
12 changes: 3 additions & 9 deletions dgraph/cmd/zero/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package zero

import (
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"log"
Expand Down Expand Up @@ -88,16 +87,11 @@ func (n *node) AmLeader() bool {
// {2 bytes Node ID} {4 bytes for random} {2 bytes zero}
func (n *node) initProposalKey(id uint64) error {
x.AssertTrue(id != 0)
random4Bytes := make([]byte, 4)
if _, err := rand.Read(random4Bytes); err != nil {
var err error
proposalKey, err = x.ProposalKey(n.Id)
if err != nil {
return err
}
proposalKey = n.Id<<48 | uint64(binary.BigEndian.Uint32(random4Bytes))<<16
// We want to avoid spillage to node id in case of overflow. For instance, if the
// random bytes end up being [xx,xx, 255, 255, 255, 255, 0 , 0] (xx, xx being the node id)
// we would spill to node id after 65535 calls to unique key.
// So by setting 48th bit to 0 we ensure that we never spill out to node ids.
proposalKey &= ^(uint64(1) << 47)
return nil
}

Expand Down
7 changes: 3 additions & 4 deletions worker/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package worker

import (
"context"
"crypto/rand"
"encoding/binary"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -113,11 +112,11 @@ var proposalKey uint64
// {2 bytes Node ID} {4 bytes for random} {2 bytes zero}
func initProposalKey(id uint64) error {
x.AssertTrue(id != 0)
b := make([]byte, 8)
if _, err := rand.Read(b); err != nil {
var err error
proposalKey, err = x.ProposalKey(groups().Node.Id)
if err != nil {
return err
}
proposalKey = groups().Node.Id<<48 | binary.BigEndian.Uint64(b)<<16
return nil
}

Expand Down
18 changes: 17 additions & 1 deletion x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import (
"bytes"
builtinGzip "compress/gzip"
"context"
"crypto/rand"
"crypto/tls"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"math"
"math/rand"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -644,6 +645,21 @@ func RetryUntilSuccess(maxRetries int, waitAfterFailure time.Duration,
return err
}

// {2 bytes Node ID} {4 bytes for random} {2 bytes zero}
func ProposalKey(id uint64) (uint64, error) {
random4Bytes := make([]byte, 4)
if _, err := rand.Read(random4Bytes); err != nil {
return 0, err
}
proposalKey := id<<48 | uint64(binary.BigEndian.Uint32(random4Bytes))<<16
// We want to avoid spillage to node id in case of overflow. For instance, if the
// random bytes end up being [xx,xx, 255, 255, 255, 255, 0 , 0] (xx, xx being the node id)
// we would spill to node id after 65535 calls to unique key.
// So by setting 48th bit to 0 we ensure that we never spill out to node ids.
proposalKey &= ^(uint64(1) << 47)
return proposalKey, nil
}

// HasString returns whether the slice contains the given string.
func HasString(a []string, b string) bool {
for _, k := range a {
Expand Down

0 comments on commit dfdfb1e

Please sign in to comment.