Skip to main content

The three readers of SourceSummaryService.read: the dialog, the cancel and the daily consume

This page lists the three readers of LedgerEntries::SourceSummaryService.read, and what each one takes from it.

Words used in this page​

WordMeaning
DeliverableThe product record that one spend pays for, here one Ads::CampaignPlacement, called the source in the code.
MarketingThe one person at Jod who runs Ads campaigns for clients in the Team Portal.
The headerOne LedgerEntries::SourceSummary: the totals of one deliverable, with the list of its lines.
A lineOne EntitlementLotAllocations::ReservedBalance: the units one deliverable still has reserved in one EntitlementLot.
Expired lotAn EntitlementLot whose expires_at is at or earlier than the expired_as_of: of the read.
Live lotAn EntitlementLot with no expires_at, or with an expires_at later than the expired_as_of: of the read.
The balance row locklock('FOR UPDATE') on the billing_entitlement_balances row, which consume and release take while they write.

Every use case below follows Acme's placement 901 through five moments.

MomentWho actsWhat is readThe answer
20 Nov to 29 Nov, the daily consumeThe daily consume jobThe lines of 901Lot 10 expires first, so the job takes 1 unit a day from lot 10, 10 units in all
30 Nov, the lot expiryBilling::ExpireEntitlementLotsJobNothing of 901The job expires only the units that no deliverable reserved, and lot 10 has none, so 901 still has 10 units reserved in it
30 Nov to 2 Dec, the daily consumeThe daily consume jobThe lines of 901Lot 10 still sorts first after it expires, so the job takes 3 more units from it
2 Dec, the dialogMarketing, through GET /team/billing/spendsThe header of 90117 units reserved, 13 consumed, and 7 of the 17 in an expired lot
2 Dec, the confirmEntitlementLotService.releaseThe lines of 90110 units go back to lot 11, and 7 units in lot 10 expire

Summary​

Three readers call the same SourceSummaryService.read, and each one takes a different part of the header.

  • US-1: As Marketing, I want the cancel dialog to show the units that will expire, so I decide first.
  • US-2: As Marketing, I want a cancel to give back units in live lots, so the company spends them again.
  • US-3: As the daily consume job, I want each day's units from the soonest-expiring lot, so no credit expires unused.
IDUse caseActorStory
UC-1Marketing sees the units in expired lots in the cancel dialogMarketing (Identities::Admin)US-1
UC-2A confirmed cancel gives back the units in live lots, and expires the restMarketing, then EntitlementLotService.releaseUS-2
UC-3The daily consume takes units from the soonest-expiring lot firstAds::CampaignPlacements::ConsumeReservedCreditsJobUS-3

Legend: The orange node is the one builder, and each lavender node is a reader.


UC-1: Marketing sees the units in expired lots in the cancel dialog​

FieldDetails
ActorMarketing (Identities::Admin)
TriggerMarketing opens the cancel dialog of the campaign of placement 901 on 2 Dec

System behaviour:

  1. The campaign page calls GET /team/billing/spends?source_type=Ads::CampaignPlacement&source_ids[]=901.
  2. Billing::Spends::TeamIndexManager opens a :repeatable_read transaction.
  3. It calls SourceSummaryService.read with source_ids: [901] and expired_as_of: Time.current.
  4. The serializer writes five fields of the header, and no line.
  5. The dialog shows the row of 901: 10 units go back, 7 expire and 13 are already used.
    • The web works out the units that go back: units_reserved minus units_reserved_in_expired_lots, 17 − 7 = 10.
    • The dialog warns that the cancel will expire 7 reserved units of the campaign.
{ "billing_spends": [{ "source_type": "Ads::CampaignPlacement", "source_id": 901, "units_reserved": 17, "units_consumed": 13, "units_reserved_in_expired_lots": 7 }] }

Business rules:

  • The :repeatable_read transaction runs all three queries on the same committed data.
    • So a consume that commits between two queries cannot make the numbers disagree.
  • The lines stay in Ruby, because no user story asks for a table per lot.
    • A later page that wants one changes only the serializer.
  • Marketing rejects a campaign in review, or cancels a campaign that runs.
    • Billing D15 — An admin can cancel a running ad campaign allows the cancel of a campaign that runs.
    • Each dialog shows one row for each placement of the campaign.
    • The cancel dialog shows the units that go back, the units that expire and the units already used.
    • The reject dialog shows only the first two, because a campaign in review has used no units.
  • A failed read keeps the confirm button of the dialog disabled.

UC-2: A confirmed cancel gives back the units in live lots, and expires the rest​

FieldDetails
ActorMarketing confirms, and EntitlementLotService.release acts
TriggerMarketing confirms the cancel of the campaign of placement 901 on 2 Dec

System behaviour (one transaction, under the balance row lock):

  1. release calls SourceSummaryService.read with source_ids: [901] and expired_as_of: Time.current, and takes the first header.
  2. release gives the units of every line back first, expired or not.
  3. Then release picks the lines with lot_expired?(as_of: expired_as_of) of the header, and expires their units.
StepLines on 2 DecWhat release writes
2, every lotLot 10 → 7
Lot 11 → 10
One release entry with available_delta: 17 and reserved_delta: -17, and one release allocation for each lot
3, the expired lotsLot 10 → 7One expire entry with available_delta: -7 for lot 10, with the EntitlementLot as its source

After both writes, lot 11 has 10 more units available, and the 7 units of lot 10 are expired.

Business rules:

  • Released credits in an expired lot expire at once.
  • Marketing can cancel the campaign although placement 901 runs.
  • release already runs under the balance row lock, so it needs no :repeatable_read transaction of its own.
  • release does nothing when the read returns no header, or a header with units_reserved at 0.
    • A total below 0 never reaches release, because the read raises first.
    • The lines are all 1 or more, so they cannot add up to a total below 0.
    • The first cancel wrote reserved_delta: -17, so a second cancel finds units_reserved: 0.
    • release then returns before it reads any line.

UC-3: The daily consume takes units from the soonest-expiring lot first​

FieldDetails
ActorAds::CampaignPlacements::ConsumeReservedCreditsJob, through EntitlementLotService.consume
TriggerThe job runs for placement 901 on 29 Nov at 00:20, with target_units_consumed: 10

System behaviour (one transaction, under the balance row lock):

  1. consume calls SourceSummaryService.read with source_ids: [901] and expired_as_of: Time.current, and takes the first header.
  2. 901 has consumed 9 units so far, so consume must take 10 − 9 = 1 unit.
  3. The lines of the header come in the spend order of Billing, so consume sorts nothing.
  4. The lines are lot 10 → 11, then lot 11 → 10.
  5. consume takes the 1 unit from lot 10, and writes one consume ledger entry.

Business rules:

  • A lot with no expires_at sorts last.
  • The job passes a total to reach, not the units of one day.
    • A second run on 29 Nov finds 10 units consumed already, so it writes nothing.
  • consume raises when the lines run out and some units it must take are still not covered.
  • consume raises when the read returns no header, because the placement has no reserve entry.