You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2019/09/09 14:36:53 UTC

[GitHub] [nifi-minifi-cpp] phrocker commented on a change in pull request #605: MINIFICPP-550 - Implement RocksDB controller service and component st…

phrocker commented on a change in pull request #605: MINIFICPP-550 - Implement RocksDB controller service and component st…
URL: https://github.com/apache/nifi-minifi-cpp/pull/605#discussion_r305364480
 
 

 ##########
 File path: extensions/rocksdb-repos/controllers/RocksDbPersistableKeyValueStoreService.cpp
 ##########
 @@ -0,0 +1,199 @@
+/**
+ * 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 "RocksDbPersistableKeyValueStoreService.h"
+
+#include "utils/StringUtils.h"
+
+#include <fstream>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace controllers {
+
+core::Property RocksDbPersistableKeyValueStoreService::Directory(
+    core::PropertyBuilder::createProperty("Directory")->withDescription("Path to a directory for the database")
+        ->isRequired(true)->build());
+
+RocksDbPersistableKeyValueStoreService::RocksDbPersistableKeyValueStoreService(const std::string& name, const std::string& id)
+    : controllers::KeyValueStoreService(name, id)
+    , controllers::AbstractAutoPersistingKeyValueStoreService(name, id)
+    , db_valid_(false)
+    , logger_(logging::LoggerFactory<RocksDbPersistableKeyValueStoreService>::getLogger()) {
+}
+
+RocksDbPersistableKeyValueStoreService::RocksDbPersistableKeyValueStoreService(const std::string& name, utils::Identifier uuid /*= utils::Identifier()*/)
+    : controllers::KeyValueStoreService(name, uuid)
+    , controllers::AbstractAutoPersistingKeyValueStoreService(name, uuid)
+    , db_valid_(false)
+    , logger_(logging::LoggerFactory<RocksDbPersistableKeyValueStoreService>::getLogger()) {
+}
+
+RocksDbPersistableKeyValueStoreService::~RocksDbPersistableKeyValueStoreService() {
+  if (db_valid_) {
+    delete db_;
+  }
+}
+
+void RocksDbPersistableKeyValueStoreService::initialize() {
+  AbstractAutoPersistingKeyValueStoreService::initialize();
+  std::set<core::Property> supportedProperties;
+  supportedProperties.insert(Directory);
+  updateSupportedProperties(supportedProperties);
+}
+
+void RocksDbPersistableKeyValueStoreService::onEnable() {
+  if (configuration_ == nullptr) {
+    logger_->log_debug("Cannot enable RocksDbPersistableKeyValueStoreService");
+    return;
+  }
+
+  AbstractAutoPersistingKeyValueStoreService::onEnable();
+
+  if (!getProperty(Directory.getName(), directory_)) {
+    logger_->log_error("Invalid or missing property: Directory");
+    return;
+  }
+
+  if (db_valid_) {
+    db_valid_ = false;
+    delete db_;
+  }
+  rocksdb::Options options;
+  options.create_if_missing = true;
+  if (!always_persist_) {
+    options.manual_wal_flush = true;
+  }
+  rocksdb::Status status = rocksdb::DB::Open(options, directory_, &db_);
+  if (status.ok()) {
+    db_valid_ = true;
+    logger_->log_trace("Successfully opened RocksDB database at %s", directory_.c_str());
+  } else {
+    logger_->log_error("Failed to open RocksDB database at %s, error: %s", directory_.c_str(), status.getState());
+    return;
+  }
+
+  if (always_persist_) {
+    default_write_options.sync = true;
+  }
+
+  logger_->log_trace("Enabled RocksDbPersistableKeyValueStoreService");
+}
+
+void RocksDbPersistableKeyValueStoreService::notifyStop() {
+  AbstractAutoPersistingKeyValueStoreService::notifyStop();
+
+  db_valid_ = false;
 
 Review comment:
   Would love some defensiveness here to ensure rogue extensions don't cause a memory error. 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services