|
| 1 | +package ext |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/md5" |
| 6 | + "encoding/json" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/signal" |
| 10 | + "syscall" |
| 11 | + "time" |
| 12 | + |
| 13 | + aero "github.com/aerospike/aerospike-client-go" |
| 14 | + "github.com/reugn/go-streams" |
| 15 | + "github.com/reugn/go-streams/flow" |
| 16 | +) |
| 17 | + |
| 18 | +// AerospikeProperties is the Aerospike connector configuration properties |
| 19 | +type AerospikeProperties struct { |
| 20 | + Policy *aero.ClientPolicy |
| 21 | + Hostname string |
| 22 | + Port int |
| 23 | + Namespase string |
| 24 | + SetName string |
| 25 | +} |
| 26 | + |
| 27 | +// ChangeNotificationProperties holds the changes polling configuration |
| 28 | +type ChangeNotificationProperties struct { |
| 29 | + PollingInterval time.Duration |
| 30 | +} |
| 31 | + |
| 32 | +// AerospikeSource connector |
| 33 | +type AerospikeSource struct { |
| 34 | + client *aero.Client |
| 35 | + records chan *aero.Result |
| 36 | + scanPolicy *aero.ScanPolicy |
| 37 | + out chan interface{} |
| 38 | + ctx context.Context |
| 39 | + properties *AerospikeProperties |
| 40 | + changeNotificationProperties *ChangeNotificationProperties |
| 41 | +} |
| 42 | + |
| 43 | +// NewAerospikeSource returns a new AerospikeSource instance |
| 44 | +// set changeNotificationProperties to nil to scan the entire namespace/set |
| 45 | +func NewAerospikeSource(ctx context.Context, |
| 46 | + properties *AerospikeProperties, |
| 47 | + scanPolicy *aero.ScanPolicy, |
| 48 | + changeNotificationProperties *ChangeNotificationProperties) (*AerospikeSource, error) { |
| 49 | + |
| 50 | + client, err := aero.NewClientWithPolicy(properties.Policy, properties.Hostname, properties.Port) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + if scanPolicy == nil { |
| 56 | + scanPolicy = aero.NewScanPolicy() |
| 57 | + } |
| 58 | + |
| 59 | + records := make(chan *aero.Result) |
| 60 | + source := &AerospikeSource{ |
| 61 | + client: client, |
| 62 | + records: records, |
| 63 | + scanPolicy: scanPolicy, |
| 64 | + out: make(chan interface{}), |
| 65 | + ctx: ctx, |
| 66 | + properties: properties, |
| 67 | + changeNotificationProperties: changeNotificationProperties, |
| 68 | + } |
| 69 | + |
| 70 | + go source.poll() |
| 71 | + go source.init() |
| 72 | + return source, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (as *AerospikeSource) poll() { |
| 76 | + if as.changeNotificationProperties == nil { |
| 77 | + // scan the entire namespace/set |
| 78 | + as.doScan() |
| 79 | + close(as.records) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + // get change notifications by polling |
| 84 | + ticker := time.NewTicker(as.changeNotificationProperties.PollingInterval) |
| 85 | +loop: |
| 86 | + for { |
| 87 | + select { |
| 88 | + case <-as.ctx.Done(): |
| 89 | + break loop |
| 90 | + case t := <-ticker.C: |
| 91 | + ts := t.UnixNano() - as.changeNotificationProperties.PollingInterval.Nanoseconds() |
| 92 | + as.scanPolicy.PredExp = []aero.PredExp{ |
| 93 | + aero.NewPredExpRecLastUpdate(), |
| 94 | + aero.NewPredExpIntegerValue(ts), |
| 95 | + aero.NewPredExpIntegerGreater(), |
| 96 | + } |
| 97 | + log.Printf("Polling records %v", as.scanPolicy.PredExp) |
| 98 | + |
| 99 | + as.doScan() |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func (as *AerospikeSource) doScan() { |
| 105 | + recordSet, err := as.client.ScanAll(as.scanPolicy, as.properties.Namespase, as.properties.SetName) |
| 106 | + if err != nil { |
| 107 | + log.Printf("Aerospike client.ScanAll failed with: %v", err) |
| 108 | + } else { |
| 109 | + for result := range recordSet.Results() { |
| 110 | + as.records <- result |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// init starts the main loop |
| 116 | +func (as *AerospikeSource) init() { |
| 117 | + sigchan := make(chan os.Signal, 1) |
| 118 | + signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM) |
| 119 | + |
| 120 | +loop: |
| 121 | + for { |
| 122 | + select { |
| 123 | + case <-sigchan: |
| 124 | + break loop |
| 125 | + case <-as.ctx.Done(): |
| 126 | + break loop |
| 127 | + case result, ok := <-as.records: |
| 128 | + if !ok { |
| 129 | + break loop |
| 130 | + } |
| 131 | + if result.Err == nil { |
| 132 | + as.out <- result.Record |
| 133 | + } else { |
| 134 | + log.Printf("Scan record error %s", result.Err) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + log.Printf("Closing Aerospike consumer") |
| 140 | + close(as.out) |
| 141 | + as.client.Close() |
| 142 | +} |
| 143 | + |
| 144 | +// Via streams data through the given flow |
| 145 | +func (as *AerospikeSource) Via(_flow streams.Flow) streams.Flow { |
| 146 | + flow.DoStream(as, _flow) |
| 147 | + return _flow |
| 148 | +} |
| 149 | + |
| 150 | +// Out returns an output channel for sending data |
| 151 | +func (as *AerospikeSource) Out() <-chan interface{} { |
| 152 | + return as.out |
| 153 | +} |
| 154 | + |
| 155 | +// AerospikeKeyBins is an Aerospike Key and BinMap container |
| 156 | +// use it to stream records to the AerospikeSink |
| 157 | +type AerospikeKeyBins struct { |
| 158 | + Key *aero.Key |
| 159 | + Bins aero.BinMap |
| 160 | +} |
| 161 | + |
| 162 | +// AerospikeSink connector |
| 163 | +type AerospikeSink struct { |
| 164 | + client *aero.Client |
| 165 | + in chan interface{} |
| 166 | + ctx context.Context |
| 167 | + properties *AerospikeProperties |
| 168 | + writePolicy *aero.WritePolicy |
| 169 | +} |
| 170 | + |
| 171 | +// NewAerospikeSink returns a new AerospikeSink instance |
| 172 | +func NewAerospikeSink(ctx context.Context, |
| 173 | + properties *AerospikeProperties, writePolicy *aero.WritePolicy) (*AerospikeSink, error) { |
| 174 | + client, err := aero.NewClientWithPolicy(properties.Policy, properties.Hostname, properties.Port) |
| 175 | + if err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + |
| 179 | + if writePolicy == nil { |
| 180 | + writePolicy = aero.NewWritePolicy(0, 0) |
| 181 | + } |
| 182 | + |
| 183 | + source := &AerospikeSink{ |
| 184 | + client: client, |
| 185 | + in: make(chan interface{}), |
| 186 | + ctx: ctx, |
| 187 | + properties: properties, |
| 188 | + writePolicy: writePolicy, |
| 189 | + } |
| 190 | + |
| 191 | + go source.init() |
| 192 | + return source, nil |
| 193 | +} |
| 194 | + |
| 195 | +// init starts the main loop |
| 196 | +func (as *AerospikeSink) init() { |
| 197 | + for msg := range as.in { |
| 198 | + switch m := msg.(type) { |
| 199 | + case AerospikeKeyBins: |
| 200 | + if err := as.client.Put(as.writePolicy, m.Key, m.Bins); err != nil { |
| 201 | + log.Printf("Aerospike client.Put failed with: %s", err) |
| 202 | + } |
| 203 | + case aero.BinMap: |
| 204 | + // use the md5 hash of a BinMap as a Key |
| 205 | + jsonStr, err := json.Marshal(m) |
| 206 | + if err == nil { |
| 207 | + key, err := aero.NewKey(as.properties.Namespase, |
| 208 | + as.properties.SetName, |
| 209 | + md5.Sum([]byte(jsonStr))) |
| 210 | + if err == nil { |
| 211 | + as.client.Put(as.writePolicy, key, m) |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + if err != nil { |
| 216 | + log.Printf("Error on processing Aerospike message: %s", err) |
| 217 | + } |
| 218 | + default: |
| 219 | + log.Printf("Unsupported message type %v", m) |
| 220 | + } |
| 221 | + } |
| 222 | + as.client.Close() |
| 223 | +} |
| 224 | + |
| 225 | +// In returns an input channel for receiving data |
| 226 | +func (as *AerospikeSink) In() chan<- interface{} { |
| 227 | + return as.in |
| 228 | +} |
0 commit comments