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
//! Caller-thread client-streaming ingestion with cancellation on early drop.
use std::{marker::PhantomData, rc::Rc};
use greptimedb_proto::v1::{
self as v1, GreptimeRequest, GreptimeResponse, RequestHeader, greptime_request,
greptime_response,
};
use crate::{
batch::{BatchEncode, RowBatch},
buffer_policy::MemoryBuffer,
client::{Client, StreamFeedback, channel_for, terminal_fault, validate_ok_trailers},
config::ClientOptions,
error::{Error, Result},
hints::WriteHints,
transport::EndpointTransport,
};
const STREAM_PATH: &str = "/greptime.v1.GreptimeDatabase/HandleRequests";
type StreamingCall = grpc::ClientStreamingCall<GreptimeRequest, GreptimeResponse>;
/// A thread-affine ingestion stream whose final response acknowledges all requests.
///
/// Dropping an unfinished stream cancels its HTTP/2 stream instead of waiting for
/// a server response. Cancellation does not undo writes already accepted by the
/// server; callers must not blindly replay requests after an uncertain failure.
pub struct StreamInserter {
call: Option<StreamingCall>,
header: RequestHeader,
batches_sent: usize,
rows_sent: usize,
finished: bool,
feedback: Option<StreamFeedback>,
_not_send_sync: PhantomData<Rc<()>>,
}
impl StreamInserter {
/// Opens a client-streaming RPC with validated routing, credentials and hints.
///
/// # Errors
/// Returns configuration, metadata, transport or stream-submission failures.
pub fn open(
channel: &mut grpc::SyncChannel<EndpointTransport>,
options: &ClientOptions,
extra_hints: Option<&WriteHints>,
) -> Result<Self> {
let call_options = options.try_build_call_options(extra_hints)?;
let header = options.build_request_header(None);
let call = channel.client_streaming(
STREAM_PATH,
grpc::ProstCodec::<GreptimeRequest, GreptimeResponse>::new(),
call_options,
)?;
Ok(Self {
call: Some(call),
header,
batches_sent: 0,
rows_sent: 0,
finished: false,
feedback: None,
_not_send_sync: PhantomData,
})
}
/// Sends one request with the stream's routing and authentication header.
///
/// Counters advance only after the complete message has been accepted by the
/// stream. A failed transport write may nevertheless have reached the peer.
///
/// # Errors
/// Returns counter overflow, terminal stream, transport or server errors.
pub fn send_request(&mut self, request: greptime_request::Request) -> Result<()> {
let rows = request_rows(&request)
.ok_or_else(|| Error::Protocol("stream request row count exceeds usize".into()))?;
let batches_sent = self
.batches_sent
.checked_add(1)
.ok_or_else(|| Error::Protocol("stream batch count exceeds usize".into()))?;
let rows_sent = self
.rows_sent
.checked_add(rows)
.ok_or_else(|| Error::Protocol("stream row count exceeds usize".into()))?;
let call = self
.call
.as_mut()
.ok_or_else(|| Error::Stream("ingestion stream is closed".into()))?;
let message = GreptimeRequest {
header: Some(self.header.clone()),
request: Some(request),
};
if let Err(error) = call.send(&message) {
let error = call_error(call, error);
self.report_error(&error);
return Err(error);
}
self.batches_sent = batches_sent;
self.rows_sent = rows_sent;
Ok(())
}
/// Sends a columnar insert without changing its reusable source buffer.
///
/// # Errors
/// Returns schema, encoding, transport or server errors.
pub fn send_batch(&mut self, batch: &(impl BatchEncode + ?Sized)) -> Result<()> {
self.send_request(greptime_request::Request::Inserts(v1::InsertRequests {
inserts: vec![batch.encode_insert_request()?],
}))
}
/// Sends a columnar insert to an explicit table without changing the batch.
///
/// # Errors
/// Returns schema, destination, encoding, transport or server errors.
pub fn send_batch_to(
&mut self,
table_name: &str,
batch: &(impl BatchEncode + ?Sized),
) -> Result<()> {
self.send_request(greptime_request::Request::Inserts(v1::InsertRequests {
inserts: vec![batch.encode_insert_request_for(table_name)?],
}))
}
/// Sends several tables in one frame after every batch has encoded successfully.
///
/// # Errors
/// Returns schema or encoding errors before sending any frame, or transport
/// and server errors while sending the completed request.
pub fn send_batches(&mut self, batches: &[&dyn BatchEncode]) -> Result<()> {
let inserts = batches
.iter()
.map(|batch| batch.encode_insert_request())
.collect::<Result<Vec<_>>>()?;
self.send_request(greptime_request::Request::Inserts(v1::InsertRequests {
inserts,
}))
}
/// Sends a row-oriented insert without modifying the source buffer.
///
/// # Errors
/// Returns schema, encoding, transport or server errors.
pub fn send_rows(&mut self, batch: &RowBatch) -> Result<()> {
self.send_request(greptime_request::Request::RowInserts(
v1::RowInsertRequests {
inserts: vec![batch.encode_row_insert_request()?],
},
))
}
/// Sends a columnar delete containing only the batch's tag and timestamp keys.
///
/// # Errors
/// Returns schema, encoding, transport or server errors.
pub fn send_delete_batch(&mut self, batch: &(impl BatchEncode + ?Sized)) -> Result<()> {
self.send_request(greptime_request::Request::Deletes(v1::DeleteRequests {
deletes: vec![batch.encode_delete_request()?],
}))
}
/// Sends a row-oriented delete without modifying the source buffer.
///
/// # Errors
/// Returns schema, encoding, transport or server errors.
pub fn send_delete_rows(&mut self, batch: &RowBatch) -> Result<()> {
self.send_request(greptime_request::Request::RowDeletes(
v1::RowDeleteRequests {
deletes: vec![batch.encode_row_delete_request()?],
},
))
}
/// Sends a columnar insert and clears its buffer only after a successful send.
///
/// Clearing follows the buffer's retention policy. Successful sending is not
/// a server commit acknowledgement; call [`Self::finish`] to obtain that.
///
/// # Errors
/// Returns schema, encoding, transport or server errors without clearing the
/// source buffer. A transport error does not guarantee the peer saw no data.
pub fn write(&mut self, batch: &mut (impl BatchEncode + MemoryBuffer + ?Sized)) -> Result<()> {
self.send_batch(batch)?;
batch.clear();
Ok(())
}
/// Sends a key-only columnar delete, then clears the reusable source buffer.
///
/// # Errors
/// Returns schema, encoding, transport or server errors without clearing the
/// source buffer. A transport error does not guarantee the peer saw no data.
pub fn write_delete(
&mut self,
batch: &mut (impl BatchEncode + MemoryBuffer + ?Sized),
) -> Result<()> {
self.send_delete_batch(batch)?;
batch.clear();
Ok(())
}
/// Returns the number of requests successfully accepted by the stream.
///
/// A multi-table request counts as one batch. This is not an acknowledgement
/// that the server has committed the requests; only `finish` provides that.
#[must_use]
pub fn batches_sent(&self) -> usize {
self.batches_sent
}
/// Returns the total row count in successfully sent insert and delete frames.
///
/// Server-side affected rows may differ from this count.
#[must_use]
pub fn rows_sent(&self) -> usize {
self.rows_sent
}
/// Half-closes the request stream and validates its single terminal response.
///
/// # Errors
/// Returns transport, gRPC trailer, GreptimeDB application-header or missing
/// affected-row errors. The consumed stream is cancelled on failed completion.
pub fn finish(mut self) -> Result<u32> {
self.finished = true;
let mut call = self
.call
.take()
.ok_or_else(|| Error::Stream("ingestion stream is closed".into()))?;
let result = (|| {
let response = call.finish().map_err(|error| call_error(&call, error))?;
let trailers = call
.trailing_metadata()
.map_err(|error| call_error(&call, error))?;
validate_ok_trailers(trailers.as_ref())?;
Error::from_response_header(response.header.as_ref())?;
match response.response {
Some(greptime_response::Response::AffectedRows(rows)) => Ok(rows.value),
None => Err(Error::Protocol(
"GreptimeDatabase returned no affected-row result".into(),
)),
}
})();
match &result {
Ok(_) => {
if let Some(feedback) = &mut self.feedback {
feedback.report(true);
}
}
Err(error) => self.report_error(error),
}
result
}
fn report_error(&mut self, error: &Error) {
if terminal_fault(error)
&& let Some(feedback) = &mut self.feedback
{
feedback.report(false);
}
}
/// Cancels the stream without waiting for the server's final response.
///
/// # Errors
/// Returns stream-control or cancellation failures.
pub fn cancel(mut self) -> Result<()> {
self.finished = true;
let mut call = self
.call
.take()
.ok_or_else(|| Error::Stream("ingestion stream is closed".into()))?;
call.cancel().map_err(|error| call_error(&call, error))
}
}
impl Client {
/// Opens an ingestion stream using this client's defaults.
///
/// # Errors
/// Returns endpoint-index, connection, configuration or stream-submission failures.
pub fn stream_inserter(&mut self) -> Result<StreamInserter> {
self.open_ingestion(None)
}
/// Opens ingestion with per-stream hints and bounded pre-send failover.
///
/// # Errors
/// Returns connection, admission-probe, or stream-submission failures.
pub fn stream_inserter_with_hints(&mut self, hints: &WriteHints) -> Result<StreamInserter> {
self.open_ingestion(Some(hints))
}
fn open_ingestion(&mut self, hints: Option<&WriteHints>) -> Result<StreamInserter> {
self.options.try_build_call_options(hints)?;
self.reconcile_stream_outcomes();
let mut attempted = Vec::new();
let mut last_error = None;
for _ in 0..self.budget() {
let index = match self.select(&attempted) {
Ok(index) => index,
Err(error) => return Err(last_error.unwrap_or(error)),
};
attempted.push(index);
if let Err(error) = self.admit(index, true) {
last_error = Some(error);
continue;
}
let channel = channel_for(&mut self.channels, &self.options, index)?;
match StreamInserter::open(channel, &self.options, hints) {
Ok(mut stream) => {
stream.feedback = Some(self.stream_feedback(index));
return Ok(stream);
}
Err(error) => {
let retry = terminal_fault(&error);
self.attempt_failure(index, &error);
last_error = Some(error);
if !retry {
break;
}
}
}
}
Err(last_error
.unwrap_or_else(|| Error::Protocol("no stream endpoint attempt was made".into())))
}
}
impl Drop for StreamInserter {
fn drop(&mut self) {
if !self.finished
&& let Some(mut call) = self.call.take()
{
// Drop cannot return an error; the call retains cancellation cleanup.
drop(call.cancel());
}
}
}
fn call_error(call: &StreamingCall, error: grpc::StreamError) -> Error {
let status = match error {
grpc::StreamError::Status(status) => status,
grpc::StreamError::WouldBlock => {
grpc::Status::internal("blocking ingestion stream returned pending")
}
};
match call.trailing_metadata() {
Ok(trailers) => Error::from_grpc_status_and_trailers(status, trailers.as_ref()),
Err(_) => Error::Grpc(status),
}
}
fn request_rows(request: &greptime_request::Request) -> Option<usize> {
use greptime_request::Request;
match request {
Request::Inserts(request) => request.inserts.iter().try_fold(0usize, |sum, insert| {
sum.checked_add(insert.row_count as usize)
}),
Request::Deletes(request) => request.deletes.iter().try_fold(0usize, |sum, delete| {
sum.checked_add(delete.row_count as usize)
}),
Request::RowInserts(request) => request.inserts.iter().try_fold(0usize, |sum, insert| {
sum.checked_add(insert.rows.as_ref().map_or(0, |rows| rows.rows.len()))
}),
Request::RowDeletes(request) => request.deletes.iter().try_fold(0usize, |sum, delete| {
sum.checked_add(delete.rows.as_ref().map_or(0, |rows| rows.rows.len()))
}),
Request::Query(_) | Request::Ddl(_) => Some(0),
}
}