mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
Stage partitions loaded by a load that may roll back per client connection and publish them when the transaction commits, guarded by a per-table generation, instead of loading every partition. ensurePartitions waited on the per-table Java lock without a timeout even when the session held PostgreSQL locks. A transaction holding a table lock while another connection held the Java lock to load that table formed a cycle PostgreSQL cannot detect. It now uses the same transaction-aware acquisition as the full-load path. Pruning also applies to the Data API outside a transaction block, a predicate on a bind parameter is left out while literal predicates in the same statement still prune, and a session in a transaction block now waits at most 30 seconds for another load of the same table.
This commit is contained in:
1 parent
a5b3fab110
commit
d1205c34a2
7 files changed
+388
-18
No files matched your search
@@ -7,6 +7,14 @@ public interface BackendSql {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies the client connection whose transaction this backend runs in, so state staged for that
|
||||
* transaction can be published or dropped when it ends. Null when the end of the transaction is not reported.
|
||||
*/
|
||||
default Object connectionKey() {
|
||||
return null;
|
||||
}
|
||||
|
||||
void execute(String sql);
|
||||
long copyIn(String copySql, InputStream data);
|
||||
}
|
||||
+104
-14
@@ -95,16 +95,76 @@ public class ExternalTableMaterializer {
|
||||
/** The partitions each partitioned table holds in PostgreSQL, so a query loads only the ones it reads. */
|
||||
private final ConcurrentHashMap<String, LoadedPartitions> loadedPartitions = new ConcurrentHashMap<>();
|
||||
|
||||
/** Bumped whenever what a table holds changes, so state staged against an older content is recognized as stale. */
|
||||
private final ConcurrentHashMap<String, Long> generations = new ConcurrentHashMap<>();
|
||||
/** Partitions loaded inside a transaction, per client connection, until that transaction commits or rolls back. */
|
||||
private final Map<Object, Map<String, PendingPartitions>> pendingPartitions = new ConcurrentHashMap<>();
|
||||
|
||||
private record PendingLoad(String fingerprint, String definitions) { }
|
||||
|
||||
/** What a partitioned table holds: the table it was loaded for, and a fingerprint per loaded partition. */
|
||||
private record LoadedPartitions(String identity, Map<List<String>, String> partitions) { }
|
||||
|
||||
/** Partitions an uncommitted transaction loaded, valid only while the table's generation is unchanged. */
|
||||
private record PendingPartitions(LoadedPartitions state, String definitions, long generation) { }
|
||||
|
||||
public void finishCycle(BackendSql backend) {
|
||||
pendingLoads.remove(backend);
|
||||
discardPendingPartitions(backend);
|
||||
}
|
||||
|
||||
private void discardPendingPartitions(BackendSql backend) {
|
||||
Object connection = backend.connectionKey();
|
||||
if (connection != null) {
|
||||
pendingPartitions.remove(connection);
|
||||
}
|
||||
}
|
||||
|
||||
/** Forgets what the table holds: its content is about to change or can no longer be vouched for. */
|
||||
private void dropHeld(String cacheKey) {
|
||||
loadedPartitions.remove(cacheKey);
|
||||
generations.merge(cacheKey, 1L, Long::sum);
|
||||
}
|
||||
|
||||
private void dropStaged(BackendSql backend, String cacheKey) {
|
||||
Object connection = backend.connectionKey();
|
||||
Map<String, PendingPartitions> staged = connection == null ? null : pendingPartitions.get(connection);
|
||||
if (staged != null) {
|
||||
staged.remove(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
private long generation(String cacheKey) {
|
||||
return generations.getOrDefault(cacheKey, 0L);
|
||||
}
|
||||
|
||||
private void publishPendingPartitions(BackendSql backend) {
|
||||
Object connection = backend.connectionKey();
|
||||
Map<String, PendingPartitions> completed = connection == null ? null : pendingPartitions.remove(connection);
|
||||
if (completed == null) {
|
||||
return;
|
||||
}
|
||||
completed.forEach((key, pending) -> {
|
||||
ReentrantLock lock = locks.computeIfAbsent(key, ignored -> new ReentrantLock());
|
||||
lock.lock();
|
||||
try {
|
||||
// another load changed the table since this transaction did, so what it staged no longer describes it
|
||||
if (generation(key) == pending.generation()) {
|
||||
schemaSignatures.put(key, pending.definitions());
|
||||
loadedPartitions.put(key, pending.state());
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void finishCycle(BackendSql backend, boolean committed) {
|
||||
if (committed) {
|
||||
publishPendingPartitions(backend);
|
||||
} else {
|
||||
discardPendingPartitions(backend);
|
||||
}
|
||||
Map<String, PendingLoad> completed = pendingLoads.remove(backend);
|
||||
if (committed && completed != null) {
|
||||
completed.forEach((key, loaded) -> {
|
||||
@@ -209,7 +269,8 @@ public class ExternalTableMaterializer {
|
||||
String previousSignature = local == null ? schemaSignatures.get(cacheKey) : local.definitions();
|
||||
latestLoads.remove(cacheKey);
|
||||
// a load of every partition replaces what the table held, so the partitions it holds are no longer known
|
||||
loadedPartitions.remove(cacheKey);
|
||||
dropHeld(cacheKey);
|
||||
dropStaged(backend, cacheKey);
|
||||
String definitions = load(backend, session, binding, roleSession, table, sources, previousSignature, null);
|
||||
if (!backend.permitsCachePublication()) {
|
||||
fingerprints.remove(cacheKey);
|
||||
@@ -254,15 +315,20 @@ public class ExternalTableMaterializer {
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether what a load puts in PostgreSQL may be undone, so it is only known to be there once the transaction commits. */
|
||||
private static boolean loadsMayRollBack(SpectrumSession session, BackendSql backend) {
|
||||
return session.inTransaction() || !backend.permitsCachePublication();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the table is loaded partition by partition: a partitioned table that has partitions, for a session
|
||||
* whose loads are published at once. A transaction may roll its load back and an Extended Query load is
|
||||
* published only when its cycle commits, so those keep loading every partition.
|
||||
* Whether the table is loaded partition by partition: a partitioned table that has partitions. A load that may
|
||||
* roll back is staged for its connection and published when the transaction commits, which needs a backend that
|
||||
* reports where the transaction ends; without one the table is loaded whole.
|
||||
*/
|
||||
private static boolean holdsPartitions(Table table, SpectrumSession session, BackendSql backend,
|
||||
List<Partition> partitions) {
|
||||
return hasPartitionKeys(table) && !partitions.isEmpty() && !session.inTransaction()
|
||||
&& backend.permitsCachePublication();
|
||||
return hasPartitionKeys(table) && !partitions.isEmpty()
|
||||
&& (!loadsMayRollBack(session, backend) || backend.connectionKey() != null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -288,10 +354,23 @@ public class ExternalTableMaterializer {
|
||||
String cacheKey = cacheKey(session, binding, table.getName());
|
||||
String label = binding.schemaName() + "." + table.getName();
|
||||
ReentrantLock lock = locks.computeIfAbsent(cacheKey, ignored -> new ReentrantLock());
|
||||
acquire(lock, label, false);
|
||||
acquire(lock, label, session.inTransaction());
|
||||
try {
|
||||
LoadedPartitions state = loadedPartitions.get(cacheKey);
|
||||
String previousSignature = schemaSignatures.get(cacheKey);
|
||||
boolean mayRollBack = loadsMayRollBack(session, backend);
|
||||
Object connection = backend.connectionKey();
|
||||
PendingPartitions staged = !mayRollBack || connection == null ? null
|
||||
: pendingPartitions.getOrDefault(connection, Map.of()).get(cacheKey);
|
||||
LoadedPartitions state;
|
||||
String previousSignature;
|
||||
if (staged != null) {
|
||||
// this transaction's own loads are in the table it reads; they hold only while nothing else changed it
|
||||
boolean current = staged.generation() == generation(cacheKey);
|
||||
state = current ? staged.state() : null;
|
||||
previousSignature = current ? staged.definitions() : null;
|
||||
} else {
|
||||
state = loadedPartitions.get(cacheKey);
|
||||
previousSignature = schemaSignatures.get(cacheKey);
|
||||
}
|
||||
boolean reset = state == null || !state.identity().equals(identity) || !definitions.equals(previousSignature);
|
||||
Map<List<String>, String> held = reset ? Map.of() : state.partitions();
|
||||
Set<List<String>> existing = partitions.stream().map(Partition::getValues).collect(Collectors.toSet());
|
||||
@@ -328,10 +407,21 @@ public class ExternalTableMaterializer {
|
||||
fingerprints.remove(cacheKey);
|
||||
latestLoads.remove(cacheKey);
|
||||
pendingLoads.values().forEach(loads -> loads.remove(cacheKey));
|
||||
loadedPartitions.remove(cacheKey);
|
||||
dropHeld(cacheKey);
|
||||
dropStaged(backend, cacheKey);
|
||||
load(backend, session, binding, roleSession, table, toLoad, previousSignature, reset ? null : deletions);
|
||||
schemaSignatures.put(cacheKey, definitions);
|
||||
loadedPartitions.put(cacheKey, new LoadedPartitions(identity, Map.copyOf(loaded)));
|
||||
LoadedPartitions loadedState = new LoadedPartitions(identity, Map.copyOf(loaded));
|
||||
if (mayRollBack) {
|
||||
// a rollback restores the committed columns, so a changed signature cannot be trusted afterwards
|
||||
if (!definitions.equals(schemaSignatures.get(cacheKey))) {
|
||||
schemaSignatures.remove(cacheKey);
|
||||
}
|
||||
pendingPartitions.computeIfAbsent(connection, ignored -> new ConcurrentHashMap<>())
|
||||
.put(cacheKey, new PendingPartitions(loadedState, definitions, generation(cacheKey)));
|
||||
} else {
|
||||
schemaSignatures.put(cacheKey, definitions);
|
||||
loadedPartitions.put(cacheKey, loadedState);
|
||||
}
|
||||
return Outcome.LOADED;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
@@ -378,7 +468,7 @@ public class ExternalTableMaterializer {
|
||||
lock.lock();
|
||||
try {
|
||||
fingerprints.remove(key);
|
||||
loadedPartitions.remove(key);
|
||||
dropHeld(key);
|
||||
latestLoads.remove(key);
|
||||
pendingLoads.values().forEach(loads -> loads.remove(key));
|
||||
} finally {
|
||||
@@ -398,7 +488,7 @@ public class ExternalTableMaterializer {
|
||||
lock.lock();
|
||||
try {
|
||||
fingerprints.remove(key);
|
||||
loadedPartitions.remove(key);
|
||||
dropHeld(key);
|
||||
schemaSignatures.remove(key);
|
||||
latestLoads.remove(key);
|
||||
pendingLoads.values().forEach(loads -> loads.remove(key));
|
||||
|
||||
@@ -16,6 +16,11 @@ public final class PostgresBackendSession implements BackendSql {
|
||||
this.backend = backend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object connectionKey() {
|
||||
return backend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String sql) {
|
||||
try {
|
||||
|
||||
+5
@@ -24,6 +24,11 @@ public final class PostgresExtendedBackendSession implements BackendSql {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object connectionKey() {
|
||||
return backend;
|
||||
}
|
||||
|
||||
/** The client's own pipelined statement failed; the backend discards everything until Sync. */
|
||||
public void onBackendError() {
|
||||
failedUntilSync = true;
|
||||
|
||||
Reference in new issue
Block a user