Skip to content

Commit 6d030f2

Browse files
committed
Collapse PostponeFunction SQL branches via parameter
Replace the if/failIfInterrupted/else branching with a single cached SQL per shape (with/without effects), gated by a runtime parameter: WHERE ... AND (@FailIfInterrupted = 0 OR Interrupted = 0) The SET clause unconditionally clears interrupted, which is a no-op when WHERE already requires it false and the intended consumption otherwise. Halves the SQL caches in PG, MariaDB, and SqlServer.
1 parent d6a5fd0 commit 6d030f2

3 files changed

Lines changed: 63 additions & 151 deletions

File tree

Stores/MariaDB/Cleipnir.ResilientFunctions.MariaDB/SqlGenerator.cs

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,7 @@ public StoreCommand SucceedFunction(
267267
}
268268

269269
private string? _postponedFunctionSql;
270-
private string? _postponedFunctionFailIfInterruptedSql;
271270
private string? _postponedFunctionWithEffectsSql;
272-
private string? _postponedFunctionWithEffectsFailIfInterruptedSql;
273271
public StoreCommand PostponeFunction(
274272
StoredId storedId,
275273
long postponeUntil,
@@ -280,88 +278,53 @@ public StoreCommand PostponeFunction(
280278
{
281279
if (effects == null)
282280
{
283-
string sql;
284-
if (failIfInterrupted)
285-
{
286-
_postponedFunctionFailIfInterruptedSql ??= $@"
287-
UPDATE {tablePrefix}
288-
SET status = {(int)Status.Postponed},
289-
expires = ?,
290-
timestamp = ?,
291-
owner = NULL
292-
WHERE
293-
id = ? AND
294-
owner = ? AND
295-
interrupted = 0";
296-
sql = _postponedFunctionFailIfInterruptedSql;
297-
}
298-
else
299-
{
300-
_postponedFunctionSql ??= $@"
301-
UPDATE {tablePrefix}
302-
SET status = {(int)Status.Postponed},
303-
expires = ?,
304-
timestamp = ?,
305-
owner = NULL,
306-
interrupted = 0
307-
WHERE
308-
id = ? AND
309-
owner = ?";
310-
sql = _postponedFunctionSql;
311-
}
281+
_postponedFunctionSql ??= $@"
282+
UPDATE {tablePrefix}
283+
SET status = {(int)Status.Postponed},
284+
expires = ?,
285+
timestamp = ?,
286+
owner = NULL,
287+
interrupted = 0
288+
WHERE
289+
id = ? AND
290+
owner = ? AND
291+
(? = 0 OR interrupted = 0)";
312292

313293
return StoreCommand.Create(
314-
sql,
294+
_postponedFunctionSql,
315295
values: [
316296
postponeUntil,
317297
timestamp,
318298
storedId.AsGuid.ToString("N"),
319299
expectedReplica.AsGuid.ToString("N"),
300+
failIfInterrupted ? 1 : 0,
320301
]
321302
);
322303
}
323304
else
324305
{
325-
string sql;
326-
if (failIfInterrupted)
327-
{
328-
_postponedFunctionWithEffectsFailIfInterruptedSql ??= $@"
329-
UPDATE {tablePrefix}
330-
SET status = {(int)Status.Postponed},
331-
expires = ?,
332-
timestamp = ?,
333-
owner = NULL,
334-
effects = ?
335-
WHERE
336-
id = ? AND
337-
owner = ? AND
338-
interrupted = 0";
339-
sql = _postponedFunctionWithEffectsFailIfInterruptedSql;
340-
}
341-
else
342-
{
343-
_postponedFunctionWithEffectsSql ??= $@"
344-
UPDATE {tablePrefix}
345-
SET status = {(int)Status.Postponed},
346-
expires = ?,
347-
timestamp = ?,
348-
owner = NULL,
349-
interrupted = 0,
350-
effects = ?
351-
WHERE
352-
id = ? AND
353-
owner = ?";
354-
sql = _postponedFunctionWithEffectsSql;
355-
}
306+
_postponedFunctionWithEffectsSql ??= $@"
307+
UPDATE {tablePrefix}
308+
SET status = {(int)Status.Postponed},
309+
expires = ?,
310+
timestamp = ?,
311+
owner = NULL,
312+
interrupted = 0,
313+
effects = ?
314+
WHERE
315+
id = ? AND
316+
owner = ? AND
317+
(? = 0 OR interrupted = 0)";
356318

357319
return StoreCommand.Create(
358-
sql,
320+
_postponedFunctionWithEffectsSql,
359321
values: [
360322
postponeUntil,
361323
timestamp,
362324
effects,
363325
storedId.AsGuid.ToString("N"),
364326
expectedReplica.AsGuid.ToString("N"),
327+
failIfInterrupted ? 1 : 0,
365328
]
366329
);
367330
}

Stores/PostgreSQL/Cleipnir.ResilientFunctions.PostgreSQL/SqlGenerator.cs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -260,51 +260,33 @@ public IEnumerable<StoreCommand> SucceedFunction(
260260
}
261261

262262
private string? _postponeFunctionSql;
263-
private string? _postponeFunctionFailIfInterruptedSql;
264263
public StoreCommand PostponeFunction(
265264
StoredId storedId,
266265
long postponeUntil,
267266
long timestamp,
268267
ReplicaId expectedReplica,
269268
bool failIfInterrupted)
270269
{
271-
string sql;
272-
if (failIfInterrupted)
273-
{
274-
_postponeFunctionFailIfInterruptedSql ??= $@"
275-
UPDATE {tablePrefix}
276-
SET status = {(int) Status.Postponed},
277-
expires = $1,
278-
owner = NULL,
279-
timestamp = $4
280-
WHERE
281-
id = $2 AND
282-
owner = $3 AND
283-
interrupted = FALSE;";
284-
sql = _postponeFunctionFailIfInterruptedSql;
285-
}
286-
else
287-
{
288-
_postponeFunctionSql ??= $@"
289-
UPDATE {tablePrefix}
290-
SET status = {(int) Status.Postponed},
291-
expires = $1,
292-
owner = NULL,
293-
interrupted = FALSE,
294-
timestamp = $4
295-
WHERE
296-
id = $2 AND
297-
owner = $3;";
298-
sql = _postponeFunctionSql;
299-
}
270+
_postponeFunctionSql ??= $@"
271+
UPDATE {tablePrefix}
272+
SET status = {(int) Status.Postponed},
273+
expires = $1,
274+
owner = NULL,
275+
interrupted = FALSE,
276+
timestamp = $4
277+
WHERE
278+
id = $2 AND
279+
owner = $3 AND
280+
(NOT $5 OR interrupted = FALSE);";
300281

301282
return StoreCommand.Create(
302-
sql,
283+
_postponeFunctionSql,
303284
values: [
304285
postponeUntil,
305286
storedId.AsGuid,
306287
expectedReplica.AsGuid,
307288
timestamp,
289+
failIfInterrupted,
308290
]
309291
);
310292
}

Stores/SqlServer/Cleipnir.ResilientFunctions.SqlServer/SqlGenerator.cs

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -268,89 +268,56 @@ public StoreCommand SucceedFunction(StoredId storedId, byte[]? result, long time
268268
}
269269

270270
private string? _postponedFunctionSql;
271-
private string? _postponedFunctionFailIfInterruptedSql;
272271
private string? _postponedFunctionWithEffectsSql;
273-
private string? _postponedFunctionWithEffectsFailIfInterruptedSql;
274272
public StoreCommand PostponeFunction(StoredId storedId, long postponeUntil, long timestamp, ReplicaId expectedReplica, string paramPrefix, bool failIfInterrupted, byte[]? effects = null)
275273
{
276274
if (effects == null)
277275
{
278-
string templateSql;
279-
if (failIfInterrupted)
280-
{
281-
_postponedFunctionFailIfInterruptedSql ??= @$"
282-
UPDATE {tablePrefix}
283-
SET Status = {(int)Status.Postponed},
284-
Expires = @PostponedUntil,
285-
Timestamp = @Timestamp,
286-
Owner = NULL
287-
WHERE Id = @Id AND Owner = @ExpectedReplica AND Interrupted = 0";
288-
templateSql = _postponedFunctionFailIfInterruptedSql;
289-
}
290-
else
291-
{
292-
_postponedFunctionSql ??= @$"
293-
UPDATE {tablePrefix}
294-
SET Status = {(int)Status.Postponed},
295-
Expires = @PostponedUntil,
296-
Timestamp = @Timestamp,
297-
Owner = NULL,
298-
Interrupted = 0
299-
WHERE Id = @Id AND Owner = @ExpectedReplica";
300-
templateSql = _postponedFunctionSql;
301-
}
276+
_postponedFunctionSql ??= @$"
277+
UPDATE {tablePrefix}
278+
SET Status = {(int)Status.Postponed},
279+
Expires = @PostponedUntil,
280+
Timestamp = @Timestamp,
281+
Owner = NULL,
282+
Interrupted = 0
283+
WHERE Id = @Id AND Owner = @ExpectedReplica AND (@FailIfInterrupted = 0 OR Interrupted = 0)";
302284

303285
var sql = paramPrefix == ""
304-
? templateSql
305-
: templateSql.Replace("@", $"@{paramPrefix}");
286+
? _postponedFunctionSql
287+
: _postponedFunctionSql.Replace("@", $"@{paramPrefix}");
306288

307289
var command = StoreCommand.Create(sql);
308290
command.AddParameter($"@{paramPrefix}PostponedUntil", postponeUntil);
309291
command.AddParameter($"@{paramPrefix}Timestamp", timestamp);
310292
command.AddParameter($"@{paramPrefix}Id", storedId.AsGuid);
311293
command.AddParameter($"@{paramPrefix}ExpectedReplica", expectedReplica.AsGuid);
294+
command.AddParameter($"@{paramPrefix}FailIfInterrupted", failIfInterrupted);
312295

313296
return command;
314297
}
315298
else
316299
{
317-
string templateSql;
318-
if (failIfInterrupted)
319-
{
320-
_postponedFunctionWithEffectsFailIfInterruptedSql ??= @$"
321-
UPDATE {tablePrefix}
322-
SET Status = {(int)Status.Postponed},
323-
Expires = @PostponedUntil,
324-
Timestamp = @Timestamp,
325-
Owner = NULL,
326-
Effects = @Effects
327-
WHERE Id = @Id AND Owner = @ExpectedReplica AND Interrupted = 0";
328-
templateSql = _postponedFunctionWithEffectsFailIfInterruptedSql;
329-
}
330-
else
331-
{
332-
_postponedFunctionWithEffectsSql ??= @$"
333-
UPDATE {tablePrefix}
334-
SET Status = {(int)Status.Postponed},
335-
Expires = @PostponedUntil,
336-
Timestamp = @Timestamp,
337-
Owner = NULL,
338-
Interrupted = 0,
339-
Effects = @Effects
340-
WHERE Id = @Id AND Owner = @ExpectedReplica";
341-
templateSql = _postponedFunctionWithEffectsSql;
342-
}
300+
_postponedFunctionWithEffectsSql ??= @$"
301+
UPDATE {tablePrefix}
302+
SET Status = {(int)Status.Postponed},
303+
Expires = @PostponedUntil,
304+
Timestamp = @Timestamp,
305+
Owner = NULL,
306+
Interrupted = 0,
307+
Effects = @Effects
308+
WHERE Id = @Id AND Owner = @ExpectedReplica AND (@FailIfInterrupted = 0 OR Interrupted = 0)";
343309

344310
var sql = paramPrefix == ""
345-
? templateSql
346-
: templateSql.Replace("@", $"@{paramPrefix}");
311+
? _postponedFunctionWithEffectsSql
312+
: _postponedFunctionWithEffectsSql.Replace("@", $"@{paramPrefix}");
347313

348314
var command = StoreCommand.Create(sql);
349315
command.AddParameter($"@{paramPrefix}PostponedUntil", postponeUntil);
350316
command.AddParameter($"@{paramPrefix}Timestamp", timestamp);
351317
command.AddParameter($"@{paramPrefix}Effects", effects);
352318
command.AddParameter($"@{paramPrefix}Id", storedId.AsGuid);
353319
command.AddParameter($"@{paramPrefix}ExpectedReplica", expectedReplica.AsGuid);
320+
command.AddParameter($"@{paramPrefix}FailIfInterrupted", failIfInterrupted);
354321

355322
return command;
356323
}

0 commit comments

Comments
 (0)