-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathKafkaClient.cs
More file actions
583 lines (499 loc) · 22.1 KB
/
Copy pathKafkaClient.cs
File metadata and controls
583 lines (499 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
using Confluent.Kafka;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Opc.Ua.Cloud.Publisher;
using Opc.Ua.Cloud.Publisher.Interfaces;
using Opc.Ua.Cloud.Publisher.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
public class KafkaClient : IBrokerClient
{
private IProducer<Null, string> _producer = null;
private IProducer<Null, string> _altProducer = null;
private IConsumer<Ignore, byte[]> _consumer = null;
private readonly ILogger _logger;
private readonly ICommandProcessor _commandProcessor;
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
// Periodic liveness probe: catches "broker came back" while we are idle.
private static readonly TimeSpan HealthCheckInterval = TimeSpan.FromSeconds(30);
private static readonly TimeSpan HealthCheckTimeout = TimeSpan.FromSeconds(5);
private Timer _healthCheckTimer;
private int _healthCheckInFlight;
public KafkaClient(ILoggerFactory loggerFactory, ICommandProcessor commandProcessor)
{
_logger = loggerFactory.CreateLogger("KafkaClient");
_commandProcessor = commandProcessor;
}
// Updates the diagnostics flag for the given broker (primary vs alt) and logs transitions.
private void SetBrokerConnected(bool isAlt, bool connected, string reason)
{
bool previous = isAlt
? Diagnostics.Singleton.Info.ConnectedToAltBroker
: Diagnostics.Singleton.Info.ConnectedToBroker;
if (isAlt)
{
Diagnostics.Singleton.Info.ConnectedToAltBroker = connected;
}
else
{
Diagnostics.Singleton.Info.ConnectedToBroker = connected;
}
if (previous != connected)
{
string label = isAlt ? "alt Kafka broker" : "Kafka broker";
if (connected)
{
_logger.LogInformation($"Connection to {label} restored ({reason}).");
}
else
{
_logger.LogWarning($"Connection to {label} lost ({reason}).");
}
}
}
// Builds the librdkafka asynchronous error handler. This fires on broker-down,
// all-brokers-down, transport errors and fatal errors, even when we are not
// actively producing - giving us a live signal to flip the diagnostics flag.
private Action<IProducer<Null, string>, Error> BuildProducerErrorHandler(bool isAlt)
{
return (producer, error) =>
{
if (error == null)
{
return;
}
// Treat fatal errors and known disconnect codes as "not connected".
bool isDisconnect =
error.IsFatal
|| error.Code == ErrorCode.Local_AllBrokersDown
|| error.Code == ErrorCode.Local_Transport
|| error.Code == ErrorCode.Local_Authentication
|| error.Code == ErrorCode.BrokerNotAvailable
|| error.Code == ErrorCode.NetworkException;
if (isDisconnect)
{
SetBrokerConnected(isAlt, false, $"{error.Code}: {error.Reason}");
}
else
{
_logger.LogWarning($"Kafka producer error ({(isAlt ? "alt" : "primary")}): {error.Code} - {error.Reason}");
}
};
}
// Wraps ProduceAsync so we can flip the diagnostics flag on each outcome:
// success -> connected = true (recovery), ProduceException -> connected = false (e.g. Local_MsgTimedOut).
private async Task TryPublishAsync(IProducer<Null, string> producer, string topic, Message<Null, string> message, bool isAlt)
{
try
{
await producer.ProduceAsync(topic, message, _cancellationTokenSource.Token).ConfigureAwait(false);
SetBrokerConnected(isAlt, true, "publish succeeded");
}
catch (ProduceException<Null, string> ex)
{
SetBrokerConnected(isAlt, false, $"publish failed: {ex.Error.Code} - {ex.Error.Reason}");
throw;
}
}
// Lazily starts a background timer that periodically probes each producer with GetMetadata.
// This catches the "broker came back" case while we are idle (no publishes in flight).
private void EnsureHealthCheckTimerStarted()
{
if (_healthCheckTimer != null)
{
return;
}
_healthCheckTimer = new Timer(
HealthCheckCallback,
state: null,
dueTime: HealthCheckInterval,
period: HealthCheckInterval);
}
private void HealthCheckCallback(object _)
{
// Prevent overlapping probes if a previous one is still running.
if (Interlocked.Exchange(ref _healthCheckInFlight, 1) == 1)
{
return;
}
try
{
ProbeProducerHealth(_producer, isAlt: false);
ProbeProducerHealth(_altProducer, isAlt: true);
}
catch (Exception ex)
{
_logger.LogError($"Kafka health check failed: {ex.Message}");
}
finally
{
Interlocked.Exchange(ref _healthCheckInFlight, 0);
}
}
private void ProbeProducerHealth(IProducer<Null, string> producer, bool isAlt)
{
if (producer == null)
{
return;
}
try
{
using var adminClient = new DependentAdminClientBuilder(producer.Handle).Build();
Metadata metadata = adminClient.GetMetadata(HealthCheckTimeout);
bool healthy = metadata?.Brokers != null && metadata.Brokers.Count > 0;
SetBrokerConnected(isAlt, healthy, healthy ? "health check ok" : "health check returned no brokers");
}
catch (KafkaException ex)
{
SetBrokerConnected(isAlt, false, $"health check failed: {ex.Error.Code} - {ex.Error.Reason}");
}
catch (Exception ex)
{
SetBrokerConnected(isAlt, false, $"health check failed: {ex.Message}");
}
}
// Verifies the broker is actually reachable by requesting cluster metadata.
// ProducerBuilder.Build() does not open a connection (it connects lazily on first use),
// so we piggy-back a dependent AdminClient on the producer's handle and call GetMetadata
// with a bounded timeout. Returns true if at least one broker responded, otherwise false.
private bool VerifyBrokerConnection(IProducer<Null, string> producer, string brokerLabel)
{
if (producer == null)
{
return false;
}
try
{
using var adminClient = new DependentAdminClientBuilder(producer.Handle).Build();
Metadata metadata = adminClient.GetMetadata(TimeSpan.FromSeconds(10));
if (metadata?.Brokers == null || metadata.Brokers.Count == 0)
{
_logger.LogError($"No brokers returned in metadata from {brokerLabel}.");
return false;
}
_logger.LogInformation($"Verified connection to {brokerLabel}: {metadata.Brokers.Count} broker(s) reachable.");
return true;
}
catch (KafkaException ex)
{
_logger.LogError($"Failed to verify connection to {brokerLabel}: {ex.Message}");
return false;
}
catch (Exception ex)
{
_logger.LogError($"Unexpected error while verifying connection to {brokerLabel}: {ex.Message}");
return false;
}
}
public async Task ConnectAsync(bool altBroker = false)
{
try
{
if (altBroker)
{
// only (re)build the alt-broker producer; leave primary producer/consumer alone
if (_altProducer != null)
{
_altProducer.Flush();
_altProducer.Dispose();
_altProducer = null;
Diagnostics.Singleton.Info.ConnectedToAltBroker = false;
}
if (string.IsNullOrEmpty(Settings.Instance.AltBrokerUrl))
{
_logger.LogError("Alt broker URL not configured. Cannot connect to alt broker!");
return;
}
var altConfig = new ProducerConfig
{
BootstrapServers = Settings.Instance.AltBrokerUrl + ":" + Settings.Instance.AltBrokerPort,
MessageTimeoutMs = 10000,
SecurityProtocol = SecurityProtocol.SaslSsl,
SaslMechanism = SaslMechanism.Plain,
SaslUsername = Settings.Instance.AltBrokerUsername,
SaslPassword = Settings.Instance.AltBrokerPassword,
MessageSendMaxRetries = 2,
RetryBackoffMs = 100,
EnableIdempotence = true,
MaxInFlight = 5
};
_altProducer = new ProducerBuilder<Null, string>(altConfig)
.SetErrorHandler(BuildProducerErrorHandler(isAlt: true))
.Build();
if (VerifyBrokerConnection(_altProducer, "alt Kafka broker"))
{
Diagnostics.Singleton.Info.ConnectedToAltBroker = true;
_logger.LogInformation("Connected to alt Kafka broker.");
}
else
{
_altProducer.Dispose();
_altProducer = null;
Diagnostics.Singleton.Info.ConnectedToAltBroker = false;
_logger.LogError("Could not verify connection to alt Kafka broker.");
}
EnsureHealthCheckTimerStarted();
return;
}
// primary broker (re)connect: cancel any pending primary operations
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
_cancellationTokenSource = new CancellationTokenSource();
// disconnect if still connected
if (_producer != null)
{
_producer.Flush();
_producer.Dispose();
_producer = null;
Diagnostics.Singleton.Info.ConnectedToBroker = false;
}
if (_altProducer != null)
{
_altProducer.Flush();
_altProducer.Dispose();
_altProducer = null;
Diagnostics.Singleton.Info.ConnectedToAltBroker = false;
}
if (_consumer != null)
{
_consumer.Close();
_consumer.Dispose();
_consumer = null;
}
if (string.IsNullOrEmpty(Settings.Instance.BrokerUrl))
{
// no broker URL configured = nothing to connect to!
_logger.LogError("Broker URL not configured. Cannot connect to broker!");
return;
}
// create Kafka client
var config = new ProducerConfig
{
BootstrapServers = Settings.Instance.BrokerUrl + ":" + Settings.Instance.BrokerPort,
MessageTimeoutMs = 10000,
SecurityProtocol = SecurityProtocol.SaslSsl,
SaslMechanism = SaslMechanism.Plain,
SaslUsername = Settings.Instance.BrokerUsername,
SaslPassword = Settings.Instance.BrokerPassword,
MessageSendMaxRetries = 2, // Reduce internal retries to fail fast and let store-and-forward handle it
RetryBackoffMs = 100,
EnableIdempotence = true, // Enable idempotence for exactly-once semantics
MaxInFlight = 5 // Limit in-flight requests to prevent ordering issues during retries
};
_producer = new ProducerBuilder<Null, string>(config)
.SetErrorHandler(BuildProducerErrorHandler(isAlt: false))
.Build();
if (VerifyBrokerConnection(_producer, "Kafka broker"))
{
Diagnostics.Singleton.Info.ConnectedToBroker = true;
}
else
{
_producer.Dispose();
_producer = null;
Diagnostics.Singleton.Info.ConnectedToBroker = false;
_logger.LogError("Could not verify connection to Kafka broker.");
return;
}
if (Settings.Instance.UseAltBrokerForMetadata && Settings.Instance.UseKafkaForAlt)
{
var altConfig = new ProducerConfig
{
BootstrapServers = Settings.Instance.AltBrokerUrl + ":" + Settings.Instance.AltBrokerPort,
MessageTimeoutMs = 10000,
SecurityProtocol = SecurityProtocol.SaslSsl,
SaslMechanism = SaslMechanism.Plain,
SaslUsername = Settings.Instance.AltBrokerUsername,
SaslPassword = Settings.Instance.AltBrokerPassword,
MessageSendMaxRetries = 2,
RetryBackoffMs = 100,
EnableIdempotence = true,
MaxInFlight = 5
};
_altProducer = new ProducerBuilder<Null, string>(altConfig)
.SetErrorHandler(BuildProducerErrorHandler(isAlt: true))
.Build();
if (VerifyBrokerConnection(_altProducer, "alt Kafka broker"))
{
Diagnostics.Singleton.Info.ConnectedToAltBroker = true;
}
else
{
_altProducer.Dispose();
_altProducer = null;
Diagnostics.Singleton.Info.ConnectedToAltBroker = false;
_logger.LogError("Could not verify connection to alt Kafka broker (metadata path).");
}
}
if (!string.IsNullOrEmpty(Settings.Instance.BrokerCommandTopic))
{
var conf = new ConsumerConfig
{
GroupId = Settings.Instance.PublisherName,
BootstrapServers = Settings.Instance.BrokerUrl + ":" + Settings.Instance.BrokerPort,
AutoOffsetReset = AutoOffsetReset.Earliest,
SecurityProtocol = SecurityProtocol.SaslSsl,
SaslMechanism = SaslMechanism.Plain,
SaslUsername = Settings.Instance.BrokerUsername,
SaslPassword = Settings.Instance.BrokerPassword
};
_consumer = new ConsumerBuilder<Ignore, byte[]>(conf).Build();
_consumer.Subscribe(Settings.Instance.BrokerCommandTopic);
// start background task to handle incoming commands
_ = Task.Run(async () => await HandleCommandAsync().ConfigureAwait(false));
}
EnsureHealthCheckTimerStarted();
_logger.LogInformation("Connected to Kafka broker.");
}
catch (Exception ex)
{
_logger.LogCritical("Failed to connect to Kafka broker: " + ex.Message);
}
}
public async Task PublishAsync(byte[] payload)
{
if (_producer == null)
{
throw new InvalidOperationException("Kafka producer is not connected.");
}
Message<Null, string> message = new()
{
Headers = new Headers() { { "Content-Type", Encoding.UTF8.GetBytes("application/json") } },
Value = Encoding.UTF8.GetString(payload)
};
await TryPublishAsync(_producer, Settings.Instance.BrokerMessageTopic, message, isAlt: false).ConfigureAwait(false);
}
public async Task PublishMetadataAsync(byte[] payload, IReadOnlyDictionary<string, string> cloudEventAttributes = null)
{
Headers headers = new Headers() { { "Content-Type", Encoding.UTF8.GetBytes("application/json") } };
if (cloudEventAttributes != null)
{
// CloudEvents binary content mode for Kafka: each attribute is carried as a "ce_"-prefixed header
// ('datacontenttype' is conveyed by the Content-Type header above).
foreach (KeyValuePair<string, string> attribute in cloudEventAttributes)
{
if (string.Equals(attribute.Key, "datacontenttype", StringComparison.Ordinal))
{
continue;
}
headers.Add("ce_" + attribute.Key, Encoding.UTF8.GetBytes(attribute.Value));
}
}
Message<Null, string> message = new()
{
Headers = headers,
Value = Encoding.UTF8.GetString(payload)
};
if (Settings.Instance.UseAltBrokerForMetadata && Settings.Instance.UseKafkaForAlt)
{
if (_altProducer == null)
{
throw new InvalidOperationException("Alternate Kafka producer is not connected.");
}
await TryPublishAsync(_altProducer, Settings.Instance.BrokerMetadataTopic, message, isAlt: true).ConfigureAwait(false);
}
else
{
if (_producer == null)
{
throw new InvalidOperationException("Kafka producer is not connected.");
}
await TryPublishAsync(_producer, Settings.Instance.BrokerMetadataTopic, message, isAlt: false).ConfigureAwait(false);
}
}
public async Task PublishResponseAsync(byte[] payload)
{
if (_producer == null)
{
throw new InvalidOperationException("Kafka producer is not connected.");
}
Message<Null, string> message = new()
{
Headers = new Headers() { { "Content-Type", Encoding.UTF8.GetBytes("application/json") } },
Value = Encoding.UTF8.GetString(payload)
};
await TryPublishAsync(_producer, Settings.Instance.BrokerResponseTopic, message, isAlt: false).ConfigureAwait(false);
}
// handles all incoming commands form the cloud
private async Task HandleCommandAsync()
{
while (!_cancellationTokenSource.IsCancellationRequested)
{
ResponseModel response = new()
{
TimeStamp = DateTime.UtcNow,
};
try
{
ConsumeResult<Ignore, byte[]> result = _consumer.Consume(_cancellationTokenSource.Token);
string requestPayload = Encoding.UTF8.GetString(result.Message.Value);
_logger.LogInformation($"Received method call with topic: {result.Topic} and payload: {requestPayload}");
// parse the message
RequestModel request = JsonConvert.DeserializeObject<RequestModel>(requestPayload);
// discard messages that are older than 15 seconds
if (request.TimeStamp < DateTime.UtcNow.AddSeconds(-15))
{
_logger.LogInformation($"Discarding old message with timestamp {request.TimeStamp}");
continue;
}
response.CorrelationId = request.CorrelationId;
// route this to the right handler
if (request.Command == "publishnodes")
{
response.Status = Encoding.UTF8.GetString(await _commandProcessor.PublishNodesAsync(requestPayload).ConfigureAwait(false));
response.Success = true;
}
else if (request.Command == "unpublishnodes")
{
response.Status = Encoding.UTF8.GetString(await _commandProcessor.UnpublishNodesAsync(requestPayload).ConfigureAwait(false));
response.Success = true;
}
else if (request.Command == "unpublishallnodes")
{
response.Status = Encoding.UTF8.GetString(await _commandProcessor.UnpublishAllNodesAsync().ConfigureAwait(false));
response.Success = true;
}
else if (request.Command == "getpublishednodes")
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.GetPublishedNodes());
response.Success = true;
}
else if (request.Command == "getinfo")
{
response.Status = Encoding.UTF8.GetString(_commandProcessor.GetInfo());
response.Success = true;
}
else
{
_logger.LogError("Unknown command received: " + result.Topic);
response.Status = "Unknown command " + result.Topic;
response.Success = false;
}
// send response to Kafka broker
await PublishResponseAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response))).ConfigureAwait(false);
}
catch (Exception ex)
{
if (ex is OperationCanceledException)
{
break;
}
_logger.LogError(ex, "HandleMessageAsync");
response.Status = ex.Message;
response.Success = false;
// send error to Kafka broker
try
{
await PublishResponseAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response))).ConfigureAwait(false);
}
catch (Exception publishEx)
{
_logger.LogError(publishEx, "Failed to publish error response");
}
}
}
}
}