Skip to content
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 @@ -61,6 +61,7 @@
import java.util.Map;
Comment thread
wendigo marked this conversation as resolved.
Outdated
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.function.BiFunction;
Expand All @@ -81,6 +82,7 @@
import static io.trino.plugin.jdbc.CaseSensitivity.CASE_SENSITIVE;
import static io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR;
import static io.trino.plugin.jdbc.JdbcWriteSessionProperties.getWriteBatchSize;
import static io.trino.plugin.jdbc.JdbcWriteSessionProperties.getWriteParallelism;
import static io.trino.plugin.jdbc.JdbcWriteSessionProperties.isNonTransactionalInsert;
import static io.trino.plugin.jdbc.PredicatePushdownController.DISABLE_PUSHDOWN;
import static io.trino.plugin.jdbc.StandardColumnMappings.varcharReadFunction;
Expand Down Expand Up @@ -1342,6 +1344,12 @@ public void truncateTable(ConnectorSession session, JdbcTableHandle handle)
execute(session, sql);
}

@Override
public OptionalInt getMaxWriteParallelism(ConnectorSession session)
{
return OptionalInt.of(getWriteParallelism(session));
}

protected void verifySchemaName(DatabaseMetaData databaseMetadata, String schemaName)
throws SQLException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -565,6 +566,12 @@ public Optional<TableScanRedirectApplicationResult> getTableScanRedirection(Conn
return delegate.getTableScanRedirection(session, tableHandle);
}

@Override
public OptionalInt getMaxWriteParallelism(ConnectorSession session)
{
return delegate.getMaxWriteParallelism(session);
}

