ydb 0.17.1

Crate contains generated low-level grpc code from YDB API protobuf, used as base for ydb crate
Documentation
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
use std::sync::Arc;
use std::time::{Duration, Instant};

use tokio::sync::{Mutex, oneshot, watch};
use tokio::task::JoinHandle;
use tokio::time::timeout;
use tokio_util::sync::CancellationToken;
use tracing::{error, trace};
use ydb_grpc::ydb_proto::topic::stream_write_message;

use crate::client_topic::compression::Executor;
use crate::client_topic::topicwriter::connection::ConnectionInfo;
use crate::client_topic::topicwriter::message::TopicWriterMessage;
use crate::client_topic::topicwriter::message_write_status::{
    MessageWriteStatus, MessageWriteStatusValidator,
};
use crate::client_topic::topicwriter::queue::Queue;
use crate::client_topic::topicwriter::stream_writer::StreamWriter;
use crate::client_topic::topicwriter::writer_options::TopicWriterOptions;
use crate::errors::NeedRetry;
use crate::grpc_connection_manager::GrpcConnectionManager;
use crate::grpc_wrapper::grpc_stream_wrapper::AsyncGrpcStreamWrapper;
use crate::grpc_wrapper::raw_topic_service::client::RawTopicClient;
use crate::grpc_wrapper::raw_topic_service::stream_write::RawServerMessage;
use crate::retry::{Retry, RetryParams};
use crate::{YdbError, YdbResult};
use ydb_grpc::ydb_proto::topic::TransactionIdentity;

pub(crate) struct ReconnectorParams {
    pub(crate) writer_options: TopicWriterOptions,
    pub(crate) producer_id: String,
    pub(crate) connection_manager: GrpcConnectionManager,
    pub(crate) cancellation_token: CancellationToken,
    pub(crate) retrier: Arc<dyn Retry>,
    pub(crate) fatal_error_tx: oneshot::Sender<YdbError>,
    pub(crate) flush_timeout: Duration,
    pub(crate) executor: Arc<dyn Executor>,
    pub(crate) tx_identity: Option<TransactionIdentity>,
    pub(crate) status_validator: MessageWriteStatusValidator,
}

#[derive(Clone)]
enum ReconnectorStatus {
    Working,
    FinishedWithError(YdbError),
    Stopped,
}

impl ReconnectorStatus {
    pub(crate) fn check_working(&self) -> YdbResult<()> {
        match self {
            ReconnectorStatus::Working => Ok(()),
            ReconnectorStatus::FinishedWithError(err) => Err(err.clone()),
            ReconnectorStatus::Stopped => Err(YdbError::custom("is stopped")),
        }
    }
}

struct ReconnectorState {
    connection_info: ConnectionInfo,
}

pub(crate) struct Reconnector {
    state: Arc<Mutex<ReconnectorState>>,
    cancellation_token: CancellationToken,
    reconnect_loop: JoinHandle<()>,
    queue: Queue,
    auto_seq_no: bool,
    flush_timeout: Duration,
    status_rx: watch::Receiver<ReconnectorStatus>,
}

impl Reconnector {
    pub(crate) async fn new(params: ReconnectorParams) -> YdbResult<Self> {
        let queue = Queue::new_with_status_validator(params.status_validator);
        let cancellation_token = params.cancellation_token;
        let auto_seq_no = params.writer_options.auto_seq_no;

        let (init_tx, init_rx) = oneshot::channel();
        let (status_tx, status_rx) = watch::channel(ReconnectorStatus::Working);

        let reconnect_loop = Reconnector::start_reconnection_loop(
            ReconnectionHelper {
                connection_manager: params.connection_manager,
                retrier: params.retrier,
                cancellation_token: cancellation_token.clone(),
                writer_options: params.writer_options,
                producer_id: params.producer_id,
                queue: queue.clone(),
                executor: params.executor,
                tx_identity: params.tx_identity,
            },
            params.fatal_error_tx,
            init_tx,
            status_tx,
        );

        let connection_info = match init_rx.await {
            Ok(Ok(connection_info)) => connection_info,
            Ok(Err(err)) => {
                return Err(err);
            }
            Err(err) => {
                return Err(YdbError::from(err));
            }
        };

        Ok(Reconnector {
            state: Arc::new(Mutex::new(ReconnectorState { connection_info })),
            cancellation_token: cancellation_token.clone(),
            reconnect_loop,
            queue,
            auto_seq_no,
            flush_timeout: params.flush_timeout,
            status_rx,
        })
    }

    fn start_reconnection_loop(
        helper: ReconnectionHelper,
        fatal_error_tx: oneshot::Sender<YdbError>,
        init_tx: oneshot::Sender<YdbResult<ConnectionInfo>>,
        status_tx: watch::Sender<ReconnectorStatus>,
    ) -> JoinHandle<()> {
        tokio::spawn(async move {
            ReconnectionLoop::new(helper, init_tx, status_tx)
                .run(fatal_error_tx)
                .await
        })
    }

    pub(crate) async fn add_message(
        &self,
        mut message: TopicWriterMessage,
        ack_sender: Option<oneshot::Sender<YdbResult<MessageWriteStatus>>>,
    ) -> YdbResult<()> {
        self.check_working()?;

        // Here we take a lock across the whole function.
        // Updating last_seq_no_assigned and putting a new message to the queue must be transactional.
        //
        // Example of a race condition that we prevent here (two threads):
        // 1. Thread1: last_seq_no_assigned = 1
        // 2. Thread2: last_seq_no_assigned = 2
        // 3. Thread2: add message<seq_no=2> to queue
        // 4. Thread1: add message<seq_no=1> to queue <--- ERROR: Message seq_no order is violated.
        let mut state_guard = self.state.lock().await;

        if self.auto_seq_no {
            if message.seq_no.is_some() {
                return Err(YdbError::custom(
                    "explicitly specifying message.seq_no is only allowed if auto_seq_no is disabled",
                ));
            }
            let last_seq_no_assigned = state_guard.connection_info.last_seq_no_assigned;
            message.seq_no = Some(last_seq_no_assigned + 1);
        }

        let Some(message_seq_no) = message.seq_no else {
            return Err(YdbError::custom("empty message seq_no is provided"));
        };
        state_guard.connection_info.last_seq_no_assigned = message_seq_no;

        let message = message.try_into()?;
        self.queue.add_message(message, ack_sender).await
    }

    pub(crate) async fn flush(&self) -> YdbResult<()> {
        self.check_working()?;
        match timeout(self.flush_timeout, self.queue.flush()).await {
            Ok(result) => result,
            Err(_) => Err(YdbError::custom("flush: timed out")),
        }
    }

    pub(crate) async fn stop(self) -> YdbResult<()> {
        self.queue.close_for_new_messages().await;
        let flush_result = self.flush().await;

        self.cancellation_token.cancel();

        let reconnector_result = self.stop_inner().await;

        flush_result?;
        reconnector_result?;

        Ok(())
    }

    async fn stop_inner(self) -> YdbResult<()> {
        match self.status() {
            ReconnectorStatus::Working => {
                self.reconnect_loop.await.map_err(|err| {
                    YdbError::custom(format!(
                        "stop: error while waiting for reconnection_loop to finish: {err}"
                    ))
                })?;
            }
            ReconnectorStatus::FinishedWithError(err) => return Err(err.clone()),
            ReconnectorStatus::Stopped => return Ok(()),
        }

        Ok(())
    }

    fn status(&self) -> ReconnectorStatus {
        self.status_rx.borrow().clone()
    }

    fn check_working(&self) -> YdbResult<()> {
        self.status().check_working()
    }
}

struct ReconnectionHelper {
    queue: Queue,
    writer_options: TopicWriterOptions,
    connection_manager: GrpcConnectionManager,
    retrier: Arc<dyn Retry>,
    cancellation_token: CancellationToken,
    producer_id: String,
    executor: Arc<dyn Executor>,
    tx_identity: Option<TransactionIdentity>,
}

enum WaitBeforeReconnectResult {
    Ok,
    Cancelled,
}

struct RecreateStreamWriterResult {
    stream_writer: StreamWriter,
    connection_info: ConnectionInfo,
}

impl ReconnectionHelper {
    async fn recreate_stream_writer(
        &self,
        error_sender: oneshot::Sender<YdbError>,
    ) -> YdbResult<RecreateStreamWriterResult> {
        self.queue.reset_progress().await;

        let mut stream = self.connect().await?;
        let init_response = ConnectionInfo::try_from(stream.receive::<RawServerMessage>().await?)?;
        let server_codecs = init_response.codecs_from_server.clone().into();

        Ok(RecreateStreamWriterResult {
            stream_writer: StreamWriter::new(
                self.writer_options.clone(),
                stream,
                self.queue.clone(),
                error_sender,
                server_codecs,
                self.executor.clone(),
                self.tx_identity.clone(),
            )
            .await?,
            connection_info: init_response,
        })
    }

    async fn connect(
        &self,
    ) -> YdbResult<
        AsyncGrpcStreamWrapper<stream_write_message::FromClient, stream_write_message::FromServer>,
    > {
        let init_request_body = stream_write_message::InitRequest {
            path: self.writer_options.topic_path.clone(),
            producer_id: self.producer_id.clone(),
            write_session_meta: self
                .writer_options
                .session_metadata
                .clone()
                .unwrap_or_default(),
            get_last_seq_no: self.writer_options.auto_seq_no,
            partitioning: Some(
                self.writer_options
                    .partitioning
                    .to_grpc_init_partitioning(self.producer_id.clone()),
            ),
        };

        let mut topic_service = self
            .connection_manager
            .get_auth_service(RawTopicClient::new)
            .await?;

        let stream = topic_service
            .stream_write(init_request_body.clone())
            .await?;

        Ok(stream)
    }

    fn is_retry_allowed(err: &YdbError) -> bool {
        match err.need_retry() {
            NeedRetry::True => true,
            // IdempotentOnly errors are retryable because the
            // 'Write to Topic With seq_no deduplication' operation is idempotent.
            NeedRetry::IdempotentOnly => true,
            NeedRetry::False => false,
        }
    }

    // Decides whether reconnect is allowed and returns a wait timeout if it is.
    fn get_timeout_before_reconnect(
        &self,
        attempt: usize,
        time_from_start: Duration,
    ) -> Option<Duration> {
        let decision = self.retrier.retry_decision(RetryParams {
            attempt,
            time_from_start,
        });

        decision.allow_retry.then_some(decision.wait_timeout)
    }

    async fn wait_before_reconnect(&self, wait_timeout: Duration) -> WaitBeforeReconnectResult {
        match timeout(wait_timeout, self.cancellation_token.cancelled()).await {
            Ok(_) => WaitBeforeReconnectResult::Cancelled,
            Err(_) => WaitBeforeReconnectResult::Ok,
        }
    }
}

struct ReconnectionLoop {
    helper: ReconnectionHelper,
    init_tx: Option<oneshot::Sender<YdbResult<ConnectionInfo>>>,
    status_tx: watch::Sender<ReconnectorStatus>,
    reconnect_start_time: Instant,
    attempt: usize,
    stream_writer: Option<StreamWriter>,
}

#[derive(Debug)]
enum ReconnectionLoopStatus {
    HandleError(YdbError),
    RecreateStreamWriter,
    WaitForErrorOrCancellation(oneshot::Receiver<YdbError>),
    Exit(Option<YdbError>),
}

impl ReconnectionLoop {
    fn new(
        helper: ReconnectionHelper,
        init_tx: oneshot::Sender<YdbResult<ConnectionInfo>>,
        status_tx: watch::Sender<ReconnectorStatus>,
    ) -> Self {
        Self {
            helper,
            init_tx: Some(init_tx),
            status_tx,
            reconnect_start_time: Instant::now(),
            attempt: 0,
            stream_writer: None,
        }
    }

