Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cpp/arcticdb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ set(arcticdb_srcs
entity/descriptor_item.hpp
entity/field_collection.hpp
entity/field_collection_proto.hpp
entity/python_bindings_common.hpp
log/log.hpp
log/trace.hpp
pipeline/column_mapping.hpp
Expand All @@ -273,6 +274,7 @@ set(arcticdb_srcs
python/numpy_buffer_holder.hpp
python/python_strings.hpp
python/python_handler_data.hpp
python/python_bindings_common.hpp
python/python_utils.hpp
pipeline/string_reducers.hpp
processing/aggregation_utils.hpp
Expand Down Expand Up @@ -329,6 +331,7 @@ set(arcticdb_srcs
storage/s3/s3_client_interface.hpp
storage/s3/s3_storage_tool.hpp
storage/s3/s3_settings.hpp
storage/python_bindings_common.hpp
storage/storage_factory.hpp
storage/storage_options.hpp
storage/storage.hpp
Expand Down Expand Up @@ -457,6 +460,7 @@ set(arcticdb_srcs
entity/metrics.cpp
entity/performance_tracing.cpp
entity/protobuf_mappings.cpp
entity/python_bindings_common.cpp
entity/types.cpp
entity/type_utils.cpp
entity/types_proto.cpp
Expand All @@ -478,6 +482,7 @@ set(arcticdb_srcs
pipeline/string_pool_utils.cpp
pipeline/value_set.cpp
pipeline/write_frame.cpp
python/python_bindings_common.cpp
python/normalization_checks.cpp
python/python_strings.cpp
python/python_utils.cpp
Expand Down Expand Up @@ -528,6 +533,7 @@ set(arcticdb_srcs
storage/s3/s3_storage_tool.cpp
storage/s3/s3_client_wrapper.cpp
storage/s3/s3_client_wrapper.hpp
storage/python_bindings_common.cpp
storage/storage_factory.cpp
storage/storage_utils.cpp
stream/aggregator.cpp
Expand Down
75 changes: 75 additions & 0 deletions cpp/arcticdb/entity/python_bindings_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* Copyright 2023 Man Group Operations Limited
*
* Use of this software is governed by the Business Source License 1.1 included in the file licenses/BSL.txt.
*
* As of the Change Date specified in that file, in accordance with the Business Source License, use of this software
* will be governed by the Apache License, version 2.0.
*/

#include <arcticdb/entity/python_bindings_common.hpp>
#include <arcticdb/entity/atom_key.hpp>
#include <arcticdb/entity/versioned_item.hpp>
#include <arcticdb/entity/types.hpp>

#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>

namespace py = pybind11;

namespace arcticdb::entity::apy {

void register_common_entity_bindings(py::module& m, bool local_bindings) {
py::class_<AtomKey, std::shared_ptr<AtomKey>>(m, "AtomKey", py::module_local(local_bindings))
.def(py::init())
.def(py::init<StreamId, VersionId, timestamp, ContentHash, IndexValue, IndexValue, KeyType>())
.def("change_id", &AtomKey::change_id)
.def_property_readonly("id", &AtomKey::id)
.def_property_readonly("version_id", &AtomKey::version_id)
.def_property_readonly("creation_ts", &AtomKey::creation_ts)
.def_property_readonly("content_hash", &AtomKey::content_hash)
.def_property_readonly("start_index", &AtomKey::start_index)
.def_property_readonly("end_index", &AtomKey::end_index)
.def_property_readonly("type", [](const AtomKey& self) { return self.type(); })
.def(pybind11::self == pybind11::self)
.def(pybind11::self != pybind11::self)
.def("__repr__", &AtomKey::view_human)
.def(py::self < py::self)
.def(py::pickle(
[](const AtomKey& key) {
constexpr int serialization_version = 0;
return py::make_tuple(
serialization_version,
key.id(),
key.version_id(),
key.creation_ts(),
key.content_hash(),
key.start_index(),
key.end_index(),
key.type()
);
},
[](py::tuple t) {
util::check(t.size() >= 7, "Invalid AtomKey pickle object!");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not check for t.size() == 7

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's for some sort of future proof, if new setting is added to the class.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for the staged data tokens API, which makes the staged data tokens (which wrap atom keys) pickleable. This helps to use the staged data API across processes which may have different ArcticDB versions.


[[maybe_unused]] const int serialization_version = t[0].cast<int>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? Why don't we remove it?

AtomKey key(
t[1].cast<StreamId>(),
t[2].cast<VersionId>(),
t[3].cast<timestamp>(),
t[4].cast<ContentHash>(),
t[5].cast<IndexValue>(),
t[6].cast<IndexValue>(),
t[7].cast<KeyType>()
);
return key;
}
));

py::class_<VersionedItem>(m, "VersionedItem", py::module_local(local_bindings))
.def_property_readonly("symbol", &VersionedItem::symbol)
.def_property_readonly("timestamp", &VersionedItem::timestamp)
.def_property_readonly("version", &VersionedItem::version);
}

} // namespace arcticdb::entity::apy
17 changes: 17 additions & 0 deletions cpp/arcticdb/entity/python_bindings_common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright 2023 Man Group Operations Limited
*
* Use of this software is governed by the Business Source License 1.1 included in the file licenses/BSL.txt.
*
* As of the Change Date specified in that file, in accordance with the Business Source License, use of this software
* will be governed by the Apache License, version 2.0.
*/

#pragma once

#include <pybind11/pybind11.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this header in the .hpp file. You can forward declare pybind11::module and place the header only in the .cpp

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll work as well. Is it for the sake of compilation speed?


namespace arcticdb::entity::apy {

void register_common_entity_bindings(pybind11::module& m, bool local_bindings);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "common" mean here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shared by multiple repos

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid bool flags on APIs, it's hard to guess what the false in entity::apy::register_common_entity_bindings(version, false); means. Instead there should be separate functions for the true and false cases, which might push down to a private function with the bool in its signature.


} // namespace arcticdb::entity::apy
58 changes: 58 additions & 0 deletions cpp/arcticdb/python/python_bindings_common.cpp
Copy link
Collaborator

@poodlewars poodlewars Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to make the metrics config pickleable? This relates to this comment on the enterprise PR https://github.com/man-group/arcticdb-enterprise/pull/281#discussion_r2650734598 . All we need is to add accessors to the fields in hte metrics config? It's confusing to use the pickling APIs for this purpose.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <arcticdb/python/python_bindings_common.hpp>
#include <arcticdb/entity/metrics.hpp>
#include <arcticdb/util/preconditions.hpp>

namespace py = pybind11;

namespace arcticdb::apy {

py::tuple to_tuple(const MetricsConfig& config) {
return py::make_tuple(
config.host, config.port, config.job_name, config.instance, config.prometheus_env, config.model_
);
}

MetricsConfig metrics_config_from_tuple(const py::tuple& t) {
util::check(t.size() == 6, "Invalid MetricsConfig pickle object, expected 6 attributes but was {}", t.size());
return MetricsConfig{
t[static_cast<uint32_t>(MetricsConfigPickleOrder::HOST)].cast<std::string>(),
t[static_cast<uint32_t>(MetricsConfigPickleOrder::PORT)].cast<std::string>(),
t[static_cast<uint32_t>(MetricsConfigPickleOrder::JOB_NAME)].cast<std::string>(),
t[static_cast<uint32_t>(MetricsConfigPickleOrder::INSTANCE)].cast<std::string>(),
t[static_cast<uint32_t>(MetricsConfigPickleOrder::PROMETHEUS_ENV)].cast<std::string>(),
t[static_cast<uint32_t>(MetricsConfigPickleOrder::MODEL)].cast<MetricsConfig::Model>()
};
}

} // namespace arcticdb::apy

void register_metrics(py::module&& m, bool local_bindings) {

auto prometheus = m.def_submodule("prometheus");
py::class_<arcticdb::PrometheusInstance, std::shared_ptr<arcticdb::PrometheusInstance>>(
prometheus, "Instance", py::module_local(local_bindings)
);

py::class_<arcticdb::MetricsConfig, std::shared_ptr<arcticdb::MetricsConfig>>(
prometheus, "MetricsConfig", py::module_local(local_bindings)
)
.def(py::init<
const std::string&,
const std::string&,
const std::string&,
const std::string&,
const std::string&,
const arcticdb::MetricsConfig::Model>())
.def(py::pickle(
[](const arcticdb::MetricsConfig& config) { return arcticdb::apy::to_tuple(config); },
[](py::tuple t) { return arcticdb::apy::metrics_config_from_tuple(t); }
));

py::enum_<arcticdb::MetricsConfig::Model>(prometheus, "MetricsConfigModel", py::module_local(local_bindings))
.value("NO_INIT", arcticdb::MetricsConfig::Model::NO_INIT)
.value("PUSH", arcticdb::MetricsConfig::Model::PUSH)
.value("PULL", arcticdb::MetricsConfig::Model::PULL)
.export_values();

prometheus.def("metrics_config_from_tuple", &arcticdb::apy::metrics_config_from_tuple);
}
21 changes: 21 additions & 0 deletions cpp/arcticdb/python/python_bindings_common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include <pybind11/pybind11.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include <arcticdb/entity/metrics.hpp>

namespace arcticdb::apy {

enum class MetricsConfigPickleOrder : uint32_t {
HOST = 0,
PORT = 1,
JOB_NAME = 2,
INSTANCE = 3,
PROMETHEUS_ENV = 4,
MODEL = 5
};

pybind11::tuple to_tuple(const MetricsConfig& config);
MetricsConfig metrics_config_from_tuple(const pybind11::tuple& t);

} // namespace arcticdb::apy

void register_metrics(pybind11::module&& m, bool local_bindings);
24 changes: 2 additions & 22 deletions cpp/arcticdb/python/python_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <arcticdb/util/error_code.hpp>
#include <arcticdb/util/type_handler.hpp>
#include <arcticdb/python/python_handlers.hpp>
#include <arcticdb/python/python_bindings_common.hpp>
#include <arcticdb/arrow/arrow_handlers.hpp>
#include <arcticdb/util/pybind_mutex.hpp>

Expand Down Expand Up @@ -291,27 +292,6 @@ void register_instrumentation(py::module&& m) {
#endif
}

void register_metrics(py::module&& m) {

auto prometheus = m.def_submodule("prometheus");
py::class_<arcticdb::PrometheusInstance, std::shared_ptr<arcticdb::PrometheusInstance>>(prometheus, "Instance");

py::class_<arcticdb::MetricsConfig, std::shared_ptr<arcticdb::MetricsConfig>>(prometheus, "MetricsConfig")
.def(py::init<
const std::string&,
const std::string&,
const std::string&,
const std::string&,
const std::string&,
const arcticdb::MetricsConfig::Model>());

py::enum_<arcticdb::MetricsConfig::Model>(prometheus, "MetricsConfigModel")
.value("NO_INIT", arcticdb::MetricsConfig::Model::NO_INIT)
.value("PUSH", arcticdb::MetricsConfig::Model::PUSH)
.value("PULL", arcticdb::MetricsConfig::Model::PULL)
.export_values();
}

/// Register handling of non-trivial types. For more information @see arcticdb::TypeHandlerRegistry and
/// @see arcticdb::ITypeHandler
void register_type_handlers() {
Expand Down Expand Up @@ -379,7 +359,7 @@ PYBIND11_MODULE(arcticdb_ext, m) {
register_configs_map_api(m);
register_log(m.def_submodule("log"));
register_instrumentation(m.def_submodule("instrumentation"));
register_metrics(m.def_submodule("metrics"));
register_metrics(m.def_submodule("metrics"), false);
register_type_handlers();

register_termination_handler();
Expand Down
Loading
Loading