diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb/sql/add_functions_and_triggers.sql b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb/sql/add_functions_and_triggers.sql index aa4264621ae0df9116b9a22663834c3cee165262..a3c5cd70295cf06f58e424b837a3f03d8686b3ab 100644 --- a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb/sql/add_functions_and_triggers.sql +++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb/sql/add_functions_and_triggers.sql @@ -248,22 +248,14 @@ CREATE OR REPLACE FUNCTION resource_allocation.process_new_claim_into_resource_u RETURNS void AS $$ DECLARE - proc_start timestamp with time zone; - proc_end timestamp with time zone; BEGIN - SELECT * from clock_timestamp() into proc_start; - -- insert the claim's start and end delta INSERT INTO resource_allocation.resource_usage_delta (claim_id, resource_id, status_id, moment, delta) VALUES (new_claim.id, new_claim.resource_id, new_claim.status_id, new_claim.starttime, new_claim.claim_size), (new_claim.id, new_claim.resource_id, new_claim.status_id, new_claim.endtime, -new_claim.claim_size); -- with the two new delta entries, use the deltas table to rebuild the usages table from the claim's starttime onwards - --TODO: use an upper limit as well? When? PERFORM resource_allocation.rebuild_resource_usages_from_deltas_for_resource_of_status(new_claim.resource_id, new_claim.status_id, new_claim.starttime); - - SELECT * from clock_timestamp() into proc_end; - RAISE NOTICE 'process_new_claim_into_resource_usages took %', proc_end - proc_start; END; $$ LANGUAGE plpgsql; ALTER FUNCTION resource_allocation.process_new_claim_into_resource_usages(new_claim resource_allocation.resource_claim) OWNER TO resourceassignment; @@ -417,20 +409,12 @@ CREATE OR REPLACE FUNCTION resource_allocation.process_old_claim_outof_resource_ RETURNS void AS $$ DECLARE - proc_start timestamp with time zone; - proc_end timestamp with time zone; BEGIN - SELECT * from clock_timestamp() into proc_start; - -- get rid of claim in delta's table (this should delete two entries, one for the starttime, and one for the endtime) DELETE FROM resource_allocation.resource_usage_delta WHERE claim_id = old_claim.id; -- with the two removed delta entries, use the deltas table to rebuild the usages table from the claim's starttime onwards - --TODO: use an upper limit as well? When? PERFORM resource_allocation.rebuild_resource_usages_from_deltas_for_resource_of_status(old_claim.resource_id, old_claim.status_id, old_claim.starttime); - - SELECT * from clock_timestamp() into proc_end; - RAISE NOTICE 'process_old_claim_outof_resource_usages took %', proc_end - proc_start; END; $$ LANGUAGE plpgsql; ALTER FUNCTION resource_allocation.process_old_claim_outof_resource_usages(old_claim resource_allocation.resource_claim) OWNER TO resourceassignment; @@ -444,11 +428,7 @@ CREATE OR REPLACE FUNCTION resource_allocation.get_resource_usage_at_or_before(_ $$ DECLARE result resource_allocation.resource_usage; - proc_start timestamp with time zone; - proc_end timestamp with time zone; BEGIN - SELECT * from clock_timestamp() into proc_start; - SELECT * FROM resource_allocation.resource_usage ru WHERE ru.resource_id = _resource_id AND ru.status_id = _claim_status_id @@ -481,9 +461,6 @@ BEGIN RAISE NOTICE 'get_resource_usage_at_or_before(_resource_id=%, status_id=%, timestamp=%, exactly_at=%, only_before=%, rebuild_usage_when_not_found=%): after rebuild, result=%.', _resource_id, _claim_status_id, _timestamp, exactly_at, only_before, false, result; END IF; - SELECT * from clock_timestamp() into proc_end; - RAISE NOTICE 'get_resource_usage_at_or_before took %', proc_end - proc_start; - RETURN result; END; $$ LANGUAGE plpgsql; @@ -529,9 +506,6 @@ DECLARE max_resource_usage_in_time_window resource_allocation.resource_usage; max_resource_at_or_before_starttime resource_allocation.resource_usage; BEGIN - --TODO: can this query be moved into if statement? - SELECT * FROM resource_allocation.get_resource_usage_at_or_before(_resource_id, _claim_status_id, _lower, false, false, false) into max_resource_at_or_before_starttime; - SELECT * FROM resource_allocation.resource_usage ru WHERE ru.resource_id = _resource_id AND ru.status_id = _claim_status_id @@ -540,20 +514,25 @@ BEGIN ORDER BY ru.usage DESC LIMIT 1 INTO max_resource_usage_in_time_window; - IF max_resource_usage_in_time_window IS NOT NULL THEN - IF max_resource_at_or_before_starttime IS NULL THEN - RETURN max_resource_usage_in_time_window; - ELSE - IF max_resource_usage_in_time_window.usage > max_resource_at_or_before_starttime.usage THEN - RETURN max_resource_usage_in_time_window; + IF max_resource_usage_in_time_window IS NULL THEN + -- no usages withing given window, so return first usage before window (which extends in time into this window) + SELECT * FROM resource_allocation.get_resource_usage_at_or_before(_resource_id, _claim_status_id, _lower, false, false, false) INTO max_resource_at_or_before_starttime; + RETURN max_resource_at_or_before_starttime; + END IF; + + IF max_resource_usage_in_time_window.as_of_timestamp > _lower THEN + -- check if the usage at_or_before_starttime is higher then in_time_window + SELECT * FROM resource_allocation.get_resource_usage_at_or_before(_resource_id, _claim_status_id, _lower, false, false, false) INTO max_resource_at_or_before_starttime; + IF max_resource_at_or_before_starttime IS NOT NULL THEN + IF max_resource_at_or_before_starttime.usage > max_resource_usage_in_time_window.usage THEN + RETURN max_resource_at_or_before_starttime; ELSE - RETURN max_resource_at_or_before_starttime; + RETURN max_resource_usage_in_time_window; END IF; END IF; - ELSE - -- could also be NULL but that is checked for elsewhere - RETURN max_resource_at_or_before_starttime; END IF; + + RETURN max_resource_usage_in_time_window; END; $$ LANGUAGE plpgsql; ALTER FUNCTION resource_allocation.get_max_resource_usage_between(_resource_id int, _claim_status_id int, _lower timestamp, _upper timestamp) OWNER TO resourceassignment; @@ -572,11 +551,7 @@ DECLARE available_capacity bigint; total_capacity bigint; current_claimed_usage bigint; - proc_start timestamp with time zone; - proc_end timestamp with time zone; BEGIN - SELECT * from clock_timestamp() into proc_start; - SELECT usage FROM resource_allocation.get_max_resource_usage_between(_resource_id, claimed_status_id, _lower, _upper) INTO max_resource_usage_value; IF max_resource_usage_value IS NULL THEN @@ -590,10 +565,6 @@ BEGIN IF available_capacity = total_capacity THEN --this is not a monitored resource, and hence we do not know how much space is actually available. --make a best guess by subtracting the current_claimed_usage from the total_capacity - - SELECT * from clock_timestamp() into proc_end; - RAISE NOTICE 'get_resource_claimable_capacity_between_a took %', proc_end - proc_start; - RETURN total_capacity - max_resource_usage_value; ELSE --this is a monitored resource, and the claimable_capacity is not just the free space (available_capacity) at this moment! @@ -606,9 +577,6 @@ BEGIN -- so take the current resource usage SELECT usage FROM resource_allocation.get_current_resource_usage(_resource_id, claimed_status_id) INTO current_claimed_usage; - SELECT * from clock_timestamp() into proc_end; - RAISE NOTICE 'get_resource_claimable_capacity_between_b took %', proc_end - proc_start; - IF current_claimed_usage IS NOT NULL THEN RETURN available_capacity + current_claimed_usage - max_resource_usage_value; END IF; @@ -629,18 +597,11 @@ CREATE OR REPLACE FUNCTION resource_allocation.has_conflict_with_overlapping_cla $$ DECLARE free_claimable_capacity bigint; - proc_start timestamp with time zone; - proc_end timestamp with time zone; BEGIN - SELECT * from clock_timestamp() into proc_start; - -- get the free free_claimable_capacity for this resource for the claim's time window -- this does not include the current claim which is (or at least should be) tentative. SELECT * FROM resource_allocation.get_resource_claimable_capacity_between(claim.resource_id, claim.starttime, claim.endtime) INTO free_claimable_capacity; - SELECT * from clock_timestamp() into proc_end; - RAISE NOTICE 'has_conflict_with_overlapping_claims took %', proc_end - proc_start; - return claim.claim_size > free_claimable_capacity; END; $$ LANGUAGE plpgsql; @@ -705,14 +666,7 @@ DECLARE task_scheduled_status_id int := 400; --beware: hard coded instead of lookup for performance task_queued_status_id int := 500; --beware: hard coded instead of lookup for performance claim_has_conflicts boolean; - proc_start timestamp with time zone; - proc_end timestamp with time zone; - part_start timestamp with time zone; - part_end timestamp with time zone; BEGIN - SELECT * from clock_timestamp() into proc_start; - SELECT * from clock_timestamp() into part_start; - --order of following steps is important, do not reorder the steps IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN IF NEW.starttime >= NEW.endtime THEN @@ -752,10 +706,6 @@ BEGIN END IF; END IF; - SELECT * from clock_timestamp() into part_end; - RAISE NOTICE 'before_claim_insertupdatedelete1 took %', part_end - part_start; - SELECT * from clock_timestamp() into part_start; - IF TG_OP = 'UPDATE' OR TG_OP = 'DELETE' THEN --update the resource usages affected by this claim --do this before we check for conflicts, because this claim might be shifted for example @@ -763,10 +713,6 @@ BEGIN PERFORM resource_allocation.process_old_claim_outof_resource_usages(OLD); END IF; - SELECT * from clock_timestamp() into part_end; - RAISE NOTICE 'before_claim_insertupdatedelete2 took %', part_end - part_start; - SELECT * from clock_timestamp() into part_start; - --only check claim if status and/or claim_size and/or start/end time changed IF TG_OP = 'INSERT' OR (TG_OP = 'UPDATE' AND (OLD.status_id <> NEW.status_id OR OLD.claim_size <> NEW.claim_size OR @@ -797,12 +743,6 @@ BEGIN PERFORM resource_allocation.process_new_claim_into_resource_usages(NEW); END IF; - SELECT * from clock_timestamp() into part_end; - RAISE NOTICE 'before_claim_insertupdatedelete3 took %', part_end - part_start; - - SELECT * from clock_timestamp() into proc_end; - RAISE NOTICE 'before_claim_insertupdatedelete took %', proc_end - proc_start; - IF TG_OP = 'DELETE' THEN RETURN OLD; END IF; @@ -839,14 +779,7 @@ DECLARE task_aborted_status_id int := 1100; --beware: hard coded instead of lookup for performance affected_claim resource_allocation.resource_claim; claim_has_conflicts boolean; - proc_start timestamp with time zone; - proc_end timestamp with time zone; - part_start timestamp with time zone; - part_end timestamp with time zone; BEGIN - SELECT * from clock_timestamp() into proc_start; - SELECT * from clock_timestamp() into part_start; - -- in the before trigger function, everything on the claim has been checked and adapted. -- now (in the after trigger, when all claims were inserted/updated in the database), let's check if the task should also be updated (to conflict status for example) -- only if claim status was changed or inserted... @@ -855,38 +788,19 @@ BEGIN --if claim status went to conflict, then set the task status to conflict as well UPDATE resource_allocation.task SET status_id=task_conflict_status_id WHERE id=NEW.task_id AND status_id <> task_conflict_status_id; ELSIF NEW.status_id = claim_tentative_status_id THEN - SELECT * from clock_timestamp() into part_start; - IF NOT EXISTS (SELECT id FROM resource_allocation.resource_claim WHERE task_id = NEW.task_id AND status_id = claim_conflict_status_id) THEN - SELECT * from clock_timestamp() into part_end; - RAISE NOTICE 'after_claim_insertupdatedelete1a took %', part_end - part_start; - SELECT * from clock_timestamp() into part_start; - IF NOT EXISTS (SELECT id FROM resource_allocation.task WHERE id = NEW.task_id AND status_id = task_approved_status_id) THEN - - SELECT * from clock_timestamp() into part_end; - RAISE NOTICE 'after_claim_insertupdatedelete1b took %', part_end - part_start; - SELECT * from clock_timestamp() into part_start; - UPDATE resource_allocation.task SET status_id=task_approved_status_id WHERE id=NEW.task_id AND status_id <> task_approved_status_id; - - SELECT * from clock_timestamp() into part_end; - RAISE NOTICE 'after_claim_insertupdatedelete1c took %', part_end - part_start; - SELECT * from clock_timestamp() into part_start; END IF; END IF; END IF; END IF; - SELECT * from clock_timestamp() into part_end; - RAISE NOTICE 'after_claim_insertupdatedelete1 took %', part_end - proc_start; - SELECT * from clock_timestamp() into part_start; - -- if this claim was moved or went from claimed to other status -- then check all other claims in conflict which might be affected by this change -- maybe they can be updated from conflict status to tentative... @@ -909,10 +823,6 @@ BEGIN END LOOP; END IF; - SELECT * from clock_timestamp() into part_end; - RAISE NOTICE 'after_claim_insertupdatedelete2 took %', part_end - part_start; - SELECT * from clock_timestamp() into part_start; - -- if this claim went from to claimed status -- then check all other claims in tentative state which might be affected by this change -- maybe they should be updated from tentative status to conflict... @@ -934,13 +844,6 @@ BEGIN END LOOP; END IF; - SELECT * from clock_timestamp() into part_end; - RAISE NOTICE 'after_claim_insertupdatedelete3 took %', part_end - part_start; - SELECT * from clock_timestamp() into part_start; - - SELECT * from clock_timestamp() into proc_end; - RAISE NOTICE 'after_claim_insertupdatedelete took %', proc_end - proc_start; - IF TG_OP = 'UPDATE' THEN -- delete obsolete claim when task is finished/aborted IF NEW.status_id = claim_claimed_status_id AND