-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition.go
130 lines (98 loc) · 2.99 KB
/
partition.go
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
package wkafka
import (
"context"
"sync"
"github.com/twmb/franz-go/pkg/kgo"
)
type partitionHandler struct {
logger Logger
mapPartitionsRevoked map[string][]int32
mapPartitionsLost map[string][]int32
mutex sync.RWMutex
}
// Flush is used to flush the partition handler and it will be ready next poll.
func (h *partitionHandler) Flush() {
h.mutex.Lock()
defer h.mutex.Unlock()
h.mapPartitionsRevoked = nil
h.mapPartitionsLost = nil
}
func (h *partitionHandler) AddPartitionsRevoked(mapPartitions map[string][]int32) {
h.mutex.Lock()
defer h.mutex.Unlock()
if h.mapPartitionsRevoked == nil {
h.mapPartitionsRevoked = make(map[string][]int32, len(mapPartitions))
}
for topic, partition := range mapPartitions {
h.mapPartitionsRevoked[topic] = partition
}
}
func (h *partitionHandler) AddPartitionsLost(mapPartitions map[string][]int32) {
h.mutex.Lock()
defer h.mutex.Unlock()
if h.mapPartitionsLost == nil {
h.mapPartitionsLost = make(map[string][]int32, len(mapPartitions))
}
for topic, partition := range mapPartitions {
h.mapPartitionsLost[topic] = partition
}
}
func (h *partitionHandler) IsRevokedRecord(r *Record) bool {
h.mutex.RLock()
defer h.mutex.RUnlock()
if len(h.mapPartitionsRevoked) == 0 {
return false
}
if _, ok := h.mapPartitionsRevoked[r.Topic]; !ok {
return false
}
for _, partition := range h.mapPartitionsRevoked[r.Topic] {
if partition == r.Partition {
return true
}
}
return false
}
// IsRevokedRecordBatch is used to check if the record is revoked.
// - If the record is revoked, it will be skipped and returns just valid records.
func (h *partitionHandler) IsRevokedRecordBatch(records []*Record) ([]*Record, bool) {
h.mutex.RLock()
defer h.mutex.RUnlock()
if len(h.mapPartitionsRevoked) == 0 {
return records, false
}
validRecords := make([]*Record, 0, len(records))
for _, r := range records {
if _, ok := h.mapPartitionsRevoked[r.Topic]; !ok {
validRecords = append(validRecords, r)
continue
}
for _, partition := range h.mapPartitionsRevoked[r.Topic] {
if partition == r.Partition {
continue
}
validRecords = append(validRecords, r)
}
}
return validRecords, len(validRecords) != len(records)
}
func partitionLost(h *partitionHandler, fn func(...OptionDLQTriggerFn)) func(context.Context, *kgo.Client, map[string][]int32) {
return func(ctx context.Context, cl *kgo.Client, partitions map[string][]int32) {
if len(partitions) == 0 {
return
}
h.logger.Debug("partition lost", "partitions", partitions)
h.AddPartitionsLost(partitions)
fn(WithDLQTriggerSpecPartitions(partitions))
}
}
func partitionRevoked(h *partitionHandler, fn func(...OptionDLQTriggerFn)) func(context.Context, *kgo.Client, map[string][]int32) {
return func(ctx context.Context, cl *kgo.Client, partitions map[string][]int32) {
if len(partitions) == 0 {
return
}
h.logger.Debug("partition revoked", "partitions", partitions)
h.AddPartitionsRevoked(partitions)
fn(WithDLQTriggerSpecPartitions(partitions))
}
}