Use DiscreteClock in ResourceManager

This commit is contained in:
Vinnie Falco
2013-10-18 15:58:17 -07:00
parent 466e623dd6
commit fc5be2b911
8 changed files with 49 additions and 112 deletions

View File

@@ -1654,6 +1654,7 @@
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\src\ripple\algorithm\api\DiscreteClock.h" />
<ClInclude Include="..\..\src\ripple\http\api\Handler.h" /> <ClInclude Include="..\..\src\ripple\http\api\Handler.h" />
<ClInclude Include="..\..\src\ripple\http\api\Server.h" /> <ClInclude Include="..\..\src\ripple\http\api\Server.h" />
<ClInclude Include="..\..\src\ripple\http\api\Port.h" /> <ClInclude Include="..\..\src\ripple\http\api\Port.h" />
@@ -1708,7 +1709,6 @@
<ClInclude Include="..\..\src\ripple\resource\impl\Kind.h" /> <ClInclude Include="..\..\src\ripple\resource\impl\Kind.h" />
<ClInclude Include="..\..\src\ripple\resource\impl\Logic.h" /> <ClInclude Include="..\..\src\ripple\resource\impl\Logic.h" />
<ClInclude Include="..\..\src\ripple\resource\impl\LogicType.h" /> <ClInclude Include="..\..\src\ripple\resource\impl\LogicType.h" />
<ClInclude Include="..\..\src\ripple\resource\impl\SimpleMonotonicClock.h" />
<ClInclude Include="..\..\src\ripple\resource\impl\Tuning.h" /> <ClInclude Include="..\..\src\ripple\resource\impl\Tuning.h" />
<ClInclude Include="..\..\src\ripple\resource\ripple_resource.h" /> <ClInclude Include="..\..\src\ripple\resource\ripple_resource.h" />
<ClInclude Include="..\..\src\ripple\rpc\api\Handler.h" /> <ClInclude Include="..\..\src\ripple\rpc\api\Handler.h" />

View File

@@ -247,6 +247,9 @@
<Filter Include="[1] Ripple\resource\impl"> <Filter Include="[1] Ripple\resource\impl">
<UniqueIdentifier>{28a8ede0-743b-4a9a-bae1-5b2bc03ee44e}</UniqueIdentifier> <UniqueIdentifier>{28a8ede0-743b-4a9a-bae1-5b2bc03ee44e}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="[1] Ripple\algorithm">
<UniqueIdentifier>{d7d4123e-3fb2-4f85-9596-1ae26b6c14fd}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\ripple_basics\containers\RangeSet.cpp"> <ClCompile Include="..\..\src\ripple_basics\containers\RangeSet.cpp">
@@ -1715,9 +1718,6 @@
<ClInclude Include="..\..\src\ripple_core\functional\LoadSource.h"> <ClInclude Include="..\..\src\ripple_core\functional\LoadSource.h">
<Filter>[1] Ripple\ripple_core\functional</Filter> <Filter>[1] Ripple\ripple_core\functional</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple_core\functional\LoadType.h">
<Filter>[1] Ripple\ripple_core\functional</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple_app\main\LoadManager.h"> <ClInclude Include="..\..\src\ripple_app\main\LoadManager.h">
<Filter>[1] Ripple\ripple_app\main</Filter> <Filter>[1] Ripple\ripple_app\main</Filter>
</ClInclude> </ClInclude>
@@ -2283,9 +2283,6 @@
<ClInclude Include="..\..\src\ripple\resource\impl\Entry.h"> <ClInclude Include="..\..\src\ripple\resource\impl\Entry.h">
<Filter>[1] Ripple\resource\impl</Filter> <Filter>[1] Ripple\resource\impl</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\resource\impl\SimpleMonotonicClock.h">
<Filter>[1] Ripple\resource\impl</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\resource\api\Fees.h"> <ClInclude Include="..\..\src\ripple\resource\api\Fees.h">
<Filter>[1] Ripple\resource\api</Filter> <Filter>[1] Ripple\resource\api</Filter>
</ClInclude> </ClInclude>
@@ -2295,9 +2292,6 @@
<ClInclude Include="..\..\src\ripple\resource\impl\Logic.h"> <ClInclude Include="..\..\src\ripple\resource\impl\Logic.h">
<Filter>[1] Ripple\resource\impl</Filter> <Filter>[1] Ripple\resource\impl</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\resource\impl\LogicType.h">
<Filter>[1] Ripple\resource\impl</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\resource\api\Gossip.h"> <ClInclude Include="..\..\src\ripple\resource\api\Gossip.h">
<Filter>[1] Ripple\resource\api</Filter> <Filter>[1] Ripple\resource\api</Filter>
</ClInclude> </ClInclude>
@@ -2307,6 +2301,12 @@
<ClInclude Include="..\..\src\ripple\resource\api\LegacyFees.h"> <ClInclude Include="..\..\src\ripple\resource\api\LegacyFees.h">
<Filter>[1] Ripple\resource\api</Filter> <Filter>[1] Ripple\resource\api</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\ripple\algorithm\api\DiscreteClock.h">
<Filter>[1] Ripple\algorithm</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\resource\impl\LogicType.h">
<Filter>[1] Ripple\resource\impl</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CustomBuild Include="..\..\src\ripple_data\protocol\ripple.proto"> <CustomBuild Include="..\..\src\ripple_data\protocol\ripple.proto">

