This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch prototype/fdb-layer
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 17ce741c7cfcb14df7a8f24d38a59a7bf302dd5d
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Sat Mar 7 23:53:22 2020 -0500

    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.
---
 src/couch_jobs/src/couch_jobs_fdb.erl    | 11 +++++++++++
 src/couch_jobs/test/couch_jobs_tests.erl | 11 +++++++++++
 2 files changed, 22 insertions(+)

diff --git a/src/couch_jobs/src/couch_jobs_fdb.erl 
b/src/couch_jobs/src/couch_jobs_fdb.erl
index e59387e..8c1ab7a 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 62a75c8..9d8e2df 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},

Reply via email to