use of org.apache.beam.sdk.nexmark.model.Bid in project beam by apache.
the class WinningBidsSimulator method nextWinningBid.
/**
* Return the next winning bid for an expired auction relative to {@code timestamp}. Return null
* if no more winning bids, in which case all expired auctions will have been removed from our
* state. Retire auctions in order of expire time.
*/
@Nullable
private TimestampedValue<AuctionBid> nextWinningBid(Instant timestamp) {
Map<Instant, List<Long>> toBeRetired = new TreeMap<>();
for (Map.Entry<Long, Auction> entry : openAuctions.entrySet()) {
if (entry.getValue().expires.compareTo(timestamp) <= 0) {
List<Long> idsAtTime = toBeRetired.computeIfAbsent(entry.getValue().expires, k -> new ArrayList<>());
idsAtTime.add(entry.getKey());
}
}
for (Map.Entry<Instant, List<Long>> entry : toBeRetired.entrySet()) {
for (long id : entry.getValue()) {
Auction auction = openAuctions.get(id);
NexmarkUtils.info("retiring auction: %s", auction);
openAuctions.remove(id);
Bid bestBid = bestBids.get(id);
if (bestBid != null) {
TimestampedValue<AuctionBid> result = TimestampedValue.of(new AuctionBid(auction, bestBid), auction.expires);
NexmarkUtils.info("winning: %s", result);
return result;
}
}
}
return null;
}
use of org.apache.beam.sdk.nexmark.model.Bid in project beam by apache.
the class WinningBidsSimulator method captureBestBid.
/**
* Try to account for {@code bid} in state. Return true if bid has now been accounted for by
* {@code bestBids}.
*/
private boolean captureBestBid(Bid bid, boolean shouldLog) {
if (closedAuctions.contains(bid.auction)) {
// Ignore bids for known, closed auctions.
if (shouldLog) {
NexmarkUtils.info("closed auction: %s", bid);
}
return true;
}
Auction auction = openAuctions.get(bid.auction);
if (auction == null) {
// winning or not.
if (shouldLog) {
NexmarkUtils.info("pending auction: %s", bid);
}
return false;
}
if (bid.price < auction.reserve) {
// Bid price is too low.
if (shouldLog) {
NexmarkUtils.info("below reserve: %s", bid);
}
return true;
}
Bid existingBid = bestBids.get(bid.auction);
if (existingBid == null || Bid.PRICE_THEN_DESCENDING_TIME.compare(existingBid, bid) < 0) {
// We've found a (new) best bid for a known auction.
bestBids.put(bid.auction, bid);
if (shouldLog) {
NexmarkUtils.info("new winning bid: %s", bid);
}
} else {
if (shouldLog) {
NexmarkUtils.info("ignoring low bid: %s", bid);
}
}
return true;
}
use of org.apache.beam.sdk.nexmark.model.Bid in project beam by apache.
the class SqlBoundedSideInputJoin method expand.
@Override
public PCollection<Bid> expand(PCollection<Event> events) {
PCollection<Row> bids = events.apply(Filter.by(NexmarkQueryUtil.IS_BID)).apply(getName() + ".SelectEvent", new SelectEvent(Event.Type.BID));
checkState(getSideInput() != null, "Configuration error: side input is null");
TupleTag<Row> sideTag = new TupleTag<Row>("side") {
};
TupleTag<Row> bidTag = new TupleTag<Row>("bid") {
};
Schema schema = Schema.of(Schema.Field.of("id", Schema.FieldType.INT64), Schema.Field.of("extra", Schema.FieldType.STRING));
PCollection<Row> sideRows = getSideInput().setSchema(schema, TypeDescriptors.kvs(TypeDescriptors.longs(), TypeDescriptors.strings()), kv -> Row.withSchema(schema).addValues(kv.getKey(), kv.getValue()).build(), row -> KV.of(row.getInt64("id"), row.getString("extra"))).apply("SideToRows", Convert.toRows());
return PCollectionTuple.of(bidTag, bids).and(sideTag, sideRows).apply(SqlTransform.query(String.format(query, configuration.sideInputRowCount)).withQueryPlannerClass(plannerClass)).apply("ResultToBid", Convert.fromRows(Bid.class));
}
use of org.apache.beam.sdk.nexmark.model.Bid in project beam by apache.
the class BidGenerator method nextBid.
/**
* Generate and return a random bid with next available id.
*/
public static Bid nextBid(long eventId, Random random, long timestamp, GeneratorConfig config) {
long auction;
// Here P(bid will be for a hot auction) = 1 - 1/hotAuctionRatio.
if (random.nextInt(config.getHotAuctionRatio()) > 0) {
// Choose the first auction in the batch of last HOT_AUCTION_RATIO auctions.
auction = (lastBase0AuctionId(eventId) / HOT_AUCTION_RATIO) * HOT_AUCTION_RATIO;
} else {
auction = nextBase0AuctionId(eventId, random, config);
}
auction += GeneratorConfig.FIRST_AUCTION_ID;
long bidder;
// Here P(bid will be by a hot bidder) = 1 - 1/hotBiddersRatio
if (random.nextInt(config.getHotBiddersRatio()) > 0) {
// Choose the second person (so hot bidders and hot sellers don't collide) in the batch of
// last HOT_BIDDER_RATIO people.
bidder = (lastBase0PersonId(eventId) / HOT_BIDDER_RATIO) * HOT_BIDDER_RATIO + 1;
} else {
bidder = nextBase0PersonId(eventId, random, config);
}
bidder += GeneratorConfig.FIRST_PERSON_ID;
long price = PriceGenerator.nextPrice(random);
int currentSize = 8 + 8 + 8 + 8;
String extra = nextExtra(random, currentSize, config.getAvgBidByteSize());
return new Bid(auction, bidder, price, new Instant(timestamp), extra);
}
use of org.apache.beam.sdk.nexmark.model.Bid in project beam by apache.
the class BoundedSideInputJoinTest method inputOutputSameEvents.
/**
* A smoke test that the count of input bids and outputs are the same, to help diagnose flakiness
* in more complex tests.
*/
@Test
@Category(NeedsRunner.class)
public void inputOutputSameEvents() throws Exception {
NexmarkConfiguration config = NexmarkConfiguration.DEFAULT.copy();
config.sideInputType = NexmarkUtils.SideInputType.DIRECT;
config.numEventGenerators = 1;
config.numEvents = 5000;
config.sideInputRowCount = 10;
config.sideInputNumShards = 3;
PCollection<KV<Long, String>> sideInput = NexmarkUtils.prepareSideInput(p, config);
try {
PCollection<Event> input = p.apply(NexmarkUtils.batchEventsSource(config));
PCollection<Bid> justBids = input.apply(NexmarkQueryUtil.JUST_BIDS);
PCollection<Long> bidCount = justBids.apply("Count Bids", Count.globally());
NexmarkQueryTransform<Bid> query = new BoundedSideInputJoin(config);
query.setSideInput(sideInput);
PCollection<TimestampedValue<Bid>> output = (PCollection<TimestampedValue<Bid>>) input.apply(new NexmarkQuery(config, query));
PCollection<Long> outputCount = output.apply("Count outputs", Count.globally());
PAssert.that(PCollectionList.of(bidCount).and(outputCount).apply(Flatten.pCollections())).satisfies(counts -> {
assertThat(Iterables.size(counts), equalTo(2));
assertThat(Iterables.get(counts, 0), greaterThan(0L));
assertThat(Iterables.get(counts, 0), equalTo(Iterables.get(counts, 1)));
return null;
});
p.run();
} finally {
NexmarkUtils.cleanUpSideInput(config);
}
}
Aggregations