-
Notifications
You must be signed in to change notification settings - Fork 5
/
postgres.go
310 lines (268 loc) · 9.55 KB
/
postgres.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* MIT License
*
* Copyright (c) 2022-2024 Tochemey
*
* 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 postgres
import (
"context"
"errors"
"fmt"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v5"
"go.uber.org/atomic"
"google.golang.org/protobuf/proto"
"github.com/tochemey/ego/v3/egopb"
"github.com/tochemey/ego/v3/internal/postgres"
"github.com/tochemey/ego/v3/offsetstore"
)
var (
columns = []string{
"projection_name",
"shard_number",
"current_offset",
"timestamp",
}
tableName = "offsets_store"
)
// offsetRow represent the offset entry in the offset store
type offsetRow struct {
// ProjectionName is the projection name
ProjectionName string
// Shard Number
ShardNumber uint64
// Value is the current offset
CurrentOffset int64
// Specifies the last update time
Timestamp int64
}
// OffsetStore implements the OffsetStore interface
// and helps persist events in a Postgres database
type OffsetStore struct {
db postgres.Postgres
sb sq.StatementBuilderType
// insertBatchSize represents the chunk of data to bulk insert.
// This helps avoid the postgres 65535 parameter limit.
// This is necessary because Postgres uses a 32-bit int for binding input parameters and
// is not able to track anything larger.
// Note: Change this value when you know the size of data to bulk insert at once. Otherwise, you
// might encounter the postgres 65535 parameter limit error.
insertBatchSize int
// hold the connection state to avoid multiple connection of the same instance
connected *atomic.Bool
}
// ensure the complete implementation of the OffsetStore interface
var _ offsetstore.OffsetStore = (*OffsetStore)(nil)
// NewOffsetStore creates an instance of OffsetStore
func NewOffsetStore(config *Config) *OffsetStore {
// create the underlying db connection
db := postgres.New(postgres.NewConfig(config.DBHost, config.DBPort, config.DBUser, config.DBPassword, config.DBName))
return &OffsetStore{
db: db,
sb: sq.StatementBuilder.PlaceholderFormat(sq.Dollar),
insertBatchSize: 500,
connected: atomic.NewBool(false),
}
}
// Connect connects to the underlying postgres database
func (x *OffsetStore) Connect(ctx context.Context) error {
// check whether this instance of the journal is connected or not
if x.connected.Load() {
return nil
}
// connect to the underlying db
if err := x.db.Connect(ctx); err != nil {
return err
}
// set the connection status
x.connected.Store(true)
return nil
}
// Disconnect disconnects from the underlying postgres database
func (x *OffsetStore) Disconnect(ctx context.Context) error {
// check whether this instance of the journal is connected or not
if !x.connected.Load() {
return nil
}
// disconnect the underlying database
if err := x.db.Disconnect(ctx); err != nil {
return err
}
// set the connection status
x.connected.Store(false)
return nil
}
// WriteOffset writes an offset into the offset store
func (x *OffsetStore) WriteOffset(ctx context.Context, offset *egopb.Offset) error {
// check whether this instance of the offset store is connected or not
if !x.connected.Load() {
return errors.New("offset store is not connected")
}
// make sure the record is defined
if offset == nil || proto.Equal(offset, new(egopb.Offset)) {
return errors.New("offset record is not defined")
}
// let us begin a database transaction to make sure we atomically write those events into the database
tx, err := x.db.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.ReadCommitted})
// return the error in case we are unable to get a database transaction
if err != nil {
return fmt.Errorf("failed to obtain a database transaction: %w", err)
}
var (
query string
args []any
)
// remove existing offset
deleteBuilder := x.sb.
Delete(tableName).
Where(sq.Eq{"projection_name": offset.GetProjectionName()}).
Where(sq.Eq{"shard_number": offset.GetShardNumber()})
// get the SQL statement to run
query, args, err = deleteBuilder.ToSql()
// handle the error while generating the SQL
if err != nil {
return fmt.Errorf("unable to build sql delete statement: %w", err)
}
// execute the query
_, execErr := tx.Exec(ctx, query, args...)
if execErr != nil {
// attempt to roll back the transaction and log the error in case there is an error
if err = tx.Rollback(ctx); err != nil {
return fmt.Errorf("unable to rollback db transaction: %w", err)
}
// return the main error
return fmt.Errorf("failed to record events: %w", execErr)
}
// create the insert statement
insertBuilder := x.sb.
Insert(tableName).
Columns(columns...).
Values(
offset.GetProjectionName(),
offset.GetShardNumber(),
offset.GetValue(),
offset.GetTimestamp())
// get the SQL statement to run
query, args, err = insertBuilder.ToSql()
// handle the error while generating the SQL
if err != nil {
return fmt.Errorf("unable to build sql insert statement: %w", err)
}
// insert into the table
_, execErr = tx.Exec(ctx, query, args...)
if execErr != nil {
// attempt to roll back the transaction and log the error in case there is an error
if err = tx.Rollback(ctx); err != nil {
return fmt.Errorf("unable to rollback db transaction: %w", err)
}
// return the main error
return fmt.Errorf("failed to record events: %w", execErr)
}
// commit the transaction
if commitErr := tx.Commit(ctx); commitErr != nil {
// return the commit error in case there is one
return fmt.Errorf("failed to record events: %w", commitErr)
}
// every looks good
return nil
}
// GetCurrentOffset returns the current offset of a given projection id
func (x *OffsetStore) GetCurrentOffset(ctx context.Context, projectionID *egopb.ProjectionId) (currentOffset *egopb.Offset, err error) {
// check whether this instance of the offset store is connected or not
if !x.connected.Load() {
return nil, errors.New("offset store is not connected")
}
// create the SQL statement
statement := x.sb.
Select(columns...).
From(tableName).
Where(sq.Eq{"projection_name": projectionID.GetProjectionName()}).
Where(sq.Eq{"shard_number": projectionID.GetShardNumber()})
// get the sql statement and the arguments
query, args, err := statement.ToSql()
if err != nil {
return nil, fmt.Errorf("failed to build the select sql statement: %w", err)
}
row := new(offsetRow)
err = x.db.Select(ctx, row, query, args...)
if err != nil {
return nil, fmt.Errorf("failed to fetch the current offset from the database: %w", err)
}
return &egopb.Offset{
ShardNumber: row.ShardNumber,
ProjectionName: row.ProjectionName,
Value: row.CurrentOffset,
Timestamp: row.Timestamp,
}, nil
}
// ResetOffset resets the offset of given projection to a given value across all shards
func (x *OffsetStore) ResetOffset(ctx context.Context, projectionName string, value int64) error {
// check whether this instance of the offset store is connected or not
if !x.connected.Load() {
return errors.New("offset store is not connected")
}
// let us begin a database transaction to make sure we atomically write those events into the database
tx, err := x.db.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.ReadCommitted})
// return the error in case we are unable to get a database transaction
if err != nil {
return fmt.Errorf("failed to obtain a database transaction: %w", err)
}
// define the current timestamp
timestamp := time.Now().UnixMilli()
// create the sql statement
statement := x.sb.
Update(tableName).
Set("current_offset", value).
Set("timestamp", timestamp).
Where(sq.Eq{"projection_name": projectionName})
// get the SQL statement to run
query, args, err := statement.ToSql()
// handle the error while generating the SQL
if err != nil {
return fmt.Errorf("unable to build sql insert statement: %w", err)
}
// insert into the table
_, execErr := tx.Exec(ctx, query, args...)
if execErr != nil {
// attempt to roll back the transaction and log the error in case there is an error
if err = tx.Rollback(ctx); err != nil {
return fmt.Errorf("unable to rollback db transaction: %w", err)
}
// return the main error
return fmt.Errorf("failed to record events: %w", execErr)
}
// commit the transaction
if commitErr := tx.Commit(ctx); commitErr != nil {
// return the commit error in case there is one
return fmt.Errorf("failed to record events: %w", commitErr)
}
// every looks good
return nil
}
// Ping verifies a connection to the database is still alive, establishing a connection if necessary.
func (x *OffsetStore) Ping(ctx context.Context) error {
// check whether we are connected or not
if !x.connected.Load() {
return x.Connect(ctx)
}
return nil
}