From 32d42c21e25075cb3a52b8ed4da4faf0619c5a87 Mon Sep 17 00:00:00 2001 From: Tom Szendrey Date: Mon, 6 Jan 2025 14:49:39 -0500 Subject: [PATCH] Revert "simplify collab/sync" (#5958) --- .../src/components/workflow/tera-workflow.vue | 12 +++-- .../hmi-client/src/services/workflow.ts | 50 +++++++++++-------- .../service/data/WorkflowService.java | 8 +++ 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/packages/client/hmi-client/src/components/workflow/tera-workflow.vue b/packages/client/hmi-client/src/components/workflow/tera-workflow.vue index 36a53f1e88..7fd5bd41aa 100644 --- a/packages/client/hmi-client/src/components/workflow/tera-workflow.vue +++ b/packages/client/hmi-client/src/components/workflow/tera-workflow.vue @@ -188,7 +188,7 @@ import { useRouter, useRoute } from 'vue-router'; import { MenuItem } from 'primevue/menuitem'; import * as EventService from '@/services/event'; import { useProjects } from '@/composables/project'; -// import useAuthStore from '@/stores/auth'; +import useAuthStore from '@/stores/auth'; import { cloneNoteBookSession } from '@/services/notebook-session'; import * as SimulateCiemssOp from '@/components/workflow/ops/simulate-ciemss/mod'; import * as StratifyMiraOp from '@/components/workflow/ops/stratify-mira/mod'; @@ -212,7 +212,7 @@ import { activeProjectId } from '@/composables/activeProject'; const WORKFLOW_SAVE_INTERVAL = 4000; -// const currentUserId = useAuthStore().user?.id; +const currentUserId = useAuthStore().user?.id; const registry = new workflowService.WorkflowRegistry(); registry.registerOp(SimulateCiemssOp); @@ -289,11 +289,13 @@ const _updateWorkflow = (event: ClientEvent) => { if (event.data.id !== wf.value.getId()) { return; } - wf.value.update(event.data as Workflow); + + const delayUpdate = isDragging || event.userId === currentUserId; + wf.value.update(event.data as Workflow, delayUpdate); }; -const saveWorkflowDebounced = debounce(_saveWorkflow, 1000); -const updateWorkflowHandler = debounce(_updateWorkflow, 800); +const saveWorkflowDebounced = debounce(_saveWorkflow, 400); +const updateWorkflowHandler = debounce(_updateWorkflow, 250); const saveWorkflowHandler = () => { saveWorkflowDebounced(); diff --git a/packages/client/hmi-client/src/services/workflow.ts b/packages/client/hmi-client/src/services/workflow.ts index 87c49d259e..52665ab5db 100644 --- a/packages/client/hmi-client/src/services/workflow.ts +++ b/packages/client/hmi-client/src/services/workflow.ts @@ -55,8 +55,14 @@ export class WorkflowWrapper { * FIXME: Need to split workflow into different categories and sending the commands * instead of the result. It is possible here to become de-synced: eg state-update-response * comes in as we are about to change the output ports. + * + * delayUpdate is a used to indicate there are actions in progress, and an update from + * the DB can potentially overwrite what the user had already done that have yet to be flushed + * to the backend. In situation like this, we will update the version (so our subsequent updates are + * not rejected) and skip the rest. For example, the user may be dragging an operator on the + * canvas when the db upate comes in. * */ - update(updatedWF: Workflow) { + update(updatedWF: Workflow, delayUpdate: boolean) { if (updatedWF.id !== this.wf.id) { throw new Error(`Workflow failed, inconsistent ids updated=${updatedWF.id} self=${this.wf.id}`); } @@ -68,27 +74,27 @@ export class WorkflowWrapper { const updatedNodeMap = new Map>(updatedWF.nodes.map((n) => [n.id, n])); const updatedEdgeMap = new Map(updatedWF.edges.map((e) => [e.id, e])); - /* FIXME: Comment out to to simplify sync logic, to remove if things are stable - Jan 2025 */ - // if (delayUpdate) { - // for (let i = 0; i < nodes.length; i++) { - // const nodeId = nodes[i].id; - // const updated = updatedNodeMap.get(nodeId); - // if (updated) { - // if (!nodes[i].version || (updated.version as number) > (nodes[i].version as number)) { - // nodes[i].version = updated.version; - // } - // } - // } - // for (let i = 0; i < edges.length; i++) { - // const edgeId = edges[i].id; - // const updated = updatedEdgeMap.get(edgeId); - // if (updated) { - // if (!edges[i].version || (updated.version as number) > (edges[i].version as number)) { - // edges[i].version = updated.version; - // } - // } - // } return; - // } + if (delayUpdate) { + for (let i = 0; i < nodes.length; i++) { + const nodeId = nodes[i].id; + const updated = updatedNodeMap.get(nodeId); + if (updated) { + if (!nodes[i].version || (updated.version as number) > (nodes[i].version as number)) { + nodes[i].version = updated.version; + } + } + } + for (let i = 0; i < edges.length; i++) { + const edgeId = edges[i].id; + const updated = updatedEdgeMap.get(edgeId); + if (updated) { + if (!edges[i].version || (updated.version as number) > (edges[i].version as number)) { + edges[i].version = updated.version; + } + } + } + return; + } // Update and deletes for (let i = 0; i < nodes.length; i++) { diff --git a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/service/data/WorkflowService.java b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/service/data/WorkflowService.java index b3289a243b..0eee4ba1ef 100644 --- a/packages/server/src/main/java/software/uncharted/terarium/hmiserver/service/data/WorkflowService.java +++ b/packages/server/src/main/java/software/uncharted/terarium/hmiserver/service/data/WorkflowService.java @@ -84,6 +84,7 @@ public Optional updateAsset( final UUID projectId, final Schema.Permission hasWritePermission ) throws IOException, IllegalArgumentException { + final long updateStart = System.currentTimeMillis(); // Fetch database copy, we will update into it final Workflow dbWorkflow = getAsset(asset.getId(), hasWritePermission).get(); @@ -190,7 +191,14 @@ public Optional updateAsset( dbWorkflowEdges.add(pair.getValue()); } + final long resolveEnd = System.currentTimeMillis(); + log.info("Resolve workflow " + dbWorkflow.getId() + " took " + (resolveEnd - updateStart)); + final Optional result = super.updateAsset(dbWorkflow, projectId, hasWritePermission); + + final long updateEnd = System.currentTimeMillis(); + log.info("Update workflow to DB " + dbWorkflow.getId() + " took " + (updateEnd - resolveEnd)); + return result; }