Search in sources :

Example 1 with Bid

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;
}
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 Bid

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;
}
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 Bid

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));
}
Also used : NexmarkConfiguration(org.apache.beam.sdk.nexmark.NexmarkConfiguration) KV(org.apache.beam.sdk.values.KV) QueryPlanner(org.apache.beam.sdk.extensions.sql.impl.QueryPlanner) ZetaSQLQueryPlanner(org.apache.beam.sdk.extensions.sql.zetasql.ZetaSQLQueryPlanner) Bid(org.apache.beam.sdk.nexmark.model.Bid) SelectEvent(org.apache.beam.sdk.nexmark.model.sql.SelectEvent) PCollection(org.apache.beam.sdk.values.PCollection) SqlTransform(org.apache.beam.sdk.extensions.sql.SqlTransform) Schema(org.apache.beam.sdk.schemas.Schema) Convert(org.apache.beam.sdk.schemas.transforms.Convert) Filter(org.apache.beam.sdk.transforms.Filter) CalciteQueryPlanner(org.apache.beam.sdk.extensions.sql.impl.CalciteQueryPlanner) Event(org.apache.beam.sdk.nexmark.model.Event) Preconditions.checkState(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState) TupleTag(org.apache.beam.sdk.values.TupleTag) TypeDescriptors(org.apache.beam.sdk.values.TypeDescriptors) PCollectionTuple(org.apache.beam.sdk.values.PCollectionTuple) NexmarkQueryTransform(org.apache.beam.sdk.nexmark.queries.NexmarkQueryTransform) NexmarkQueryUtil(org.apache.beam.sdk.nexmark.queries.NexmarkQueryUtil) Row(org.apache.beam.sdk.values.Row) Schema(org.apache.beam.sdk.schemas.Schema) TupleTag(org.apache.beam.sdk.values.TupleTag) Row(org.apache.beam.sdk.values.Row) Bid(org.apache.beam.sdk.nexmark.model.Bid) SelectEvent(org.apache.beam.sdk.nexmark.model.sql.SelectEvent)

Example 4 with Bid

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);
}
Also used : Instant(org.joda.time.Instant) Bid(org.apache.beam.sdk.nexmark.model.Bid)

Example 5 with Bid

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);
    }
}
Also used : KV(org.apache.beam.sdk.values.KV) PCollection(org.apache.beam.sdk.values.PCollection) TimestampedValue(org.apache.beam.sdk.values.TimestampedValue) NexmarkConfiguration(org.apache.beam.sdk.nexmark.NexmarkConfiguration) Event(org.apache.beam.sdk.nexmark.model.Event) Bid(org.apache.beam.sdk.nexmark.model.Bid) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

Bid (org.apache.beam.sdk.nexmark.model.Bid)12 AuctionBid (org.apache.beam.sdk.nexmark.model.AuctionBid)5 DoFn (org.apache.beam.sdk.transforms.DoFn)5 KV (org.apache.beam.sdk.values.KV)5 Auction (org.apache.beam.sdk.nexmark.model.Auction)4 NexmarkConfiguration (org.apache.beam.sdk.nexmark.NexmarkConfiguration)3 Event (org.apache.beam.sdk.nexmark.model.Event)3 PCollection (org.apache.beam.sdk.values.PCollection)3 TimestampedValue (org.apache.beam.sdk.values.TimestampedValue)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 Instant (org.joda.time.Instant)2 Test (org.junit.Test)2 Category (org.junit.experimental.categories.Category)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 SqlTransform (org.apache.beam.sdk.extensions.sql.SqlTransform)1 CalciteQueryPlanner (org.apache.beam.sdk.extensions.sql.impl.CalciteQueryPlanner)1 QueryPlanner (org.apache.beam.sdk.extensions.sql.impl.QueryPlanner)1