-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregator_addr.go
269 lines (240 loc) · 6.86 KB
/
aggregator_addr.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
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
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net"
"sync"
"time"
uuid "github.com/google/uuid"
"github.com/linkedin/goavro/v2"
)
const (
schemaService = "ADS"
schemaSignal = "ANON_IP_ADDRS"
)
// The Avro codec that we use to encode data before sending it to Kafka.
var ourCodec = func() *goavro.Codec {
codec, err := goavro.NewCodec(`{
"type": "record",
"name": "DefaultMessage",
"fields": [
{ "name": "wallet_id", "type": "string" },
{ "name": "service", "type": "string" },
{ "name": "signal", "type": "string" },
{ "name": "score", "type": "int" },
{ "name": "justification", "type": "string" },
{ "name": "created_at", "type": "string" }
]}`)
if err != nil {
l.Fatalf("Failed to create Avro codec: %v", err)
}
return codec
}()
type kafkaMessage struct {
WalletID string `json:"wallet_id"`
Service string `json:"service"`
Signal string `json:"signal"`
Score int32 `json:"score"`
Justification string `json:"justification"`
CreatedAt string `json:"created_at"`
}
// addrAggregator implements an aggregator that keeps track of tokenized IP
// addresses and their respective meta data.
type addrAggregator struct {
sync.RWMutex
wg sync.WaitGroup
fwdInterval time.Duration
keyExpiry time.Duration
addrs WalletsByKeyID
tokenizer tokenizer
inbox chan serializer
outbox chan token
done chan empty
}
// newAddrAggregator returns a new address aggregator.
func newAddrAggregator() aggregator {
return &addrAggregator{
done: make(chan empty),
addrs: make(WalletsByKeyID),
}
}
// setConfig sets the given configuration.
func (a *addrAggregator) setConfig(c *config) {
a.Lock()
defer a.Unlock()
a.fwdInterval = c.fwdInterval
a.keyExpiry = c.keyExpiry
l.Printf("Forward interval: %s, key expiry: %s", a.fwdInterval, a.keyExpiry)
}
// use sets the tokenizer that must be used.
func (a *addrAggregator) use(t tokenizer) {
a.Lock()
defer a.Unlock()
a.tokenizer = t
}
// connect sets the inbox to retrieve serialized data from and the outbox to
// send tokens to.
func (a *addrAggregator) connect(inbox chan serializer, outbox chan token) {
a.Lock()
defer a.Unlock()
a.inbox = inbox
a.outbox = outbox
}
// start starts the address aggregator.
func (a *addrAggregator) start() {
if err := a.tokenizer.resetKey(); err != nil {
l.Fatalf("Failed to reset tokenizer key: %v", err)
}
a.wg.Add(1)
go func() {
defer a.wg.Done()
a.RLock() // Protect read of fwdInterval and keyExpiry.
fwdTicker := time.NewTicker(a.fwdInterval)
keyTicker := time.NewTicker(a.keyExpiry)
a.RUnlock()
l.Println("Starting address aggregator loop.")
for {
select {
case <-a.done:
return
case <-fwdTicker.C:
if err := a.flush(); err != nil {
l.Printf("Failed to forward addresses: %v", err)
}
case <-keyTicker.C:
if err := a.tokenizer.resetKey(); err != nil {
l.Fatalf("Failed to reset tokenizer key: %v", err)
}
case req := <-a.inbox:
switch v := req.(type) {
case *clientRequest:
if err := a.processRequest(v); err != nil {
l.Printf("Failed to process client request: %v", err)
}
l.Printf("Processed request for wallet %s.", v.Wallet)
default:
// We are not prepared to process whatever data structure
// we were given. Simply tokenize it and forward it right
// away, without aggregation.
t, err := a.tokenizer.tokenize(v)
if err != nil {
l.Printf("Failed to tokenize blob: %v", err)
}
a.outbox <- t
l.Println("Type not supported. Forwarded.")
}
}
}
}()
}
// stop stops the address aggregator.
func (a *addrAggregator) stop() {
close(a.done)
a.wg.Wait()
l.Println("Stopped address aggregator.")
}
// processRequest processes an incoming client request.
func (a *addrAggregator) processRequest(req *clientRequest) error {
a.Lock()
defer a.Unlock()
// Update metrics when we're done processing the request.
defer func() {
m.numWallets.Set(float64(a.addrs.numWallets()))
m.numAddrs.Set(float64(a.addrs.numAddrs()))
}()
rawToken, keyID, err := a.tokenizer.tokenizeAndKeyID(req)
if err != nil {
return err
}
// The tokenized IP address may not be printable, so let's encode it.
token := base64.StdEncoding.EncodeToString(rawToken)
// If we're using a tokenizer that preserves the blob's length, we turn the
// byte slice back into an IP address.
if a.tokenizer.preservesLen() {
if len(rawToken) != net.IPv4len && len(rawToken) != net.IPv6len {
return errors.New("token is neither of length IPv4 nor IPv6")
}
token = net.IP(rawToken).String()
}
wallets, exists := a.addrs[*keyID]
if !exists {
// We're starting a new key ID epoch.
wallets := make(AddrsByWallet)
wallets[req.Wallet] = AddressSet{
token: empty{},
}
a.addrs[*keyID] = wallets
} else {
// We're adding to the existing epoch.
addrSet, exists := wallets[req.Wallet]
if !exists {
// We have no addresses for the given wallet yet. Create a new
// address set.
wallets[req.Wallet] = AddressSet{
token: empty{},
}
} else {
// Add address to the given wallet's address set.
addrSet[token] = empty{}
}
}
return nil
}
// compileKafkaMsg turns the given arguments into a byte slice that's ready to
// be sent to our Kafka cluster.
func compileKafkaMsg(keyID keyID, walletID uuid.UUID, addrs AddressSet) ([]byte, error) {
// We're abusing our schema's justification field by storing JSON in it.
// While not elegant, this lets us ingest anonymized IP addresses without
// modifying the schema.
justification := struct {
KeyID uuid.UUID `json:"keyid"`
Addrs []string `json:"addrs"`
}{
KeyID: keyID.UUID,
}
justification.Addrs = append(justification.Addrs, addrs.sorted()...)
jsonBytes, err := json.Marshal(justification)
if err != nil {
return nil, err
}
// Populate the remaining schema fields and turn it into JSON.
msg := kafkaMessage{
WalletID: walletID.String(),
Service: schemaService,
Signal: schemaSignal,
Justification: string(jsonBytes),
CreatedAt: time.Now().UTC().Format(time.RFC3339),
}
jsonBytes, err = json.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("failed to marshal Kafka message: %w", err)
}
return avroEncode(ourCodec, jsonBytes)
}
// flush flushes the aggregator's addresses to the outbox.
func (a *addrAggregator) flush() error {
a.Lock()
defer a.Unlock()
if len(a.addrs) == 0 {
return nil
}
for keyID, wallets := range a.addrs {
totalAddrs := 0
// Compile the anonymized IP addresses that we've seen for a given
// wallet ID.
for walletID, addrSet := range wallets {
totalAddrs += len(addrSet)
kafkaMsg, err := compileKafkaMsg(keyID, walletID, addrSet)
if err != nil {
return err
}
a.outbox <- token(kafkaMsg)
}
l.Printf("Forwarded %d addresses of %d wallets using key ID %s.",
totalAddrs, len(wallets), keyID)
}
a.addrs = make(WalletsByKeyID)
return nil
}