bakaid commented on a change in pull request #605:
URL: https://github.com/apache/nifi-minifi-cpp/pull/605#discussion_r413745773



##########
File path: extensions/standard-processors/processors/TailFile.cpp
##########
@@ -232,20 +236,61 @@ void TailFile::parseStateFileLine(char *buf) {
     const auto file = key.substr(strlen(POSITION_STR));
     tail_states_[file].currentTailFilePosition_ = std::stoull(value);
   }
-
-  return;
 }
 
-bool TailFile::recoverState() {
-  std::ifstream file(state_file_.c_str(), std::ifstream::in);
-  if (!file.good()) {
-    logger_->log_error("load state file failed %s", state_file_);
-    return false;
+
+
+bool TailFile::recoverState(const std::shared_ptr<core::ProcessContext>& 
context) {
+  bool state_load_success = false;
+
+  std::unordered_map<std::string, std::string> state_map;
+  if (state_manager_->get(state_map)) {
+    std::map<std::string, TailState> new_tail_states;
+    size_t i = 0;
+    while (true) {
+      std::string name;
+      try {
+        name = state_map.at("file." + std::to_string(i) + ".name");
+      } catch (...) {
+        break;
+      }
+      try {
+        const std::string& current = state_map.at("file." + std::to_string(i) 
+ ".current");
+        uint64_t position = std::stoull(state_map.at("file." + 
std::to_string(i) + ".position"));
+
+        std::string fileLocation, fileName;
+        if (utils::file::PathUtils::getFileNameAndPath(current, fileLocation, 
fileName)) {
+          logger_->log_debug("Received path %s, file %s", fileLocation, 
fileName);
+          new_tail_states.emplace(fileName, TailState { fileLocation, 
fileName, position, 0 });
+        } else {
+          new_tail_states.emplace(current, TailState { fileLocation, current, 
position, 0 });
+        }
+      } catch (...) {
+        continue;
+      }
+      ++i;
+    }
+    state_load_success = true;
+    tail_states_ = std::move(new_tail_states);
+    for (const auto& s : tail_states_) {
+      logger_->log_debug("TailState %s: %s, %s, %llu, %llu", s.first, 
s.second.path_, s.second.current_file_name_, s.second.currentTailFilePosition_, 
s.second.currentTailFileModificationTime_);
+    }
+  } else {
+    logger_->log_info("Found no stored state");
   }
-  tail_states_.clear();
-  char buf[BUFFER_SIZE];
-  for (file.getline(buf, BUFFER_SIZE); file.good(); file.getline(buf, 
BUFFER_SIZE)) {
-    parseStateFileLine(buf);
+
+  /* We could not get the state form the StateManager, try to migrate the old 
state file if it exists */

Review comment:
       Fixed.

##########
File path: 
libminifi/src/controllers/keyvalue/AbstractCoreComponentStateManagerProvider.cpp
##########
@@ -0,0 +1,144 @@
+/**
+ * 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 "controllers/keyvalue/AbstractCoreComponentStateManagerProvider.h"
+
+#include "rapidjson/rapidjson.h"
+#include "rapidjson/document.h"
+#include "rapidjson/stringbuffer.h"
+#include "rapidjson/writer.h"
+
+#include <memory>
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace controllers {
+
+AbstractCoreComponentStateManagerProvider::AbstractCoreComponentStateManager::AbstractCoreComponentStateManager(
+    std::shared_ptr<AbstractCoreComponentStateManagerProvider> provider,
+    const std::string& id)
+    : provider_(std::move(provider))
+    , id_(id)
+    , state_valid_(false) {
+  std::string serialized;
+  if (provider_->getImpl(id_, serialized) && 
provider_->deserialize(serialized, state_)) {
+    state_valid_ = true;
+  }
+}
+
+bool 
AbstractCoreComponentStateManagerProvider::AbstractCoreComponentStateManager::set(const
 std::unordered_map<std::string, std::string>& kvs) {
+  if (provider_->setImpl(id_, provider_->serialize(kvs))) {
+    state_valid_ = true;
+    state_ = kvs;
+    return true;
+  } else {
+    return false;
+  }
+}
+
+bool 
AbstractCoreComponentStateManagerProvider::AbstractCoreComponentStateManager::get(std::unordered_map<std::string,
 std::string>& kvs) {
+  if (!state_valid_) {
+    return false;
+  }
+  kvs = state_;
+  return true;
+}
+
+bool 
AbstractCoreComponentStateManagerProvider::AbstractCoreComponentStateManager::clear()
 {
+  if (!state_valid_) {
+    return false;
+  }
+  if (provider_->removeImpl(id_)) {
+    state_valid_ = false;
+    state_.clear();
+    return true;
+  } else {
+    return false;
+  }
+}
+
+bool 
AbstractCoreComponentStateManagerProvider::AbstractCoreComponentStateManager::persist()
 {
+  if (!state_valid_) {
+    return false;
+  }
+  return provider_->persistImpl();
+}
+
+AbstractCoreComponentStateManagerProvider::~AbstractCoreComponentStateManagerProvider()
 {
+}

Review comment:
       Done.




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


Reply via email to