-
Notifications
You must be signed in to change notification settings - Fork 3
/
bqs.cpp
374 lines (323 loc) · 12 KB
/
bqs.cpp
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
#include <fstream>
#include <string>
#include <vector>
#include <grpc/grpc.h>
#include <grpc/support/log.h>
#include <grpcpp/grpcpp.h>
#include "google/cloud/bigquery/storage/v1/stream.pb.h"
#include "google/cloud/bigquery/storage/v1/storage.pb.h"
#include "google/cloud/bigquery/storage/v1/storage.grpc.pb.h"
#include <Rcpp.h>
#include "RProgress.h"
using google::cloud::bigquery::storage::v1::ReadSession;
using google::cloud::bigquery::storage::v1::BigQueryRead;
// -- Utilities and logging ----------------------------------------------------
// Define a default logger for gRPC
void bqs_default_log(gpr_log_func_args* args) {
Rcpp::Rcerr << args->message << std::endl;
}
// Set gRPC verbosity level
// [[Rcpp::export(rng=false)]]
void bqs_set_log_verbosity(int severity) {
//-1 UNSET
// 0 DEBUG
// 1 INFO
// 2 ERROR
// 3 QUIET
gpr_set_log_verbosity(static_cast<gpr_log_severity>(severity));
}
// Set gRPC default logger
// [[Rcpp::export(rng=false)]]
void bqs_init_logger() {
gpr_set_log_function(bqs_default_log);
bqs_set_log_verbosity(2);
}
// Check gRPC version
// [[Rcpp::export(rng=false)]]
std::string grpc_version() {
std::string version;
version += grpc_version_string();
version += " ";
version += grpc_g_stands_for();
return version;
}
// Simple read file to read configuration from json
std::string readfile(std::string filename)
{
std::ifstream ifs(filename);
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
return content;
}
// append std::string at the end of a std::vector<uint8_t> vector
void to_raw(const std::string input, std::vector<uint8_t>* output) {
output->insert(output->end(), input.begin(), input.end());
}
// -- Client class -------------------------------------------------------------
class BigQueryReadClient {
public:
BigQueryReadClient(std::shared_ptr<grpc::Channel> channel)
: stub_(BigQueryRead::NewStub(channel)) {
}
void SetClientInfo(const std::string &client_info) {
client_info_ = client_info;
}
// Creation read sessions
ReadSession CreateReadSession(const std::string& project,
const std::string& dataset,
const std::string& table,
const std::string& parent,
const std::int64_t& timestamp_seconds,
const std::int32_t& timestamp_nanos,
const std::vector<std::string>& selected_fields,
const std::string& row_restriction
) {
google::cloud::bigquery::storage::v1::CreateReadSessionRequest method_request;
ReadSession *read_session = method_request.mutable_read_session();
std::string table_fullname =
"projects/" + project + "/datasets/" + dataset + "/tables/" + table;
read_session->set_table(table_fullname);
read_session->set_data_format(
google::cloud::bigquery::storage::v1::DataFormat::ARROW);
if (timestamp_seconds > 0 || timestamp_nanos > 0) {
read_session->mutable_table_modifiers()->
mutable_snapshot_time()->set_seconds(timestamp_seconds);
read_session->mutable_table_modifiers()->
mutable_snapshot_time()->set_nanos(timestamp_nanos);
}
if (!row_restriction.empty()) {
read_session->mutable_read_options()->
set_row_restriction(row_restriction);
}
for (int i = 0; i < int(selected_fields.size()); i++) {
read_session->mutable_read_options()->
add_selected_fields(selected_fields[i]);
}
method_request.set_parent("projects/" + parent);
grpc::ClientContext context;
context.AddMetadata("x-goog-request-params",
"read_session.table=" + table_fullname);
context.AddMetadata("x-goog-api-client", client_info_);
ReadSession method_response;
// The actual RPC.
grpc::Status status = stub_->
CreateReadSession(&context, method_request, &method_response);
if (!status.ok()) {
std::string err;
err += "gRPC method CreateReadSession error -> ";
err += status.error_message();
Rcpp::stop(err.c_str());
}
return method_response;
}
// Read rows from a stream
void ReadRows(const std::string stream,
std::vector<uint8_t>* ipc_stream,
std::int64_t& n,
long int& rows_count,
long int& pages_count,
bool quiet,
RProgress::RProgress* pb,
bool last_stream) {
grpc::ClientContext context;
context.AddMetadata("x-goog-request-params", "read_stream=" + stream);
context.AddMetadata("x-goog-api-client", client_info_);
google::cloud::bigquery::storage::v1::ReadRowsRequest method_request;
method_request.set_read_stream(stream);
method_request.set_offset(0);
google::cloud::bigquery::storage::v1::ReadRowsResponse method_response;
std::unique_ptr<grpc::ClientReader<google::cloud::bigquery::storage::v1::ReadRowsResponse> > reader(
stub_->ReadRows(&context, method_request));
while (reader->Read(&method_response)) {
to_raw(method_response.arrow_record_batch().serialized_record_batch(),
ipc_stream);
method_request.set_offset(
method_request.offset() + method_response.row_count());
pages_count += 1;
if (!quiet) {
if (method_response.has_throttle_state()) {
pb->set_extra(method_response.throttle_state().throttle_percent());
}
if (n > 0) {
if (method_request.offset() + rows_count >= n) {
pb->update(1);
break;
}
pb->tick(method_response.row_count());
} else {
pb->tick(
(method_response.stats().progress().at_response_end()-
method_response.stats().progress().at_response_start()) * 200);
}
} else {
Rcpp::checkUserInterrupt();
}
}
if (n < 0) {
grpc::Status status = reader->Finish();
if (!status.ok()) {
std::string err;
err += "grpc method ReadRows error -> ";
err += status.error_message();
Rcpp::stop(err.c_str());
} else {
if (last_stream) {
pb->update(1);
}
}
}
rows_count += method_request.offset();
}
// Split stream
std::vector<std::string> SplitReadStream(
std::string& stream, double& fraction) {
google::cloud::bigquery::storage::v1::SplitReadStreamRequest method_request;
method_request.set_name(stream);
method_request.set_fraction(fraction);
grpc::ClientContext context;
context.AddMetadata("x-goog-request-params",
"name=" + stream);
context.AddMetadata("x-goog-api-client", client_info_);
google::cloud::bigquery::storage::v1::SplitReadStreamResponse method_response;
// The actual RPC.
grpc::Status status = stub_->
SplitReadStream(&context, method_request, &method_response);
if (!status.ok()) {
std::string err;
err += "gRPC method SplitReadStream error -> ";
err += status.error_message();
Rcpp::stop(err.c_str());
}
return {method_response.primary_stream().name(),
method_response.remainder_stream().name()};
}
private:
std::unique_ptr<BigQueryRead::Stub> stub_;
std::string client_info_;
};
// -- Credentials functions ----------------------------------------------------
std::shared_ptr<grpc::ChannelCredentials> bqs_ssl(
std::string root_certificate) {
grpc::SslCredentialsOptions ssl_options;
if (!root_certificate.empty()) {
ssl_options.pem_root_certs = root_certificate;
}
return grpc::SslCredentials(ssl_options);
}
std::shared_ptr<grpc::ChannelCredentials> bqs_credentials(
std::shared_ptr<grpc::ChannelCredentials> channel_cred,
std::shared_ptr<grpc::CallCredentials> call_cred = nullptr) {
if (channel_cred) {
if (call_cred) {
channel_cred = grpc::CompositeChannelCredentials(
channel_cred,
call_cred
);
}
}
return channel_cred == nullptr ? nullptr : channel_cred;
}
std::shared_ptr<grpc::ChannelCredentials> bqs_google_credentials() {
auto gcp_cred = grpc::GoogleDefaultCredentials();
return bqs_credentials(gcp_cred);
}
std::shared_ptr<grpc::ChannelCredentials> bqs_refresh_token_credentials(
std::string refresh_token, std::string root_certificate = "") {
auto ssl_cred = bqs_ssl(root_certificate);
auto token_cred = grpc::GoogleRefreshTokenCredentials(refresh_token);
return bqs_credentials(ssl_cred, token_cred);
}
std::shared_ptr<grpc::ChannelCredentials> bqs_access_token_credentials(
std::string access_token, std::string root_certificate = "") {
auto ssl_cred = bqs_ssl(root_certificate);
auto token_cred = grpc::AccessTokenCredentials(access_token);
return bqs_credentials(ssl_cred, token_cred);
}
// -- Client functions ---------------------------------------------------------
SEXP bqs_read_client(std::shared_ptr<grpc::ChannelCredentials> cred,
std::string client_info,
std::string service_configuration,
std::string target) {
grpc::ChannelArguments channel_arguments;
channel_arguments.SetServiceConfigJSON(service_configuration);
BigQueryReadClient *client = new BigQueryReadClient(
grpc::CreateCustomChannel(target, cred, channel_arguments)
);
client->SetClientInfo(client_info);
Rcpp::XPtr<BigQueryReadClient> ptr(client, true);
return ptr;
}
// [[Rcpp::export(rng=false)]]
SEXP bqs_client(std::string client_info,
std::string service_configuration,
std::string refresh_token = "",
std::string access_token = "",
std::string root_certificate = "",
std::string target = "bigquerystorage.googleapis.com:443") {
std::shared_ptr<grpc::ChannelCredentials> cred;
if (!refresh_token.empty()) {
cred = bqs_refresh_token_credentials(refresh_token,
readfile(root_certificate));
}
if (!cred && !access_token.empty()) {
cred = bqs_access_token_credentials(access_token,
readfile(root_certificate));
}
if (!cred) {
cred = bqs_google_credentials();
}
if (!cred) {
Rcpp::stop("Could not create credentials.");
}
return bqs_read_client(cred, client_info,
readfile(service_configuration), target);
}
// [[Rcpp::export(rng=false)]]
SEXP bqs_ipc_stream(SEXP client,
std::string project,
std::string dataset,
std::string table,
std::string parent,
std::int64_t n,
std::vector<std::string> selected_fields,
std::string row_restriction = "",
std::int64_t timestamp_seconds = 0,
std::int32_t timestamp_nanos = 0,
bool quiet = false) {
Rcpp::XPtr<BigQueryReadClient> client_ptr(client);
std::vector<uint8_t> schema;
std::vector<uint8_t> ipc_stream;
long int rows_count = 0;
long int pages_count = 0;
// Retrieve ReadSession
ReadSession read_session = client_ptr->CreateReadSession(
project,
dataset,
table,
parent,
timestamp_seconds,
timestamp_nanos,
selected_fields,
row_restriction);
// Add schema to IPC stream
to_raw(read_session.arrow_schema().serialized_schema(), &schema);
RProgress::RProgress pb(
"\033[42m\033[30mStreaming (:percent)\033[39m\033[49m [:bar] eta[:eta|:elapsed] throt[:extra]");
pb.set_cursor_char(">");
if (n > 0) {
pb.set_total(n);
} else {
pb.set_total(100 * read_session.streams_size());
}
// Add batches to IPC stream
for (int i = 0; i < read_session.streams_size(); i++) {
client_ptr->ReadRows(read_session.streams(i).name(), &ipc_stream,
n, rows_count, pages_count, quiet,
&pb, i == read_session.streams_size() - 1);
}
if (!quiet) {
REprintf("Streamed %ld rows in %ld messages.\n", rows_count, pages_count);
}
// Return stream
return Rcpp::List::create(schema, ipc_stream);
}