Skip to content

Commit ed44616

Browse files
authored
cleanup(pubsublite): have routing policy return std::uint32_t (#8675)
1 parent 48af7d1 commit ed44616

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

google/cloud/pubsublite/internal/default_routing_policy.cc

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/pubsublite/internal/default_routing_policy.h"
16+
#include "google/cloud/internal/sha256_hash.h"
1617
#include "google/cloud/version.h"
1718
#include <unordered_map>
1819

@@ -21,10 +22,13 @@ namespace cloud {
2122
namespace pubsublite_internal {
2223
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2324

25+
// will always be 32 as specified in sha256_hash.h
26+
std::uint32_t constexpr kNumBytesSha256 = 32;
27+
2428
// Uses the identity that `(a*b) % m == ((a % m) * (b % m)) % m`
25-
std::uint64_t ModPow(std::uint64_t val, std::uint64_t pow, std::uint32_t mod) {
29+
std::uint64_t ModPow(std::uint64_t val, std::uint32_t pow, std::uint32_t mod) {
2630
std::uint64_t result = 1;
27-
for (std::uint32_t i = 0; i < pow; ++i) {
31+
for (std::uint32_t i = 0; i != pow; ++i) {
2832
result *= (val % mod);
2933
result %= mod;
3034
}
@@ -33,29 +37,29 @@ std::uint64_t ModPow(std::uint64_t val, std::uint64_t pow, std::uint32_t mod) {
3337

3438
// Uses the identity that `(a*b) % m == ((a % m) * (b % m)) % m`
3539
// Uses the identity that `(a+b) % m == ((a % m) + (b % m)) % m`
36-
std::uint64_t GetMod(std::array<uint8_t, 32> big_endian, std::uint32_t mod) {
40+
std::uint32_t GetMod(std::array<uint8_t, kNumBytesSha256> big_endian,
41+
std::uint32_t mod) {
3742
std::uint64_t result = 0;
38-
for (std::uint64_t i = 0; i < big_endian.size(); ++i) {
39-
std::uint64_t val_mod = big_endian[i] % mod;
43+
for (std::uint32_t i = 0; i != kNumBytesSha256; ++i) {
44+
std::uint32_t val_mod = big_endian[i] % mod;
4045

41-
std::uint64_t pow = big_endian.size() - (i + 1);
42-
std::uint64_t pow_mod = ModPow(
43-
// 2^8
44-
static_cast<std::uint64_t>(1 << 8), pow, mod);
46+
std::uint32_t pow = kNumBytesSha256 - (i + 1);
47+
std::uint64_t pow_mod = ModPow(256, pow, mod);
4548

4649
result += (val_mod * pow_mod) % mod;
4750
result %= mod;
4851
}
49-
return result;
52+
// within bounds because `mod`ed by a std::uint32_t value
53+
return static_cast<std::uint32_t>(result);
5054
}
5155

52-
std::uint64_t DefaultRoutingPolicy::Route(std::uint32_t num_partitions) {
56+
Partition DefaultRoutingPolicy::Route(Partition num_partitions) {
5357
// atomic operation
54-
return counter_++ % num_partitions;
58+
return static_cast<std::uint32_t>(counter_++ % num_partitions);
5559
}
5660

57-
std::uint64_t DefaultRoutingPolicy::Route(std::string const& message_key,
58-
std::uint32_t num_partitions) {
61+
Partition DefaultRoutingPolicy::Route(std::string const& message_key,
62+
Partition num_partitions) {
5963
return GetMod(google::cloud::internal::Sha256Hash(message_key),
6064
num_partitions);
6165
}

google/cloud/pubsublite/internal/default_routing_policy.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_PUBSUBLITE_INTERNAL_DEFAULT_ROUTING_POLICY_H
1717

1818
#include "google/cloud/pubsublite/internal/routing_policy.h"
19-
#include "google/cloud/internal/sha256_hash.h"
2019
#include "google/cloud/version.h"
2120
#include <atomic>
2221
#include <cstddef>
@@ -33,7 +32,7 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3332
std::uint64_t ModPow(std::uint64_t val, std::uint64_t pow, std::uint32_t mod);
3433

3534
// returns <integer value of `big_endian`> % `mod` while accounting for overflow
36-
std::uint64_t GetMod(std::array<uint8_t, 32> big_endian, std::uint32_t mod);
35+
std::uint32_t GetMod(std::array<uint8_t, 32> big_endian, std::uint32_t mod);
3736

3837
/**
3938
* Implements the same routing policy as all the other Pub/Sub Lite client
@@ -49,9 +48,9 @@ std::uint64_t GetMod(std::array<uint8_t, 32> big_endian, std::uint32_t mod);
4948
*/
5049
class DefaultRoutingPolicy : public RoutingPolicy {
5150
public:
52-
std::uint64_t Route(std::uint32_t num_partitions) override;
53-
std::uint64_t Route(std::string const& message_key,
54-
std::uint32_t num_partitions) override;
51+
Partition Route(Partition num_partitions) override;
52+
Partition Route(std::string const& message_key,
53+
Partition num_partitions) override;
5554

5655
private:
5756
std::atomic<std::int64_t> counter_{0};

google/cloud/pubsublite/internal/default_routing_policy_test.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "google/cloud/pubsublite/internal/default_routing_policy.h"
1616
#include <gmock/gmock.h>
17+
#include <array>
1718
#include <unordered_map>
1819

1920
namespace google {
@@ -50,9 +51,9 @@ TEST(DefaultRoutingPolicyTest, RouteWithKey) {
5051
TEST(DefaultRoutingPolicyTest, RouteWithoutKey) {
5152
unsigned int num_partitions = 29;
5253
DefaultRoutingPolicy rp;
53-
uint64_t initial_partition = rp.Route(num_partitions);
54+
std::uint32_t initial_partition = rp.Route(num_partitions);
5455
for (unsigned int i = 0; i < num_partitions; ++i) {
55-
uint64_t next_partition = rp.Route(num_partitions);
56+
std::uint32_t next_partition = rp.Route(num_partitions);
5657
EXPECT_EQ((initial_partition + 1) % num_partitions,
5758
next_partition % num_partitions);
5859
initial_partition = next_partition;
@@ -70,6 +71,7 @@ TEST(TestGetMod, MaxValue) {
7071
EXPECT_EQ(GetMod(arr, 100), 35);
7172
EXPECT_EQ(GetMod(arr, 10023), 5397);
7273
EXPECT_EQ(GetMod(arr, UINT8_MAX), 0);
74+
EXPECT_EQ(GetMod(arr, UINT32_MAX - 1), 255);
7375
}
7476

7577
TEST(TestGetMod, OneLessThanMaxValue) {
@@ -82,6 +84,7 @@ TEST(TestGetMod, OneLessThanMaxValue) {
8284
EXPECT_EQ(GetMod(arr, 100), 34);
8385
EXPECT_EQ(GetMod(arr, 10023), 5396);
8486
EXPECT_EQ(GetMod(arr, UINT8_MAX), 254);
87+
EXPECT_EQ(GetMod(arr, UINT32_MAX - 1), 254);
8588
}
8689

8790
TEST(TestGetMod, Zeros) {
@@ -93,6 +96,7 @@ TEST(TestGetMod, Zeros) {
9396
EXPECT_EQ(GetMod(arr, 100), 0);
9497
EXPECT_EQ(GetMod(arr, 10023), 0);
9598
EXPECT_EQ(GetMod(arr, UINT8_MAX), 0);
99+
EXPECT_EQ(GetMod(arr, UINT32_MAX - 1), 0);
96100
}
97101

98102
TEST(TestGetMod, ArbitraryValue) {
@@ -105,6 +109,7 @@ TEST(TestGetMod, ArbitraryValue) {
105109
EXPECT_EQ(GetMod(arr, 10023), 3346);
106110
EXPECT_EQ(GetMod(arr, 109000), 60390);
107111
EXPECT_EQ(GetMod(arr, UINT8_MAX), 100);
112+
EXPECT_EQ(GetMod(arr, UINT32_MAX - 1), 1136793478);
108113
}
109114

110115
TEST(TestGetMod, ArbitraryValue1) {
@@ -115,6 +120,7 @@ TEST(TestGetMod, ArbitraryValue1) {
115120
EXPECT_EQ(GetMod(arr, 102301), 93535);
116121
EXPECT_EQ(GetMod(arr, 23), 13);
117122
EXPECT_EQ(GetMod(arr, UINT8_MAX), 37);
123+
EXPECT_EQ(GetMod(arr, UINT32_MAX - 1), 3416191692);
118124
}
119125

120126
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

google/cloud/pubsublite/internal/routing_policy.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ namespace cloud {
2222
namespace pubsublite_internal {
2323
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
2424

25+
/**
26+
* A partition should be an integer in the range [0, UINT32_MAX).
27+
*/
28+
using Partition = std::uint32_t;
29+
2530
/**
2631
* Interface for a Pub/Sub Lite routing policy that determines the partition
2732
* that a message should be sent to, either depending on the message's key or
@@ -30,9 +35,9 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
3035
class RoutingPolicy {
3136
public:
3237
virtual ~RoutingPolicy() = default;
33-
virtual std::uint64_t Route(std::uint32_t num_partitions) = 0;
34-
virtual std::uint64_t Route(std::string const& message_key,
35-
std::uint32_t num_partitions) = 0;
38+
virtual Partition Route(Partition num_partitions) = 0;
39+
virtual Partition Route(std::string const& message_key,
40+
Partition num_partitions) = 0;
3641
};
3742

3843
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END

0 commit comments

Comments
 (0)