Skip to content
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

Support remote config from extraction pipelines [DEGR-2723] #63

Merged
merged 30 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 21 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
18 changes: 14 additions & 4 deletions Cognite.Simulator.Tests/UtilsTests/ConnectorBaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cognite.Extractor.Common;
using Cognite.Extractor.Logging;
using Cognite.Extractor.Utils;
using Cognite.Simulator.Extensions;
using Cognite.Simulator.Utils;
Expand All @@ -23,6 +24,9 @@ public async Task TestConnectorBase()
{
var services = new ServiceCollection();
services.AddCogniteTestClient();
services.AddLogger();
services.AddSingleton<RemoteConfigManager<BaseConfig>>(provider => null!);
services.AddSingleton<BaseConfig>();
services.AddTransient<TestConnector>();
services.AddSingleton<ExtractionPipeline>();
var simConfig = new SimulatorConfig
Expand Down Expand Up @@ -110,20 +114,24 @@ await cdf.ExtPipes
}
}


/// <summary>
/// Implements a simple mock connector that only reports
/// status back to CDF (Heartbeat)
/// </summary>
internal class TestConnector : ConnectorBase
internal class TestConnector : ConnectorBase<BaseConfig>
{
private readonly ExtractionPipeline _pipeline;
private readonly RemoteConfigManager<BaseConfig> _remoteConfigManager;
private readonly SimulatorConfig _config;



public TestConnector(
CogniteDestination cdf,
ExtractionPipeline pipeline,
SimulatorConfig config,
ILogger<ConnectorBase> logger) :
ILogger<TestConnector> logger,
RemoteConfigManager<BaseConfig> remoteConfigManager) :
base(
cdf,
new ConnectorConfig
Expand All @@ -135,10 +143,12 @@ public TestConnector(
{
config
},
logger)
logger,
remoteConfigManager)
{
_pipeline = pipeline;
_config = config;
_remoteConfigManager = remoteConfigManager;
}

public override string GetConnectorVersion()
Expand Down
35 changes: 32 additions & 3 deletions Cognite.Simulator.Utils/ConnectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Cognite.Extractor.Configuration;

namespace Cognite.Simulator.Utils
{
Expand All @@ -16,7 +17,7 @@
/// The connector information is saved as a CDF sequence, where the rows
/// are key/value pairs (see <seealso cref="SimulatorIntegrationSequenceRows"/>)
/// </summary>
public abstract class ConnectorBase
public abstract class ConnectorBase<T> where T : BaseConfig
{
/// <summary>
/// CDF client wrapper
Expand All @@ -30,12 +31,14 @@
private ConnectorConfig Config { get; }

private readonly Dictionary<string, string> _simulatorSequenceIds;
private readonly ILogger<ConnectorBase> _logger;
private readonly ILogger<ConnectorBase<T>> _logger;
private readonly ConnectorConfig _config;

private long LastLicenseCheckTimestamp { get; set; }
private string LastLicenseCheckResult { get; set; }

private readonly RemoteConfigManager<T> _remoteconfigmanager;
kbrattli marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Initialize the connector with the given parameters
/// </summary>
Expand All @@ -47,14 +50,16 @@
CogniteDestination cdf,
ConnectorConfig config,
IList<SimulatorConfig> simulators,
ILogger<ConnectorBase> logger)
ILogger<ConnectorBase<T>> logger,
RemoteConfigManager<T> remoteConfigManager)

Check warning on line 54 in Cognite.Simulator.Utils/ConnectorBase.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'remoteConfigManager' has no matching param tag in the XML comment for 'ConnectorBase<T>.ConnectorBase(CogniteDestination, ConnectorConfig, IList<SimulatorConfig>, ILogger<ConnectorBase<T>>, RemoteConfigManager<T>)' (but other parameters do)
{
Cdf = cdf;
Simulators = simulators;
Config = config;
_simulatorSequenceIds = new Dictionary<string, string>();
_logger = logger;
_config = config;
_remoteconfigmanager = remoteConfigManager;
}

/// <summary>
Expand Down Expand Up @@ -284,6 +289,30 @@
.ConfigureAwait(false);
}
}

/// <summary>
/// Task that runs in a loop, checking for new config in extraction pipelines
/// </summary>
public async Task CheckForNewConfig(CancellationToken token)
kbrattli marked this conversation as resolved.
Show resolved Hide resolved
{
while (!token.IsCancellationRequested)
{
await Task
.Delay(900000, token) // Run every 15 minutes
kbrattli marked this conversation as resolved.
Show resolved Hide resolved
kbrattli marked this conversation as resolved.
Show resolved Hide resolved
.ConfigureAwait(false);
_logger.LogDebug("Checking remote config");
kbrattli marked this conversation as resolved.
Show resolved Hide resolved
if (_remoteconfigmanager == null) return;
var newConfig = await _remoteconfigmanager.FetchLatest(token).ConfigureAwait(false);
if (newConfig != null)
{
throw new NewConfigDetected();
}
}
}
}

public class NewConfigDetected : Exception

Check warning on line 314 in Cognite.Simulator.Utils/ConnectorBase.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'NewConfigDetected'
{
}

/// <summary>
Expand Down
44 changes: 44 additions & 0 deletions Cognite.Simulator.Utils/ExtractionPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Cognite.Extractor.Configuration;
using System.IO;

namespace Cognite.Simulator.Utils
{
Expand Down Expand Up @@ -237,5 +239,47 @@
services.AddSingleton(config.PipelineNotification);
services.AddScoped<ExtractionPipeline>();
}
/// <summary>
/// Uses a minimum config that needs "cognite" and type: "specified",
/// then uses that to either fetch remote Config Extraction Pipeline, or just adds
/// the local one based on if "type" is remote or local.
kbrattli marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <typeparam name="T">The complete config object to be parsed</typeparam>
/// <param name="services">Service collection</param>
/// <param name="path">Path to config file</param>
/// <param name="types">Types to use for deserialization</param>
/// <param name="appId">App ID for measuring network data</param>
/// <param name="token">Cancellation token</param>
/// <param name="version">Config version</param>
/// <param name="acceptedConfigVersions">Accepted config versions</param>
public static async Task<T> AddConfiguration<T>(
this IServiceCollection services,
string path,
Type[] types,
string appId,
CancellationToken token,
int version = 1,
int[] acceptedConfigVersions = null) where T : BaseConfig
{
var localConfig = services.AddConfig<T>(path, version);
var remoteConfig = new RemoteConfig
{
Type = localConfig.Type,
Cognite = localConfig.Cognite
};

return await services.AddRemoteConfig<T>(

Check warning on line 271 in Cognite.Simulator.Utils/ExtractionPipeline.cs

View workflow job for this annotation

GitHub Actions / build

Consider calling ConfigureAwait on the awaited task (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007)
logger: null,
kbrattli marked this conversation as resolved.
Show resolved Hide resolved
path: path,
types: types,
appId: appId,
userAgent: null,
setDestination: true,
bufferConfigFile: false,
remoteConfig: remoteConfig,
token: token,
acceptedConfigVersions: acceptedConfigVersions
);
}
}
}
Loading