All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.trino.operator.join.PartitionedConsumption Maven / Gradle / Ivy
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.operator.join;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import com.google.errorprone.annotations.ThreadSafe;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import jakarta.annotation.Nullable;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.util.concurrent.Futures.allAsList;
import static com.google.common.util.concurrent.Futures.immediateVoidFuture;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static java.util.Objects.requireNonNull;
@ThreadSafe
public final class PartitionedConsumption
{
private final int consumersCount;
private final AtomicInteger consumed = new AtomicInteger();
@Nullable
private List> partitions;
PartitionedConsumption(
int consumersCount,
Iterable partitionNumbers,
IntFunction> loader,
IntConsumer disposer,
IntFunction> disposed)
{
this(consumersCount, immediateVoidFuture(), partitionNumbers, loader, disposer, disposed);
}
private PartitionedConsumption(
int consumersCount,
ListenableFuture activator,
Iterable partitionNumbers,
IntFunction> loader,
IntConsumer disposer,
IntFunction> disposed)
{
checkArgument(consumersCount > 0, "consumersCount must be positive");
this.consumersCount = consumersCount;
this.partitions = createPartitions(activator, partitionNumbers, loader, disposer, disposed);
}
private List> createPartitions(
ListenableFuture activator,
Iterable partitionNumbers,
IntFunction> loader,
IntConsumer disposer,
IntFunction> disposed)
{
requireNonNull(partitionNumbers, "partitionNumbers is null");
requireNonNull(loader, "loader is null");
requireNonNull(disposer, "disposer is null");
ImmutableList.Builder> partitions = ImmutableList.builder();
ListenableFuture partitionActivator = activator;
for (Integer partitionNumber : partitionNumbers) {
Partition partition = new Partition<>(consumersCount, partitionNumber, loader, partitionActivator, disposer);
partitions.add(partition);
partitionActivator = disposed.apply(partitionNumber);
}
return partitions.build();
}
Iterator> beginConsumption()
{
Queue> partitions = new ArrayDeque<>(requireNonNull(this.partitions, "partitions is already null"));
if (consumed.incrementAndGet() >= consumersCount) {
// Unreference futures to allow GC
this.partitions = null;
}
return new AbstractIterator<>()
{
@Override
protected Partition computeNext()
{
Partition next = partitions.poll();
if (next != null) {
return next;
}
return endOfData();
}
};
}
public static class Partition
{
private final int partitionNumber;
private final SettableFuture requested;
private final ListenableFuture loaded;
private final IntConsumer disposer;
@GuardedBy("this")
private int pendingReleases;
public Partition(
int consumersCount,
int partitionNumber,
IntFunction> loader,
ListenableFuture previousReleased,
IntConsumer disposer)
{
this.partitionNumber = partitionNumber;
this.requested = SettableFuture.create();
this.loaded = Futures.transformAsync(
allAsList(requested, previousReleased),
ignored -> loader.apply(partitionNumber),
directExecutor());
this.disposer = disposer;
this.pendingReleases = consumersCount;
}
public int number()
{
return partitionNumber;
}
public ListenableFuture load()
{
requested.set(null);
return loaded;
}
public synchronized void release()
{
checkState(loaded.isDone());
pendingReleases--;
checkState(pendingReleases >= 0);
if (pendingReleases == 0) {
disposer.accept(partitionNumber);
}
}
}
}