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

Use HttpBackgroundJob instead of Deferred Tasks during search indexing #14063

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

hyzx86
Copy link
Contributor

@hyzx86 hyzx86 commented Aug 3, 2023

relate: #10858 (comment)

Summary by CodeRabbit

  • New Features

    • Enhanced search indexing to be handled asynchronously post-HTTP request across various modules, improving performance without blocking user interactions.
  • Refactor

    • Transitioned from immediate to deferred indexing methods in several backend processes to optimize system responsiveness and resource management.
  • Tests

    • Updated test scenarios to adapt to new asynchronous indexing behavior, ensuring reliability and accuracy in search functionalities.

@jtkech
Copy link
Member

jtkech commented Aug 3, 2023

I will review soon, maybe also in ElasticIndexingContentHandler, maybe some other places.

In the meantime I see one problem, e.g. when we publish an item from a BackgroundTask, in this context we can't trigger a BackgroundJob, we would need the same check done in the extension itself.

if (httpContextAccessor.HttpContext == null ||
    httpContextAccessor.HttpContext.Items.TryGetValue("IsBackground", out _))

Need to check why unit tests fail.

Also, will need to be reviewed by @Skrypt.


Hmm, also when executing recipes, each step is executed in its own scope, a step may update content items, then a subsequent step may need these items to be already indexed, which would not be the case if we wait the end of the request in place of waiting the end of the step scope.

Maybe this is why unit tests are failing, will think about it tomorrow.

@jtkech
Copy link
Member

jtkech commented Aug 8, 2023

@hyzx86 Okay I fixed the unit tests by adding a delay in 3 places.

// Search indexes are no longer updated in a deferred task at the end of a shell scope
// but in a background job after the http request, so they are not already up to date.
await Task.Delay(1_500);

Not sure it's a common case to update an index and just after do a query that should rely on this index update, but at least it will break those that are using the same kind of unit tests.

Hmm, finally maybe okay when executed through a recipe.

But still the problem when for example we publish an item in a background task, we would need to check it (easy to do) and then still use the deferred task.

OR at the end of a background task, in the same way we set an HttpContext, we could clear it (as aspnetcore does at the end of a request) and maybe the ActionContext too (as aspnetcore doesn't).

@Piedone Piedone changed the title Use HttpBackgourndJob to replace ShellScope AddDeferredTask Use HttpBackgroundJob to replace ShellScope AddDeferredTask Jan 10, 2024
Copy link
Member

@Piedone Piedone left a comment

Choose a reason for hiding this comment

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

I sincerely apologize for us taking so much time here. I checked out the PR, going over old ones, and it needs changes before it can be merged.

@Piedone
Copy link
Member

Piedone commented Mar 21, 2024

Is this something you'd like to revisit any time soon @hyzx86 or should we close?

@Skrypt
Copy link
Contributor

Skrypt commented Mar 22, 2024

This should work after merging #13721 as it will no longer do a SQL query in the LuceneIndexingContentHandler. Though same logic needs to be replied in ElasticIndexingContentHandler and AzureAISearch one too.

Comment on lines 60 to 61
&& "new version".Equals(nodes[0]["displayText"].ToString())
&& "second content item display text".Equals(nodes[1]["displayText"].ToString())
Copy link
Member

Choose a reason for hiding this comment

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

Put the strings to the right of the operator, and simply use ==.

&& "second content item display text".Equals(nodes[1]["displayText"].ToString())
)
{
Assert.True(true);
Copy link
Member

Choose a reason for hiding this comment

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

This is a no-op.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I don't see what you mean. Do I need to change the code here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

@Piedone
Copy link
Member

Piedone commented Mar 24, 2024

So, let's revisit this after #13721 is merged. @Skrypt will you be able to review this one after that too, or should somebody else do it?

hyzx86 and others added 3 commits March 25, 2024 14:14
@hyzx86 hyzx86 requested a review from Piedone March 25, 2024 06:19
@Piedone
Copy link
Member

Piedone commented Mar 25, 2024

While I pointed out some small issues, I'll only review this is Jasmin won't.

@Piedone Piedone changed the title Use HttpBackgroundJob to replace ShellScope AddDeferredTask Use HttpBackgroundJob instead of Deferred Tasks during search indexing Apr 21, 2024
@@ -81,6 +81,10 @@ public virtual async Task InitializeAsync()
TenantName = tenantName;
}

// Search indexes are no longer updated in a deferred task at the end of a shell scope
// but in a background job after the http request, so they are not already up to date.
await Task.Delay(2_000);
Copy link
Member

Choose a reason for hiding this comment

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

This can make the test flaky. Instead, explicitly wait for the process to finish, don't really on occasionally correct time.

See how you fixed this under another PR of yours when I pointed it out.

Also in RecentBlogPostsQueryTests.

Copy link
Contributor

coderabbitai bot commented Apr 30, 2024

Walkthrough

The recent updates in OrchardCore involve transitioning indexing operations from executing at the end of a shell scope to a background job after an HTTP request. This change aims to boost performance by running the indexing process asynchronously. It impacts Lucene and Elasticsearch handlers, along with related test scenarios, ensuring indexing aligns with the new asynchronous execution model.

Changes

File Path Change Summary
.../Lucene/Handler/LuceneIndexingContentHandler.cs
.../Elasticsearch.Core/Handlers/ElasticIndexingContentHandler.cs
Replaced local variable and ShellScope.AddDeferredTask with HttpBackgroundJob.ExecuteAfterEndOfRequestAsync for asynchronous content indexing. Added using OrchardCore.BackgroundJobs;.
.../ContentManagement/DeploymentPlans/ContentStepLuceneQueryTests.cs
.../GraphQL/Queries/RecentBlogPostsQueryTests.cs
.../Context/SiteContext.cs
Updated the timing of search index updates to use background jobs post-HTTP request, affecting test timings and necessitating delays in test scenarios. Added relevant using statements. Adjusted SiteContext to include a delay in InitializeAsync.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

@hyzx86
Copy link
Contributor Author

hyzx86 commented Apr 30, 2024

@coderabbitai review

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Out of diff range and nitpick comments (1)
test/OrchardCore.Tests/Utilities/TimeoutTaskRunner.cs (1)

14-14: Reduce the delay interval or make it configurable.

Currently, there is a hardcoded delay of 500 milliseconds in each iteration of the loop. This might not be optimal for all scenarios and could lead to unnecessary delays in tests. Consider making this delay configurable or reducing it to speed up the checks.

@hyzx86
Copy link
Contributor Author

hyzx86 commented May 1, 2024

@Piedone , This PR is ready to review. 😊

@Piedone
Copy link
Member

Piedone commented May 2, 2024

This is meant to be applicable only after #13721, no?

@hyzx86

This comment was marked as off-topic.

@hyzx86
Copy link
Contributor Author

hyzx86 commented May 2, 2024

This is meant to be applicable only after #13721, no?

Okay, I did get the order wrong, but this PR I think is done.

@hyzx86 hyzx86 marked this pull request as draft May 2, 2024 05:18
@Piedone
Copy link
Member

Piedone commented May 2, 2024

Please let me know when the other one is merged and we can get back to this.

Copy link

This pull request has merge conflicts. Please resolve those before requesting a review.

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.

None yet

6 participants