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

fix: use rpc for pins upsert #1088

Merged
merged 14 commits into from
Mar 16, 2022
Merged

fix: use rpc for pins upsert #1088

merged 14 commits into from
Mar 16, 2022

Conversation

GaryHomewood
Copy link
Contributor

@GaryHomewood GaryHomewood commented Mar 9, 2022

Revert to using a DB function to upsert pins so that we have the location id.

fixes #1082

@flea89 flea89 self-assigned this Mar 9, 2022
Copy link
Contributor

@flea89 flea89 left a comment

Choose a reason for hiding this comment

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

This should fix it. 🤞
I've left a couple of comments.

BEGIN
FOREACH pin_data IN array json_arr_to_json_element_array(data -> 'pins')
LOOP
-- Add to pin_location table if new
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we be calling upsert_pin here instead?
We'd avoid some duplication. There's an inconsistency with the data inputs, in one function we use content_cid and in the other we usecid, but I think that can be easily changed? And we'd benefit from a bit more consistency?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Corrected to re-use upsert_pin and to return an array of pin Ids.
Also changed to use content_cid consistenly.


if (error) {
throw new DBError(error)
}

return data
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm not mistaken the rpc call is currently returning void, so there's no actual useful data to return here.
It'd be probably nice to return an array of the inserted pin ids, to be consistent with upsertPin. If not we get rid of the return statement here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think we could be consistent and return Pin Ids

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Amended to return pinIds. Also amended upsert_pin to return pin_id not pin_location_id.

Copy link
Contributor

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

Thanks for putting this together @GaryHomewood Overall looking good, let's wait for #1093 to get in to have ipfsPeerId column in location. And please look into @flea89's good suggestions

location: {
peer_id: pin.location.peerId,
peer_name: pin.location.peerName,
region: pin.location.region
Copy link
Contributor

Choose a reason for hiding this comment

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

Be aware this will change soon (today) to have one more thing: https://github.com/web3-storage/web3.storage/pull/1093/files

So let's wait on that PR to get in and update this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Merged latest to include ipfs peer id.

Copy link
Contributor

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

Nice, it looks much cleaner now 👌🏼
Left some comments on migration scripts to wrap thiss up

@@ -0,0 +1,59 @@
DROP FUNCTION IF EXISTS upsert_pin;
Copy link
Contributor

Choose a reason for hiding this comment

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

This files is not needed anymore right? We already have a migration 004

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's true, have removed.

insert into pin (content_cid, status, pin_location_id, updated_at)
values (data ->> 'content_cid',
(data -> 'pin' ->> 'status')::pin_status_type,
pin_location_result_id,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change? It is not in functions.sql, so I guess we don't need it?

With that in mind, we are not changing upsert_pin and only upsert_pins should be in the migration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also true, have removed. Migration is just the new function.

Copy link
Contributor

@vasco-santos vasco-santos left a comment

Choose a reason for hiding this comment

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

LGTM

Please add a comment in release PR for API to not forget about running migration script

@GaryHomewood GaryHomewood merged commit 6a8e394 into main Mar 16, 2022
@GaryHomewood GaryHomewood deleted the fix/1082-upsert-pins-error branch March 16, 2022 15:46
@alanshaw
Copy link
Member

The cron job has changed in the PR and it now relies on a function that currently does not exist in the production DB. So for 2 days now the pins job has been failing because it can't update pins.

@flea89
Copy link
Contributor

flea89 commented Mar 18, 2022

@alanshaw good catch.

TL;DR
As I'm sure you already figure out the fix is releasing this and running the migration that has been added there.

However, this raises some questions for me.

How can we avoid this in the future?
It doesn't seem documentation or processes make it really clear that any cron code that lands in main is automatically in production, and there's no safe-guarding against it, isn't it?
Even if you do know about it, as this PR has proved, it's really easy to forget?

I wonder if we should have a release step for cron code as well?
ie. Running the cron from a "prod" branch and automating that through release-please could be a way?
This is just the first idea that pop to mind, I'm sure there are better ones if we think about it.

@alanshaw
Copy link
Member

Extracting from the convo here: https://filecoinproject.slack.com/archives/C02BZPRS9HP/p1647599946603429

I'm open to suggestions for making this better so it doesn't happen again.

We could maybe do something where cron is included in a release please process and the yaml is copied (and committed) from the project dir to the root on release?

@flea89 as we both suggested, adding it to release please might help. I think it's unexpected for changes to cron to immediately be in production when that is not the case for the rest of the packages in the repo. Any chance you can open an issue so we can track? 🙏🙏🙏

@flea89
Copy link
Contributor

flea89 commented Mar 18, 2022

Sure, here it is!

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.

Upsert pins failing with null value for pin_location_id
4 participants