[PH2][Refactor][FunctionSplit][chttp2_transport.cc] Splitting large function (#37453)

[PH2][Refactor][FunctionSplit][chttp2_transport.cc]

Splitting very large function perform_stream_op_locked into smaller functions.

Adding new functions :

1. send_initial_metadata_locked
2. send_message_locked
3. send_trailing_metadata_locked
4. recv_initial_metadata_locked
5. recv_message_locked
6. recv_trailing_metadata_locked

All the above functions get called from perform_stream_op_locked .

Why was this done?

I ran into test failures while working on this : https://github.com/grpc/grpc/pull/37186/files .
The test failed because the function execute_stream_op was too large. So I had to split the function as well in the same PR (which is messy).
Decided to do the same type of splitting here.

Closes #37453

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/37453 from tanvi-jagtap:split_function 5e06594e81
PiperOrigin-RevId: 663570972
pull/37483/head
Tanvi Jagtap 4 months ago committed by Copybara-Service
parent 3063716cef
commit 44382df0fc
  1. 151
      src/core/ext/transport/chttp2/transport/chttp2_transport.cc

@ -1362,49 +1362,10 @@ static void log_metadata(const grpc_metadata_batch* md_batch, uint32_t id,
}); });
} }
static void perform_stream_op_locked(void* stream_op, static void send_initial_metadata_locked(
grpc_error_handle /*error_ignored*/) { grpc_transport_stream_op_batch* op, grpc_chttp2_stream* s,
grpc_transport_stream_op_batch* op = grpc_transport_stream_op_batch_payload* op_payload,
static_cast<grpc_transport_stream_op_batch*>(stream_op); grpc_chttp2_transport* t, grpc_closure* on_complete) {
grpc_chttp2_stream* s =
static_cast<grpc_chttp2_stream*>(op->handler_private.extra_arg);
grpc_transport_stream_op_batch_payload* op_payload = op->payload;
grpc_chttp2_transport* t = s->t.get();
s->traced = op->is_traced;
if (!grpc_core::IsCallTracerInTransportEnabled()) {
s->call_tracer = CallTracerIfSampled(s);
}
s->tcp_tracer = TcpTracerIfSampled(s);
if (GRPC_TRACE_FLAG_ENABLED(http)) {
LOG(INFO) << "perform_stream_op_locked[s=" << s << "; op=" << op
<< "]: " << grpc_transport_stream_op_batch_string(op, false)
<< "; on_complete = " << op->on_complete;
if (op->send_initial_metadata) {
log_metadata(op_payload->send_initial_metadata.send_initial_metadata,
s->id, t->is_client, true);
}
if (op->send_trailing_metadata) {
log_metadata(op_payload->send_trailing_metadata.send_trailing_metadata,
s->id, t->is_client, false);
}
}
grpc_closure* on_complete = op->on_complete;
// on_complete will be null if and only if there are no send ops in the batch.
if (on_complete != nullptr) {
// This batch has send ops. Use final_data as a barrier until enqueue time;
// the initial counter is dropped at the end of this function.
on_complete->next_data.scratch = CLOSURE_BARRIER_FIRST_REF_BIT;
on_complete->error_data.error = 0;
}
if (op->cancel_stream) {
grpc_chttp2_cancel_stream(t, s, op_payload->cancel_stream.cancel_error,
op_payload->cancel_stream.tarpit);
}
if (op->send_initial_metadata) {
if (!grpc_core::IsCallTracerInTransportEnabled()) { if (!grpc_core::IsCallTracerInTransportEnabled()) {
if (s->call_tracer != nullptr) { if (s->call_tracer != nullptr) {
s->call_tracer->RecordAnnotation( s->call_tracer->RecordAnnotation(
@ -1414,8 +1375,7 @@ static void perform_stream_op_locked(void* stream_op,
.Add(s->flow_control.stats())); .Add(s->flow_control.stats()));
} }
} else if (grpc_core::IsTraceRecordCallopsEnabled()) { } else if (grpc_core::IsTraceRecordCallopsEnabled()) {
auto* call_tracer = auto* call_tracer = s->arena->GetContext<grpc_core::CallTracerInterface>();
s->arena->GetContext<grpc_core::CallTracerInterface>();
if (call_tracer != nullptr && call_tracer->IsSampled()) { if (call_tracer != nullptr && call_tracer->IsSampled()) {
call_tracer->RecordAnnotation( call_tracer->RecordAnnotation(
grpc_core::HttpAnnotation(grpc_core::HttpAnnotation::Type::kStart, grpc_core::HttpAnnotation(grpc_core::HttpAnnotation::Type::kStart,
@ -1434,8 +1394,8 @@ static void perform_stream_op_locked(void* stream_op,
s->send_initial_metadata = s->send_initial_metadata =
op_payload->send_initial_metadata.send_initial_metadata; op_payload->send_initial_metadata.send_initial_metadata;
if (t->is_client) { if (t->is_client) {
s->deadline = std::min( s->deadline =
s->deadline, std::min(s->deadline,
s->send_initial_metadata->get(grpc_core::GrpcTimeoutMetadata()) s->send_initial_metadata->get(grpc_core::GrpcTimeoutMetadata())
.value_or(grpc_core::Timestamp::InfFuture())); .value_or(grpc_core::Timestamp::InfFuture()));
} }
@ -1481,7 +1441,10 @@ static void perform_stream_op_locked(void* stream_op,
} }
} }
if (op->send_message) { static void send_message_locked(
grpc_transport_stream_op_batch* op, grpc_chttp2_stream* s,
grpc_transport_stream_op_batch_payload* op_payload,
grpc_chttp2_transport* t, grpc_closure* on_complete) {
t->num_messages_in_next_write++; t->num_messages_in_next_write++;
grpc_core::global_stats().IncrementHttp2SendMessageSize( grpc_core::global_stats().IncrementHttp2SendMessageSize(
op->payload->send_message.send_message->Length()); op->payload->send_message.send_message->Length());
@ -1497,8 +1460,8 @@ static void perform_stream_op_locked(void* stream_op,
absl::OkStatus(), absl::OkStatus(),
"fetching_send_message_finished"); "fetching_send_message_finished");
} else { } else {
uint8_t* frame_hdr = grpc_slice_buffer_tiny_add( uint8_t* frame_hdr = grpc_slice_buffer_tiny_add(&s->flow_controlled_buffer,
&s->flow_controlled_buffer, GRPC_HEADER_SIZE_IN_BYTES); GRPC_HEADER_SIZE_IN_BYTES);
frame_hdr[0] = (flags & GRPC_WRITE_INTERNAL_COMPRESS) != 0; frame_hdr[0] = (flags & GRPC_WRITE_INTERNAL_COMPRESS) != 0;
size_t len = op_payload->send_message.send_message->Length(); size_t len = op_payload->send_message.send_message->Length();
frame_hdr[1] = static_cast<uint8_t>(len >> 24); frame_hdr[1] = static_cast<uint8_t>(len >> 24);
@ -1550,16 +1513,18 @@ static void perform_stream_op_locked(void* stream_op,
*list = cb; *list = cb;
} }
if (s->id != 0 && if (s->id != 0 && (!s->write_buffering || s->flow_controlled_buffer.length >
(!s->write_buffering || t->write_buffer_size)) {
s->flow_controlled_buffer.length > t->write_buffer_size)) {
grpc_chttp2_mark_stream_writable(t, s); grpc_chttp2_mark_stream_writable(t, s);
grpc_chttp2_initiate_write(t, GRPC_CHTTP2_INITIATE_WRITE_SEND_MESSAGE); grpc_chttp2_initiate_write(t, GRPC_CHTTP2_INITIATE_WRITE_SEND_MESSAGE);
} }
} }
} }
if (op->send_trailing_metadata) { static void send_trailing_metadata_locked(
grpc_transport_stream_op_batch* op, grpc_chttp2_stream* s,
grpc_transport_stream_op_batch_payload* op_payload,
grpc_chttp2_transport* t, grpc_closure* on_complete) {
CHECK_EQ(s->send_trailing_metadata_finished, nullptr); CHECK_EQ(s->send_trailing_metadata_finished, nullptr);
on_complete->next_data.scratch |= t->closure_barrier_may_cover_write; on_complete->next_data.scratch |= t->closure_barrier_may_cover_write;
s->send_trailing_metadata_finished = add_closure_barrier(on_complete); s->send_trailing_metadata_finished = add_closure_barrier(on_complete);
@ -1589,7 +1554,9 @@ static void perform_stream_op_locked(void* stream_op,
} }
} }
if (op->recv_initial_metadata) { static void recv_initial_metadata_locked(
grpc_chttp2_stream* s, grpc_transport_stream_op_batch_payload* op_payload,
grpc_chttp2_transport* t) {
CHECK_EQ(s->recv_initial_metadata_ready, nullptr); CHECK_EQ(s->recv_initial_metadata_ready, nullptr);
s->recv_initial_metadata_ready = s->recv_initial_metadata_ready =
op_payload->recv_initial_metadata.recv_initial_metadata_ready; op_payload->recv_initial_metadata.recv_initial_metadata_ready;
@ -1603,7 +1570,9 @@ static void perform_stream_op_locked(void* stream_op,
grpc_chttp2_maybe_complete_recv_initial_metadata(t, s); grpc_chttp2_maybe_complete_recv_initial_metadata(t, s);
} }
if (op->recv_message) { static void recv_message_locked(
grpc_chttp2_stream* s, grpc_transport_stream_op_batch_payload* op_payload,
grpc_chttp2_transport* t) {
CHECK_EQ(s->recv_message_ready, nullptr); CHECK_EQ(s->recv_message_ready, nullptr);
s->recv_message_ready = op_payload->recv_message.recv_message_ready; s->recv_message_ready = op_payload->recv_message.recv_message_ready;
s->recv_message = op_payload->recv_message.recv_message; s->recv_message = op_payload->recv_message.recv_message;
@ -1614,7 +1583,9 @@ static void perform_stream_op_locked(void* stream_op,
grpc_chttp2_maybe_complete_recv_trailing_metadata(t, s); grpc_chttp2_maybe_complete_recv_trailing_metadata(t, s);
} }
if (op->recv_trailing_metadata) { static void recv_trailing_metadata_locked(
grpc_chttp2_stream* s, grpc_transport_stream_op_batch_payload* op_payload,
grpc_chttp2_transport* t) {
CHECK_EQ(s->collecting_stats, nullptr); CHECK_EQ(s->collecting_stats, nullptr);
s->collecting_stats = op_payload->recv_trailing_metadata.collect_stats; s->collecting_stats = op_payload->recv_trailing_metadata.collect_stats;
CHECK_EQ(s->recv_trailing_metadata_finished, nullptr); CHECK_EQ(s->recv_trailing_metadata_finished, nullptr);
@ -1626,6 +1597,72 @@ static void perform_stream_op_locked(void* stream_op,
grpc_chttp2_maybe_complete_recv_trailing_metadata(t, s); grpc_chttp2_maybe_complete_recv_trailing_metadata(t, s);
} }
static void perform_stream_op_locked(void* stream_op,
grpc_error_handle /*error_ignored*/) {
grpc_transport_stream_op_batch* op =
static_cast<grpc_transport_stream_op_batch*>(stream_op);
grpc_chttp2_stream* s =
static_cast<grpc_chttp2_stream*>(op->handler_private.extra_arg);
grpc_transport_stream_op_batch_payload* op_payload = op->payload;
grpc_chttp2_transport* t = s->t.get();
s->traced = op->is_traced;
if (!grpc_core::IsCallTracerInTransportEnabled()) {
s->call_tracer = CallTracerIfSampled(s);
}
s->tcp_tracer = TcpTracerIfSampled(s);
if (GRPC_TRACE_FLAG_ENABLED(http)) {
LOG(INFO) << "perform_stream_op_locked[s=" << s << "; op=" << op
<< "]: " << grpc_transport_stream_op_batch_string(op, false)
<< "; on_complete = " << op->on_complete;
if (op->send_initial_metadata) {
log_metadata(op_payload->send_initial_metadata.send_initial_metadata,
s->id, t->is_client, true);
}
if (op->send_trailing_metadata) {
log_metadata(op_payload->send_trailing_metadata.send_trailing_metadata,
s->id, t->is_client, false);
}
}
grpc_closure* on_complete = op->on_complete;
// on_complete will be null if and only if there are no send ops in the batch.
if (on_complete != nullptr) {
// This batch has send ops. Use final_data as a barrier until enqueue time;
// the initial counter is dropped at the end of this function.
on_complete->next_data.scratch = CLOSURE_BARRIER_FIRST_REF_BIT;
on_complete->error_data.error = 0;
}
if (op->cancel_stream) {
grpc_chttp2_cancel_stream(t, s, op_payload->cancel_stream.cancel_error,
op_payload->cancel_stream.tarpit);
}
if (op->send_initial_metadata) {
send_initial_metadata_locked(op, s, op_payload, t, on_complete);
}
if (op->send_message) {
send_message_locked(op, s, op_payload, t, on_complete);
}
if (op->send_trailing_metadata) {
send_trailing_metadata_locked(op, s, op_payload, t, on_complete);
}
if (op->recv_initial_metadata) {
recv_initial_metadata_locked(s, op_payload, t);
}
if (op->recv_message) {
recv_message_locked(s, op_payload, t);
}
if (op->recv_trailing_metadata) {
recv_trailing_metadata_locked(s, op_payload, t);
}
if (on_complete != nullptr) { if (on_complete != nullptr) {
grpc_chttp2_complete_closure_step(t, &on_complete, absl::OkStatus(), grpc_chttp2_complete_closure_step(t, &on_complete, absl::OkStatus(),
"op->on_complete"); "op->on_complete");

Loading…
Cancel
Save