-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_listener.go
125 lines (110 loc) · 3.55 KB
/
stream_listener.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
package main
import (
"time"
"github.com/aws/aws-sdk-go/service/dynamodbstreams"
logging "github.com/sirupsen/logrus"
)
const waitForMoreRecords = 10
func (listener *streamListener) listen(altRecords <-chan *dynamodbstreams.Record, quitSelf chan bool, quitAlt chan bool) {
go listener.processSelfStream(quitSelf, quitAlt)
go listener.processAltStream(altRecords, quitSelf, quitAlt)
}
func (listener *streamListener) getShards() error {
var result *dynamodbstreams.DescribeStreamOutput
var err error
var isBidirectional = false
lastEvaluatedShardId := ""
for {
result, err = listener.describeStream(lastEvaluatedShardId)
if err != nil {
logger.WithFields(logging.Fields{
"Table": listener.streamOwner,
"Error": err,
}).Error("Failed to describe stream")
return err
}
for _, shard := range result.StreamDescription.Shards {
listener.shardLock.Lock()
_, isActivelyProcessed := listener.activeShardProcessors[*shard.ShardId]
_, isCompleted := listener.completedShards[*shard.ShardId]
listener.shardLock.Unlock()
if !isActivelyProcessed && !isCompleted {
logger.WithFields(logging.Fields{
"Table": listener.streamOwner,
"ShardId": *shard.ShardId,
}).Debug("Starting processor for shard")
listener.shardLock.Lock()
listener.activeShardProcessors[*shard.ShardId] = true
listener.shardLock.Unlock()
go listener.processShard(shard, isBidirectional)
} else {
// Shard is already being processed
continue
}
}
if result.StreamDescription.LastEvaluatedShardId != nil {
lastEvaluatedShardId = *result.StreamDescription.LastEvaluatedShardId
} else {
// No more shards to be processed for now
// wait a few seconds before trying again
// This API cannot be called more than 10/s
logger.WithFields(logging.Fields{
"Table": listener.streamOwner,
}).Debug("No more shards to be processed for now")
lastEvaluatedShardId = ""
isBidirectional = true
time.Sleep(shardEnumerateInterval)
}
}
}
func (listener *streamListener) processSelfStream(quitSelf chan bool, quitAlt chan bool) {
for {
select {
case <- quitSelf:
logger.WithFields(logging.Fields{
"Table": listener.streamOwner,
}).Error("Quitting processing stream")
return
default:
err := listener.getShards()
if err != nil {
quitAlt <- true
break
}
}
}
}
func (listener *streamListener) processAltStream(altRecords <-chan *dynamodbstreams.Record, quitSelf chan bool, quitAlt chan bool) {
for {
select {
case record := <-altRecords:
if *record.Dynamodb.NewImage[replicationSource].S == listener.streamOwner {
logger.WithFields(logging.Fields{
"Table": listener.streamOwner,
"Record Key": record.Dynamodb.Keys,
}).Debug("Found record in alt stream with myself as source. Ignoring record")
continue
}
oldT := record.Dynamodb.OldImage[replicationTimestamp]
newT := record.Dynamodb.NewImage[replicationTimestamp]
if oldT != nil && newT != nil && *oldT.N == *newT.N {
continue
}
logger.WithFields(logging.Fields{
"Table": listener.streamOwner,
"Record Number": *record.Dynamodb.SequenceNumber,
"Record Creation Time": record.Dynamodb.ApproximateCreationDateTime,
"Record Key": record.Dynamodb.Keys,
"Record New Image": record.Dynamodb.NewImage,
}).Debug("Received record from alt table")
listener.updateTable(record)
case <- quitSelf:
logger.WithFields(logging.Fields{
"Table": listener.streamOwner,
}).Error("Received quit signal")
return
default:
time.Sleep(time.Duration(waitForMoreRecords))
}
}
}