Fix reusing the cached hash of nil ID (#7753)

This commit is contained in:
Kai Yang
2020-03-27 23:40:03 +08:00
committed by GitHub
parent c195dc8f88
commit 6a3503c494
3 changed files with 12 additions and 2 deletions
+1 -1
View File
@@ -372,7 +372,7 @@ ObjectID ObjectID::GenerateObjectId(const std::string &task_id_binary,
ObjectIDFlagsType flags,
ObjectIDIndexType object_index) {
RAY_CHECK(task_id_binary.size() == TaskID::Size());
ObjectID ret = ObjectID::Nil();
ObjectID ret;
std::memcpy(ret.id_, task_id_binary.c_str(), TaskID::kLength);
std::memcpy(ret.id_ + TaskID::kLength, &flags, sizeof(flags));
std::memcpy(ret.id_ + TaskID::kLength + kFlagsBytesLength, &object_index,
+1 -1
View File
@@ -455,7 +455,7 @@ template <typename T>
T BaseID<T>::FromBinary(const std::string &binary) {
RAY_CHECK(binary.size() == T::Size() || binary.size() == 0)
<< "expected size is " << T::Size() << ", but got " << binary.size();
T t = T::Nil();
T t;
std::memcpy(t.MutableData(), binary.data(), binary.size());
return t;
}
+10
View File
@@ -113,6 +113,16 @@ TEST(NilTest, TestIsNil) {
ASSERT_TRUE(ObjectID::Nil().IsNil());
}
TEST(HashTest, TestNilHash) {
// Manually trigger the hash calculation of the static global nil ID.
auto nil_hash = ObjectID::Nil().Hash();
ObjectID id1 = ObjectID::FromRandom();
ASSERT_NE(nil_hash, id1.Hash());
ObjectID id2 = ObjectID::FromBinary(ObjectID::FromRandom().Binary());
ASSERT_NE(nil_hash, id2.Hash());
ASSERT_NE(id1.Hash(), id2.Hash());
}
} // namespace ray
int main(int argc, char **argv) {