Skip to content

Commit 4cb65d4

Browse files
authored
Merge pull request #333 from splitio/splitclient-init-rollback
removed mode from splitclient init
2 parents 7cfb698 + a485d53 commit 4cb65d4

10 files changed

Lines changed: 64 additions & 47 deletions

File tree

Splitio.Redis/Services/Client/Classes/RedisClient.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Splitio.Services.InputValidation.Classes;
1717
using Splitio.Services.Parsing;
1818
using Splitio.Services.Shared.Classes;
19+
using Splitio.Services.Tasks;
1920

2021
namespace Splitio.Redis.Services.Client.Classes
2122
{
@@ -29,12 +30,14 @@ public class RedisClient : SplitClient
2930
private ConnectionPoolManager _connectionPoolManager;
3031
private IFeatureFlagCacheConsumer _featureFlagCacheConsumer;
3132

32-
public RedisClient(ConfigurationOptions config, string apiKey) : base(apiKey, config.Mode)
33+
public RedisClient(ConfigurationOptions config, string apiKey) : base(apiKey)
3334
{
3435
_config = new RedisConfig();
3536

3637
ReadConfig(config);
37-
38+
39+
BuildRedisEventsManager();
40+
BuildStatusAndTaskManager();
3841
BuildFallbackCalculator(_config.FallbackTreatments);
3942
BuildRedisCache();
4043
BuildTreatmentLog(config.ImpressionListener);
@@ -56,6 +59,12 @@ public RedisClient(ConfigurationOptions config, string apiKey) : base(apiKey, co
5659
}
5760

5861
#region Private Methods
62+
private void BuildRedisEventsManager()
63+
{
64+
_eventsManager = null;
65+
_internalEventsTask = new NoOpInternalEventsTask();
66+
}
67+
5968
private void ReadConfig(ConfigurationOptions config)
6069
{
6170
var baseConfig = _configService.ReadConfig(config, ConfigTypes.Redis);

src/Splitio/Services/Cache/Classes/InMemorySplitCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void Update(List<ParsedSplit> toAdd, List<string> toRemove, long till)
7878
}
7979

8080
SetChangeNumber(till);
81-
if (eventsFlags.Any())
81+
if (eventsFlags.Count > 0)
8282
{
8383
Task task = new Task(() =>
8484
{

src/Splitio/Services/Client/Classes/JSONFileClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public JSONFileClient(string splitsFilePath,
3232
ITrafficTypeValidator trafficTypeValidator = null,
3333
IImpressionsManager impressionsManager = null,
3434
IRuleBasedSegmentCache ruleBasedSegmentCache = null
35-
) : base("localhost", config.Mode)
35+
) : base("localhost")
3636
{
3737
_segmentCache = segmentCacheInstance ?? new InMemorySegmentCache(new ConcurrentDictionary<string, Segment>(), _internalEventsTask);
3838
var rbsCache = ruleBasedSegmentCache ?? new InMemoryRuleBasedSegmentCache(new ConcurrentDictionary<string, RuleBasedSegment>(), _internalEventsTask);
@@ -52,6 +52,8 @@ public JSONFileClient(string splitsFilePath,
5252
parsedSplits.TryAdd(split.name, _splitParser.Parse(split, rbsCache));
5353
}
5454

55+
BuildEventsManager();
56+
BuildStatusAndTaskManager();
5557
BuildFallbackCalculator(config.FallbackTreatments);
5658
BuildFlagSetsFilter(new HashSet<string>());
5759
_featureFlagCache = featureFlagCacheInstance ?? new InMemorySplitCache(new ConcurrentDictionary<string, ParsedSplit>(parsedSplits), _flagSetsFilter, _internalEventsTask);

src/Splitio/Services/Client/Classes/LocalhostClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ public class LocalhostClient : SplitClient
2727

2828
private readonly object _lock = new object();
2929

30-
public LocalhostClient(ConfigurationOptions configurationOptions) : base("localhost", configurationOptions.Mode)
30+
public LocalhostClient(ConfigurationOptions configurationOptions) : base("localhost")
3131
{
32+
BuildEventsManager();
33+
BuildStatusAndTaskManager();
3234
var configs = (LocalhostClientConfigurations)_configService.ReadConfig(configurationOptions, ConfigTypes.Localhost, _statusManager);
3335

3436
_fullPath = LookupFilePath(configs.FilePath);

src/Splitio/Services/Client/Classes/SelfRefreshingClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ public class SelfRefreshingClient : SplitClient
5252
private IRuleBasedSegmentCache _ruleBasedSegmentCache;
5353
private IUpdater<RuleBasedSegmentDto> _ruleBasedSegmentUpdater;
5454

55-
public SelfRefreshingClient(string apiKey, ConfigurationOptions config) : base(apiKey, config.Mode)
55+
public SelfRefreshingClient(string apiKey, ConfigurationOptions config) : base(apiKey)
5656
{
5757
_config = (SelfRefreshingConfig)_configService.ReadConfig(config, ConfigTypes.InMemory);
5858

59+
BuildEventsManager();
60+
BuildStatusAndTaskManager();
5961
BuildFallbackCalculator(_config.FallbackTreatments);
6062
BuildFlagSetsFilter(_config.FlagSetsFilter);
6163
BuildSplitCache();

src/Splitio/Services/Client/Classes/SplitClient.cs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public abstract class SplitClient : ISplitClient
6666
protected IEventsManager<SdkEvent, SdkInternalEvent, EventMetadata> _eventsManager;
6767
protected IInternalEventsTask _internalEventsTask;
6868
private EventHandler<EventMetadata> SdkReadyEvent;
69-
private Mode _configMode;
7069

7170
public event EventHandler<EventMetadata> SdkReady
7271
{
@@ -86,24 +85,9 @@ public event EventHandler<EventMetadata> SdkReady
8685
}
8786
public event EventHandler<EventMetadata> SdkUpdate;
8887

89-
protected SplitClient(string apikey, Mode configMode)
88+
protected SplitClient(string apikey)
9089
{
9190
ApiKey = apikey;
92-
_configMode = configMode;
93-
94-
if (configMode == Mode.Standalone)
95-
{
96-
_eventsManager = new EventsManager<SdkEvent, SdkInternalEvent, EventMetadata>(new EventsManagerConfig(), new EventDelivery<SdkEvent, EventMetadata>());
97-
_internalEventsTask = new InternalEventsTask(_eventsManager, new SplitQueue<EventSource.Workers.SdkEventNotification>());
98-
_internalEventsTask.Start();
99-
RegisterEvents();
100-
}
101-
else
102-
{
103-
_eventsManager = null;
104-
_internalEventsTask = new NoOpInternalEventsTask();
105-
}
106-
10791
_wrapperAdapter = WrapperAdapter.Instance();
10892
_keyValidator = new KeyValidator();
10993
_splitNameValidator = new SplitNameValidator();
@@ -112,8 +96,6 @@ protected SplitClient(string apikey, Mode configMode)
11296
_factoryInstantiationsService = FactoryInstantiationsService.Instance();
11397
_flagSetsValidator = new FlagSetsValidator();
11498
_configService = new ConfigService(_wrapperAdapter, _flagSetsValidator, new SdkMetadataValidator());
115-
_statusManager = new InMemoryReadinessGatesCache(_internalEventsTask);
116-
_tasksManager = new TasksManager(_statusManager);
11799
}
118100

119101
#region GetTreatment
@@ -330,12 +312,14 @@ public virtual async Task DestroyAsync()
330312
if (_statusManager.IsDestroyed()) return;
331313

332314
_log.Info(Messages.InitDestroy);
333-
_internalEventsTask.Stop();
334315
_factoryInstantiationsService.Decrease(ApiKey);
335316
_statusManager.SetDestroy();
336317
await _syncManager.ShutdownAsync();
337-
UnregisterEvents();
338-
318+
if (_eventsManager != null)
319+
{
320+
_internalEventsTask.Stop();
321+
UnregisterEvents();
322+
}
339323
_log.Info(Messages.Destroyed);
340324
}
341325

@@ -345,12 +329,14 @@ public virtual void Destroy()
345329

346330
_log.Info(Messages.InitDestroy);
347331

348-
_internalEventsTask.Stop();
349332
_factoryInstantiationsService.Decrease(ApiKey);
350333
_statusManager.SetDestroy();
351334
_syncManager.Shutdown();
352-
UnregisterEvents();
353-
335+
if (_eventsManager != null)
336+
{
337+
_internalEventsTask.Stop();
338+
UnregisterEvents();
339+
}
354340
_log.Info(Messages.Destroyed);
355341
}
356342
#endregion
@@ -427,6 +413,12 @@ public ISplitManager GetSplitManager()
427413
#endregion
428414

429415
#region Protected Methods
416+
protected void BuildStatusAndTaskManager()
417+
{
418+
_statusManager = new InMemoryReadinessGatesCache(_internalEventsTask);
419+
_tasksManager = new TasksManager(_statusManager);
420+
}
421+
430422
protected void BuildUniqueKeysTracker(BaseConfig config)
431423
{
432424
var bloomFilter = new BloomFilter(config.BfExpectedElements, config.BfErrorRate);
@@ -464,6 +456,14 @@ protected void BuildFallbackCalculator(FallbackTreatmentsConfiguration fallbackT
464456
{
465457
_fallbackTreatmentCalculator = new FallbackTreatmentCalculator(fallbackTreatmentsConfiguration);
466458
}
459+
460+
protected void BuildEventsManager()
461+
{
462+
_eventsManager = new EventsManager<SdkEvent, SdkInternalEvent, EventMetadata>(new EventsManagerConfig(), new EventDelivery<SdkEvent, EventMetadata>());
463+
_internalEventsTask = new InternalEventsTask(_eventsManager, new SplitQueue<EventSource.Workers.SdkEventNotification>());
464+
_internalEventsTask.Start();
465+
RegisterEvents();
466+
}
467467
#endregion
468468

469469
#region Private Async Methods
@@ -532,11 +532,8 @@ private void RegisterEvents()
532532
}
533533
private void UnregisterEvents()
534534
{
535-
if (_configMode == Mode.Standalone)
536-
{
537-
_eventsManager.Unregister(SdkEvent.SdkReady);
538-
_eventsManager.Unregister(SdkEvent.SdkUpdate);
539-
}
535+
_eventsManager.Unregister(SdkEvent.SdkReady);
536+
_eventsManager.Unregister(SdkEvent.SdkUpdate);
540537
}
541538
private List<TreatmentResult> GetTreatmentsSync(Enums.API method, Key key, List<string> features, Dictionary<string, object> attributes = null, EvaluationOptions evaluationOptions = null)
542539
{

src/Splitio/Services/Tasks/NoOpInternalEventsTask.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ namespace Splitio.Services.Tasks
55
{
66
public class NoOpInternalEventsTask : IInternalEventsTask
77
{
8-
public NoOpInternalEventsTask()
9-
{
10-
return;
11-
}
8+
public NoOpInternalEventsTask() {}
129
public async Task AddToQueue(SdkInternalEvent sdkInternalEvent, EventMetadata eventMetadata)
13-
{ return; }
10+
{ await DoNothing(); }
1411

15-
public void Start() { return; }
12+
public void Start() { }
1613

17-
public void Stop() { return; }
14+
public void Stop() { }
15+
16+
public static Task<bool> DoNothing()
17+
{ return Task.FromResult(true); }
1818
}
1919
}

tests/Splitio-tests/Unit Tests/Client/SplitClientForTesting.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ public SplitClientForTesting(IFeatureFlagCacheConsumer featureFlagCacheConsumer,
2222
ISyncManager syncManager,
2323
ITelemetryEvaluationProducer telemetryEvaluationProducer,
2424
IFallbackTreatmentCalculator fallbackTreatmentCalculator)
25-
: base("SplitClientForTesting", Mode.Standalone)
25+
: base("SplitClientForTesting")
2626
{
27+
BuildEventsManager();
28+
BuildStatusAndTaskManager();
2729
_fallbackTreatmentCalculator = fallbackTreatmentCalculator;
2830
_eventsLog = eventsLog;
2931
_impressionsLog = impressionsLog;
@@ -33,7 +35,6 @@ public SplitClientForTesting(IFeatureFlagCacheConsumer featureFlagCacheConsumer,
3335
_impressionsManager = impressionsManager;
3436
_syncManager = syncManager;
3537
_telemetryEvaluationProducer = telemetryEvaluationProducer;
36-
3738
BuildClientExtension();
3839
}
3940
}

tests/Splitio.TestSupport/SplitClientForTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ public class SplitClientForTest : SplitClient
88
{
99
private readonly Dictionary<string, string> _tests;
1010

11-
public SplitClientForTest(ConfigurationOptions config) : base("SplitClientForTest", config.Mode)
11+
public SplitClientForTest(ConfigurationOptions config) : base("SplitClientForTest")
1212
{
13+
BuildEventsManager();
1314
BuildFallbackCalculator(config.FallbackTreatments);
1415
_tests = new Dictionary<string, string>();
1516
}

tests/Splitio.Tests.Common/BaseAsyncClientTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ public async Task TrackAsync_WithValidData_ReturnsTrue()
984984
new EventBackend { Key = "key_4", TrafficTypeName = "traffic_type_4", EventTypeId = "event_type_4", Value = 444, Properties = properties }
985985
};
986986

987-
var apikey = "base-apikey17";
987+
var apikey = "base-apikey20";
988988

989989
var splitFactory = new SplitFactory(apikey, configurations);
990990
var client = splitFactory.Client();
@@ -1001,6 +1001,7 @@ public async Task TrackAsync_WithValidData_ReturnsTrue()
10011001
}
10021002

10031003
//Validate Events sent to the be.
1004+
await Task.Delay(1000);
10041005
AssertSentEvents(events);
10051006
await client.DestroyAsync();
10061007
}
@@ -1042,6 +1043,7 @@ public async Task TrackAsync_WithBUR_ReturnsTrue()
10421043
}
10431044

10441045
//Validate Events sent to the be.
1046+
await Task.Delay(1000);
10451047
AssertSentEvents(events);
10461048
await client.DestroyAsync();
10471049
}
@@ -1091,6 +1093,7 @@ public async Task TrackAsync_WithInvalidData_ReturnsFalse()
10911093
.ToList();
10921094

10931095
//Validate Events sent to the be.
1096+
await Task.Delay(1000);
10941097
AssertSentEvents(events);
10951098
await client.DestroyAsync();
10961099
}

0 commit comments

Comments
 (0)