Skip to content
Snippets Groups Projects
Commit d2318c75 authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

Task #10811: check_new_claim_for_conflicts which sets claims to conflict when...

Task #10811: check_new_claim_for_conflicts which sets claims to conflict when they don't fit. Added before insert check which only accepts new claims in claimed status
parent 22b3c91d
No related branches found
No related tags found
No related merge requests found
...@@ -475,46 +475,56 @@ COMMENT ON FUNCTION resource_monitoring.get_max_resource_usage_between(_resource ...@@ -475,46 +475,56 @@ COMMENT ON FUNCTION resource_monitoring.get_max_resource_usage_between(_resource
--------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------
DROP FUNCTION IF EXISTS resource_monitoring.check_new_claim_for_conflicts(new_claim resource_allocation.resource_claim); DROP FUNCTION IF EXISTS resource_monitoring.get_resource_claimable_capacity_between(_resource_id int, _lower timestamp, _upper timestamp);
CREATE OR REPLACE FUNCTION resource_monitoring.check_new_claim_for_conflicts(new_claim resource_allocation.resource_claim) CREATE OR REPLACE FUNCTION resource_monitoring.get_resource_claimable_capacity_between(_resource_id int, _lower timestamp, _upper timestamp)
RETURNS void AS RETURNS bigint AS
$$ $$
DECLARE DECLARE
overlapping_claim RECORD; allocated_status_id int := 1; --beware: hard coded instead of lookup for performance
claimed_status_id int; max_resource_usage resource_monitoring.resource_usage;
allocated_status_id int; max_resource_usage_value int;
available_capacity bigint;
BEGIN BEGIN
RAISE NOTICE ''; SELECT * FROM resource_monitoring.get_max_resource_usage_between(_resource_id, allocated_status_id, _lower, _upper) INTO max_resource_usage;
RAISE NOTICE '------- resource_monitoring.check_new_claim_for_conflicts() -------';
RAISE NOTICE 'new claim: %', new_claim;
SELECT id FROM resource_allocation.resource_claim_status WHERE name = 'claimed' LIMIT 1 INTO claimed_status_id; IF max_resource_usage IS NULL THEN
SELECT id FROM resource_allocation.resource_claim_status WHERE name = 'allocated' LIMIT 1 INTO allocated_status_id; max_resource_usage_value := 0;
ELSE
IF claimed_status_id IS NULL THEN max_resource_usage_value := max_resource_usage.usage;
RAISE EXCEPTION 'resource_monitoring.check_new_claim_for_conflicts could not find claimed status id.';
END IF; END IF;
IF allocated_status_id IS NULL THEN SELECT available FROM resource_monitoring.resource_capacity WHERE resource_id = _resource_id LIMIT 1 INTO available_capacity;
RAISE EXCEPTION 'resource_monitoring.check_new_claim_for_conflicts could not find allocated status id.';
END IF; RETURN available_capacity - max_resource_usage_value;
END;
$$ LANGUAGE plpgsql;
ALTER FUNCTION resource_monitoring.get_resource_claimable_capacity_between(_resource_id int, _lower timestamp, _upper timestamp) OWNER TO resourceassignment;
COMMENT ON FUNCTION resource_monitoring.get_resource_claimable_capacity_between(_resource_id int, _lower timestamp, _upper timestamp)
IS 'get the maximum resource usage for the given _resource_id for claims with given _claim_status_id in the period between the given _lower and _upper timestamps';
RAISE NOTICE 'getresource_usage_at(%, %, %) %', new_claim.resource_id, claimed_status_id, new_claim.starttime, resource_monitoring.get_resource_usage_at(new_claim.resource_id, claimed_status_id, new_claim.starttime); ---------------------------------------------------------------------------------------------------------------------
RAISE NOTICE 'get_max_resource_usage_between(%, %, %, %) %', new_claim.resource_id, claimed_status_id, new_claim.starttime,new_claim.endtime, resource_monitoring.get_max_resource_usage_between(new_claim.resource_id, claimed_status_id, new_claim.starttime, new_claim.endtime); DROP FUNCTION IF EXISTS resource_monitoring.check_new_claim_for_conflicts(new_claim resource_allocation.resource_claim);
-- search for overlapping claims on the same resource CREATE OR REPLACE FUNCTION resource_monitoring.check_new_claim_for_conflicts(new_claim resource_allocation.resource_claim)
FOR overlapping_claim IN SELECT * RETURNS void AS
FROM resource_allocation.resource_claim rc $$
WHERE rc.resource_id = new_claim.resource_id DECLARE
AND rc.id != new_claim.id claimed_status_id int := 0; --beware: hard coded instead of lookup for performance
AND rc.starttime <= new_claim.endtime conflict_status_id int := 2; --beware: hard coded instead of lookup for performance
AND rc.endtime >= new_claim.starttime free_claimable_capacity bigint;
LOOP BEGIN
--TODO: check if new claim fits in resource capacity minus overlapping_claim sizes and minus unaccounted_for_usage --only check claimes in claimed status
RAISE NOTICE 'overlapping_claim: %', overlapping_claim; IF new_claim.status_id = claimed_status_id THEN
END LOOP; -- get the free free_claimable_capacity for this resource for this claim's time window
SELECT * FROM resource_monitoring.get_resource_claimable_capacity_between(new_claim.resource_id, new_claim.starttime, new_claim.endtime) INTO free_claimable_capacity;
-- if new claim to large, set claim status to conflict
IF new_claim.claim_size > free_claimable_capacity THEN
UPDATE resource_allocation.resource_claim SET status_id=conflict_status_id WHERE id=new_claim.id;
END IF;
END IF;
END; END;
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
ALTER FUNCTION resource_monitoring.check_new_claim_for_conflicts(new_claim resource_allocation.resource_claim) OWNER TO resourceassignment; ALTER FUNCTION resource_monitoring.check_new_claim_for_conflicts(new_claim resource_allocation.resource_claim) OWNER TO resourceassignment;
...@@ -564,5 +574,33 @@ CREATE TRIGGER T_on_claim_insertupdatedelete ...@@ -564,5 +574,33 @@ CREATE TRIGGER T_on_claim_insertupdatedelete
--------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------
DROP TRIGGER IF EXISTS T_before_claim_insertupdatedelete ON resource_allocation.resource_claim CASCADE;
DROP FUNCTION IF EXISTS resource_monitoring.before_claim_insertupdatedelete();
CREATE OR REPLACE FUNCTION resource_monitoring.before_claim_insertupdatedelete()
RETURNS trigger AS
$BODY$
DECLARE
claimed_status_id int := 0; --beware: hard coded instead of lookup for performance
BEGIN
IF NEW.status_id != claimed_status_id THEN
RAISE EXCEPTION 'newly inserted claims should not have status other than claimed; claim: %', NEW;
END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION resource_monitoring.before_claim_insertupdatedelete()
OWNER TO resourceassignment;
CREATE TRIGGER T_before_claim_insertupdatedelete
BEFORE INSERT
ON resource_allocation.resource_claim
FOR EACH ROW
EXECUTE PROCEDURE resource_monitoring.before_claim_insertupdatedelete();
---------------------------------------------------------------------------------------------------------------------
COMMIT; COMMIT;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment