Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

GDGT-3400: give each channel of a dashboard subscription its own queue key (#84849)

Bartosz Rybacki70b09748da53
diff --git a/src/metabase/notification/send.clj b/src/metabase/notification/send.clj
index 8c679839175..07886167504 100644
--- a/src/metabase/notification/send.clj
+++ b/src/metabase/notification/send.clj
@@ -369,11 +369,12 @@
(defprotocol NotificationQueueProtocol
"Protocol for notification queue implementations."
- (put-notification! [this notification] "Add a notification to the queue. If a notification with the same id is already in the queue, replace it and return the replaced one.")
+ (put-notification! [this notification] "Add a notification to the queue. If a notification with the same queue key is already in the queue, replace it and return the replaced one. The queue key is `:notification/queue-key` in the notification's metadata, else its `:id`.")
(take-notification-with-timeout! [this timeout-ms] "Take the next notification from the queue, returning nil if timeout is reached.")
(queue-size [this] "Get the current size of the queue."))
-;; A priority queue that deduplicates notifications by id
+;; A priority queue that deduplicates notifications by queue key: `:notification/queue-key` in the
+;; notification's metadata, else its `:id`
(deftype ^:private DedupPriorityQueue
[^java.util.PriorityQueue items-list
^java.util.concurrent.ConcurrentHashMap id->notification
@@ -381,7 +382,7 @@
^java.util.concurrent.locks.Condition not-empty-cond]
NotificationQueueProtocol
(put-notification! [_ notification]
- (let [id (or (:id notification) (str (random-uuid)))
+ (let [id (or (:notification/queue-key (meta notification)) (:id notification) (str (random-uuid)))
item (NotificationQueueEntry. id (subscription->deadline (:triggering_subscription notification)))]
(.lock queue-lock)
(try
@@ -418,9 +419,9 @@
(defn- create-dedup-priority-queue
"A thread-safe, prioritized notification queue with the following properties:
- - Notifications are identified by unique IDs
+ - Notifications are identified by their queue key: `:notification/queue-key` in the metadata, else `:id`
- Prioritized by deadline
- - Adding a notification with an ID already in the queue replaces the existing one
+ - Adding a notification with a queue key already in the queue replaces the existing one
- Taking from an empty queue blocks until a notification is available
- Multiple threads can safely add and take from the queue concurrently"
[]
@@ -468,11 +469,13 @@
(defn- create-notification-dispatcher
"Create a thread pool for sending notifications.
- There can only be one notification with the same id in the queue.
- - if a notification of the same id is already in the queue, then replace it
+ There can only be one notification with the same queue key in the queue. The queue key is
+ `:notification/queue-key` in the notification's metadata, else its `:id`. Each channel of a dashboard
+ subscription sets its own queue key, because all channels have the pulse id as `:id`.
+ - if a notification with the same queue key is already in the queue, then replace it
(we keep the latest version because it likely contains the most up-to-date information
such as: creator_id, active status, handlers info etc.)
- - if a notification doesn't have id, put it into queue regardless (used to send unsaved notifications)
+ - if a notification has no queue key and no id, put it into the queue regardless (used to send unsaved notifications)
Returns a map with :dispatch-fn and :shutdown-fn."
[pool-size queue]
@@ -593,7 +596,7 @@
(assoc :auto-complete sync?))
(log/with-context {:notification_id (:id notification)
:payload_type (:payload_type notification)}
- (let [notification (with-meta notification {:notification/triggered-at-ns (u/start-timer)})]
+ (let [notification (vary-meta notification assoc :notification/triggered-at-ns (u/start-timer))]
(log/debugf "Will be send %s" (if sync? "synchronously" "asynchronously"))
(if sync?
(send-notification-sync! notification)
diff --git a/src/metabase/pulse/send.clj b/src/metabase/pulse/send.clj
index 44119c6559e..33bfedb3a76 100644
--- a/src/metabase/pulse/send.clj
+++ b/src/metabase/pulse/send.clj
@@ -136,7 +136,11 @@
(let [delivery-failures (volatile! [])]
(doseq [pulse-channel channels]
(try
- (notification/send-notification! (notification-info pulse dashboard pulse-channel) :notification/sync? (not async?))
+ ;; every channel has the pulse id as `:id`, so each one gets its own queue key: the notification queue would
+ ;; otherwise replace a queued channel with the next one
+ (notification/send-notification! (vary-meta (notification-info pulse dashboard pulse-channel)
+ assoc :notification/queue-key [(:id pulse) (:id pulse-channel)])
+ :notification/sync? (not async?))
(catch Exception e
(log/errorf "[Pulse %d] Error sending to %s channel: %s" (:id pulse) (:channel_type pulse-channel) (ex-message e))
(when (delivery-failure? e)
diff --git a/test/metabase/notification/send_test.clj b/test/metabase/notification/send_test.clj
index 74985efb640..2450056d454 100644
--- a/test/metabase/notification/send_test.clj
+++ b/test/metabase/notification/send_test.clj
@@ -800,6 +800,14 @@
"put returns the notification that it replaced")
(is (= {:id 1 :payload_type :notification/testing :test-value "B"}
(take-notification! queue)))))
+ (testing "notifications with the same ID and different queue keys both stay in the queue (GDGT-3400)"
+ (let [queue (#'notification.send/create-dedup-priority-queue)]
+ (#'notification.send/put-notification! queue (with-meta {:id 1 :payload_type :notification/testing :test-value "A"}
+ {:notification/queue-key [1 11]}))
+ (#'notification.send/put-notification! queue (with-meta {:id 1 :payload_type :notification/testing :test-value "B"}
+ {:notification/queue-key [1 12]}))
+ (is (= #{"A" "B"}
+ (set (map :test-value [(take-notification! queue) (take-notification! queue)]))))))
(testing "multiple notifications are processed in order"
(let [queue (#'notification.send/create-dedup-priority-queue)]
(#'notification.send/put-notification! queue {:id 1 :payload_type :notification/testing :test-value "A"})
diff --git a/test/metabase/pulse/send_test.clj b/test/metabase/pulse/send_test.clj
index 7cb68ea05b7..f9f64a39918 100644
--- a/test/metabase/pulse/send_test.clj
+++ b/test/metabase/pulse/send_test.clj
@@ -11,6 +11,7 @@
[metabase.channel.render.body :as body]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
+ [metabase.notification.send :as notification.send]
[metabase.notification.test-util :as notification.tu]
[metabase.pulse.models.pulse :as models.pulse]
[metabase.pulse.send :as pulse.send]
@@ -677,6 +678,29 @@
(is (= #{:channel/email :channel/slack}
(set (map :channel_type (:failed-handlers (ex-data e))))))))))))
+(deftest channels-of-one-pulse-are-queued-separately-test
+ (testing "each channel of a dashboard subscription is its own queue entry, so one channel does not replace another (GDGT-3400)"
+ (mt/with-temp
+ [:model/Dashboard {dashboard-id :id} {}
+ :model/Pulse {pulse-id :id} {:name "Test Pulse"
+ :dashboard_id dashboard-id}
+ :model/PulseChannel _ {:pulse_id pulse-id
+ :channel_type "email"
+ :details {:emails ["[email protected]"]}}
+ :model/PulseChannel _ {:pulse_id pulse-id
+ :channel_type "slack"
+ :details {:channel "#general"}}]
+ ;; no worker takes from this queue, so every channel is still in it when the send ends
+ (let [queue (#'notification.send/create-dedup-priority-queue)]
+ (mt/with-dynamic-fn-redefs [notification.send/send-notification-async!
+ (fn [notification]
+ (#'notification.send/put-notification! queue notification))]
+ (pulse.send/send-pulse! (t2/select-one :model/Pulse pulse-id) :async? true))
+ (is (= #{:channel/email :channel/slack}
+ (set (for [notification (repeatedly #(#'notification.send/take-notification-with-timeout! queue 0))
+ :while notification]
+ (-> notification :handlers first :channel_type)))))))))
+
(deftest webhook-failure-names-the-channel-test
(testing "a pulse webhook handler carries its channel under :channel; the failure still names the channel (GDGT-3144)"
(notification.tu/with-send-notification-sync