RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
Public Types | Public Member Functions | Public Attributes | Protected Types | Protected Attributes
RTBKIT::Router Struct Reference

#include <router.h>

Inheritance diagram for RTBKIT::Router:
Datacratic::ServiceBase RTBKIT::MonitorProvider Datacratic::EventRecorder

List of all members.

Public Types

typedef std::map< std::string,
AgentInfo
Agents
typedef TimeoutMap< Id,
AuctionInfo
InFlight
typedef std::function< void(const
AgentInfoEntry &info)> 
OnAgentFn

Public Member Functions

 Router (ServiceBase &parent, const std::string &serviceName="router", double secondsUntilLossAssumed=2.0, bool connectPostAuctionLoop=true)
 Router (std::shared_ptr< ServiceProxies > services, const std::string &serviceName="router", double secondsUntilLossAssumed=2.0, bool connectPostAuctionLoop=true)
double secondsUntilLossAssumed () const
void setSecondsUntilLossAssumed (double newValue)
std::shared_ptr< BankergetBanker () const
void setBanker (const std::shared_ptr< Banker > &newBanker)
void init ()
void bindTcp ()
void bindAgents (std::string agentUri)
void bindAugmentors (const std::string &uri)
void unsafeDisableMonitor ()
virtual void start (boost::function< void()> onStop=boost::function< void()>())
virtual void sleepUntilIdle ()
void issueTimestamp ()
virtual size_t numNonIdle () const
virtual void shutdown ()
template<typename F >
void forAllExchanges (F functor)
void connectExchange (ExchangeConnector &exchange)
void addExchange (ExchangeConnector *exchange)
void addExchange (ExchangeConnector &exchange)
void addExchange (std::unique_ptr< ExchangeConnector > &&exchange)
void startExchange (const std::string &exchangeType, const Json::Value &exchangeConfig)
void startExchange (const Json::Value &exchangeConfig)
void injectAuction (std::shared_ptr< Auction > auction, double lossTime=INFINITY)
std::shared_ptr< AuctioninjectAuction (Auction::HandleAuction onAuctionFinished, std::shared_ptr< BidRequest > request, const std::string &requestStr, const std::string &requestStrFormat, double startTime=0.0, double expiryTime=0.0, double lossTime=INFINITY)
void notifyFinishedAuction (const Id &auctionId)
int numAuctionsInProgress () const
int numAuctionsAwaitingResult () const
Json::Value getStats () const
Json::Value getAgentInfo (const std::string &agent) const
Json::Value getAllAgentInfo () const
Json::Value getAccountInfo (const AccountKey &account) const
void setGlobalBidProbability (double val)
void setBidsErrorRate (double val)
void setBudgetErrorRate (double val)
void setAcceptAuctionProbability (double val)
virtual Json::Value getServiceStatus () const
virtual void submitToPostAuctionService (std::shared_ptr< Auction > auction, Id auctionId, const Auction::Response &bid)
void updateAllAgents ()
AuctionInfoaddAuction (std::shared_ptr< Auction > auction, Date timeout)
void run ()
void handleAgentMessage (const std::vector< std::string > &message)
void checkDeadAgents ()
void checkExpiredAuctions ()
void returnErrorResponse (const std::vector< std::string > &message, const std::string &error)
void doShutdown ()
std::shared_ptr< AugmentationInfopreprocessAuction (const std::shared_ptr< Auction > &auction)
void augmentAuction (const std::shared_ptr< AugmentationInfo > &info)
void doStartBidding (const std::vector< std::string > &message)
void doStartBidding (const std::shared_ptr< AugmentationInfo > &augInfo)
void doSubmitted (std::shared_ptr< Auction > auction)
void doBid (const std::vector< std::string > &message)
void doPong (int level, const std::vector< std::string > &message)
void sendPings ()
void doStats (const std::vector< std::string > &message)
void onNewAuction (std::shared_ptr< Auction > auction)
void onAuctionDone (std::shared_ptr< Auction > auction)
void doConfig (const std::string &agent, std::shared_ptr< const AgentConfig > config)
void unconfigure (const std::string &agent, const AgentConfig &config)
void configure (const std::string &agent, AgentConfig &config)
template<typename... Args>
void sendAgentMessage (const std::string &agent, const std::string &messageType, const Date &date, Args...args)
void sendBidResponse (const std::string &agent, const AgentInfo &info, BidStatus status, Date timestamp, const std::string &message, const Id &auctionId, int spotNum=-1, Amount price=Amount(), const Auction *auction=0, const std::string &bidData="", const Json::Value &metadata=Json::Value(), const std::string &augmentationsStr="")
void forEachAgent (const OnAgentFn &onAgent) const
void forEachAccountAgent (const AccountKey &account, const OnAgentFn &onAgent) const
AgentInfoEntry getAgentEntry (const std::string &agent) const
void throwException (const std::string &key, const std::string &fmt,...) __attribute__((__noreturn__))
template<typename... Args>
void logRouterError (const std::string &function, const std::string &exception, Args...args)
template<typename... Args>
void logMessage (const std::string &channel, Args...args)
template<typename... Args>
void logMessageNoTimestamp (const std::string &channel, Args...args)
void debugAuction (const Id &auction, const std::string &type, const std::vector< std::string > &args=std::vector< std::string >())
void debugAuctionImpl (const Id &auction, const std::string &type, const std::vector< std::string > &args)
void debugSpot (const Id &auction, const Id &spot, const std::string &type, const std::vector< std::string > &args=std::vector< std::string >())
void debugSpotImpl (const Id &auction, const Id &spot, const std::string &type, const std::vector< std::string > &args)
void expireDebugInfo ()
void dumpAuction (const Id &auction) const
void dumpSpot (const Id &auction, const Id &spot) const
Date getCurrentTime () const
std::string getProviderName () const
Json::Value getProviderIndicators () const

