From c5200c596d76883b42241bb62948fb32b0f15930 Mon Sep 17 00:00:00 2001 From: Tanishq Gandhi Date: Thu, 29 Oct 2026 20:39:53 -0700 Subject: [PATCH 1/3] fix(hub): record an anonymous view with a NULL uid instead of failing with 500 A logged-out viewer sends userId 0, which no user row has, so the user_action insert broke its foreign key after the view count was already incremented. postView now records the view with a NULL uid, and recordUserAction defaults to null instead of 0. Closes #9225 --- .../web/resource/dashboard/hub/HubResource.scala | 6 ++++-- .../web/resource/dashboard/hub/HubResourceSpec.scala | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala index 0c703067f22..06c40d39c3a 100644 --- a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala +++ b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala @@ -147,7 +147,7 @@ object HubResource { */ def recordUserAction( request: HttpServletRequest, - userId: Integer = Integer.valueOf(0), + userId: Integer = null, entityId: Integer, entityType: EntityType, action: ActionType @@ -418,7 +418,9 @@ class HubResource { .returning(viewCountColumn) .fetchOne() - recordUserAction(request, userId, entityID, entityType, View) + // An anonymous viewer has no user row, so the view is recorded with a NULL uid. + val viewerUid = Option(userId).filter(_.intValue() > 0).orNull + recordUserAction(request, viewerUid, entityID, entityType, View) record.get(viewCountColumn) } diff --git a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala index d634392a2db..b52293c9801 100644 --- a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala @@ -795,6 +795,17 @@ class HubResourceSpec ) } + it should "record an anonymous view (uid 0 or none) with a NULL uid" in { + val id = seedWorkflow(810603, "wf_view_anonymous") + hub.postView(req, ViewRequest(id, Integer.valueOf(0), Wf)) shouldBe 1 + hub.postView(req, ViewRequest(id, null, Wf)) shouldBe 2 + + auditRows() shouldBe Seq( + (null, "workflow", 810603, "view", "127.0.0.1"), + (null, "workflow", 810603, "view", "127.0.0.1") + ) + } + it should "keep workflow and dataset view counts apart when the ids collide" in { seedWorkflow(830001, "wf_view_shared_id") seedDataset(830001, "ds_view_shared_id") From 5526d69474b52c597813b70a751e1dd7ce499840 Mon Sep 17 00:00:00 2001 From: Tanishq Gandhi Date: Thu, 5 Nov 2026 10:18:54 -0800 Subject: [PATCH 2/3] fix(hub): document the NULL anonymous uid and test recordUserAction's default --- .../texera/web/resource/dashboard/hub/HubResource.scala | 2 +- .../web/resource/dashboard/hub/HubResourceSpec.scala | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala index 06c40d39c3a..e747cea6621 100644 --- a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala +++ b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala @@ -140,7 +140,7 @@ object HubResource { * Records a user's action in the system. * * @param request The HTTP request object to extract the user's IP address. - * @param userId The ID of the user performing the action (default is 0 for anonymous users). + * @param userId The ID of the user performing the action; omitted (NULL) for an anonymous user. * @param entityId The ID of the entity associated with the action. * @param entityType The type of entity being acted upon (validated before processing). * @param action The action performed by the user ("like", "unlike", "view", "clone"). diff --git a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala index b52293c9801..21a7a07a6a7 100644 --- a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala @@ -453,6 +453,13 @@ class HubResourceSpec auditRows() shouldBe Seq((Integer.valueOf(ownerUid), "workflow", 810102, "clone", null)) } + it should "store a NULL uid when userId is omitted" in { + val id = seedWorkflow(810103, "wf_audit_anonymous") + recordUserAction(req, entityId = id, entityType = Wf, action = ActionType.View) + + auditRows() shouldBe Seq((null, "workflow", 810103, "view", "127.0.0.1")) + } + it should "tag each row with its own entity type, keeping equal ids apart" in { // Same numeric id on purpose: only resource_type separates the two rows. seedWorkflow(830001, "wf_shared_id") From cd4d0d58ccd4afc9dd4a2797f34bfcaae5b3a0c7 Mon Sep 17 00:00:00 2001 From: Tanishq Gandhi Date: Mon, 16 Nov 2026 23:25:21 -0800 Subject: [PATCH 3/3] fix(amber): record a view and its count in one transaction Takes the anonymous viewer as Option[Integer] instead of a null uid, and writes the view count and its user_action row together, so a view that cannot be recorded does not raise the count. --- .../resource/dashboard/hub/HubResource.scala | 44 ++++++++++--------- .../dashboard/hub/HubResourceSpec.scala | 26 ++++++++--- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala index e747cea6621..3740c90a0d2 100644 --- a/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala +++ b/amber/src/main/scala/org/apache/texera/web/resource/dashboard/hub/HubResource.scala @@ -37,7 +37,7 @@ import org.apache.texera.web.resource.dashboard.user.workflow.WorkflowResource.{ mapWorkflowEntries } import org.jooq.impl.DSL -import org.jooq.{Record, Table, TableField} +import org.jooq.{DSLContext, Record, Table, TableField} import java.util.regex.Pattern import javax.annotation.security.RolesAllowed @@ -144,24 +144,26 @@ object HubResource { * @param entityId The ID of the entity associated with the action. * @param entityType The type of entity being acted upon (validated before processing). * @param action The action performed by the user ("like", "unlike", "view", "clone"). + * @param ctx The context to write with, so a caller can include the row in its transaction. */ def recordUserAction( request: HttpServletRequest, - userId: Integer = null, + userId: Option[Integer] = None, entityId: Integer, entityType: EntityType, - action: ActionType + action: ActionType, + ctx: DSLContext = context ): Unit = { val userIp = request.getRemoteAddr val actionEnum = ActionEnum.values().find(_.getLiteral.equalsIgnoreCase(action.value)).get - val query = context + val query = ctx .insertInto(USER_ACTION) - .set(USER_ACTION.UID, userId) .set(USER_ACTION.RESOURCE_ID, entityId) .set(USER_ACTION.RESOURCE_TYPE, entityType.value) .set(USER_ACTION.ACTION, actionEnum) + userId.foreach(query.set(USER_ACTION.UID, _)) if (ipv4Pattern.matcher(userIp).matches()) { query.set(USER_ACTION.IP, userIp) } @@ -203,7 +205,7 @@ object HubResource { .set(idColumn, entityId) .execute() - recordUserAction(request, userId, entityId, entityType, Like) + recordUserAction(request, Some(userId), entityId, entityType, Like) true } else if (!isLike && alreadyLiked) { context @@ -211,7 +213,7 @@ object HubResource { .where(uidColumn.eq(userId).and(idColumn.eq(entityId))) .execute() - recordUserAction(request, userId, entityId, entityType, Unlike) + recordUserAction(request, Some(userId), entityId, entityType, Unlike) true } else { false @@ -237,7 +239,7 @@ object HubResource { val (table, uidColumn, idColumn) = (entityTables.table, entityTables.uidColumn, entityTables.idColumn) - recordUserAction(request, userId, entityId, entityType, Clone) + recordUserAction(request, Some(userId), entityId, entityType, Clone) val existingCloneRecord = context .selectFrom(table) @@ -409,20 +411,22 @@ class HubResource { val (table, idColumn, viewCountColumn) = (entityTables.table, entityTables.idColumn, entityTables.viewCountColumn) - val record = context - .insertInto(table) - .set(idColumn, entityID) - .set(viewCountColumn, Integer.valueOf(1)) - .onDuplicateKeyUpdate() - .set(viewCountColumn, viewCountColumn.add(1)) - .returning(viewCountColumn) - .fetchOne() - // An anonymous viewer has no user row, so the view is recorded with a NULL uid. - val viewerUid = Option(userId).filter(_.intValue() > 0).orNull - recordUserAction(request, viewerUid, entityID, entityType, View) + val viewerUid = Option(userId).filter(_.intValue() > 0) - record.get(viewCountColumn) + // One transaction, so a view whose record cannot be stored does not raise the count either. + SqlServer.withTransaction(context) { ctx => + val record = ctx + .insertInto(table) + .set(idColumn, entityID) + .set(viewCountColumn, Integer.valueOf(1)) + .onDuplicateKeyUpdate() + .set(viewCountColumn, viewCountColumn.add(1)) + .returning(viewCountColumn) + .fetchOne() + recordUserAction(request, viewerUid, entityID, entityType, View, ctx) + record.get(viewCountColumn) + } } /** diff --git a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala index 21a7a07a6a7..d5c53b59e42 100644 --- a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubResourceSpec.scala @@ -432,13 +432,19 @@ class HubResourceSpec } "recordUserAction" should "insert a user_action row for the entity" in { - recordUserAction(req, Integer.valueOf(likerUid), Integer.valueOf(wid), Wf, ActionType.Like) + recordUserAction( + req, + Some(Integer.valueOf(likerUid)), + Integer.valueOf(wid), + Wf, + ActionType.Like + ) userActionRows shouldBe 1 } it should "persist uid, resource type, resource id, action and an IPv4 address" in { val id = seedWorkflow(810101, "wf_audit") - recordUserAction(req, Integer.valueOf(likerUid), id, Wf, ActionType.View) + recordUserAction(req, Some(Integer.valueOf(likerUid)), id, Wf, ActionType.View) auditRows() shouldBe Seq( (Integer.valueOf(likerUid), "workflow", 810101, "view", "127.0.0.1") @@ -447,7 +453,7 @@ class HubResourceSpec it should "leave the ip column null when the remote address is not IPv4-shaped" in { val id = seedWorkflow(810102, "wf_audit_v6") - recordUserAction(nonIpv4Req, Integer.valueOf(ownerUid), id, Wf, ActionType.Clone) + recordUserAction(nonIpv4Req, Some(Integer.valueOf(ownerUid)), id, Wf, ActionType.Clone) // Everything but the address is still recorded; only the ip column is skipped. auditRows() shouldBe Seq((Integer.valueOf(ownerUid), "workflow", 810102, "clone", null)) @@ -466,8 +472,8 @@ class HubResourceSpec seedDataset(830001, "ds_shared_id") val shared = Integer.valueOf(830001) - recordUserAction(req, Integer.valueOf(ownerUid), shared, Wf, ActionType.Like) - recordUserAction(req, Integer.valueOf(ownerUid), shared, Ds, ActionType.Unlike) + recordUserAction(req, Some(Integer.valueOf(ownerUid)), shared, Wf, ActionType.Like) + recordUserAction(req, Some(Integer.valueOf(ownerUid)), shared, Ds, ActionType.Unlike) auditRows().map { case (_, resourceType, _, action, _) => (resourceType, action) } shouldBe Seq(("workflow", "like"), ("dataset", "unlike")) @@ -813,6 +819,16 @@ class HubResourceSpec ) } + it should "leave the count unchanged when the view cannot be recorded" in { + val id = seedWorkflow(810605, "wf_view_missing_user") + hub.postView(req, ViewRequest(id, Integer.valueOf(likerUid), Wf)) shouldBe 1 + + // A uid with no user row, e.g. an account deleted while another tab was still open. + an[Exception] should be thrownBy hub.postView(req, ViewRequest(id, Integer.valueOf(999999), Wf)) + + workflowViewCount(810605) shouldBe Some(1) + } + it should "keep workflow and dataset view counts apart when the ids collide" in { seedWorkflow(830001, "wf_view_shared_id") seedDataset(830001, "ds_view_shared_id")