Search in sources :

Example 1 with Auction

use of org.apache.beam.sdk.nexmark.model.Auction 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;
}
Also used : Auction(org.apache.beam.sdk.nexmark.model.Auction) Instant(org.joda.time.Instant) TreeMap(java.util.TreeMap) AuctionBid(org.apache.beam.sdk.nexmark.model.AuctionBid) ArrayList(java.util.ArrayList) List(java.util.List) Bid(org.apache.beam.sdk.nexmark.model.Bid) AuctionBid(org.apache.beam.sdk.nexmark.model.AuctionBid) TreeMap(java.util.TreeMap) Map(java.util.Map) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with Auction

use of org.apache.beam.sdk.nexmark.model.Auction 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;
}
Also used : Auction(org.apache.beam.sdk.nexmark.model.Auction) Bid(org.apache.beam.sdk.nexmark.model.Bid) AuctionBid(org.apache.beam.sdk.nexmark.model.AuctionBid)

Example 3 with Auction

use of org.apache.beam.sdk.nexmark.model.Auction in project beam by apache.

the class AuctionGenerator method nextAuction.

/**
 * Generate and return a random auction with next available id.
 */
public static Auction nextAuction(long eventsCountSoFar, long eventId, Random random, long timestamp, GeneratorConfig config) {
    long id = lastBase0AuctionId(eventId) + GeneratorConfig.FIRST_AUCTION_ID;
    long seller;
    // Here P(auction will be for a hot seller) = 1 - 1/hotSellersRatio.
    if (random.nextInt(config.getHotSellersRatio()) > 0) {
        // Choose the first person in the batch of last HOT_SELLER_RATIO people.
        seller = (lastBase0PersonId(eventId) / HOT_SELLER_RATIO) * HOT_SELLER_RATIO;
    } else {
        seller = nextBase0PersonId(eventId, random, config);
    }
    seller += GeneratorConfig.FIRST_PERSON_ID;
    long category = GeneratorConfig.FIRST_CATEGORY_ID + random.nextInt(NUM_CATEGORIES);
    long initialBid = nextPrice(random);
    long expires = timestamp + nextAuctionLengthMs(eventsCountSoFar, random, timestamp, config);
    String name = nextString(random, 20);
    String desc = nextString(random, 100);
    long reserve = initialBid + nextPrice(random);
    int currentSize = 8 + name.length() + desc.length() + 8 + 8 + 8 + 8 + 8;
    String extra = nextExtra(random, currentSize, config.getAvgAuctionByteSize());
    return new Auction(id, name, desc, initialBid, reserve, new Instant(timestamp), new Instant(expires), seller, category, extra);
}
Also used : Auction(org.apache.beam.sdk.nexmark.model.Auction) Instant(org.joda.time.Instant) StringsGenerator.nextString(org.apache.beam.sdk.nexmark.sources.generator.model.StringsGenerator.nextString)

Example 4 with Auction

use of org.apache.beam.sdk.nexmark.model.Auction in project beam by apache.

the class Query3 method expand.

@Override
public PCollection<NameCityStateId> expand(PCollection<Event> events) {
    PCollection<KV<Long, Event>> auctionsBySellerId = events.apply(NexmarkQueryUtil.JUST_NEW_AUCTIONS).apply(name + ".InCategory", Filter.by(auction -> auction.category == 10)).apply("EventByAuctionSeller", ParDo.of(new DoFn<Auction, KV<Long, Event>>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            Event e = new Event();
            e.newAuction = c.element();
            c.output(KV.of(c.element().seller, e));
        }
    }));
    PCollection<KV<Long, Event>> personsById = events.apply(NexmarkQueryUtil.JUST_NEW_PERSONS).apply(name + ".InState", Filter.by(person -> "OR".equals(person.state) || "ID".equals(person.state) || "CA".equals(person.state))).apply("EventByPersonId", ParDo.of(new DoFn<Person, KV<Long, Event>>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            Event e = new Event();
            e.newPerson = c.element();
            c.output(KV.of(c.element().id, e));
        }
    }));
    // Join auctions and people.
    return PCollectionList.of(auctionsBySellerId).and(personsById).apply(Flatten.pCollections()).apply(name + ".Join", ParDo.of(joinDoFn)).apply(name + ".Project", ParDo.of(new DoFn<KV<Auction, Person>, NameCityStateId>() {

        @ProcessElement
        public void processElement(ProcessContext c) {
            Auction auction = c.element().getKey();
            Person person = c.element().getValue();
            c.output(new NameCityStateId(person.name, person.city, person.state, auction.id));
        }
    }));
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) NameCityStateId(org.apache.beam.sdk.nexmark.model.NameCityStateId) Auction(org.apache.beam.sdk.nexmark.model.Auction) Event(org.apache.beam.sdk.nexmark.model.Event) KV(org.apache.beam.sdk.values.KV) Person(org.apache.beam.sdk.nexmark.model.Person)

