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

Add getEvents backed by DB #215

Merged
merged 67 commits into from
Aug 20, 2024
Merged

Add getEvents backed by DB #215

merged 67 commits into from
Aug 20, 2024

Conversation

psheth9
Copy link
Contributor

@psheth9 psheth9 commented Jun 13, 2024

Overall design:

  • Ingest events into DB: store entire eventXDR along with metadata like contract_ids, topics etc..
  • Combine contract_ids and topics from all the filters for a given request and pass it along to DB (to save multiple DB calls)
  • Fetch events and apply filter again based on the request (on less data comparatively)

Other Refactor:

  • Remove in-memory events store
  • Introduce endLedgerSeq
  • Add migration related logic for events

Other related PR:

  • Migration failures and refactor daemon.go PR

Data Flow:

image

Design Docs:

Parent Epic #115
Fixes: #190

@psheth9 psheth9 mentioned this pull request Jun 13, 2024
Base automatically changed from ingest-events-in-db to events-db-backend June 18, 2024 04:42
@psheth9
Copy link
Contributor Author

psheth9 commented Jun 18, 2024

ledgerCloseMetaWithEvents in get_events_test does not seem to work during DB ingestion.

Error logs:
unknown tx hash in LedgerCloseMeta: 4705598f90aa1c2affc48ffb10f42ab1a4d9545fcb8ed0adfd6b2ae141aa1c21

But also at the same time txMetaWithEvents in transaction_test works fine for event ingestion so it has to do something with the mocked LCM object itself.

@Shaptic
Copy link
Contributor

Shaptic commented Jun 18, 2024

@psheth9 you're changing the hash of the transaction when you inject events into it after creation which makes it invalid

@psheth9
Copy link
Contributor Author

psheth9 commented Jun 20, 2024

you're changing the hash of the transaction when you inject events into it after creation which makes it invalid