Public Attributes

boost::function< void(double)> acceptAuctionProbabilityFn
std::function< void(std::shared_ptr
< Auction >, Id,
Auction::Response)> 
onSubmittedAuction
ZmqNamedClientBus agentEndpoint
ZmqNamedProxy postAuctionEndpoint
Agents agents
ML::RingBufferSRMW< std::pair
< std::string, std::shared_ptr
< const AgentConfig > > > 
configBuffer
ML::RingBufferSRMW
< std::shared_ptr
< AugmentationInfo > > 
startBiddingBuffer
ML::RingBufferSRMW
< std::shared_ptr< Auction > > 
submittedBuffer
ML::RingBufferSWMR
< std::shared_ptr< Auction > > 
auctionGraveyard
ML::Wakeup_Fd wakeupMainLoop
AugmentationLoop augmentationLoop
Blacklist blacklist
InFlight inFlight
DutyCycleEntry dutyCycleCurrent
std::vector< DutyCycleEntrydutyCycleHistory
Lock lock
std::shared_ptr< Bankerbanker
double secondsUntilLossAssumed_
double globalBidProbability
double bidsErrorRate
double budgetErrorRate
bool connectPostAuctionLoop
AllAgentInfoallAgents
GcLock allAgentsGc
AgentConfigurationListener configListener
bool initialized
std::vector< std::shared_ptr
< ExchangeConnector > > 
exchanges
ZmqNamedPublisher logger
bool doDebug
ML::Spinlock debugLock
TimeoutMap< Id, AuctionDebugInfodebugInfo
uint64_t numAuctions
uint64_t numBids
uint64_t numNonEmptyBids
uint64_t numAuctionsWithBid
uint64_t numNoPotentialBidders
uint64_t numNoBidders
MonitorClient monitorClient
Date slowModeLastAuction
int slowModeCount
MonitorProviderClient monitorProviderClient

Protected Types

typedef std::recursive_mutex Lock
typedef std::unique_lock< Lock > Guard

Protected Attributes

boost::scoped_ptr< boost::thread > runThread
boost::scoped_ptr< boost::thread > cleanupThread
int shutdown_

Detailed Description

An RTB router. Contains everything needed to run auctions.

Definition at line 97 of file router.h.


Member Typedef Documentation

typedef std::map<std::string, AgentInfo> RTBKIT::Router::Agents

Map from the configured name of the agent to the agent info.

Definition at line 364 of file router.h.

List of auctions we're currently tracking as active.

Definition at line 378 of file router.h.


Member Function Documentation

