Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions tpu_sync/transport/block_transport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1015,15 +1015,15 @@ absl::Status BlockTransport::ProcessSocketPush(
const uint8_t major_order = first.major_order;
const size_t block_count = static_cast<size_t>(count_or_size);

auto status_or_fd = raw_transport_.conn_pool().Borrow(peer, local_ip);
if (!status_or_fd.ok()) {
return status_or_fd.status();
auto borrowed_fd = raw_transport_.BorrowConnection(peer, local_ip);
if (!borrowed_fd.ok()) {
return borrowed_fd.status();
}

const int fd = status_or_fd.value();
const int fd = borrowed_fd.value();
bool ok_to_pool = false;
auto fd_cleaner = absl::MakeCleanup([&] {
raw_transport_.conn_pool().Return(ok_to_pool, fd, peer, local_ip);
raw_transport_.ReturnConnection(ok_to_pool, fd, peer, local_ip);
});

lib::ChunkHeader header = {};
Expand Down Expand Up @@ -1154,16 +1154,16 @@ void BlockTransport::H2hReadWorker(
const std::vector<uint8_t*>& explicit_dst_ptrs,
std::vector<absl::Status>& statuses, MajorOrder major_order,
BlockReceivedCallback on_block_received, uint64_t uuid) {
auto status_or_fd = raw_transport_.conn_pool().Borrow(peer, local_ip);
if (!status_or_fd.ok()) {
statuses[stream_idx] = status_or_fd.status();
auto borrowed_fd = raw_transport_.BorrowConnection(peer, local_ip);
if (!borrowed_fd.ok()) {
statuses[stream_idx] = borrowed_fd.status();
return;
}

const int fd = status_or_fd.value();
const int fd = borrowed_fd.value();
bool ok_to_pool = false;
auto fd_cleaner = absl::MakeCleanup([&] {
raw_transport_.conn_pool().Return(ok_to_pool, fd, peer, local_ip);
raw_transport_.ReturnConnection(ok_to_pool, fd, peer, local_ip);
});

size_t SF = block_delegate_->shard_factor();
Expand Down
1 change: 1 addition & 0 deletions tpu_sync/transport/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ cc_library(
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/types:span",
Expand Down
12 changes: 6 additions & 6 deletions tpu_sync/transport/lib/raw_buffer_transport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,10 @@ absl::Status RawBufferTransport::PullBuffer(
", Size: ", size_bytes, ", Shard Host Size: ", host_size));
}

ASSIGN_OR_RETURN(const int fd, conn_pool_.Borrow(peer));
ASSIGN_OR_RETURN(const int fd, BorrowConnection(peer));
bool ok_to_pool = false;
auto fd_cleaner =
absl::MakeCleanup([&] { conn_pool_.Return(ok_to_pool, fd, peer); });
absl::MakeCleanup([&] { ReturnConnection(ok_to_pool, fd, peer); });

ChunkHeader header = {};
header.version = 1;
Expand Down Expand Up @@ -565,10 +565,10 @@ absl::Status RawBufferTransport::ProcessSocketBufferPush(
"Destination peer address cannot be empty");
}

ASSIGN_OR_RETURN(const int fd, conn_pool_.Borrow(peer));
ASSIGN_OR_RETURN(const int fd, BorrowConnection(peer));
bool ok_to_pool = false;
auto fd_cleaner =
absl::MakeCleanup([&] { conn_pool_.Return(ok_to_pool, fd, peer); });
absl::MakeCleanup([&] { ReturnConnection(ok_to_pool, fd, peer); });

const uint8_t opcode = request.socket_opcode;
const uint64_t uuid = request.uuid;
Expand Down Expand Up @@ -722,10 +722,10 @@ absl::Status RawBufferTransport::PushBatch(
"Destination peer address cannot be empty");
}

ASSIGN_OR_RETURN(const int fd, conn_pool_.Borrow(peer));
ASSIGN_OR_RETURN(const int fd, BorrowConnection(peer));
bool ok_to_pool = false;
auto fd_cleaner =
absl::MakeCleanup([&] { conn_pool_.Return(ok_to_pool, fd, peer); });
absl::MakeCleanup([&] { ReturnConnection(ok_to_pool, fd, peer); });

ChunkHeader header = {};
header.version = 1;
Expand Down
14 changes: 12 additions & 2 deletions tpu_sync/transport/lib/raw_buffer_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "absl/container/flat_hash_set.h"
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/span.h"
Expand Down Expand Up @@ -75,8 +76,17 @@ class RawBufferTransport final {
// Return the local IP addresses.
absl::Span<const std::string> local_ips() const { return local_ips_; }

// Return the connection pool that manages the sockets that connect to peers.
ConnPool& conn_pool() { return conn_pool_; }
// Borrows a connection from the connection pool.
absl::StatusOr<int> BorrowConnection(absl::string_view peer,
absl::string_view local_ip = "") {
return conn_pool_.Borrow(peer, local_ip);
}

// Returns a connection to the connection pool.
void ReturnConnection(bool ok_to_pool, int fd, absl::string_view peer,
absl::string_view local_ip = "") {
conn_pool_.Return(ok_to_pool, fd, peer, local_ip);
}

// Synchronously pulls a buffer identified by `buffer_id` from the remote
// `peer`, by sending out a `kOpBufferPull ChunkHeader` and then receiving
Expand Down
Loading