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

Add tasks status definition + use HashedWheelTimer in waitFor #393

Merged
merged 4 commits into from
Aug 3, 2017

Conversation

jlogeart
Copy link
Contributor

@jlogeart jlogeart commented Aug 1, 2017

This PR makes two changes:

  • Adds a TaskStatusDefinition to simply query the status of task without waiting for it
  • In the WaitForTaskDefinition, replaces the creation of Timer with a HashedWheelTimer

The motivation of the first change is to allow the caller to handle the retries (if any) instead of imposing a strategy. It also allows not to create a new Timer (and therefore a new Thread) for each call.

The motivation of the second change is to allow multiple waitFors without creating new timer Threads for each one.

At Flow, we ran into issues while checking the status of multiple index tasks with aggressive baseDelay as it ended up creating several thousands of Threads. The recursive implementation of the WaitForTaskDsl.request allows for the creation of several Threads per waitFor call. In our case, it lead to java.lang.OutOfMemoryError: unable to create new native thread.

I understand we do not want to encourage clients to issue thousand of calls at once but again in our case, a few waitFors with short baseDelay lead to the creation of several thousands threads and eventually to a painful death ;-)

Since extreme time precision is not a requirement, the proposed solution therefore uses a HashedWheelTimer with a tick duration of 50ms. This number is roughly in the order of a network round trip. That would allow to defer the calls to the underlying http client.

Please feel free to modify :)

Copy link
Contributor

@ElPicador ElPicador 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 the PR, it's indeed a nice new feature

eventually {
val status = client.execute { getStatus task task from "indexToGetTaskStatus" }
whenReady(status) { result =>
result.status == "published"
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 there be a assertion here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Same question for the test below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah you are right. Fixed.


val task = taskShouldBeCreated(create)

eventually {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need the eventually?

Copy link
Contributor

Choose a reason for hiding this comment

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

Same question for the test below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need eventually because it takes some time for a task to be actually published: the very frirst call may indicate the task as "pending" and not "published".

So eventually retries (as defined in Eventually) until the block does not throw any exception (in our case a TestFailedException if the status is not "published").

Copy link
Contributor

Choose a reason for hiding this comment

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

ok make sense

@@ -64,6 +60,19 @@ trait WaitForTaskDsl {
implicit object WaitForTaskDefinitionExecutable
extends Executable[WaitForTaskDefinition, TaskStatus] {

// Run every 50 ms, use a wheel with 512 buckets
private lazy val timer = {
val threadFactory = new ThreadFactory {
Copy link
Contributor

Choose a reason for hiding this comment

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

couldn't we reuse the thread pool from the execution context?
Not a big fan of hiding a theadFactory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This ThreadFactory is used to create the worker thread of the wheel, and nothing else. This is the thread that runs the "timed out" timer tasks. The problem of using the given ExecutionContext is that we impose the creation of a long running worker thread in the pool. So if the client passes in a single thread pool, we are left with no thread to execute the map operations over the returned Futures. Also if the created thread is not a daemon thread, it may lead to complications when terminating the application.

In a nutshell, I think it makes more sense for the library to take care of its own worker thread and prevent the user from having to care about it. (That is basically what the current implementation using Timer does!)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you are right the current implementation was using an internal pool of threads, but I was not very big fan of it.
At least, could you expose the number of threads of this pool in the configuration? So people could tweak it.

Copy link
Contributor Author

@jlogeart jlogeart Aug 2, 2017

Choose a reason for hiding this comment

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

It will always be one thread. The real configuration is what HashedWheelTimer offers as parameters:

  • ThreadFactory to determine how to create this single worker thread
  • the tickDuration: how often are timed out tasks being executed by the worker thread
  • the size of the wheel

I am not sure users care too much about all of them but maybe the tickDuration if they are willing to go for "more often" or "more batching". I guess we can add a tickDuration as a FiniteDuration in the WaitForTaskDefinition. It exposes the internals though.

Something like:

case class WaitForTaskDefinition(taskId: Long,
                             index: Option[String] = None,
                             baseDelay: Long = 100,
                             maxDelay: Long = Long.MaxValue,
                             tickDuration: Long = 50)  // using Long here for consistency with the other fields
...

new HashedWheelTimer(threadFactory, query.tickDuration, TimeUnit.MILLISECONDS, 512)

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

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 the whole point being to reuse this HashedWheelTimer for all submitted queries, there is no way to use the query itself to create it.

Given that it is used for network calls, I think 50 or 100 ms is a good number, especially considering the default base delay is 100ms and then 200, 400 etc.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine by me 😄

@jlogeart
Copy link
Contributor Author

jlogeart commented Aug 3, 2017

Are more changes required before merging?

@ElPicador ElPicador merged commit 4b528a1 into algolia:master Aug 3, 2017
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

2 participants