-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services/horizon/internal/ingest: Prevent redundant and concurrent st…
…ate verification runs (#4821) Disable concurrent state verification and add configuration to specifiy how often state verificaition runs and a timeout for capping the duration of a state verification run.
- Loading branch information
Showing
17 changed files
with
297 additions
and
81 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
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
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,38 @@ | ||
package history | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/stellar/go/support/db" | ||
"github.com/stellar/go/support/errors" | ||
) | ||
|
||
// stateVerificationLockId is the objid for the advisory lock acquired during | ||
// state verification. The value is arbitrary. The only requirement is that | ||
// all ingesting nodes use the same value which is why it's hard coded here. | ||
const stateVerificationLockId = 73897213 | ||
|
||
// TryStateVerificationLock attempts to acquire the state verification lock | ||
// which gives the ingesting node exclusive access to perform state verification. | ||
// TryStateVerificationLock returns true if the lock was acquired or false if the | ||
// lock could not be acquired because it is held by another node. | ||
func (q *Q) TryStateVerificationLock(ctx context.Context) (bool, error) { | ||
if tx := q.GetTx(); tx == nil { | ||
return false, errors.New("cannot be called outside of a transaction") | ||
} | ||
|
||
var acquired []bool | ||
err := q.SelectRaw( | ||
context.WithValue(ctx, &db.QueryTypeContextKey, db.AdvisoryLockQueryType), | ||
&acquired, | ||
"SELECT pg_try_advisory_xact_lock(?)", | ||
stateVerificationLockId, | ||
) | ||
if err != nil { | ||
return false, errors.Wrap(err, "error acquiring advisory lock for state verification") | ||
} | ||
if len(acquired) != 1 { | ||
return false, errors.Wrap(err, "invalid response from advisory lock") | ||
} | ||
return acquired[0], nil | ||
} |
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,47 @@ | ||
package history | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
|
||
"github.com/stellar/go/services/horizon/internal/test" | ||
) | ||
|
||
func TestTryStateVerificationLock(t *testing.T) { | ||
tt := test.Start(t) | ||
defer tt.Finish() | ||
test.ResetHorizonDB(t, tt.HorizonDB) | ||
q := &Q{tt.HorizonSession()} | ||
otherQ := &Q{q.Clone()} | ||
|
||
_, err := q.TryStateVerificationLock(context.Background()) | ||
tt.Assert.EqualError(err, "cannot be called outside of a transaction") | ||
|
||
tt.Assert.NoError(q.BeginTx(&sql.TxOptions{ | ||
Isolation: sql.LevelRepeatableRead, | ||
ReadOnly: true, | ||
})) | ||
ok, err := q.TryStateVerificationLock(context.Background()) | ||
tt.Assert.NoError(err) | ||
tt.Assert.True(ok) | ||
|
||
// lock is already held by q so we will not succeed | ||
tt.Assert.NoError(otherQ.BeginTx(&sql.TxOptions{ | ||
Isolation: sql.LevelRepeatableRead, | ||
ReadOnly: true, | ||
})) | ||
ok, err = otherQ.TryStateVerificationLock(context.Background()) | ||
tt.Assert.NoError(err) | ||
tt.Assert.False(ok) | ||
|
||
// when q is rolled back that releases the lock | ||
tt.Assert.NoError(q.Rollback()) | ||
|
||
// now otherQ is able to acquire the lock | ||
ok, err = otherQ.TryStateVerificationLock(context.Background()) | ||
tt.Assert.NoError(err) | ||
tt.Assert.True(ok) | ||
|
||
tt.Assert.NoError(otherQ.Rollback()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
Oops, something went wrong.