Skip to content

Commit 476e472

Browse files
authored
Fix/rewrite jobs fqn locks (#2067)
* Update rewrite_jobs_fqn_table function to correctly ignore existing symlinked jobs Signed-off-by: Michael Collado <collado.mike@gmail.com> * Update rewrite_jobs_fqn_table to avoid updating jobs_fqn table when nothing has changed Signed-off-by: Michael Collado <collado.mike@gmail.com> Signed-off-by: Michael Collado <collado.mike@gmail.com>
1 parent 3dba995 commit 476e472

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

api/src/main/resources/marquez/db/migration/R__1_Jobs_view_and_rewrite_function.sql

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ CREATE OR REPLACE FUNCTION rewrite_jobs_fqn_table() RETURNS TRIGGER AS
2727
$$
2828
DECLARE
2929
job_uuid uuid;
30+
new_symlink_target_uuid uuid;
31+
old_symlink_target_uuid uuid;
3032
inserted_job jobs_view%rowtype;
3133
BEGIN
3234
INSERT INTO jobs (uuid, type, created_at, updated_at, namespace_uuid, name, description,
@@ -55,13 +57,19 @@ BEGIN
5557
current_job_context_uuid = EXCLUDED.current_job_context_uuid,
5658
current_location = EXCLUDED.current_location,
5759
current_inputs = EXCLUDED.current_inputs,
58-
-- update the symlink target if not null. otherwise, keep the old value
59-
symlink_target_uuid = COALESCE(
60-
EXCLUDED.symlink_target_uuid,
61-
jobs.symlink_target_uuid)
62-
RETURNING uuid INTO job_uuid;
63-
IF TG_OP = 'INSERT' OR
64-
(TG_OP = 'UPDATE' AND OLD.symlink_target_uuid IS DISTINCT FROM NEW.symlink_target_uuid) THEN
60+
-- update the symlink target if null. otherwise, keep the old value
61+
symlink_target_uuid = COALESCE(jobs.symlink_target_uuid,
62+
EXCLUDED.symlink_target_uuid)
63+
-- the SELECT statement below will get the OLD symlink_target_uuid in case of update and the NEW
64+
-- version in case of insert
65+
RETURNING uuid, symlink_target_uuid, (SELECT symlink_target_uuid FROM jobs j2 WHERE j2.uuid=jobs.uuid)
66+
INTO job_uuid, new_symlink_target_uuid, old_symlink_target_uuid;
67+
68+
-- update the jobs_fqn table only when inserting a new record (NEW.uuid will equal the job_uuid
69+
-- when inserting a new record) or when the symlink_target_uuid is being updated.
70+
IF NEW.uuid = job_uuid OR
71+
(new_symlink_target_uuid IS DISTINCT FROM old_symlink_target_uuid) THEN
72+
RAISE LOG 'Updating jobs_fqn due to % to job % (%)', TG_OP, NEW.name, job_uuid;
6573
WITH RECURSIVE
6674
jobs_symlink AS (SELECT uuid, uuid AS link_target_uuid, symlink_target_uuid
6775
FROM jobs j
@@ -124,4 +132,4 @@ CREATE TRIGGER update_symlinks
124132
INSTEAD OF UPDATE OR INSERT
125133
ON jobs_view
126134
FOR EACH ROW
127-
EXECUTE FUNCTION rewrite_jobs_fqn_table();
135+
EXECUTE FUNCTION rewrite_jobs_fqn_table();

api/src/test/java/marquez/db/JobDaoTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ public void testUpsertJobWithNewSymlink() {
171171
jobDao.findJobByName(symlinkJob.getNamespaceName(), symlinkJob.getName()),
172172
targetJob.getNamespaceName(),
173173
targetJob.getName());
174+
175+
// try to update the symlink target - it should be ignored
176+
JobRow anotherTargetJob =
177+
createJobWithoutSymlinkTarget(
178+
jdbi, namespace, "anotherTarget", "we'll attempt to update the symlink");
179+
createJobWithSymlinkTarget(
180+
jdbi, namespace, symlinkJobName, anotherTargetJob.getUuid(), "the symlink job");
181+
182+
// the original symlink target should be returned
183+
assertJobIdEquals(
184+
jobDao.findJobByName(symlinkJob.getNamespaceName(), symlinkJob.getName()),
185+
targetJob.getNamespaceName(),
186+
targetJob.getName());
174187
}
175188

176189
public void testSymlinkParentJobRenamesChildren() throws SQLException {

0 commit comments

Comments
 (0)