Skip to content

Commit

Permalink
[Improvement]: Refactor TableFormat to a non-enumeration type to supp…
Browse files Browse the repository at this point in the history
…ort non-open source formats (#3167)

* Make TableFormat extensible

* Make TableFormat extensible | fix ut

* Make TableFormat extensible | fix ut

* Make TableFormat extensible | fix ut

* Fix json serializer && deserializer for TableFormat

* Fix reviewer

* Fix checkstyle

* Fix checkstyle

* Fix checkstyle
  • Loading branch information
baiyangtx authored Sep 24, 2024
1 parent efd6650 commit dd29101
Show file tree
Hide file tree
Showing 27 changed files with 407 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.amoro.config.ConfigHelpers;
import org.apache.amoro.config.Configurations;
import org.apache.amoro.server.dashboard.DashboardServer;
import org.apache.amoro.server.dashboard.JavalinJsonMapper;
import org.apache.amoro.server.dashboard.response.ErrorResponse;
import org.apache.amoro.server.dashboard.utils.AmsUtil;
import org.apache.amoro.server.dashboard.utils.CommonUtil;
Expand Down Expand Up @@ -242,6 +243,7 @@ private void initHttpService() {
config.addStaticFiles(dashboardServer.configStaticFiles());
config.sessionHandler(SessionHandler::new);
config.enableCorsForAllOrigins();
config.jsonMapper(JavalinJsonMapper.createDefaultJsonMapper());
config.showJavalinBanner = false;
});
httpServer.routes(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.amoro.server.dashboard;

import io.javalin.plugin.json.JsonMapper;
import org.apache.amoro.TableFormat;
import org.apache.amoro.shade.jackson2.com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.amoro.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.amoro.shade.jackson2.com.fasterxml.jackson.databind.module.SimpleModule;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/** Json mapper to adapt shaded jackson. */
public class JavalinJsonMapper implements JsonMapper {

private final ObjectMapper objectMapper;

public static JavalinJsonMapper createDefaultJsonMapper() {
ObjectMapper om = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(TableFormat.class, new TableFormat.JsonSerializer());
module.addDeserializer(TableFormat.class, new TableFormat.JsonDeserializer());
om.registerModule(module);
return new JavalinJsonMapper(om);
}

public JavalinJsonMapper(ObjectMapper shadedMapper) {
this.objectMapper = shadedMapper;
}

@NotNull
@Override
public String toJsonString(@NotNull Object obj) {
if (obj instanceof String) {
return (String) obj;
}
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@NotNull
@Override
public InputStream toJsonStream(@NotNull Object obj) {
if (obj instanceof String) {
String result = (String) obj;
return new ByteArrayInputStream(result.getBytes());
} else {
byte[] string = new byte[0];
try {
string = objectMapper.writeValueAsBytes(obj);
return new ByteArrayInputStream(string);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

@NotNull
@Override
public <T> T fromJsonString(@NotNull String json, @NotNull Class<T> targetClass) {
try {
return objectMapper.readValue(json, targetClass);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@NotNull
@Override
public <T> T fromJsonStream(@NotNull InputStream json, @NotNull Class<T> targetClass) {
try {
return objectMapper.readValue(json, targetClass);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -497,18 +497,16 @@ public void getTableList(Context ctx) {
ServerCatalog serverCatalog = tableService.getServerCatalog(catalog);
Function<TableFormat, String> formatToType =
format -> {
switch (format) {
case MIXED_HIVE:
case MIXED_ICEBERG:
return TableMeta.TableType.ARCTIC.toString();
case PAIMON:
return TableMeta.TableType.PAIMON.toString();
case ICEBERG:
return TableMeta.TableType.ICEBERG.toString();
case HUDI:
return TableMeta.TableType.HUDI.toString();
default:
throw new IllegalStateException("Unknown format");
if (format.equals(TableFormat.MIXED_HIVE) || format.equals(TableFormat.MIXED_ICEBERG)) {
return TableMeta.TableType.ARCTIC.toString();
} else if (format.equals(TableFormat.PAIMON)) {
return TableMeta.TableType.PAIMON.toString();
} else if (format.equals(TableFormat.ICEBERG)) {
return TableMeta.TableType.ICEBERG.toString();
} else if (format.equals(TableFormat.HUDI)) {
return TableMeta.TableType.HUDI.toString();
} else {
return format.toString();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ default void cleanDanglingDeleteFiles(TableRuntime tableRuntime) {

static TableMaintainer ofTable(AmoroTable<?> amoroTable) {
TableFormat format = amoroTable.format();
if (format == TableFormat.MIXED_HIVE || format == TableFormat.MIXED_ICEBERG) {
if (format.in(TableFormat.MIXED_HIVE, TableFormat.MIXED_ICEBERG)) {
return new MixedTableMaintainer((MixedTable) amoroTable.originalTable());
} else if (format == TableFormat.ICEBERG) {
} else if (TableFormat.ICEBERG.equals(format)) {
return new IcebergTableMaintainer((Table) amoroTable.originalTable());
} else {
throw new RuntimeException("Unsupported table type" + amoroTable.originalTable().getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public TableRuntime getTableRuntime() {
protected void initEvaluator() {
long startTime = System.currentTimeMillis();
TableFileScanHelper tableFileScanHelper;
if (TableFormat.ICEBERG == mixedTable.format()) {
if (TableFormat.ICEBERG.equals(mixedTable.format())) {
tableFileScanHelper =
new IcebergTableFileScanHelper(mixedTable.asUnkeyedTable(), currentSnapshot.snapshotId());
} else {
Expand Down Expand Up @@ -145,7 +145,7 @@ private Map<String, String> partitionProperties(Pair<Integer, StructLike> partit
}

protected PartitionEvaluator buildEvaluator(Pair<Integer, StructLike> partition) {
if (TableFormat.ICEBERG == mixedTable.format()) {
if (TableFormat.ICEBERG.equals(mixedTable.format())) {
return new CommonPartitionEvaluator(tableRuntime, partition, System.currentTimeMillis());
} else {
Map<String, String> partitionProperties = partitionProperties(partition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public PartitionPlannerFactory(
}

public PartitionEvaluator buildPartitionPlanner(Pair<Integer, StructLike> partition) {
if (TableFormat.ICEBERG == mixedTable.format()) {
if (TableFormat.ICEBERG.equals(mixedTable.format())) {
return new IcebergPartitionPlan(tableRuntime, mixedTable, partition, planTime);
} else {
if (TableTypeUtil.isHive(mixedTable)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.amoro.server.persistence.converter;

import org.apache.amoro.TableFormat;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes(TableFormat.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class TableFormatConverter implements TypeHandler<TableFormat> {

@Override
public void setParameter(PreparedStatement ps, int i, TableFormat parameter, JdbcType jdbcType)
throws SQLException {
if (parameter == null) {
ps.setString(i, "");
} else {
ps.setString(i, parameter.name());
}
}

@Override
public TableFormat getResult(ResultSet rs, String columnName) throws SQLException {
String res = rs.getString(columnName);
if (res == null) {
return null;
}
return TableFormat.valueOf(res);
}

@Override
public TableFormat getResult(ResultSet rs, int columnIndex) throws SQLException {
String res = rs.getString(columnIndex);
if (res == null) {
return null;
}
return TableFormat.valueOf(res);
}

@Override
public TableFormat getResult(CallableStatement cs, int columnIndex) throws SQLException {
String res = cs.getString(columnIndex);
if (res == null) {
return null;
}
return TableFormat.valueOf(res);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.amoro.server.persistence.converter.Map2StringConverter;
import org.apache.amoro.server.persistence.converter.MapLong2StringConverter;
import org.apache.amoro.server.persistence.converter.OptimizingStatusConverter;
import org.apache.amoro.server.persistence.converter.TableFormatConverter;
import org.apache.amoro.server.table.TableMetadata;
import org.apache.amoro.server.table.TableRuntime;
import org.apache.ibatis.annotations.Delete;
Expand Down Expand Up @@ -78,7 +79,10 @@ Integer decTableCount(
@Result(property = "tableIdentifier.tableName", column = "table_name"),
@Result(property = "tableIdentifier.database", column = "db_name"),
@Result(property = "tableIdentifier.catalog", column = "catalog_name"),
@Result(property = "tableIdentifier.format", column = "format"),
@Result(
property = "tableIdentifier.format",
column = "format",
typeHandler = TableFormatConverter.class),
@Result(property = "primaryKey", column = "primary_key"),
@Result(property = "tableLocation", column = "table_location"),
@Result(property = "baseLocation", column = "base_location"),
Expand Down Expand Up @@ -113,7 +117,10 @@ Integer decTableCount(
@Result(property = "tableIdentifier.catalog", column = "catalog_name"),
@Result(property = "tableIdentifier.database", column = "db_name"),
@Result(property = "tableIdentifier.tableName", column = "table_name"),
@Result(property = "tableIdentifier.format", column = "format"),
@Result(
property = "tableIdentifier.format",
column = "format",
typeHandler = TableFormatConverter.class),
@Result(property = "primaryKey", column = "primary_key"),
@Result(property = "tableLocation", column = "table_location"),
@Result(property = "baseLocation", column = "base_location"),
Expand Down Expand Up @@ -183,7 +190,10 @@ int commitTableChange(
@Result(property = "tableIdentifier.catalog", column = "catalog_name"),
@Result(property = "tableIdentifier.database", column = "db_name"),
@Result(property = "tableIdentifier.tableName", column = "table_name"),
@Result(property = "tableIdentifier.format", column = "format"),
@Result(
property = "tableIdentifier.format",
column = "format",
typeHandler = TableFormatConverter.class),
@Result(property = "primaryKey", column = "primary_key"),
@Result(property = "tableLocation", column = "table_location"),
@Result(property = "baseLocation", column = "base_location"),
Expand Down Expand Up @@ -218,7 +228,10 @@ int commitTableChange(
@Result(property = "tableIdentifier.catalog", column = "catalog_name"),
@Result(property = "tableIdentifier.database", column = "db_name"),
@Result(property = "tableIdentifier.tableName", column = "table_name"),
@Result(property = "tableIdentifier.format", column = "format"),
@Result(
property = "tableIdentifier.format",
column = "format",
typeHandler = TableFormatConverter.class),
@Result(property = "primaryKey", column = "primary_key"),
@Result(property = "tableLocation", column = "table_location"),
@Result(property = "baseLocation", column = "base_location"),
Expand All @@ -245,7 +258,7 @@ TableMetadata selectTableMetaByName(
@Insert(
"INSERT INTO table_identifier(catalog_name, db_name, table_name, format) VALUES("
+ " #{tableIdentifier.catalog}, #{tableIdentifier.database}, #{tableIdentifier.tableName}, "
+ " #{tableIdentifier.format})")
+ " #{tableIdentifier.format, typeHandler=org.apache.amoro.server.persistence.converter.TableFormatConverter})")
@Options(useGeneratedKeys = true, keyProperty = "tableIdentifier.id")
void insertTable(@Param("tableIdentifier") ServerTableIdentifier tableIdentifier);

Expand All @@ -268,7 +281,7 @@ Integer deleteTableIdByName(
@Result(property = "tableName", column = "table_name"),
@Result(property = "database", column = "db_name"),
@Result(property = "catalog", column = "catalog_name"),
@Result(property = "format", column = "format"),
@Result(property = "format", column = "format", typeHandler = TableFormatConverter.class)
})
ServerTableIdentifier selectTableIdentifier(
@Param("catalogName") String catalogName,
Expand All @@ -283,7 +296,7 @@ ServerTableIdentifier selectTableIdentifier(
@Result(property = "catalog", column = "catalog_name"),
@Result(property = "database", column = "db_name"),
@Result(property = "tableName", column = "table_name"),
@Result(property = "format", column = "format")
@Result(property = "format", column = "format", typeHandler = TableFormatConverter.class)
})
List<ServerTableIdentifier> selectTableIdentifiersByDb(
@Param("catalogName") String catalogName, @Param("databaseName") String databaseName);
Expand All @@ -296,7 +309,7 @@ List<ServerTableIdentifier> selectTableIdentifiersByDb(
@Result(property = "catalog", column = "catalog_name"),
@Result(property = "database", column = "db_name"),
@Result(property = "tableName", column = "table_name"),
@Result(property = "format", column = "format")
@Result(property = "format", column = "format", typeHandler = TableFormatConverter.class)
})
List<ServerTableIdentifier> selectTableIdentifiersByCatalog(
@Param("catalogName") String catalogName);
Expand All @@ -307,7 +320,7 @@ List<ServerTableIdentifier> selectTableIdentifiersByCatalog(
@Result(property = "catalog", column = "catalog_name"),
@Result(property = "database", column = "db_name"),
@Result(property = "tableName", column = "table_name"),
@Result(property = "format", column = "format")
@Result(property = "format", column = "format", typeHandler = TableFormatConverter.class)
})
List<ServerTableIdentifier> selectAllTableIdentifiers();

Expand Down Expand Up @@ -383,7 +396,7 @@ List<ServerTableIdentifier> selectTableIdentifiersByCatalog(
@Result(property = "catalogName", column = "catalog_name"),
@Result(property = "dbName", column = "db_name"),
@Result(property = "tableName", column = "table_name"),
@Result(property = "format", column = "format"),
@Result(property = "format", column = "format", typeHandler = TableFormatConverter.class),
@Result(property = "currentSnapshotId", column = "current_snapshot_id"),
@Result(property = "currentChangeSnapshotId", column = "current_change_snapshotId"),
@Result(property = "lastOptimizedSnapshotId", column = "last_optimized_snapshotId"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ private boolean triggerTableAdded(
AmoroTable<?> table =
catalog.loadTable(
serverTableIdentifier.getDatabase(), serverTableIdentifier.getTableName());
if (TableFormat.ICEBERG == table.format()) {
if (TableFormat.ICEBERG.equals(table.format())) {
if (TablePropertyUtil.isMixedTableStore(table.properties())) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class InternalTableUtil {
*/
public static boolean isLegacyMixedIceberg(
org.apache.amoro.server.table.TableMetadata internalTableMetadata) {
return TableFormat.MIXED_ICEBERG == internalTableMetadata.getFormat()
return TableFormat.MIXED_ICEBERG.equals(internalTableMetadata.getFormat())
&& !Boolean.parseBoolean(
internalTableMetadata.getProperties().get(MIXED_ICEBERG_BASED_REST));
}
Expand Down
Loading

0 comments on commit dd29101

Please sign in to comment.