[GitHub] [nifi-minifi-cpp] bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance repo performance should be improved

2020-02-05 Thread GitBox
bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance 
repo performance should be improved
URL: https://github.com/apache/nifi-minifi-cpp/pull/716#discussion_r375190678
 
 

 ##
 File path: libminifi/test/rocksdb-tests/DBProvenanceRepositoryTests.cpp
 ##
 @@ -0,0 +1,122 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ProvenanceRepository.h"
+#include "../TestBase.h"
+#include 
+#include 
+#include 
+#include 
+
+#define TEST_PROVENANCE_STORAGE_SIZE (1024*100)  // 100 KB
+#define TEST_MAX_PROVENANCE_STORAGE_SIZE (100*1024*1024)  // 100 MB
+
+#define TEST_PROVENANCE_ENTRY_LIFE_TIME (1000)  // 1 sec
+
+void generateData(std::vector& data) {
+  std::random_device rd;
+  std::mt19937 eng(rd());
+
+  std::uniform_int_distribution<> distr(std::numeric_limits::min(), 
std::numeric_limits::max());
+  auto rand = std::bind(distr, eng);
+  std::generate_n(data.begin(), data.size(), rand);
+}
+
+void provisionRepo(minifi::provenance::ProvenanceRepository& repo, size_t 
count, size_t size) {
+  for (int i = 0; i < count; ++i) {
+std::vector v(size);
+generateData(v);
+REQUIRE(repo.Put(std::to_string(i), reinterpret_cast(v.data()), v.size()));
+  }
+}
+
+void verifyMaxKeyCount(const minifi::provenance::ProvenanceRepository& repo, 
uint64_t keyCount) {
+  uint64_t k = keyCount;
+
+  for (int i = 0; i < 5; ++i) {
+std::this_thread::sleep_for(std::chrono::seconds(1));
+k = std::min(k, repo.getKeyCount());
+if (k < keyCount) {
+  break;
+}
+  }
+
+  REQUIRE(k < keyCount);
+}
+
+TEST_CASE("Test size limit", "[sizeLimitTest]") {
+  TestController testController;
+
+  char dirtemplate[] = "/tmp/db.XX";
+  auto temp_dir = testController.createTempDirectory(dirtemplate);
+  REQUIRE(!temp_dir.empty());
+
+  // 20 sec, 100kb - going to exceed the latter
+  minifi::provenance::ProvenanceRepository provdb("TestProvRepo", temp_dir,
+  MAX_PROVENANCE_ENTRY_LIFE_TIME, TEST_PROVENANCE_STORAGE_SIZE, 1000);
+
+  auto configuration = 
std::make_shared();
+  
configuration->set(minifi::Configure::nifi_dbcontent_repository_directory_default,
 temp_dir);
+
+  REQUIRE(provdb.initialize(configuration));
+
+  uint64_t keyCount = 500;
+
+  provisionRepo(provdb, keyCount, 10240);
+
+  verifyMaxKeyCount(provdb, 200);
+}
+
+TEST_CASE("Test time limit", "[timeLimitTest]") {
+  TestController testController;
+
+  char dirtemplate[] = "/tmp/db.XX";
+  auto temp_dir = testController.createTempDirectory(dirtemplate);
+  REQUIRE(!temp_dir.empty());
+
+  // 20 sec, 100kb - going to exceed the latter
+  minifi::provenance::ProvenanceRepository provdb("TestProvRepo", temp_dir,
 
 Review comment:
   TEST_PROVENANCE_ENTRY_LIFE_TIME is `1 sec`, TEST_MAX_PROVENANCE_STORAGE_SIZE 
is `100 MB`, and I think in this case we expect to exceed the former, not the 
latter.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [nifi-minifi-cpp] bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance repo performance should be improved

2020-02-05 Thread GitBox
bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance 
repo performance should be improved
URL: https://github.com/apache/nifi-minifi-cpp/pull/716#discussion_r375190251
 
 

 ##
 File path: libminifi/test/rocksdb-tests/DBProvenanceRepositoryTests.cpp
 ##
 @@ -0,0 +1,122 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ProvenanceRepository.h"
+#include "../TestBase.h"
+#include 
+#include 
+#include 
+#include 
+
+#define TEST_PROVENANCE_STORAGE_SIZE (1024*100)  // 100 KB
+#define TEST_MAX_PROVENANCE_STORAGE_SIZE (100*1024*1024)  // 100 MB
+
+#define TEST_PROVENANCE_ENTRY_LIFE_TIME (1000)  // 1 sec
+
+void generateData(std::vector& data) {
+  std::random_device rd;
+  std::mt19937 eng(rd());
+
+  std::uniform_int_distribution<> distr(std::numeric_limits::min(), 
std::numeric_limits::max());
+  auto rand = std::bind(distr, eng);
+  std::generate_n(data.begin(), data.size(), rand);
+}
+
+void provisionRepo(minifi::provenance::ProvenanceRepository& repo, size_t 
count, size_t size) {
+  for (int i = 0; i < count; ++i) {
+std::vector v(size);
+generateData(v);
+REQUIRE(repo.Put(std::to_string(i), reinterpret_cast(v.data()), v.size()));
+  }
+}
+
+void verifyMaxKeyCount(const minifi::provenance::ProvenanceRepository& repo, 
uint64_t keyCount) {
+  uint64_t k = keyCount;
+
+  for (int i = 0; i < 5; ++i) {
+std::this_thread::sleep_for(std::chrono::seconds(1));
+k = std::min(k, repo.getKeyCount());
+if (k < keyCount) {
+  break;
+}
+  }
+
+  REQUIRE(k < keyCount);
+}
+
+TEST_CASE("Test size limit", "[sizeLimitTest]") {
+  TestController testController;
+
+  char dirtemplate[] = "/tmp/db.XX";
+  auto temp_dir = testController.createTempDirectory(dirtemplate);
+  REQUIRE(!temp_dir.empty());
+
+  // 20 sec, 100kb - going to exceed the latter
+  minifi::provenance::ProvenanceRepository provdb("TestProvRepo", temp_dir,
 
 Review comment:
   MAX_PROVENANCE_ENTRY_LIFE_TIME is 60 seconds, where does `20 sec` come from?


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [nifi-minifi-cpp] bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance repo performance should be improved

2020-02-04 Thread GitBox
bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance 
repo performance should be improved
URL: https://github.com/apache/nifi-minifi-cpp/pull/716#discussion_r374680995
 
 

 ##
 File path: extensions/rocksdb-repos/ProvenanceRepository.cpp
 ##
 @@ -17,72 +17,31 @@
  */
 
 #include "ProvenanceRepository.h"
-#include "rocksdb/write_batch.h"
 #include 
-#include 
-#include "rocksdb/options.h"
-#include "provenance/Provenance.h"
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace provenance {
 
-void ProvenanceRepository::flush() {
-  rocksdb::WriteBatch batch;
-  std::string key;
-  std::string value;
-  rocksdb::ReadOptions options;
-  uint64_t decrement_total = 0;
-  while (keys_to_delete.size_approx() > 0) {
-if (keys_to_delete.try_dequeue(key)) {
-  db_->Get(options, key, );
-  decrement_total += value.size();
-  batch.Delete(key);
-  logger_->log_debug("Removing %s", key);
-}
-  }
-  if (db_->Write(rocksdb::WriteOptions(), ).ok()) {
-logger_->log_debug("Decrementing %u from a repo size of %u", 
decrement_total, repo_size_.load());
-if (decrement_total > repo_size_.load()) {
-  repo_size_ = 0;
-} else {
-  repo_size_ -= decrement_total;
-}
-  }
+void ProvenanceRepository::printStats() {
+  std::string key_count;
+  db_->GetProperty("rocksdb.estimate-num-keys", _count);
+
+  std::string table_readers;
+  db_->GetProperty("rocksdb.estimate-table-readers-mem", _readers);
+
+  std::string all_memtables;
+  db_->GetProperty("rocksdb.cur-size-all-mem-tables", _memtables);
+
+  logger_->log_info("Repository stats: key count: %zu, table readers size: 
%zu, all memory tables size: %zu",
+key_count, table_readers, all_memtables);
 }
 
 void ProvenanceRepository::run() {
   while (running_) {
-std::this_thread::sleep_for(std::chrono::milliseconds(purge_period_));
-uint64_t curTime = getTimeMillis();
-// threshold for purge
-uint64_t purgeThreshold = max_partition_bytes_ * 3 / 4;
-
-uint64_t size = getRepoSize();
-
-if (size >= purgeThreshold) {
-  rocksdb::Iterator* it = db_->NewIterator(rocksdb::ReadOptions());
-  for (it->SeekToFirst(); it->Valid(); it->Next()) {
-ProvenanceEventRecord eventRead;
-std::string key = it->key().ToString();
-uint64_t eventTime = 
eventRead.getEventTime(reinterpret_cast(const_cast(it->value().data())),
 it->value().size());
-if (eventTime > 0) {
-  if ((curTime - eventTime) > (uint64_t)max_partition_millis_)
-Delete(key);
-} else {
-  logger_->log_debug("NiFi Provenance retrieve event %s fail", key);
-  Delete(key);
-}
-  }
-  delete it;
-}
-flush();
-size = getRepoSize();
-if (size > (uint64_t)max_partition_bytes_)
-  repo_full_ = true;
-else
-  repo_full_ = false;
+std::this_thread::sleep_for(std::chrono::seconds(30));
 
 Review comment:
   I agree with moving as much as possible to a common implementation, but I 
don't want this delay on master even until that follow-up task is completed - 
it worsens the situation, by 3 times, if I understand correctly.
   I am fine with working this around for the time being by sleeping 30 times 1 
second while checking `running_`, for example.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [nifi-minifi-cpp] bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance repo performance should be improved

2020-02-04 Thread GitBox
bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance 
repo performance should be improved
URL: https://github.com/apache/nifi-minifi-cpp/pull/716#discussion_r374680995
 
 

 ##
 File path: extensions/rocksdb-repos/ProvenanceRepository.cpp
 ##
 @@ -17,72 +17,31 @@
  */
 
 #include "ProvenanceRepository.h"
-#include "rocksdb/write_batch.h"
 #include 
-#include 
-#include "rocksdb/options.h"
-#include "provenance/Provenance.h"
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace provenance {
 
-void ProvenanceRepository::flush() {
-  rocksdb::WriteBatch batch;
-  std::string key;
-  std::string value;
-  rocksdb::ReadOptions options;
-  uint64_t decrement_total = 0;
-  while (keys_to_delete.size_approx() > 0) {
-if (keys_to_delete.try_dequeue(key)) {
-  db_->Get(options, key, );
-  decrement_total += value.size();
-  batch.Delete(key);
-  logger_->log_debug("Removing %s", key);
-}
-  }
-  if (db_->Write(rocksdb::WriteOptions(), ).ok()) {
-logger_->log_debug("Decrementing %u from a repo size of %u", 
decrement_total, repo_size_.load());
-if (decrement_total > repo_size_.load()) {
-  repo_size_ = 0;
-} else {
-  repo_size_ -= decrement_total;
-}
-  }
+void ProvenanceRepository::printStats() {
+  std::string key_count;
+  db_->GetProperty("rocksdb.estimate-num-keys", _count);
+
+  std::string table_readers;
+  db_->GetProperty("rocksdb.estimate-table-readers-mem", _readers);
+
+  std::string all_memtables;
+  db_->GetProperty("rocksdb.cur-size-all-mem-tables", _memtables);
+
+  logger_->log_info("Repository stats: key count: %zu, table readers size: 
%zu, all memory tables size: %zu",
+key_count, table_readers, all_memtables);
 }
 
 void ProvenanceRepository::run() {
   while (running_) {
-std::this_thread::sleep_for(std::chrono::milliseconds(purge_period_));
-uint64_t curTime = getTimeMillis();
-// threshold for purge
-uint64_t purgeThreshold = max_partition_bytes_ * 3 / 4;
-
-uint64_t size = getRepoSize();
-
-if (size >= purgeThreshold) {
-  rocksdb::Iterator* it = db_->NewIterator(rocksdb::ReadOptions());
-  for (it->SeekToFirst(); it->Valid(); it->Next()) {
-ProvenanceEventRecord eventRead;
-std::string key = it->key().ToString();
-uint64_t eventTime = 
eventRead.getEventTime(reinterpret_cast(const_cast(it->value().data())),
 it->value().size());
-if (eventTime > 0) {
-  if ((curTime - eventTime) > (uint64_t)max_partition_millis_)
-Delete(key);
-} else {
-  logger_->log_debug("NiFi Provenance retrieve event %s fail", key);
-  Delete(key);
-}
-  }
-  delete it;
-}
-flush();
-size = getRepoSize();
-if (size > (uint64_t)max_partition_bytes_)
-  repo_full_ = true;
-else
-  repo_full_ = false;
+std::this_thread::sleep_for(std::chrono::seconds(30));
 
 Review comment:
   I agree with moving as much as possible to a common implementation, but I 
don't want this delay on master even until that follow-up task is completed - 
it worsens the situation, by 3 times, if I understand correctly.
   I am fine with working this around for the time being by sleeping 30 times 1 
seconds while checking `running_`, for example.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [nifi-minifi-cpp] bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance repo performance should be improved

2020-02-04 Thread GitBox
bakaid commented on a change in pull request #716: MINIFICPP-1127 - Provenance 
repo performance should be improved
URL: https://github.com/apache/nifi-minifi-cpp/pull/716#discussion_r374675202
 
 

 ##
 File path: extensions/rocksdb-repos/ProvenanceRepository.cpp
 ##
 @@ -17,72 +17,31 @@
  */
 
 #include "ProvenanceRepository.h"
-#include "rocksdb/write_batch.h"
 #include 
-#include 
-#include "rocksdb/options.h"
-#include "provenance/Provenance.h"
 namespace org {
 namespace apache {
 namespace nifi {
 namespace minifi {
 namespace provenance {
 
-void ProvenanceRepository::flush() {
-  rocksdb::WriteBatch batch;
-  std::string key;
-  std::string value;
-  rocksdb::ReadOptions options;
-  uint64_t decrement_total = 0;
-  while (keys_to_delete.size_approx() > 0) {
-if (keys_to_delete.try_dequeue(key)) {
-  db_->Get(options, key, );
-  decrement_total += value.size();
-  batch.Delete(key);
-  logger_->log_debug("Removing %s", key);
-}
-  }
-  if (db_->Write(rocksdb::WriteOptions(), ).ok()) {
-logger_->log_debug("Decrementing %u from a repo size of %u", 
decrement_total, repo_size_.load());
-if (decrement_total > repo_size_.load()) {
-  repo_size_ = 0;
-} else {
-  repo_size_ -= decrement_total;
-}
-  }
+void ProvenanceRepository::printStats() {
+  std::string key_count;
+  db_->GetProperty("rocksdb.estimate-num-keys", _count);
+
+  std::string table_readers;
+  db_->GetProperty("rocksdb.estimate-table-readers-mem", _readers);
+
+  std::string all_memtables;
+  db_->GetProperty("rocksdb.cur-size-all-mem-tables", _memtables);
+
+  logger_->log_info("Repository stats: key count: %zu, table readers size: 
%zu, all memory tables size: %zu",
+key_count, table_readers, all_memtables);
 }
 
 void ProvenanceRepository::run() {
   while (running_) {
-std::this_thread::sleep_for(std::chrono::milliseconds(purge_period_));
-uint64_t curTime = getTimeMillis();
-// threshold for purge
-uint64_t purgeThreshold = max_partition_bytes_ * 3 / 4;
-
-uint64_t size = getRepoSize();
-
-if (size >= purgeThreshold) {
-  rocksdb::Iterator* it = db_->NewIterator(rocksdb::ReadOptions());
-  for (it->SeekToFirst(); it->Valid(); it->Next()) {
-ProvenanceEventRecord eventRead;
-std::string key = it->key().ToString();
-uint64_t eventTime = 
eventRead.getEventTime(reinterpret_cast(const_cast(it->value().data())),
 it->value().size());
-if (eventTime > 0) {
-  if ((curTime - eventTime) > (uint64_t)max_partition_millis_)
-Delete(key);
-} else {
-  logger_->log_debug("NiFi Provenance retrieve event %s fail", key);
-  Delete(key);
-}
-  }
-  delete it;
-}
-flush();
-size = getRepoSize();
-if (size > (uint64_t)max_partition_bytes_)
-  repo_full_ = true;
-else
-  repo_full_ = false;
+std::this_thread::sleep_for(std::chrono::seconds(30));
 
 Review comment:
   This will prevent proper agent shutdown, on average for 15 seconds, which is 
pretty bad.


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services