-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PoC] Postgres implementation on transaction ID #22
Changes from all commits
d02617e
8f7473c
36c32ef
b878499
93aa24b
a592881
fe33cc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,6 @@ | ||||||||
package sql | ||||||||
|
||||||||
import ( | ||||||||
"database/sql" | ||||||||
"fmt" | ||||||||
"strings" | ||||||||
|
||||||||
|
@@ -15,22 +14,23 @@ type DefaultPostgreSQLSchema struct { | |||||||
} | ||||||||
|
||||||||
func (s DefaultPostgreSQLSchema) SchemaInitializingQueries(topic string) []string { | ||||||||
createMessagesTable := strings.Join([]string{ | ||||||||
`CREATE TABLE IF NOT EXISTS ` + s.MessagesTable(topic) + ` (`, | ||||||||
`"offset" SERIAL,`, | ||||||||
`"uuid" VARCHAR(36) NOT NULL,`, | ||||||||
`"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,`, | ||||||||
`"payload" JSON DEFAULT NULL,`, | ||||||||
`"metadata" JSON DEFAULT NULL`, | ||||||||
`);`, | ||||||||
}, "\n") | ||||||||
createMessagesTable := ` | ||||||||
CREATE TABLE IF NOT EXISTS ` + s.MessagesTable(topic) + ` ( | ||||||||
"offset" SERIAL, | ||||||||
"uuid" VARCHAR(36) NOT NULL, | ||||||||
"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||||||||
"payload" JSON DEFAULT NULL, | ||||||||
"metadata" JSON DEFAULT NULL, | ||||||||
"transaction_id" xid8 NOT NULL | ||||||||
); | ||||||||
` | ||||||||
|
||||||||
return []string{createMessagesTable} | ||||||||
} | ||||||||
|
||||||||
func (s DefaultPostgreSQLSchema) InsertQuery(topic string, msgs message.Messages) (string, []interface{}, error) { | ||||||||
insertQuery := fmt.Sprintf( | ||||||||
`INSERT INTO %s (uuid, payload, metadata) VALUES %s`, | ||||||||
`INSERT INTO %s (uuid, payload, metadata, transaction_id) VALUES %s`, | ||||||||
s.MessagesTable(topic), | ||||||||
defaultInsertMarkers(len(msgs)), | ||||||||
) | ||||||||
|
@@ -48,7 +48,7 @@ func defaultInsertMarkers(count int) string { | |||||||
|
||||||||
index := 1 | ||||||||
for i := 0; i < count; i++ { | ||||||||
result.WriteString(fmt.Sprintf("($%d,$%d,$%d),", index, index+1, index+2)) | ||||||||
result.WriteString(fmt.Sprintf("($%d,$%d,$%d,pg_current_xact_id()),", index, index+1, index+2)) | ||||||||
index += 3 | ||||||||
} | ||||||||
|
||||||||
|
@@ -58,17 +58,32 @@ func defaultInsertMarkers(count int) string { | |||||||
func (s DefaultPostgreSQLSchema) SelectQuery(topic string, consumerGroup string, offsetsAdapter OffsetsAdapter) (string, []interface{}) { | ||||||||
nextOffsetQuery, nextOffsetArgs := offsetsAdapter.NextOffsetQuery(topic, consumerGroup) | ||||||||
selectQuery := ` | ||||||||
SELECT "offset", uuid, payload, metadata FROM ` + s.MessagesTable(topic) + ` | ||||||||
WHERE | ||||||||
"offset" > (` + nextOffsetQuery + `) | ||||||||
WITH last_processed AS ( | ||||||||
` + nextOffsetQuery + ` | ||||||||
) | ||||||||
|
||||||||
SELECT "offset", transaction_id, uuid, payload, metadata FROM ` + s.MessagesTable(topic) + ` | ||||||||
|
||||||||
WHERE | ||||||||
( | ||||||||
( | ||||||||
transaction_id = (SELECT last_processed_transaction_id FROM last_processed) | ||||||||
AND | ||||||||
"offset" > (SELECT offset_acked FROM last_processed) | ||||||||
) | ||||||||
OR | ||||||||
(transaction_id > (SELECT last_processed_transaction_id FROM last_processed)) | ||||||||
) | ||||||||
AND | ||||||||
transaction_id < pg_snapshot_xmin(pg_current_snapshot()) | ||||||||
ORDER BY | ||||||||
"offset" ASC | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose there should be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, this type of ordering, should be covered by index like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch and point about the index! I applied them in #23 |
||||||||
LIMIT 1` | ||||||||
LIMIT 100` | ||||||||
|
||||||||
return selectQuery, nextOffsetArgs | ||||||||
} | ||||||||
|
||||||||
func (s DefaultPostgreSQLSchema) UnmarshalMessage(row *sql.Row) (offset int, msg *message.Message, err error) { | ||||||||
func (s DefaultPostgreSQLSchema) UnmarshalMessage(row Scanner) (offset int64, transactionID int64, msg *message.Message, err error) { | ||||||||
return unmarshalDefaultMessage(row) | ||||||||
} | ||||||||
|
||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, looks unsuitable for MySQL (there is no snapshots)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stupid copy-paste 😅 Removed in this PR: #23