|
21 | 21 | #include "google/cloud/spanner/read_partition.h" |
22 | 22 | #include "google/cloud/grpc_utils/grpc_error_delegate.h" |
23 | 23 | #include "google/cloud/internal/make_unique.h" |
24 | | -#include <google/spanner/v1/spanner.pb.h> |
25 | 24 | #include <limits> |
26 | | -#include <memory> |
27 | 25 |
|
28 | 26 | namespace google { |
29 | 27 | namespace cloud { |
@@ -111,7 +109,8 @@ ConnectionImpl::ConnectionImpl(Database db, std::shared_ptr<SpannerStub> stub, |
111 | 109 | stub_(std::move(stub)), |
112 | 110 | retry_policy_(std::move(retry_policy)), |
113 | 111 | backoff_policy_(std::move(backoff_policy)), |
114 | | - session_pool_(this) {} |
| 112 | + session_pool_(std::make_shared<SessionPool>( |
| 113 | + db_, stub_, retry_policy_->clone(), backoff_policy_->clone())) {} |
115 | 114 |
|
116 | 115 | RowStream ConnectionImpl::Read(ReadParams params) { |
117 | 116 | return internal::Visit( |
@@ -286,7 +285,7 @@ RowStream ConnectionImpl::ReadImpl(SessionHolder& session, |
286 | 285 | spanner_proto::TransactionSelector& s, |
287 | 286 | ReadParams params) { |
288 | 287 | if (!session) { |
289 | | - auto session_or = AllocateSession(); |
| 288 | + auto session_or = session_pool_->Allocate(); |
290 | 289 | if (!session_or) { |
291 | 290 | return RowStream( |
292 | 291 | google::cloud::internal::make_unique<StatusOnlyResultSetSource>( |
@@ -348,7 +347,7 @@ StatusOr<std::vector<ReadPartition>> ConnectionImpl::PartitionReadImpl( |
348 | 347 | if (!session) { |
349 | 348 | // Since the session may be sent to other machines, it should not be |
350 | 349 | // returned to the pool when the Transaction is destroyed. |
351 | | - auto session_or = AllocateSession(/*dissociate_from_pool=*/true); |
| 350 | + auto session_or = session_pool_->Allocate(/*dissociate_from_pool=*/true); |
352 | 351 | if (!session_or) { |
353 | 352 | return std::move(session_or).status(); |
354 | 353 | } |
@@ -401,7 +400,7 @@ StatusOr<ResultType> ConnectionImpl::ExecuteSqlImpl( |
401 | 400 | google::spanner::v1 ::ExecuteSqlRequest& request)> const& |
402 | 401 | retry_resume_fn) { |
403 | 402 | if (!session) { |
404 | | - auto session_or = AllocateSession(); |
| 403 | + auto session_or = session_pool_->Allocate(); |
405 | 404 | if (!session_or) { |
406 | 405 | return std::move(session_or).status(); |
407 | 406 | } |
@@ -560,7 +559,7 @@ StatusOr<std::vector<QueryPartition>> ConnectionImpl::PartitionQueryImpl( |
560 | 559 | if (!session) { |
561 | 560 | // Since the session may be sent to other machines, it should not be |
562 | 561 | // returned to the pool when the Transaction is destroyed. |
563 | | - auto session_or = AllocateSession(/*dissociate_from_pool=*/true); |
| 562 | + auto session_or = session_pool_->Allocate(/*dissociate_from_pool=*/true); |
564 | 563 | if (!session_or) { |
565 | 564 | return std::move(session_or).status(); |
566 | 565 | } |
@@ -606,7 +605,7 @@ StatusOr<BatchDmlResult> ConnectionImpl::ExecuteBatchDmlImpl( |
606 | 605 | SessionHolder& session, spanner_proto::TransactionSelector& s, |
607 | 606 | std::int64_t seqno, ExecuteBatchDmlParams params) { |
608 | 607 | if (!session) { |
609 | | - auto session_or = AllocateSession(); |
| 608 | + auto session_or = session_pool_->Allocate(); |
610 | 609 | if (!session_or) { |
611 | 610 | return std::move(session_or).status(); |
612 | 611 | } |
@@ -649,7 +648,7 @@ StatusOr<PartitionedDmlResult> ConnectionImpl::ExecutePartitionedDmlImpl( |
649 | 648 | SessionHolder& session, spanner_proto::TransactionSelector& s, |
650 | 649 | std::int64_t seqno, ExecutePartitionedDmlParams params) { |
651 | 650 | if (!session) { |
652 | | - auto session_or = AllocateSession(); |
| 651 | + auto session_or = session_pool_->Allocate(); |
653 | 652 | if (!session_or) { |
654 | 653 | return std::move(session_or).status(); |
655 | 654 | } |
@@ -703,7 +702,7 @@ StatusOr<CommitResult> ConnectionImpl::CommitImpl( |
703 | 702 | SessionHolder& session, spanner_proto::TransactionSelector& s, |
704 | 703 | CommitParams params) { |
705 | 704 | if (!session) { |
706 | | - auto session_or = AllocateSession(); |
| 705 | + auto session_or = session_pool_->Allocate(); |
707 | 706 | if (!session_or) { |
708 | 707 | return std::move(session_or).status(); |
709 | 708 | } |
@@ -743,7 +742,7 @@ StatusOr<CommitResult> ConnectionImpl::CommitImpl( |
743 | 742 | Status ConnectionImpl::RollbackImpl(SessionHolder& session, |
744 | 743 | spanner_proto::TransactionSelector& s) { |
745 | 744 | if (!session) { |
746 | | - auto session_or = AllocateSession(); |
| 745 | + auto session_or = session_pool_->Allocate(); |
747 | 746 | if (!session_or) { |
748 | 747 | return std::move(session_or).status(); |
749 | 748 | } |
@@ -771,55 +770,6 @@ Status ConnectionImpl::RollbackImpl(SessionHolder& session, |
771 | 770 | request, __func__); |
772 | 771 | } |
773 | 772 |
|
774 | | -StatusOr<SessionHolder> ConnectionImpl::AllocateSession( |
775 | | - bool dissociate_from_pool) { |
776 | | - auto session = session_pool_.Allocate(dissociate_from_pool); |
777 | | - if (!session.ok()) { |
778 | | - return std::move(session).status(); |
779 | | - } |
780 | | - |
781 | | - if (dissociate_from_pool) { |
782 | | - // Uses the default deleter; the Session is not returned to the pool. |
783 | | - return {*std::move(session)}; |
784 | | - } |
785 | | - |
786 | | - std::weak_ptr<ConnectionImpl> connection = shared_from_this(); |
787 | | - return SessionHolder(session->release(), [connection](Session* session) { |
788 | | - auto shared_connection = connection.lock(); |
789 | | - // If `connection` is still alive, release the `Session` to its pool; |
790 | | - // otherwise just delete the `Session`. |
791 | | - if (shared_connection) { |
792 | | - shared_connection->ReleaseSession(session); |
793 | | - } else { |
794 | | - delete session; |
795 | | - } |
796 | | - }); |
797 | | -} |
798 | | - |
799 | | -StatusOr<std::vector<std::unique_ptr<Session>>> ConnectionImpl::CreateSessions( |
800 | | - int num_sessions) { |
801 | | - spanner_proto::BatchCreateSessionsRequest request; |
802 | | - request.set_database(db_.FullName()); |
803 | | - request.set_session_count(std::int32_t{num_sessions}); |
804 | | - auto response = RetryLoop( |
805 | | - retry_policy_->clone(), backoff_policy_->clone(), true, |
806 | | - [this](grpc::ClientContext& context, |
807 | | - spanner_proto::BatchCreateSessionsRequest const& request) { |
808 | | - return stub_->BatchCreateSessions(context, request); |
809 | | - }, |
810 | | - request, __func__); |
811 | | - if (!response) { |
812 | | - return response.status(); |
813 | | - } |
814 | | - std::vector<std::unique_ptr<Session>> sessions; |
815 | | - sessions.reserve(response->session_size()); |
816 | | - for (auto& session : *response->mutable_session()) { |
817 | | - sessions.push_back(google::cloud::internal::make_unique<Session>( |
818 | | - std::move(*session.mutable_name()))); |
819 | | - } |
820 | | - return {std::move(sessions)}; |
821 | | -} |
822 | | - |
823 | 773 | } // namespace internal |
824 | 774 | } // namespace SPANNER_CLIENT_NS |
825 | 775 | } // namespace spanner |
|
0 commit comments