From fd8a8c8b67a5db9e2c50dfc1212792403587f153 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 19 Mar 2019 17:12:55 -0400 Subject: [PATCH] make event message processing idempotent --- src/Core/Models/Data/EventMessage.cs | 2 ++ src/Core/Models/Data/EventTableEntity.cs | 13 ++++++++----- .../Repositories/TableStorage/EventRepository.cs | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Core/Models/Data/EventMessage.cs b/src/Core/Models/Data/EventMessage.cs index 3f75914b7..452853bfe 100644 --- a/src/Core/Models/Data/EventMessage.cs +++ b/src/Core/Models/Data/EventMessage.cs @@ -8,6 +8,7 @@ namespace Bit.Core.Models.Data public EventMessage() { } public EventMessage(CurrentContext currentContext) + : base() { IpAddress = currentContext.IpAddress; DeviceType = currentContext.DeviceType; @@ -24,5 +25,6 @@ namespace Bit.Core.Models.Data public Guid? ActingUserId { get; set; } public DeviceType? DeviceType { get; set; } public string IpAddress { get; set; } + public Guid? IdempotencyId { get; private set; } = Guid.NewGuid(); } } diff --git a/src/Core/Models/Data/EventTableEntity.cs b/src/Core/Models/Data/EventTableEntity.cs index dc31698ca..052b08765 100644 --- a/src/Core/Models/Data/EventTableEntity.cs +++ b/src/Core/Models/Data/EventTableEntity.cs @@ -65,7 +65,8 @@ namespace Bit.Core.Models.Data return result; } - public override void ReadEntity(IDictionary properties, OperationContext operationContext) + public override void ReadEntity(IDictionary properties, + OperationContext operationContext) { base.ReadEntity(properties, operationContext); @@ -82,9 +83,9 @@ namespace Bit.Core.Models.Data } } - public static List IndexEvent(IEvent e) + public static List IndexEvent(EventMessage e) { - var uniquifier = Guid.NewGuid(); + var uniquifier = e.IdempotencyId.GetValueOrDefault(Guid.NewGuid()); var pKey = e.OrganizationId.HasValue ? $"OrganizationId={e.OrganizationId}" : $"UserId={e.UserId}"; var dateKey = CoreHelpers.DateTimeToTableStorageKey(e.Date); @@ -102,7 +103,8 @@ namespace Bit.Core.Models.Data entities.Add(new EventTableEntity(e) { PartitionKey = pKey, - RowKey = string.Format("ActingUserId={0}__Date={1}__Uniquifier={2}", e.ActingUserId, dateKey, uniquifier) + RowKey = string.Format("ActingUserId={0}__Date={1}__Uniquifier={2}", + e.ActingUserId, dateKey, uniquifier) }); } @@ -111,7 +113,8 @@ namespace Bit.Core.Models.Data entities.Add(new EventTableEntity(e) { PartitionKey = pKey, - RowKey = string.Format("CipherId={0}__Date={1}__Uniquifier={2}", e.CipherId, dateKey, uniquifier) + RowKey = string.Format("CipherId={0}__Date={1}__Uniquifier={2}", + e.CipherId, dateKey, uniquifier) }); } diff --git a/src/Core/Repositories/TableStorage/EventRepository.cs b/src/Core/Repositories/TableStorage/EventRepository.cs index 8b574376d..62c58596b 100644 --- a/src/Core/Repositories/TableStorage/EventRepository.cs +++ b/src/Core/Repositories/TableStorage/EventRepository.cs @@ -109,7 +109,7 @@ namespace Bit.Core.Repositories.TableStorage public async Task CreateEntityAsync(ITableEntity entity) { - await _table.ExecuteAsync(TableOperation.Insert(entity)); + await _table.ExecuteAsync(TableOperation.InsertOrReplace(entity)); } public async Task> GetManyAsync(string partitionKey, string rowKey,