Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 21d13a9

Browse files
authored
Use BatchCreateSessionsRequest to create sessions. (#924)
* Use BatchCreateSessionsRequest to create sessions. Part of #307 * fix return type to work with all compilers. * fix windows by avoiding narrowing. * Fix signed/unsigned and narrowing issues. * Fix the new tests to use BatchCreateSessions Also fixed a typo in a comment/string
1 parent 68fd303 commit 21d13a9

File tree

5 files changed

+196
-235
lines changed

5 files changed

+196
-235
lines changed

google/cloud/spanner/internal/connection_impl.cc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "google/cloud/grpc_utils/grpc_error_delegate.h"
2323
#include "google/cloud/internal/make_unique.h"
2424
#include <google/spanner/v1/spanner.pb.h>
25+
#include <limits>
2526
#include <memory>
2627

2728
namespace google {
@@ -675,25 +676,27 @@ StatusOr<SessionHolder> ConnectionImpl::AllocateSession(
675676
}
676677

677678
StatusOr<std::vector<std::unique_ptr<Session>>> ConnectionImpl::CreateSessions(
678-
size_t /*num_sessions*/) {
679-
// TODO(#307) use BatchCreateSessions. For now, `num_sessions` is ignored.
680-
spanner_proto::CreateSessionRequest request;
679+
int num_sessions) {
680+
spanner_proto::BatchCreateSessionsRequest request;
681681
request.set_database(db_.FullName());
682+
request.set_session_count(std::int32_t{num_sessions});
682683
auto response = RetryLoop(
683684
retry_policy_->clone(), backoff_policy_->clone(), true,
684685
[this](grpc::ClientContext& context,
685-
spanner_proto::CreateSessionRequest const& request) {
686-
return stub_->CreateSession(context, request);
686+
spanner_proto::BatchCreateSessionsRequest const& request) {
687+
return stub_->BatchCreateSessions(context, request);
687688
},
688689
request, __func__);
689690
if (!response) {
690691
return response.status();
691692
}
692-
std::vector<std::unique_ptr<Session>> ret;
693-
ret.reserve(1);
694-
ret.push_back(google::cloud::internal::make_unique<Session>(
695-
std::move(*response->mutable_name())));
696-
return {std::move(ret)};
693+
std::vector<std::unique_ptr<Session>> sessions;
694+
sessions.reserve(response->session_size());
695+
for (auto& session : *response->mutable_session()) {
696+
sessions.push_back(google::cloud::internal::make_unique<Session>(
697+
std::move(*session.mutable_name())));
698+
}
699+
return {std::move(sessions)};
697700
}
698701

699702
} // namespace internal

google/cloud/spanner/internal/connection_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class ConnectionImpl : public Connection,
145145

146146
// `SessionManager` methods; used by the `SessionPool`
147147
StatusOr<std::vector<std::unique_ptr<Session>>> CreateSessions(
148-
size_t num_sessions) override;
148+
int num_sessions) override;
149149

150150
Database db_;
151151
std::shared_ptr<SpannerStub> stub_;

0 commit comments

Comments
 (0)