View File

@@ -53,12 +53,14 @@ public:
typedef SharedData <State> SharedState; typedef SharedData <State> SharedState;
SharedState m_state; SharedState m_state;
DiscreteClock <DiscreteTime> m_clock;
Journal m_journal; Journal m_journal;
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
Logic (Journal journal) Logic (DiscreteClock <DiscreteTime>::Source& source, Journal journal)
: m_journal (journal) : m_clock (source)
, m_journal (journal)
{ {
#if 1 #if 1
#if BEAST_MSVC #if BEAST_MSVC
@@ -83,11 +85,6 @@ public:
state->table.clear(); state->table.clear();
} }
virtual DiscreteTime get_now ()
{
return 0;
}
Consumer newInboundEndpoint (IPEndpoint const& address) Consumer newInboundEndpoint (IPEndpoint const& address)
{ {
if (isWhitelisted (address)) if (isWhitelisted (address))
@@ -216,7 +213,7 @@ public:
Gossip exportConsumers () Gossip exportConsumers ()
{ {
DiscreteTime const now (get_now()); DiscreteTime const now (m_clock());
Gossip gossip; Gossip gossip;
SharedState::Access state (m_state); SharedState::Access state (m_state);
@@ -242,7 +239,7 @@ public:
void importConsumers (std::string const& origin, Gossip const& gossip) void importConsumers (std::string const& origin, Gossip const& gossip)
{ {
DiscreteTime const now (get_now()); DiscreteTime const now (m_clock());
{ {
SharedState::Access state (m_state); SharedState::Access state (m_state);
@@ -311,7 +308,7 @@ public:
{ {
SharedState::Access state (m_state); SharedState::Access state (m_state);
DiscreteTime const now (get_now()); DiscreteTime const now (m_clock());
for (List <Entry>::iterator iter ( for (List <Entry>::iterator iter (
state->inactive.begin()); iter != state->inactive.end();) state->inactive.begin()); iter != state->inactive.end();)
@@ -390,7 +387,7 @@ public:
break; break;
} }
state->inactive.push_back (entry); state->inactive.push_back (entry);
entry.whenExpires = get_now() + secondsUntilExpiration; entry.whenExpires = m_clock() + secondsUntilExpiration;
} }
} }
@@ -405,7 +402,7 @@ public:
Disposition charge (Entry& entry, Charge const& fee, SharedState::Access& state) Disposition charge (Entry& entry, Charge const& fee, SharedState::Access& state)
{ {
DiscreteTime const now (get_now()); DiscreteTime const now (m_clock());
int const balance (entry.add (fee.cost(), now)); int const balance (entry.add (fee.cost(), now));
m_journal.info << "Charging " << entry.label() << " for " << fee; m_journal.info << "Charging " << entry.label() << " for " << fee;
return disposition (balance); return disposition (balance);
@@ -414,7 +411,7 @@ public:
bool warn (Entry& entry, SharedState::Access& state) bool warn (Entry& entry, SharedState::Access& state)
{ {
bool notify (false); bool notify (false);
DiscreteTime const now (get_now()); DiscreteTime const now (m_clock());
if (entry.balance (now) >= warningThreshold && now != entry.lastWarningTime) if (entry.balance (now) >= warningThreshold && now != entry.lastWarningTime)
{ {
charge (entry, feeWarning, state); charge (entry, feeWarning, state);
@@ -431,7 +428,7 @@ public:
bool disconnect (Entry& entry, SharedState::Access& state) bool disconnect (Entry& entry, SharedState::Access& state)
{ {
bool drop (false); bool drop (false);
DiscreteTime const now (get_now()); DiscreteTime const now (m_clock());
if (entry.balance (now) >= dropThreshold) if (entry.balance (now) >= dropThreshold)
{ {
charge (entry, feeDrop, state); charge (entry, feeDrop, state);
@@ -442,7 +439,7 @@ public:
int balance (Entry& entry, SharedState::Access& state) int balance (Entry& entry, SharedState::Access& state)
{ {
return entry.balance (get_now()); return entry.balance (m_clock());
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@@ -511,7 +508,7 @@ public:
void onWrite (PropertyStream::Map& map) void onWrite (PropertyStream::Map& map)
{ {
DiscreteTime const now (get_now()); DiscreteTime const now (m_clock());
SharedState::Access state (m_state); SharedState::Access state (m_state);

View File

@@ -23,25 +23,23 @@
namespace ripple { namespace ripple {
namespace Resource { namespace Resource {
/** Provides the Clock required by Logic's get_now(). template <class DiscreteClockSourceType>
This allows the unit tests to provide its own manual clock. class LogicType
*/ : private BaseFromMember <DiscreteClockSourceType>
template <typename Clock> , public Logic
class LogicType : public Logic
{ {
public: public:
typedef typename DiscreteClockSourceType::DiscreteClockType DiscreteClockType;
explicit LogicType (Journal journal) explicit LogicType (Journal journal)
: Logic (journal) : Logic (BaseFromMember <DiscreteClockSourceType>::member(), journal)
{ {
} }
DiscreteTime get_now () DiscreteClockSourceType& clock()
{ {
return m_clock(); return BaseFromMember <DiscreteClockSourceType>::member();
} }
private:
Clock m_clock;
}; };
} }

View File

@@ -30,8 +30,7 @@ public:
ManagerImp (Journal journal) ManagerImp (Journal journal)
: Thread ("Resource::Manager") : Thread ("Resource::Manager")
, , m_journal (journal)
m_journal (journal)
, m_logic (journal) , m_logic (journal)
{ {
startThread (); startThread ();

View File

@@ -1,40 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_RESOURCE_SIMPLEMONOTONICCLOCK_H_INCLUDED
#define RIPPLE_RESOURCE_SIMPLEMONOTONICCLOCK_H_INCLUDED
namespace ripple {
namespace Resource {
/** Monotonically increasing time value. */
struct SimpleMonotonicClock
{
typedef int value_type;
value_type operator() () const
{
return value_type (RelativeTime::fromStartup().inSeconds());
}
};
}
}
#endif

View File

@@ -23,22 +23,6 @@ namespace Resource {
class Tests : public UnitTest class Tests : public UnitTest
{ {
public: public:
// A manually operated clock
class TestClock
{
public:
static int& now()
{
static int when (0);
return when;
}
int operator() () const
{
return now();
}
};
void createGossip (Gossip& gossip) void createGossip (Gossip& gossip)
{ {
int const v (10 + random().nextInt (10)); int const v (10 + random().nextInt (10));
@@ -58,8 +42,7 @@ public:
{ {
beginTestCase ("Imports"); beginTestCase ("Imports");
ScopedPointer <Logic> logic ( LogicType <ManualClock> logic (journal());
new LogicType <TestClock> (journal()));
Gossip g[5]; Gossip g[5];
@@ -67,7 +50,7 @@ public:
createGossip (g[i]); createGossip (g[i]);
for (int i = 0; i < 5; ++i) for (int i = 0; i < 5; ++i)
logic->importConsumers (String::fromNumber (i).toStdString(), g[i]); logic.importConsumers (String::fromNumber (i).toStdString(), g[i]);
pass(); pass();
} }
@@ -76,8 +59,7 @@ public:
{ {
beginTestCase ("Import"); beginTestCase ("Import");
ScopedPointer <Logic> logic ( LogicType <ManualClock> logic (journal());
new LogicType <TestClock> (journal()));
Gossip g; Gossip g;
Gossip::Item item; Gossip::Item item;
@@ -85,7 +67,7 @@ public:
item.address = IPEndpoint (IPEndpoint::V4 (207, 127, 82, 1)); item.address = IPEndpoint (IPEndpoint::V4 (207, 127, 82, 1));
g.items.push_back (item); g.items.push_back (item);
logic->importConsumers ("g", g); logic.importConsumers ("g", g);
pass(); pass();
} }
@@ -93,34 +75,34 @@ public:
void testCharges () void testCharges ()
{ {
beginTestCase ("Charge"); beginTestCase ("Charge");
ScopedPointer <Logic> logic (
new LogicType <TestClock> (journal())); LogicType <ManualClock> logic (journal());
{ {
IPEndpoint address (IPEndpoint::from_string ("207.127.82.1")); IPEndpoint address (IPEndpoint::from_string ("207.127.82.1"));
Consumer c (logic->newInboundEndpoint (address)); Consumer c (logic.newInboundEndpoint (address));
logMessage ("Charging " + c.label() + " 10,000 units"); logMessage ("Charging " + c.label() + " 10,000 units");
c.charge (10000); c.charge (10000);
for (int i = 0; i < 128; ++i) for (int i = 0; i < 128; ++i)
{ {
logMessage ( logMessage (
"Time = " + String::fromNumber (TestClock::now()) + "Time = " + String::fromNumber (logic.clock().now()) +
", Balance = " + String::fromNumber (c.balance())); ", Balance = " + String::fromNumber (c.balance()));
++TestClock::now(); ++logic.clock().now();
} }
} }
{ {
IPEndpoint address (IPEndpoint::from_string ("207.127.82.2")); IPEndpoint address (IPEndpoint::from_string ("207.127.82.2"));
Consumer c (logic->newInboundEndpoint (address)); Consumer c (logic.newInboundEndpoint (address));
logMessage ("Charging " + c.label() + " 1000 units per second"); logMessage ("Charging " + c.label() + " 1000 units per second");
for (int i = 0; i < 128; ++i) for (int i = 0; i < 128; ++i)
{ {
c.charge (1000); c.charge (1000);
logMessage ( logMessage (
"Time = " + String::fromNumber (TestClock::now()) + "Time = " + String::fromNumber (logic.clock().now()) +
", Balance = " + String::fromNumber (c.balance())); ", Balance = " + String::fromNumber (c.balance()));
++TestClock::now(); ++logic.clock().now();
} }
} }
@@ -129,9 +111,9 @@ public:
void runTest () void runTest ()
{ {
//testCharges(); testCharges();
testImports(); testImports();
//testImport(); testImport();
} }
Tests () : UnitTest ("ResourceManager", "ripple", runManual) Tests () : UnitTest ("ResourceManager", "ripple", runManual)

View File

@@ -21,6 +21,8 @@
#include "ripple_resource.h" #include "ripple_resource.h"
#include "../algorithm/api/DiscreteClock.h"
#include "beast/modules/beast_core/system/BeforeBoost.h" #include "beast/modules/beast_core/system/BeforeBoost.h"
#include <boost/unordered_map.hpp> #include <boost/unordered_map.hpp>
@@ -31,7 +33,6 @@
# include "impl/Tuning.h" # include "impl/Tuning.h"
# include "impl/Entry.h" # include "impl/Entry.h"
# include "impl/Import.h" # include "impl/Import.h"
#include "impl/SimpleMonotonicClock.h"
#include "impl/Charge.cpp" #include "impl/Charge.cpp"
# include "impl/Logic.h" # include "impl/Logic.h"
# include "impl/LogicType.h" # include "impl/LogicType.h"