Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Add Lease Timestamps #7305

Merged
merged 9 commits into from
Oct 28, 2024
Merged

Add Lease Timestamps #7305

merged 9 commits into from
Oct 28, 2024

Conversation

fsamuel-bs
Copy link
Contributor

@fsamuel-bs fsamuel-bs commented Oct 1, 2024

General

Before this PR:
Workflows that attempted to implement a queue-like behavior with atlas timestamps did not perform well. The main issue was that they needed to scan from the immutable timestamp to account for written timestamps for ongoing transactions.

After this PR:
There's a new feature, called leased timestamps. With it, we know, for a given workflow, the minimum timestamp of open transactions. With this information, the range of timestamps we need to scan is significantly reduced.

==COMMIT_MSG==
==COMMIT_MSG==

Priority:

Concerns / possible downsides (what feedback would you like?):

Is documentation needed?:

Compatibility

Does this PR create any API breaks (e.g. at the Java or HTTP layers) - if so, do we have compatibility?:

Does this PR change the persisted format of any data - if so, do we have forward and backward compatibility?:

The code in this PR may be part of a blue-green deploy. Can upgrades from previous versions safely coexist? (Consider restarts of blue or green nodes.):

Does this PR rely on statements being true about other products at a deployment - if so, do we have correct product dependencies on these products (or other ways of verifying that these statements are true)?:

Does this PR need a schema migration?

Testing and Correctness

What, if any, assumptions are made about the current state of the world? If they change over time, how will we find out?:

What was existing testing like? What have you done to improve it?:

If this PR contains complex concurrent or asynchronous code, is it correct? The onus is on the PR writer to demonstrate this.:

If this PR involves acquiring locks or other shared resources, how do we ensure that these are always released?:

Execution

How would I tell this PR works in production? (Metrics, logs, etc.):

Has the safety of all log arguments been decided correctly?:

Will this change significantly affect our spending on metrics or logs?:

How would I tell that this PR does not work in production? (monitors, etc.):

If this PR does not work as expected, how do I fix that state? Would rollback be straightforward?:

If the above plan is more complex than “recall and rollback”, please tag the support PoC here (if it is the end of the week, tag both the current and next PoC):

Scale

Would this PR be expected to pose a risk at scale? Think of the shopping product at our largest stack.:

Would this PR be expected to perform a large number of database calls, and/or expensive database calls (e.g., row range scans, concurrent CAS)?:

Would this PR ever, with time and scale, become the wrong thing to do - and if so, how would we know that we need to do something differently?:

Development Process

Where should we start reviewing?:

If this PR is in excess of 500 lines excluding versions lock-files, why does it not make sense to split it?:

Please tag any other people who should be aware of this PR:
@jeremyk-91
@sverma30
@raiju

@changelog-app
Copy link

changelog-app bot commented Oct 1, 2024

Generate changelog in changelog/@unreleased

What do the change types mean?
  • feature: A new feature of the service.
  • improvement: An incremental improvement in the functionality or operation of the service.
  • fix: Remedies the incorrect behaviour of a component of the service in a backwards-compatible way.
  • break: Has the potential to break consumers of this service's API, inclusive of both Palantir services
    and external consumers of the service's API (e.g. customer-written software or integrations).
  • deprecation: Advertises the intention to remove service functionality without any change to the
    operation of the service itself.
  • manualTask: Requires the possibility of manual intervention (running a script, eyeballing configuration,
    performing database surgery, ...) at the time of upgrade for it to succeed.
  • migration: A fully automatic upgrade migration task with no engineer input required.

Note: only one type should be chosen.

How are new versions calculated?
  • ❗The break and manual task changelog types will result in a major release!
  • 🐛 The fix changelog type will result in a minor release in most cases, and a patch release version for patch branches. This behaviour is configurable in autorelease.
  • ✨ All others will result in a minor version release.

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Add leased timestamps to TimestampLeaseAwareTransaction and TimestampLeaseAwareTransactionManager APIs.

Check the box to generate changelog(s)

  • Generate changelog entry

* @throws RuntimeException If requesting more timestamps in {@code preCommitAction} than specified in
* timestampCount.
*/
void preCommit(String timestampLockDescriptor, int timestampCount, Consumer<TimestampSupplier> preCommitAction);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PreCommitCondition is already a thing in AtlasDB, does the naming not clash here?

