Skip to content

Conversation

jrockway
Copy link
Contributor

@jrockway jrockway commented Sep 25, 2024

This adds the basic snapshot database schema, and a package to put the business logic in.

I have stubbed out some steps for creating snapshots, just to ensure the foreign keys work as expected.

I also added a function to dockertestenv to return a database with migrations already applied. This pattern is cut-n-pasted into a ton of places. I'll clean that up in the near future.

@jrockway jrockway force-pushed the jonathan/mldm-104-snapshot-tables branch from 5ce9746 to d19550e Compare September 25, 2024 21:16
Copy link

codecov bot commented Sep 25, 2024

Codecov Report

Attention: Patch coverage is 72.72727% with 12 lines in your changes missing coverage. Please review.

Project coverage is 62.22%. Comparing base (e57e4b5) to head (c43679e).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
src/internal/recovery/recovery.go 54.54% 10 Missing ⚠️
src/internal/clusterstate/v2.12.0/snapshot.go 85.71% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10358      +/-   ##
==========================================
+ Coverage   62.18%   62.22%   +0.04%     
==========================================
  Files        1219     1221       +2     
  Lines       87663    87706      +43     
  Branches     1820     1820              
==========================================
+ Hits        54513    54578      +65     
+ Misses      32310    32290      -20     
+ Partials      840      838       -2     
Flag Coverage Δ
62.22% <72.72%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jrockway jrockway force-pushed the jonathan/mldm-104-snapshot-tables branch from d19550e to 6bdc734 Compare September 26, 2024 19:14
@jrockway jrockway changed the base branch from muyang-MLDM-100 to master September 26, 2024 19:14
Copy link

github-actions bot commented Sep 26, 2024

size-limit report 📦

Path Size
Entry 93.11 KB (0%)
Vendor XL 800.86 KB (0%)
Chunks 292.47 KB (0%)
Everything 1.16 MB (0%)

snapID, err = createSnapshotRow(ctx, tx, s)
return errors.Wrap(err, "createSnapshotRow")
}); err != nil {
t.Fatalf("WithTx: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use require.NoError here and the following error checks? It can also pass in a message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

There are so many require in our code base. So when to use require and when to use Fatalf?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd say be consistent with the existing tests in the file. If starting a new file, avoid require.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it.

why did we have require?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

People coming from other languages expect to write tests like that, so they added it to the codebase so they could continue doing things their own way instead of the go way. At the end of the day it doesn't really matter, but a lot of our tests abort too soon (meaning you need to run it multiple times to gradually fix an error, rather than being able to confidently have a fixed test after reading the output) because of the require sugar layer. (The open source library we "copied" has "assert" and "require" that Errorf or Fatalf depending on which one was used. This would avoid the bailout-early test.)

The other problem is that people write things like require.Equal(want, len(got)) which prints a message like "want: 1, got: 2" which doesn't answer the follow-up question "ok well what was in got". Hand-written tests tend to not make this mistake.

In general you should use cmp.Diff in these cases as the diff always explains the failure as deeply as necessary to fix the test. There is also require.NoDiff which I don't mind people using in new code, though it's a little uglier than manually calling cmp.Diff in my opinion (and often kills the test prematurely, again because require's thing is to end the test case with Fatalf on failure).

Copy link
Contributor

Choose a reason for hiding this comment

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

This might be useful to be brought up in a team meeting

t.Fatalf("writer.Close: %v", err)
}
if err := dbutil.WithTx(ctx, db, func(ctx context.Context, tx *pachsql.Tx) error {
return errors.Wrap(addDatabaseDump(ctx, tx, snapID, *fsID), "addDatabaseDump")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also read from database to make sure they are properly updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be nice, but we don't have any functions to read the rows yet and this PR doesn't need to add them. Really all we're trying to check here is that the foreign key works and doesn't cause an error when adding a presumably-valid fileset id to the row.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I added a test for this.

type SnapshotID int64

// String implements fmt.Stringer.
func (id SnapshotID) String() string {
Copy link
Contributor

@Zhang-Muyang Zhang-Muyang Sep 27, 2024

Choose a reason for hiding this comment

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

For my understanding: Will this String() be ever passed in a id<1? Considering that is invalid and should have raised error in upper stack.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's meant to store a postgres bigserial type, which is an integer from 0 to 2^63-1, but there is no type in Go that has those constraints. No matter type we pick on the Go side, there will be values that can't be used as row IDs. (The alternative is to use uint64 and make [2^63, 2^64-1] the invalid ids.)

@jrockway jrockway force-pushed the jonathan/mldm-104-snapshot-tables branch from 2e7aca1 to c43679e Compare September 30, 2024 19:38
@jrockway
Copy link
Contributor Author

PTAL.

@jrockway jrockway merged commit 49ae9c6 into master Sep 30, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants