From: Tomek Mrugalski Date: Wed, 27 May 2015 11:38:02 +0000 (+0200) Subject: [3798] assigned-addresses stat modifications in AllocEngine X-Git-Tag: trac3830_base~9^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4608140690ea2ca5111ecc01b34ae87a0d3a6bf9;p=thirdparty%2Fkea.git [3798] assigned-addresses stat modifications in AllocEngine --- diff --git a/src/lib/dhcpsrv/alloc_engine.cc b/src/lib/dhcpsrv/alloc_engine.cc index 53d6cb0fe6..704eeebb7b 100644 --- a/src/lib/dhcpsrv/alloc_engine.cc +++ b/src/lib/dhcpsrv/alloc_engine.cc @@ -20,11 +20,12 @@ #include #include #include - +#include #include #include #include +#include #include #include #include @@ -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(-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(1)); + return (lease); } else { // One of many failures with LeaseMgr (e.g. lost connection to the diff --git a/src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc b/src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc index 01b678cca7..0fe00d66d2 100644 --- a/src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc +++ b/src/lib/dhcpsrv/tests/alloc_engine4_unittest.cc @@ -15,10 +15,12 @@ #include #include #include +#include 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 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(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 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(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(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