summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Vatamaniuc <vatamane@apache.org>2020-03-07 23:53:22 -0500
committerNick Vatamaniuc <nickva@users.noreply.github.com>2020-03-09 15:10:34 -0400
commit17ce741c7cfcb14df7a8f24d38a59a7bf302dd5d (patch)
tree5e44673f313b1c1bdcd64710c913a39ebc9d4889
parent97458c3e906a40949619779caa2c6bc3324e632d (diff)
downloadcouchdb-17ce741c7cfcb14df7a8f24d38a59a7bf302dd5d.tar.gz
Optimize resubmitting pending jobs
Previously even when the scheduled time was the same the job was still deleted and re-inserted into the pending queue. Now we perform the re-enqueing operation only if the scheduled time has changed. So if the whole operation is run in its own transaction, the transaction will now become a read-only transaction. This optimization should come in handy with the indexing auto-builder, for example, where multiple nodes might try to re-enqueue the job and then only the first would succeed, and the rest will perform a quick one row read and not do any writes.
-rw-r--r--src/couch_jobs/src/couch_jobs_fdb.erl11
-rw-r--r--src/couch_jobs/test/couch_jobs_tests.erl11
2 files changed, 22 insertions, 0 deletions
diff --git a/src/couch_jobs/src/couch_jobs_fdb.erl b/src/couch_jobs/src/couch_jobs_fdb.erl
index e59387ee1..8c1ab7ac5 100644
--- a/src/couch_jobs/src/couch_jobs_fdb.erl
+++ b/src/couch_jobs/src/couch_jobs_fdb.erl
@@ -225,6 +225,17 @@ resubmit(#{jtx := true} = JTx0, #{job := true} = Job, NewSTime) ->
data => Data
},
{ok, Job1};
+ pending when STime == OldSTime ->
+ % If pending and scheduled time doesn't change avoid generating
+ % un-necessary writes by removing and re-adding the jobs into the
+ % pending queue.
+ Job1 = Job#{
+ stime => STime,
+ seq => ?PENDING_SEQ,
+ state => pending,
+ data => Data
+ },
+ {ok, Job1};
pending ->
JV1 = JV#jv{seq = ?PENDING_SEQ, stime = STime},
set_job_val(Tx, Key, JV1),
diff --git a/src/couch_jobs/test/couch_jobs_tests.erl b/src/couch_jobs/test/couch_jobs_tests.erl
index 62a75c83e..9d8e2df50 100644
--- a/src/couch_jobs/test/couch_jobs_tests.erl
+++ b/src/couch_jobs/test/couch_jobs_tests.erl
@@ -35,6 +35,7 @@ couch_jobs_basic_test_() ->
[
fun add_remove_pending/1,
fun add_remove_errors/1,
+ fun add_with_the_same_scheduled_time/1,
fun get_job_data_and_state/1,
fun resubmit_as_job_creator/1,
fun type_timeouts_and_server/1,
@@ -159,6 +160,16 @@ add_remove_errors(#{t1 := T, j1 := J}) ->
end).
+add_with_the_same_scheduled_time(#{t1 := T, j1 := J}) ->
+ ?_test(begin
+ ?assertEqual(ok, couch_jobs:add(?TX, T, J, #{})),
+ fabric2_fdb:transactional(fun(Tx) ->
+ ?assertEqual(ok, couch_jobs:add(Tx, T, J, #{})),
+ ?assert(erlfdb:is_read_only(Tx))
+ end)
+ end).
+
+
resubmit_as_job_creator(#{t1 := T, j1 := J}) ->
?_test(begin
Data = #{<<"x">> => 42},