bdr: conflict handlers: refactored handler invocation
authorChristian Kruse <[email protected]>
Thu, 8 May 2014 10:17:58 +0000 (12:17 +0200)
committerAndres Freund <[email protected]>
Thu, 3 Jul 2014 15:55:32 +0000 (17:55 +0200)
In the previous code we forgot in one case to call conflict
handlers. This lead to calling them in each if branch. Thus we decided
to refactor the code to call the handlers above the code.

contrib/bdr/bdr_apply.c

index 9604c736b25b25f9b916f78ff7f66d9a5f1a1a38..b55af2cf4fc65bb79a325f00bcb0003562ce05d9 100644 (file)
@@ -832,6 +832,44 @@ check_apply_update(RepNodeId local_node_id, TimestampTz local_ts,
         * sysid + TLI to discern.
         */
 
+       if (new_tuple)
+       {
+           /*
+            * We let users decide how the conflict should be resolved; if no
+            * trigger could be found or if the trigger decided not to care,
+            * we fall back to „last update wins“
+            */
+
+           TimestampDifference(replication_origin_timestamp, local_ts,
+                               &secs, &microsecs);
+
+           *new_tuple = bdr_conflict_handlers_resolve(rel, local_tuple,
+                                                      remote_tuple, "UPDATE",
+                                                      BDRUpdateUpdateConflictHandler,
+                                                      abs(secs) * 1000000 + abs(microsecs),
+                                                      &skip);
+
+           if (skip)
+           {
+               *perform_update = false;
+               *log_update = false;
+               return;
+           }
+           else if (*new_tuple)
+           {
+               *perform_update = true;
+               *log_update = true;
+               return;
+           }
+
+
+           /*
+            * if user decided not to skip the conflict but didn't provide a
+            * resolving tuple we fall back to default handling
+            */
+       }
+
+
        cmp = timestamptz_cmp_internal(replication_origin_timestamp, local_ts);
 
        if (cmp > 0)
@@ -847,72 +885,29 @@ check_apply_update(RepNodeId local_node_id, TimestampTz local_ts,
            fetch_sysid_via_node_id(bdr_apply_worker->origin_id,
                                    &remote_sysid, &remote_tli);
 
-           /*
-            * We let users decide how the conflict should be resolved; if no
-            * trigger could be found or if the trigger decided not to care,
-            * we fall back to „last update wins“
-            *
-            * Since the comparison of the two timestamps is 0 we simply give
-            * 0 as the timeframe as well.
-            */
-           if (new_tuple)
-               *new_tuple = bdr_conflict_handlers_resolve(rel, local_tuple,
-                                                     remote_tuple, "UPDATE",
-                                             BDRUpdateUpdateConflictHandler,
-                                                          0, &skip);
-
            /*
             * Always ignore this update if the user decides to; otherwise
             * apply the user tuple or, if none supplied, fall back to „last
             * update wins“
             */
-           if (!skip)
-           {
-               if (new_tuple == NULL || *new_tuple == NULL)
-               {
-                   if (local_sysid < remote_sysid)
-                       *perform_update = true;
-                   else if (local_sysid > remote_sysid)
-                       *perform_update = false;
-                   else if (local_tli < remote_tli)
-                       *perform_update = true;
-                   else if (local_tli > remote_tli)
-                       *perform_update = false;
-                   else
-                       /* shouldn't happen */
-                       elog(ERROR, "unsuccessful node comparison");
-               }
-               else
-                   *perform_update = true;
-           }
-           else
+           if (local_sysid < remote_sysid)
+               *perform_update = true;
+           else if (local_sysid > remote_sysid)
+               *perform_update = false;
+           else if (local_tli < remote_tli)
+               *perform_update = true;
+           else if (local_tli > remote_tli)
                *perform_update = false;
+           else
+               /* shouldn't happen */
+               elog(ERROR, "unsuccessful node comparison");
 
            *log_update = true;
            return;
        }
        else
        {
-           TimestampDifference(replication_origin_timestamp, local_ts,
-                               &secs, &microsecs);
-
-           /*
-            * We let users decide how the conflict should be resolved; if no
-            * trigger could be found or if the triggers decide not to care,
-            * we fall back to „last update wins“
-            */
-           if (new_tuple)
-               *new_tuple = bdr_conflict_handlers_resolve(rel, local_tuple,
-                                                     remote_tuple, "UPDATE",
-                                             BDRUpdateUpdateConflictHandler,
-                                       abs(secs) * 1000000 + abs(microsecs),
-                                                          &skip);
-
-           if (new_tuple == NULL || *new_tuple == NULL || skip)
-               *perform_update = false;
-           else
-               *perform_update = true;
-
+           *perform_update = false;
            *log_update = true;
 
            return;