Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support refreshing Iceberg tables #5707

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
470b09c
Initial commit of refreshing Iceberg.
lbooker42 Jul 2, 2024
a8d957a
Rebased to main.
lbooker42 Jul 2, 2024
264fdb1
Change IcebergInstructions refreshing indicator to enum instead of bo…
lbooker42 Jul 2, 2024
58d0a73
WIP, for review
lbooker42 Jul 3, 2024
e090474
Manual and auto-refreshing working, better documentation.
lbooker42 Jul 23, 2024
57021ad
Addressed more PR comments, some remaining.
lbooker42 Jul 23, 2024
fb882e8
WIP, some PR comments addressed.
lbooker42 Jul 26, 2024
5bbdeb2
WIP, even more PR comments addressed.
lbooker42 Jul 27, 2024
3da205c
Nearly all PR comments addressed.
lbooker42 Jul 27, 2024
91acf9b
merged with main
lbooker42 Jul 27, 2024
08dd329
Adjustment to IcebergInstructions update mode.
lbooker42 Jul 29, 2024
7af0d1d
Added python wrapper for Iceberg refreshing tables.
lbooker42 Jul 29, 2024
2d79c38
Changes to mocked tests for ColumnSourceManager and PartitionAwareSou…
lbooker42 Jul 29, 2024
3809f21
Added DHError handler and add'l documentation to python `snapshots()`…
lbooker42 Jul 30, 2024
5273a15
Fixed typo in JavaDoc
lbooker42 Jul 30, 2024
b9e2c6e
WIP
lbooker42 Jul 31, 2024
9937f79
Suggestion from review
lbooker42 Jul 31, 2024
cd08038
WIP, changes to revert some transaction token code.
lbooker42 Jul 31, 2024
f28325f
Correct logic across multiple transactions.
lbooker42 Aug 21, 2024
2d92b3f
Merged with main
lbooker42 Aug 21, 2024
cd31d82
Moved transaction accumulation to AbstractTableLocationProvider
lbooker42 Aug 23, 2024
d680c0c
Moved transaction accumulation to AbstractTableLocationProvider
lbooker42 Aug 26, 2024
6607fc3
PR comments addressed.
lbooker42 Aug 28, 2024
893336f
Updated to use IcebergTableAdapter and exposed in python. Addressed P…
lbooker42 Aug 30, 2024
68e4546
Incorporated external PR to update PartitioningColumnDataIndex for re…
lbooker42 Aug 30, 2024
273f5c1
Added additional snapshots with removes to IcebergToolsTest resources.
lbooker42 Sep 3, 2024
1e92a19
Merge branch 'main' into lab-iceberg-refreshing
lbooker42 Sep 3, 2024
f72c1b7
Manual and auto refreshing tests for Iceberg.
lbooker42 Sep 4, 2024
5c7ff12
Manual and auto refreshing tests for Iceberg, not passing.
lbooker42 Sep 4, 2024
92eec61
PR comments addressed.
lbooker42 Sep 4, 2024
95194b7
Implemented improved location reference counting in AbstractTableLoca…
lbooker42 Sep 5, 2024
09e2b6e
Fixing doc problem.
lbooker42 Sep 5, 2024
30910dd
For review only, does not compile :(
lbooker42 Sep 12, 2024
dd12240
Compiles now, still many problems
lbooker42 Sep 13, 2024
944dac6
Working through problems.
lbooker42 Sep 13, 2024
912b9f2
Cleanup and minor changes
lbooker42 Sep 13, 2024
e2b6fd0
Refactored ATLP
lbooker42 Sep 18, 2024
72b03be
Merged with main.
lbooker42 Sep 18, 2024
01e50fe
Updated but RCSM still not referenced properly.
lbooker42 Sep 18, 2024
81e88d8
Refreshing tests still need work.
lbooker42 Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,6 @@ interface Listener extends BasicTableDataListener {
*/
void endTransaction(@NotNull Object token);

/**
* <p>
* Notify the listener of a {@link TableLocationKey} encountered while initiating or maintaining the location
* subscription. This should occur at most once per location, but the order of delivery is <i>not</i>
* guaranteed.
* </p>
* <p>
* If transactionToken is {@code null}, the key will be added to the pending additions immediately.
* </p>
*
* @param tableLocationKey The new table location key.
* @param transactionToken The token identifying the transaction, or {@code null} if this addition is not part
* of a transaction.
*/
void handleTableLocationKeyAdded(
@NotNull ImmutableTableLocationKey tableLocationKey,
@Nullable Object transactionToken);

/**
* Notify the listener of a {@link TableLocationKey} encountered while initiating or maintaining the location
* subscription. This should occur at most once per location, but the order of delivery is <i>not</i>
Copy link
Member

@rcaudy rcaudy Jul 3, 2024

Choose a reason for hiding this comment

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

Consider whether we can have add + remove + add. What about remove + add in the same pull?
Should document that this may change the "at most once per location" guarantee, and define semantics.
I think it should be something like:
We allow re-add of a removed TLK. Downstream consumers should process these in an order that respects delivery and transactionality.

Within one transaction, expect at most one of "remove" or "add" for a given TLK.
Within one transaction, we can allow remove followed by add, but not add followed by remove. This dictates that we deliver pending removes before pending adds in processPending.
That is, one transaction allows:

  1. Replace a TLK (remove followed by add)
  2. Remove a TLK (remove)
  3. Add a TLK (add)
    Double add, double remove, or add followed by remove is right out.

Processing an addition to a transaction.

  1. Remove: If there's an existing accumulated remove, error. Else, if there's an existing accumulated add, error. Else, accumulate the remove.
  2. Add: If there's an existing accumulated add, error. Else, accumulate the add.

Across multiple transactions delivered as a batch, ensure that the right end-state is achieved.

  1. Add + remove collapses pairwise to no-op
  2. Remove + add (assuming prior add) should be processed in order. We might very well choose to not allow re-add at this time, I don't expect Iceberg to do this. If we do allow it, we need to be conscious that the removed location's region(s) need(s) to be used for previous data, while the added one needs to be used for current data.
  3. Multiple adds or removes within without their opposite intervening is an error.

null token should be handled exactly the same as a single-element transaction.

Processing a transaction:

  1. Process removes first. If there's an add pending, then delete, swallow the remove. Else, if there's a remove pending, error. Else, store the remove as pending.
  2. Process adds. If there's an add pending, error. Else, store the add as pending.

Note: removal support means that RegionedColumnSources may no longer be immutable! We need to be sure that we are aware of whether a particular TLP might remove data, and ensure that in those cases the RCS is not marked immutable. REVISED: ONLY REPLACE IS AN ISSUE FOR IMMUTABILITY, AS LONG AS WE DON'T RESUSE SLOTS.

We discussed that TLPs should probably specify whether they are guaranteeing that they will never remove TLKs, and whether their TLs will never remove or modify rows. I think if and when we encounter data sources that require modify support, we should probably just use SourcePartitionedTable instead of PartitionAwareSourceTable.

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'm not sure if I need to handle the RCS immutability question in this PR since Iceberg will not modify rows.

Copy link
Member

Choose a reason for hiding this comment

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

Removing a region makes the values in the corresponding row key range disappear. That's OK for immutability.
If you allow a new region to use the same slot, or allow the old region to reincarnate in the same slot potentially with different data, you are violating immutability.

Not reusing slots means that a long-lived iceberg table may eventually exhaust its row key space.

Copy link
Member

Choose a reason for hiding this comment

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

Replace (remove + add of a TLK) requires some kind of versioning of the TL, in a way that the TLK is aware of in order to ensure that we provide the table with the right TL for the version. AbstractTableLocationProvider's location caching layer is not currently sufficient for atomically replacing TLs.

Expand All @@ -66,50 +48,33 @@ void handleTableLocationKeyAdded(
*
* @param tableLocationKey The new table location key.
*/
default void handleTableLocationKeyAdded(@NotNull ImmutableTableLocationKey tableLocationKey) {
handleTableLocationKeyAdded(tableLocationKey, null);
}
void handleTableLocationKeyAdded(@NotNull ImmutableTableLocationKey tableLocationKey);
Copy link
Member

Choose a reason for hiding this comment

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

Good change, may be breaking for DHE, please consult Andy pre-merge.


/**
* <p>
* Notify the listener of a {@link TableLocationKey} that has been removed.
* </p>
* <p>
* If transactionToken is {@code null}, the key will be added to the pending removals immediately.
* </p>
*
* @param tableLocationKey The table location key that was removed.
* @param transactionToken The token identifying the transaction, or {@code null} if this addition is not part
* of a transaction.
*/
void handleTableLocationKeyRemoved(
@NotNull ImmutableTableLocationKey tableLocationKey,
@Nullable Object transactionToken);

/**
* Notify the listener of a {@link TableLocationKey} that has been removed. This addition is not part of any
* transaction, and is equivalent to {@code handleTableLocationKeyAdded(tableLocationKey, null);} by default.
* Notify the listener of a {@link TableLocationKey} that has been removed. This removal is not part of any
* transaction, and is equivalent to {@code handleTableLocationKeyRemoved(tableLocationKey, null);} by default.
*
* @param tableLocationKey The table location key that was removed.
*/
@SuppressWarnings("unused")
default void handleTableLocationKeyRemoved(@NotNull ImmutableTableLocationKey tableLocationKey) {
handleTableLocationKeyRemoved(tableLocationKey, null);
}
void handleTableLocationKeyRemoved(@NotNull ImmutableTableLocationKey tableLocationKey);

/**
* <p>
* Notify the listener of collections of {@link TableLocationKey TableLocationKeys} added or removed while
* initiating or maintaining the location subscription. This should occur at most once per location, but the
* order of delivery is <i>not</i> guaranteed.
* initiating or maintaining the location subscription. Addition or removal should occur at most once per
* location, but the order of delivery is <i>not</i> guaranteed.
* </p>
*
* @param addedKeys Collection of table location keys that were added.
* @param removedKeys Collection of table location keys that were removed.
*/
void handleTableLocationKeysUpdate(
@Nullable Collection<ImmutableTableLocationKey> addedKeys,
@Nullable Collection<ImmutableTableLocationKey> removedKeys);
default void handleTableLocationKeysUpdate(
@NotNull Collection<ImmutableTableLocationKey> addedKeys,
@NotNull Collection<ImmutableTableLocationKey> removedKeys) {
removedKeys.forEach(this::handleTableLocationKeyRemoved);
addedKeys.forEach(this::handleTableLocationKeyAdded);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public final void incrementReferenceCount() {
/**
* Decrement the reference count by one, when the reference count reaches zero this location will be cleared.
*
* @throws IllegalStateException If the reference count was not successfully incremented
* @throws IllegalStateException If the reference count was not successfully decremented
*/
public void decrementReferenceCount() {
referenceCounted.decrementReferenceCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,55 @@ public abstract class AbstractTableLocationProvider
extends SubscriptionAggregator<TableLocationProvider.Listener>
implements TableLocationProvider {

private static final Set<TableLocationKey> EMPTY_TABLE_LOCATION_KEYS = Collections.emptySet();
private static final Set<ImmutableTableLocationKey> EMPTY_TABLE_LOCATION_KEYS = Collections.emptySet();

/**
* Helper class to manage a transaction of added and removed location keys.
*/
private static class Transaction {
Set<TableLocationKey> locationsAdded = EMPTY_TABLE_LOCATION_KEYS;
Set<TableLocationKey> locationsRemoved = EMPTY_TABLE_LOCATION_KEYS;
Set<ImmutableTableLocationKey> locationsAdded = EMPTY_TABLE_LOCATION_KEYS;
Set<ImmutableTableLocationKey> locationsRemoved = EMPTY_TABLE_LOCATION_KEYS;

void addlocationKey(TableLocationKey locationKey) {
void addlocationKey(ImmutableTableLocationKey locationKey) {
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
if (locationsAdded == EMPTY_TABLE_LOCATION_KEYS) {
locationsAdded = new HashSet<>();
} else if (locationsAdded.contains(locationKey)) {
}
// When adding a location in a transaction, check for logical consistency.
// 1. If the location was already added in this transaction, we have a problem. A transaction should not
// add the same location twice.
// 2. If the location was already removed in this transaction, we have a `replace` operation which is not a
// logical error (although it may not be supported by all consumers).
if (!locationsAdded.add(locationKey)) {
throw new IllegalStateException("TableLocationKey " + locationKey
+ " was already added in this transaction.");
}
locationsAdded.add(locationKey);
}

void removeLocationKey(TableLocationKey locationKey) {
void removeLocationKey(ImmutableTableLocationKey locationKey) {
rcaudy marked this conversation as resolved.
Show resolved Hide resolved
if (locationsRemoved == EMPTY_TABLE_LOCATION_KEYS) {
locationsRemoved = new HashSet<>();
} else if (locationsRemoved.contains(locationKey)) {
}
// When removing a location in a transaction, check for logical consistency.
// 1. If the location was already removed in this transaction, we have a problem. A transaction should not
// remove the same location twice.
if (!locationsRemoved.add(locationKey)) {
throw new IllegalStateException("TableLocationKey " + locationKey
+ " was already removed and has not been replaced.");
} else if (locationsAdded.contains(locationKey)) {
}
// 2. If the location was already added in this transaction, we have a problem. A transaction should not
// add then remove the same location.
if (locationsAdded.contains(locationKey)) {
throw new IllegalStateException("TableLocationKey " + locationKey
+ " was removed after being added in the same transaction.");
}
locationsRemoved.add(locationKey);
}
}

private final ImmutableTableKey tableKey;

// These sets represent open transactions that are being accumulated.
// Open transactions that are being accumulated
private final Map<Object, Transaction> transactions = new HashMap<>();

private final Object transactionLock = new Object();

/**
* Map from {@link TableLocationKey} to itself, or to a {@link TableLocation}. The values are {@link TableLocation}s
* if:
Expand Down Expand Up @@ -122,7 +131,7 @@ public final ImmutableTableKey getKey() {

@Override
protected final void deliverInitialSnapshot(@NotNull final TableLocationProvider.Listener listener) {
listener.handleTableLocationKeysUpdate(unmodifiableTableLocationKeys, null);
listener.handleTableLocationKeysUpdate(unmodifiableTableLocationKeys, List.of());
}

/**
Expand All @@ -131,12 +140,14 @@ protected final void deliverInitialSnapshot(@NotNull final TableLocationProvider
* @param token A token to identify the transaction
*/
protected void beginTransaction(@NotNull final Object token) {
synchronized (transactionLock) {
synchronized (transactions) {
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
// Verify that we can start a new transaction with this token.
if (transactions.containsKey(token)) {
throw new IllegalStateException("A transaction with token " + token + " is currently open.");
}
transactions.put(token, new Transaction());
transactions.compute(token, (key, val) -> {
if (val != null) {
throw new IllegalStateException("A transaction with token " + token + " is already open.");
}
return new Transaction();
});
}
}

Expand All @@ -147,52 +158,50 @@ protected void beginTransaction(@NotNull final Object token) {
*/
protected void endTransaction(@NotNull final Object token) {
final Transaction transaction;
synchronized (transactionLock) {
synchronized (transactions) {
// Verify that this transaction is open.
transaction = transactions.remove(token);
if (transaction == null) {
throw new IllegalStateException("No transaction with token " + token + " is currently open.");
}
}

final Collection<ImmutableTableLocationKey> addedImmutableKeys =
final Collection<ImmutableTableLocationKey> addedKeys =
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
new ArrayList<>(transaction.locationsAdded.size());
final Collection<ImmutableTableLocationKey> removedImmutableKeys =
final Collection<ImmutableTableLocationKey> removedKeys =
new ArrayList<>(transaction.locationsRemoved.size());

// Process the accumulated adds and removes under a lock on `tableLocations` to keep modifications atomic to
// other holders of this lock.
synchronized (tableLocations) {
if (transaction.locationsAdded != EMPTY_TABLE_LOCATION_KEYS
|| transaction.locationsRemoved != EMPTY_TABLE_LOCATION_KEYS) {
for (TableLocationKey locationKey : transaction.locationsAdded) {
locationCreatedRecorder = false;
final Object result = tableLocations.putIfAbsent(locationKey, this::observeInsert);
visitLocationKey(locationKey);
if (locationCreatedRecorder) {
verifyPartitionKeys(locationKey);
addedImmutableKeys.add(toKeyImmutable(result));
}
}
// Return early if there are no changes to process.
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
if (transaction.locationsAdded == EMPTY_TABLE_LOCATION_KEYS
&& transaction.locationsRemoved == EMPTY_TABLE_LOCATION_KEYS) {
return;
}

for (TableLocationKey locationKey : transaction.locationsRemoved) {
final Object removedLocation = tableLocations.remove(locationKey);
if (removedLocation != null) {
removedImmutableKeys.add(toKeyImmutable(locationKey));
}
}
for (ImmutableTableLocationKey locationKey : transaction.locationsRemoved) {
final Object removedLocation = tableLocations.remove(locationKey);
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
if (removedLocation != null) {
removedKeys.add(locationKey);
}
}

if (subscriptions != null) {
for (ImmutableTableLocationKey locationKey : transaction.locationsAdded) {
locationCreatedRecorder = false;
tableLocations.putIfAbsent(locationKey, this::observeInsert);
visitLocationKey(locationKey);
if (locationCreatedRecorder) {
verifyPartitionKeys(locationKey);
addedKeys.add(locationKey);
}
}
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved

if (subscriptions != null && (!addedKeys.isEmpty() || !removedKeys.isEmpty())) {
synchronized (subscriptions) {
// Push the notifications to the subscribers.
if ((!addedImmutableKeys.isEmpty() || !removedImmutableKeys.isEmpty())
&& subscriptions.deliverNotification(
Listener::handleTableLocationKeysUpdate,
addedImmutableKeys,
removedImmutableKeys,
true)) {
if (subscriptions.deliverNotification(
Listener::handleTableLocationKeysUpdate,
addedKeys,
removedKeys,
true)) {
onEmpty();
}
}
Expand Down Expand Up @@ -220,29 +229,27 @@ protected final void handleTableLocationKeyAdded(
@NotNull final TableLocationKey locationKey,
@Nullable final Object transactionToken) {

if (!supportsSubscriptions()) {
tableLocations.putIfAbsent(locationKey, TableLocationKey::makeImmutable);
visitLocationKey(locationKey);
return;
}

if (transactionToken != null) {
// When adding a location in a transaction, check for logical consistency.
// 1. If the location was already added in this transaction, we have a problem. A transaction should not
// add the same location twice.
// 2. If the location was already removed in this transaction, we have a `replace` operation which is not a
// logical error (although it may not be supported by all consumers).
synchronized (transactionLock) {
final Transaction transaction = transactions.get(transactionToken);
final Transaction transaction;
synchronized (transactions) {
transaction = transactions.get(transactionToken);
if (transaction == null) {
throw new IllegalStateException(
"No transaction with token " + transactionToken + " is currently open.");
}
transaction.addlocationKey(locationKey);
}
// Store an immutable key
transaction.addlocationKey(toKeyImmutable(locationKey));
return;
}

if (!supportsSubscriptions()) {
tableLocations.putIfAbsent(locationKey, TableLocationKey::makeImmutable);
visitLocationKey(locationKey);
return;
}


// If we're not in a transaction, we should push this key immediately.
synchronized (subscriptions) {
// Since we're holding the lock on subscriptions, the following code is overly complicated - we could
Expand Down Expand Up @@ -328,12 +335,9 @@ protected void doInitialization() {
@Override
@NotNull
public final Collection<ImmutableTableLocationKey> getTableLocationKeys() {
// This lock is held while `endTransaction()` updates `tableLocations` with the accumulated adds/removes.
// Locking here ensures that this call won't return while `tableLocations` (and `unmodifiableTableLocationKeys`)
// contain a partial transaction.
synchronized (tableLocations) {
return unmodifiableTableLocationKeys;
}
final List<ImmutableTableLocationKey> result = new ArrayList<>(tableLocations.size());
tableLocations.keySet().forEach(key -> result.add((ImmutableTableLocationKey) key));
return result;
lbooker42 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down Expand Up @@ -413,25 +417,22 @@ protected final void handleTableLocationKeyRemoved(@NotNull final TableLocationK
protected void handleTableLocationKeyRemoved(
@NotNull final TableLocationKey locationKey,
@Nullable final Object transactionToken) {
if (!supportsSubscriptions()) {
return;
}

// When removing a location in a transaction, check for logical consistency.
// 1. If the location was already removed in this transaction, we have a problem. A transaction should not
// remove the same location twice.
// 2. If the location was already added in this transaction, we have a problem. A transaction should not
// add then remove the same location.
if (transactionToken != null) {
synchronized (transactionLock) {
final Transaction transaction = transactions.get(transactionToken);
final Transaction transaction;
synchronized (transactions) {
transaction = transactions.get(transactionToken);
if (transaction == null) {
throw new IllegalStateException(
"No transaction with token " + transactionToken + " is currently open.");
}
transaction.removeLocationKey(locationKey);
return;
}
transaction.removeLocationKey(toKeyImmutable(locationKey));
return;
}

if (!supportsSubscriptions()) {
tableLocations.remove(locationKey);
return;
}

// If we're not in a transaction, we should push this key immediately.
Expand Down
Loading
Loading