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

Dynamically configure spooling properties #24757

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -219,7 +219,7 @@ public final class SystemSessionProperties
public static final String IDLE_WRITER_MIN_DATA_SIZE_THRESHOLD = "idle_writer_min_data_size_threshold";
public static final String CLOSE_IDLE_WRITERS_TRIGGER_DURATION = "close_idle_writers_trigger_duration";
public static final String COLUMNAR_FILTER_EVALUATION_ENABLED = "columnar_filter_evaluation_enabled";
public static final String SPOOLING_PROTOCOL_ENABLED = "spooling_protocol_enabled";
public static final String SPOOLING_ENABLED = "spooling_enabled";

private final List<PropertyMetadata<?>> sessionProperties;

Expand Down Expand Up @@ -1131,7 +1131,7 @@ public SystemSessionProperties(
optimizerConfig.isUnsafePushdownAllowed(),
true),
booleanProperty(
SPOOLING_PROTOCOL_ENABLED,
SPOOLING_ENABLED,
"Enable client spooling protocol",
true,
true));
Expand Down Expand Up @@ -2024,9 +2024,9 @@ public static boolean isColumnarFilterEvaluationEnabled(Session session)
return session.getSystemProperty(COLUMNAR_FILTER_EVALUATION_ENABLED, Boolean.class);
}

public static boolean isSpoolingProtocolEnabled(Session session)
public static boolean isSpoolingEnabled(Session session)
{
return session.getSystemProperty(SPOOLING_PROTOCOL_ENABLED, Boolean.class);
return session.getSystemProperty(SPOOLING_ENABLED, Boolean.class);
}

public static boolean isUnsafePushdownAllowed(Session session)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static io.airlift.units.DataSize.succinctBytes;
import static io.trino.SystemSessionProperties.getRetryPolicy;
import static io.trino.SystemSessionProperties.isSpoolingProtocolEnabled;
import static io.trino.SystemSessionProperties.isSpoolingEnabled;
import static io.trino.execution.BasicStageStats.EMPTY_STAGE_STATS;
import static io.trino.execution.QueryState.DISPATCHING;
import static io.trino.execution.QueryState.FAILED;
Expand Down Expand Up @@ -310,7 +310,7 @@ static QueryStateMachine beginWithTicker(
session = session.withExchangeEncryption(serializeAesEncryptionKey(createRandomAesEncryptionKey()));
}

