diff --git a/CEP/Calibration/BBSControl/sql/create_blackboard_functions.sql b/CEP/Calibration/BBSControl/sql/create_blackboard_functions.sql index 0ccd515e3bc2ab2dc414724e19cec2893c8a7525..7f0b924b31221ac1c1f791106acef245b7c58415 100644 --- a/CEP/Calibration/BBSControl/sql/create_blackboard_functions.sql +++ b/CEP/Calibration/BBSControl/sql/create_blackboard_functions.sql @@ -1,25 +1,13 @@ ----------------- --- RUN STATES -- ----------------- - --- -1 - FAILED --- 0 - WAITING_FOR_CONTROL --- 1 - WAITING_FOR_WORKERS --- 2 - COMPUTING_WORKER_INDEX --- 3 - PROCESSING --- 4 - DONE - - ------------------- --- SHARED STATE -- ------------------- +------------- +-- SESSION -- +------------- -CREATE OR REPLACE FUNCTION blackboard.init_shared_state(_key TEXT, - OUT _id INTEGER) AS +CREATE OR REPLACE FUNCTION blackboard.init_session(_key TEXT, OUT _id INTEGER) + AS $$ BEGIN BEGIN - INSERT INTO blackboard.shared_state (key) + INSERT INTO blackboard.session (key) VALUES (_key); EXCEPTION WHEN unique_violation THEN @@ -30,30 +18,30 @@ $$ -- Should always succeed. SELECT id INTO STRICT _id - FROM blackboard.shared_state - WHERE key = _key; + FROM blackboard.session + WHERE key = _key; END; $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION blackboard.set_run_state(_state_id INTEGER, - _hostname TEXT, _pid BIGINT, _run_state INTEGER, OUT _status INTEGER) AS +CREATE OR REPLACE FUNCTION blackboard.set_session_state(_session_id INTEGER, + _hostname TEXT, _pid BIGINT, _state INTEGER, OUT _status INTEGER) AS $$ BEGIN _status := -1; - -- Test if caller is the control process and set the run_state in a - -- single query. Also, set the run_finish field. - UPDATE blackboard.shared_state - SET run_state = _run_state - WHERE id = _state_id + -- Test if caller is the control process and set the state in a single + -- query. Also, set the finish field if appropriate. + UPDATE blackboard.session + SET state = _state + WHERE id = _session_id AND control_hostname = _hostname AND control_pid = _pid; - IF FOUND AND (_run_state = -1 OR _run_state = 4) THEN - UPDATE blackboard.shared_state - SET run_finish = now() - WHERE id = _state_id; + IF FOUND AND (_state = -1 OR _state = 4) THEN + UPDATE blackboard.session + SET finish = now() + WHERE id = _session_id; END IF; IF FOUND THEN @@ -63,16 +51,16 @@ $$ $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION blackboard.get_run_state(_state_id INTEGER, - OUT _status INTEGER, OUT _run_state INTEGER) AS +CREATE OR REPLACE FUNCTION blackboard.get_session_state(_session_id INTEGER, + OUT _status INTEGER, OUT _state INTEGER) AS $$ BEGIN _status := -1; - SELECT run_state - INTO _run_state - FROM blackboard.shared_state - WHERE id = _state_id; + SELECT state + INTO _state + FROM blackboard.session + WHERE id = _session_id; IF FOUND THEN _status := 0; @@ -82,61 +70,60 @@ $$ LANGUAGE plpgsql; --------------- --- REGISTER -- --------------- +------------------ +-- REGISTRATION -- +------------------ -CREATE OR REPLACE FUNCTION blackboard.create_kernel_slot(_state_id INTEGER, +CREATE OR REPLACE FUNCTION blackboard.create_kernel_slot(_session_id INTEGER, _hostname TEXT, _pid BIGINT, _filesystem TEXT, _path TEXT) RETURNS VOID AS $$ BEGIN - -- Verify that the caller is the control process and that the run_state - -- is set to WAITING_FOR_CONTROL. Row lock the shared_state to prevent + -- Verify that the caller is the control process and that the session + -- state is set to WAITING_FOR_CONTROL. Row lock the session to prevent -- concurrent updates. - IF NOT blackboard.test_and_lock_shared_state(_state_id, _hostname, _pid, - 0) THEN - RAISE EXCEPTION 'Incorrect run state or operation not permitted'; + IF NOT blackboard.test_and_lock_session(_session_id, _hostname, _pid, 0) + THEN + RAISE EXCEPTION 'Operation not permitted'; END IF; - INSERT INTO blackboard.worker_register (state_id, worker_type, - filesystem, path) - VALUES (_state_id, 'K', _filesystem, _path); + INSERT INTO blackboard.worker (session_id, type, filesystem, path) + VALUES (_session_id, 0, _filesystem, _path); END; $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION blackboard.create_solver_slot(_state_id INTEGER, +CREATE OR REPLACE FUNCTION blackboard.create_solver_slot(_session_id INTEGER, _hostname TEXT, _pid INTEGER) RETURNS VOID AS $$ BEGIN - -- Verify that the caller is the control process and that the run_state - -- is set to WAITING_FOR_CONTROL. Row lock the shared_state to prevent + -- Verify that the caller is the control process and that the session + -- state is set to WAITING_FOR_CONTROL. Row lock the session to prevent -- concurrent updates. - IF NOT blackboard.test_and_lock_shared_state(_state_id, _hostname, _pid, - 0) THEN - RAISE EXCEPTION 'Incorrect run state or operation not permitted'; + IF NOT blackboard.test_and_lock_session(_session_id, _hostname, _pid, 0) + THEN + RAISE EXCEPTION 'Operation not permitted'; END IF; - INSERT INTO blackboard.worker_register (state_id, worker_type) - VALUES (_state_id, 'S'); + INSERT INTO blackboard.worker (session_id, type) + VALUES (_session_id, 1); END; $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION blackboard.register_as_control(_state_id INTEGER, +CREATE OR REPLACE FUNCTION blackboard.register_as_control(_session_id INTEGER, _hostname TEXT, _pid BIGINT, OUT _status INTEGER) AS $$ BEGIN _status := -1; - -- Verify that run_state is set to WAITING_FOR_CONTROL and try to - -- register in a single query. - UPDATE blackboard.shared_state + -- Verify that the session state is set to WAITING_FOR_CONTROL and try + -- to register in a single query. + UPDATE blackboard.session SET control_hostname = _hostname, control_pid = _pid - WHERE id = _state_id - AND run_state = 0 + WHERE id = _session_id + AND state = 0 AND control_hostname IS NULL AND control_pid IS NULL; @@ -147,7 +134,7 @@ $$ $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION blackboard.register_as_kernel(_state_id INTEGER, +CREATE OR REPLACE FUNCTION blackboard.register_as_kernel(_session_id INTEGER, _hostname TEXT, _pid BIGINT, _filesystem TEXT, _path TEXT, _axis_freq_lower BYTEA, _axis_freq_upper BYTEA, _axis_time_lower BYTEA, _axis_time_upper BYTEA, OUT _status INTEGER) AS @@ -155,21 +142,21 @@ $$ BEGIN _status := -1; - -- Check if the run_state is set to WAITING_FOR_WORKERS and row lock the - -- shared_state to prevent concurrent updates. - IF NOT blackboard.test_and_lock_shared_state(_state_id, 1) THEN + -- Verify that the session state is set to WAITING_FOR_WORKERS and row + -- lock the session to prevent concurrent updates. + IF NOT blackboard.test_and_lock_session(_session_id, 1) THEN RETURN; END IF; -- Try to register. - UPDATE blackboard.worker_register + UPDATE blackboard.worker SET hostname = _hostname, pid = _pid, axis_freq_lower = _axis_freq_lower, axis_freq_upper = _axis_freq_upper, axis_time_lower = _axis_time_lower, axis_time_upper = _axis_time_upper - WHERE state_id = _state_id - AND worker_type = 'K' + WHERE session_id = _session_id + AND type = 0 AND hostname IS NULL AND pid IS NULL AND filesystem = _filesystem @@ -182,7 +169,7 @@ $$ $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION blackboard.register_as_solver(_state_id INTEGER, +CREATE OR REPLACE FUNCTION blackboard.register_as_solver(_session_id INTEGER, _hostname TEXT, _pid BIGINT, _port INTEGER, OUT _status INTEGER) AS $$ DECLARE @@ -190,9 +177,9 @@ $$ BEGIN _status := -1; - -- Check if the run_state is set to WAITING_FOR_WORKERS and row lock the - -- shared_state to prevent concurrent updates. - IF NOT blackboard.test_and_lock_shared_state(_state_id, 1) THEN + -- Verify that the session state is set to WAITING_FOR_WORKERS and row + -- lock the session to prevent concurrent updates. + IF NOT blackboard.test_and_lock_session(_session_id, 1) THEN RETURN; END IF; @@ -202,9 +189,9 @@ $$ -- are locked? If not, the following query may create a deadlock. SELECT id INTO _slot_id - FROM blackboard.worker_register - WHERE state_id = _state_id - AND worker_type = 'S' + FROM blackboard.worker + WHERE session_id = _session_id + AND type = 1 AND hostname IS NULL AND pid IS NULL ORDER BY id @@ -215,7 +202,7 @@ $$ END IF; -- Try to register. - UPDATE blackboard.worker_register + UPDATE blackboard.worker SET hostname = _hostname, pid = _pid, port = _port WHERE id = _slot_id; @@ -226,36 +213,36 @@ $$ $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION blackboard.get_register(_state_id INTEGER) -RETURNS SETOF blackboard.worker_register AS +CREATE OR REPLACE FUNCTION blackboard.get_workers(_session_id INTEGER) +RETURNS SETOF blackboard.worker AS $$ SELECT * - FROM blackboard.worker_register - WHERE state_id = $1 + FROM blackboard.worker + WHERE session_id = $1 ORDER BY id; $$ LANGUAGE SQL; -CREATE OR REPLACE FUNCTION blackboard.set_index(_state_id INTEGER, - _hostname TEXT, _pid BIGINT, _target_hostname TEXT, _target_pid BIGINT, +CREATE OR REPLACE FUNCTION blackboard.set_worker_index(_session_id INTEGER, + _hostname TEXT, _pid BIGINT, _worker_hostname TEXT, _worker_pid BIGINT, _index INTEGER, OUT _status INTEGER) AS $$ BEGIN _status := -1; - -- Verify that the caller is the control process and that the run_state - -- is set to COMPUTING_WORKER_INDEX. Row lock the shared_state to + -- Verify that the caller is the control process and that the session + -- state is set to COMPUTING_WORKER_INDEX. Row lock the session to -- prevent concurrent updates. - IF NOT blackboard.test_and_lock_shared_state(_state_id, _hostname, _pid, - 2) THEN + IF NOT blackboard.test_and_lock_session(_session_id, _hostname, _pid, 2) + THEN RETURN; END IF; - UPDATE blackboard.worker_register + UPDATE blackboard.worker SET index = _index - WHERE state_id = _state_id - AND hostname = _target_hostname - AND pid = _target_pid; + WHERE session_id = _session_id + AND hostname = _worker_hostname + AND pid = _worker_pid; IF FOUND THEN _status := 0; @@ -270,27 +257,27 @@ LANGUAGE plpgsql; ------------- -- (PRIVATE FUNCTION, DO NOT CALL FROM C++) -CREATE OR REPLACE FUNCTION blackboard.get_active_command_id(_state_id INTEGER, +CREATE OR REPLACE FUNCTION blackboard.get_active_command_id(_session_id INTEGER, _worker_id INTEGER, OUT _id INTEGER) AS $$ BEGIN -- Note that STRICT is not used with INTO in the query below because it - -- can return zero rows, which is ok and should not raise an exception. + -- can return zero rows, which is OK and should not raise an exception. -- Without the STRICT modifier, returning multiple rows will not raise -- an exception, even though for this query it should be considered an -- error. However, this is already covered by the UNIQUE column -- constraint on blackboard.command.id. -- - -- PRECONDITION: _worker_id refers to a worker that is registered - -- to the shared_state with id _state_id. + -- PRECONDITION: _worker_id refers to a worker that is registered to the + -- session with id _session_id. SELECT command.id INTO _id FROM blackboard.command AS command, - blackboard.worker_register AS worker_register - WHERE command.state_id = _state_id - AND worker_register.id = _worker_id - AND (command.target IS NULL - OR worker_register.worker_type = command.target) + blackboard.worker AS worker + WHERE command.session_id = _session_id + AND worker.id = _worker_id + AND (command.addressee IS NULL + OR command.addressee = worker.type) AND command.id > ( SELECT COALESCE(MAX(command_id), -1) @@ -305,7 +292,7 @@ LANGUAGE plpgsql; -- NOTE: This function has an extra status code! -- -2 - Queue empty. -CREATE OR REPLACE FUNCTION blackboard.get_command(_state_id INTEGER, +CREATE OR REPLACE FUNCTION blackboard.get_command(_session_id INTEGER, _hostname TEXT, _pid BIGINT, OUT _status INTEGER, OUT _id INTEGER, OUT _type TEXT, OUT _name TEXT, OUT _args TEXT) AS $$ @@ -317,11 +304,11 @@ $$ -- Get the worker_id of the calling process. Because there is no stored -- procedure that can change the worker_id assigned to a process after -- it has registered, concurrent transactions are harmless. Therefore, - -- the register does not need to be locked. + -- the worker table does not need to be locked. SELECT id INTO _worker_id - FROM blackboard.worker_register - WHERE state_id = _state_id + FROM blackboard.worker + WHERE session_id = _session_id AND hostname = _hostname AND pid = _pid; @@ -335,7 +322,7 @@ $$ -- active command might change between fetching the id of the active -- command and fetching the details of that command using the SELECT -- query below. - _id := blackboard.get_active_command_id(_state_id, _worker_id); + _id := blackboard.get_active_command_id(_session_id, _worker_id); IF _id IS NULL THEN -- Queue is empty, so return an empty command. _status := -2; @@ -359,24 +346,25 @@ LANGUAGE plpgsql; -- COMMAND -- ------------- -CREATE OR REPLACE FUNCTION blackboard.add_command(_state_id INTEGER, - _hostname TEXT, _pid BIGINT, _target CHARACTER, _type TEXT, _name TEXT, _args TEXT, - OUT _status INTEGER, OUT _id INTEGER) AS +CREATE OR REPLACE FUNCTION blackboard.add_command(_session_id INTEGER, + _hostname TEXT, _pid BIGINT, _addressee INTEGER, _type TEXT, _name TEXT, + _args TEXT, OUT _status INTEGER, OUT _id INTEGER) AS $$ BEGIN _status := -1; - -- Verify that the caller is the control process and that the run_state - -- is set to PROCESSING. Row lock the shared_state to prevent concurrent + -- Verify that the caller is the control process and that the state is + -- set to PROCESSING. Row lock the session to prevent concurrent -- updates. - IF NOT blackboard.test_and_lock_shared_state(_state_id, _hostname, _pid, + IF NOT blackboard.test_and_lock_session(_session_id, _hostname, _pid, 3) THEN RETURN; END IF; INSERT - INTO blackboard.command(state_id, target, type, name, args) - VALUES (_state_id, _target, _type, _name, _args) + INTO blackboard.command(session_id, addressee, type, name, + args) + VALUES (_session_id, _addressee, _type, _name, _args) RETURNING id INTO STRICT _id; @@ -386,25 +374,11 @@ $$ LANGUAGE plpgsql; ---CREATE OR REPLACE FUNCTION blackboard.add_command(_state_id INTEGER, --- _hostname TEXT, _pid BIGINT, _type TEXT, OUT _status INTEGER, --- OUT _id INTEGER) AS ---$$ --- BEGIN --- SELECT * --- INTO STRICT _status, _id --- FROM blackboard.add_command(_state_id, _hostname, _pid, --- _type, NULL, NULL); --- END; ---$$ ---LANGUAGE plpgsql; - - ------------ -- RESULT -- ------------ -CREATE OR REPLACE FUNCTION blackboard.add_result(_state_id INTEGER, +CREATE OR REPLACE FUNCTION blackboard.add_result(_session_id INTEGER, _hostname TEXT, _pid BIGINT, _command_id INTEGER, _result_code INTEGER, _message TEXT, OUT _status INTEGER) AS $$ @@ -414,20 +388,20 @@ $$ BEGIN _status := -1; - -- Check if the run_state is set to PROCESSING and row lock the - -- shared_state to prevent concurrent updates. - IF NOT blackboard.test_and_lock_shared_state(_state_id, 3) THEN + -- Check if the state is set to PROCESSING and row lock the session to + -- prevent concurrent updates. + IF NOT blackboard.test_and_lock_session(_session_id, 3) THEN RETURN; END IF; -- Get the worker_id of the calling process. Because there is no stored -- procedure that can change the worker_id assigned to a process after -- it has registered, concurrent transactions are harmless. Therefore, - -- the register does not need to be locked. + -- the worker table does not need to be locked. SELECT id INTO _worker_id - FROM blackboard.worker_register - WHERE state_id = _state_id + FROM blackboard.worker + WHERE session_id = _session_id AND hostname = _hostname AND pid = _pid; @@ -436,7 +410,7 @@ $$ END IF; -- Find the active command for this process. - _active_command_id := blackboard.get_active_command_id(_state_id, + _active_command_id := blackboard.get_active_command_id(_session_id, _worker_id); -- Verify that the caller is trying to add a result corresponding to its @@ -455,19 +429,18 @@ $$ END; $$ LANGUAGE plpgsql; - --OUT _result_count INTEGER, OUT _fail_count INTEGER) AS CREATE OR REPLACE FUNCTION blackboard.get_command_status(_command_id INTEGER, - OUT _status INTEGER, OUT _target CHARACTER, OUT _result_count INTEGER, + OUT _status INTEGER, OUT _addressee INTEGER, OUT _result_count INTEGER, OUT _fail_count INTEGER) AS $$ DECLARE - _state_id INTEGER; + _session_id INTEGER; BEGIN _status := -1; - SELECT state_id, target - INTO _state_id, _target + SELECT session_id, addressee + INTO _session_id, _addressee FROM blackboard.command WHERE id = _command_id; @@ -475,19 +448,19 @@ $$ RETURN; END IF; - -- COUNT() counts everything except NULL. NULLIF(x, y) returns NULL - -- if x = y, otherwise it returns x. NULLIF is used below to count all - -- rows where result_code != 0. + -- COUNT() counts everything except NULL. NULLIF(x, y) returns NULL if + -- x = y, otherwise it returns x. NULLIF is used below to count all rows + -- where result_code != 0. SELECT COUNT(1), COUNT(NULLIF(result_code = 0, TRUE)) INTO _result_count, _fail_count - FROM blackboard.worker_register AS worker_register, + FROM blackboard.worker AS worker, blackboard.command AS command, blackboard.result AS result WHERE command.id = _command_id - AND (command.target IS NULL - OR worker_register.worker_type = command.target) - AND worker_register.state_id = _state_id - AND result.worker_id = worker_register.id + AND (command.addressee IS NULL + OR command.addressee = worker.type) + AND worker.session_id = _session_id + AND result.worker_id = worker.id AND result.command_id = _command_id; _status := 0; @@ -495,145 +468,48 @@ $$ $$ LANGUAGE plpgsql; -CREATE TYPE blackboard.foo AS -( - worker_type CHARACTER, - result_count BIGINT, - fail_count BIGINT -); - -CREATE OR REPLACE FUNCTION blackboard.get_command_status3(_command_id INTEGER) -RETURNS SETOF blackboard.foo AS -$$ - SELECT worker_register.worker_type, COUNT(1), COUNT(NULLIF(result_code = 0, TRUE)) - FROM blackboard.command AS command, - blackboard.worker_register AS worker_register, - blackboard.result AS result - WHERE command.id = $1 - AND worker_register.state_id = command.state_id - AND result.worker_id = worker_register.id - AND result.command_id = command.id - GROUP BY worker_register.worker_type -$$ -LANGUAGE SQL; - -CREATE OR REPLACE FUNCTION blackboard.get_command_status2(_command_id INTEGER, - OUT _worker_type CHARACTER, OUT _result_count INTEGER, - OUT _fail_count INTEGER) -RETURNS SETOF RECORD AS --- OUT _status INTEGER, OUT _result_count --- OUT _solver_ok INTEGER, OUT _solver_fail INTEGER) AS -$$ - DECLARE - _state_id INTEGER; - _row RECORD; - BEGIN --- _status := -1; - --- SELECT state_id --- INTO _state_id --- FROM blackboard.command --- WHERE id = _command_id; - --- IF NOT FOUND THEN --- RETURN; --- END IF; - - -- COUNT() counts everything except NULL. NULLIF(x, y) returns NULL - -- if x = y, otherwise it returns x. NULLIF is used below to count all - -- rows where result_code != 0. --- SELECT COUNT(1), COUNT(NULLIF(result_code = 0, TRUE)) --- result_code = 0 AND , TRUE)), COUNT(NULLIF(result_code = 0, TRUE)) --- SELECT COUNT(NULLIF((worker_type = 'S' OR result_count != 0), TRUE)) - FOR _row IN - SELECT worker_register.worker_type AS a, COUNT(1) AS b, COUNT(NULLIF(result_code = 0, TRUE)) AS c - FROM blackboard.command AS command, - blackboard.worker_register AS worker_register, - blackboard.result AS result - WHERE command.id = _command_id - AND worker_register.state_id = command.state_id - AND result.worker_id = worker_register.id - AND result.command_id = command.id - GROUP BY worker_register.worker_type - LOOP - _worker_type := _row.a; - _result_count := _row.b; - _fail_count := _row.c; - RETURN NEXT; - END LOOP; - RETURN; - --- _status := 0; - END; -$$ -LANGUAGE plpgsql; - -- Return type for blackboard.get_results() that hides implementation details -- from the caller. CREATE TYPE blackboard.result_iface AS ( --- command_id INTEGER, hostname TEXT, pid BIGINT, result_code INTEGER, message TEXT ); ---CREATE OR REPLACE FUNCTION blackboard.get_results(_command_id INTEGER) ---RETURNS SETOF blackboard.result_iface AS ---$$ --- DECLARE --- _row blackboard.result_iface; --- BEGIN --- FOR _row IN --- SELECT hostname, pid, result_code, message --- FROM blackboard.command AS command, --- blackboard.worker_register AS worker_register, --- blackboard.result AS result --- WHERE command.id = _command_id --- AND worker_register.state_id = command.state_id --- AND result.worker_id = worker_register.id --- AND result.command_id = _command_id --- LOOP --- RETURN NEXT _row; --- END LOOP; --- RETURN; --- END; ---$$ ---LANGUAGE plpgsql; - CREATE OR REPLACE FUNCTION blackboard.get_results(_command_id INTEGER) RETURNS SETOF blackboard.result_iface AS $$ SELECT hostname, pid, result_code, message FROM blackboard.command AS command, - blackboard.worker_register AS worker_register, + blackboard.worker AS worker, blackboard.result AS result WHERE command.id = $1 - AND worker_register.state_id = command.state_id - AND result.worker_id = worker_register.id + AND worker.session_id = command.session_id + AND result.worker_id = worker.id AND result.command_id = $1; $$ LANGUAGE SQL; + ---------------------- -- HELPER FUNCTIONS -- ---------------------- -CREATE OR REPLACE FUNCTION blackboard.test_and_lock_shared_state - (_state_id INTEGER, _hostname TEXT, _pid BIGINT, _run_state INTEGER) +CREATE OR REPLACE FUNCTION blackboard.test_and_lock_session(_session_id INTEGER, + _hostname TEXT, _pid BIGINT, _state INTEGER) RETURNS BOOLEAN AS $$ BEGIN -- Checks if the process is the control process _and_ if the current - -- run_state matches the specified _run_state. The shared_state is row - -- locked FOR SHARE for the duration of the calling transaction to - -- prevent concurrent modification of the run_state or the control - -- process. + -- session state matches the specified _state. The session is row locked + -- FOR SHARE for the duration of the calling transaction to prevent + -- concurrent modification of the state or the control process. PERFORM * - FROM blackboard.shared_state - WHERE id = _state_id - AND run_state = _run_state + FROM blackboard.session + WHERE id = _session_id + AND state = _state AND control_hostname = _hostname AND control_pid = _pid FOR SHARE; @@ -643,19 +519,18 @@ $$ $$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION blackboard.test_and_lock_shared_state - (_state_id INTEGER, _run_state INTEGER) +CREATE OR REPLACE FUNCTION blackboard.test_and_lock_session(_session_id INTEGER, + _state INTEGER) RETURNS BOOLEAN AS $$ BEGIN - -- Checks if the current run_state matches the specified _run_state. - -- The shared_state is row locked FOR SHARE for the duration of the - -- calling transaction to prevent concurrent modification of the - -- run_state. + -- Checks if the current state matches the specified _state. The session + -- is row locked FOR SHARE for the duration of the calling transaction + -- to prevent concurrent modification of the session state. PERFORM * - FROM blackboard.shared_state - WHERE id = _state_id - AND run_state = _run_state + FROM blackboard.session + WHERE id = _session_id + AND state = _state FOR SHARE; RETURN FOUND; diff --git a/CEP/Calibration/BBSControl/sql/create_blackboard_tables.sql b/CEP/Calibration/BBSControl/sql/create_blackboard_tables.sql index 597dafd01bf8abfa1cbf07391cbc65df5a343097..32d99664133619703f0acd44fe66a18793dda25a 100644 --- a/CEP/Calibration/BBSControl/sql/create_blackboard_tables.sql +++ b/CEP/Calibration/BBSControl/sql/create_blackboard_tables.sql @@ -1,25 +1,44 @@ -CREATE TABLE blackboard.shared_state +-------------------- +-- SESSION STATES -- +-------------------- + +-- -1 - FAILED +-- 0 - WAITING_FOR_CONTROL +-- 1 - WAITING_FOR_WORKERS +-- 2 - COMPUTING_WORKER_INDEX +-- 3 - workerING +-- 4 - DONE + +------------------ +-- WORKER TYPES -- +------------------ + +-- 0 - KERNEL +-- 1 - SOLVER + + +CREATE TABLE blackboard.session ( id SERIAL PRIMARY KEY, key TEXT NOT NULL UNIQUE, control_hostname TEXT , control_pid BIGINT , - run_state INTEGER NOT NULL DEFAULT 0, - run_start TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), - run_finish TIMESTAMP WITH TIME ZONE + state INTEGER NOT NULL DEFAULT 0, + start TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), + finish TIMESTAMP WITH TIME ZONE ); -CREATE TABLE blackboard.worker_register +CREATE TABLE blackboard.worker ( id SERIAL PRIMARY KEY, - state_id INTEGER NOT NULL + session_id INTEGER NOT NULL REFERENCES - blackboard.shared_state (id) + blackboard.session (id) ON DELETE CASCADE, hostname TEXT , pid BIGINT , index INTEGER , - worker_type CHARACTER NOT NULL, + type INTEGER NOT NULL, port INTEGER , filesystem TEXT , path TEXT , @@ -28,17 +47,17 @@ CREATE TABLE blackboard.worker_register axis_time_lower BYTEA , axis_time_upper BYTEA , - UNIQUE (state_id, hostname, pid) + UNIQUE (session_id, hostname, pid) ); CREATE TABLE blackboard.command ( id SERIAL PRIMARY KEY, - state_id INTEGER NOT NULL + session_id INTEGER NOT NULL REFERENCES - blackboard.shared_state (id) + blackboard.session (id) ON DELETE CASCADE, - target CHARACTER , + addressee INTEGER , type TEXT NOT NULL, name TEXT , args TEXT @@ -52,7 +71,7 @@ CREATE TABLE blackboard.result ON DELETE CASCADE, worker_id INTEGER NOT NULL REFERENCES - blackboard.worker_register (id) + blackboard.worker (id) ON DELETE CASCADE, timestamp TIMESTAMP WITH TIME ZONE DEFAULT now(), result_code INTEGER , diff --git a/CEP/Calibration/BBSControl/sql/create_blackboard_triggers.sql b/CEP/Calibration/BBSControl/sql/create_blackboard_triggers.sql index 8cc20a2ded23f4ae54750d19b194a2b551d8af62..3165fe7c3722284f428f5334a3bbfd919eac4f93 100644 --- a/CEP/Calibration/BBSControl/sql/create_blackboard_triggers.sql +++ b/CEP/Calibration/BBSControl/sql/create_blackboard_triggers.sql @@ -7,8 +7,8 @@ $$ $$ LANGUAGE plpgsql; -CREATE TRIGGER modify_register - AFTER INSERT OR UPDATE OR DELETE ON blackboard.worker_register +CREATE TRIGGER modify_worker + AFTER INSERT OR UPDATE OR DELETE ON blackboard.worker FOR EACH STATEMENT EXECUTE PROCEDURE blackboard.notify_client(); CREATE TRIGGER insert_command