Skip to content

Commit 347d4a1

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Migrate AllowSkipThriftCow checks to Thrift always-on reflection, structured annotation
Summary: Migrate templates for checking for *allow skip Thrift cow* annotation to use always-on reflection, and look for the equivalent structured annotation. The previous implementation used Thrift legacy reflection to check for an unstructured annotation. Reviewed By: iahs Differential Revision: D90624675 fbshipit-source-id: ee72a666b2de291bdfab4d93da4b4cb21b1a521f
1 parent 31c956b commit 347d4a1

File tree

6 files changed

+40
-82
lines changed

6 files changed

+40
-82
lines changed

fboss/thrift_cow/nodes/BUCK

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ cpp_library(
5252
":node_utils",
5353
":serializer",
5454
"//fatal:fatal",
55+
"//fboss/agent/if:common-cpp2-types",
5556
"//fboss/agent/state:nodebase",
5657
"//fboss/fsdb/if:fsdb_oper-cpp2-types",
5758
"//fboss/thrift_cow/visitors:visitors",
@@ -60,6 +61,7 @@ cpp_library(
6061
"//folly:fbstring",
6162
"//folly/json:dynamic",
6263
"//thrift/lib/cpp2/folly_dynamic:folly_dynamic",
64+
"//thrift/lib/cpp2/gen:module_types_h",
6365
"//thrift/lib/cpp2/protocol:protocol",
6466
"//thrift/lib/cpp2/protocol/detail:protocol_methods",
6567
"//thrift/lib/cpp2/reflection:reflection",

fboss/thrift_cow/nodes/ThriftStructNode-inl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ struct ThriftStructFields : public FieldBaseType {
169169
using Members = typename Info::members;
170170

171171
// Extracting useful common types out of each member via Traits.h
172-
using MemberTypes = fatal::
173-
transform<Members, ExtractStructFields<Derived, EnableHybridStorage>>;
172+
using MemberTypes = fatal::transform<
173+
Members,
174+
ExtractStructFields<TType, Derived, EnableHybridStorage>>;
174175

175176
// type list of members with SkipThriftCow enabled
176177
using MemberTypesWithSkipThriftCow =

fboss/thrift_cow/nodes/Traits.h

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,28 @@
1010

1111
#pragma once
1212

13+
#include <fboss/agent/if/gen-cpp2/common_types.h>
1314
#include <fboss/thrift_cow/nodes/Types.h>
14-
#include <thrift/lib/cpp2/reflection/reflection.h>
15+
#include <thrift/lib/cpp2/gen/module_types_h.h>
1516
#include <type_traits>
1617

1718
namespace facebook::fboss::thrift_cow {
1819

19-
// helper struct to read Thrift annotation allow_skip_thrift_cow
20-
template <typename T, typename T2 = void>
21-
struct read_annotation_allow_skip_thrift_cow {
22-
static constexpr bool value = false;
23-
};
24-
25-
// fatal respsents true and false as
26-
// "constexpr" char sequence of "1" and "0", respectively
27-
using fatal_true = fatal::sequence<char, '1'>;
28-
29-
// need a little template specialization magic since annotation values are void
30-
// when nothing is set. without this we can't try to pull out
31-
// annotation allow_skip_thrift_cow on structs that don't have annotatiosn
32-
template <>
33-
struct read_annotation_allow_skip_thrift_cow<void> {
34-
static constexpr bool value = false;
35-
};
36-
37-
FATAL_S(allow_skip_thrift_cow_annotation, "allow_skip_thrift_cow");
38-
39-
template <typename Annotations>
40-
struct read_annotation_allow_skip_thrift_cow<
41-
Annotations,
42-
typename std::enable_if_t<std::is_same_v<
43-
typename Annotations::keys::allow_skip_thrift_cow,
44-
allow_skip_thrift_cow_annotation>>> {
45-
static constexpr bool value = std::is_same<
46-
typename Annotations::values::allow_skip_thrift_cow,
47-
fatal_true>::value;
48-
};
49-
50-
template <typename TC, typename TType, typename = void>
51-
struct read_type_annotation_allow_skip_thrift_cow {
52-
static constexpr bool value = false;
53-
};
54-
55-
template <typename TC, typename TType>
56-
struct read_type_annotation_allow_skip_thrift_cow<
57-
TC,
58-
TType,
59-
typename std::enable_if_t<
60-
std::is_same_v<TC, apache::thrift::type_class::structure>>> {
61-
using annotations =
62-
typename apache::thrift::reflect_struct<TType>::annotations;
63-
static constexpr bool value =
64-
read_annotation_allow_skip_thrift_cow<annotations>::value;
65-
};
20+
// Concept to check if a Thrift field is annotated with AllowSkipThriftCow at
21+
// compile time
22+
template <typename TStruct, typename FieldId>
23+
concept field_allow_skip_thrift_cow = apache::thrift::is_thrift_class_v<
24+
TStruct> &&
25+
apache::thrift::has_field_annotation<facebook::fboss::AllowSkipThriftCow,
26+
TStruct,
27+
FieldId>();
28+
29+
// Concept to check if a Thrift type is annotated with AllowSkipThriftCow at
30+
// compile time
31+
template <typename TType>
32+
concept type_allow_skip_thrift_cow = apache::thrift::is_thrift_class_v<TType> &&
33+
apache::thrift::
34+
has_struct_annotation<facebook::fboss::AllowSkipThriftCow, TType>();
6635

6736
template <typename TType, bool EnableHybridStorage = false>
6837
struct ThriftStructResolver {
@@ -125,10 +94,7 @@ struct ConvertToNodeTraits<
12594
apache::thrift::type_class::structure,
12695
TType> {
12796
static constexpr bool skipThriftCow =
128-
EnableHybridStorage &&
129-
read_type_annotation_allow_skip_thrift_cow<
130-
apache::thrift::type_class::structure,
131-
TType>::value;
97+
EnableHybridStorage && type_allow_skip_thrift_cow<TType>;
13298
using default_type = std::conditional_t<
13399
skipThriftCow,
134100
ThriftHybridNode<apache::thrift::type_class::structure, TType>,
@@ -232,19 +198,25 @@ struct ConvertToImmutableNodeTraits {
232198
template <typename Derived, typename Name>
233199
struct ResolveMemberType : std::false_type {};
234200

235-
template <typename Derived, typename Member, bool EnableHybridStorage>
201+
template <
202+
typename Struct,
203+
typename Derived,
204+
typename Member,
205+
bool EnableHybridStorage>
236206
struct StructMemberTraits {
237207
using member = Member;
238-
using traits = StructMemberTraits<Derived, Member, EnableHybridStorage>;
208+
using traits =
209+
StructMemberTraits<Struct, Derived, Member, EnableHybridStorage>;
239210
using name = typename Member::name;
240211
using ttype = typename Member::type;
241212
using tc = typename Member::type_class;
242213

243214
// read member annotations
244-
using member_annotations = typename Member::annotations;
245215
static constexpr bool allowSkipThriftCow = EnableHybridStorage &&
246-
(read_annotation_allow_skip_thrift_cow<member_annotations>::value ||
247-
read_type_annotation_allow_skip_thrift_cow<tc, ttype>::value);
216+
(field_allow_skip_thrift_cow<
217+
Struct,
218+
apache::thrift::field_id<Member::id::value>> ||
219+
type_allow_skip_thrift_cow<ttype>);
248220

249221
// need to resolve here
250222
using default_type = std::conditional_t<
@@ -263,10 +235,10 @@ struct StructMemberTraits {
263235
default_type>;
264236
};
265237

266-
template <typename Derived, bool EnableHybridStorage>
238+
template <typename Struct, typename Derived, bool EnableHybridStorage>
267239
struct ExtractStructFields {
268240
template <typename T>
269-
using apply = StructMemberTraits<Derived, T, EnableHybridStorage>;
241+
using apply = StructMemberTraits<Struct, Derived, T, EnableHybridStorage>;
270242
};
271243

272244
template <typename Member, bool EnableHybridStorage = false>

fboss/thrift_cow/nodes/tests/BUCK

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ cpp_unittest(
3232
"//fboss/thrift_cow/nodes:serializer",
3333
"//thrift/lib/cpp2/folly_dynamic:folly_dynamic",
3434
"//thrift/lib/cpp2/protocol:protocol",
35-
"//thrift/lib/cpp2/reflection:reflection",
36-
"//thrift/lib/cpp2/reflection:testing",
3735
],
3836
)
3937

fboss/thrift_cow/nodes/tests/ThriftHybridStructNodeTests.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
#include <thrift/lib/cpp2/folly_dynamic/folly_dynamic.h>
1212
#include <thrift/lib/cpp2/protocol/Serializer.h>
13-
#include <thrift/lib/cpp2/reflection/reflection.h>
14-
#include <thrift/lib/cpp2/reflection/testing.h>
1513
#include "fboss/thrift_cow/nodes/Serializer.h"
1614
#include "fboss/thrift_cow/nodes/Types.h"
1715
#include "fboss/thrift_cow/nodes/tests/gen-cpp2/test_fatal_types.h"
@@ -27,10 +25,8 @@ using k = test_tags::strings;
2725

2826
template <typename ThriftType, bool EnableHybridStorage>
2927
struct is_allow_skip_thrift_cow {
30-
using annotations = apache::thrift::reflect_struct<ThriftType>::annotations;
3128
static constexpr bool value =
32-
read_annotation_allow_skip_thrift_cow<annotations>::value &&
33-
EnableHybridStorage;
29+
type_allow_skip_thrift_cow<ThriftType> && EnableHybridStorage;
3430
};
3531

3632
struct TestParams {

fboss/thrift_cow/nodes/tests/ThriftStructNodeTests.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include <fboss/thrift_cow/visitors/tests/VisitorTestUtils.h>
1212
#include <thrift/lib/cpp2/folly_dynamic/folly_dynamic.h>
1313
#include <thrift/lib/cpp2/protocol/Serializer.h>
14-
#include <thrift/lib/cpp2/reflection/reflection.h>
15-
#include <thrift/lib/cpp2/reflection/testing.h>
1614
#include "fboss/fsdb/if/gen-cpp2/fsdb_oper_types.h"
1715
#include "fboss/thrift_cow/nodes/Serializer.h"
1816
#include "fboss/thrift_cow/nodes/Types.h"
@@ -38,18 +36,9 @@ cfg::L4PortRange buildPortRange(int min, int max) {
3836
} // namespace
3937

4038
TEST(ThriftStructNodeTests, ReadThriftStructAnnotation) {
41-
static_assert(
42-
read_type_annotation_allow_skip_thrift_cow<
43-
apache::thrift::type_class::structure,
44-
TestStruct3>::value == true);
45-
static_assert(
46-
read_type_annotation_allow_skip_thrift_cow<
47-
apache::thrift::type_class::structure,
48-
ParentTestStruct>::value == false);
49-
static_assert(
50-
read_type_annotation_allow_skip_thrift_cow<
51-
apache::thrift::type_class::integral,
52-
int>::value == false);
39+
static_assert(type_allow_skip_thrift_cow<TestStruct3> == true);
40+
static_assert(type_allow_skip_thrift_cow<ParentTestStruct> == false);
41+
static_assert(type_allow_skip_thrift_cow<int> == false);
5342
}
5443

5544
TEST(ThriftStructNodeTests, ThriftStructFieldsSimple) {

0 commit comments

Comments
 (0)