Skip to content

Commit

Permalink
Rust provide method metadata
Browse files Browse the repository at this point in the history
Summary:
In order to "properly" migrate from ThreadManager to ResourcePools in the underlying C++ thrift server, we need to generate method metadata maps.

nhawkes got started with this diff and made great progress, but I have the bandwidth to put the rest of it together, so I'm taking over the diff.

So instead of pushing ALL of the MethodMetadata generation to the codegenerated code, I'm just pushing the following fields

1. RpcKind
2. InteractionType
3. Name
4. Starts-interaction
5. Interaction name

The others, we want 100% owned by Rust Thrift's srserver implementation (i.e. ExecutorType). These are not something Rust thrift services should codegen, or have any control of in the code generation phases as they are configurable when setting up a thrift server.

NOTE: Overriding the thrift request priority MAY come at a future date, which comes through annotations. Presently it is NOT supported.

Also: Regenerating the test-fixtures with `cd ~/fbsource/fbcode && buck run fbcode//thrift/compiler/test:build_fixtures`

Reviewed By: tlj77

Differential Revision:
D65489886

Privacy Context Container: L1122763

fbshipit-source-id: c54f8a3acbe6af3ee39e944bf75ac3f546000257
  • Loading branch information
slawlor authored and facebook-github-bot committed Nov 12, 2024
1 parent df63de4 commit 3a374b2
Show file tree
Hide file tree
Showing 20 changed files with 1,179 additions and 226 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{{!
Copyright (c) Meta Platforms, Inc. and affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}}
// From {{service:program_name}}.{{service:name}}:
{{^service:interaction?}}
{{#service:rustFunctions}}
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
{{#function:stream?}}
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_STREAMING_RESPONSE,
{{/function:stream?}}
{{^function:stream?}}{{#function:sink?}}
rpc_kind: ::fbthrift::processor::RpcKind::SINK,
{{/function:sink?}}{{/function:stream?}}
{{^function:stream?}}{{^function:sink?}}
{{#function:return_type}}{{#type:void?}}
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
{{/type:void?}}{{^type:void?}}
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
{{/type:void?}}{{/function:return_type}}
{{/function:sink?}}{{/function:stream?}}
name: "{{function:name}}",
starts_interaction: {{#function:starts_interaction?}}true{{/function:starts_interaction?}}{{^function:starts_interaction?}}false{{/function:starts_interaction?}},
interaction_name: {{#function:starts_interaction?}}Some("{{service:name}}"){{/function:starts_interaction?}}{{^function:starts_interaction?}}None{{/function:starts_interaction?}},
},
{{/service:rustFunctions}}
{{#service:interactions}}{{#service:rustFunctions}}
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::InteractionV1,
{{#function:stream?}}
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_STREAMING_RESPONSE,
{{/function:stream?}}
{{^function:stream?}}{{#function:sink?}}
rpc_kind: ::fbthrift::processor::RpcKind::SINK,
{{/function:sink?}}{{/function:stream?}}
{{^function:stream?}}{{^function:sink?}}
{{#function:return_type}}{{#type:void?}}
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
{{/type:void?}}{{^type:void?}}
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
{{/type:void?}}{{/function:return_type}}
{{/function:sink?}}{{/function:stream?}}
name: "{{service:name}}.{{function:name}}",
starts_interaction: false,
interaction_name: Some("{{service:name}}"),
},
{{/service:rustFunctions}}{{/service:interactions}}
{{/service:interaction?}}
{{#service:interaction?}}
// Interaction's method names are never queried directly.
// They are always queried from the "main" processor.
{{/service:interaction?}}
{{#service:extends}}

{{>lib/method_metadata}}
{{/service:extends}}
34 changes: 0 additions & 34 deletions thrift/compiler/generate/templates/rust/lib/method_names.mustache

This file was deleted.

4 changes: 2 additions & 2 deletions thrift/compiler/generate/templates/rust/lib/server.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@ where
self.handle_create_interaction(idx)
}

fn get_method_names(&self) -> &'static [&'static ::std::primitive::str] {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
{{>lib/method_names}}
{{>lib/method_metadata}}
]
}

Expand Down
28 changes: 23 additions & 5 deletions thrift/compiler/test/fixtures/adapter/out/rust/gen-rust/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,16 @@ where
self.handle_create_interaction(idx)
}

fn get_method_names(&self) -> &'static [&'static ::std::primitive::str] {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
// From module.Service:
"func",
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
name: "func",
starts_interaction: false,
interaction_name: None,
},
]
}

Expand Down Expand Up @@ -825,11 +831,23 @@ where
self.handle_create_interaction(idx)
}

fn get_method_names(&self) -> &'static [&'static ::std::primitive::str] {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
// From module.AdapterService:
"count",
"adaptedTypes",
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
name: "count",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
name: "adaptedTypes",
starts_interaction: false,
interaction_name: None,
},
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1158,16 +1158,58 @@ where
self.handle_create_interaction(idx)
}

fn get_method_names(&self) -> &'static [&'static ::std::primitive::str] {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
// From module.MyService:
"ping",
"getRandomData",
"hasDataById",
"getDataById",
"putDataById",
"lobDataById",
"doNothing",
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "ping",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
name: "getRandomData",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
name: "hasDataById",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
name: "getDataById",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "putDataById",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "lobDataById",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "doNothing",
starts_interaction: false,
interaction_name: None,
},
]
}

Expand Down Expand Up @@ -1619,11 +1661,23 @@ where
self.handle_create_interaction(idx)
}

fn get_method_names(&self) -> &'static [&'static ::std::primitive::str] {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
// From module.MyServicePrioParent:
"ping",
"pong",
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "ping",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "pong",
starts_interaction: false,
interaction_name: None,
},
]
}

Expand Down Expand Up @@ -1970,14 +2024,32 @@ where
self.handle_create_interaction(idx)
}

fn get_method_names(&self) -> &'static [&'static ::std::primitive::str] {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
// From module.MyServicePrioChild:
"pang",
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "pang",
starts_interaction: false,
interaction_name: None,
},

// From module.MyServicePrioParent:
"ping",
"pong",
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "ping",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "pong",
starts_interaction: false,
interaction_name: None,
},
]
}

