|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hudi.util; |
| 20 | + |
| 21 | +import org.apache.hudi.common.model.HoodieDeltaWriteStat; |
| 22 | +import org.apache.hudi.common.model.HoodieWriteStat; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +/** |
| 30 | + * Helper clazz to merge hoodie write stats that belong to one file path. |
| 31 | + * |
| 32 | + * <p>CAUTION: The merge can be buggy, we need to maintain the new variables for the write stat. |
| 33 | + */ |
| 34 | +public class WriteStatMerger { |
| 35 | + public static HoodieWriteStat merge(HoodieWriteStat stat1, HoodieWriteStat stat2) { |
| 36 | + if (stat1 instanceof HoodieDeltaWriteStat) { |
| 37 | + return mergeDeltaWriteStat((HoodieDeltaWriteStat) stat1, (HoodieDeltaWriteStat) stat2); |
| 38 | + } |
| 39 | + return mergeWriteStat(new HoodieWriteStat(), stat1, stat2); |
| 40 | + } |
| 41 | + |
| 42 | + private static HoodieDeltaWriteStat mergeDeltaWriteStat( |
| 43 | + HoodieDeltaWriteStat stat1, |
| 44 | + HoodieDeltaWriteStat stat2) { |
| 45 | + HoodieDeltaWriteStat merged = new HoodieDeltaWriteStat(); |
| 46 | + mergeWriteStat(merged, stat1, stat2); |
| 47 | + merged.setLogVersion(stat2.getLogVersion()); |
| 48 | + merged.setLogOffset(stat2.getLogOffset()); |
| 49 | + merged.setBaseFile(stat2.getBaseFile()); |
| 50 | + // log files |
| 51 | + List<String> mergedLogFiles = new ArrayList<>(stat1.getLogFiles()); |
| 52 | + for (String logFile : stat2.getLogFiles()) { |
| 53 | + if (!mergedLogFiles.contains(logFile)) { |
| 54 | + mergedLogFiles.add(logFile); |
| 55 | + } |
| 56 | + } |
| 57 | + merged.setLogFiles(mergedLogFiles); |
| 58 | + // column stats |
| 59 | + if (stat1.getColumnStats().isPresent()) { |
| 60 | + merged.putRecordsStats(stat1.getColumnStats().get()); |
| 61 | + } |
| 62 | + if (stat2.getColumnStats().isPresent()) { |
| 63 | + merged.putRecordsStats(stat2.getColumnStats().get()); |
| 64 | + } |
| 65 | + return merged; |
| 66 | + } |
| 67 | + |
| 68 | + private static HoodieWriteStat mergeWriteStat(HoodieWriteStat merged, HoodieWriteStat stat1, HoodieWriteStat stat2) { |
| 69 | + merged.setFileId(stat2.getFileId()); |
| 70 | + merged.setPath(stat2.getPath()); |
| 71 | + // merge cdc stats |
| 72 | + merged.setCdcStats(getMergedCdcStats(stat1.getCdcStats(), stat2.getCdcStats())); |
| 73 | + // prev commit |
| 74 | + merged.setPrevCommit(stat2.getPrevCommit()); |
| 75 | + |
| 76 | + merged.setNumWrites(stat2.getNumWrites() + stat1.getNumWrites()); |
| 77 | + merged.setNumDeletes(stat2.getNumDeletes() + stat1.getNumDeletes()); |
| 78 | + merged.setNumUpdateWrites(stat2.getNumUpdateWrites() + stat1.getNumUpdateWrites()); |
| 79 | + merged.setNumInserts(stat2.getNumInserts() + stat1.getNumInserts()); |
| 80 | + merged.setTotalWriteBytes(stat2.getTotalWriteBytes() + stat1.getTotalWriteBytes()); |
| 81 | + merged.setTotalWriteErrors(stat2.getTotalWriteErrors() + stat1.getTotalWriteErrors()); |
| 82 | + |
| 83 | + // ------------------------------------------------------------------------- |
| 84 | + // Nullable |
| 85 | + // ------------------------------------------------------------------------- |
| 86 | + |
| 87 | + // tmp path |
| 88 | + merged.setTempPath(stat2.getTempPath()); |
| 89 | + // partition path |
| 90 | + merged.setPartitionPath(stat2.getPartitionPath()); |
| 91 | + // runtime stats |
| 92 | + merged.setRuntimeStats(getMergedRuntimeStats(stat1.getRuntimeStats(), stat2.getRuntimeStats())); |
| 93 | + |
| 94 | + // log statistics |
| 95 | + merged.setTotalLogRecords(stat2.getTotalLogRecords() + stat1.getTotalLogRecords()); |
| 96 | + merged.setTotalLogFilesCompacted(stat2.getTotalLogFilesCompacted() + stat1.getTotalLogFilesCompacted()); |
| 97 | + merged.setTotalLogSizeCompacted(stat2.getTotalLogSizeCompacted() + stat1.getTotalLogSizeCompacted()); |
| 98 | + merged.setTotalUpdatedRecordsCompacted(stat2.getTotalUpdatedRecordsCompacted() + stat1.getTotalUpdatedRecordsCompacted()); |
| 99 | + merged.setTotalLogBlocks(stat2.getTotalLogBlocks() + stat1.getTotalLogBlocks()); |
| 100 | + merged.setTotalCorruptLogBlock(stat2.getTotalCorruptLogBlock() + stat1.getTotalCorruptLogBlock()); |
| 101 | + merged.setTotalRollbackBlocks(stat2.getTotalRollbackBlocks() + stat1.getTotalRollbackBlocks()); |
| 102 | + merged.setFileSizeInBytes(stat2.getFileSizeInBytes() + stat1.getFileSizeInBytes()); |
| 103 | + // event time |
| 104 | + merged.setMinEventTime(minLong(stat1.getMinEventTime(), stat2.getMinEventTime())); |
| 105 | + merged.setMaxEventTime(maxLong(stat1.getMaxEventTime(), stat2.getMaxEventTime())); |
| 106 | + return stat2; |
| 107 | + } |
| 108 | + |
| 109 | + private static HoodieWriteStat.RuntimeStats getMergedRuntimeStats( |
| 110 | + HoodieWriteStat.RuntimeStats runtimeStats1, |
| 111 | + HoodieWriteStat.RuntimeStats runtimeStats2) { |
| 112 | + final HoodieWriteStat.RuntimeStats runtimeStats; |
| 113 | + if (runtimeStats1 != null && runtimeStats2 != null) { |
| 114 | + runtimeStats = new HoodieWriteStat.RuntimeStats(); |
| 115 | + runtimeStats.setTotalScanTime(runtimeStats1.getTotalScanTime() + runtimeStats2.getTotalScanTime()); |
| 116 | + runtimeStats.setTotalUpsertTime(runtimeStats1.getTotalUpsertTime() + runtimeStats2.getTotalUpsertTime()); |
| 117 | + runtimeStats.setTotalCreateTime(runtimeStats1.getTotalCreateTime() + runtimeStats2.getTotalCreateTime()); |
| 118 | + } else if (runtimeStats1 == null) { |
| 119 | + runtimeStats = runtimeStats2; |
| 120 | + } else { |
| 121 | + runtimeStats = runtimeStats1; |
| 122 | + } |
| 123 | + return runtimeStats; |
| 124 | + } |
| 125 | + |
| 126 | + private static Map<String, Long> getMergedCdcStats(Map<String, Long> cdcStats1, Map<String, Long> cdcStats2) { |
| 127 | + final Map<String, Long> cdcStats; |
| 128 | + if (cdcStats1 != null && cdcStats2 != null) { |
| 129 | + cdcStats = new HashMap<>(); |
| 130 | + cdcStats.putAll(cdcStats1); |
| 131 | + cdcStats.putAll(cdcStats2); |
| 132 | + } else if (cdcStats1 == null) { |
| 133 | + cdcStats = cdcStats2; |
| 134 | + } else { |
| 135 | + cdcStats = cdcStats1; |
| 136 | + } |
| 137 | + return cdcStats; |
| 138 | + } |
| 139 | + |
| 140 | + private static Long minLong(Long v1, Long v2) { |
| 141 | + if (v1 == null) { |
| 142 | + return v2; |
| 143 | + } |
| 144 | + if (v2 == null) { |
| 145 | + return v1; |
| 146 | + } |
| 147 | + return v1.compareTo(v2) < 0 ? v1 : v2; |
| 148 | + } |
| 149 | + |
| 150 | + private static Long maxLong(Long v1, Long v2) { |
| 151 | + if (v1 == null) { |
| 152 | + return v2; |
| 153 | + } |
| 154 | + if (v2 == null) { |
| 155 | + return v1; |
| 156 | + } |
| 157 | + return v1.compareTo(v2) > 0 ? v1 : v2; |
| 158 | + } |
| 159 | +} |
0 commit comments