AuctionInfo & RTBKIT::Router::addAuction ( std::shared_ptr< Auction auction,
Date  timeout 
)

Add the given auction to our data structures.

Definition at line 1540 of file router.cc.

void RTBKIT::Router::addExchange ( ExchangeConnector exchange) [inline]

Register the exchange with the router and make it take ownership of it

Definition at line 182 of file router.h.

void RTBKIT::Router::addExchange ( ExchangeConnector exchange) [inline]

Register the exchange with the router. The router will not take ownership of the exchange, which means that it needs to be freed by the calling code after the router has exited.

Definition at line 193 of file router.h.

void RTBKIT::Router::addExchange ( std::unique_ptr< ExchangeConnector > &&  exchange) [inline]

Register the exchange

Definition at line 201 of file router.h.

void RTBKIT::Router::augmentAuction ( const std::shared_ptr< AugmentationInfo > &  info)

Send the auction for augmentation. Once that is done, doStartBidding will be called.

Definition at line 1081 of file router.cc.

void RTBKIT::Router::bindAgents ( std::string  agentUri)

Bind a zeroMQ URI for the agent to listen on.

Definition at line 255 of file router.cc.

void RTBKIT::Router::bindAugmentors ( const std::string &  uri)

Bind a zeroMQ URI to listen for augmentation messages on.

Definition at line 267 of file router.cc.

Bind to TCP/IP ports and publish where to connect to.

Definition at line 247 of file router.cc.

void RTBKIT::Router::configure ( const std::string &  agent,
AgentConfig config 
)

Add the given agent (with the given configuration) to the configuration structures.

Definition at line 2331 of file router.cc.

void RTBKIT::Router::connectExchange ( ExchangeConnector exchange) [inline]

Connect the exchange connector to the router, but do not make the router know about or own the exchange.

Used mostly for testing where we want to control the exchange connector objects independently of the router.

This method should almost never be used, as the given exchange will not participate in validation of bidding agent configuration.

Definition at line 175 of file router.h.

void RTBKIT::Router::doBid ( const std::vector< std::string > &  message)

An agent bid on an auction. Arrange for this bid to be recorded.

Definition at line 1571 of file router.cc.

void RTBKIT::Router::doConfig ( const std::string &  agent,
std::shared_ptr< const AgentConfig config 
)

Got a configuration message; update our internal data structures

Definition at line 2289 of file router.cc.

void RTBKIT::Router::doPong ( int  level,
const std::vector< std::string > &  message 
)

An agent responded to a ping message. Arrange for the ping time to be recorded.

Definition at line 2449 of file router.cc.

void RTBKIT::Router::doStartBidding ( const std::vector< std::string > &  message)

We've finished augmenting our auctions. Allow the agents to bid on them.

Definition at line 1268 of file router.cc.

void RTBKIT::Router::doStartBidding ( const std::shared_ptr< AugmentationInfo > &  augInfo)

Ditto but taking the augmented auction directly.

Definition at line 1277 of file router.cc.

void RTBKIT::Router::doStats ( const std::vector< std::string > &  message)

Someone wants stats.

Definition at line 1028 of file router.cc.

void RTBKIT::Router::doSubmitted ( std::shared_ptr< Auction auction)

Auction has been submitted. Do the final cleanup here and send it off to the post auction loop.

Definition at line 2002 of file router.cc.

template<typename F >
void RTBKIT::Router::forAllExchanges ( functor) [inline]

Iterate exchanges

Definition at line 161 of file router.h.

void RTBKIT::Router::forEachAccountAgent ( const AccountKey account,
const OnAgentFn &  onAgent 
) const

Call the given callback for each agent that is bidding on the given account.

Definition at line 2540 of file router.cc.

void RTBKIT::Router::forEachAgent ( const OnAgentFn &  onAgent) const

Call the given callback for each agent.

Definition at line 2529 of file router.cc.

Return information about all agents bidding on the given account.

AgentInfoEntry RTBKIT::Router::getAgentEntry ( const std::string &  agent) const

Find the information entry for the given agent. All elements are guaranteed to be valid until the object is destroyed.

Definition at line 2558 of file router.cc.

Json::Value RTBKIT::Router::getAgentInfo ( const std::string &  agent) const

Return information about a given agent.

Definition at line 2405 of file router.cc.

Return information about all agents.

Definition at line 2412 of file router.cc.

string RTBKIT::Router::getProviderName ( ) const [virtual]

MonitorProvider interface

Implements RTBKIT::MonitorProvider.

Definition at line 2696 of file router.cc.

Return service status.

Reimplemented from Datacratic::ServiceBase.

Definition at line 1074 of file router.cc.

Return a stats object that tells us what's going on.

Definition at line 2390 of file router.cc.

Initialize all of the internal data structures and configuration.

Definition at line 177 of file router.cc.

void RTBKIT::Router::injectAuction ( std::shared_ptr< Auction auction,
double  lossTime = INFINITY 
)

Inject an auction into the router. auction: the auction object lossTime: the time at which, if no win was forthcoming, a loss will be assumed and the message sent. If the time is infinity then no message will be sent; if it is equal to 0.0 then secondsSinceLossAssumed will be added to the current time and that value used.

Definition at line 690 of file router.cc.

std::shared_ptr< Auction > RTBKIT::Router::injectAuction ( Auction::HandleAuction  onAuctionFinished,
std::shared_ptr< BidRequest request,
const std::string &  requestStr,
const std::string &  requestStrFormat,
double  startTime = 0.0,
double  expiryTime = 0.0,
double  lossTime = INFINITY 
)

Inject an auction into the router given its components.

onAuctionFinished: this is the callback that will be called once the auction is finished id: string ID to represent the auction. Must be globally unique for the router. request: JSON string with the bid request startTime: time at which the auction starts; if empty the current time will be used expiryTime: time at which the auction expires; if empty the startTime + 30ms will be used. lossTime: the time at which, if no win was forthcoming, a loss will be assumed and the LOSS message sent. If the time is infinity then no message will be sent; if it is equal to 0.0 then secondsSinceLossAssumed will be added to the current time and that value used.

Returns the created auction object.

Note that no error handling will be done on an invalid request; the auction will be silently dropped and the callback never called.

This function does not deal with timeouts. If you want the auction to time out, you should call notifyTooLate on the returned auction at the desired time, externally.

Definition at line 719 of file router.cc.

Simple logging method to output the current time on stderr.

Definition at line 372 of file router.cc.

template<typename... Args>
void RTBKIT::Router::logMessage ( const std::string &  channel,
Args...  args 
) [inline]

Log a given message to the given channel.

Definition at line 568 of file router.h.

template<typename... Args>
void RTBKIT::Router::logMessageNoTimestamp ( const std::string &  channel,
Args...  args 
) [inline]

Log a given message to the given channel.

Definition at line 577 of file router.h.

template<typename... Args>
void RTBKIT::Router::logRouterError ( const std::string &  function,
const std::string &  exception,
Args...  args 
) [inline]

Log a router error.

Definition at line 552 of file router.h.

void RTBKIT::Router::notifyFinishedAuction ( const Id auctionId)

Notify the router that the given auction will never receive another message and should be forgotten. This is mostly for simulation so that the router knows when it can shut down.

Normally finishedSpot should be called, not finishedAuction. In particular, this message will NOT remove anything from the finished auction structures, which is where auctions end up after a bid.

There is no penalty for not calling this, apart from potentially having to wait one hour before the simulation decides that it is finished.

If the auction doesn't exist, the message will be silently ignored.

Definition at line 743 of file router.cc.

Return the number of auctions awaiting a win/loss message.

Return the number of auctions in progress.

Definition at line 750 of file router.cc.

size_t RTBKIT::Router::numNonIdle ( ) const [virtual]

How many things (auctions, etc) are non-idle?

Definition at line 342 of file router.cc.

void RTBKIT::Router::onAuctionDone ( std::shared_ptr< Auction auction)

An auction finished.

Definition at line 2233 of file router.cc.

void RTBKIT::Router::onNewAuction ( std::shared_ptr< Auction auction)

We got a new auction.

Definition at line 2179 of file router.cc.

std::shared_ptr< AugmentationInfo > RTBKIT::Router::preprocessAuction ( const std::shared_ptr< Auction > &  auction)

Perform initial auction processing to see how it can be used. Returns a null pointer if the auction has no potential bidders.

This can be called from any thread.

Definition at line 1113 of file router.cc.