This is not the case as I am not touching any fields related to hash. Also ledgerCloseMetaWithEvents is being used everywhere (in old code) to validate get_events RPC so this should work automatically with new code. Unfortunately it does not :(

@psheth9 psheth9 marked this pull request as ready for review June 20, 2024 15:24
@psheth9 psheth9 changed the title [DRAFT] Add getEvents backed by DB [WIP] Add getEvents backed by DB Jun 20, 2024
@psheth9 psheth9 requested a review from Shaptic June 20, 2024 15:37
@psheth9 psheth9 self-assigned this Jun 21, 2024
@psheth9 psheth9 requested a review from tamirms August 13, 2024 18:58
Copy link
Contributor

@Shaptic Shaptic left a comment

Choose a reason for hiding this comment

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

Left a buncha comments, mostly small stuff and a few questions

cmd/soroban-rpc/internal/daemon/daemon.go Outdated Show resolved Hide resolved
cmd/soroban-rpc/internal/daemon/daemon.go Outdated Show resolved Hide resolved
cmd/soroban-rpc/internal/daemon/daemon.go Outdated Show resolved Hide resolved
cmd/soroban-rpc/internal/daemon/daemon.go Show resolved Hide resolved
cmd/soroban-rpc/internal/db/cursor.go Show resolved Hide resolved
cmd/soroban-rpc/internal/methods/get_events.go Outdated Show resolved Hide resolved
cmd/soroban-rpc/internal/methods/get_events.go Outdated Show resolved Hide resolved
aditya1702 and others added 4 commits August 16, 2024 13:21
* Fix migrations - 1

* Make migrations sequential - 1

* Make migrations sequential - 2

* Fix failing unittest - 1

* Fix linting errors - 1

* Fix failing integration test - 1

* Remove %w from Fatal strings

* refactor migrationApplierFactoryF

* Add ledger seq to fatal error string

* Add comments - 1

* Fix - 1

* Optimise migrations - 1

* Optimise migrations - 2

* Optimise migrations - 3

* Fix linting - 1

* Fix linting - 2

* Remove dupicate latest ledger fetch code

* Rollback db in daemon instead of migration

* Remove unused constant

* Remove unused constant - 2

* Add rollback() statement

* Small change

* Abstract transaction and rollback management inside migration code

* Fix failing unittest
@psheth9 psheth9 requested a review from Shaptic August 20, 2024 19:26
Copy link
Contributor

@Shaptic Shaptic left a comment

Choose a reason for hiding this comment

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

LGTM! I'd like @2opremio to have a last look, though, since he was driving most of the reviews. We should definitely add Prometheus metrics to these in a separate PR.

Comment on lines +158 to +165
func getEventTypeXDRFromEventType() map[string]xdr.ContractEventType {
return map[string]xdr.ContractEventType{
EventTypeSystem: xdr.ContractEventTypeSystem,
EventTypeContract: xdr.ContractEventTypeContract,
EventTypeDiagnostic: xdr.ContractEventTypeDiagnostic,
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Can/Should this just be a var at the top of the file? You could then use len(EventTypeMap) instead of a hardcoded 3 constant elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well initially it was var same as eventTypeFromXDR but linter does not like global var :( so it was more of temp fix. it will all be gone when we target all of this lint error together to have consistency in code.

@psheth9
Copy link
Contributor Author

psheth9 commented Aug 20, 2024

@Shaptic Thanks for review comments and stamp !!

Yes anyway I have to create final PR for events-db-backend to main. @2opremio you can take a final look there. Thank you :)

@psheth9 psheth9 merged commit a7e6459 into events-db-backend Aug 20, 2024
17 checks passed
@psheth9 psheth9 deleted the refactor-get-events branch August 20, 2024 20:19
@psheth9 psheth9 mentioned this pull request Aug 20, 2024
psheth9 added a commit that referenced this pull request Aug 24, 2024
* Ingest events into DB (#192)

* Ingest events into DB

* Update tests

* Update tests

* Fix tests

* Ignore ingestion when events are empty

* Add getEvents backed by DB  (#215)

* Ingest events into DB

* Update tests

* Update tests

* Fix tests

* Ignore ingestion when events are empty

* Add getEvents backed by DB

* Refactor getEvents to call fetch events from db

* Remove in-memory events store

* Make use of cursor in pagination and combine filters

* Update scan function logic in order to test

* Move NewTestDb util

* Trim events db and remove eventTypes in SELECT query as event types are not indexed

* Introduce cursor range and add logs for latency

* remove event memory store

* remove event memory store from latest merge

* Fix lint issues part 1

* Fix lint issues part 2

* Fix more lint and add ledger range code for events

* Update eventHandler mock

* Fix 2 major errors in tests: nil pointer reference and unknown hash

* Fix contract Id filter logic and add cursor set to avoid duplicates

* Validate requested start ledger with stored ledger range

* Fix lint error part 4

* Add migration for events table

* Fix lint error part 5

* Fix lint error part 6

* Address review comments pt1

* Make contract id a blob type

* Remove events package and move cursor.go to db package

* Fix lint error part 6

* Fix lint error part 7

* Optimize migration code

* Introduce endLedger and remove Cursor id from schema

* Use LedgerReader to get Ledger Range in events

* Fix lint errors

* Add benchmark for testing various load parameter

* update benchmark

* Comment benchmark events

* Reduce allocs pt1

* Benchmark with 30 million events

* Refactor getEvents to backed only by Events table

* change test db path

* Use Binary encoding for saving events into DB

* Correct number of topics

* reduce events in benchmark so that tests run

* update events schema

* Fix topic count

* Fix fetch query to not stop if null topic

* Fix lint pt1

* Fix trimEvents and lint errors

* update log info

* Fix more lint errors

* Fix more lint errors pt 11

* Fix format in error

* Fix linter error pt 12

* Add nolint for GetEvents as a temp fix.

* Add events table migration (#262)

* Fix migrations - 1

* Make migrations sequential - 1

* Make migrations sequential - 2

* Fix failing unittest - 1

* Fix linting errors - 1

* Fix failing integration test - 1

* Remove %w from Fatal strings

* refactor migrationApplierFactoryF

* Add ledger seq to fatal error string

* Add comments - 1

* Fix - 1

* Optimise migrations - 1

* Optimise migrations - 2

* Optimise migrations - 3

* Fix linting - 1

* Fix linting - 2

* Remove dupicate latest ledger fetch code

* Rollback db in daemon instead of migration

* Remove unused constant

* Remove unused constant - 2

* Add rollback() statement

* Small change

* Abstract transaction and rollback management inside migration code

* Fix failing unittest

* Address review comments

* Store binary of topics instead of string

* Unify min/max topic count in event.go

* Address review comments for event Types and fix unit tests

* cleanup

* Fix linter errors for one last time

---------

Co-authored-by: Aditya Vyas <aditya.vyas@stellar.org>

* Fix linter errors pt 1

* Add nolint for InsertEvents

* Add nolint for InsertEvents again

* Address review comments

* Fix lint

* Fix lint pt1

* Fix lint pt2

* Fix index out of bound error and fix logging

* Add changelog entry

---------

Co-authored-by: Aditya Vyas <aditya.vyas@stellar.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change Any changes to existing API or addition of new API
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants