diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ec3e7d2307..e9bb0587321 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -537,6 +537,7 @@ set(SOURCES db/forward_iterator.cc db/import_column_family_job.cc db/internal_stats.cc + db/listener.cc db/logs_with_prep_tracker.cc db/log_reader.cc db/log_writer.cc diff --git a/TARGETS b/TARGETS index afd52bd8848..117b646867a 100644 --- a/TARGETS +++ b/TARGETS @@ -152,6 +152,7 @@ cpp_library( "db/forward_iterator.cc", "db/import_column_family_job.cc", "db/internal_stats.cc", + "db/listener.cc", "db/log_reader.cc", "db/log_writer.cc", "db/logs_with_prep_tracker.cc", diff --git a/db/listener.cc b/db/listener.cc new file mode 100644 index 00000000000..69d7b379790 --- /dev/null +++ b/db/listener.cc @@ -0,0 +1,69 @@ +// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. +// This source code is licensed under both the GPLv2 (found in the +// COPYING file in the root directory) and Apache 2.0 License +// (found in the LICENSE.Apache file in the root directory). +// + +#include "rocksdb/listener.h" + +namespace ROCKSDB_NAMESPACE { + +Status GetStringFromCompactionReason(std::string* compaction_str, + CompactionReason compaction_reason) { + switch (compaction_reason) { + case CompactionReason::kUnknown: + *compaction_str = "Unknown"; + return Status::OK(); + case CompactionReason::kLevelL0FilesNum: + *compaction_str = "LevelL0FilesNum"; + return Status::OK(); + case CompactionReason::kLevelMaxLevelSize: + *compaction_str = "LevelMaxLevelSize"; + return Status::OK(); + case CompactionReason::kUniversalSizeAmplification: + *compaction_str = "UniversalSizeAmplification"; + return Status::OK(); + case CompactionReason::kUniversalSizeRatio: + *compaction_str = "UniversalSizeRatio"; + return Status::OK(); + case CompactionReason::kUniversalSortedRunNum: + *compaction_str = "UniversalSortedRunNum"; + return Status::OK(); + case CompactionReason::kFIFOMaxSize: + *compaction_str = "FIFOMaxSize"; + return Status::OK(); + case CompactionReason::kFIFOReduceNumFiles: + *compaction_str = "FIFOReduceNumFiles"; + return Status::OK(); + case CompactionReason::kFIFOTtl: + *compaction_str = "FIFOTtl"; + return Status::OK(); + case CompactionReason::kManualCompaction: + *compaction_str = "ManualCompaction"; + return Status::OK(); + case CompactionReason::kFilesMarkedForCompaction: + *compaction_str = "FilesMarkedForCompaction"; + return Status::OK(); + case CompactionReason::kBottommostFiles: + *compaction_str = "BottommostFiles"; + return Status::OK(); + case CompactionReason::kTtl: + *compaction_str = "Ttl"; + return Status::OK(); + case CompactionReason::kFlush: + *compaction_str = "Flush"; + return Status::OK(); + case CompactionReason::kExternalSstIngestion: + *compaction_str = "ExternalSstIngestion"; + return Status::OK(); + case CompactionReason::kPeriodicCompaction: + *compaction_str = "PeriodicCompaction"; + return Status::OK(); + case CompactionReason::kNumOfReasons: + // fall through + default: + return Status::InvalidArgument("Invalid compaction reason"); + } +} + +} diff --git a/db/listener_test.cc b/db/listener_test.cc index eb1a08a3548..3d6408ec4fc 100644 --- a/db/listener_test.cc +++ b/db/listener_test.cc @@ -18,6 +18,7 @@ #include "rocksdb/db.h" #include "rocksdb/env.h" #include "rocksdb/filter_policy.h" +#include "rocksdb/listener.h" #include "rocksdb/options.h" #include "rocksdb/perf_context.h" #include "rocksdb/slice.h" @@ -39,6 +40,8 @@ namespace ROCKSDB_NAMESPACE { +class ListenerTest : public testing::Test {}; + class EventListenerTest : public DBTestBase { public: EventListenerTest() : DBTestBase("/listener_test") {} @@ -1032,6 +1035,27 @@ TEST_F(EventListenerTest, OnFileOperationTest) { ASSERT_GT(listener->file_reads_.load(), 0); } +TEST_F(ListenerTest, GetStringFromCompactionReason) { + std::string res; + + ASSERT_OK(GetStringFromCompactionReason(&res, CompactionReason::kUnknown)); + ASSERT_EQ(res, "Unknown"); + + ASSERT_OK(GetStringFromCompactionReason(&res, CompactionReason::kLevelL0FilesNum)); + ASSERT_EQ(res, "LevelL0FilesNum"); + + ASSERT_OK(GetStringFromCompactionReason(&res, CompactionReason::kLevelMaxLevelSize)); + ASSERT_EQ(res, "LevelMaxLevelSize"); + + ASSERT_OK(GetStringFromCompactionReason(&res, CompactionReason::kUniversalSizeAmplification)); + ASSERT_EQ(res, "UniversalSizeAmplification"); + + ASSERT_OK(GetStringFromCompactionReason(&res, CompactionReason::kUniversalSizeRatio)); + ASSERT_EQ(res, "UniversalSizeRatio"); + + ASSERT_NOK( + GetStringFromCompactionReason(&res, static_cast(-10))); +} } // namespace ROCKSDB_NAMESPACE #endif // ROCKSDB_LITE diff --git a/include/rocksdb/listener.h b/include/rocksdb/listener.h index d1c953f0f0e..2da690c43d1 100644 --- a/include/rocksdb/listener.h +++ b/include/rocksdb/listener.h @@ -135,6 +135,9 @@ struct WriteStallInfo { } condition; }; +Status GetStringFromCompactionReason(std::string* compaction_str, + CompactionReason compaction_reason); + #ifndef ROCKSDB_LITE struct TableFileDeletionInfo { diff --git a/src.mk b/src.mk index 9e9dd1a9e81..7a978bb5687 100644 --- a/src.mk +++ b/src.mk @@ -40,6 +40,7 @@ LIB_SOURCES = \ db/forward_iterator.cc \ db/import_column_family_job.cc \ db/internal_stats.cc \ + db/listener.cc \ db/logs_with_prep_tracker.cc \ db/log_reader.cc \ db/log_writer.cc \