-
Notifications
You must be signed in to change notification settings - Fork 132
/
event.go
157 lines (141 loc) · 5.01 KB
/
event.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
package repository
import (
"context"
"fmt"
"math"
"time"
"github.com/go-redis/redis"
"github.com/gogo/protobuf/proto"
pool "github.com/jolestar/go-commons-pool"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/armadaproject/armada/internal/armada/repository/apimessages"
"github.com/armadaproject/armada/internal/armada/repository/sequence"
"github.com/armadaproject/armada/internal/common/compress"
"github.com/armadaproject/armada/pkg/api"
"github.com/armadaproject/armada/pkg/armadaevents"
)
const (
eventStreamPrefix = "Events:"
dataKey = "message"
)
type EventRepository interface {
CheckStreamExists(queue string, jobSetId string) (bool, error)
ReadEvents(queue, jobSetId string, lastId string, limit int64, block time.Duration) ([]*api.EventStreamMessage, error)
GetLastMessageId(queue, jobSetId string) (string, error)
}
type RedisEventRepository struct {
db redis.UniversalClient
decompressorPool *pool.ObjectPool
}
func NewEventRepository(db redis.UniversalClient) *RedisEventRepository {
// This is basically the default config but with a max of 100 rather than 8 and a min of 10 rather than 0.
poolConfig := pool.ObjectPoolConfig{
MaxTotal: 100,
MaxIdle: 50,
MinIdle: 10,
BlockWhenExhausted: true,
MinEvictableIdleTime: 30 * time.Minute,
SoftMinEvictableIdleTime: math.MaxInt64,
TimeBetweenEvictionRuns: 0,
NumTestsPerEvictionRun: 10,
}
decompressorPool := pool.NewObjectPool(context.Background(), pool.NewPooledObjectFactorySimple(
func(context.Context) (interface{}, error) {
return compress.NewZlibDecompressor(), nil
}), &poolConfig)
return &RedisEventRepository{db: db, decompressorPool: decompressorPool}
}
func (repo *RedisEventRepository) CheckStreamExists(queue string, jobSetId string) (bool, error) {
result, err := repo.db.Exists(getJobSetEventsKey(queue, jobSetId)).Result()
if err != nil {
return false, err
}
exists := result > 0
return exists, nil
}
func (repo *RedisEventRepository) ReadEvents(queue string, jobSetId string, lastId string, limit int64, block time.Duration) ([]*api.EventStreamMessage, error) {
from, err := sequence.Parse(lastId)
if err != nil {
return nil, err
}
seqId := from.PrevRedisId()
cmd, err := repo.db.XRead(&redis.XReadArgs{
Streams: []string{getJobSetEventsKey(queue, jobSetId), seqId},
Count: limit,
Block: block,
}).Result()
// redis signals empty list by Nil
if err == redis.Nil {
return make([]*api.EventStreamMessage, 0), nil
} else if err != nil {
return nil, errors.WithStack(fmt.Errorf("%s (fromId: %s, seqId: %s)", err, from, seqId))
}
messages := make([]*api.EventStreamMessage, 0, len(cmd[0].Messages))
for _, m := range cmd[0].Messages {
// TODO: here we decompress all the events we fetched from the db- it would be much better
// If we could decompress lazily, but the interface confines us somewhat here
apiEvents, err := repo.extractEvents(m, queue, jobSetId)
if err != nil {
return nil, err
}
for i, msg := range apiEvents {
msgId, err := sequence.FromRedisId(m.ID, i, i == len(apiEvents)-1)
if err != nil {
return nil, err
}
if msgId.IsAfter(from) {
messages = append(messages, &api.EventStreamMessage{Id: msgId.String(), Message: msg})
}
}
}
return messages, nil
}
func (repo *RedisEventRepository) GetLastMessageId(queue, jobSetId string) (string, error) {
msg, err := repo.db.XRevRangeN(getJobSetEventsKey(queue, jobSetId), "+", "-", 1).Result()
if err != nil {
return "", errors.Wrap(err, "Error retrieving the last message id from Redis")
}
if len(msg) > 0 {
apiEvents, err := repo.extractEvents(msg[0], queue, jobSetId)
if err != nil {
return "", err
}
msgId, err := sequence.FromRedisId(msg[0].ID, len(apiEvents)-1, true)
if err != nil {
return "", err
}
return msgId.String(), nil
}
return "0", nil
}
func (repo *RedisEventRepository) extractEvents(msg redis.XMessage, queue, jobSetId string) ([]*api.EventMessage, error) {
data := msg.Values[dataKey]
bytes := []byte(data.(string))
decompressor, err := repo.decompressorPool.BorrowObject(context.Background())
if err != nil {
return nil, errors.WithStack(err)
}
defer func(decompressorPool *pool.ObjectPool, ctx context.Context, object interface{}) {
err := decompressorPool.ReturnObject(ctx, object)
if err != nil {
log.WithError(err).Errorf("Error returning decompressor to pool")
}
}(repo.decompressorPool, context.Background(), decompressor)
decompressedData, err := decompressor.(compress.Decompressor).Decompress(bytes)
if err != nil {
return nil, errors.WithStack(err)
}
es := &armadaevents.EventSequence{}
err = proto.Unmarshal(decompressedData, es)
if err != nil {
return nil, errors.WithStack(err)
}
// These fields are not present in the db messages, so we add them back here
es.Queue = queue
es.JobSetName = jobSetId
return apimessages.FromEventSequence(es)
}
func getJobSetEventsKey(queue, jobSetId string) string {
return eventStreamPrefix + queue + ":" + jobSetId
}