    async fn run(&mut self, fatal_error_tx: oneshot::Sender<YdbError>) {
        let mut status = ReconnectionLoopStatus::RecreateStreamWriter;

        let final_result = loop {
            status = match status {
                ReconnectionLoopStatus::HandleError(err) => self.handle_error(err).await,
                ReconnectionLoopStatus::RecreateStreamWriter => self.recreate_stream_writer().await,
                ReconnectionLoopStatus::WaitForErrorOrCancellation(error_receiver) => {
                    self.wait_for_error_or_cancellation(error_receiver).await
                }
                ReconnectionLoopStatus::Exit(err) => {
                    break err;
                }
            };
        };

        if let Some(stream_writer) = self.stream_writer.take() {
            let _ = stream_writer.stop().await;
        }

        if let Some(final_error) = final_result {
            self.update_status(ReconnectorStatus::FinishedWithError(final_error.clone()));
            self.helper
                .queue
                .notify_reception_tickets(final_error.clone())
                .await;

            if let Some(tx) = self.init_tx.take() {
                let _ = tx.send(Err(final_error.clone()));
            }

            if let Err(err) = fatal_error_tx.send(final_error) {
                error!("can't send fatal error to TopicWriter: channel is closed: {err}");
            }
        } else {
            self.update_status(ReconnectorStatus::Stopped);
        }
    }

    fn update_status(&self, status: ReconnectorStatus) {
        if let Err(err) = self.status_tx.send(status) {
            error!("can't update status: status channel is closed: {err}");
        }
    }

    async fn handle_error(&mut self, err: YdbError) -> ReconnectionLoopStatus {
        if !ReconnectionHelper::is_retry_allowed(&err) {
            trace!("reconnect is not allowed for error: {err}");
            return ReconnectionLoopStatus::Exit(Some(err));
        }

        trace!("error, trying to reconnect: {err}");

        let Some(wait_timeout) = self
            .helper
            .get_timeout_before_reconnect(self.attempt, self.reconnect_start_time.elapsed())
        else {
            return ReconnectionLoopStatus::Exit(Some(YdbError::custom(format!(
                "reconnect is not allowed after {} attempts for error: {err}",
                self.attempt,
            ))));
        };

        match self.helper.wait_before_reconnect(wait_timeout).await {
            WaitBeforeReconnectResult::Ok => ReconnectionLoopStatus::RecreateStreamWriter,
            WaitBeforeReconnectResult::Cancelled => ReconnectionLoopStatus::Exit(None),
        }
    }

    async fn recreate_stream_writer(&mut self) -> ReconnectionLoopStatus {
        if self.helper.cancellation_token.is_cancelled() {
            return ReconnectionLoopStatus::Exit(None);
        }

        // Wait ending old stream writer before recreating
        if let Some(old) = self.stream_writer.take()
            && let Err(err) = old.stop().await
        {
            return ReconnectionLoopStatus::HandleError(err);
        }

        let (error_sender, error_receiver) = oneshot::channel();
        match self.helper.recreate_stream_writer(error_sender).await {
            Ok(swr) => {
                self.stream_writer = Some(swr.stream_writer);
                self.attempt = 0;

                if let Some(tx) = self.init_tx.take() {
                    let _ = tx.send(Ok(swr.connection_info));
                }

                ReconnectionLoopStatus::WaitForErrorOrCancellation(error_receiver)
            }
            Err(err) => {
                trace!("error creating stream writer: {err}");
                self.attempt += 1;

                ReconnectionLoopStatus::HandleError(err)
            }
        }
    }

    async fn wait_for_error_or_cancellation(
        &mut self,
        error_receiver: oneshot::Receiver<YdbError>,
    ) -> ReconnectionLoopStatus {
        tokio::select! {
            _ = self.helper.cancellation_token.cancelled() => ReconnectionLoopStatus::Exit(None),
            received_err = error_receiver => match received_err {
                Ok(err) => {
                    self.reconnect_start_time = Instant::now();
                    ReconnectionLoopStatus::HandleError(err)
                },
                Err(chan_err) => ReconnectionLoopStatus::Exit(Some(YdbError::custom(format!("error channel error: {chan_err}"))))
            },
        }
    }
}