Smart Client hangs as a result of duplicate key values

At one of the locations, where we’re sending Alarms and Events to Milestone (using MIP SDK’s Component Integration APIs), the Milestone Smart Client hangs as a result of duplicate key-values. This is the error printed in the logs:

2021-06-01 12:34:03 PM UTC+03:00 Error XPathSqlConnection Error importing XML: Error executing SQL command: ‘INSERT INTO [Central].[t001_eventheader_t002] (alarm_t001_ID,class,customtag,id,message,name,priority,timestamp,type,version,priorityname,messageid ) VALUES (484361,‘Analytics’,‘Office’,‘3402c0ae-3032-4242-a1ba-200bfcb02f5d’,‘TRIPWIRE’,‘TRIPWIRE’,1,6/1/2021 9:33:59 AM,‘Person’,‘1.0’,‘New’,) SET = SCOPE_IDENTITY()’. InnerException: Cannot insert duplicate key row in object ‘Central.T001_EventHeader_T002’ with unique index ‘IX_T001_EventHeader_T002_ID’. The duplicate key value is (3402c0ae-3032-4242-a1ba-200bfcb02f5d).

The statement has been terminated.

Skipping import of root node: . EventHeader ID: 3402c0ae-3032-4242-a1ba-200bfcb02f5d

2021-06-01 12:34:03 PM UTC+03:00 Error EventHandler Failed to write event to database.

SqlException: Message: Cannot insert duplicate key row in object ‘Central.Event_Active’ with unique index ‘IX_Event_Active_UniqueId’. The duplicate key value is (f5ad62a0-fc2b-469d-8524-cb1b50befc23).

InnerException.Message: Null

InnerException.Type: Null

Number: 2601

ErrorCode: -2146232060

Procedure: Central.Event_Add

State: 1

Source: .Net SqlClient Data Provider

Please suggest a way to resolve this and also why help me understand as to why is this happening.

Attaching the full log -

What you must troubleshoot is how you send the events.

For reference you might be using methods of a sample, and you could investigate whether the sample works, if not it might indicate an issue with your test server setup.

If you want more detailed help in troubleshooting please start by sharing how you in your code submit events.

Sure. We’re using the the samples as reference to submit the events.

For instance:

To submit an Alarm -

EventHeader eventHeader = new EventHeader()

  {

    ID = Guid.NewGuid(),

    Class = "Analytics",

    Type = alarmDetails.alertObject.Type,

    Timestamp = alarmDetails.sourceTime.Subtract(new TimeSpan(0, 0, 3)),

    Message = alarmMessage,

    Name = alarmDetails.ruleName,

    Source = eventSource,

    Priority = (ushort)(alarmDetails.RegionPriority + 1),

    PriorityName = priorityStr,

    CustomTag = alarmDetails.alarmName

  };

    Alarm alarm = new Alarm()

    {

      AssignedTo = milestoneUsername,

      EventHeader = eventHeader,

      Vendor = vendor,

      RuleList = ruleList,

      ObjectList = objectList,

      Description = alarmDetails.RegionName,

      Location = alarmDetails.sourceLocation,

      StartTime = alarmDetails.sourceTime.Subtract(new TimeSpan(0, 0, 5)),

      EndTime = eventHeader.Timestamp.AddSeconds(5),

      State = 1

    };

EnvironmentManager.Instance.SendMessage(

      new VideoOS.Platform.Messaging.Message(

        MessageId.Server.NewAlarmCommand) { Data = alarm });

And to send event:

EventHeader eventHeader1 = new EventHeader()

      {

        ID = Guid.NewGuid(),

        Class = "Analytics",

        Type = alarmDetails.alertObject.Type,

        Timestamp = alarmDetails.sourceTime.Subtract(new TimeSpan(0, 0, 3)),

        Message = alarmDetails.ruleName,

        Name = alarmDetails.ruleName,

        Source = eventSource,

        CustomTag = alarmDetails.alarmName

      };

      AnalyticsEvent analyticsEvent = new AnalyticsEvent();

      analyticsEvent.EventHeader = eventHeader1;

      analyticsEvent.Location = alarmDetails.sourceLocation;

      analyticsEvent.Description = companyNameInFilenames + " Analytics Event";

      analyticsEvent.Vendor = vendor;

      analyticsEvent.ObjectList = objectList;

EnvironmentManager.Instance.SendMessage(

        new VideoOS.Platform.Messaging.Message(MessageId.Server.NewEventCommand) { Data = analyticsEvent });

My question is - is the EventHeader.ID field necessary to be filled? (since that seems to be causing the duplicate issue)

Also, what happens if the ID is assigned all zeroes?

What I can comfortably say is that setting the ID is the recommended method and is what our samples show.

I did a test and was able to make the LibraryEventGenerator sample work without setting the ID.

My suspicion is that the issue is not caused by sending wrong events but by a corruption in the database. The key question here might be, does the issue go away if you stop your integration from submitting events?

Thanks for confirming that setting the ID is not mandatory. The database corruption was the likely cause and the database was cleared/reset. But yes, the issue goes away if we stop submitting the events and comes back when we start.

How do we resolve this? Please help in this regard.

OK. There might be no corruption but a break down in the code that submits the events.

If not for the understanding that you have this working in other sites I would ask you to single step debug the sending of events.

There is one thing the puzzles me and I would like to ask clarifying questions.

Normally the recommendation is that a solution submits analytics events, these are matched against enabled alarm definitions (setup in Management Client), and if there is a match an alarm is triggered.

The alternative is to not assume any setup by the customers, in this case you submit alarms directly instead.

Questions: Are you sending both alarm and events? Does your customer have enabled alarm definition that will raise an alarm based on the incoming analytics event?

Thanks for your prompt response.

Yes, we’re sending both alarm and events. And yes, the customer has enabled alarm definition as well.

Do you think that has caused the problem here?

My initial idea was no. Having covered your code I am beginning to think of this as one of the least improbable causes. Can you please test?

Preferably test by only submitting events and letting the enabled alarm definitions take care of the triggering of alarms.

I would like to have your assessment.

Is this integration working in many XProtect sites and at one site it fails?

Or is this the first deployment and you do not have similar sites where this has proven to work?

Something in between?

Sure, we’ll test this part of submitting only events and letting alarm definitions take care of triggering of alarms.

Yes, this integration is working in a lot of places over the past decade. We’ve seen this issue the first time and only here.

Update (more of an FYI) - On the mentioned site, we found that there are no alarm definitions created. We’re submitting events as well as alarms.

Is there anything else that can be done to fix this issue?

Thank you for you update. This issue might not be a common issue so can you please create a new case via MyMilestone? When you create the case, please add all the information and attach the logs (if you have). Then, Milestone Technical Support could help you with solving the issue.

I’m not sure how to do that. I don’t see any option to create a case in my profile (even in My Milestone Portal). Can you please help me do that? There is a Technical Support person from Milestone assigned to this particular project, however. But the person wasn’t able to get the reason, which is why I turned to the dev forum for answers.

We have created support case MSC607740, we might later post here if we learn something that is good to share with Developer Forum.