Skip to content
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

services/horizon/internal/db2/history: Use FastBatchInsertBuilder to insert ledgers into the history_ledgers #4947

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion services/horizon/internal/action_offers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func TestOfferActions_Show(t *testing.T) {
ht.Assert.NoError(err)

ledgerCloseTime := time.Now().Unix()
_, err = q.InsertLedger(ctx, xdr.LedgerHeaderHistoryEntry{
ht.Assert.NoError(q.Begin())
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err = ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Header: xdr.LedgerHeader{
LedgerSeq: 100,
ScpValue: xdr.StellarValue{
Expand All @@ -33,6 +35,8 @@ func TestOfferActions_Show(t *testing.T) {
},
}, 0, 0, 0, 0, 0)
ht.Assert.NoError(err)
ht.Assert.NoError(ledgerBatch.Exec(ht.Ctx, q))
ht.Assert.NoError(q.Commit())

issuer := xdr.MustAddress("GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H")
nativeAsset := xdr.MustNewNativeAsset()
Expand Down
19 changes: 16 additions & 3 deletions services/horizon/internal/actions/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ func TestAccountInfo(t *testing.T) {
assert.NoError(t, err)

ledgerFourCloseTime := time.Now().Unix()
_, err = q.InsertLedger(tt.Ctx, xdr.LedgerHeaderHistoryEntry{
assert.NoError(t, q.Begin())
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err = ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Header: xdr.LedgerHeader{
LedgerSeq: 4,
ScpValue: xdr.StellarValue{
Expand All @@ -237,6 +239,8 @@ func TestAccountInfo(t *testing.T) {
},
}, 0, 0, 0, 0, 0)
assert.NoError(t, err)
assert.NoError(t, ledgerBatch.Exec(tt.Ctx, q))
assert.NoError(t, q.Commit())

account, err := AccountInfo(tt.Ctx, &history.Q{tt.HorizonSession()}, accountID)
tt.Assert.NoError(err)
Expand Down Expand Up @@ -408,7 +412,9 @@ func TestGetAccountsHandlerPageResultsByAsset(t *testing.T) {
err := q.UpsertAccounts(tt.Ctx, []history.AccountEntry{account1, account2})
assert.NoError(t, err)
ledgerCloseTime := time.Now().Unix()
_, err = q.InsertLedger(tt.Ctx, xdr.LedgerHeaderHistoryEntry{
assert.NoError(t, q.Begin())
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err = ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Header: xdr.LedgerHeader{
LedgerSeq: 1234,
ScpValue: xdr.StellarValue{
Expand All @@ -417,6 +423,8 @@ func TestGetAccountsHandlerPageResultsByAsset(t *testing.T) {
},
}, 0, 0, 0, 0, 0)
assert.NoError(t, err)
assert.NoError(t, ledgerBatch.Exec(tt.Ctx, q))
assert.NoError(t, q.Commit())

for _, row := range accountSigners {
_, err = q.CreateAccountSigner(tt.Ctx, row.Account, row.Signer, row.Weight, nil)
Expand Down Expand Up @@ -511,7 +519,9 @@ func TestGetAccountsHandlerPageResultsByLiquidityPool(t *testing.T) {
assert.NoError(t, err)

ledgerCloseTime := time.Now().Unix()
_, err = q.InsertLedger(tt.Ctx, xdr.LedgerHeaderHistoryEntry{
assert.NoError(t, q.Begin())
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err = ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Header: xdr.LedgerHeader{
LedgerSeq: 1234,
ScpValue: xdr.StellarValue{
Expand All @@ -520,6 +530,9 @@ func TestGetAccountsHandlerPageResultsByLiquidityPool(t *testing.T) {
},
}, 0, 0, 0, 0, 0)
assert.NoError(t, err)
assert.NoError(t, ledgerBatch.Exec(tt.Ctx, q))
assert.NoError(t, q.Commit())

var assetType, code, issuer string
usd.MustExtract(&assetType, &code, &issuer)
params := map[string]string{
Expand Down
16 changes: 12 additions & 4 deletions services/horizon/internal/actions/offer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,19 @@ func TestGetOfferByIDHandler(t *testing.T) {
handler := GetOfferByID{}

ledgerCloseTime := time.Now().Unix()
_, err := q.InsertLedger(tt.Ctx, xdr.LedgerHeaderHistoryEntry{
assert.NoError(t, q.Begin())
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err := ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Header: xdr.LedgerHeader{
LedgerSeq: 3,
ScpValue: xdr.StellarValue{
CloseTime: xdr.TimePoint(ledgerCloseTime),
},
},
}, 0, 0, 0, 0, 0)
tt.Assert.NoError(err)
assert.NoError(t, err)
assert.NoError(t, ledgerBatch.Exec(tt.Ctx, q))
assert.NoError(t, q.Commit())

err = q.UpsertOffers(tt.Ctx, []history.Offer{eurOffer, usdOffer})
tt.Assert.NoError(err)
Expand Down Expand Up @@ -186,15 +190,19 @@ func TestGetOffersHandler(t *testing.T) {
handler := GetOffersHandler{}

ledgerCloseTime := time.Now().Unix()
_, err := q.InsertLedger(tt.Ctx, xdr.LedgerHeaderHistoryEntry{
assert.NoError(t, q.Begin())
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err := ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Header: xdr.LedgerHeader{
LedgerSeq: 3,
ScpValue: xdr.StellarValue{
CloseTime: xdr.TimePoint(ledgerCloseTime),
},
},
}, 0, 0, 0, 0, 0)
tt.Assert.NoError(err)
assert.NoError(t, err)
assert.NoError(t, ledgerBatch.Exec(tt.Ctx, q))
assert.NoError(t, q.Commit())

err = q.UpsertOffers(tt.Ctx, []history.Offer{eurOffer, twoEurOffer, usdOffer})
tt.Assert.NoError(err)
Expand Down
7 changes: 6 additions & 1 deletion services/horizon/internal/actions_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ func TestAccountActions_InvalidID(t *testing.T) {
ht.Assert.NoError(err)
err = q.UpdateIngestVersion(ht.Ctx, ingest.CurrentVersion)
ht.Assert.NoError(err)
_, err = q.InsertLedger(ht.Ctx, xdr.LedgerHeaderHistoryEntry{

ht.Assert.NoError(q.Begin())
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err = ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Header: xdr.LedgerHeader{
LedgerSeq: 100,
},
}, 0, 0, 0, 0, 0)
ht.Assert.NoError(err)
ht.Assert.NoError(ledgerBatch.Exec(ht.Ctx, q))
ht.Assert.NoError(q.Commit())

// existing account
w := ht.Get(
Expand Down
6 changes: 5 additions & 1 deletion services/horizon/internal/actions_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ func TestDataActions_Show(t *testing.T) {
ht.Assert.NoError(err)
err = q.UpdateIngestVersion(ht.Ctx, ingest.CurrentVersion)
ht.Assert.NoError(err)
_, err = q.InsertLedger(ht.Ctx, xdr.LedgerHeaderHistoryEntry{
ht.Assert.NoError(q.Begin())
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err = ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Header: xdr.LedgerHeader{
LedgerSeq: 100,
},
}, 0, 0, 0, 0, 0)
ht.Assert.NoError(err)
ht.Assert.NoError(ledgerBatch.Exec(ht.Ctx, q))
ht.Assert.NoError(q.Commit())

err = q.UpsertAccountData(ht.Ctx, []history.Data{data1, data2})
assert.NoError(t, err)
Expand Down
46 changes: 32 additions & 14 deletions services/horizon/internal/db2/history/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
sq "github.com/Masterminds/squirrel"
"github.com/guregu/null"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/ordered"
"github.com/stellar/go/toid"
Expand Down Expand Up @@ -90,27 +91,46 @@ func (q *LedgersQ) Select(ctx context.Context, dest interface{}) error {

// QLedgers defines ingestion ledger related queries.
type QLedgers interface {
InsertLedger(
ctx context.Context,
NewLedgerBatchInsertBuilder() LedgerBatchInsertBuilder
}

// LedgerBatchInsertBuilder is used to insert ledgers into the
// history_ledgers table
type LedgerBatchInsertBuilder interface {
Add(
ledger xdr.LedgerHeaderHistoryEntry,
successTxsCount int,
failedTxsCount int,
opCount int,
txSetOpCount int,
ingestVersion int,
) (int64, error)
) error
Exec(ctx context.Context, session db.SessionInterface) error
}

// ledgerBatchInsertBuilder is a simple wrapper around db.BatchInsertBuilder
type ledgerBatchInsertBuilder struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost sounds like this could be generalized into a tableBatchInsertBuilder and reused?

Copy link
Contributor Author

@tamirms tamirms Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

db.FastBatchInsertBuilder is the component which could be used by multiple tables. ledgerBatchInsertBuilder has an Add() function which is specific to inserting ledgers. For other tables the Add() function would take different parameters

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sure, got it.

I think I was just thrown off by the fact that you have var table string as a separate variable that you explicitly assigned to "history_ledgers"; I'm seeing now that it's more of a quirk of not being able to assign in within the struct, e.g. how you can do something like

class ledgerBatchInsertBuilder {
  builder: db.FastBatchInsertBuilder;
  table: string = "history_ledgers";

  // constructors, etc...
}

in JS.

builder db.FastBatchInsertBuilder
table string
}

// NewLedgerBatchInsertBuilder constructs a new EffectBatchInsertBuilder instance
func (q *Q) NewLedgerBatchInsertBuilder() LedgerBatchInsertBuilder {
return &ledgerBatchInsertBuilder{
table: "history_ledgers",
builder: db.FastBatchInsertBuilder{},
}
}

// InsertLedger creates a row in the history_ledgers table.
// Returns number of rows affected and error.
func (q *Q) InsertLedger(ctx context.Context,
// Add adds a effect to the batch
func (i *ledgerBatchInsertBuilder) Add(
ledger xdr.LedgerHeaderHistoryEntry,
successTxsCount int,
failedTxsCount int,
opCount int,
txSetOpCount int,
ingestVersion int,
) (int64, error) {
) error {
m, err := ledgerHeaderToMap(
ledger,
successTxsCount,
Expand All @@ -120,16 +140,14 @@ func (q *Q) InsertLedger(ctx context.Context,
ingestVersion,
)
if err != nil {
return 0, err
return err
}

sql := sq.Insert("history_ledgers").SetMap(m)
result, err := q.Exec(ctx, sql)
if err != nil {
return 0, err
}
return i.builder.Row(m)
}

return result.RowsAffected()
func (i *ledgerBatchInsertBuilder) Exec(ctx context.Context, session db.SessionInterface) error {
return i.builder.Exec(ctx, session, i.table)
}

// GetLedgerGaps obtains ingestion gaps in the history_ledgers table.
Expand Down
14 changes: 10 additions & 4 deletions services/horizon/internal/db2/history/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ func TestInsertLedger(t *testing.T) {
tt.Assert.NoError(err)
expectedLedger.LedgerHeaderXDR = null.NewString(ledgerHeaderBase64, true)

rowsAffected, err := q.InsertLedger(tt.Ctx,
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err = ledgerBatch.Add(
ledgerEntry,
12,
3,
Expand All @@ -128,7 +129,9 @@ func TestInsertLedger(t *testing.T) {
int(expectedLedger.ImporterVersion),
)
tt.Assert.NoError(err)
tt.Assert.Equal(rowsAffected, int64(1))
tt.Assert.NoError(q.Begin())
tt.Assert.NoError(ledgerBatch.Exec(tt.Ctx, q.SessionInterface))
tt.Assert.NoError(q.Commit())

err = q.LedgerBySequence(tt.Ctx, &ledgerFromDB, 69859)
tt.Assert.NoError(err)
Expand Down Expand Up @@ -204,7 +207,8 @@ func insertLedgerWithSequence(tt *test.T, q *Q, seq uint32) {
ledgerHeaderBase64, err := xdr.MarshalBase64(ledgerEntry.Header)
tt.Assert.NoError(err)
expectedLedger.LedgerHeaderXDR = null.NewString(ledgerHeaderBase64, true)
rowsAffected, err := q.InsertLedger(tt.Ctx,
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err = ledgerBatch.Add(
ledgerEntry,
12,
3,
Expand All @@ -213,7 +217,9 @@ func insertLedgerWithSequence(tt *test.T, q *Q, seq uint32) {
int(expectedLedger.ImporterVersion),
)
tt.Assert.NoError(err)
tt.Assert.Equal(rowsAffected, int64(1))
tt.Assert.NoError(q.Begin())
tt.Assert.NoError(ledgerBatch.Exec(tt.Ctx, q.SessionInterface))
tt.Assert.NoError(q.Commit())
}

func TestGetLedgerGaps(t *testing.T) {
Expand Down
27 changes: 21 additions & 6 deletions services/horizon/internal/db2/history/mock_q_ledgers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,38 @@ package history
import (
"context"

"github.com/stretchr/testify/mock"

"github.com/stellar/go/support/db"
"github.com/stellar/go/xdr"

"github.com/stretchr/testify/mock"
)

type MockQLedgers struct {
mock.Mock
}

func (m *MockQLedgers) InsertLedger(ctx context.Context,
func (m *MockQLedgers) NewLedgerBatchInsertBuilder() LedgerBatchInsertBuilder {
a := m.Called()
return a.Get(0).(LedgerBatchInsertBuilder)
}

type MockLedgersBatchInsertBuilder struct {
mock.Mock
}

func (m *MockLedgersBatchInsertBuilder) Add(
ledger xdr.LedgerHeaderHistoryEntry,
successTxsCount int,
failedTxsCount int,
opCount int,
txSetOpCount int,
ingestVersion int,
) (int64, error) {
a := m.Called(ctx, ledger, successTxsCount, failedTxsCount, opCount, txSetOpCount, ingestVersion)
return a.Get(0).(int64), a.Error(1)
) error {
a := m.Called(ledger, successTxsCount, failedTxsCount, opCount, txSetOpCount, ingestVersion)
return a.Error(0)
}

func (m *MockLedgersBatchInsertBuilder) Exec(ctx context.Context, session db.SessionInterface) error {
a := m.Called(ctx, session)
return a.Error(0)
}
6 changes: 5 additions & 1 deletion services/horizon/internal/db2/history/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func TestTransactionByLiquidityPool(t *testing.T) {

// Insert a phony ledger
ledgerCloseTime := time.Now().Unix()
_, err := q.InsertLedger(tt.Ctx, xdr.LedgerHeaderHistoryEntry{
ledgerBatch := q.NewLedgerBatchInsertBuilder()
err := ledgerBatch.Add(xdr.LedgerHeaderHistoryEntry{
Header: xdr.LedgerHeader{
LedgerSeq: xdr.Uint32(sequence),
ScpValue: xdr.StellarValue{
Expand All @@ -54,6 +55,9 @@ func TestTransactionByLiquidityPool(t *testing.T) {
},
}, 0, 0, 0, 0, 0)
tt.Assert.NoError(err)
tt.Assert.NoError(q.Begin())
tt.Assert.NoError(ledgerBatch.Exec(tt.Ctx, q.SessionInterface))
tt.Assert.NoError(q.Commit())

// Insert a phony transaction
transactionBuilder := q.NewTransactionBatchInsertBuilder(2)
Expand Down
1 change: 1 addition & 0 deletions services/horizon/internal/ingest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func NewSystem(config Config) (System, error) {
ctx: ctx,
config: config,
historyQ: historyQ,
session: historyQ,
historyAdapter: historyAdapter,
filters: filters,
},
Expand Down
4 changes: 3 additions & 1 deletion services/horizon/internal/ingest/processor_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/ingest/filters"
"github.com/stellar/go/services/horizon/internal/ingest/processors"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
Expand Down Expand Up @@ -88,6 +89,7 @@ type ProcessorRunner struct {

ctx context.Context
historyQ history.IngestionQ
session db.SessionInterface
historyAdapter historyArchiveAdapterInterface
logMemoryStats bool
filters filters.Filters
Expand Down Expand Up @@ -143,7 +145,7 @@ func (s *ProcessorRunner) buildTransactionProcessor(
return newGroupTransactionProcessors([]horizonTransactionProcessor{
statsLedgerTransactionProcessor,
processors.NewEffectProcessor(s.historyQ, sequence),
processors.NewLedgerProcessor(s.historyQ, ledger, CurrentVersion),
processors.NewLedgerProcessor(s.session, s.historyQ, ledger, CurrentVersion),
processors.NewOperationProcessor(s.historyQ, sequence),
tradeProcessor,
processors.NewParticipantsProcessor(s.historyQ, sequence),
Expand Down
Loading