-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add environment ID support for hooks. #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Threading.Tasks; | ||
|
@@ -22,7 +23,7 @@ namespace LaunchDarkly.Sdk.Server.Internal.DataSources | |
/// This component is also responsible for receiving updates to the data source status, broadcasting | ||
/// them to any status listeners, and tracking the length of any period of sustained failure. | ||
/// </remarks> | ||
internal sealed class DataSourceUpdatesImpl : IDataSourceUpdates | ||
internal sealed class DataSourceUpdatesImpl : IDataSourceUpdates, IDataSourceUpdatesHeaders | ||
{ | ||
#region Private fields | ||
|
||
|
@@ -84,7 +85,7 @@ internal DataSourceUpdatesImpl( | |
StateSince = DateTime.Now, | ||
LastError = null | ||
}; | ||
_status = new StateMonitor<DataSourceStatus, StateAndError>(initialStatus, MaybeUpdateStatus, _log); | ||
_status = new StateMonitor<DataSourceStatus, StateAndError>(initialStatus, MaybeUpdateStatus, _log); | ||
} | ||
|
||
#endregion | ||
|
@@ -93,47 +94,7 @@ internal DataSourceUpdatesImpl( | |
|
||
public bool Init(FullDataSet<ItemDescriptor> allData) | ||
{ | ||
ImmutableDictionary<DataKind, ImmutableDictionary<string, ItemDescriptor>> oldData = null; | ||
|
||
try | ||
{ | ||
if (HasFlagChangeListeners()) | ||
{ | ||
// Query the existing data if any, so that after the update we can send events for | ||
// whatever was changed | ||
var oldDataBuilder = ImmutableDictionary.CreateBuilder<DataKind, | ||
ImmutableDictionary<string, ItemDescriptor>>(); | ||
foreach (var kind in DataModel.AllDataKinds) | ||
{ | ||
var items = _store.GetAll(kind); | ||
oldDataBuilder.Add(kind, items.Items.ToImmutableDictionary()); | ||
} | ||
oldData = oldDataBuilder.ToImmutable(); | ||
} | ||
_store.Init(DataStoreSorter.SortAllCollections(allData)); | ||
_lastStoreUpdateFailed = false; | ||
} | ||
catch (Exception e) | ||
{ | ||
ReportStoreFailure(e); | ||
return false; | ||
} | ||
|
||
// Calling Init implies that the data source is now in a valid state. | ||
UpdateStatus(DataSourceState.Valid, null); | ||
|
||
// We must always update the dependency graph even if we don't currently have any event listeners, because if | ||
// listeners are added later, we don't want to have to reread the whole data store to compute the graph | ||
UpdateDependencyTrackerFromFullDataSet(allData); | ||
|
||
// Now, if we previously queried the old data because someone is listening for flag change events, compare | ||
// the versions of all items and generate events for those (and any other items that depend on them) | ||
if (oldData != null) | ||
{ | ||
SendChangeEvents(ComputeChangedItemsForFullDataSet(oldData, FullDataSetToMap(allData))); | ||
} | ||
|
||
return true; | ||
return InitWithHeaders(allData, null); | ||
} | ||
|
||
public bool Upsert(DataKind kind, string key, ItemDescriptor item) | ||
|
@@ -326,5 +287,67 @@ private void ReportStoreFailure(Exception e) | |
} | ||
|
||
#endregion | ||
|
||
#region IDataSourceUpdatesHeaders methods | ||
public bool InitWithHeaders(FullDataSet<ItemDescriptor> allData, IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers) | ||
{ | ||
ImmutableDictionary<DataKind, ImmutableDictionary<string, ItemDescriptor>> oldData = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of the method body is from the original init. |
||
|
||
try | ||
{ | ||
if (HasFlagChangeListeners()) | ||
{ | ||
// Query the existing data if any, so that after the update we can send events for | ||
// whatever was changed | ||
var oldDataBuilder = ImmutableDictionary.CreateBuilder<DataKind, | ||
ImmutableDictionary<string, ItemDescriptor>>(); | ||
foreach (var kind in DataModel.AllDataKinds) | ||
{ | ||
var items = _store.GetAll(kind); | ||
oldDataBuilder.Add(kind, items.Items.ToImmutableDictionary()); | ||
} | ||
oldData = oldDataBuilder.ToImmutable(); | ||
} | ||
|
||
var sortedCollections = DataStoreSorter.SortAllCollections(allData); | ||
|
||
if (_store is IDataStoreMetadata storeMetadata) | ||
{ | ||
var environmentId = headers?.FirstOrDefault((item) => | ||
item.Key.ToLower() == HeaderConstants.EnvironmentId).Value | ||
?.FirstOrDefault(); | ||
storeMetadata.InitWithMetadata(sortedCollections, new InitMetadata(environmentId)); | ||
} | ||
else | ||
{ | ||
_store.Init(sortedCollections); | ||
} | ||
|
||
_lastStoreUpdateFailed = false; | ||
} | ||
catch (Exception e) | ||
{ | ||
ReportStoreFailure(e); | ||
return false; | ||
} | ||
|
||
// Calling Init implies that the data source is now in a valid state. | ||
UpdateStatus(DataSourceState.Valid, null); | ||
|
||
// We must always update the dependency graph even if we don't currently have any event listeners, because if | ||
// listeners are added later, we don't want to have to reread the whole data store to compute the graph | ||
UpdateDependencyTrackerFromFullDataSet(allData); | ||
|
||
// Now, if we previously queried the old data because someone is listening for flag change events, compare | ||
// the versions of all items and generate events for those (and any other items that depend on them) | ||
if (oldData != null) | ||
{ | ||
SendChangeEvents(ComputeChangedItemsForFullDataSet(oldData, FullDataSetToMap(allData))); | ||
} | ||
|
||
return true; | ||
} | ||
#endregion | ||
< 6D40 td id="diff-de0e152c1004e60fe2d836ce5a1c9283458d0e3cde3d5ba34f8c529b9c26b3ebR351" data-line-number="351" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum"> |
|
|
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 15 additions & 1 deletion
16
pkgs/sdk/server/src/Internal/DataSources/IFeatureRequestor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
|
||
using static LaunchDarkly.Sdk.Server.Subsystems.DataStoreTypes; | ||
|
||
namespace LaunchDarkly.Sdk.Server.Internal.DataSources | ||
{ | ||
internal class DataSetWithHeaders | ||
{ | ||
public readonly FullDataSet<ItemDescriptor>? DataSet; | ||
public readonly IEnumerable<KeyValuePair<string, IEnumerable<string>>> Headers; | ||
|
||
public DataSetWithHeaders(FullDataSet<ItemDescriptor>? dataSet, | ||
IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers) | ||
{ | ||
DataSet = dataSet; | ||
Headers = headers; | ||
} | ||
} | ||
|
||
internal interface IFeatureRequestor : IDisposable | ||
{ | ||
Task<FullDataSet<ItemDescriptor>?> GetAllDataAsync(); | ||
Task<DataSetWithHeaders> GetAllDataAsync(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a whitespace change.