Example 5 with Auction

use of org.apache.beam.sdk.nexmark.model.Auction in project beam by apache.

the class WinningBids method expand.

@Override
public PCollection<AuctionBid> expand(PCollection<Event> events) {
    // Window auctions and bids into custom auction windows. New people events will be discarded.
    // This will allow us to bring bids and auctions together irrespective of how long
    // each auction is open for.
    events = events.apply("Window", Window.into(auctionOrBidWindowFn));
    // Key auctions by their id.
    PCollection<KV<Long, Auction>> auctionsById = events.apply(NexmarkQueryUtil.JUST_NEW_AUCTIONS).apply("AuctionById:", NexmarkQueryUtil.AUCTION_BY_ID);
    // Key bids by their auction id.
    PCollection<KV<Long, Bid>> bidsByAuctionId = events.apply(NexmarkQueryUtil.JUST_BIDS).apply("BidByAuction", NexmarkQueryUtil.BID_BY_AUCTION);
    // Find the highest price valid bid for each closed auction.
    return // Join auctions and bids.
    KeyedPCollectionTuple.of(NexmarkQueryUtil.AUCTION_TAG, auctionsById).and(NexmarkQueryUtil.BID_TAG, bidsByAuctionId).apply(CoGroupByKey.create()).apply(name + ".Join", ParDo.of(new DoFn<KV<Long, CoGbkResult>, AuctionBid>() {

        private final Counter noAuctionCounter = Metrics.counter(name, "noAuction");

        private final Counter underReserveCounter = Metrics.counter(name, "underReserve");

        private final Counter noValidBidsCounter = Metrics.counter(name, "noValidBids");

        @ProcessElement
        public void processElement(ProcessContext c) {
            @Nullable Auction auction = c.element().getValue().getOnly(NexmarkQueryUtil.AUCTION_TAG, null);
            if (auction == null) {
                // We have bids without a matching auction. Give up.
                noAuctionCounter.inc();
                return;
            }
            // Find the current winning bid for auction.
            // The earliest bid with the maximum price above the reserve wins.
            Bid bestBid = null;
            for (Bid bid : c.element().getValue().getAll(NexmarkQueryUtil.BID_TAG)) {
                // Bids too late for their auction will have been
                // filtered out by the window merge function.
                checkState(bid.dateTime.compareTo(auction.expires) < 0);
                if (bid.price < auction.reserve) {
                    // Bid price is below auction reserve.
                    underReserveCounter.inc();
                    continue;
                }
                if (bestBid == null || Bid.PRICE_THEN_DESCENDING_TIME.compare(bid, bestBid) > 0) {
                    bestBid = bid;
                }
            }
            if (bestBid == null) {
                // We don't have any valid bids for auction.
                noValidBidsCounter.inc();
                return;
            }
            c.output(new AuctionBid(auction, bestBid));
        }
    }));
}
Also used : DoFn(org.apache.beam.sdk.transforms.DoFn) Counter(org.apache.beam.sdk.metrics.Counter) AuctionBid(org.apache.beam.sdk.nexmark.model.AuctionBid) Auction(org.apache.beam.sdk.nexmark.model.Auction) KV(org.apache.beam.sdk.values.KV) Bid(org.apache.beam.sdk.nexmark.model.Bid) AuctionBid(org.apache.beam.sdk.nexmark.model.AuctionBid) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CoGbkResult(org.apache.beam.sdk.transforms.join.CoGbkResult)

Aggregations

Auction (org.apache.beam.sdk.nexmark.model.Auction)7 AuctionBid (org.apache.beam.sdk.nexmark.model.AuctionBid)4 Bid (org.apache.beam.sdk.nexmark.model.Bid)4 DoFn (org.apache.beam.sdk.transforms.DoFn)4 KV (org.apache.beam.sdk.values.KV)4 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 Person (org.apache.beam.sdk.nexmark.model.Person)2 CoGbkResult (org.apache.beam.sdk.transforms.join.CoGbkResult)2 Instant (org.joda.time.Instant)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Counter (org.apache.beam.sdk.metrics.Counter)1 CategoryPrice (org.apache.beam.sdk.nexmark.model.CategoryPrice)1 Event (org.apache.beam.sdk.nexmark.model.Event)1 IdNameReserve (org.apache.beam.sdk.nexmark.model.IdNameReserve)1 NameCityStateId (org.apache.beam.sdk.nexmark.model.NameCityStateId)1 StringsGenerator.nextString (org.apache.beam.sdk.nexmark.sources.generator.model.StringsGenerator.nextString)1