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

Refactor persistence strategy for dlo generation #292

Merged
merged 3 commits into from
Mar 10, 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 @@ -9,7 +9,6 @@
import com.linkedin.openhouse.jobs.spark.state.StateManager;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -43,36 +42,43 @@ protected void runInner(Operations ops) throws Exception {
.tableFileStats(tableFileStats)
.tablePartitionStats(tablePartitionStats)
.build();
runInnerTableScope(spark, strategiesGenerator);
runInnerPartitionScope(spark, strategiesGenerator);
// Run table scope for unpartitioned table, and run both table and partition scope for
// partitioned table
boolean isPartitioned = ops.getTable(fqtn).spec().isPartitioned();
runInnerTableScope(spark, strategiesGenerator, isPartitioned);
if (isPartitioned) {
runInnerPartitionScope(spark, strategiesGenerator);
}
}

private void runInnerTableScope(
SparkSession spark, OpenHouseDataLayoutStrategyGenerator strategiesGenerator) {
log.info("Generating strategies for table {}", fqtn);
SparkSession spark,
OpenHouseDataLayoutStrategyGenerator strategiesGenerator,
boolean isPartitioned) {
log.info("Generating table-level strategies for table {}", fqtn);
List<DataLayoutStrategy> strategies = strategiesGenerator.generateTableLevelStrategies();
log.info(
"Generated {} strategies {}",
strategies.size(),
strategies.stream().map(Object::toString).collect(Collectors.joining(", ")));
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you preserve this logging line, it's helpful to see the values for an app run

log.info("Generated {} strategies for table {}", strategies.size(), fqtn);
StrategiesDao dao = StrategiesDaoTableProps.builder().spark(spark).build();
dao.save(fqtn, strategies);
appendToDloStrategiesTable(spark, outputFqtn, strategies, false);
appendToDloStrategiesTable(spark, outputFqtn, strategies, false, isPartitioned);
}

private void runInnerPartitionScope(
SparkSession spark, OpenHouseDataLayoutStrategyGenerator strategiesGenerator) {
log.info("Generating partition-level strategies for table {}", fqtn);
List<DataLayoutStrategy> strategies = strategiesGenerator.generatePartitionLevelStrategies();
log.info("Generated {} strategies", strategies.size());
appendToDloStrategiesTable(spark, partitionLevelOutputFqtn, strategies, true);
log.info("Generated {} strategies for table {}", strategies.size(), fqtn);
StrategiesDao dao = StrategiesDaoTableProps.builder().spark(spark).build();
dao.savePartitionScope(fqtn, strategies);
appendToDloStrategiesTable(spark, partitionLevelOutputFqtn, strategies, true, true);
}

