Skip to content

Commit 0b658c8

Browse files
authored
Skip empty where clause execution on trigger (#1110)
* return null on empty where * add telemetry point * track event BuildRenewLeasesWithEmptyMatchCondtion
1 parent aa7d21f commit 0b658c8

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/Telemetry/Telemetry.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ public enum TelemetryEventName
349349
TriggerFunction,
350350
TriggerMonitorStart,
351351
Upsert,
352-
InsertGlobalStateTableRow
352+
InsertGlobalStateTableRow,
353+
BuildRenewLeasesWithEmptyMatchCondtion
353354
}
354355

355356
/// <summary>
@@ -440,7 +441,7 @@ public enum TelemetryErrorName
440441
Upsert,
441442
UpsertRollback,
442443
GetServerTelemetryProperties,
443-
GetLeaseLockedOrMaxAttemptRowCount,
444+
GetLeaseLockedOrMaxAttemptRowCount
444445
}
445446

446447
internal class ServerProperties

src/TriggerBinding/SqlTableChangeMonitor.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -525,28 +525,30 @@ private async Task RenewLeasesAsync(SqlConnection connection, CancellationToken
525525
{
526526
try
527527
{
528-
using (SqlCommand renewLeasesCommand = this.BuildRenewLeasesCommand(connection, transaction))
528+
SqlCommand renewLeasesCommand = this.BuildRenewLeasesCommand(connection, transaction);
529+
if (renewLeasesCommand != null)
529530
{
530-
var stopwatch = Stopwatch.StartNew();
531+
using (renewLeasesCommand)
532+
{
533+
var stopwatch = Stopwatch.StartNew();
531534

532-
int rowsAffected = await renewLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token, true);
535+
int rowsAffected = await renewLeasesCommand.ExecuteNonQueryAsyncWithLogging(this._logger, token, true);
533536

534-
long durationMs = stopwatch.ElapsedMilliseconds;
537+
long durationMs = stopwatch.ElapsedMilliseconds;
535538

536-
if (rowsAffected > 0)
537-
{
538-
this._logger.LogDebug($"Renewed leases for {rowsAffected} rows");
539-
// Only send an event if we actually updated rows to reduce the overall number of events we send
540-
var measures = new Dictionary<TelemetryMeasureName, double>
539+
if (rowsAffected > 0)
541540
{
542-
[TelemetryMeasureName.DurationMs] = durationMs,
543-
};
544-
545-
TelemetryInstance.TrackEvent(TelemetryEventName.RenewLeases, this._telemetryProps, measures);
541+
this._logger.LogDebug($"Renewed leases for {rowsAffected} rows");
542+
// Only send an event if we actually updated rows to reduce the overall number of events we send
543+
var measures = new Dictionary<TelemetryMeasureName, double>
544+
{
545+
[TelemetryMeasureName.DurationMs] = durationMs,
546+
};
547+
548+
TelemetryInstance.TrackEvent(TelemetryEventName.RenewLeases, this._telemetryProps, measures);
549+
}
550+
transaction.Commit();
546551
}
547-
548-
549-
transaction.Commit();
550552
}
551553
}
552554
catch (Exception e)
@@ -978,9 +980,12 @@ WHEN NOT MATCHED THEN
978980
private SqlCommand BuildRenewLeasesCommand(SqlConnection connection, SqlTransaction transaction)
979981
{
980982
string matchCondition = string.Join(" OR ", this._rowMatchConditions.Take(this._rowsToProcess.Count));
983+
// If the matchCondition is empty return null to avoid empty where clause query failure.
981984
if (string.IsNullOrEmpty(matchCondition))
982985
{
983986
this._logger.LogError($"MatchCondition resolved to empty with '{this._rowsToProcess.Count}' rowsToProcess.");
987+
TelemetryInstance.TrackEvent(TelemetryEventName.BuildRenewLeasesWithEmptyMatchCondtion);
988+
return null;
984989
}
985990
string renewLeasesQuery = $@"
986991
{AppLockStatements}

0 commit comments

Comments
 (0)