Skip to content
Merged
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
17 changes: 5 additions & 12 deletions src/TriggerBinding/SqlTriggerListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,17 @@ public SqlTriggerListener(string connectionString, string tableName, string user
this._executor = executor ?? throw new ArgumentNullException(nameof(executor));
this._logger = logger ?? throw new ArgumentNullException(nameof(logger));
this._configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
int configuredMaxChangesPerWorker;
int? configuredMaxChangesPerWorker;
// Do not convert the scale-monitor ID to lower-case string since SQL table names can be case-sensitive
// depending on the collation of the current database.
this._scaleMonitorDescriptor = new ScaleMonitorDescriptor($"{userFunctionId}-SqlTrigger-{tableName}");

// In case converting from string to int is not possible from the user input.
try
configuredMaxChangesPerWorker = configuration.GetValue<int?>(ConfigKey_SqlTrigger_MaxChangesPerWorker);
this._maxChangesPerWorker = configuredMaxChangesPerWorker ?? DefaultMaxChangesPerWorker;
if (this._maxChangesPerWorker <= 0)
{
configuredMaxChangesPerWorker = configuration.GetValue<int>(ConfigKey_SqlTrigger_MaxChangesPerWorker);
}
catch (Exception ex)
{
this._logger.LogError($"Failed to resolve integer value from user configured setting '{ConfigKey_SqlTrigger_MaxChangesPerWorker}' due to exception: {ex.GetType()}. Exception message: {ex.Message}");
TelemetryInstance.TrackException(TelemetryErrorName.InvalidConfigurationValue, ex, this._telemetryProps);

configuredMaxChangesPerWorker = DefaultMaxChangesPerWorker;
throw new InvalidOperationException($"Invalid value for configuration setting '{ConfigKey_SqlTrigger_MaxChangesPerWorker}'. Ensure that the value is a positive integer.");
}
this._maxChangesPerWorker = configuredMaxChangesPerWorker > 0 ? configuredMaxChangesPerWorker : DefaultMaxChangesPerWorker;
}

public void Cancel()
Expand Down
16 changes: 4 additions & 12 deletions test/Unit/TriggerBinding/SqlTriggerListenerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,12 @@ public void ScaleMonitorGetScaleStatus_UserConfiguredMaxChangesPerWorker_Respect
[InlineData("-1")]
[InlineData("0")]
[InlineData("10000000000")]
public void ScaleMonitorGetScaleStatus_InvalidUserConfiguredMaxChangesPerWorker_UsesDefaultValue(string maxChangesPerWorker)
public void InvalidUserConfiguredMaxChangesPerWorker(string maxChangesPerWorker)
{
(IScaleMonitor<SqlTriggerMetrics> monitor, _) = GetScaleMonitor(maxChangesPerWorker);

ScaleStatusContext context;
ScaleStatus scaleStatus;

context = GetScaleStatusContext(new int[] { 0, 0, 0, 0, 10000 }, 10);
scaleStatus = monitor.GetScaleStatus(context);
Assert.Equal(ScaleVote.None, scaleStatus.Vote);
(Mock<ILogger> mockLogger, List<string> logMessages) = CreateMockLogger();
Mock<IConfiguration> mockConfiguration = CreateMockConfiguration(maxChangesPerWorker);

context = GetScaleStatusContext(new int[] { 0, 0, 0, 0, 10001 }, 10);
scaleStatus = monitor.GetScaleStatus(context);
Assert.Equal(ScaleVote.ScaleOut, scaleStatus.Vote);
Assert.Throws<InvalidOperationException>(() => new SqlTriggerListener<object>("testConnectionString", "testTableName", "testUserFunctionId", Mock.Of<ITriggeredFunctionExecutor>(), mockLogger.Object, mockConfiguration.Object));
}

private static IScaleMonitor<SqlTriggerMetrics> GetScaleMonitor(string tableName, string userFunctionId)
Expand Down