Skip to content

Commit

Permalink
Merge branch 'master' into AMORO-2842
Browse files Browse the repository at this point in the history
  • Loading branch information
huyuanfeng2018 authored Sep 24, 2024
2 parents dc78c5b + dd29101 commit 50b6577
Show file tree
Hide file tree
Showing 31 changed files with 553 additions and 210 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 @@ -500,18 +500,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 @@ -37,11 +37,14 @@
import org.apache.amoro.utils.MixedTableUtil;
import org.apache.amoro.utils.TablePropertyUtil;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Snapshot;
import org.apache.iceberg.SnapshotSummary;
import org.apache.iceberg.StructLike;
import org.apache.iceberg.expressions.Expression;
import org.apache.iceberg.expressions.Expressions;
import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.util.Pair;
import org.apache.iceberg.util.PropertyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -77,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 @@ -142,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 Expand Up @@ -177,6 +180,13 @@ public PendingInput getPendingInput() {
if (!isInitialized) {
initEvaluator();
}
// Dangling delete files will cause the data scanned by TableScan
// to be inconsistent with the snapshot summary of iceberg
if (TableFormat.ICEBERG == mixedTable.format()) {
Snapshot snapshot = mixedTable.asUnkeyedTable().snapshot(currentSnapshot.snapshotId());
return new PendingInput(partitionPlanMap.values(), snapshot);
}

return new PendingInput(partitionPlanMap.values());
}

Expand All @@ -191,37 +201,73 @@ public static class PendingInput {

@JsonIgnore private final Map<Integer, Set<StructLike>> partitions = Maps.newHashMap();

private int totalFileCount = 0;
private long totalFileSize = 0L;
private long totalFileRecords = 0L;
private int dataFileCount = 0;
private long dataFileSize = 0;
private long dataFileRecords = 0;
private long dataFileSize = 0L;
private long dataFileRecords = 0L;
private int equalityDeleteFileCount = 0;
private int positionalDeleteFileCount = 0;
private long positionalDeleteBytes = 0L;
private long equalityDeleteBytes = 0L;
private long equalityDeleteFileRecords = 0L;
private long positionalDeleteFileRecords = 0L;
private int danglingDeleteFileCount = 0;
private int healthScore = -1; // -1 means not calculated

public PendingInput() {}

public PendingInput(Collection<PartitionEvaluator> evaluators) {
initialize(evaluators);
totalFileCount = dataFileCount + positionalDeleteFileCount + equalityDeleteFileCount;
totalFileSize = dataFileSize + positionalDeleteBytes + equalityDeleteBytes;
totalFileRecords = dataFileRecords + positionalDeleteFileRecords + equalityDeleteFileRecords;
}

public PendingInput(Collection<PartitionEvaluator> evaluators, Snapshot snapshot) {
initialize(evaluators);
Map<String, String> summary = snapshot.summary();
int totalDeleteFiles =
PropertyUtil.propertyAsInt(summary, SnapshotSummary.TOTAL_DELETE_FILES_PROP, 0);
int totalDataFiles =
PropertyUtil.propertyAsInt(summary, SnapshotSummary.TOTAL_DATA_FILES_PROP, 0);
totalFileRecords = PropertyUtil.propertyAsInt(summary, SnapshotSummary.TOTAL_RECORDS_PROP, 0);
totalFileSize = PropertyUtil.propertyAsLong(summary, SnapshotSummary.TOTAL_FILE_SIZE_PROP, 0);
totalFileCount = totalDeleteFiles + totalDataFiles;
danglingDeleteFileCount =
totalDeleteFiles - equalityDeleteFileCount - positionalDeleteFileCount;
}

private void initialize(Collection<PartitionEvaluator> evaluators) {
double totalHealthScore = 0;
for (PartitionEvaluator evaluator : evaluators) {
partitions
.computeIfAbsent(evaluator.getPartition().first(), ignore -> Sets.newHashSet())
.add(evaluator.getPartition().second());
dataFileCount += evaluator.getFragmentFileCount() + evaluator.getSegmentFileCount();
dataFileSize += evaluator.getFragmentFileSize() + evaluator.getSegmentFileSize();
dataFileRecords += evaluator.getFragmentFileRecords() + evaluator.getSegmentFileRecords();
positionalDeleteBytes += evaluator.getPosDeleteFileSize();
positionalDeleteFileRecords += evaluator.getPosDeleteFileRecords();
positionalDeleteFileCount += evaluator.getPosDeleteFileCount();
equalityDeleteBytes += evaluator.getEqualityDeleteFileSize();
equalityDeleteFileRecords += evaluator.getEqualityDeleteFileRecords();
equalityDeleteFileCount += evaluator.getEqualityDeleteFileCount();
addPartitionData(evaluator);
totalHealthScore += evaluator.getHealthScore();
}
healthScore = (int) Math.ceil(totalHealthScore / evaluators.size());
healthScore = avgHealthScore(totalHealthScore, evaluators.size());
}

private void addPartitionData(PartitionEvaluator evaluator) {
partitions
.computeIfAbsent(evaluator.getPartition().first(), ignore -> Sets.newHashSet())
.add(evaluator.getPartition().second());
dataFileCount += evaluator.getFragmentFileCount() + evaluator.getSegmentFileCount();
dataFileSize += evaluator.getFragmentFileSize() + evaluator.getSegmentFileSize();
dataFileRecords += evaluator.getFragmentFileRecords() + evaluator.getSegmentFileRecords();
positionalDeleteBytes += evaluator.getPosDeleteFileSize();
positionalDeleteFileRecords += evaluator.getPosDeleteFileRecords();
positionalDeleteFileCount += evaluator.getPosDeleteFileCount();
equalityDeleteBytes += evaluator.getEqualityDeleteFileSize();
equalityDeleteFileRecords += evaluator.getEqualityDeleteFileRecords();
equalityDeleteFileCount += evaluator.getEqualityDeleteFileCount();
}

private int avgHealthScore(double totalHealthScore, int partitionCount) {
if (partitionCount == 0) {
return 0;
}
return (int) Math.ceil(totalHealthScore / partitionCount);
}

public Map<Integer, Set<StructLike>> getPartitions() {
Expand Down Expand Up @@ -268,9 +314,28 @@ public int getHealthScore() {
return healthScore;
}

public int getTotalFileCount() {
return totalFileCount;
}

public long getTotalFileSize() {
return totalFileSize;
}

public long getTotalFileRecords() {
return totalFileRecords;
}

public int getDanglingDeleteFileCount() {
return danglingDeleteFileCount;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("totalFileCount", totalFileCount)
.add("totalFileSize", totalFileSize)
.add("totalFileRecords", totalFileRecords)
.add("partitions", partitions)
.add("dataFileCount", dataFileCount)
.add("dataFileSize", dataFileSize)
Expand All @@ -282,6 +347,7 @@ public String toString() {
.add("equalityDeleteFileRecords", equalityDeleteFileRecords)
.add("positionalDeleteFileRecords", positionalDeleteFileRecords)
.add("healthScore", healthScore)
.add("danglingDeleteFileCount", danglingDeleteFileCount)
.toString();
}
}
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
Loading

0 comments on commit 50b6577

Please sign in to comment.