Expand Down Expand Up @@ -2265,7 +2337,7 @@ pub mod bad_service {
self.handle_create_interaction(idx)
}

fn get_method_names(&self) -> &'static [&'static ::std::primitive::str] {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
// From module.BadInteraction:
// Interaction's method names are never queried directly.
Expand Down Expand Up @@ -2606,11 +2678,30 @@ where
self.handle_create_interaction(idx)
}

fn get_method_names(&self) -> &'static [&'static ::std::primitive::str] {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
// From module.BadService:
"bar",
"BadInteraction.foo",
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
name: "createBadInteraction",
starts_interaction: true,
interaction_name: Some("BadService"),
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_SINGLE_RESPONSE,
name: "bar",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::InteractionV1,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "BadInteraction.foo",
starts_interaction: false,
interaction_name: Some("BadInteraction"),
},
]
}

Expand Down Expand Up @@ -3174,12 +3265,30 @@ where
self.handle_create_interaction(idx)
}

fn get_method_names(&self) -> &'static [&'static ::std::primitive::str] {
fn get_method_metadata(&self) -> &'static [::fbthrift::processor::MethodMetadata] {
&[
// From module.FooBarBazService:
"foo",
"bar",
"baz",
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "foo",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "bar",
starts_interaction: false,
interaction_name: None,
},
::fbthrift::processor::MethodMetadata{
interaction_type: ::fbthrift::processor::InteractionType::None,
rpc_kind: ::fbthrift::processor::RpcKind::SINGLE_REQUEST_NO_RESPONSE,
name: "baz",
starts_interaction: false,
interaction_name: None,
},
]
}

Expand Down
Loading

0 comments on commit 3a374b2

Please sign in to comment.