Browse Source

[3798] assigned-addresses stat modifications in AllocEngine

Tomek Mrugalski 10 years ago
parent
commit
4608140690
2 changed files with 125 additions and 1 deletions
  1. 15 1
      src/lib/dhcpsrv/alloc_engine.cc
  2. 110 0
      src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc

+ 15 - 1
src/lib/dhcpsrv/alloc_engine.cc

@@ -20,11 +20,12 @@
 #include <dhcpsrv/host.h>
 #include <dhcpsrv/lease_mgr_factory.h>
 #include <dhcp/dhcp6.h>
-
+#include <stats/stats_mgr.h>
 #include <hooks/server_hooks.h>
 #include <hooks/hooks_manager.h>
 
 #include <cstring>
+#include <sstream>
 #include <limits>
 #include <vector>
 #include <stdint.h>
@@ -1546,6 +1547,12 @@ AllocEngine::requestLease4(AllocEngine::ClientContext4& ctx) {
     if (new_lease && client_lease) {
         ctx.old_lease_ = Lease4Ptr(new Lease4(*client_lease));
         lease_mgr.deleteLease(client_lease->addr_);
+
+        // Need to decrease statistic for assigned addresses.
+        std::stringstream name;
+        name << "subnet[" << ctx.subnet_->getID() << "].assigned-addresses";
+        isc::stats::StatsMgr::instance().addValue(name.str(),
+                                                  static_cast<uint64_t>(-1));
     }
 
     // Return the allocated lease or NULL pointer if allocation was
@@ -1622,6 +1629,13 @@ AllocEngine::createLease4(const ClientContext4& ctx, const IOAddress& addr) {
         // That is a real (REQUEST) allocation
         bool status = LeaseMgrFactory::instance().addLease(lease);
         if (status) {
+
+            // The lease insertion succeeded, let's bump up the statistic.
+            std::stringstream name;
+            name << "subnet[" << ctx.subnet_->getID() << "].assigned-addresses";
+            isc::stats::StatsMgr::instance().addValue(name.str(),
+                                                      static_cast<uint64_t>(1));
+
             return (lease);
         } else {
             // One of many failures with LeaseMgr (e.g. lost connection to the

+ 110 - 0
src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc

@@ -15,10 +15,12 @@
 #include <config.h>
 #include <dhcpsrv/tests/alloc_engine_utils.h>
 #include <dhcpsrv/tests/test_utils.h>
+#include <stats/stats_mgr.h>
 
 using namespace std;
 using namespace isc::hooks;
 using namespace isc::asiolink;
+using namespace isc::stats;
 
 namespace isc {
 namespace dhcp {
@@ -1503,6 +1505,114 @@ TEST_F(AllocEngine4Test, findReservation) {
     EXPECT_FALSE(ctx.host_);
 }
 
+// This test checks if the simple IPv4 allocation can succeed and that
+// statistic for allocated addresses is increased appropriately.
+TEST_F(AllocEngine4Test, simpleAlloc4Stats) {
+    boost::scoped_ptr<AllocEngine> engine;
+    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
+                                                 100, false)));
+    ASSERT_TRUE(engine);
+
+    AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_, IOAddress("0.0.0.0"),
+                                    false, true, "somehost.example.com.", false);
+
+    // Let's pretend 100 addresses were allocated already
+    stringstream name;
+    name << "subnet[" << subnet_->getID() << "].assigned-addresses";
+    StatsMgr::instance().addValue(name.str(), static_cast<uint64_t>(100));
+
+    Lease4Ptr lease = engine->allocateLease4(ctx);
+    // The new lease has been allocated, so the old lease should not exist.
+    EXPECT_FALSE(ctx.old_lease_);
+
+    // Check that we got a lease
+    ASSERT_TRUE(lease);
+
+    // The statistic should be there and it should be increased by 1 (to 101).
+    ObservationPtr stat = StatsMgr::instance().getObservation(name.str());
+    ASSERT_TRUE(stat);
+    EXPECT_EQ(101, stat->getInteger().first);
+}
+
+// This test checks if the fake allocation (for DISCOVER) can succeed
+// and that it doesn't increase allocated-addresses statistic.
+TEST_F(AllocEngine4Test, fakeAlloc4Stat) {
+    boost::scoped_ptr<AllocEngine> engine;
+    ASSERT_NO_THROW(engine.reset(new AllocEngine(AllocEngine::ALLOC_ITERATIVE,
+                                                 100, false)));
+    ASSERT_TRUE(engine);
+
+    AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_,
+                                    IOAddress("0.0.0.0"), false, true,
+                                    "host.example.com.", true);
+
+    // Let's pretend 100 addresses were allocated already
+    stringstream name;
+    name << "subnet[" << subnet_->getID() << "].assigned-addresses";
+    StatsMgr::instance().addValue(name.str(), static_cast<uint64_t>(100));
+
+    Lease4Ptr lease = engine->allocateLease4(ctx);
+
+    // The new lease has been allocated, so the old lease should not exist.
+    EXPECT_FALSE(ctx.old_lease_);
+
+    // Check that we got a lease
+    ASSERT_TRUE(lease);
+
+    // The statistic should be there and it should not be increased
+    // (should be still equal to 100).
+    ObservationPtr stat = StatsMgr::instance().getObservation(name.str());
+    ASSERT_TRUE(stat);
+    EXPECT_EQ(100, stat->getInteger().first);
+}
+
+// This test checks that the allocated-addresses statistic is decreased when
+// the client has a lease and a reservation for a different address is
+// available.
+TEST_F(AllocEngine4Test, reservedAddressExistingLeaseStat) {
+    // Create the reservation for the client.
+    HostPtr host(new Host(&hwaddr_->hwaddr_[0], hwaddr_->hwaddr_.size(),
+                          Host::IDENT_HWADDR, subnet_->getID(),
+                          SubnetID(0), IOAddress("192.0.2.123")));
+    CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host);
+    CfgMgr::instance().commit();
+
+    // Create a lease for the client with a different address than the reserved
+    // one.
+    Lease4Ptr lease(new Lease4(IOAddress("192.0.2.101"), hwaddr_,
+                               &clientid_->getClientId()[0],
+                               clientid_->getClientId().size(),
+                               100, 30, 60, time(NULL), subnet_->getID(),
+                               false, false, ""));
+    LeaseMgrFactory::instance().addLease(lease);
+
+    AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false);
+
+    // Let's pretend 100 addresses were allocated already
+    stringstream name;
+    name << "subnet[" << subnet_->getID() << "].assigned-addresses";
+    StatsMgr::instance().addValue(name.str(), static_cast<uint64_t>(100));
+
+    // Request allocation of the reserved address.
+    AllocEngine::ClientContext4 ctx(subnet_, clientid_, hwaddr_,
+                                    IOAddress("192.0.2.123"), false, false,
+                                    "", false);
+    AllocEngine::findReservation(ctx);
+    Lease4Ptr allocated_lease = engine.allocateLease4(ctx);
+
+    ASSERT_TRUE(allocated_lease);
+
+    // The statistic should be still at 100. Note that it was decreased
+    // (because old lease was removed), but also increased (because the
+    // new lease was immediately allocated).
+    ObservationPtr stat = StatsMgr::instance().getObservation(name.str());
+    ASSERT_TRUE(stat);
+    EXPECT_EQ(100, stat->getInteger().first);
+
+    // Lets' double check that the actual allocation took place.
+    EXPECT_FALSE(ctx.fake_allocation_);
+}
+
 }; // namespace test
 }; // namespace dhcp
 }; // namespace isc