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 2022/12/21 10:23:02 UTC

[GitHub] [nifi-minifi-cpp] fgerlits commented on a diff in pull request #1443: MINIFICPP-1972 - Refactor State Manager code

fgerlits commented on code in PR #1443:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1443#discussion_r1053171804


##########
extensions/rocksdb-repos/controllers/RocksDbStateStorage.cpp:
##########
@@ -18,53 +18,40 @@
 #include <fstream>
 #include <utility>
 
-#include "RocksDbPersistableKeyValueStoreService.h"
+#include "RocksDbStateStorage.h"
 #include "../encryption/RocksDbEncryptionProvider.h"
 #include "utils/StringUtils.h"
 #include "core/PropertyBuilder.h"
 #include "core/Resource.h"
 
 namespace org::apache::nifi::minifi::controllers {
 
-const core::Property RocksDbPersistableKeyValueStoreService::LinkedServices(
-    core::PropertyBuilder::createProperty("Linked Services")
-    ->withDescription("Referenced Controller Services")
-    ->build());
-const core::Property RocksDbPersistableKeyValueStoreService::AlwaysPersist(
-    core::PropertyBuilder::createProperty(AbstractAutoPersistingKeyValueStoreService::AlwaysPersistPropertyName)
-    ->withDescription("Persist every change instead of persisting it periodically.")
-    ->isRequired(false)
-    ->withDefaultValue<bool>(false)
-    ->build());

Review Comment:
   These moves are problematic, because there is no guarantee that the `REGISTER_RESOURCE_AS` in `RocksDbStateStorage.cpp` will run later than the static initializers in `AutoPersistor.cpp`.
   
   If we want to de-duplicate these properties, then we'll need to collect all static `Property` (and `Relationship`) initializers in a single file for all classes which depend on the `Property`s of  `AutoPersistor`.  See `KafkaProcessorStaticDefinitions.cpp`, for example.
   
   But since we only use this pair of properties in two places, I would probably just keep them duplicated.



##########
extensions/rocksdb-repos/controllers/RocksDbStateStorage.cpp:
##########
@@ -219,20 +208,20 @@ bool RocksDbPersistableKeyValueStoreService::update(const std::string& /*key*/,
   throw std::logic_error("Unsupported method");
 }
 
-bool RocksDbPersistableKeyValueStoreService::persist() {
+bool RocksDbStateStorage::persistNonVirtual() {
   if (!db_) {
     return false;
   }
   auto opendb = db_->open();
   if (!opendb) {
     return false;
   }
-  if (always_persist_) {
+  if (auto_persistor_.isAlwaysPersisting()) {
     return true;
   }
   return opendb->FlushWAL(true /*sync*/).ok();
 }
 
-REGISTER_RESOURCE_AS(RocksDbPersistableKeyValueStoreService, ControllerService, ("RocksDbPersistableKeyValueStoreService", "rocksdbpersistablekeyvaluestoreservice"));
+REGISTER_RESOURCE_AS(RocksDbStateStorage, ControllerService, ("RocksDbPersistableKeyValueStoreService", "rocksdbpersistablekeyvaluestoreservice", "RocksDbStateStorage"));

Review Comment:
   I guess we have to keep the old names for backward compatibility, but it would be useful to put TODO comments at these places so we will remember to remove the old names for the 1.0 release.



##########
extensions/standard-processors/controllers/PersistentMapStateStorage.h:
##########
@@ -22,34 +22,28 @@
 #include <memory>
 #include <utility>
 
-#include "controllers/keyvalue/AbstractAutoPersistingKeyValueStoreService.h"
-#include "UnorderedMapKeyValueStoreService.h"
+#include "controllers/keyvalue/AutoPersistor.h"
+#include "VolatileMapStateStorage.h"
 #include "core/Core.h"
 #include "properties/Configure.h"
 #include "core/logging/Logger.h"
 #include "core/logging/LoggerConfiguration.h"
 
 namespace org::apache::nifi::minifi::controllers {
 
-class UnorderedMapPersistableKeyValueStoreService : public AbstractAutoPersistingKeyValueStoreService,
-                                                    public UnorderedMapKeyValueStoreService {
+class PersistentMapStateStorage : public VolatileMapStateStorage {

Review Comment:
   I find it strange that `PersistentMapStateStorage` inherits from `VolatileMapStateStorage`, as this is not an IS-A relationship.  It would be better to have a separate (e.g) `StateStorageMap` object contained by both, without inheritance.



##########
libminifi/include/controllers/keyvalue/AutoPersistor.h:
##########
@@ -16,47 +16,49 @@
  */
 #pragma once
 
+#include <condition_variable>
+#include <memory>
+#include <mutex>
 #include <string>
 #include <thread>
-#include <mutex>
-#include <memory>
 #include <utility>
 
-#include "PersistableKeyValueStoreService.h"
+#include "core/ConfigurableComponent.h"
 #include "core/Core.h"
-#include "properties/Configure.h"
-#include "core/logging/Logger.h"
 #include "core/logging/LoggerFactory.h"
 #include "utils/Export.h"
 
 namespace org::apache::nifi::minifi::controllers {
 
-class AbstractAutoPersistingKeyValueStoreService : virtual public PersistableKeyValueStoreService {
+/**
+ *  Persists in given intervals.
+ *  Has an own thread, so stop() must be called before destruction of data used by persist_.
+ */
+class AutoPersistor {
  public:
-  explicit AbstractAutoPersistingKeyValueStoreService(std::string name, const utils::Identifier& uuid = {});
+  virtual ~AutoPersistor();

Review Comment:
   this doesn't need to be virtual



##########
libminifi/include/controllers/keyvalue/KeyValueStateManager.h:
##########
@@ -0,0 +1,59 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+#include <unordered_map>
+
+#include "core/StateManager.h"
+#include "core/Core.h"
+
+namespace org::apache::nifi::minifi::controllers {
+
+class KeyValueStateStorage;
+
+class KeyValueStateManager final : public core::StateManager {
+ public:
+  KeyValueStateManager(const utils::Identifier& id, KeyValueStateStorage* storage);
+
+  bool set(const core::StateManager::State& kvs) override;
+  bool get(core::StateManager::State& kvs) override;
+  bool clear() override;
+  bool persist() override;
+
+  [[nodiscard]] bool isTransactionInProgress() const override;
+  bool beginTransaction() override;
+  bool commit() override;
+  bool rollback() override;
+
+ private:
+  enum class ChangeType {
+    NONE,
+    SET,
+    CLEAR
+  };
+
+  KeyValueStateStorage* storage_;

Review Comment:
   the type of `storage_` could be `gsl::not_null<...>`



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org