Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ public KeyImpression Build(TreatmentResult result, Key key, Dictionary<string, o
var impression = new KeyImpression(key.matchingKey, result.FeatureFlagName, result.Treatment, result.ImpTime, result.ChangeNumber, _labelsEnabled ? result.Label : null, key.bucketingKeyHadValue ? key.bucketingKey : null, result.ImpressionsDisabled);

var validatorResult = _propertiesValidator.IsValid(properties);
if (validatorResult.Success)
if (validatorResult.Success && properties != null)
{
if (properties != null) {
impression.Properties = JsonConvert.SerializeObject(validatorResult.Value);
}
impression.Properties = JsonConvert.SerializeObject(validatorResult.Value);
}

try
Expand Down
64 changes: 54 additions & 10 deletions src/Splitio/Services/Impressions/Classes/UniqueKeysTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class UniqueKeysTracker : TrackerComponent, IUniqueKeysTracker
private readonly ConcurrentDictionary<string, HashSet<string>> _cache;
private readonly IImpressionsSenderAdapter _senderAdapter;
private readonly ISplitTask _cacheLongTermCleaningTask;
private int _keySize;

public UniqueKeysTracker(ComponentConfig config,
IFilterAdapter filterAdapter,
Expand All @@ -34,6 +35,7 @@ public UniqueKeysTracker(ComponentConfig config,
_senderAdapter = senderAdapter;
_cacheLongTermCleaningTask = cacheLongTermCleaningTask;
_cacheLongTermCleaningTask.SetAction(_filterAdapter.Clear);
_keySize = 0;
}

#region Public Methods
Expand All @@ -48,8 +50,8 @@ public bool Track(string key, string featureName)
hashSet.Add(key);
return hashSet;
});

if (_cache.Count >= _cacheMaxSize)
_keySize++;
if (_keySize >= _maxBulkSize)
{
_taskBulkData.Start();
}
Expand All @@ -76,32 +78,74 @@ protected override async Task SendBulkDataAsync()
try
{
var uniques = new ConcurrentDictionary<string, HashSet<string>>(_cache);

var keySize = _keySize;
_cache.Clear();
_keySize = 0;

if (!uniques.Any()) return;

var values = uniques
.Select(v => new Mtks(v.Key, v.Value))
.ToList();

if (values.Count <= _maxBulkSize)
if (keySize <= _maxBulkSize)
{
await _senderAdapter.RecordUniqueKeysAsync(values);
return;
}
List<Mtks> bulksFlattened = FlattenBulks(values);
await SendAllBulks(bulksFlattened);
}
catch (Exception e)
{
_logger.Error("Exception caught sending Unique Keys.", e);
}
}

while (values.Count > 0)
protected async Task SendAllBulks(List<Mtks> bulksFlattened)
{
List<Mtks> bulksToSend = new List<Mtks>();
int bulkSize = 0;
foreach (var unique in bulksFlattened)
{
if ((unique.Keys.Count() + bulkSize) > _maxBulkSize)
{
var bulkToPost = Util.Helper.TakeFromList(values, _maxBulkSize);

await _senderAdapter.RecordUniqueKeysAsync(bulkToPost);
await _senderAdapter.RecordUniqueKeysAsync(bulksToSend);
bulkSize = 0;
bulksToSend = new List<Mtks>();
}
bulkSize += unique.Keys.Count();
bulksToSend.Add(unique);
}
catch (Exception e)
await _senderAdapter.RecordUniqueKeysAsync(bulksToSend);
}

protected List<Mtks> FlattenBulks(List<Mtks> values)
{
List<Mtks> bulksFlattened = new List<Mtks>();
foreach (var unique in values)
{
_logger.Error("Exception caught sending Unique Keys.", e);
var bulks = ConvertToBulks(unique);
bulksFlattened.AddRange(bulks);
}
return bulksFlattened;
}

