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

Postgres optimized revision caching #555

Merged
merged 2 commits into from
Apr 22, 2022
Merged

Postgres optimized revision caching #555

merged 2 commits into from
Apr 22, 2022

Conversation

jakedt
Copy link
Member

@jakedt jakedt commented Apr 22, 2022

No description provided.

@jakedt jakedt requested a review from a team as a code owner April 22, 2022 04:16
@github-actions github-actions bot added area/datastore Affects the storage system area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools) labels Apr 22, 2022
@jakedt jakedt changed the title Postgres revision caching Postgres optimized revision caching Apr 22, 2022
updateGroup singleflight.Group
}

type validRevision struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 this is a small change but I like it

internal/datastore/postgres/postgres.go Show resolved Hide resolved
@@ -23,7 +24,8 @@ const (
SELECT COALESCE(
(SELECT MIN(%[1]s) FROM %[2]s WHERE %[3]s >= TO_TIMESTAMP(FLOOR(EXTRACT(EPOCH FROM NOW() AT TIME ZONE 'utc') * 1000000000 / %[4]d) * %[4]d / 1000000000)),
(SELECT MAX(%[1]s) FROM %[2]s)
);`
),
%[4]d - CAST(EXTRACT(EPOCH FROM NOW() AT TIME ZONE 'utc') * 1000000000 as bigint) %% %[4]d;`
Copy link
Contributor

Choose a reason for hiding this comment

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

update the comment to note that it's also returning validForNanos now?

@jakedt
Copy link
Member Author

jakedt commented Apr 22, 2022

Updated

Copy link
Contributor

@ecordell ecordell left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@ecordell ecordell left a comment

Choose a reason for hiding this comment

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

unlgtm, the e2e failure looks potentially real

ecordell
ecordell previously approved these changes Apr 22, 2022
Copy link
Contributor

@ecordell ecordell left a comment

Choose a reason for hiding this comment

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

LGTM

// it will remain valid.
type OptimizedRevisionFunction func(context.Context) (rev datastore.Revision, validFor time.Duration, err error)

// NewRemoteClockRevisions returns a RemoteClockRevisions for the given configuration
Copy link
Member

Choose a reason for hiding this comment

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

Fix comment

lastQuantizedRevision, err, _ := cor.updateGroup.Do("", func() (interface{}, error) {
log.Debug().Time("now", localNow).Time("valid", lastRevision.validThrough).Msg("computing new revision")

optimized, validFor, err := cor.optimizedFunc(ctx)
Copy link
Member

Choose a reason for hiding this comment

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

Add a panic here if it is not set? its slightly nicer than just an NPE

Copy link
Member Author

Choose a reason for hiding this comment

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

At the cost of requiring an extra conditional every time.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but I tend to think having panics when its an expected state makes sense; it would mean they forgot to call the assignment method, and that's certainly an expected bug

Copy link
Member

Choose a reason for hiding this comment

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

I wish we had debug-only checks

Copy link
Contributor

Choose a reason for hiding this comment

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

I've seen this done with build tags in https://github.com/dlsniper/debugger and in some etcd fault injection library that I can't find now. I assume the compiler removes no-op functions but I haven't checked.


rvt := localNow.
Add(validFor).
Add(cor.maxRevisionStaleness)
Copy link
Member

Choose a reason for hiding this comment

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

Won't adding these push it above the max staleness?

Copy link
Member Author

Choose a reason for hiding this comment

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

My definition for staleness is "time after which the new revision would have been selected". So in this case we allow for it to be used slightly longer than it's strictly valid for, but which gives us a minimum valid lifetime for any revision we ask the database to compute. E.g you can't get back a revision that's only good for 1 nanosecond.

Copy link
Member

Choose a reason for hiding this comment

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

But why not just do max(validFor, maxRevisionStaleness) then?

}

// CachedOptimizedRevisions does caching and deduplication for requests for optimized revisions.
type CachedOptimizedRevisions struct {
Copy link
Member

Choose a reason for hiding this comment

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

Move above the function defs for this type?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know. All of the fields are private, it's probably less important than the API to interact with it.

Copy link
Member

Choose a reason for hiding this comment

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

True, but it was highly unexpected when I landed on the file

@jakedt jakedt force-pushed the postgres-revision-caching branch 2 times, most recently from 244d25c to 6116688 Compare April 22, 2022 17:39
Copy link
Contributor

@ecordell ecordell left a comment

Choose a reason for hiding this comment

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

LGTM

@jakedt jakedt merged commit 51b1d54 into main Apr 22, 2022
@jakedt jakedt deleted the postgres-revision-caching branch April 22, 2022 19:05
@github-actions github-actions bot locked and limited conversation to collaborators Apr 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/datastore Affects the storage system area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants