|
2 | 2 | using Google.Protobuf; |
3 | 3 | using Google.Protobuf.WellKnownTypes; |
4 | 4 | using Microsoft.Extensions.Logging; |
| 5 | +using Ydb.Sdk.Ado.Internal; |
5 | 6 | using Ydb.Topic; |
6 | 7 | using Ydb.Topic.V1; |
7 | 8 |
|
@@ -146,37 +147,44 @@ private async Task WaitBufferAvailable(CancellationToken cancellationToken) |
146 | 147 |
|
147 | 148 | private async void StartWriteWorker() |
148 | 149 | { |
149 | | - await Initialize(); |
150 | | - |
151 | 150 | try |
152 | 151 | { |
153 | | - while (!_disposeCts.Token.IsCancellationRequested) |
154 | | - { |
155 | | - await _tcsWakeUp.Task.WaitAsync(_disposeCts.Token); |
156 | | - _tcsWakeUp = new TaskCompletionSource(); |
| 152 | + await Initialize(); |
157 | 153 |
|
158 | | - if (_toSendBuffer.IsEmpty) |
| 154 | + try |
| 155 | + { |
| 156 | + while (!_disposeCts.Token.IsCancellationRequested) |
159 | 157 | { |
160 | | - continue; |
161 | | - } |
| 158 | + await _tcsWakeUp.Task.WaitAsync(_disposeCts.Token); |
| 159 | + _tcsWakeUp = new TaskCompletionSource(); |
162 | 160 |
|
163 | | - await _sendInFlightMessagesSemaphoreSlim.WaitAsync(_disposeCts.Token); |
164 | | - try |
165 | | - { |
166 | | - if (_session.IsActive) |
| 161 | + if (_toSendBuffer.IsEmpty) |
167 | 162 | { |
168 | | - await _session.Write(_toSendBuffer); |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + await _sendInFlightMessagesSemaphoreSlim.WaitAsync(_disposeCts.Token); |
| 167 | + try |
| 168 | + { |
| 169 | + if (_session.IsActive) |
| 170 | + { |
| 171 | + await _session.Write(_toSendBuffer); |
| 172 | + } |
| 173 | + } |
| 174 | + finally |
| 175 | + { |
| 176 | + _sendInFlightMessagesSemaphoreSlim.Release(); |
169 | 177 | } |
170 | 178 | } |
171 | | - finally |
172 | | - { |
173 | | - _sendInFlightMessagesSemaphoreSlim.Release(); |
174 | | - } |
| 179 | + } |
| 180 | + catch (OperationCanceledException) |
| 181 | + { |
| 182 | + _logger.LogInformation("WriteWorker[{WriterConfig}] is disposed", _config); |
175 | 183 | } |
176 | 184 | } |
177 | | - catch (OperationCanceledException) |
| 185 | + catch (Exception e) |
178 | 186 | { |
179 | | - _logger.LogInformation("WriteWorker[{WriterConfig}] is disposed", _config); |
| 187 | + _logger.LogCritical(e, "WriteWorker[{WriterConfig}] has unhandled exception! Bug report!", _config); |
180 | 188 | } |
181 | 189 | } |
182 | 190 |
|
@@ -226,21 +234,22 @@ private async Task Initialize() |
226 | 234 |
|
227 | 235 | var receivedInitMessage = stream.Current; |
228 | 236 |
|
229 | | - var status = Status.FromProto(receivedInitMessage.Status, receivedInitMessage.Issues); |
230 | | - |
231 | | - if (status.IsNotSuccess) |
| 237 | + if (receivedInitMessage.Status.IsNotSuccess()) |
232 | 238 | { |
233 | | - if (RetrySettings.DefaultInstance.GetRetryRule(status.StatusCode).Policy != RetryPolicy.None) |
| 239 | + var statusCode = receivedInitMessage.Status.Code(); |
| 240 | + var statusMessage = statusCode.ToMessage(receivedInitMessage.Issues); |
| 241 | + |
| 242 | + if (RetrySettings.DefaultInstance.GetRetryRule(statusCode).Policy != RetryPolicy.None) |
234 | 243 | { |
235 | | - _logger.LogError("Writer initialization failed to start. Reason: {Status}", status); |
| 244 | + _logger.LogError("Writer initialization failed to start. Reason: {Status}", statusMessage); |
236 | 245 |
|
237 | 246 | _ = Task.Run(Initialize); |
238 | 247 | } |
239 | 248 | else |
240 | 249 | { |
241 | | - _logger.LogCritical("Writer initialization failed to start. Reason: {Status}", status); |
| 250 | + _logger.LogCritical("Writer initialization failed to start. Reason: {Status}", statusMessage); |
242 | 251 |
|
243 | | - _session = new NotStartedWriterSession("Initialization failed", status); |
| 252 | + _session = new NotStartedWriterSession($"Initialization failed! Reason: {statusMessage}"); |
244 | 253 | } |
245 | 254 |
|
246 | 255 | return; |
@@ -390,11 +399,6 @@ public NotStartedWriterSession(string reasonExceptionMessage) |
390 | 399 | _reasonException = new WriterException(reasonExceptionMessage); |
391 | 400 | } |
392 | 401 |
|
393 | | - public NotStartedWriterSession(string reasonExceptionMessage, Status status) |
394 | | - { |
395 | | - _reasonException = new WriterException(reasonExceptionMessage, status); |
396 | | - } |
397 | | - |
398 | 402 | public Task Write(ConcurrentQueue<MessageSending> toSendBuffer) |
399 | 403 | { |
400 | 404 | while (toSendBuffer.TryDequeue(out var messageSending)) |
@@ -481,7 +485,7 @@ public async Task Write(ConcurrentQueue<MessageSending> toSendBuffer) |
481 | 485 |
|
482 | 486 | var messageData = sendData.MessageData; |
483 | 487 |
|
484 | | - if (messageData.SeqNo == default) |
| 488 | + if (messageData.SeqNo == 0) |
485 | 489 | { |
486 | 490 | messageData.SeqNo = ++currentSeqNum; |
487 | 491 | } |
@@ -511,13 +515,12 @@ private async Task RunProcessingWriteAck() |
511 | 515 | while (await Stream.MoveNextAsync()) |
512 | 516 | { |
513 | 517 | var messageFromServer = Stream.Current; |
514 | | - var status = Status.FromProto(messageFromServer.Status, messageFromServer.Issues); |
515 | 518 |
|
516 | | - if (status.IsNotSuccess) |
| 519 | + if (messageFromServer.Status.IsNotSuccess()) |
517 | 520 | { |
518 | 521 | Logger.LogError( |
519 | 522 | "WriterSession[{SessionId}] received unsuccessful status while processing writeAck: {Status}", |
520 | | - SessionId, status); |
| 523 | + SessionId, messageFromServer.Status.Code().ToMessage(messageFromServer.Issues)); |
521 | 524 | return; |
522 | 525 | } |
523 | 526 |
|
|
0 commit comments