Copilot commented on code in PR #2209:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2209#discussion_r3766899966


##########
extensions/rocksdb-repos/RocksDbProvenanceRepository.cpp:
##########
@@ -0,0 +1,207 @@
+/**
+ * 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 "RocksDbProvenanceRepository.h"
+
+#include <string>
+
+#include "core/Resource.h"
+
+namespace org::apache::nifi::minifi::provenance {
+
+namespace {
+class EventCursor : public ProvenanceRepository::Cursor {
+public:
+  explicit EventCursor(std::string event_id): event_id_(std::move(event_id)) {}
+  [[nodiscard]]
+  std::string toString() const override {
+    return event_id_;
+  }
+  ~EventCursor() override = default;
+
+  std::string event_id_;
+};
+}  // namespace
+
+static const std::string_view NEXT_EVENT_UUID_KEY = "next_event_uuid";
+
+bool RocksDbProvenanceRepository::initialize(const 
std::shared_ptr<org::apache::nifi::minifi::Configure> &config) {
+  if (!RocksDbRepository::initialize(config)) {
+    return false;
+  }
+  std::string value;
+  if (config->get(Configure::nifi_provenance_repository_directory_default, 
value) && !value.empty()) {
+    directory_ = value;
+  }
+  logger_->log_debug("MiNiFi Provenance Repository Directory {}", directory_);
+  if (config->get(Configure::nifi_provenance_repository_max_storage_size, 
value)) {
+    max_partition_bytes_ = gsl::narrow<int64_t>(parsing::parseDataSize(value) 
| utils::orThrow("expected parsable data size"));
+  }
+  logger_->log_debug("MiNiFi Provenance Max Partition Bytes {}", 
max_partition_bytes_);
+  if (config->get(Configure::nifi_provenance_repository_max_storage_time, 
value)) {
+    if (auto max_partition = 
utils::timeutils::StringToDuration<std::chrono::milliseconds>(value))
+      max_partition_millis_ = *max_partition;
+  }
+  logger_->log_debug("MiNiFi Provenance Max Storage Time: [{}]", 
max_partition_millis_);
+
+  verify_checksums_in_rocksdb_reads_ = 
(config->get(Configure::nifi_provenance_repository_rocksdb_read_verify_checksums)
 | utils::andThen(&utils::string::toBool)).value_or(false);
+  logger_->log_debug("{} checksum verification in 
RocksDbProvenanceRepository", verify_checksums_in_rocksdb_reads_ ? "Using" : 
"Not using");
+
+  auto db_options = [] (minifi::internal::Writable<rocksdb::DBOptions>& 
db_opts) {
+    minifi::internal::setCommonRocksDbOptions(db_opts);
+  };
+
+  // Rocksdb write buffers act as a log of database operation: grow till 
reaching the limit, serialized after
+  // This shouldn't go above 16MB and the configured total size of the db 
should cap it as well
+  auto cf_options = [this] (rocksdb::ColumnFamilyOptions& cf_opts) {
+    int64_t max_buffer_size = 16 << 20;
+    cf_opts.write_buffer_size = gsl::narrow<size_t>(std::min(max_buffer_size, 
max_partition_bytes_));
+    cf_opts.max_write_buffer_number = 4;
+    cf_opts.min_write_buffer_number_to_merge = 1;
+
+    cf_opts.compaction_style = rocksdb::CompactionStyle::kCompactionStyleFIFO;
+    cf_opts.compaction_options_fifo = 
rocksdb::CompactionOptionsFIFO(max_partition_bytes_, false);
+    if (max_partition_millis_ > std::chrono::milliseconds(0)) {
+      cf_opts.ttl = 
std::chrono::duration_cast<std::chrono::seconds>(max_partition_millis_).count();
+    }
+  };
+
+  db_ = minifi::internal::RocksDatabase::create(db_options, cf_options, 
directory_,
+    minifi::internal::getRocksDbOptionsToOverride(config, 
Configure::nifi_provenance_repository_rocksdb_options));
+  std::string internal_state_db_uri = [&] {
+    const std::string_view minifidb_scheme = "minifidb://";
+    if (directory_.starts_with(minifidb_scheme)) {
+      return directory_ + "-internal-state";
+    }
+    std::string uri = utils::string::join_pack(minifidb_scheme, directory_);
+    if (uri.ends_with("/") || uri.ends_with("\\")) {
+      uri.pop_back();
+    }
+    return uri + "/internal-state";
+  }();
+  internal_state_db_ = minifi::internal::RocksDatabase::create(db_options, {}, 
internal_state_db_uri, {});
+  if (auto open_state_db = internal_state_db_->open()) {
+    rocksdb::ReadOptions options;
+    options.verify_checksums = verify_checksums_in_rocksdb_reads_;
+    std::string next_event_uuid_str;
+    if (open_state_db->Get(options, NEXT_EVENT_UUID_KEY, 
&next_event_uuid_str).ok()) {
+      next_event_id_ = next_event_uuid_str;
+    } else {
+      logger_->log_error("Could not find '{}'", NEXT_EVENT_UUID_KEY);
+      next_event_id_ = utils::IdGenerator::getIdGenerator()->generate();

Review Comment:
   When the state key is absent, seeding from a random UUID does not account 
for records already present in the provenance database (notably on upgrade from 
the previous repository layout or after loss of the side database). The new 
sequence can sort before existing keys, so cursor-based reporting may never see 
newly appended events. Initialize this counter to one past the greatest 
existing event key when the state entry is missing; only generate a random seed 
for an empty repository.
   
   This issue also appears in the following locations of the same file:
   - line 126
   - line 164
   - line 171



##########
libminifi/include/core/repository/VolatileProvenanceRepository.h:
##########
@@ -38,6 +39,36 @@ class VolatileProvenanceRepository : public 
VolatileRepository {
     stop();
   }
 
+  bool initialize(const std::shared_ptr<Configure> &configure) override {
+    if (!VolatileRepository::initialize(configure)) {
+      return false;
+    }
+    next_event_id_ = utils::IdGenerator::getIdGenerator()->generate();
+    return true;
+  }
+
+  std::expected<void, std::string> appendEvents(const 
std::vector<std::shared_ptr<provenance::ProvenanceEventRecord>>& events) 
override {
+    std::vector<std::pair<std::string, std::unique_ptr<io::BufferStream>>> 
data;
+    data.reserve(events.size());
+    std::lock_guard guard(next_event_id_mtx_);
+    for (auto& event : events) {
+      event->setUUID(next_event_id_++);
+      data.emplace_back(event->getUUIDStr(), 
std::make_unique<io::BufferStream>());
+      event->serialize(*data.back().second);
+    }
+    MultiPut(data);
+
+    return {};

Review Comment:
   `MultiPut` can fail (for example when an entry cannot be inserted), but this 
implementation always reports success. Callers will therefore discard 
provenance events even though `appendEvents` returned a successful `expected`; 
propagate the failure as `std::unexpected` as the RocksDB implementation does.



##########
libminifi/src/core/reporting/SiteToSiteProvenanceReportingTask.cpp:
##########
@@ -111,6 +111,10 @@ std::string 
SiteToSiteProvenanceReportingTask::getJsonReport(core::ProcessContex
 
     recordJson.AddMember("entityType", "org.apache.nifi.flowfile.FlowFile", 
alloc);
 
+    if (auto event_ordinal = record->getEventOrdinal()) {
+      recordJson.AddMember("eventOrdinal", event_ordinal.value(), alloc);
+    }

Review Comment:
   The new `eventOrdinal` output is unreachable for built-in events: the field 
defaults to `nullopt`, and there are no calls to `setEventOrdinal` in event 
creation, repository append, or deserialization. Consequently this branch never 
emits the field. Populate and persist the ordinal in the repository/event 
lifecycle (and add a reporting assertion), or remove the dead output until 
ordinals are available.
   
   This issue also appears on line 220 of the same file.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to