elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
6.59k stars 1.21k forks source link

StartAt activities throw with Elsa.MongoDb 3.0.5 #4866

Closed RainerAtSpirit closed 6 months ago

RainerAtSpirit commented 10 months ago

Steps to reproduce. Create two workflows with a StartAt activity.

Execute the first one.

A record get created in bookmarks.

Execute the second.

It throws:

MongoDB.Driver.MongoCommandException: Command findAndModify failed: Plan executor error during findAndModify :: caused by :: E11000 duplicate key error collection: CorasCloud_WorkFlowV3.bookmarks index: BookmarkId_1 dup key: { BookmarkId: null }.
   at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ProcessResponse(ConnectionId connectionId, CommandMessage responseMessage)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol`1 protocol, ICoreSession session, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.CommandOperationBase`1.ExecuteProtocolAsync(IChannelSource channelSource, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.WriteCommandOperation`1.ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindAndModifyOperationBase`1.ExecuteAttemptAsync(RetryableWriteContext context, Int32 attempt, Nullable`1 transactionNumber, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableWriteOperationExecutor.ExecuteAsync[TResult](IRetryableWriteOperation`1 operation, RetryableWriteContext context, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableWriteOperationExecutor.ExecuteAsync[TResult](IRetryableWriteOperation`1 operation, IWriteBinding binding, Boolean retryRequested, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteWriteOperationAsync[TResult](IWriteBinding binding, IWriteOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteWriteOperationAsync[TResult](IClientSessionHandle session, IWriteOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
   at Elsa.MongoDb.Common.MongoDbStore`1.SaveAsync[TResult](TDocument document, Expression`1 selector, CancellationToken cancellationToken)
   at Elsa.MongoDb.Modules.Runtime.MongoBookmarkStore.SaveAsync(StoredBookmark record, CancellationToken cancellationToken)
   at Elsa.Workflows.Runtime.Services.BookmarkUpdater.StoreBookmarksAsync(String workflowInstanceId, IEnumerable`1 bookmarks, String correlationId, CancellationToken cancellationToken)
   at Elsa.Workflows.Runtime.Services.BookmarkUpdater.UpdateBookmarksAsync(UpdateBookmarksRequest request, CancellationToken cancellationToken)
   at Elsa.Workflows.Runtime.Services.BookmarksPersister.PersistBookmarksAsync(WorkflowExecutionContext context, Diff`1 diff)
   at Elsa.Workflows.Runtime.Middleware.Workflows.PersistBookmarkMiddleware.InvokeAsync(WorkflowExecutionContext context)
   at Elsa.Workflows.Runtime.Middleware.Workflows.PersistentVariablesMiddleware.InvokeAsync(WorkflowExecutionContext context)
   at Elsa.Workflows.Runtime.Middleware.Workflows.ScheduleBackgroundActivitiesMiddleware.InvokeAsync(WorkflowExecutionContext context)
   at Elsa.Workflows.Pipelines.WorkflowExecution.WorkflowExecutionPipeline.ExecuteAsync(WorkflowExecutionContext context)
   at Elsa.Workflows.Services.WorkflowRunner.RunAsync(WorkflowExecutionContext workflowExecutionContext)
   at Elsa.Workflows.Services.WorkflowRunner.RunAsync(Workflow workflow, RunWorkflowOptions options, CancellationToken cancellationToken)
   at Elsa.Workflows.Runtime.Services.WorkflowHost.StartWorkflowAsync(StartWorkflowHostOptions options, CancellationToken cancellationToken)
   at Elsa.Workflows.Runtime.Services.DefaultWorkflowRuntime.StartWorkflowAsync(IWorkflowHost workflowHost, StartWorkflowRuntimeOptions options)
   at Elsa.Workflows.Runtime.Services.DefaultWorkflowRuntime.StartWorkflowAsync(IWorkflowHost workflowHost, StartWorkflowRuntimeOptions options)
   at Elsa.Workflows.Runtime.Services.DefaultWorkflowRuntime.StartWorkflowAsync(String definitionId, StartWorkflowRuntimeOptions options)
   at Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Execute.Execute.HandleAsync(Request request, CancellationToken cancellationToken)
   at FastEndpoints.Endpoint`2.ExecAsync(CancellationToken ct)
   at FastEndpoints.Endpoint`2.ExecAsync(CancellationToken ct)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at CorasCloud.WorkFlow.Server.Middleware.JwtMiddleware.Invoke(HttpContext httpContext, IWorkFlowContext ctx) in C:\cc\CorasCloud2023\CorasCloud.WorkFlow.Server\Middleware\JwtMiddleware.cs:line 46
   at Elsa.Http.Middleware.WorkflowsMiddleware.InvokeAsync(HttpContext httpContext)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

The error is caused by the BookmarkId index, which requires unique values.

image

image

However, the payload for a Bookmark in mongo doesn't have that property, so it will fail.

image

RainerAtSpirit commented 10 months ago

As a temporary workaround we decided to delete the BookmarkId index, which wasn't used by the system anyway.

image

sfmskywalker commented 6 months ago

Hi @RainerAtSpirit , thanks for reporting this issue. I can't seem to reproduce this with the latest version of the source code (in the main branch), so I will go ahead and close this issue as solved - which means it should be fixed at least as of the upcoming 3.2 release.