* Clients can use {@link #getLockedTimestamp(String)} to fetch the earliest locked
* timestamp for a given {@param timestampLockDescriptor} of an open transaction.
* Note these semantics are the quite similar to {@link TransactionManager#getImmutableTimestamp()}, but instead
* of tracking open start transaction timestamps, we track open {@code preCommitAction} timestamps.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed: the consequence of this API is that you EXPECT the user to just call into their stores (and by extension the Transaction in AtlasDB) so this NEEDs to happen before the code in SnapshotTransaction#commitWrites or something.

Is there a usecase that you had for this style of work outside of this workstream? Because I struggle to understand why this is better than the more generic idea of "delayed-writes" (and not allowing people to interact with their Transaction object at the point where that function is ran).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional concern I'd have is: would people expect that, if they happen to use their Transaction object from multiple threads, that there's an ordering between all interactions with the transaction object happened AND THEN these runnables are ran?

So concretely, would we want to have another state in https://github.com/palantir/atlasdb/blob/develop/atlasdb-impl-shared/src/main/java/com/palantir/atlasdb/transaction/impl/SnapshotTransaction.java#L243 or something?

@fsamuel-bs fsamuel-bs force-pushed the ssouza/lock-aware-txn branch 2 times, most recently from 7d87249 to 87c0469 Compare October 22, 2024 14:20
Comment on lines 32 to 84
static class PreCommitAction {
final Consumer<LongSupplier> action;
final int timestampCount;

PreCommitAction(Consumer<LongSupplier> action, int timestampCount) {
this.action = action;
this.timestampCount = timestampCount;
}
}

static class PerLeaseActions {
final List<PreCommitAction> runnables = new ArrayList<>();
int timestampCount = 0;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These ideally should be records, but Atlas is not on java 17 yet.

Comment on lines 53 to 54
perLeaseActions.timestampCount += numLeasedTimestamps;
perLeaseActions.runnables.add(new PreCommitAction(action, numLeasedTimestamps));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a bit wrong to be accessing nested elements like this. :/

@fsamuel-bs fsamuel-bs requested a review from jkozlowski October 23, 2024 10:41
@fsamuel-bs fsamuel-bs marked this pull request as ready for review October 23, 2024 10:41
@fsamuel-bs fsamuel-bs changed the title LockAware Timestamps LeaseAware Timestamps Oct 23, 2024
@fsamuel-bs fsamuel-bs changed the title LeaseAware Timestamps Add Lease Timestamps Oct 23, 2024
* for open transactions.
*/
@Beta
long getLeasedTimestamp(TimestampLeaseName leaseName);
Copy link
Contributor

@jkozlowski jkozlowski Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this here will break upgrades for people, I suspect? or be a runtime failure?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider the same underhanded trick you're doing for TimestampLeaseAwareTransaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(. Ya, there are some internal dependencies. Let me fix this then.

* If no transactions with a {@code timestampLeaseName} lock are open, this method returns a new fresh timestamp
* (i.e. equivalent to {@link TimelockService#getFreshTimestamp()}).
* <p>
* Consumers should to fetch the leased timestamp outside of transactions that potentially use it - if fetching the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say this is maybe a useful warning, but it assumes a lot about how this is used, therefore possibly making it not worth putting in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be bias towards over-communicating - this feature is too complex already.

* (i.e. equivalent to {@link TimelockService#getFreshTimestamp()}).
* <p>
* Consumers should to fetch the leased timestamp outside of transactions that potentially use it - if fetching the
* leased timestamp inside a transaction, it's possible for the transaction's start timestamp < leased timestamp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a warning that this is a very advanced feature and nobody should use this without TS involvement. Also, a warning that this timestamp MAY sometimes go back (if observed globally)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just do the RestrictedApi thing then.

@@ -17,6 +17,7 @@ license {
}

dependencies {
api project(":timelock-api")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Understood where this comes from, and I think in general this is fine, but I also don't understand exactly how these subprojects are decomped. Specifically, at least in the past, internal products would run without timelock.

Should we duplicate TimestampLeaseName in atlasdb-api instead of doing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's too confusing internally. It's a pet peeve of mine to not have duplicate things flying around.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that, but libraries are hard

@fsamuel-bs fsamuel-bs force-pushed the ssouza/lock-aware-txn branch 3 times, most recently from 23543fa to 497c720 Compare October 25, 2024 12:42
@fsamuel-bs fsamuel-bs force-pushed the ssouza/lock-aware-txn branch from b29dd9d to 4099756 Compare October 25, 2024 15:59
@bulldozer-bot bulldozer-bot bot merged commit 9afedb1 into develop Oct 28, 2024
21 checks passed
@bulldozer-bot bulldozer-bot bot deleted the ssouza/lock-aware-txn branch October 28, 2024 13:53
@autorelease3
Copy link

autorelease3 bot commented Oct 28, 2024

Released 0.1180.0

Copy link
Contributor

@jeremyk-91 jeremyk-91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general design looks right, though I think some things were rushed in the name of speed, and we should probably come back to them.

Some of the implementation details of TPCA also seem a bit unexpected, so would be worth checking if they need to be exactly as written as well.

Also, please write a description! I know there is a lot of context on this feature currently, but given some of the other constraints we have, that might change.

@@ -1,6 +1,7 @@
apply from: "../gradle/shared.gradle"

dependencies {
api project(":atlasdb-commons-api")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's probably a reason we need this, but it's not immediately obvious to me why this is necessary over and above, e.g. timelock-api.

This really should be in the description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We moved TimestampLeaseName to :atlasdb-commons-api, which is used in lock-impl. Our gradle plugin orchestration requires all dependencies to be explicitly declared.

* meaning the transaction cannot read all data up to leased timestamp.
*
* @param leaseName the name of the lease the timestamps are bound to
* @return the timestamp that is before any timestamp returned by the consumer of {@link TimestampLeaseAwareTransaction#preCommit(TimestampLeaseName, int, Consumer)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

throw new SafeRuntimeException(
"Cannot fetch leased timestamps since pre-commit action requested 0 leased timestamps",
SafeArg.of("leaseName", timestampLeaseName));
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: we have the 0 case for some internal usage? It seems that to add an action via preCommit, you're not allowed for this to be zero

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have theoretical ones, but I'll expose it as a new public method in Transaction with simpler signature - only preCommit(Runnable).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants