Browse Source
Replaces the pg_advisory_xact_lock in create_or_update_active_alarm with a Guava Striped lock around the DAO call, and removes the 4.2.2.7 upgrade script and migration that only shipped the SQL function change.pull/15641/head
4 changed files with 13 additions and 111 deletions
@ -1,86 +0,0 @@ |
|||
-- |
|||
-- SPDX-FileCopyrightText: Copyright The Thingsboard Authors |
|||
-- SPDX-License-Identifier: Apache-2.0 |
|||
-- |
|||
|
|||
-- CREATE_OR_UPDATE_ACTIVE_ALARM CONCURRENT DUPLICATE FIX START |
|||
|
|||
CREATE OR REPLACE FUNCTION create_or_update_active_alarm( |
|||
t_id uuid, c_id uuid, a_id uuid, a_created_ts bigint, |
|||
a_o_id uuid, a_o_type integer, a_type varchar, |
|||
a_severity varchar, a_start_ts bigint, a_end_ts bigint, |
|||
a_details varchar, |
|||
a_propagate boolean, a_propagate_to_owner boolean, |
|||
a_propagate_to_tenant boolean, a_propagation_types varchar, |
|||
a_creation_enabled boolean) |
|||
RETURNS varchar |
|||
LANGUAGE plpgsql |
|||
AS |
|||
$$ |
|||
DECLARE |
|||
null_id constant uuid = '13814000-1dd2-11b2-8080-808080808080'::uuid; |
|||
existing alarm; |
|||
result alarm_info; |
|||
row_count integer; |
|||
BEGIN |
|||
-- Serialize concurrent callers for the same (originator, type) so that the SELECT/INSERT |
|||
-- pair below cannot interleave and produce duplicate active alarms. |
|||
-- Correctness relies on READ COMMITTED (ThingsBoard's default isolation level): after the |
|||
-- lock is granted, the SELECT below takes a fresh per-statement snapshot and sees the row a |
|||
-- prior caller just committed. Under REPEATABLE READ / SERIALIZABLE the transaction's older |
|||
-- snapshot would miss that row and INSERT a duplicate, so do not call this under those levels. |
|||
PERFORM pg_advisory_xact_lock(hashtext(a_o_id::text), hashtext(a_type)); |
|||
SELECT * INTO existing FROM alarm a WHERE a.originator_id = a_o_id AND a.type = a_type AND a.cleared = false ORDER BY a.start_ts DESC FOR UPDATE; |
|||
IF existing.id IS NULL THEN |
|||
IF a_creation_enabled = FALSE THEN |
|||
RETURN json_build_object('success', false)::text; |
|||
END IF; |
|||
IF c_id = null_id THEN |
|||
c_id = NULL; |
|||
end if; |
|||
INSERT INTO alarm |
|||
(tenant_id, customer_id, id, created_time, |
|||
originator_id, originator_type, type, |
|||
severity, start_ts, end_ts, |
|||
additional_info, |
|||
propagate, propagate_to_owner, propagate_to_tenant, propagate_relation_types, |
|||
acknowledged, ack_ts, |
|||
cleared, clear_ts, |
|||
assignee_id, assign_ts) |
|||
VALUES |
|||
(t_id, c_id, a_id, a_created_ts, |
|||
a_o_id, a_o_type, a_type, |
|||
a_severity, a_start_ts, a_end_ts, |
|||
a_details, |
|||
a_propagate, a_propagate_to_owner, a_propagate_to_tenant, a_propagation_types, |
|||
false, 0, false, 0, NULL, 0); |
|||
INSERT INTO alarm_types (tenant_id, type) VALUES (t_id, a_type) ON CONFLICT (tenant_id, type) DO NOTHING; |
|||
SELECT * INTO result FROM alarm_info a WHERE a.id = a_id AND a.tenant_id = t_id; |
|||
RETURN json_build_object('success', true, 'created', true, 'modified', true, 'alarm', row_to_json(result))::text; |
|||
ELSE |
|||
UPDATE alarm a |
|||
SET severity = a_severity, |
|||
start_ts = a_start_ts, |
|||
end_ts = a_end_ts, |
|||
additional_info = a_details, |
|||
propagate = a_propagate, |
|||
propagate_to_owner = a_propagate_to_owner, |
|||
propagate_to_tenant = a_propagate_to_tenant, |
|||
propagate_relation_types = a_propagation_types |
|||
WHERE a.id = existing.id |
|||
AND a.tenant_id = t_id |
|||
AND (severity != a_severity OR start_ts != a_start_ts OR end_ts != a_end_ts OR additional_info != a_details |
|||
OR propagate != a_propagate OR propagate_to_owner != a_propagate_to_owner OR |
|||
propagate_to_tenant != a_propagate_to_tenant OR propagate_relation_types != a_propagation_types); |
|||
GET DIAGNOSTICS row_count = ROW_COUNT; |
|||
SELECT * INTO result FROM alarm_info a WHERE a.id = existing.id AND a.tenant_id = t_id; |
|||
IF row_count > 0 THEN |
|||
RETURN json_build_object('success', true, 'modified', true, 'alarm', row_to_json(result), 'old', row_to_json(existing))::text; |
|||
ELSE |
|||
RETURN json_build_object('success', true, 'modified', false, 'alarm', row_to_json(result))::text; |
|||
END IF; |
|||
END IF; |
|||
END |
|||
$$; |
|||
|
|||
-- CREATE_OR_UPDATE_ACTIVE_ALARM CONCURRENT DUPLICATE FIX END |
|||
@ -1,17 +0,0 @@ |
|||
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
|
|||
// SPDX-License-Identifier: Apache-2.0
|
|||
package org.thingsboard.server.service.install.lts; |
|||
|
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Component |
|||
@TbCoreComponent |
|||
public class V4_2_2_7Migration implements LtsMigration { |
|||
|
|||
@Override |
|||
public String getVersion() { |
|||
return "4.2.2.7"; |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue