1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
package transactions
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config"
"gitlab.com/gitlab-org/gitaly/v14/internal/transaction/voting"
)
var ErrNotFound = errors.New("transaction not found")
// Manager handles reference transactions for Praefect. It is required in order
// for Praefect to handle transactions directly instead of having to reach out
// to reference transaction RPCs.
type Manager struct {
idSequence uint64
lock sync.Mutex
transactions map[uint64]*transaction
counterMetric *prometheus.CounterVec
delayMetric *prometheus.HistogramVec
subtransactionsMetric prometheus.Histogram
}
// NewManager creates a new transactions Manager.
func NewManager(cfg config.Config) *Manager {
return &Manager{
transactions: make(map[uint64]*transaction),
counterMetric: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "gitaly",
Subsystem: "praefect",
Name: "transactions_total",
Help: "Total number of transaction actions",
},
[]string{"action"},
),
delayMetric: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "gitaly",
Subsystem: "praefect",
Name: "transactions_delay_seconds",
Help: "Delay between casting a vote and reaching quorum",
Buckets: cfg.Prometheus.GRPCLatencyBuckets,
},
[]string{"action"},
),
subtransactionsMetric: prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "gitaly_praefect_subtransactions_per_transaction_total",
Help: "The number of subtransactions created for a single registered transaction",
Buckets: []float64{0.0, 1.0, 2.0, 4.0, 8.0, 16.0, 32.0},
},
),
}
}
func (mgr *Manager) Describe(descs chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(mgr, descs)
}
func (mgr *Manager) Collect(metrics chan<- prometheus.Metric) {
mgr.counterMetric.Collect(metrics)
mgr.delayMetric.Collect(metrics)
mgr.subtransactionsMetric.Collect(metrics)
}
func (mgr *Manager) log(ctx context.Context) logrus.FieldLogger {
return ctxlogrus.Extract(ctx).WithField("component", "transactions.Manager")
}
// CancelFunc is the transaction cancellation function returned by
// `RegisterTransaction`. Calling it will cause the transaction to be removed
// from the transaction manager.
type CancelFunc func() error
// RegisterTransaction registers a new reference transaction for a set of nodes
// taking part in the transaction. `threshold` is the threshold at which an
// election will succeed. It needs to be in the range `weight(voters)/2 <
// threshold <= weight(voters) to avoid indecidable votes.
func (mgr *Manager) RegisterTransaction(ctx context.Context, voters []Voter, threshold uint) (Transaction, CancelFunc, error) {
mgr.lock.Lock()
defer mgr.lock.Unlock()
transactionID := atomic.AddUint64(&mgr.idSequence, 1)
transaction, err := newTransaction(transactionID, voters, threshold)
if err != nil {
return nil, nil, err
}
if _, ok := mgr.transactions[transactionID]; ok {
return nil, nil, errors.New("transaction exists already")
}
mgr.transactions[transactionID] = transaction
mgr.log(ctx).WithFields(logrus.Fields{
"transaction.id": transactionID,
"transaction.voters": voters,
}).Debug("RegisterTransaction")
mgr.counterMetric.WithLabelValues("registered").Add(float64(len(voters)))
return transaction, func() error {
return mgr.cancelTransaction(ctx, transaction)
}, nil
}
func (mgr *Manager) cancelTransaction(ctx context.Context, transaction *transaction) error {
mgr.lock.Lock()
defer mgr.lock.Unlock()
delete(mgr.transactions, transaction.ID())
transaction.cancel()
mgr.subtransactionsMetric.Observe(float64(transaction.CountSubtransactions()))
var committed uint64
state, err := transaction.State()
if err != nil {
return err
}
for _, result := range state {
if result == VoteCommitted {
committed++
}
}
mgr.log(ctx).WithFields(logrus.Fields{
"transaction.id": transaction.ID(),
"transaction.committed": fmt.Sprintf("%d/%d", committed, len(state)),
"transaction.subtransactions": transaction.CountSubtransactions(),
}).Info("transaction completed")
return nil
}
func (mgr *Manager) voteTransaction(ctx context.Context, transactionID uint64, node string, vote voting.Vote) error {
mgr.lock.Lock()
transaction, ok := mgr.transactions[transactionID]
mgr.lock.Unlock()
if !ok {
return fmt.Errorf("%w: %d", ErrNotFound, transactionID)
}
if err := transaction.vote(ctx, node, vote); err != nil {
return err
}
return nil
}
// VoteTransaction is called by a client who's casting a vote on a reference
// transaction. It waits until quorum was reached on the given transaction.
func (mgr *Manager) VoteTransaction(ctx context.Context, transactionID uint64, node string, vote voting.Vote) error {
start := time.Now()
defer func() {
delay := time.Since(start)
mgr.delayMetric.WithLabelValues("vote").Observe(delay.Seconds())
}()
logger := mgr.log(ctx).WithFields(logrus.Fields{
"transaction.id": transactionID,
"transaction.voter": node,
"transaction.hash": vote.String(),
})
mgr.counterMetric.WithLabelValues("started").Inc()
logger.Debug("VoteTransaction")
if err := mgr.voteTransaction(ctx, transactionID, node, vote); err != nil {
var counterLabel string
if errors.Is(err, ErrTransactionStopped) {
counterLabel = "stopped"
// Stopped transactions indicate a graceful
// termination, so we should not log an error here.
} else if errors.Is(err, ErrTransactionFailed) {
counterLabel = "failed"
logger.WithError(err).Error("VoteTransaction: did not reach quorum")
} else if errors.Is(err, ErrTransactionCanceled) {
counterLabel = "canceled"
logger.WithError(err).Error("VoteTransaction: transaction was canceled")
} else {
counterLabel = "invalid"
logger.WithError(err).Error("VoteTransaction: failure")
}
mgr.counterMetric.WithLabelValues(counterLabel).Inc()
return err
}
logger.Info("VoteTransaction: transaction committed")
mgr.counterMetric.WithLabelValues("committed").Inc()
return nil
}
// StopTransaction will gracefully stop a transaction.
func (mgr *Manager) StopTransaction(ctx context.Context, transactionID uint64) error {
mgr.lock.Lock()
transaction, ok := mgr.transactions[transactionID]
mgr.lock.Unlock()
if !ok {
return fmt.Errorf("%w: %d", ErrNotFound, transactionID)
}
if err := transaction.stop(); err != nil {
return err
}
mgr.log(ctx).WithFields(logrus.Fields{
"transaction.id": transactionID,
}).Debug("VoteTransaction: transaction stopped")
mgr.counterMetric.WithLabelValues("stopped").Inc()
return nil
}
|