redis / redis-om-dotnet

Object mapping, and more, for Redis and .NET
MIT License
457 stars 76 forks source link

An item with the same key has already been added #476

Open JesusVFR opened 3 weeks ago

JesusVFR commented 3 weeks ago

Hi guys,

I'm just load testing the API to see how fast we can read and write documents with OM library.

I've create a Model like:

[Document(IndexName = "aggregations-idx", StorageType = StorageType.Json, Prefixes = [CacheKeysOptions.Aggregations])]
public sealed class Aggregation
{
    private readonly string _key = default!;

    [RedisIdField] [Indexed] public string Id { get; set; }

   [Searchable]
   public string Key => !string.IsNullOrEmpty(_key)
    ? _key
    : Convert.ToBase64String(Encoding.UTF8.GetBytes($"{SelectionId}{Brand:D}{Segment:D}"));

    [Indexed] public int MarketId { get; set; }

    [Indexed] public long SelectionId { get; set; }

    public SegmentType Segment { get; set; }

    public BrandType Brand { get; set; }

    public int SingleTotalBets { get; set; }

    public double SingleTotalStake { get; set; }

    public double SingleTotalWon { get; set; }

    public double MultipleTotalStake { get; set; }

    public double MultipleTotalWon { get; set; }

    public double MultipleRealStake { get; set; }

    public double Slur0 { get; set; }

    public double Slur1 { get; set; }

    public double Slur2 { get; set; }

    public double Slur3 { get; set; }

    public double Slur4 { get; set; }

    public double Slur5 { get; set; }

    public long LastModifiedTimestamp { get; set; }
}

as you can see we used : [Searchable] Key field.

When I try to read from a multithread context the key, I get error like:

"An item with the same key has already been added. Key: xxxxxxxxx"

The stacktrace is:

System.ArgumentException
  HResult=0x80070057
  Message=An item with the same key has already been added. Key: Aggregations:01J5ZXHVCC4PG7R6K526DR53TE
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException[T](T key)
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Redis.OM.Modeling.RedisCollectionStateManager.InsertIntoData(String key, Object value)
   at Redis.OM.Searching.RedisCollectionEnumerator`1.ConcatenateRecords()
   at Redis.OM.Searching.RedisCollectionEnumerator`1.<GetNextChunkAsync>d__21.MoveNext()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Redis.OM.Searching.RedisCollectionEnumerator`1.<MoveNextAsync>d__17.MoveNext()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Redis.OM.Searching.RedisCollection`1.<ToListAsync>d__40.MoveNext()
   at Redis.OM.Searching.RedisCollection`1.<ToListAsync>d__40.MoveNext()
   at Phoenix.Risk.RunningOn.Application.Repositories.AggregationRepository.<GetByKeyAsync>d__5.MoveNext() in C:\Users\a.mori\Sviluppo\Phoenix\RunningOnService\src\Phoenix.Risk.RunningOn.Application\Repositories\AggregationRepository.cs:line 43

  This exception was originally thrown at this call stack:
    [External Code]
    Phoenix.Risk.RunningOn.Application.Repositories.AggregationRepository.GetByKeyAsync(Phoenix.Risk.RunningOn.Domain.Entities.Aggregation, System.Threading.CancellationToken) in AggregationRepository.cs

I try to get the Document by the field "Key" and I get Error on method "InsertIntoData"

slorello89 commented 3 weeks ago

The RedisCollection is not thread safe if you do not disable saveState see https://github.com/redis/redis-om-dotnet/blob/main/docs/faq.md