Skip to content

Commit

Permalink
Decision (#30)
Browse files Browse the repository at this point in the history
* decisions

* decision in progress, added Request template param to exchange_handler to pass extended requests

* decisions

* refactoring decision making

* decisions

* decision tree for actions

* variadic template and matched user data request example

* code style and some refactoring

* some requested changes added

* flexibility
  • Loading branch information
abushev authored and venediktov committed May 20, 2017
1 parent 89f48f2 commit ac21ea5
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 221 deletions.
21 changes: 10 additions & 11 deletions examples/bidder/response_builder.hpp → examples/bidder/bidder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,23 @@
#include <iostream>
#include "rtb/common/perf_timer.hpp"
#include "bidder_selector.hpp"
#include "examples/multiexchange/user_info.hpp"

namespace vanilla {
template<typename Config = BidderConfig, typename T = std::string>
class ResponseBuilder {
using BidRequest = openrtb::BidRequest<T>;
using BidResponse = openrtb::BidResponse<T>;
using Impression = openrtb::Impression<T>;
using SeatBid = openrtb::SeatBid<T>;
using Bid = openrtb::Bid<T>;
template<typename DSL, typename Config = BidderConfig>
class Bidder {
using BidRequest = typename DSL::deserialized_type;
using BidResponse = typename DSL::serialized_type;
using Impression = typename DSL::Impression;
using SeatBid = typename DSL::SeatBid;
using Bid = typename DSL::Bid;

public:
ResponseBuilder(BidderCaches<> &caches) :
Bidder(BidderCaches<Config> &caches) :
selector{caches}, uuid_generator{}
{
}

const BidResponse& build(const vanilla::VanillaRequest &vanilla_request) {
template <typename Request>
const BidResponse& bid(const Request &vanilla_request) {
response.clear();
const BidRequest &request = vanilla_request.bid_request;
for (auto &imp : request.imp) {
Expand Down
3 changes: 3 additions & 0 deletions examples/bidder/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct bidder_config_data {
std::string geo_campaign_source;
std::string campaign_data_source;
std::string campaign_data_ipc_name;
std::string key_value_host;
int key_value_port;
int timeout;
unsigned int concurrency;
short port;
Expand All @@ -34,6 +36,7 @@ struct bidder_config_data {
geo_source{}, geo_ipc_name{}, geo_campaign_ipc_name{},
geo_campaign_source{},
campaign_data_source{}, campaign_data_ipc_name{},
key_value_host{}, key_value_port{},
timeout{}, concurrency{},
port{}, host{}, root{}, num_of_bidders{}
{}
Expand Down
41 changes: 41 additions & 0 deletions examples/bidder/decision_exchange.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* File: decision_exchange.hpp
* Author: arseny.bushev@gmail.com
*
* Created on 9 мая 2017 г., 17:12
*/

#ifndef DECISION_EXCHANGE_HPP
#define DECISION_EXCHANGE_HPP

#include <rtb/common/decision_tree.hpp>

namespace vanilla {
namespace decision_exchange {
template <unsigned int SIZE, typename ...Args>
class decision_exchange {
static constexpr int tree_depth{static_cast<int>(SIZE)};
using decision_manager = vanilla::common::decision_tree_manager<tree_depth,Args...>;
public:
using decision_tree_type = typename decision_manager::decision_tree_type;
using decision_action = vanilla::common::decision_action<Args...>;

template <typename T>
decision_exchange(T &&decision_tree):
decision_tree{decision_tree}, manager{this->decision_tree}
{}
template<typename ...TArgs>
void exchange(TArgs && ...args) {
//decision_manager manager(decision_tree);
manager.execute(std::forward<TArgs>(args)...);
}
private:
decision_tree_type decision_tree;
decision_manager manager;

};
}
}

#endif /* DECISION_EXCHANGE_HPP */

87 changes: 51 additions & 36 deletions examples/bidder/http_bidder_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <vector>
#include <random>
#include <utility>
#include <boost/log/trivial.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
Expand All @@ -23,11 +24,15 @@
#include "examples/datacache/geo_entity.hpp"
#include "examples/datacache/city_country_entity.hpp"
#include "examples/datacache/ad_entity.hpp"
#include "bidder.hpp"

#include "rtb/common/perf_timer.hpp"
#include "config.hpp"
#include "serialization.hpp"
#include "bidder_selector.hpp"
#include "decision_exchange.hpp"
#include "rtb/client/empty_key_value_client.hpp"
#include "examples/multiexchange/user_info.hpp"


extern void init_framework_logging(const std::string &) ;
Expand All @@ -42,13 +47,16 @@ auto random_pick(int max) {
return dis(gen);
}

namespace bidder_decision_codes {
enum {EXIT=-1, USER_DATA=0, NO_BID, AUCTION_ASYNC, SIZE};
}

int main(int argc, char *argv[]) {
using namespace std::placeholders;
using namespace vanilla::exchange;
using namespace std::chrono_literals;
using restful_dispatcher_t = http::crud::crud_dispatcher<http::server::request, http::server::reply> ;
using BidRequest = openrtb::BidRequest<std::string>;
//using BidRequest = openrtb::BidRequest<std::string>;
using BidResponse = openrtb::BidResponse<std::string>;
using SeatBid = openrtb::SeatBid<std::string>;
using Bid = openrtb::Bid<std::string>;
Expand All @@ -71,6 +79,8 @@ int main(int argc, char *argv[]) {
("bidder.geo_campaign_source", boost::program_options::value<std::string>(&d.geo_campaign_source)->default_value("data/geo_campaign"), "geo_campaign_source file name")
("bidder.campaign_data_ipc_name", boost::program_options::value<std::string>(&d.campaign_data_ipc_name)->default_value("vanilla-campaign-data-ipc"), "campaign data ipc name")
("bidder.campaign_data_source", boost::program_options::value<std::string>(&d.campaign_data_source)->default_value("data/campaign_data"), "campaign_data_source file name")
("bidder.key_value_host", boost::program_options::value<std::string>(&d.key_value_host)->default_value("0.0.0.0"), "key value storage host")
("bidder.key_value_port", boost::program_options::value<int>(&d.key_value_port)->default_value(0), "key value storage port")
;
});

Expand All @@ -95,49 +105,54 @@ int main(int argc, char *argv[]) {
LOG(error) << e.what();
return 0;
}
exchange_handler<DSL::GenericDSL<>> bid_handler(std::chrono::milliseconds(config.data().timeout));

using bid_handler_type = exchange_handler<DSL::GenericDSL<>, vanilla::VanillaRequest>;
using BidRequest = bid_handler_type::auction_request_type;
using decision_exchange_type = vanilla::decision_exchange::decision_exchange<bidder_decision_codes::SIZE, http::server::reply&, BidRequest&>;

bid_handler_type bid_handler(std::chrono::milliseconds(config.data().timeout));

auto request_user_data_f = [&bid_handler, &config](http::server::reply &reply, BidRequest & bid_request) -> bool {
using kv_type = vanilla::client::empty_key_value_client;
thread_local kv_type kv_client;
bool is_matched_user = bid_request.user_info.user_id.length();
if (!is_matched_user) {
return true; // bid unmatched
}
if (!kv_client.connected()) {
kv_client.connect(config.data().key_value_host, config.data().key_value_port);
}
kv_client.request(bid_request.user_info.user_id, bid_request.user_info.user_data);
return true;
};
auto no_bid_f = [&bid_handler, &config](http::server::reply &reply, BidRequest & bid_request) -> bool {
reply << http::server::reply::flush("");
};
auto auction_async_f = [&bid_handler](http::server::reply &reply, BidRequest & bid_request) -> bool {
return bid_handler.handle_auction_async(reply, bid_request);
};
const decision_exchange_type::decision_tree_type decision_tree = {{
{bidder_decision_codes::USER_DATA, {request_user_data_f, bidder_decision_codes::AUCTION_ASYNC, bidder_decision_codes::NO_BID}},
{bidder_decision_codes::NO_BID, {no_bid_f, bidder_decision_codes::EXIT, bidder_decision_codes::EXIT}},
{bidder_decision_codes::AUCTION_ASYNC, {auction_async_f, bidder_decision_codes::EXIT, bidder_decision_codes::EXIT}}
}};
decision_exchange_type decision_exchange(decision_tree);

bid_handler
.logger([](const std::string &data) {
//LOG(debug) << "bid request=" << data ;
})
.error_logger([](const std::string &data) {
LOG(debug) << "bid request error " << data ;
})

.auction_async([&](const BidRequest &request) {

thread_local vanilla::BidderSelector<> selector(caches);
BidResponse response;
for(auto &imp : request.imp) {
if(auto ad = selector.select(request, imp)) {
boost::uuids::uuid bidid = uuid_generator();
response.bidid = boost::uuids::to_string(bidid);
if (request.cur.size()) {
response.cur = request.cur[0];
} else if (imp.bidfloorcur.length()) {
response.cur = imp.bidfloorcur; // Just return back
}
Bid bid;
bid.id = boost::uuids::to_string(bidid); // TODO check documentation
// Is it the same as response.bidid?
// Wrong filling type
bid.impid = imp.id;
bid.price = ad->max_bid_micros / 1000000.0; // Not micros?
bid.w = ad->width;
bid.h = ad->height;
bid.adm = ad->code;
bid.adid = ad->ad_id;
if (response.seatbid.size() == 0) {
SeatBid seatbid;
seatbid.bid.push_back(bid);
response.seatbid.push_back(seatbid);
} else {
response.seatbid.back().bid.push_back(bid);
}
}
}
return response;
});
thread_local vanilla::Bidder<DSL::GenericDSL<>, BidderConfig> bidder(caches);
return bidder.bid(request);
})
.decision([&decision_exchange](auto && ... args) {
decision_exchange.exchange(args...);
})
;

connection_endpoint ep {std::make_tuple(config.data().host, boost::lexical_cast<std::string>(config.data().port), config.data().root)};

Expand Down
8 changes: 4 additions & 4 deletions examples/bidder/multi_bidder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#else
#include <process.h>
#endif
#include "response_builder.hpp"
#include "bidder.hpp"
#include "examples/multiexchange/user_info.hpp"

#include "rtb/core/core.hpp"
Expand All @@ -46,10 +46,10 @@ using RtbBidderCaches = vanilla::BidderCaches<BidderConfig>;

void run(short port, RtbBidderCaches &bidder_caches) {
using namespace vanilla::messaging;
vanilla::ResponseBuilder<BidderConfig> response_builder(bidder_caches);
communicator<broadcast>().inbound(port).process<vanilla::VanillaRequest>([&response_builder](auto endpoint, vanilla::VanillaRequest vanilla_request) {
vanilla::Bidder<DSL::GenericDSL<>, BidderConfig> bidder(bidder_caches);
communicator<broadcast>().inbound(port).process<vanilla::VanillaRequest>([&bidder](auto endpoint, vanilla::VanillaRequest vanilla_request) {
LOG(debug) << "Request from user " << vanilla_request.user_info.user_id;
return response_builder.build(vanilla_request);
return bidder.bid(vanilla_request);
}).dispatch();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/multiexchange/multi_exchange_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int main(int argc, char* argv[]) {
if(request.user) {
vanilla_request.user_info.user_id = request.user.get().buyeruid;
}
vanilla::multibidder_communicator<> communicator(
vanilla::multibidder_communicator<DSL::GenericDSL<> > communicator(
config.data().bidders_port,
std::chrono::milliseconds(config.data().bidders_response_timeout)
);
Expand Down
3 changes: 2 additions & 1 deletion examples/multiexchange/user_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

#include <string>
#include "rtb/core/bid_request.hpp"
#include "rtb/DSL/generic_dsl.hpp"

namespace vanilla {
struct UserInfo {
std::string user_id{};
std::string user_data{};
};

using VanillaRequest = vanilla::BidRequest<vanilla::UserInfo>;
using VanillaRequest = vanilla::BidRequest<DSL::GenericDSL<std::string>, vanilla::UserInfo>;
}


Expand Down
9 changes: 9 additions & 0 deletions rtb/DSL/generic_dsl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
* GNU General Public License for more details.
*/

#ifndef RTB_DSL_HPP
#define RTB_DSL_HPP

#include "core/openrtb.hpp"
#include "encoders.hpp"
#include <vector>
Expand All @@ -34,15 +37,19 @@ namespace DSL {
//BidRequest
using Banner = openrtb::Banner<T>;
using AdPosition = openrtb::AdPosition;
public:
using Impression = openrtb::Impression<T>;
private:
using User = openrtb::User<T>;
using Geo = openrtb::Geo<T>;
using Site = openrtb::Site<T>;
using Publisher = openrtb::Publisher<T>;
using BidRequest = openrtb::BidRequest<T>;
//BidResponse
public:
using Bid = openrtb::Bid<T>;
using SeatBid = openrtb::SeatBid<T>;
private:
using NoBidReason = openrtb::NoBidReason;
using CreativeAttribute= openrtb::CreativeAttribute;
using BidResponse = openrtb::BidResponse<T>;
Expand Down Expand Up @@ -194,3 +201,5 @@ namespace DSL {
};

} //namespace

#endif

0 comments on commit ac21ea5

Please sign in to comment.