template<typename... Args>
void RTBKIT::Router::sendAgentMessage ( const std::string &  agent,
const std::string &  messageType,
const Date date,
Args...  args 
) [inline]

Send the given message to the given bidding agent.

Definition at line 465 of file router.h.

void RTBKIT::Router::sendBidResponse ( const std::string &  agent,
const AgentInfo info,
BidStatus  status,
Date  timestamp,
const std::string &  message,
const Id auctionId,
int  spotNum = -1,
Amount  price = Amount(),
const Auction auction = 0,
const std::string &  bidData = "",
const Json::Value metadata = Json::Value(),
const std::string &  augmentationsStr = "" 
)

Send the given bid response to the given bidding agent.

Definition at line 2485 of file router.cc.

Send out a "ping" message to each agent, and interpret the results of the previous set of pings (do we need to throttle down?)

Definition at line 2428 of file router.cc.

void RTBKIT::Router::setAcceptAuctionProbability ( double  val) [inline]

Auction accept probability

Definition at line 317 of file router.h.

void RTBKIT::Router::setBidsErrorRate ( double  val) [inline]

Proportion of bids that should be rejected with an arbitrary error.

Definition at line 304 of file router.h.

void RTBKIT::Router::setBudgetErrorRate ( double  val) [inline]

Proportion of bids that should be rejected with an out of budget error.

Definition at line 309 of file router.h.

void RTBKIT::Router::setGlobalBidProbability ( double  val) [inline]

Multiplier for the bid probability of all agents.

Definition at line 299 of file router.h.

void RTBKIT::Router::sleepUntilIdle ( ) [virtual]

Sleep until the router is idle (there are no more auctions and all connections are idle).

Definition at line 359 of file router.cc.

void RTBKIT::Router::start ( boost::function< void()>  onStop = boost::function<void ()>()) [virtual]

Start the router running in a separate thread. The given function will be called when the thread is stopped.

Definition at line 289 of file router.cc.

void RTBKIT::Router::startExchange ( const std::string &  exchangeType,
const Json::Value exchangeConfig 
)

Start up a new exchange and connect it to the router. The exchange will read its configuration from the given JSON blob.

Definition at line 2718 of file router.cc.

void RTBKIT::Router::startExchange ( const Json::Value exchangeConfig)

Start up a new exchange and connect it to the router. The exchange will read its configuration and type from the given JSON blob.

Definition at line 2731 of file router.cc.

void RTBKIT::Router::submitToPostAuctionService ( std::shared_ptr< Auction auction,
Id  auctionId,
const Auction::Response bid 
) [virtual]

Function to pass a submitted auction on to the post auction loop.

Definition at line 2572 of file router.cc.

void RTBKIT::Router::throwException ( const std::string &  key,
const std::string &  fmt,
  ... 
)

Throw an exception and log the error in Graphite and in the router log file.

Definition at line 2612 of file router.cc.

void RTBKIT::Router::unconfigure ( const std::string &  agent,
const AgentConfig config 
)

Remove the given agent (with the given configuration) from the configuration structures.

Definition at line 2325 of file router.cc.

Disable the monitor for testing purposes. In production this could lead to unbounded overspend, so please do really only use it for testing.

Definition at line 279 of file router.cc.


Member Data Documentation

boost::function<void (double)> RTBKIT::Router::acceptAuctionProbabilityFn

Overwrite this function such that it causes the number of auctions coming in to be throttled.

Definition at line 314 of file router.h.

Pointer to current version. Protected by allAgentsGc.

Definition at line 504 of file router.h.

RCU protection for allAgents.

Definition at line 507 of file router.h.

Listen for changes in configuration and let the router know about them.

Definition at line 527 of file router.h.

Debug only

Definition at line 624 of file router.h.

std::vector<std::shared_ptr<ExchangeConnector> > RTBKIT::Router::exchanges

List of exchanges that are active.

Definition at line 533 of file router.h.

Are we initialized?

Definition at line 530 of file router.h.

std::function<void (std::shared_ptr<Auction>, Id, Auction::Response)> RTBKIT::Router::onSubmittedAuction

Function to override if other behaviour than sending a response to the post auction loop is desired.

Definition at line 332 of file router.h.


The documentation for this struct was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator