forked from blnkfinance/blnk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.go
More file actions
343 lines (307 loc) · 11.5 KB
/
Copy pathqueue.go
File metadata and controls
343 lines (307 loc) · 11.5 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
Copyright 2024 Blnk Finance Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package blnk
import (
"context"
"encoding/json"
"fmt"
"hash/fnv"
"time"
"github.com/blnkfinance/blnk/config"
"github.com/blnkfinance/blnk/internal/hotpairs"
"github.com/blnkfinance/blnk/internal/metrics"
redis_db "github.com/blnkfinance/blnk/internal/redis-db"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
otelmetric "go.opentelemetry.io/otel/metric"
"github.com/blnkfinance/blnk/model"
"github.com/hibiken/asynq"
)
// Queue represents a queue for handling various tasks.
type Queue struct {
Client *asynq.Client
Inspector *asynq.Inspector
config *config.Configuration
}
// TransactionTypePayload represents the payload for a transaction type.
type TransactionTypePayload struct {
Data model.Transaction
}
// NewQueue initializes a new Queue instance with the provided configuration.
//
// Parameters:
// - conf *config.Configuration: The configuration for the queue.
//
// Returns:
// - *Queue: A pointer to the newly created Queue instance.
func NewQueue(conf *config.Configuration, client *asynq.Client) *Queue {
redisOption, err := redis_db.ParseRedisURL(conf.Redis.Dns, conf.Redis.SkipTLSVerify)
if err != nil {
logrus.WithError(err).Fatal("failed to parse Redis URL")
}
queueOptions := asynq.RedisClientOpt{Addr: redisOption.Addr, Password: redisOption.Password, DB: redisOption.DB, TLSConfig: redisOption.TLSConfig, PoolSize: conf.Redis.PoolSize}
inspector := asynq.NewInspector(queueOptions)
return &Queue{
Client: client,
Inspector: inspector,
config: conf,
}
}
// queueInflightExpiry enqueues a task to handle inflight expiry for a transaction.
//
// Parameters:
// - transactionID string: The ID of the transaction.
// - expiresAt time.Time: The expiration time for the inflight status.
//
// Returns:
// - error: An error if the task could not be enqueued.
func (q *Queue) queueInflightExpiry(transactionID string, expiresAt time.Time) error {
IPayload, err := json.Marshal(transactionID)
if err != nil {
return err
}
taskOptions := []asynq.Option{
asynq.TaskID(transactionID),
asynq.Queue(q.config.Queue.InflightExpiryQueue),
asynq.ProcessIn(time.Until(expiresAt)),
}
task := asynq.NewTask(q.config.Queue.InflightExpiryQueue, IPayload, taskOptions...)
_, err = q.Client.Enqueue(task)
if err != nil {
logrus.WithError(err).WithField("transaction_id", transactionID).Error("failed to enqueue inflight expiry")
return err
}
logrus.WithField("transaction_id", transactionID).Debug("successfully enqueued inflight expiry")
return nil
}
// queueIndexBatch enqueues a batch of items to be indexed in dependency order.
// This ensures that dependencies (e.g., balances) are indexed before the primary item (e.g., transaction).
// Uses the same IndexQueue but with a different task type for routing.
//
// Parameters:
// - batch interface{}: The batch containing dependencies and primary item to index.
//
// Returns:
// - error: An error if the task could not be enqueued.
func (q *Queue) queueIndexBatch(batch interface{}) error {
if q.config.TypeSense.Dns == "" {
return nil
}
payload, err := json.Marshal(batch)
if err != nil {
return err
}
taskOptions := []asynq.Option{asynq.Queue(q.config.Queue.IndexQueue)}
task := asynq.NewTask("new:index:batch", payload, taskOptions...)
_, err = q.Client.Enqueue(task)
if err != nil {
logrus.WithError(err).Error("failed to enqueue index batch")
return err
}
logrus.Debug("successfully enqueued index batch")
return nil
}
// queueIndexData enqueues a task to index data in a specified collection.
//
// Parameters:
// - id string: The ID of the data to index.
// - collection string: The name of the collection to index the data in.
// - data interface{}: The data to be indexed.
//
// Returns:
// - error: An error if the task could not be enqueued.
func (q *Queue) queueIndexData(id string, collection string, data interface{}) error {
if q.config.TypeSense.Dns == "" {
return nil
}
payload := map[string]interface{}{
"collection": collection,
"payload": data,
}
IPayload, err := json.Marshal(payload)
if err != nil {
return err
}
taskOptions := []asynq.Option{asynq.Queue(q.config.Queue.IndexQueue)}
task := asynq.NewTask(q.config.Queue.IndexQueue, IPayload, taskOptions...)
_, err = q.Client.Enqueue(task)
if err != nil {
logrus.WithError(err).WithField("id", id).Error("failed to enqueue index data")
return err
}
logrus.WithField("id", id).Debug("successfully enqueued index data")
return nil
}
// Enqueue enqueues a transaction to the Redis queue.
//
// Parameters:
// - ctx context.Context: The context for the operation.
// - transaction *model.Transaction: The transaction to be enqueued.
//
// Returns:
// - error: An error if the transaction could not be enqueued.
func (q *Queue) Enqueue(ctx context.Context, transaction *model.Transaction) error {
ctx, span := tracer.Start(ctx, "Adding Transaction To Redis Queue")
defer span.End()
payload, err := json.Marshal(transaction)
if err != nil {
return err
}
task := q.geTask(transaction, payload)
_, err = q.Client.EnqueueContext(ctx, task, asynq.MaxRetry(q.config.Queue.MaxRetryAttempts))
if err != nil {
logrus.WithError(err).WithField("reference", transaction.Reference).Error("failed to enqueue transaction")
return err
}
logrus.WithField("reference", transaction.Reference).Debug("successfully enqueued transaction")
// Record enqueue metrics.
metrics.QueueEnqueuedTotal.Add(ctx, 1,
otelmetric.WithAttributes(attribute.String("queue_name", task.Type())),
)
return nil
}
// QueueInflightExpiry handles queuing a transaction for inflight expiration.
// This method is separate from the main Enqueue to ensure expiration is handled
// regardless of whether the transaction is queued or processed immediately.
//
// Parameters:
// - ctx context.Context: The context for the operation.
// - transaction *model.Transaction: The transaction to queue for expiration.
//
// Returns:
// - error: An error if the expiration could not be queued.
func (q *Queue) QueueInflightExpiry(ctx context.Context, transaction *model.Transaction) error {
if !transaction.InflightExpiryDate.IsZero() {
return q.queueInflightExpiry(transaction.TransactionID, transaction.InflightExpiryDate)
}
return nil
}
// geTask generates a task for a transaction and assigns it to a specific queue based on the balance ID.
// It ensures that transactions are evenly distributed across multiple queues by hashing the balance ID.
// This approach helps to avoid race conditions on a balance by ensuring that all transactions related to the same balance
// are processed serially within the same queue, thereby maintaining accuracy and consistency.
//
// Parameters:
// - transaction *model.Transaction: The transaction for which to generate the task.
// - payload []byte: The payload for the task, typically the serialized transaction data.
//
// Returns:
// - *asynq.Task: The generated task ready to be enqueued.
func (q *Queue) geTask(transaction *model.Transaction, payload []byte) *asynq.Task {
queueName := q.transactionQueueName(transaction)
taskOptions := []asynq.Option{asynq.TaskID(transaction.TransactionID), asynq.Queue(queueName)}
if !transaction.ScheduledFor.IsZero() {
taskOptions = append(taskOptions, asynq.ProcessIn(time.Until(transaction.ScheduledFor)))
}
return asynq.NewTask(queueName, payload, taskOptions...)
}
func (q *Queue) transactionQueueName(transaction *model.Transaction) string {
if q.config.Queue.EnableHotLane && transaction != nil && transaction.MetaData != nil {
if hotpairs.QueueLaneFromMetadata(transaction.MetaData) == hotpairs.LaneHot {
metrics.HotpairsLaneRoutedTotal.Add(context.Background(), 1,
otelmetric.WithAttributes(attribute.String("lane", "hot")),
)
return q.config.Queue.HotQueueName
}
}
metrics.HotpairsLaneRoutedTotal.Add(context.Background(), 1,
otelmetric.WithAttributes(attribute.String("lane", "normal")),
)
queueIndex := hashBalanceID(transaction.Source) % q.config.Queue.NumberOfQueues
return fmt.Sprintf("%s_%d", q.config.Queue.TransactionQueue, queueIndex+1)
}
// hashBalanceID returns a consistent hash value for a string balance ID.
//
// Parameters:
// - balanceID string: The balance ID to hash.
//
// Returns:
// - int: The hash value of the balance ID.
func hashBalanceID(balanceID string) int {
hasher := fnv.New32a()
_, _ = hasher.Write([]byte(balanceID))
return int(hasher.Sum32())
}
// GetTransactionFromQueue retrieves a transaction from the queue by its ID.
//
// Parameters:
// - transactionID string: The ID of the transaction to retrieve.
//
// Returns:
// - *model.Transaction: A pointer to the Transaction model if found.
// - error: An error if the transaction could not be retrieved.
func (q *Queue) GetTransactionFromQueue(transactionID string) (*model.Transaction, error) {
for i := 1; i <= q.config.Queue.NumberOfQueues; i++ {
queueName := fmt.Sprintf("%s_%d", q.config.Queue.TransactionQueue, i)
task, err := q.Inspector.GetTaskInfo(queueName, transactionID)
if err == nil && task != nil {
var txn model.Transaction
if err := json.Unmarshal(task.Payload, &txn); err != nil {
return nil, err
}
return &txn, nil
}
}
if q.config.Queue.EnableHotLane {
task, err := q.Inspector.GetTaskInfo(q.config.Queue.HotQueueName, transactionID)
if err == nil && task != nil {
var txn model.Transaction
if err := json.Unmarshal(task.Payload, &txn); err != nil {
return nil, err
}
return &txn, nil
}
}
return nil, nil // Return nil if transaction is not found in any queue
}
// queueInflightCommit enqueues a task to handle inflight commit for a transaction.
//
// Parameters:
// - transactionID string: The ID of the transaction.
// - commitAt time.Time: The scheduled time to automatically commit the inflight transaction.
//
// Returns:
// - error: An error if the task could not be enqueued.
func (q *Queue) queueInflightCommit(transactionID string, commitAt time.Time) error {
IPayload, err := json.Marshal(transactionID)
if err != nil {
return err
}
taskOptions := []asynq.Option{
asynq.TaskID(transactionID),
asynq.Queue(q.config.Queue.InflightCommitQueue),
asynq.ProcessIn(time.Until(commitAt)),
}
task := asynq.NewTask(q.config.Queue.InflightCommitQueue, IPayload, taskOptions...)
_, err = q.Client.Enqueue(task)
if err != nil {
logrus.WithError(err).WithField("transaction_id", transactionID).Error("failed to enqueue inflight commit")
return err
}
logrus.WithField("transaction_id", transactionID).Debug("successfully enqueued inflight commit")
return nil
}
// QueueInflightCommit schedules an automatic commit for an inflight transaction at the specified date.
//
// Parameters:
// - ctx context.Context: The context for the operation.
// - transaction *model.Transaction: The transaction to be committed automatically.
//
// Returns:
// - error: An error if the task could not be enqueued.
func (q *Queue) QueueInflightCommit(ctx context.Context, transaction *model.Transaction) error {
if !transaction.InflightCommitDate.IsZero() {
return q.queueInflightCommit(transaction.TransactionID, transaction.InflightCommitDate)
}
return nil
}