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

exp/ingest: ledger backend support #1404

Merged
merged 43 commits into from
Jun 17, 2019

Conversation

ire-and-curses
Copy link
Member

@ire-and-curses ire-and-curses commented Jun 13, 2019

If you're making a doc PR or something tiny where the below is irrelevant, just delete this
template and use a short description.

PR Structure

  • This PR has reasonably narrow scope (if not, break it down into smaller PRs).
  • This PR avoids mixing refactoring changes with feature changes (split into two PRs
    otherwise).
  • This PR's title starts with name of package that is most changed in the PR, ex.
    services/friendbot

Thoroughness

  • This PR adds tests for the most critical parts of the new functionality or fixes.
  • I've updated any docs (developer docs, .md
    files, etc... affected by this change). Take a look in the docs folder for a given service,
    like this one.

Release planning

  • I've updated the relevant CHANGELOG (here for Horizon) if
    needed with deprecations, added features, breaking changes, and DB schema changes.
  • I've decided if this PR requires a new major/minor version according to
    semver, or if it's mainly a patch change. The PR is targeted at the next
    release branch if it's not a patch change.

Summary

Goal and scope

This PR adds support for reading transaction information and headers of individual ledgers from a stellar-core database. This work is part of the ingest project, and is intended to allow a data pipeline to poll for current ledger information for a detailed and current view of the ledger state.

Summary of changes

Following the design described in the ingestion plan, this PR provides

  • interface io.LedgerReadCloser -> implemented by io.DBLedgerReadCloser
  • interface ledgerbackend.LedgerBackend -> implemented by ledgerbackend.DatabaseBackend
  • adapter implementation ingestadapters.LedgerBackendAdapter
  • supporting datastructures (especially LedgerTransaction and LedgerCloseMeta)

A simple demo running against a local stellar-core docker container is included (ingest/cmd/main).

Known limitations & issues

I ran into a number of questions during the implementation and would appreciate any feedback.

  1. I modified some of the interfaces. Specifically, I added Close methods to allow the DB session to be cleared up. I also added an Init method for the LedgerReadCloser, to allow the internal data to be loaded in advance of the first Read() call. Does this make sense?
  2. Should there be an interface that LedgerBackendAdapter should conform to?
  3. I did a lot of manual testing of this code but it needs more unit tests to verify the data that is returned is correct. This is hard to do currently as DB calls are internal to the objects. I could try to refactor the code through dependency injection to allow mock DB objects to be provided, but I'm a little worried it will add a lot of complexity. I'm open to suggestions for how best to do that, or if there's another approach to testing that would be better.
  4. I'm not sure whether I have deserialised the ledger hash correctly, would appreciate a sanity check there.
  5. The returned data is my best guess at what's required for the pipeline. Does it look sufficient, or are there things missing?
  6. During my testing I found it helpful to see a timestamp for the latest ledger sequence number. This is not part of the interface, so I just logged it internally. We could consider returning the timestamp to the caller so they could e.g. monitor staleness.
  7. I used individual raw string queries because they were simple. These could be combined to reduce the number of DB calls if needed. I'm also not sure if there's a better way to construct the queries using the DB objects provided by support/db.
  8. Once final interfaces are merged, the docs should be updated.

What shouldn't be reviewed

Should all be reviewed.

@ire-and-curses ire-and-curses added the ingest New ingestion system label Jun 13, 2019
Copy link
Contributor

@bartekn bartekn left a comment

Choose a reason for hiding this comment

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

Looks good. There are a few things to change but in general it goes in the right direction. I answered your questions in code comments. Let me know if I missed anything.

exp/ingest/adapters/ledger_backend_adapter.go Outdated Show resolved Hide resolved
exp/ingest/adapters/ledger_backend_adapter_test.go Outdated Show resolved Hide resolved
exp/ingest/io/ledger_read_closer.go Outdated Show resolved Hide resolved
exp/ingest/io/ledger_read_closer.go Outdated Show resolved Hide resolved
exp/ingest/io/ledger_read_closer.go Outdated Show resolved Hide resolved
xdr/db.go Show resolved Hide resolved
var _ LedgerBackend = (*DatabaseBackend)(nil)

// DatabaseBackend implements a database data store.
type DatabaseBackend struct {
Copy link
Contributor

@bartekn bartekn Jun 13, 2019

Choose a reason for hiding this comment

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

I'm open to suggestions for how best to do that, or if there's another approach to testing that would be better.

I think we should just mock db.Session:

  1. Create a private interface in ledgerbackend package with methods we use (SelectRaw, GetRaw...).
  2. Create a mock.
  3. Then in tests program a mock to set the results to what you need using Call.Run.

However, I think we need at least a few tests that work against a running database to ensure queries are correct etc. These can be run in CI only.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a great idea. It's not possible at present because we access session.DB.Close() in DatabaseBackend.Close(). Since DB is a struct field, not a method, it can't be accessed through an interface.

One solution would be to add a new method session.Close() to the db package. This could delegate to the underlying *sqlx.DB.Close() for the real thing, and be a no-op for the mock. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, let's do it.

exp/ingest/adapters/history_archive_adapter_test.go Outdated Show resolved Hide resolved
exp/ingest/io/ledger_read_closer.go Show resolved Hide resolved
exp/ingest/ledgerbackend/ledger_backend.go Outdated Show resolved Hide resolved
@ire-and-curses
Copy link
Member Author

@bartekn I've implemented all requested changes except the three unresolved issues above:

  1. No SQL error raised for empty rows
  2. Missing Close() method for session mock interface
  3. Keep or remove redundant index field?

Please take a look and let me know your preferences and I'll finish this up.

@ire-and-curses
Copy link
Member Author

@bartekn remaining changes have been made - PTAL. I can work on adding more detailed unit and CI tests now, but I think I'd prefer to merge this and add them in a new PR, so the code is generally available (it's still in exp), unless you feel strongly the other way.

Copy link
Contributor

@bartekn bartekn left a comment

Choose a reason for hiding this comment

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

Last issues. The one in DBLedgerReadCloser.GetHeader() is important so will approve once it's fixed.

exp/ingest/io/ledger_read_closer.go Outdated Show resolved Hide resolved
exp/ingest/io/ledger_read_closer.go Outdated Show resolved Hide resolved
exp/ingest/io/ledger_read_closer.go Outdated Show resolved Hide resolved
@ire-and-curses ire-and-curses merged commit 62027aa into stellar:master Jun 17, 2019
@ire-and-curses ire-and-curses deleted the es-1285-ledger_backend branch June 17, 2019 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ingest New ingestion system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants