-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2452 from fluidity-money/develop-add-last-check-w…
…orker-server-failsafe Introduce a final check in the event of any weirdness with message du…
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
database/101-up-postgres/20231214171310-failsafe_add_transaction_hash_index.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
-- migrate:up | ||
|
||
CREATE TABLE failsafe_transaction_hash ( | ||
transaction_hash VARCHAR, | ||
log_index BIGINT, | ||
worker_id VARCHAR, | ||
PRIMARY KEY(transaction_hash, log_index, worker_id) | ||
); | ||
|
||
-- migrate:down | ||
|
||
DROP TABLE failsafe_transaction_hash; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2022 Fluidity Money. All rights reserved. Use of this | ||
// source code is governed by a GPL-style license that can be found in the | ||
// LICENSE.md file. | ||
|
||
package failsafe | ||
|
||
// Context to use for when failsafe acquisition goes wrong | ||
const Context = "POSTGRES/FAILSAFE" | ||
|
||
// TableFailsafeTransactionHashIndex to be used as the final check before | ||
// a side effectful action where duplication could possibly happen at the | ||
// infra level | ||
const TableFailsafeTransactionHashIndex = "failsafe_transaction_hash_index" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2022 Fluidity Money. All rights reserved. Use of this | ||
// source code is governed by a GPL-style license that can be found in the | ||
// LICENSE.md file. | ||
|
||
package failsafe | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fluidity-money/fluidity-app/lib/log" | ||
"github.com/fluidity-money/fluidity-app/lib/postgres" | ||
"github.com/fluidity-money/fluidity-app/lib/types/ethereum" | ||
"github.com/fluidity-money/fluidity-app/lib/types/misc" | ||
"github.com/fluidity-money/fluidity-app/lib/util" | ||
) | ||
|
||
// CommitTransactionHashIndex using the transactionHash given, the | ||
// logIndex, and the worker ID, forming a composite primary key | ||
// that guarantees uniqueness. Will Fatal if the insertion fails, with a | ||
// reason. Useful for identifying duplication-related issues. | ||
func CommitTransactionHashIndex(transactionHash ethereum.Hash, logIndex misc.BigInt) { | ||
postgresClient := postgres.Client() | ||
|
||
statementText := fmt.Sprintf(` | ||
INSERT INTO %v ( | ||
transaction_hash, | ||
log_index, | ||
worker_id | ||
) | ||
VALUES ( | ||
$1, | ||
$2, | ||
$3 | ||
)`, | ||
|
||
TableFailsafeTransactionHashIndex, | ||
) | ||
|
||
workerId := util.GetWorkerId() | ||
|
||
_, err := postgresClient.Exec(statementText, transactionHash, logIndex, workerId) | ||
|
||
if err != nil { | ||
log.Fatal(func(k *log.Log) { | ||
k.Context = Context | ||
|
||
k.Format( | ||
"Failed to acquire a failsafe for transaction hash %v, log index %v, worker id %v", | ||
transactionHash, | ||
logIndex, | ||
workerId, | ||
) | ||
|
||
k.Payload = err | ||
}) | ||
} | ||
} |