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: make sync opt to be selectable for auto setting update logic #425

Conversation

kiyohara
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
BC breaks? no
Related Issue -
Need Doc update no

Describe your change

In algoliasearch-rails gem 1.x the auto setting update logic run asynchronously.
This behavior has been changed since the version 2.0 release to 'synchronously API call.'
ref. Here is the discussion that considered changing the specification.

It means if you have some settings changes, all API calls hook the setting update API, and are blocked until the settings changing process is finished in Algolia server.

I believe the spec change is reasonable for almost all situations because in many cases API calls require the response based on the new setting immediately.
However, I think, Algolia is a backend for sites that has big records and big client accesses, and is required quick response without '504 Gateway Timeout'.
I guess this is one of the reasons that all methods' default sync option is set as 'asynchronous' in this gem, like the algolia_set_settings method.

I think the best way how to decide the sync behavior, especially focused on "auto setting update logic", is to depend on AlgoliaSearch.configuration's :synchronous option similar to any other method's default option like this.

This PR add sync option select logic in auto setting update func.
It decide a sync option (sync or async) based on the above config.
The default behavior is async, same as 1.x.

What problem is this fixing?

When you deploy some new index settings changes to your site using this gem, your site clients might see '504 Gateway Timeout' when they search anything.


This is a real problem we are experiencing with our site management.
Many 504 responses are occurred when we update index settings.
We believe that a quick response with a little incorrect is better than a slow response that is 100% correct (and sometimes it's 504), for our site customers.
So, we decided to change back gem version to 1.26.0.
Of cause, I understand that it's not all user's requirements.
So, I fix it to depend on AlgoliaSearch.configuration.

Copy link
Contributor

@DevinCodes DevinCodes left a comment

Choose a reason for hiding this comment

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

Hi @kiyohara !

Thank you again for your contribution. I think it's a very valid one, so I'd love to merge this. Before we merge it, though, could you please write a test to ensure it works properly?

Thank you in advance!

(Don't worry about the failing CI for now, this is due to credentials as mentioned in another of your PRs 😉 )

@kiyohara
Copy link
Contributor Author

Hi @DevinCodes.

Thank you for your review.
I'm glad you accepted the concept of this PR.
I wrote tests. Could you check it, please?

Of course, All tests I added have passed in my local dev env.
Please run it in your CI env's rspec.

I'll also add the tests for another PR #426 soon.

@kiyohara kiyohara requested a review from DevinCodes April 11, 2022 13:26
Copy link
Contributor

@DevinCodes DevinCodes left a comment

Choose a reason for hiding this comment

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

I just have one small suggestion, other than that this looks good! Let me know what you think 🙂

end

it 'should not call set_setting!' do
expect_any_instance_of(Algolia::Search::Index).not_to receive(:set_settings)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be as follows?

Suggested change
expect_any_instance_of(Algolia::Search::Index).not_to receive(:set_settings)
expect_any_instance_of(Algolia::Search::Index).not_to receive(:set_settings!)

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 think your suggestion is perfectly right.

On the other hand, I was wondering why this test passed.
After additional investigation, it turns out that I have overlooked the cache mechanism.
When algolia_ensure_init is called more than once, neither set_settings nor set_settings! were being called ever at the second time and after, under any conditions.
I should have used the Red-Green-Refactor approach, but I skipped the Red check phase. embarrassing.

I have fixed the test, please check it out.

…ate_logic' of github.com:kiyohara/algoliasearch-rails into fix/make_sync_opt_to_be_selectable_for_auto_setting_update_logic
@kiyohara kiyohara requested a review from DevinCodes April 15, 2022 13:18
@kiyohara
Copy link
Contributor Author

@DevinCodes I believe the test is more complete thanks to your suggestions.
Thank you so much. It's ready for your review.
Is there anything I can do to help you get started with this PR review?

Copy link
Contributor

@DevinCodes DevinCodes left a comment

Choose a reason for hiding this comment

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

Thank you again @kiyohara ! 🙇‍♂️

The PR looks great, I just have one question. This is probably to some context that I'm missing, but afterwards we can merge the PR I think 😄

end

it 'should call set_setting' do # NOTE: set_setting! call set_setting internally.
expect_any_instance_of(Algolia::Search::Index).to receive(:set_settings).and_call_original
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understand why we need to add the .and_call_original at the end here. Could you explain why this is needed, please?

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 think this is a very good point.

Initially, I was going to write a test here to make sure that set_setting is not executed as opposed to the above test to make sure that set_setting! is executed.
However, I found that set_setting! executes set_setting internally, so I changed it to a test to make sure that set_setting is executed.
set_setting! use the return value of set_setting, so and_call_original was needed.

However, your point has made me realize that.
If set_setting! is using set_setting internally, then there is no point in testing the contrast between the two.
I think the key point is whether it is asynchronous, i.e., whether the wait_task is executed.

I modified the test according to this concept.
May I ask you to check it again, please?

end

it 'should call set_setting' do
expect_any_instance_of(Algolia::Search::Index).to receive(:set_settings).and_call_original
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unlike the previous line, this line is a simple typo.
cand_call_original was not needed. 🙇

@kiyohara kiyohara requested a review from DevinCodes May 11, 2022 13:05
Copy link
Contributor

@DevinCodes DevinCodes left a comment

Choose a reason for hiding this comment

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

Thank you @kiyohara ! 🚀

@DevinCodes DevinCodes merged commit 3fd9038 into algolia:master May 17, 2022
@kiyohara kiyohara deleted the fix/make_sync_opt_to_be_selectable_for_auto_setting_update_logic branch May 18, 2022 22:26
@kiyohara
Copy link
Contributor Author

I'm so happy! 😄

@kiyohara
Copy link
Contributor Author

kiyohara commented Jun 2, 2022

@DevinCodes I'm so happy that this PR was merged. Thank you so much.
BTW, do you have any info on the timeline for when the gem containing this PR would be released?

Half of the several problems with our web service operations have been resolved with the new release of algoliasearch-client-ruby. We are so glad and appreciate you 🙏

We are eagerly waiting for the other half of the issues to be resolved by a new version releasing of algoliasearch-rails 😄

@DevinCodes
Copy link
Contributor

Thank you for the follow-up @kiyohara , and sorry for the delay!

We'll release a new version on Monday. Your changes will be included in that release 🙂

@kiyohara
Copy link
Contributor Author

kiyohara commented Jun 6, 2022

@DevinCodes Sorry for urging you to release a new version. I appreciate your kindness.
Thanks to you I can tell my team members that the Gemfile.lock of our product can be updated. We can continue to use Algolia with more peace of mind.
Thank you again!

@DevinCodes
Copy link
Contributor

@kiyohara we've just released v2.2.1 with your changes. Thank you again for your contributions 🙂

@kiyohara
Copy link
Contributor Author

kiyohara commented Jun 7, 2022

@DevinCodes I'm so glad! 🎉 Thank you for your arrangement of the release 🙏

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