if (!queryType.map(SELECT::equals).orElse(false) || !isSpoolingProtocolEnabled(session)) {
if (!queryType.map(SELECT::equals).orElse(false) || !isSpoolingEnabled(session)) {
session = session.withoutSpooling();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
import static io.trino.server.protocol.spooling.SpooledBlock.SPOOLING_METADATA_SYMBOL;
import static io.trino.server.protocol.spooling.SpooledBlock.SPOOLING_METADATA_TYPE;
import static io.trino.server.protocol.spooling.SpooledBlock.createNonSpooledPage;
import static io.trino.server.protocol.spooling.SpoolingSessionProperties.getInitialSegmentSize;
import static io.trino.server.protocol.spooling.SpoolingSessionProperties.getMaxInlinedRows;
import static io.trino.server.protocol.spooling.SpoolingSessionProperties.getMaxInlinedSize;
import static io.trino.server.protocol.spooling.SpoolingSessionProperties.getMaxSegmentSize;
import static io.trino.server.protocol.spooling.SpoolingSessionProperties.isAllowInlining;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
Expand Down Expand Up @@ -160,11 +165,11 @@ public OutputSpoolingOperator(OperatorContext operatorContext, QueryDataEncoder
{
this.operatorContext = requireNonNull(operatorContext, "operatorContext is null");
this.controller = new OutputSpoolingController(
spoolingConfig.isAllowInlining(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't spoolingConfig now an unused parameter? If so, let's drop it.

spoolingConfig.getMaximumInlinedRows(),
spoolingConfig.getMaximumSegmentSize().toBytes(),
spoolingConfig.getInitialSegmentSize().toBytes(),
spoolingConfig.getMaximumSegmentSize().toBytes());
isAllowInlining(operatorContext.getSession()),
getMaxInlinedRows(operatorContext.getSession()),
getMaxInlinedSize(operatorContext.getSession()).toBytes(),
getInitialSegmentSize(operatorContext.getSession()).toBytes(),
getMaxSegmentSize(operatorContext.getSession()).toBytes());
this.userMemoryContext = operatorContext.newLocalUserMemoryContext(OutputSpoolingOperator.class.getSimpleName());
this.queryDataEncoder = requireNonNull(queryDataEncoder, "queryDataEncoder is null");
this.spoolingManager = requireNonNull(spoolingManager, "spoolingManager is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
import io.airlift.configuration.ConfigDescription;
import io.airlift.configuration.ConfigSecuritySensitive;
import io.airlift.units.DataSize;
import io.airlift.units.MaxDataSize;
import io.airlift.units.MinDataSize;
import io.trino.util.Ciphers;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
Expand Down Expand Up @@ -68,6 +72,8 @@ public SpoolingConfig setRetrievalMode(SegmentRetrievalMode retrievalMode)
return this;
}

@MinDataSize("1kB")
@MaxDataSize("128MB")
public DataSize getInitialSegmentSize()
{
return initialSegmentSize;
Expand All @@ -81,6 +87,8 @@ public SpoolingConfig setInitialSegmentSize(DataSize initialSegmentSize)
return this;
}

@MinDataSize("1kB")
@MaxDataSize("128MB")
public DataSize getMaximumSegmentSize()
{
return maximumSegmentSize;
Expand All @@ -107,6 +115,8 @@ public SpoolingConfig setAllowInlining(boolean allowInlining)
return this;
}

@Min(1)
@Max(1_000_000)
public long getMaximumInlinedRows()
{
return maximumInlinedRows;
Expand All @@ -120,6 +130,8 @@ public SpoolingConfig setMaximumInlinedRows(long maximumInlinedRows)
return this;
}

@MinDataSize("1kB")
@MaxDataSize("1MB")
public DataSize getMaximumInlinedSize()
{
return maximumInlinedSize;
Expand All @@ -141,6 +153,12 @@ public boolean isSharedEncryptionKeyAes256()
.orElse(true);
}

@AssertTrue(message = "protocol.spooling.initial-segment-size must be smaller than protocol.spooling.maximum-segment-size")
public boolean areSegmentSizesCorrect()
{
return getInitialSegmentSize().compareTo(getMaximumSegmentSize()) < 0;
}

@AssertTrue(message = "protocol.spooling.shared-secret-key must be set")
public boolean isSharedEncryptionKeySet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import com.google.inject.multibindings.OptionalBinder;
import com.google.inject.multibindings.ProvidesIntoSet;
import io.airlift.configuration.AbstractConfigurationAwareModule;
import io.trino.SystemSessionPropertiesProvider;
import io.trino.server.ServerConfig;
import io.trino.server.protocol.spooling.SpoolingConfig.SegmentRetrievalMode;
import io.trino.spi.spool.SpoolingManager;

import static com.google.inject.multibindings.Multibinder.newSetBinder;
import static com.google.inject.multibindings.OptionalBinder.newOptionalBinder;
import static io.airlift.jaxrs.JaxrsBinder.jaxrsBinder;
import static io.trino.server.protocol.spooling.QueryDataEncoder.EncoderSelector.noEncoder;
Expand All @@ -49,6 +51,8 @@ protected void setup(Binder binder)
return;
}

newSetBinder(binder, SystemSessionPropertiesProvider.class).addBinding().to(SpoolingSessionProperties.class).in(Scopes.SINGLETON);

boolean isCoordinator = buildConfigObject(ServerConfig.class).isCoordinator();
SpoolingConfig spoolingConfig = buildConfigObject(SpoolingConfig.class);
binder.bind(QueryDataEncoder.EncoderSelector.class).to(PreferredQueryDataEncoderSelector.class).in(Scopes.SINGLETON);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.server.protocol.spooling;

import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import io.airlift.units.DataSize;
import io.trino.Session;
import io.trino.SystemSessionPropertiesProvider;
import io.trino.spi.TrinoException;
import io.trino.spi.session.PropertyMetadata;

import java.util.List;
import java.util.function.Consumer;

import static io.airlift.units.DataSize.Unit.KILOBYTE;
import static io.airlift.units.DataSize.Unit.MEGABYTE;
import static io.trino.plugin.base.session.PropertyMetadataUtil.dataSizeProperty;
import static io.trino.spi.StandardErrorCode.INVALID_SESSION_PROPERTY;
import static io.trino.spi.session.PropertyMetadata.booleanProperty;
import static io.trino.spi.session.PropertyMetadata.longProperty;

public class SpoolingSessionProperties
implements SystemSessionPropertiesProvider
{
// Spooled segments
public static final String INITIAL_SEGMENT_SIZE = "spooling_initial_segment_size";
public static final String MAX_SEGMENT_SIZE = "spooling_max_segment_size";

// Inlined segments
public static final String ALLOW_INLINING = "spooling_inlining_enabled";
public static final String MAX_INLINED_SIZE = "spooling_max_inlined_size";
public static final String MAX_INLINED_ROWS = "spooling_max_inlined_rows";

private final List<PropertyMetadata<?>> sessionProperties;

@Inject
public SpoolingSessionProperties(SpoolingConfig spoolingConfig)
{
sessionProperties = ImmutableList.<PropertyMetadata<?>>builder()
.add(dataSizeProperty(
INITIAL_SEGMENT_SIZE,
"Initial size of a spooled segment",
spoolingConfig.getInitialSegmentSize(),
isDataSizeBetween(INITIAL_SEGMENT_SIZE, DataSize.of(1, KILOBYTE), DataSize.of(128, MEGABYTE)),
false))
.add(dataSizeProperty(
MAX_SEGMENT_SIZE,
"Maximum size of a spooled segment",
spoolingConfig.getMaximumSegmentSize(),
isDataSizeBetween(MAX_SEGMENT_SIZE, DataSize.of(1, KILOBYTE), DataSize.of(128, MEGABYTE)),
false))
.add(booleanProperty(
ALLOW_INLINING,
"Allow inlining initial rows",
spoolingConfig.isAllowInlining(),
false))
.add(dataSizeProperty(
MAX_INLINED_SIZE,
"Maximum size of inlined data",
spoolingConfig.getMaximumInlinedSize(),
isDataSizeBetween(MAX_INLINED_SIZE, DataSize.of(1, KILOBYTE), DataSize.of(1, MEGABYTE)),
false))
.add(longProperty(
MAX_INLINED_ROWS,
"Maximum number of rows that are allowed to be inlined per worker",
spoolingConfig.getMaximumInlinedRows(),
false))
.build();
}

private Consumer<DataSize> isDataSizeBetween(String property, DataSize min, DataSize max)
{
return value -> {
if (min.compareTo(value) > 0) {
throw new TrinoException(INVALID_SESSION_PROPERTY, "Session property '" + property + "' must be greater than " + min);
}

if (max.compareTo(value) < 0) {
throw new TrinoException(INVALID_SESSION_PROPERTY, "Session property '" + property + "' must be smaller than " + max);
}
};
}

public static DataSize getInitialSegmentSize(Session session)
{
return session.getSystemProperty(INITIAL_SEGMENT_SIZE, DataSize.class);
}

public static DataSize getMaxSegmentSize(Session session)
{
return session.getSystemProperty(MAX_SEGMENT_SIZE, DataSize.class);
}

public static boolean isAllowInlining(Session session)
{
return session.getSystemProperty(ALLOW_INLINING, Boolean.class);
}

public static DataSize getMaxInlinedSize(Session session)
{
return session.getSystemProperty(MAX_INLINED_SIZE, DataSize.class);
}

public static long getMaxInlinedRows(Session session)
{
return session.getSystemProperty(MAX_INLINED_ROWS, Long.class);
}

@Override
public List<PropertyMetadata<?>> getSessionProperties()
{
return sessionProperties;
}
}
Loading