|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.fluss.client.write; |
| 19 | + |
| 20 | +import org.apache.fluss.annotation.Internal; |
| 21 | +import org.apache.fluss.metadata.TablePath; |
| 22 | +import org.apache.fluss.row.InternalRow; |
| 23 | +import org.apache.fluss.rpc.messages.ApplyShreddingSchemaRequest; |
| 24 | +import org.apache.fluss.rpc.messages.ApplyShreddingSchemaResponse; |
| 25 | +import org.apache.fluss.rpc.messages.PbTablePath; |
| 26 | +import org.apache.fluss.types.variant.ShreddingSchema; |
| 27 | +import org.apache.fluss.types.variant.ShreddingSchemaInferrer; |
| 28 | +import org.apache.fluss.types.variant.Variant; |
| 29 | +import org.apache.fluss.types.variant.VariantStatisticsCollector; |
| 30 | + |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 36 | +import java.util.function.Function; |
| 37 | + |
| 38 | +/** |
| 39 | + * Manages automatic Variant shredding inference for a single table on the client write path. |
| 40 | + * |
| 41 | + * <p>Each time a row is appended to a write batch ({@link #collectRow(InternalRow)}), this manager |
| 42 | + * extracts the Variant values from the row's Variant-typed columns and feeds them into per-column |
| 43 | + * {@link VariantStatisticsCollector}s. Once the minimum sample threshold is met and a non-empty |
| 44 | + * {@link ShreddingSchema} is inferred, an asynchronous RPC is dispatched to the Coordinator to |
| 45 | + * trigger server-side schema evolution. |
| 46 | + * |
| 47 | + * <p>Schema evolution is triggered <em>at most once</em> per manager instance. If the RPC fails |
| 48 | + * (e.g., transient network error), the {@link #schemaTriggered} flag is reset to allow a retry on |
| 49 | + * the next collected row. |
| 50 | + * |
| 51 | + * <p>Thread safety: {@link #collectRow} is called from the writer thread and is guarded by the |
| 52 | + * deque lock in {@link RecordAccumulator}. The {@link #schemaTriggered} flag is an {@link |
| 53 | + * AtomicBoolean} so it can safely be reset from the RPC callback thread. |
| 54 | + */ |
| 55 | +@Internal |
| 56 | +public class VariantShreddingManager { |
| 57 | + |
| 58 | + private static final Logger LOG = LoggerFactory.getLogger(VariantShreddingManager.class); |
| 59 | + |
| 60 | + private final TablePath tablePath; |
| 61 | + |
| 62 | + /** |
| 63 | + * Column indices (into the row's schema) of all Variant-typed columns. Each index maps to the |
| 64 | + * corresponding {@link VariantStatisticsCollector} in {@link #collectors} at the same array |
| 65 | + * position. |
| 66 | + */ |
| 67 | + private final int[] variantColumnIndices; |
| 68 | + |
| 69 | + /** |
| 70 | + * Names of the Variant columns, used to construct the column-name-based {@link |
| 71 | + * ShreddingSchema}. |
| 72 | + */ |
| 73 | + private final String[] variantColumnNames; |
| 74 | + |
| 75 | + /** One statistics collector per Variant column. */ |
| 76 | + private final VariantStatisticsCollector[] collectors; |
| 77 | + |
| 78 | + /** Inferrer, configured from the table's shredding options. */ |
| 79 | + private final ShreddingSchemaInferrer inferrer; |
| 80 | + |
| 81 | + /** |
| 82 | + * Guards against duplicate schema evolution RPCs. Set to {@code true} when an RPC is in flight; |
| 83 | + * reset to {@code false} on RPC failure so the next {@link #collectRow} call can retry. |
| 84 | + */ |
| 85 | + private final AtomicBoolean schemaTriggered = new AtomicBoolean(false); |
| 86 | + |
| 87 | + /** |
| 88 | + * Callback that sends the {@link ApplyShreddingSchemaRequest} to the Coordinator and returns a |
| 89 | + * future. Injected by {@link RecordAccumulator} so this class does not depend on a concrete RPC |
| 90 | + * client. |
| 91 | + */ |
| 92 | + private final Function< |
| 93 | + ApplyShreddingSchemaRequest, CompletableFuture<ApplyShreddingSchemaResponse>> |
| 94 | + rpcCaller; |
| 95 | + |
| 96 | + public VariantShreddingManager( |
| 97 | + TablePath tablePath, |
| 98 | + int[] variantColumnIndices, |
| 99 | + String[] variantColumnNames, |
| 100 | + ShreddingSchemaInferrer inferrer, |
| 101 | + Function<ApplyShreddingSchemaRequest, CompletableFuture<ApplyShreddingSchemaResponse>> |
| 102 | + rpcCaller) { |
| 103 | + this.tablePath = tablePath; |
| 104 | + this.variantColumnIndices = variantColumnIndices; |
| 105 | + this.variantColumnNames = variantColumnNames; |
| 106 | + this.inferrer = inferrer; |
| 107 | + this.rpcCaller = rpcCaller; |
| 108 | + |
| 109 | + this.collectors = new VariantStatisticsCollector[variantColumnIndices.length]; |
| 110 | + for (int i = 0; i < variantColumnIndices.length; i++) { |
| 111 | + this.collectors[i] = new VariantStatisticsCollector(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Collects statistics from one row that is about to be (or has just been) written. |
| 117 | + * |
| 118 | + * <p>This method extracts the Variant value at each variant-column index from {@code row} and |
| 119 | + * feeds it into the corresponding {@link VariantStatisticsCollector}. If the inferrer produces |
| 120 | + * a non-empty schema for any column, {@link #triggerSchemaEvolution} is called. |
| 121 | + * |
| 122 | + * @param row the row being written |
| 123 | + */ |
| 124 | + public void collectRow(InternalRow row) { |
| 125 | + if (schemaTriggered.get()) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + for (int c = 0; c < variantColumnIndices.length; c++) { |
| 130 | + int colIdx = variantColumnIndices[c]; |
| 131 | + Variant variant = row.isNullAt(colIdx) ? null : row.getVariant(colIdx); |
| 132 | + collectors[c].collect(variant); |
| 133 | + } |
| 134 | + |
| 135 | + maybeInferAndTrigger(); |
| 136 | + } |
| 137 | + |
| 138 | + // -------------------------------------------------------------------------------------------- |
| 139 | + // Internal helpers |
| 140 | + // -------------------------------------------------------------------------------------------- |
| 141 | + |
| 142 | + private void maybeInferAndTrigger() { |
| 143 | + for (int c = 0; c < variantColumnIndices.length; c++) { |
| 144 | + VariantStatisticsCollector collector = collectors[c]; |
| 145 | + long totalRecords = collector.getTotalRecords(); |
| 146 | + |
| 147 | + // Skip inference until we have enough samples to be statistically meaningful. |
| 148 | + // This avoids creating empty ShreddingSchema objects on every row. |
| 149 | + if (totalRecords < inferrer.getMinSampleSize()) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + ShreddingSchema schema = |
| 154 | + inferrer.infer(variantColumnNames[c], collector.getStatistics(), totalRecords); |
| 155 | + if (!schema.getFields().isEmpty()) { |
| 156 | + triggerSchemaEvolution(schema); |
| 157 | + return; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private void triggerSchemaEvolution(ShreddingSchema schema) { |
| 163 | + if (!schemaTriggered.compareAndSet(false, true)) { |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + String schemaJson = schema.toJson(); |
| 168 | + LOG.info( |
| 169 | + "Triggering Variant shredding schema evolution for table {}: {}", |
| 170 | + tablePath, |
| 171 | + schemaJson); |
| 172 | + |
| 173 | + ApplyShreddingSchemaRequest request = |
| 174 | + new ApplyShreddingSchemaRequest() |
| 175 | + .setTablePath( |
| 176 | + new PbTablePath() |
| 177 | + .setDatabaseName(tablePath.getDatabaseName()) |
| 178 | + .setTableName(tablePath.getTableName())) |
| 179 | + .setShreddingSchemaJson(schemaJson); |
| 180 | + |
| 181 | + rpcCaller |
| 182 | + .apply(request) |
| 183 | + .whenComplete( |
| 184 | + (resp, ex) -> { |
| 185 | + if (ex != null) { |
| 186 | + LOG.warn( |
| 187 | + "Failed to apply Variant shredding schema for table {}, " |
| 188 | + + "will retry on next row: {}", |
| 189 | + tablePath, |
| 190 | + ex.getMessage()); |
| 191 | + schemaTriggered.set(false); |
| 192 | + } else { |
| 193 | + LOG.info( |
| 194 | + "Successfully applied Variant shredding schema for table {}", |
| 195 | + tablePath); |
| 196 | + } |
| 197 | + }); |
| 198 | + } |
| 199 | +} |
0 commit comments