Skip to content

Commit 9938ae5

Browse files
fixes
1 parent 1060fce commit 9938ae5

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

slo/src/AdoNet/SloTableContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ PRIMARY KEY (Guid, Id)
109109
}
110110

111111
protected override async Task<(int, StatusCode, object?)> Select(
112-
dynamic select,
112+
(Guid Guid, int Id) select,
113113
int readTimeout,
114114
Counter? errorsTotal = null
115115
)
@@ -146,10 +146,10 @@ PRIMARY KEY (Guid, Id)
146146
return (attempts, ((YdbException)policyResult.FinalException)?.Code ?? StatusCode.Success, policyResult.Result);
147147
}
148148

149-
protected override async Task<int> SelectCount(string sql)
149+
protected override async Task<int> SelectCount()
150150
{
151151
await using var ydbConnection = await client.OpenConnectionAsync();
152152

153-
return (int)(await new YdbCommand(ydbConnection) { CommandText = sql }.ExecuteScalarAsync())!;
153+
return (int)(await new YdbCommand(ydbConnection) { CommandText = $"SELECT MAX(Id) FROM {SloTable.Name}" }.ExecuteScalarAsync())!;
154154
}
155155
}

slo/src/EF/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ await Cli.Run((mode, config) =>
1818
CliMode.Run => new SloTableContext(new PooledDbContextFactory<TableDbContext>(
1919
new DbContextOptionsBuilder<TableDbContext>()
2020
.UseYdb(config.ConnectionString)
21-
.UseLoggerFactory(ISloContext.Factory)
2221
.Options)),
2322
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null)
2423
};

slo/src/EF/SloTableContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int operationTimeout
3232
}
3333

3434
protected override async Task<(int, StatusCode, object?)> Select(
35-
dynamic select,
35+
(Guid Guid, int Id) select,
3636
int readTimeout,
3737
Counter? errorsTotal = null
3838
)
@@ -43,10 +43,10 @@ int operationTimeout
4343
return (0, StatusCode.Success, null);
4444
}
4545

46-
protected override async Task<int> SelectCount(string sql)
46+
protected override async Task<int> SelectCount()
4747
{
4848
await using var dbContext = await client.CreateDbContextAsync();
4949

50-
return await dbContext.Database.SqlQueryRaw<int>($"SELECT COUNT(*) FROM {SloTable.Name}").SingleAsync();
50+
return await dbContext.SloEntities.CountAsync();
5151
}
5252
}

slo/src/Internal/SloTableContext.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public async Task Run(RunConfig runConfig)
9191
intervalMilliseconds: runConfig.ReportPeriod);
9292
prometheus.Start();
9393

94-
_maxId = await SelectCount($"SELECT MAX(id) as max_id FROM `{SloTable.Name}`;");
94+
_maxId = await SelectCount() + 1;
9595

9696
Logger.LogInformation("Init row count: {MaxId}", _maxId);
9797

@@ -110,7 +110,7 @@ public async Task Run(RunConfig runConfig)
110110
var writeTask = ShootingTask(writeLimiter, "write", Save);
111111
var readTask = ShootingTask(readLimiter, "read", Select);
112112

113-
Logger.LogInformation("Started write / read shooting..");
113+
Logger.LogInformation("Started write / read shooting...");
114114

115115
try
116116
{
@@ -210,25 +210,32 @@ Task ShootingTask(RateLimiter rateLimitPolicy, string operationType,
210210

211211
_ = Task.Run(async () =>
212212
{
213-
pendingOperations.Inc();
214-
var sw = Stopwatch.StartNew();
215-
var (attempts, statusCode) = await action(runConfig, errorsTotal);
216-
sw.Stop();
217-
218-
retryAttempts.Set(attempts);
219-
operationsTotal.Inc();
220-
pendingOperations.Dec();
221-
222-
if (statusCode != StatusCode.Success)
213+
try
223214
{
224-
errorsTotal.WithLabels(statusCode.StatusName()).Inc();
225-
operationsFailureTotal.Inc();
226-
operationLatencySeconds.WithLabels("err").Observe(sw.Elapsed.TotalSeconds);
215+
pendingOperations.Inc();
216+
var sw = Stopwatch.StartNew();
217+
var (attempts, statusCode) = await action(runConfig, errorsTotal);
218+
sw.Stop();
219+
220+
retryAttempts.Set(attempts);
221+
operationsTotal.Inc();
222+
pendingOperations.Dec();
223+
224+
if (statusCode != StatusCode.Success)
225+
{
226+
errorsTotal.WithLabels(statusCode.StatusName()).Inc();
227+
operationsFailureTotal.Inc();
228+
operationLatencySeconds.WithLabels("err").Observe(sw.Elapsed.TotalSeconds);
229+
}
230+
else
231+
{
232+
operationsSuccessTotal.Inc();
233+
operationLatencySeconds.WithLabels("success").Observe(sw.Elapsed.TotalSeconds);
234+
}
227235
}
228-
else
236+
catch (Exception e)
229237
{
230-
operationsSuccessTotal.Inc();
231-
operationLatencySeconds.WithLabels("success").Observe(sw.Elapsed.TotalSeconds);
238+
Console.WriteLine(e);
232239
}
233240
}, cancellationTokenSource.Token);
234241
}
@@ -242,10 +249,10 @@ Task ShootingTask(RateLimiter rateLimitPolicy, string operationType,
242249
protected abstract Task<(int, StatusCode)> Save(SloTable sloTable, int writeTimeout,
243250
Counter? errorsTotal = null);
244251

245-
protected abstract Task<(int, StatusCode, object?)> Select(dynamic select, int readTimeout,
252+
protected abstract Task<(int, StatusCode, object?)> Select((Guid Guid, int Id) select, int readTimeout,
246253
Counter? errorsTotal = null);
247254

248-
protected abstract Task<int> SelectCount(string sql);
255+
protected abstract Task<int> SelectCount();
249256

250257
private Task<(int, StatusCode)> Save(Config config, Counter? errorsTotal = null)
251258
{
@@ -271,7 +278,7 @@ Task ShootingTask(RateLimiter rateLimitPolicy, string operationType,
271278
{
272279
var id = Random.Shared.Next(_maxId);
273280
var (attempts, code, _) =
274-
await Select(new { Guid = GuidFromInt(id), Id = id }, config.ReadTimeout, errorsTotal);
281+
await Select(new ValueTuple<Guid, int>(GuidFromInt(id), id), config.ReadTimeout, errorsTotal);
275282

276283
return (attempts, code);
277284
}

0 commit comments

Comments
 (0)