Fix Kafka retry seek, bound buffers, and cut hot-path CPU/memory waste (Phases 41–44).
Stop consumers from committing past failed offsets, harden Rabbit/DataLake/gateway backpressure, dispose JsonDocument/MinIO correctly, and batch expensive DB/HTTP paths. Track progress in docs/performance.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Text.Json;
|
||||
using Confluent.Kafka;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Minio;
|
||||
using Minio.DataModel.Args;
|
||||
|
||||
public sealed class DataLakeWriterService : BackgroundService
|
||||
@@ -8,6 +9,7 @@ public sealed class DataLakeWriterService : BackgroundService
|
||||
private readonly KafkaOptions _kafkaOptions;
|
||||
private readonly DataLakeOptions _opts;
|
||||
private readonly MinioOptions _minioOpts;
|
||||
private readonly IMinioClient _minio;
|
||||
private readonly ILogger<DataLakeWriterService> _logger;
|
||||
|
||||
// Buffer key: identifies one Parquet file-to-be.
|
||||
@@ -24,11 +26,13 @@ public sealed class DataLakeWriterService : BackgroundService
|
||||
IOptions<KafkaOptions> kafkaOptions,
|
||||
IOptions<DataLakeOptions> opts,
|
||||
IOptions<MinioOptions> minioOpts,
|
||||
IMinioClient minio,
|
||||
ILogger<DataLakeWriterService> logger)
|
||||
{
|
||||
_kafkaOptions = kafkaOptions.Value;
|
||||
_opts = opts.Value;
|
||||
_minioOpts = minioOpts.Value;
|
||||
_minio = minio;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -51,8 +55,8 @@ public sealed class DataLakeWriterService : BackgroundService
|
||||
});
|
||||
|
||||
_logger.LogInformation(
|
||||
"DataLakeWriterService started. FlushCount={FlushCount} FlushIntervalSeconds={FlushInterval}",
|
||||
_opts.FlushCount, _opts.FlushIntervalSeconds);
|
||||
"DataLakeWriterService started. FlushCount={FlushCount} FlushIntervalSeconds={FlushInterval} MaxBufferedEvents={MaxBuffered}",
|
||||
_opts.FlushCount, _opts.FlushIntervalSeconds, _opts.MaxBufferedEvents);
|
||||
|
||||
var lastFlush = DateTimeOffset.UtcNow;
|
||||
|
||||
@@ -72,7 +76,24 @@ public sealed class DataLakeWriterService : BackgroundService
|
||||
}
|
||||
|
||||
if (result is not null)
|
||||
{
|
||||
var totalBefore = _buffer.Values.Sum(v => v.Count);
|
||||
if (totalBefore >= _opts.MaxBufferedEvents)
|
||||
{
|
||||
// Consume advanced the position — seek back so we do not drop the message.
|
||||
consumer.Seek(result.TopicPartitionOffset);
|
||||
_logger.LogCritical(
|
||||
"[DATA-LAKE] Buffer at cap {Cap} — pausing consume until flush frees space",
|
||||
_opts.MaxBufferedEvents);
|
||||
await FlushAsync(consumer, ct);
|
||||
lastFlush = DateTimeOffset.UtcNow;
|
||||
if (_buffer.Values.Sum(v => v.Count) >= _opts.MaxBufferedEvents)
|
||||
await Task.Delay(5_000, ct);
|
||||
continue;
|
||||
}
|
||||
|
||||
AddToBuffer(result);
|
||||
}
|
||||
|
||||
var totalBuffered = _buffer.Values.Sum(v => v.Count);
|
||||
var shouldFlushCount = totalBuffered >= _opts.FlushCount;
|
||||
@@ -245,18 +266,18 @@ public sealed class DataLakeWriterService : BackgroundService
|
||||
|
||||
private async Task UploadToMinioAsync(string objectKey, byte[] bytes, CancellationToken ct)
|
||||
{
|
||||
var client = MinioClientFactory.Build(_minioOpts);
|
||||
var bucket = _opts.BucketName;
|
||||
|
||||
var exists = await client.BucketExistsAsync(
|
||||
var exists = await _minio.BucketExistsAsync(
|
||||
new BucketExistsArgs().WithBucket(bucket), ct);
|
||||
if (!exists)
|
||||
await client.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucket), ct);
|
||||
await _minio.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucket), ct);
|
||||
|
||||
await client.PutObjectAsync(new PutObjectArgs()
|
||||
await using var stream = new MemoryStream(bytes);
|
||||
await _minio.PutObjectAsync(new PutObjectArgs()
|
||||
.WithBucket(bucket)
|
||||
.WithObject(objectKey)
|
||||
.WithStreamData(new MemoryStream(bytes))
|
||||
.WithStreamData(stream)
|
||||
.WithObjectSize(bytes.Length)
|
||||
.WithContentType("application/octet-stream"),
|
||||
ct);
|
||||
|
||||
Reference in New Issue
Block a user