diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-06-22 15:19:49 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-07-01 08:52:05 +0300 |
commit | f26d405744302aa52f1ed535eccc942de653d81c (patch) | |
tree | 8ccf835f0668c43f2a2ba6a33adfb856ae3acf28 /internal/praefect/transactions | |
parent | 8a3f303aeada1abf246f2fb9f59f7242ba7d77e4 (diff) |
transactions: Accept `Voter` structures on transaction registration
Currently, the transactions manager only accepts a set of node names
when registering a new transaction. With the introduction of weighted
voting, the user registering a transaction may choose to set up
additional information for each node, e.g. its weight. Let's thus expose
the `Voter` structure such that callers may set up this information.
Diffstat (limited to 'internal/praefect/transactions')
-rw-r--r-- | internal/praefect/transactions/manager.go | 6 | ||||
-rw-r--r-- | internal/praefect/transactions/transaction.go | 14 |
2 files changed, 11 insertions, 9 deletions
diff --git a/internal/praefect/transactions/manager.go b/internal/praefect/transactions/manager.go index ec4047185..721e47522 100644 --- a/internal/praefect/transactions/manager.go +++ b/internal/praefect/transactions/manager.go @@ -107,7 +107,7 @@ type CancelFunc func() // RegisterTransaction registers a new reference transaction for a set of nodes // taking part in the transaction. -func (mgr *Manager) RegisterTransaction(ctx context.Context, nodes []string) (uint64, CancelFunc, error) { +func (mgr *Manager) RegisterTransaction(ctx context.Context, voters []Voter) (uint64, CancelFunc, error) { mgr.lock.Lock() defer mgr.lock.Unlock() @@ -117,7 +117,7 @@ func (mgr *Manager) RegisterTransaction(ctx context.Context, nodes []string) (ui // nodes still have in-flight transactions. transactionID := mgr.txIDGenerator.ID() - transaction, err := newTransaction(nodes) + transaction, err := newTransaction(voters) if err != nil { return 0, nil, err } @@ -129,7 +129,7 @@ func (mgr *Manager) RegisterTransaction(ctx context.Context, nodes []string) (ui mgr.log(ctx).WithFields(logrus.Fields{ "transaction_id": transactionID, - "nodes": nodes, + "voters": voters, }).Debug("RegisterTransaction") mgr.counterMetric.WithLabelValues("registered").Inc() diff --git a/internal/praefect/transactions/transaction.go b/internal/praefect/transactions/transaction.go index 7c7fb96bb..a9744e049 100644 --- a/internal/praefect/transactions/transaction.go +++ b/internal/praefect/transactions/transaction.go @@ -32,17 +32,19 @@ type transaction struct { votersByNode map[string]*Voter } -func newTransaction(nodes []string) (*transaction, error) { - if len(nodes) == 0 { +func newTransaction(voters []Voter) (*transaction, error) { + if len(voters) == 0 { return nil, ErrMissingNodes } - votersByNode := make(map[string]*Voter, len(nodes)) - for _, node := range nodes { - if _, ok := votersByNode[node]; ok { + votersByNode := make(map[string]*Voter, len(voters)) + for _, voter := range voters { + if _, ok := votersByNode[voter.Name]; ok { return nil, ErrDuplicateNodes } - votersByNode[node] = &Voter{Name: node} + + voter := voter // rescope loop variable + votersByNode[voter.Name] = &voter } return &transaction{ |