-// Copyright (C) 2012-2020 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
/// or equal to this value will be included
///
/// @return collection of IPv4 leases
- virtual Lease4Collection getLeases4ByRelayId(const OptionBuffer& relay_id,
- const asiolink::IOAddress& lower_bound_address,
- const LeasePageSize& page_size,
- const time_t& qry_start_time = 0,
- const time_t& qry_end_time = 0) = 0;
+ virtual Lease4Collection
+ getLeases4ByRelayId(const OptionBuffer& relay_id,
+ const asiolink::IOAddress& lower_bound_address,
+ const LeasePageSize& page_size,
+ const time_t& qry_start_time = 0,
+ const time_t& qry_end_time = 0) = 0;
/// @brief Returns existing IPv4 leases with a given remote-id.
///
/// or equal to this value will be included. Defaults to zero.
///
/// @return collection of IPv4 leases
- virtual Lease4Collection getLeases4ByRemoteId(const OptionBuffer& remote_id,
- const asiolink::IOAddress& lower_bound_address,
- const LeasePageSize& page_size,
- const time_t& qry_start_time = 0,
- const time_t& qry_end_time = 0) = 0;
+ virtual Lease4Collection
+ getLeases4ByRemoteId(const OptionBuffer& remote_id,
+ const asiolink::IOAddress& lower_bound_address,
+ const LeasePageSize& page_size,
+ const time_t& qry_start_time = 0,
+ const time_t& qry_end_time = 0) = 0;
/// @brief Returns existing IPv6 leases with a given relay-id.
///
/// @param page_size maximum size of the page returned.
///
/// @return collection of IPv6 leases
- virtual Lease6Collection getLeases6ByRelayId(const DUID& relay_id,
- const asiolink::IOAddress& link_addr,
- const asiolink::IOAddress& lower_bound_address,
- const LeasePageSize& page_size) = 0;
+ virtual Lease6Collection
+ getLeases6ByRelayId(const DUID& relay_id,
+ const asiolink::IOAddress& link_addr,
+ const asiolink::IOAddress& lower_bound_address,
+ const LeasePageSize& page_size) = 0;
/// @brief Returns existing IPv6 leases with a given remote-id.
///
/// @param page_size maximum size of the page returned.
///
/// @return collection of IPv6 leases
- virtual Lease6Collection getLeases6ByRemoteId(const OptionBuffer& remote_id,
- const asiolink::IOAddress& link_addr,
- const asiolink::IOAddress& lower_bound_address,
- const LeasePageSize& page_size) = 0;
+ virtual Lease6Collection
+ getLeases6ByRemoteId(const OptionBuffer& remote_id,
+ const asiolink::IOAddress& link_addr,
+ const asiolink::IOAddress& lower_bound_address,
+ const LeasePageSize& page_size) = 0;
/// @brief Returns existing IPv6 leases with on a given link.
///
/// @param page_size maximum size of the page returned.
///
/// @return collection of IPv6 leases
- virtual Lease6Collection getLeases6ByLink(const asiolink::IOAddress& link_addr,
- const asiolink::IOAddress& lower_bound_address,
- const LeasePageSize& page_size) = 0;
+ virtual Lease6Collection
+ getLeases6ByLink(const asiolink::IOAddress& link_addr,
+ const asiolink::IOAddress& lower_bound_address,
+ const LeasePageSize& page_size) = 0;
+
+ /// @brief Write V4 leases to a file.
+ ///
+ /// @param filename File name to write leases.
+ virtual void writeLeases4(const std::string& filename) = 0;
+
+ /// @brief Write V6 leases to a file.
+ ///
+ /// @param filename File name to write leases.
+ virtual void writeLeases6(const std::string& filename) = 0;
private:
/// The IOService object, used for all ASIO operations.
isc_throw(NotImplemented, "Memfile_LeaseMgr::getLeases6ByLink not implemented");
}
+void
+Memfile_LeaseMgr::writeLeases4(const std::string& filename) {
+ if (MultiThreadingMgr::instance().getMode()) {
+ std::lock_guard<std::mutex> lock(*mutex_);
+ writeLeases4Internal(filename);
+ } else {
+ writeLeases4Internal(filename);
+ }
+}
+
+void
+Memfile_LeaseMgr::writeLeases4Internal(const std::string& filename) {
+ if (lease_file4_->getFilename() == filename) {
+ lease_file4_->close();
+ }
+ try {
+ std::ostringstream old;
+ old << filename << ".bak" << getpid();
+ ::rename(filename.c_str(), old.str().c_str());
+ CSVLeaseFile4 backup(filename);
+ backup.open();
+ for (const auto& lease : storage4_) {
+ backup.append(*lease);
+ }
+ backup.close();
+ } catch (const std::exception&) {
+ if (lease_file4_->getFilename() == filename) {
+ lease_file4_->open(true);
+ }
+ throw;
+ }
+}
+
+void
+Memfile_LeaseMgr::writeLeases6(const std::string& filename) {
+ if (MultiThreadingMgr::instance().getMode()) {
+ std::lock_guard<std::mutex> lock(*mutex_);
+ writeLeases6Internal(filename);
+ } else {
+ writeLeases6Internal(filename);
+ }
+}
+
+void
+Memfile_LeaseMgr::writeLeases6Internal(const std::string& filename) {
+ if (lease_file6_->getFilename() == filename) {
+ lease_file6_->close();
+ }
+ try {
+ std::ostringstream old;
+ old << filename << ".bak" << getpid();
+ ::rename(filename.c_str(), old.str().c_str());
+ CSVLeaseFile6 backup(filename);
+ backup.open();
+ for (const auto& lease : storage6_) {
+ backup.append(*lease);
+ }
+ backup.close();
+ } catch (const std::exception&) {
+ if (lease_file6_->getFilename() == filename) {
+ lease_file6_->open(true);
+ }
+ throw;
+ }
+}
+
} // namespace dhcp
} // namespace isc
/// @brief stores IPv6 leases
Lease6Storage storage6_;
+protected:
/// @brief Holds the pointer to the DHCPv4 lease file IO.
boost::shared_ptr<CSVLeaseFile4> lease_file4_;
getLeases6ByLink(const asiolink::IOAddress& link_addr,
const asiolink::IOAddress& lower_bound_address,
const LeasePageSize& page_size) override;
+
+ /// @brief Write V4 leases to a file.
+ ///
+ /// @param filename File name to write leases.
+ virtual void writeLeases4(const std::string& filename) override;
+
+ /// @brief Write V6 leases to a file.
+ ///
+ /// @param filename File name to write leases.
+ virtual void writeLeases6(const std::string& filename) override;
+
+private:
+ /// @brief Write V4 leases to a file.
+ ///
+ /// @param filename File name to write leases.
+ /// Must be called from a thread-safe context.
+ virtual void writeLeases4Internal(const std::string& filename);
+
+ /// @brief Write V6 leases to a file.
+ ///
+ /// @param filename File name to write leases.
+ /// Must be called from a thread-safe context.
+ virtual void writeLeases6Internal(const std::string& filename);
};
} // namespace dhcp
isc_throw(NotImplemented, "MySqlLeaseMgr::clearClassLeaseCounts() not implemented");
}
+void
+MySqlLeaseMgr::writeLeases4(const std::string&) {
+ isc_throw(NotImplemented, "MySqlLeaseMgr::writeLeases4() not implemented");
+}
+
+void
+MySqlLeaseMgr::writeLeases6(const std::string&) {
+ isc_throw(NotImplemented, "MySqlLeaseMgr::writeLeases6() not implemented");
+}
+
LeaseStatsQueryPtr
MySqlLeaseMgr::startLeaseStatsQuery4() {
// Get a context
/// @brief Clears the class-lease count map.
virtual void clearClassLeaseCounts() override;
+ /// @brief Write V4 leases to a file.
+ virtual void writeLeases4(const std::string& /*filename*/) override;
+
+ /// @brief Write V6 leases to a file.
+ virtual void writeLeases6(const std::string& /*filename*/) override;
+
/// @brief Check Error and Throw Exception
///
/// This method invokes @ref MySqlConnection::checkError.
isc_throw(NotImplemented, "PgSqlLeaseMgr::clearClassLeaseCounts() not implemented");
}
+void
+PgSqlLeaseMgr::writeLeases4(const std::string&) {
+ isc_throw(NotImplemented, "PgSqlLeaseMgr::writeLeases4() not implemented");
+}
+
+void
+PgSqlLeaseMgr::writeLeases6(const std::string&) {
+ isc_throw(NotImplemented, "PgSqlLeaseMgr::writeLeases6() not implemented");
+}
+
LeaseStatsQueryPtr
PgSqlLeaseMgr::startLeaseStatsQuery4() {
// Get a context
const asiolink::IOAddress& lower_bound_address,
const LeasePageSize& page_size) override;
+ /// @brief Write V4 leases to a file.
+ virtual void writeLeases4(const std::string& /*filename*/) override;
+
+ /// @brief Write V6 leases to a file.
+ virtual void writeLeases6(const std::string& /*filename*/) override;
+
/// @brief Context RAII Allocator.
class PgSqlLeaseContextAlloc {
public:
isc_throw(NotImplemented, "ConcreteLeaseMgr::clearClassLeaseCounts() not implemented");
}
-
/// @brief Stub implementation.
Lease4Collection
getLeases4ByRelayId(const OptionBuffer& /* relay_id */,
const IOAddress& /* lower_bound_address */,
const LeasePageSize& /* page_size */) override {
isc_throw(NotImplemented, "ConcreteLeaseMgr::getLeases6ByLink not implemented");
+
+ /// @brief Pretends to write V4 leases to a file.
+ virtual void writeLeases4(const std::string&) override {
+ isc_throw(NotImplemented, "ConcreteLeaseMgr::writeLeases4() not implemented");
+ }
+
+ /// @brief Pretends to write V6 leases to a file.
+ virtual void writeLeases6(const std::string&) override {
+ isc_throw(NotImplemented, "ConcreteLeaseMgr::writeLeases6() not implemented");
}
/// @brief Returns backend type.
EXPECT_EQ(2, memfile_mgr->getClassLeaseCount("slice", Lease::TYPE_PD));
}
+/// @brief Class derived from @c Memfile_LeaseMgr to test write to file.
+class WFMemfileLeaseMgr : public Memfile_LeaseMgr {
+public:
+
+ /// @brief Constructor.
+ WFMemfileLeaseMgr(const DatabaseConnection::ParameterMap& parameters)
+ : Memfile_LeaseMgr(parameters) {
+ }
+
+ using Memfile_LeaseMgr::lease_file4_;
+ using Memfile_LeaseMgr::lease_file6_;
+};
+
+//////// test plan:
+// bad file, basic, overwrite file, not-persistent x v4/v6
+
} // namespace