forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dedupe.go
195 lines (163 loc) · 5.65 KB
/
dedupe.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
// Copyright (c) 2018 Lorenzo Alberton
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package processor
import (
"bytes"
"fmt"
"strconv"
"github.com/OneOfOne/xxhash"
"github.com/Jeffail/benthos/lib/types"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
)
//------------------------------------------------------------------------------
func init() {
Constructors["dedupe"] = TypeSpec{
constructor: NewDedupe,
description: `
Dedupes messages by caching selected (and optionally hashed) parts, dropping
messages that are already cached. The hash type can be chosen from: none or
xxhash (more will come soon).
Caches should be configured as a resource, for more information check out the
[documentation here](../caches).`,
}
}
//------------------------------------------------------------------------------
// DedupeConfig contains any configuration for the Dedupe processor.
type DedupeConfig struct {
Cache string `json:"cache" yaml:"cache"`
HashType string `json:"hash" yaml:"hash"`
Parts []int `json:"parts" yaml:"parts"` // message parts to hash
DropOnCacheErr bool `json:"drop_on_err" yaml:"drop_on_err"`
}
// NewDedupeConfig returns a DedupeConfig with default values.
func NewDedupeConfig() DedupeConfig {
return DedupeConfig{
Cache: "",
HashType: "none",
Parts: []int{0}, // only consider the 1st part
DropOnCacheErr: true,
}
}
//------------------------------------------------------------------------------
type hasher interface {
Write(str []byte) (int, error)
Bytes() []byte
}
type hasherFunc func() hasher
//------------------------------------------------------------------------------
type xxhashHasher struct {
h *xxhash.XXHash64
}
func (x *xxhashHasher) Write(str []byte) (int, error) {
return x.h.Write(str)
}
func (x *xxhashHasher) Bytes() []byte {
return []byte(strconv.FormatUint(x.h.Sum64(), 10))
}
//------------------------------------------------------------------------------
func strToHasher(str string) (hasherFunc, error) {
switch str {
case "none":
return func() hasher {
return bytes.NewBuffer(nil)
}, nil
case "xxhash":
return func() hasher {
return &xxhashHasher{
h: xxhash.New64(),
}
}, nil
}
return nil, fmt.Errorf("hash type not recognised: %v", str)
}
//------------------------------------------------------------------------------
// Dedupe is a processor that hashes each message and checks if the has is already
// present in the cache
type Dedupe struct {
conf Config
log log.Modular
stats metrics.Type
cache types.Cache
hasherFunc hasherFunc
}
// NewDedupe returns a Dedupe processor.
func NewDedupe(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
c, err := mgr.GetCache(conf.Dedupe.Cache)
if err != nil {
return nil, err
}
hFunc, err := strToHasher(conf.Dedupe.HashType)
if err != nil {
return nil, err
}
return &Dedupe{
conf: conf,
log: log.NewModule(".processor.dedupe"),
stats: stats,
cache: c,
hasherFunc: hFunc,
}, nil
}
//------------------------------------------------------------------------------
// ProcessMessage checks each message against a set of bounds.
func (d *Dedupe) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
d.stats.Incr("processor.dedupe.count", 1)
hasher := d.hasherFunc()
lParts := msg.Len()
for _, index := range d.conf.Dedupe.Parts {
if index < 0 {
// Negative indexes count backwards from the end.
index = lParts + index
}
// Check boundary of part index.
if index < 0 || index >= lParts {
d.stats.Incr("processor.dedupe.dropped_part_out_of_bounds", 1)
d.stats.Incr("processor.dedupe.dropped", 1)
return nil, types.NewSimpleResponse(nil)
}
// Attempt to add part to hash.
if _, err := hasher.Write(msg.Get(index)); nil != err {
d.stats.Incr("processor.dedupe.error.hash", 1)
d.stats.Incr("processor.dedupe.dropped", 1)
d.log.Errorf("Hash error: %v\n", err)
return nil, types.NewSimpleResponse(nil)
}
}
if err := d.cache.Add(string(hasher.Bytes()), []byte{'t'}); err != nil {
if err != types.ErrKeyAlreadyExists {
d.stats.Incr("processor.dedupe.error.cache", 1)
d.log.Errorf("Cache error: %v\n", err)
if d.conf.Dedupe.DropOnCacheErr {
d.stats.Incr("processor.dedupe.dropped", 1)
return nil, types.NewSimpleResponse(nil)
}
} else {
d.stats.Incr("processor.dedupe.dropped", 1)
return nil, types.NewSimpleResponse(nil)
}
}
d.stats.Incr("processor.dedupe.sent", 1)
msgs := [1]types.Message{msg}
return msgs[:], nil
}
//------------------------------------------------------------------------------