Browse Source

enhance(rtc): ensure local-t and remote-t are monotonically increasing,

and remote-t >= local-t
rcmerci 5 months ago
parent
commit
fa3ec6556e

+ 2 - 3
src/main/frontend/worker/rtc/core.cljs

@@ -480,7 +480,7 @@
             (m/?<
             (m/?<
              (m/latest
              (m/latest
               (fn [rtc-state rtc-auto-push? rtc-remote-profile?
               (fn [rtc-state rtc-auto-push? rtc-remote-profile?
-                   rtc-lock online-users pending-local-ops-count local-tx remote-tx]
+                   rtc-lock online-users pending-local-ops-count [local-tx remote-tx]]
                 {:graph-uuid graph-uuid
                 {:graph-uuid graph-uuid
                  :local-graph-schema-version (db-schema/schema-version->string local-graph-schema-version)
                  :local-graph-schema-version (db-schema/schema-version->string local-graph-schema-version)
                  :remote-graph-schema-version (db-schema/schema-version->string remote-graph-schema-version)
                  :remote-graph-schema-version (db-schema/schema-version->string remote-graph-schema-version)
@@ -498,8 +498,7 @@
               (m/watch *rtc-auto-push?) (m/watch *rtc-remote-profile?)
               (m/watch *rtc-auto-push?) (m/watch *rtc-remote-profile?)
               (m/watch *rtc-lock') (m/watch *online-users)
               (m/watch *rtc-lock') (m/watch *online-users)
               (client-op/create-pending-block-ops-count-flow repo)
               (client-op/create-pending-block-ops-count-flow repo)
-              (rtc-log-and-state/create-local-t-flow graph-uuid)
-              (rtc-log-and-state/create-remote-t-flow graph-uuid))))
+              (rtc-log-and-state/create-local&remote-t-flow graph-uuid))))
           (catch Cancelled _))))))
           (catch Cancelled _))))))
 
 
 (def ^:private create-get-state-flow (c.m/throttle 300 create-get-state-flow*))
 (def ^:private create-get-state-flow (c.m/throttle 300 create-get-state-flow*))

+ 27 - 5
src/main/frontend/worker/rtc/log_and_state.cljs

@@ -64,26 +64,48 @@
     (string? v) (uuid v)
     (string? v) (uuid v)
     :else       (throw (ex-info "illegal value" {:data v}))))
     :else       (throw (ex-info "illegal value" {:data v}))))
 
 
-(defn create-local-t-flow
+(defn- create-local-t-flow
   [graph-uuid]
   [graph-uuid]
   (->> (m/watch *graph-uuid->local-t)
   (->> (m/watch *graph-uuid->local-t)
        (m/eduction (keep (fn [m] (get m (ensure-uuid graph-uuid)))))
        (m/eduction (keep (fn [m] (get m (ensure-uuid graph-uuid)))))
        c.m/continue-flow))
        c.m/continue-flow))
 
 
-(defn create-remote-t-flow
+(defn- create-remote-t-flow
   [graph-uuid]
   [graph-uuid]
-  {:pre [(some? graph-uuid)]}
   (->> (m/watch *graph-uuid->remote-t)
   (->> (m/watch *graph-uuid->remote-t)
        (m/eduction (keep (fn [m] (get m (ensure-uuid graph-uuid)))))
        (m/eduction (keep (fn [m] (get m (ensure-uuid graph-uuid)))))
        c.m/continue-flow))
        c.m/continue-flow))
 
 
+(defn create-local&remote-t-flow
+  "ensure local-t <= remote-t"
+  [graph-uuid]
+  (assert (some? graph-uuid))
+  (->> (m/latest vector (create-local-t-flow graph-uuid) (create-remote-t-flow graph-uuid))
+       (m/eduction (filter (fn [[local-t remote-t]] (>= remote-t local-t))))))
+
 (defn update-local-t
 (defn update-local-t
   [graph-uuid local-t]
   [graph-uuid local-t]
-  (swap! *graph-uuid->local-t assoc (ensure-uuid graph-uuid) local-t))
+  (let [graph-uuid (ensure-uuid graph-uuid)
+        current-remote-t (get @*graph-uuid->remote-t graph-uuid)
+        current-local-t (get @*graph-uuid->local-t graph-uuid)]
+    (when (and current-remote-t current-local-t)
+      (assert (and (>= local-t current-local-t) (<= local-t current-remote-t))
+              {:local-t local-t
+               :current-local-t current-local-t
+               :current-remote-t current-remote-t}))
+    (swap! *graph-uuid->local-t assoc graph-uuid local-t)))
 
 
 (defn update-remote-t
 (defn update-remote-t
   [graph-uuid remote-t]
   [graph-uuid remote-t]
-  (swap! *graph-uuid->remote-t assoc (ensure-uuid graph-uuid) remote-t))
+  (let [graph-uuid (ensure-uuid graph-uuid)
+        current-remote-t (get @*graph-uuid->remote-t graph-uuid)
+        current-local-t (get @*graph-uuid->local-t graph-uuid)]
+    (when (and current-remote-t current-local-t)
+      (assert (and (>= remote-t current-remote-t) (>= remote-t current-local-t))
+              {:remote-t remote-t
+               :current-local-t current-local-t
+               :current-remote-t current-remote-t}))
+    (swap! *graph-uuid->remote-t assoc graph-uuid remote-t)))
 
 
 ;;; subscribe-logs, push to frontend
 ;;; subscribe-logs, push to frontend
 ;;; TODO: refactor by using c.m/run-background-task
 ;;; TODO: refactor by using c.m/run-background-task