Fix flaky SimCacheTest.SimCacheLogging (#9373)

Summary:
The random string may contain the string we're checking, e.g.:
```
ADD - 206FBC78E96BC4C6A2DDDDC0AD5D1ADD - 111
```
Only check the line starts-with "ADD -".

Pull Request resolved: https://github.com/facebook/rocksdb/pull/9373

Test Plan: `gtest-parallel ./sim_cache_test --gtest_filter=SimCacheTest.SimCacheLogging -r 1000`

Reviewed By: riversand963

Differential Revision: D33519574

Pulled By: jay-zhuang

fbshipit-source-id: d0c1c9b0b489246d292e7da4133030edaa748099
main
Jay Zhuang 3 years ago committed by Facebook GitHub Bot
parent 55a2105258
commit 6bab278291
  1. 47
      utilities/simulator_cache/sim_cache.cc
  2. 18
      utilities/simulator_cache/sim_cache_test.cc

@ -6,6 +6,7 @@
#include "rocksdb/utilities/sim_cache.h" #include "rocksdb/utilities/sim_cache.h"
#include <atomic> #include <atomic>
#include <iomanip>
#include "file/writable_file_writer.h" #include "file/writable_file_writer.h"
#include "monitoring/statistics.h" #include "monitoring/statistics.h"
@ -13,7 +14,6 @@
#include "rocksdb/env.h" #include "rocksdb/env.h"
#include "rocksdb/file_system.h" #include "rocksdb/file_system.h"
#include "util/mutexlock.h" #include "util/mutexlock.h"
#include "util/string_util.h"
namespace ROCKSDB_NAMESPACE { namespace ROCKSDB_NAMESPACE {
@ -68,11 +68,12 @@ class CacheActivityLogger {
return; return;
} }
std::string log_line = "LOOKUP - " + key.ToString(true) + "\n"; std::ostringstream oss;
// line format: "LOOKUP - <KEY>" // line format: "LOOKUP - <KEY>"
oss << "LOOKUP - " << key.ToString(true) << std::endl;
MutexLock l(&mutex_); MutexLock l(&mutex_);
Status s = file_writer_->Append(log_line); Status s = file_writer_->Append(oss.str());
if (!s.ok() && bg_status_.ok()) { if (!s.ok() && bg_status_.ok()) {
bg_status_ = s; bg_status_ = s;
} }
@ -88,15 +89,11 @@ class CacheActivityLogger {
return; return;
} }
std::string log_line = "ADD - "; std::ostringstream oss;
log_line += key.ToString(true);
log_line += " - ";
AppendNumberTo(&log_line, size);
log_line += "\n";
// line format: "ADD - <KEY> - <KEY-SIZE>" // line format: "ADD - <KEY> - <KEY-SIZE>"
oss << "ADD - " << key.ToString(true) << " - " << size << std::endl;
MutexLock l(&mutex_); MutexLock l(&mutex_);
Status s = file_writer_->Append(log_line); Status s = file_writer_->Append(oss.str());
if (!s.ok() && bg_status_.ok()) { if (!s.ok() && bg_status_.ok()) {
bg_status_ = s; bg_status_ = s;
} }
@ -298,25 +295,23 @@ class SimCacheImpl : public SimCache {
} }
std::string ToString() const override { std::string ToString() const override {
std::string res; std::ostringstream oss;
res.append("SimCache MISSes: " + std::to_string(get_miss_counter()) + "\n"); oss << "SimCache MISSes: " << get_miss_counter() << std::endl;
res.append("SimCache HITs: " + std::to_string(get_hit_counter()) + "\n"); oss << "SimCache HITs: " << get_hit_counter() << std::endl;
char buff[350];
auto lookups = get_miss_counter() + get_hit_counter(); auto lookups = get_miss_counter() + get_hit_counter();
snprintf(buff, sizeof(buff), "SimCache HITRATE: %.2f%%\n", oss << "SimCache HITRATE: " << std::fixed << std::setprecision(2)
(lookups == 0 ? 0 : get_hit_counter() * 100.0f / lookups)); << (lookups == 0 ? 0 : get_hit_counter() * 100.0f / lookups)
res.append(buff); << std::endl;
return res; return oss.str();
} }
std::string GetPrintableOptions() const override { std::string GetPrintableOptions() const override {
std::string ret; std::ostringstream oss;
ret.reserve(20000); oss << " cache_options:" << std::endl;
ret.append(" cache_options:\n"); oss << cache_->GetPrintableOptions();
ret.append(cache_->GetPrintableOptions()); oss << " sim_cache_options:" << std::endl;
ret.append(" sim_cache_options:\n"); oss << key_only_cache_->GetPrintableOptions();
ret.append(key_only_cache_->GetPrintableOptions()); return oss.str();
return ret;
} }
Status StartActivityLogging(const std::string& activity_log_file, Env* env, Status StartActivityLogging(const std::string& activity_log_file, Env* env,

@ -177,23 +177,21 @@ TEST_F(SimCacheTest, SimCacheLogging) {
std::string file_contents = ""; std::string file_contents = "";
ASSERT_OK(ReadFileToString(env_, log_file, &file_contents)); ASSERT_OK(ReadFileToString(env_, log_file, &file_contents));
std::istringstream contents(file_contents);
int lookup_num = 0; int lookup_num = 0;
int add_num = 0; int add_num = 0;
std::string::size_type pos;
// count number of lookups std::string line;
pos = 0; // count number of lookups and additions
while ((pos = file_contents.find("LOOKUP -", pos)) != std::string::npos) { while (std::getline(contents, line)) {
// check if the line starts with LOOKUP or ADD
if (line.rfind("LOOKUP -", 0) == 0) {
++lookup_num; ++lookup_num;
pos += 1;
} }
if (line.rfind("ADD -", 0) == 0) {
// count number of additions
pos = 0;
while ((pos = file_contents.find("ADD -", pos)) != std::string::npos) {
++add_num; ++add_num;
pos += 1; }
} }
// We asked for every block twice // We asked for every block twice

Loading…
Cancel
Save