private void appendToDloStrategiesTable(
protected void appendToDloStrategiesTable(
SparkSession spark,
String outputFqtn,
List<DataLayoutStrategy> strategies,
boolean isPartitionScope) {
boolean isPartitionScope,
boolean isPartitioned) {
if (outputFqtn != null && !strategies.isEmpty()) {
createTableIfNotExists(spark, outputFqtn, isPartitionScope);
List<String> rows = new ArrayList<>();
Expand All @@ -96,8 +102,9 @@ private void appendToDloStrategiesTable(
} else {
rows.add(
String.format(
"('%s', current_timestamp(), %f, %f, %f, %d, %d, %d, %d, %d, %d)",
"('%s', %b, current_timestamp(), %f, %f, %f, %d, %d, %d, %d, %d, %d)",
fqtn,
isPartitioned,
strategy.getCost(),
strategy.getGain(),
strategy.getEntropy(),
Expand Down Expand Up @@ -143,6 +150,7 @@ private void createTableIfNotExists(
String.format(
"CREATE TABLE IF NOT EXISTS %s ("
+ "fqtn STRING, "
+ "isPartitioned BOOLEAN, "
+ "timestamp TIMESTAMP, "
+ "estimated_compute_cost DOUBLE, "
+ "estimated_file_count_reduction DOUBLE, "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.linkedin.openhouse.jobs.spark;

import com.linkedin.openhouse.datalayout.persistence.StrategiesDaoTableProps;
import com.linkedin.openhouse.datalayout.strategy.DataLayoutStrategy;
import com.linkedin.openhouse.tablestest.OpenHouseSparkITest;
import java.util.List;
import org.apache.spark.sql.AnalysisException;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class DataLayoutStrategyGeneratorSparkAppTest extends OpenHouseSparkITest {
@Test
public void testPartitionedTable() throws Exception {
try (SparkSession spark = getSparkSession()) {
// create table
String fqtn = "dlo_partitioned.test_table";
String outputFqtn = "dlo_partitioned.dlo_strategies";
String partitionLevelOutputFqtn = "dlo_partitioned.dlo_partition_strategies";
spark.sql(
String.format(
"create table openhouse.%s (id int, name string) partitioned by (id)", fqtn));
spark.sql(String.format("insert into openhouse.%s values (1, 'a'), (2, 'b')", fqtn));
// run app
Operations ops = Operations.withCatalog(spark, null);
DataLayoutStrategyGeneratorSparkApp app =
new DataLayoutStrategyGeneratorSparkApp(
"test-job-id", null, fqtn, outputFqtn, partitionLevelOutputFqtn);
app.runInner(ops);
// test
Assertions.assertEquals(1, spark.sql(String.format("select * from %s", outputFqtn)).count());
Assertions.assertEquals(
2, spark.sql(String.format("select * from %s", partitionLevelOutputFqtn)).count());
Assertions.assertTrue(
spark
.sql(String.format("select * from %s", outputFqtn))
.select("isPartitioned")
.first()
.getBoolean(0));
Assertions.assertNotNull(ops.getTable(fqtn).properties().get("write.data-layout.strategies"));
List<DataLayoutStrategy> strategies =
StrategiesDaoTableProps.deserializeList(
ops.getTable(fqtn).properties().get("write.data-layout.partition-strategies"));
Assertions.assertEquals(2, strategies.size());
}
}

@Test
public void testUnpartitionedTable() throws Exception {
try (SparkSession spark = getSparkSession()) {
// create table
String fqtn = "dlo_unpartitioned.test_table";
String outputFqtn = "dlo_unpartitioned.dlo_strategies";
String partitionLevelOutputFqtn = "dlo_unpartitioned.dlo_partition_strategies";
spark.sql(String.format("create table openhouse.%s (id int, name string)", fqtn));
spark.sql(String.format("insert into openhouse.%s values (1, 'a'), (2, 'b')", fqtn));
// run app
Operations ops = Operations.withCatalog(spark, null);
DataLayoutStrategyGeneratorSparkApp app =
new DataLayoutStrategyGeneratorSparkApp(
"test-job-id", null, fqtn, outputFqtn, partitionLevelOutputFqtn);
app.runInner(ops);
// test
Assertions.assertEquals(1, spark.sql(String.format("select * from %s", outputFqtn)).count());
Assertions.assertThrows(
AnalysisException.class,
() -> spark.sql(String.format("select * from %s", partitionLevelOutputFqtn)));
Assertions.assertFalse(
spark
.sql(String.format("select * from %s", outputFqtn))
.select("isPartitioned")
.first()
.getBoolean(0));
Assertions.assertNotNull(ops.getTable(fqtn).properties().get("write.data-layout.strategies"));
Assertions.assertNull(
ops.getTable(fqtn).properties().get("write.data-layout.partition-strategies"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
public interface StrategiesDao {
void save(String fqtn, List<DataLayoutStrategy> strategies);

void savePartitionScope(String fqtn, List<DataLayoutStrategy> strategies);

List<DataLayoutStrategy> load(String fqtn);

List<DataLayoutStrategy> loadPartitionScope(String fqtn);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
@Builder
public class StrategiesDaoTableProps implements StrategiesDao {
public static final String DATA_LAYOUT_STRATEGIES_PROPERTY_KEY = "write.data-layout.strategies";
public static final String DATA_LAYOUT_STRATEGIES_PARTITION_PROPERTY_KEY =
"write.data-layout.partition-strategies";

private final SparkSession spark;

@Override
Expand All @@ -32,6 +35,16 @@ public void save(String fqtn, List<DataLayoutStrategy> strategies) {
fqtn, DATA_LAYOUT_STRATEGIES_PROPERTY_KEY, propValue));
}

@Override
public void savePartitionScope(String fqtn, List<DataLayoutStrategy> strategies) {
String propValue = serialize(strategies);
log.info("Saving partition level strategies {} for table {}", propValue, fqtn);
spark.sql(
String.format(
"ALTER TABLE %s SET TBLPROPERTIES ('%s' = '%s')",
fqtn, DATA_LAYOUT_STRATEGIES_PARTITION_PROPERTY_KEY, propValue));
}

@Override
public List<DataLayoutStrategy> load(String fqtn) {
String propValue =
Expand All @@ -45,6 +58,20 @@ public List<DataLayoutStrategy> load(String fqtn) {
return deserializeList(propValue);
}

@Override
public List<DataLayoutStrategy> loadPartitionScope(String fqtn) {
String propValue =
spark
.sql(
String.format(
"SHOW TBLPROPERTIES %s ('%s')",
fqtn, DATA_LAYOUT_STRATEGIES_PARTITION_PROPERTY_KEY))
.collectAsList()
.get(0)
.getString(1);
return deserializeList(propValue);
}

public static String serialize(List<DataLayoutStrategy> strategies) {
Gson gson = new GsonBuilder().create();
Type type = new TypeToken<ArrayList<DataLayoutStrategy>>() {}.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void testCompactionStrategyGenerationNonPartitioned() throws Exception {
List<DataLayoutStrategy> strategies = strategyGenerator.generateTableLevelStrategies();
Assertions.assertEquals(1, strategies.size());
StrategiesDao dao = StrategiesDaoTableProps.builder().spark(spark).build();
dao.save(testTable, strategies);
List<DataLayoutStrategy> retrievedStrategies = dao.load(testTable);
dao.savePartitionScope(testTable, strategies);
List<DataLayoutStrategy> retrievedStrategies = dao.loadPartitionScope(testTable);
Assertions.assertEquals(strategies, retrievedStrategies);
}
}
Expand Down
Loading