-
Notifications
You must be signed in to change notification settings - Fork 29
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Algolia | ||
* http://www.algolia.com/ | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package algolia.definitions | ||
|
||
import algolia.http.{GET, HttpPayload} | ||
import algolia.responses.{AlgoliaTask, TaskStatus} | ||
import algolia.{AlgoliaClient, Executable} | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
case class TaskStatusDefinition(taskId: Long, index: Option[String] = None) extends Definition { | ||
|
||
def from(index: String): TaskStatusDefinition = copy(index = Some(index)) | ||
|
||
override private[algolia] def build(): HttpPayload = { | ||
HttpPayload( | ||
GET, | ||
Seq("1", "indexes") ++ index ++ Seq("task", taskId.toString), | ||
isSearch = true | ||
) | ||
} | ||
|
||
} | ||
|
||
trait TaskStatusDsl { | ||
|
||
case object getStatus { | ||
def task(task: AlgoliaTask): TaskStatusDefinition = | ||
TaskStatusDefinition(task.idToWaitFor()) | ||
} | ||
|
||
implicit object TaskStatusExecutable extends Executable[TaskStatusDefinition, TaskStatus] { | ||
|
||
override def apply(client: AlgoliaClient, query: TaskStatusDefinition)( | ||
implicit executor: ExecutionContext): Future[TaskStatus] = { | ||
client.request[TaskStatus](query.build()) | ||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Algolia | ||
* http://www.algolia.com/ | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package algolia.dsl | ||
|
||
import algolia.AlgoliaDsl._ | ||
import algolia.AlgoliaTest | ||
import algolia.http.{GET, HttpPayload} | ||
import algolia.responses.Task | ||
|
||
class TaskStatusTest extends AlgoliaTest { | ||
|
||
describe("get task status") { | ||
|
||
it("get the status of a task") { | ||
getStatus task Task(1L, None) from "toto" | ||
} | ||
|
||
it("should call API") { | ||
(getStatus task Task(1L, None) from "toto").build() should be( | ||
HttpPayload( | ||
GET, | ||
Seq("1", "indexes", "toto", "task", "1"), | ||
isSearch = true | ||
) | ||
) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016 Algolia | ||
* http://www.algolia.com/ | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package algolia.integration | ||
|
||
import algolia.AlgoliaDsl._ | ||
import algolia.AlgoliaTest | ||
import algolia.responses.{TaskIndexing, TasksMultipleIndex} | ||
|
||
import scala.concurrent.Future | ||
|
||
class TaskStatusIntegrationTest extends AlgoliaTest { | ||
|
||
after { | ||
clearIndices("indexToGetTaskStatus") | ||
} | ||
|
||
it("should get the status of a single object task") { | ||
val create: Future[TaskIndexing] = client.execute { | ||
index into "indexToGetTaskStatus" `object` ObjectToGet("1", "toto") | ||
} | ||
|
||
val task = taskShouldBeCreated(create) | ||
|
||
eventually { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question for the test below There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need So There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok make sense |
||
val status = client.execute { getStatus task task from "indexToGetTaskStatus" } | ||
whenReady(status) { result => | ||
result.status == "published" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't there be a assertion here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question for the test below There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah you are right. Fixed. |
||
} | ||
} | ||
} | ||
|
||
it("should get the status of a multiple objects task") { | ||
val create: Future[TasksMultipleIndex] = client.execute { | ||
batch( | ||
index into "indexToGetTaskStatus" `object` ObjectToGet("1", "toto"), | ||
index into "indexToGetTaskStatus" `object` ObjectToGet("2", "tata") | ||
) | ||
} | ||
|
||
val task = taskShouldBeCreated(create) | ||
|
||
eventually { | ||
val status = client.execute { getStatus task task from "indexToGetTaskStatus" } | ||
whenReady(status) { result => | ||
result.status == "published" | ||
} | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 givenExecutionContext
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 returnedFutures
. 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!)There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 threadtickDuration
: how often are timed out tasks being executed by the worker threadI 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 atickDuration
as aFiniteDuration
in theWaitForTaskDefinition
. It exposes the internals though.Something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine by me 😄