You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add additional debug logging
* Add max attempts to lease locked check
* log when match condition is null
---------
Co-authored-by: MaddyDev <[email protected]>
this._logger.LogDebug($"Executed GetChangesCommand in GetTableChangesAsync. {rows.Count} available changed rows ({leaseLockedRowCount} found with lease locks).");
326
+
this._logger.LogDebug($"Executed GetChangesCommand in GetTableChangesAsync. {rows.Count} available changed rows. {leaseLockedOrMaxAttemptRowCountMessage}");
stringleasesTableJoinCondition=string.Join(" AND ",this._primaryKeyColumns.Select(col =>$"c.{col.name.AsBracketQuotedString()} = l.{col.name.AsBracketQuotedString()}"));
853
-
intleaseLockedRowsCount=0;
854
-
longgetLockedRowCountDurationMs=0L;
855
873
// Get the count of changes from CHANGETABLE that meet the following criteria:
856
-
// * Not Null LeaseExpirationTime AND
857
-
// * LeaseExpirationTime > Current Time
858
-
stringgetLeaseLockedrowCountQuery=$@"
874
+
// Lease locked:
875
+
// * Have attempts remaining (Attempt count < Max attempts)
876
+
// * NOT NULL LeaseExpirationTime
877
+
// * LeaseExpirationTime > Current Time
878
+
// Max Attempts reached:
879
+
// * NULL LeaseExpirationTime OR LeaseExpirationTime <= Current Time
880
+
// * No attempts remaining (Attempt count = Max attempts)
881
+
stringgetLeaseLockedOrMaxAttemptRowCountQuery=$@"
859
882
{AppLockStatements}
860
883
861
884
DECLARE @last_sync_version bigint;
862
885
SELECT @last_sync_version = LastSyncVersion
863
886
FROM {GlobalStateTableName}
864
887
WHERE UserFunctionID = '{this._userFunctionId}' AND UserTableID = {this._userTableId};
865
-
866
-
SELECT COUNT(*)
888
+
DECLARE @lease_locked_count int;
889
+
DECLARE @max_attempts_count int;
890
+
SELECT
891
+
@lease_locked_count = COUNT(CASE WHEN l.{LeasesTableAttemptCountColumnName} < {MaxChangeProcessAttemptCount} AND l.{LeasesTableLeaseExpirationTimeColumnName} IS NOT NULL AND l.{LeasesTableLeaseExpirationTimeColumnName} > SYSDATETIME() THEN 1 ELSE NULL END),
892
+
@max_attempts_count = COUNT(CASE WHEN (l.{LeasesTableLeaseExpirationTimeColumnName} IS NULL OR l.{LeasesTableLeaseExpirationTimeColumnName} <= SYSDATETIME()) AND l.{LeasesTableAttemptCountColumnName} = {MaxChangeProcessAttemptCount} THEN 1 ELSE NULL END)
867
893
FROM CHANGETABLE(CHANGES {this._userTable.BracketQuotedFullName}, @last_sync_version) AS c
868
-
LEFT OUTER JOIN {this._bracketedLeasesTableName} AS l ON {leasesTableJoinCondition}
869
-
WHERE l.{LeasesTableLeaseExpirationTimeColumnName} IS NOT NULL AND l.{LeasesTableLeaseExpirationTimeColumnName} > SYSDATETIME()";
894
+
LEFT OUTER JOIN {this._bracketedLeasesTableName} AS l ON {leasesTableJoinCondition};
895
+
IF @lease_locked_count > 0 OR @max_attempts_count > 0
896
+
BEGIN
897
+
SELECT '(' + CAST(@lease_locked_count AS NVARCHAR) + ' found with lease locks and ' + CAST(@max_attempts_count AS NVARCHAR) + ' ignored because they''ve reached the max attempt limit)';
this._logger.LogError($"Failed to query count of lease locked changes for table '{this._userTable.FullName}' due to exception: {ex.GetType()}. Exception message: {ex.Message}");
this._logger.LogError($"Failed to query count of lease locked or max attempt changes for table '{this._userTable.FullName}' due to exception: {ex.GetType()}. Exception message: {ex.Message}");
// This is currently only used for debugging, so ignore the exception if we can. If the error is a fatal one though then the connection or transaction will be
884
911
// unusable so we have to let this bubble up so we can attempt to reconnect
stringmatchCondition=string.Join(" OR ",this._rowMatchConditions.Take(this._rowsToProcess.Count));
955
-
981
+
if(string.IsNullOrEmpty(matchCondition))
982
+
{
983
+
this._logger.LogError($"MatchCondition resolved to empty with '{this._rowsToProcess.Count}' rowsToProcess.");
984
+
}
956
985
stringrenewLeasesQuery=$@"
957
986
{AppLockStatements}
958
987
@@ -1043,7 +1072,12 @@ LEFT OUTER JOIN {this._bracketedLeasesTableName} AS l ON {leasesTableJoinConditi
1043
1072
SET LastSyncVersion = {newLastSyncVersion}, LastAccessTime = GETUTCDATE()
1044
1073
WHERE UserFunctionID = '{this._userFunctionId}' AND UserTableID = {this._userTableId};
1045
1074
1075
+
DECLARE @max_attempt_rows_to_be_deleted int;
1076
+
SELECT @max_attempt_rows_to_be_deleted = COUNT(*) FROM {this._bracketedLeasesTableName} WHERE {LeasesTableChangeVersionColumnName} <= {newLastSyncVersion} AND {LeasesTableAttemptCountColumnName} = {MaxChangeProcessAttemptCount};
1077
+
1046
1078
DELETE FROM {this._bracketedLeasesTableName} WHERE {LeasesTableChangeVersionColumnName} <= {newLastSyncVersion};
1079
+
1080
+
SELECT 'Updated LastSyncVersion from ' + CAST(@current_last_sync_version AS NVARCHAR) + ' to {newLastSyncVersion} MaxAttemptRowsToBeDeleted=' + CAST(@max_attempt_rows_to_be_deleted AS NVARCHAR);
0 commit comments