protected List<Mtks> ConvertToBulks(Mtks unique)
{
if (unique.Keys.Count > _maxBulkSize)
{
List<Mtks> chunks = new List<Mtks>();
var uniqueTemp = new List<String>(unique.Keys.ToArray());
var bulks = Util.Helper.ChunkBy(uniqueTemp, _maxBulkSize);
foreach (var bulk in bulks)
{
chunks.Add(new Mtks(unique.Feature, new HashSet<string>(bulk)));
}
return chunks;
}

return new List<Mtks> { unique };
}
#endregion
}
Expand Down
4 changes: 2 additions & 2 deletions src/Splitio/Services/Shared/Classes/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public BaseConfig ReadRedisConfig(ConfigurationOptions config)
baseConfig.UniqueKeysRefreshRate = 300;
baseConfig.ImpressionsCounterRefreshRate = 300;
baseConfig.ImpressionsCountBulkSize = 10000;
baseConfig.UniqueKeysBulkSize = 10000;
baseConfig.UniqueKeysBulkSize = 5000;

return baseConfig;
}
Expand All @@ -93,7 +93,7 @@ public SelfRefreshingConfig ReadInMemoryConfig(ConfigurationOptions config)
ImpressionsCounterCacheMaxSize = baseConfig.ImpressionsCounterCacheMaxSize,
FlagSetsFilter = baseConfig.FlagSetsFilter,
FlagSetsInvalid = baseConfig.FlagSetsInvalid,
UniqueKeysBulkSize = 30000,
UniqueKeysBulkSize = 5000,
ImpressionsCountBulkSize = 30000,
UniqueKeysRefreshRate = 3600,
ImpressionsCounterRefreshRate = 1800, // Send bulk impressions count - Refresh rate: 30 min.
Expand Down
10 changes: 10 additions & 0 deletions src/Splitio/Util/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Splitio.Telemetry.Domain.Enums;
using Splitio.Telemetry.Storages;
using System.Collections.Generic;
using System.Linq;

namespace Splitio.Util
{
Expand Down Expand Up @@ -49,5 +50,14 @@ public static bool HasNonASCIICharacters(string input)

return false;
}

public static List<List<T>> ChunkBy<T>(List<T> source, int chunkSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task PeriodicTask_ShouldSendBulk()
}

[TestMethod]
public void Track_WithFullSize_ShouldSendBulk()
public void Track_WithFullSize_ShouldSendTwoBulk()
{
// Arrange.
_cache.Clear();
Expand Down Expand Up @@ -101,16 +101,17 @@ public void Track_WithFullSize_ShouldSendBulk()
Assert.AreEqual(1, values2.Count);
_cache.TryGetValue("feature-name-test-3", out HashSet<string> values3);
Assert.AreEqual(1, values3.Count);
Assert.IsTrue(_uniqueKeysTracker.Track("key-test", "feature-name-test-5"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-2", "feature-name-test-5"));

Thread.Sleep(500);
_senderAdapter.Verify(mock => mock.RecordUniqueKeysAsync(It.IsAny<List<Mtks>>()), Times.Once);
_senderAdapter.Verify(mock => mock.RecordUniqueKeysAsync(It.IsAny<List<Mtks>>()), Times.Exactly(2));

_cache.Clear();
}

[TestMethod]
public void Track_WithFullSize_ShouldSendTwoBulk()
public void Track_WithFullSize_ShouldSplitBulks()
{
// Arrange.
_cache.Clear();
Expand All @@ -122,15 +123,21 @@ public void Track_WithFullSize_ShouldSendTwoBulk()
_uniqueKeysTracker = new UniqueKeysTracker(config, _filterAdapter.Object, _cache, _senderAdapter.Object, task, cacheLongTermCleaningTask, sendBulkDataTask);

// Act && Assert.
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-1", "feature-name-test"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-2", "feature-name-test"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-3", "feature-name-test"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-4", "feature-name-test"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-5", "feature-name-test"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-6", "feature-name-test"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-1", "feature-name-test-2"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-2", "feature-name-test-2"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-2", "feature-name-test-3"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-2", "feature-name-test-4"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-2", "feature-name-test-5"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-2", "feature-name-test-6"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-3", "feature-name-test-2"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-4", "feature-name-test-2"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-5", "feature-name-test-2"));
Assert.IsTrue(_uniqueKeysTracker.Track("key-test-6", "feature-name-test-2"));

Thread.Sleep(500);
_senderAdapter.Verify(mock => mock.RecordUniqueKeysAsync(It.IsAny<List<Mtks>>()), Times.Exactly(2));
_senderAdapter.Verify(mock => mock.RecordUniqueKeysAsync(It.IsAny<List<Mtks>>()), Times.Exactly(4));

_cache.Clear();
}
Expand Down