Symptoms
Sometimes when Syncing Groups the below error is thrown in the K2 hostServer Logs.
64007 Violation of PRIMARY KEY constraint 'PK_Identity_IdentityUpdate'. Cannot insert duplicate key in object 'Identity.IdentityUpdate'. The duplicate key value is (137, 0x005f0729c6eb360cade6c9e403baa06f, 4, 1).
Full stack trace:
64007 Violation of PRIMARY KEY constraint 'PK_Identity_IdentityUpdate'. Cannot insert duplicate key in object 'Identity.IdentityUpdate'. The duplicate key value is (137, 0x005f0729c6eb360cade6c9e403baa06f, 4, 1).
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Booleanand dataReady)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlBulkCopy.RunParser(BulkCopySimpleResultSet bulkCopyHandler)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinuedOnSuccess(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestAsync(CancellationToken cts, TaskCompletionSource`1 source)
at System.Data.SqlClient.SqlBulkCopy.WriteToServerInternalAsync(CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServerAsync(Int32 columnCount, CancellationToken ctoken)
at System.Data.SqlClient.SqlBulkCopy.WriteToServer(IDataReader reader)
at SourceCode.Data.SafeSqlClient.ExecuteBulkCopy(IDataReader reader)
at SourceCode.Hosting.Runtime.SqlCacheManager.BulkUpdateIdentityMembers(IDataReader reader, IdentityResolveRequest request, DateTime lastModified, Boolean replaceUsers, Boolean replaceGroups, Boolean replaceRoles)
at SourceCode.Hosting.Runtime.ResolverManager.ResolveIdentityRequest(IdentityResolveRequest request)
We suspect since there are two K2 server nodes(clustered), both servers are attempting to add a user to the bulk update but one did successfully and the other is failing.
Diagnoses
Identity Service SQL Bulk operations needs to be safe guarded against failed transactions.
Resolution
We have made changes to this mechanism in Appit 1.4 which removes some of the surrounding complexity of these SQL bulk operations which will solve the Primary Key violations.
Restarting the K2 Server will clear the IdentityUpdate table and the issue should be resolved.