public void onDataChanged(SchemaTableName table)
{
invalidateAllIf(statisticsCache, key -> key.mayReference(table));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -994,6 +995,12 @@ public void renameSchema(ConnectorSession session, String schemaName, String new
jdbcClient.renameSchema(session, schemaName, newSchemaName);
}

@Override
public OptionalInt getMaxWriterTasks(ConnectorSession session)
{
return jdbcClient.getMaxWriteParallelism(session);
Comment thread
wendigo marked this conversation as resolved.
Outdated
}

private static boolean isTableHandleForProcedure(ConnectorTableHandle tableHandle)
{
return tableHandle instanceof JdbcProcedureHandle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;
import java.util.function.Supplier;
Expand Down Expand Up @@ -431,4 +432,10 @@ public void truncateTable(ConnectorSession session, JdbcTableHandle handle)
{
delegate().truncateTable(session, handle);
}

@Override
public OptionalInt getMaxWriteParallelism(ConnectorSession session)
{
return delegate().getMaxWriteParallelism(session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;

Expand Down Expand Up @@ -229,4 +230,6 @@ default Optional<TableScanRedirectApplicationResult> getTableScanRedirection(Con
OptionalLong delete(ConnectorSession session, JdbcTableHandle handle);

void truncateTable(ConnectorSession session, JdbcTableHandle handle);

OptionalInt getMaxWriteParallelism(ConnectorSession session);
Comment thread
wendigo marked this conversation as resolved.
Outdated
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
public class JdbcWriteConfig
{
public static final int MAX_ALLOWED_WRITE_BATCH_SIZE = 10_000_000;
static final int DEFAULT_WRITE_PARALELLISM = 8;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

by default QueryManagerConfig defines maxWriterTasksCount = 100 so this is a drastic reduction of writers. Is it by intention?
I see, that it's only for jdbc, so it's probably acceptable and 8 is even better than 100.
Should it be documented?
@jhlodin

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

100 means unbounded. for jdbc the higher number of writers, the bigger number of connections opened/acquired/held per write which can result in high lock contention on the database side.


private int writeBatchSize = 1000;
private int writeParallelism = DEFAULT_WRITE_PARALELLISM;
Comment thread
wendigo marked this conversation as resolved.
Outdated

// Do not create temporary table during insert.
// This means that the write operation can fail and leave the table in an inconsistent state.
Expand Down Expand Up @@ -57,4 +59,19 @@ public JdbcWriteConfig setNonTransactionalInsert(boolean nonTransactionalInsert)
this.nonTransactionalInsert = nonTransactionalInsert;
return this;
}

@Min(1)
@Max(128)
Comment thread
wendigo marked this conversation as resolved.
Outdated
public int getWriteParallelism()
{
return writeParallelism;
}

@Config("write.parallelism")
@ConfigDescription("Maximum number of parallel write tasks")
public JdbcWriteConfig setWriteParallelism(int writeParallelism)
{
this.writeParallelism = writeParallelism;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class JdbcWriteSessionProperties
{
public static final String WRITE_BATCH_SIZE = "write_batch_size";
public static final String NON_TRANSACTIONAL_INSERT = "non_transactional_insert";
public static final String WRITE_PARALLELISM = "write_parallelism";

private final List<PropertyMetadata<?>> properties;

Expand All @@ -51,6 +52,11 @@ public JdbcWriteSessionProperties(JdbcWriteConfig writeConfig)
"Do not use temporary table on insert to table",
writeConfig.isNonTransactionalInsert(),
false))
.add(integerProperty(
WRITE_PARALLELISM,
"Maximum number of parallel write tasks",
writeConfig.getWriteParallelism(),
false))
.build();
}

Expand All @@ -65,6 +71,11 @@ public static int getWriteBatchSize(ConnectorSession session)
return session.getProperty(WRITE_BATCH_SIZE, Integer.class);
}

public static int getWriteParallelism(ConnectorSession session)
{
return session.getProperty(WRITE_PARALLELISM, Integer.class);
}

public static boolean isNonTransactionalInsert(ConnectorSession session)
{
return session.getProperty(NON_TRANSACTIONAL_INSERT, Boolean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.Set;

Expand Down Expand Up @@ -451,4 +452,10 @@ public void truncateTable(ConnectorSession session, JdbcTableHandle handle)
{
stats.getTruncateTable().wrap(() -> delegate().truncateTable(session, handle));
}

@Override
public OptionalInt getMaxWriteParallelism(ConnectorSession session)
{
return delegate().getMaxWriteParallelism(session);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.trino.sql.planner.plan.OutputNode;
import io.trino.sql.planner.plan.ProjectNode;
import io.trino.sql.planner.plan.TableScanNode;
import io.trino.sql.planner.plan.TableWriterNode;
import io.trino.sql.planner.plan.TopNNode;
import io.trino.sql.planner.plan.ValuesNode;
import io.trino.sql.query.QueryAssertions.QueryAssert;
Expand Down Expand Up @@ -73,6 +74,7 @@
import static io.trino.sql.planner.OptimizerConfig.JoinDistributionType.PARTITIONED;
import static io.trino.sql.planner.assertions.PlanMatchPattern.anyTree;
import static io.trino.sql.planner.assertions.PlanMatchPattern.node;
import static io.trino.sql.planner.optimizations.PlanNodeSearcher.searchFrom;
import static io.trino.testing.DataProviders.toDataProvider;
import static io.trino.testing.QueryAssertions.assertEqualsIgnoreOrder;
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_AGGREGATION_PUSHDOWN;
Expand Down Expand Up @@ -1683,6 +1685,46 @@ public void testWriteBatchSizeSessionProperty(Integer batchSize, Integer numberO
}
}

@Test(dataProvider = "writeTaskParallelismDataProvider")
public void testWriteTaskParallelismSessionProperty(int parallelism, int numberOfRows)
{
if (!hasBehavior(SUPPORTS_CREATE_TABLE)) {
throw new SkipException("CREATE TABLE is required for write_parallelism test but is not supported");
}

Session session = Session.builder(getSession())
.setCatalogSessionProperty(getSession().getCatalog().orElseThrow(), "write_parallelism", String.valueOf(parallelism))
.build();

try (TestTable table = new TestTable(
getQueryRunner()::execute,
"write_parallelism",
"(a varchar(128), b bigint)")) {
assertUpdate(session, "INSERT INTO " + table.getName() + " (a, b) SELECT clerk, orderkey FROM tpch.sf100.orders LIMIT " + numberOfRows, numberOfRows, plan -> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

very generous

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TableWriterNode.WriterTarget target = searchFrom(plan.getRoot())
.where(node -> node instanceof TableWriterNode)
.findFirst()
.map(TableWriterNode.class::cast)
.map(TableWriterNode::getTarget)
.orElseThrow();

assertThat(target.getMaxWriterTasks(getQueryRunner().getMetadata(), getSession()))
.hasValue(parallelism);
});
}
}

@DataProvider
public static Object[][] writeTaskParallelismDataProvider()
{
return new Object[][]{
{1, 10_000},
{2, 10_000},
{4, 10_000},
{16, 10_000},
{32, 10_000}};
}

private static List<String> buildRowsForInsert(int numberOfRows)
{
List<String> result = new ArrayList<>(numberOfRows);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void testDefaults()
{
assertRecordedDefaults(recordDefaults(JdbcWriteConfig.class)
.setWriteBatchSize(1000)
.setWriteParallelism(8)
.setNonTransactionalInsert(false));
}

Expand All @@ -42,11 +43,13 @@ public void testExplicitPropertyMappings()
Map<String, String> properties = ImmutableMap.<String, String>builder()
.put("write.batch-size", "24")
.put("insert.non-transactional-insert.enabled", "true")
.put("write.parallelism", "16")
.buildOrThrow();

JdbcWriteConfig expected = new JdbcWriteConfig()
.setWriteBatchSize(24)
.setNonTransactionalInsert(true);
.setNonTransactionalInsert(true)
.setWriteParallelism(16);

assertFullMapping(properties, expected);
}
Expand Down