njnu-seafish commented on code in PR #18549:
URL: 
https://github.com/apache/dolphinscheduler/pull/18549#discussion_r3987290767


##########
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertMapper.xml:
##########
@@ -51,6 +51,29 @@
         having count(*) = 0
     </insert>
 
+    <!--
+      Idempotent insert for task-result alerts.
+      Uses INSERT ... SELECT ... WHERE NOT EXISTS to skip duplicates
+      without dialect-specific upsert syntax (ON DUPLICATE KEY / MERGE / ON 
CONFLICT).
+      The uk_alert_dedup unique constraint remains as a concurrent-safety net;
+      a DuplicateKeyException is caught by the DAO layer and treated as a skip.
+    -->
+    <insert id="insertTaskResultAlertIfAbsent">
+        INSERT INTO t_ds_alert(sign, title, content, alert_status, 
warning_type, log, alertgroup_id,
+                               create_time, update_time, project_code, 
workflow_definition_code,
+                               workflow_instance_id, alert_type)
+        SELECT #{alert.sign}, #{alert.title}, #{alert.content}, 
#{alert.alertStatus.code},
+               #{alert.warningType.code}, #{alert.log}, #{alert.alertGroupId}, 
#{alert.createTime},
+               #{alert.updateTime}, #{alert.projectCode}, 
#{alert.workflowDefinitionCode},
+               #{alert.workflowInstanceId}, #{alert.alertType.code}
+        WHERE NOT EXISTS (
+            SELECT 1 FROM t_ds_alert
+            WHERE sign = #{alert.sign}
+              AND workflow_instance_id = #{alert.workflowInstanceId}
+              AND alert_type = #{alert.alertType.code}
+        )
+    </insert>

Review Comment:
   > Such logic should not be handled in the database, but should be handled by 
code.
   
   Thanks for the feedback. I've moved the dedup logic out of SQL and into 
application code.
   
   The INSERT ... SELECT ... WHERE NOT EXISTS statement has been replaced with 
a plain INSERT ... VALUES. Idempotency is now handled entirely in code: 
AlertDao#addTaskResultAlert performs a direct insert and catches 
DuplicateKeyException (thrown by the uk_alert_dedup unique constraint) to treat 
the duplicate as a skip. The unique constraint remains as the DB-level safety 
net, but no dedup logic lives in the SQL itself.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to