|
| 1 | +/* |
| 2 | + * Copyright (2025) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.delta.spark.internal.v2.utils; |
| 17 | + |
| 18 | +import io.delta.kernel.Scan; |
| 19 | +import io.delta.kernel.defaults.engine.DefaultEngine; |
| 20 | +import io.delta.kernel.engine.Engine; |
| 21 | +import io.delta.kernel.internal.ScanImpl; |
| 22 | +import io.delta.kernel.internal.SnapshotImpl; |
| 23 | +import io.delta.kernel.internal.actions.Metadata; |
| 24 | +import io.delta.kernel.internal.actions.Protocol; |
| 25 | +import io.delta.kernel.internal.checksum.CRCInfo; |
| 26 | +import io.delta.kernel.internal.lang.Lazy; |
| 27 | +import io.delta.kernel.internal.metrics.SnapshotQueryContext; |
| 28 | +import io.delta.kernel.internal.metrics.SnapshotReportImpl; |
| 29 | +import io.delta.kernel.internal.replay.LogReplay; |
| 30 | +import io.delta.kernel.internal.snapshot.LogSegment; |
| 31 | +import io.delta.kernel.metrics.SnapshotReport; |
| 32 | +import java.io.Serializable; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Optional; |
| 35 | +import org.apache.hadoop.conf.Configuration; |
| 36 | +import org.apache.spark.util.SerializableConfiguration; |
| 37 | + |
| 38 | +/** |
| 39 | + * Serializable carrier for a Delta snapshot's state. Created on the driver from an existing {@link |
| 40 | + * SnapshotImpl} (zero I/O), and reconstructed on the executor as a read-only {@link Scan} via |
| 41 | + * {@link #toScan(Configuration)}. The returned {@code Scan} interface exposes only read operations, |
| 42 | + * preventing accidental misuse of write-path APIs (e.g. {@code Committer}). |
| 43 | + */ |
| 44 | +public class SerializableReadOnlySnapshot implements Serializable { |
| 45 | + |
| 46 | + private static final long serialVersionUID = 1L; |
| 47 | + |
| 48 | + private final String dataPath; |
| 49 | + private final String logPath; |
| 50 | + private final long version; |
| 51 | + |
| 52 | + private final ArrayList<SerializableFileStatus> deltas; |
| 53 | + private final ArrayList<SerializableFileStatus> compactions; |
| 54 | + private final ArrayList<SerializableFileStatus> checkpoints; |
| 55 | + private final SerializableFileStatus deltaAtEndVersion; |
| 56 | + private final SerializableFileStatus lastSeenChecksum; // nullable |
| 57 | + private final Long maxPublishedDeltaVersion; // nullable |
| 58 | + |
| 59 | + private final Protocol protocol; |
| 60 | + private final Metadata metadata; |
| 61 | + private final SerializableConfiguration hadoopConf; |
| 62 | + |
| 63 | + private SerializableReadOnlySnapshot( |
| 64 | + String dataPath, |
| 65 | + String logPath, |
| 66 | + long version, |
| 67 | + ArrayList<SerializableFileStatus> deltas, |
| 68 | + ArrayList<SerializableFileStatus> compactions, |
| 69 | + ArrayList<SerializableFileStatus> checkpoints, |
| 70 | + SerializableFileStatus deltaAtEndVersion, |
| 71 | + SerializableFileStatus lastSeenChecksum, |
| 72 | + Long maxPublishedDeltaVersion, |
| 73 | + Protocol protocol, |
| 74 | + Metadata metadata, |
| 75 | + SerializableConfiguration hadoopConf) { |
| 76 | + this.dataPath = dataPath; |
| 77 | + this.logPath = logPath; |
| 78 | + this.version = version; |
| 79 | + this.deltas = deltas; |
| 80 | + this.compactions = compactions; |
| 81 | + this.checkpoints = checkpoints; |
| 82 | + this.deltaAtEndVersion = deltaAtEndVersion; |
| 83 | + this.lastSeenChecksum = lastSeenChecksum; |
| 84 | + this.maxPublishedDeltaVersion = maxPublishedDeltaVersion; |
| 85 | + this.protocol = protocol; |
| 86 | + this.metadata = metadata; |
| 87 | + this.hadoopConf = hadoopConf; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Driver-side: extract the snapshot state from an existing Kernel {@link SnapshotImpl}. This |
| 92 | + * performs zero I/O — all data is already in memory on the driver. |
| 93 | + */ |
| 94 | + public static SerializableReadOnlySnapshot fromSnapshot( |
| 95 | + SnapshotImpl snapshot, Configuration hadoopConf) { |
| 96 | + LogSegment logSegment = snapshot.getLogSegment(); |
| 97 | + return new SerializableReadOnlySnapshot( |
| 98 | + snapshot.getDataPath().toString(), |
| 99 | + logSegment.getLogPath().toString(), |
| 100 | + snapshot.getVersion(null /* engine, unused for SnapshotImpl */), |
| 101 | + new ArrayList<>(SerializableFileStatus.fromList(logSegment.getDeltas())), |
| 102 | + new ArrayList<>(SerializableFileStatus.fromList(logSegment.getCompactions())), |
| 103 | + new ArrayList<>(SerializableFileStatus.fromList(logSegment.getCheckpoints())), |
| 104 | + SerializableFileStatus.from(logSegment.getDeltaFileAtEndVersion()), |
| 105 | + logSegment.getLastSeenChecksum().map(SerializableFileStatus::from).orElse(null), |
| 106 | + logSegment.getMaxPublishedDeltaVersion().orElse(null), |
| 107 | + snapshot.getProtocol(), |
| 108 | + snapshot.getMetadata(), |
| 109 | + new SerializableConfiguration(hadoopConf)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Executor-side: reconstruct a read-only {@link Scan} from the serialized snapshot state. The |
| 114 | + * returned {@code Scan} only exposes {@code getScanFiles()} — no write-path or commit APIs. |
| 115 | + */ |
| 116 | + public Scan toScan(Configuration hadoopConfOverride) { |
| 117 | + return buildScan(hadoopConfOverride); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Executor-side: reconstruct using the serialized Hadoop configuration. Convenience overload when |
| 122 | + * no conf override is needed. |
| 123 | + */ |
| 124 | + public Scan toScan() { |
| 125 | + return buildScan(hadoopConf.value()); |
| 126 | + } |
| 127 | + |
| 128 | + public long getVersion() { |
| 129 | + return version; |
| 130 | + } |
| 131 | + |
| 132 | + /** Returns the serialized Hadoop configuration for creating an Engine on the executor. */ |
| 133 | + public Configuration getHadoopConf() { |
| 134 | + return hadoopConf.value(); |
| 135 | + } |
| 136 | + |
| 137 | + // ---- internal reconstruction ---- |
| 138 | + |
| 139 | + private Scan buildScan(Configuration conf) { |
| 140 | + Engine engine = DefaultEngine.create(conf); |
| 141 | + io.delta.kernel.utils.Path kernelDataPath = new io.delta.kernel.utils.Path(dataPath); |
| 142 | + io.delta.kernel.utils.Path kernelLogPath = new io.delta.kernel.utils.Path(logPath); |
| 143 | + |
| 144 | + LogSegment logSegment = |
| 145 | + new LogSegment( |
| 146 | + kernelLogPath, |
| 147 | + version, |
| 148 | + SerializableFileStatus.toFileStatusList(deltas), |
| 149 | + SerializableFileStatus.toFileStatusList(compactions), |
| 150 | + SerializableFileStatus.toFileStatusList(checkpoints), |
| 151 | + deltaAtEndVersion.toFileStatus(), |
| 152 | + Optional.ofNullable(lastSeenChecksum).map(SerializableFileStatus::toFileStatus), |
| 153 | + Optional.ofNullable(maxPublishedDeltaVersion)); |
| 154 | + |
| 155 | + Lazy<LogSegment> lazyLogSegment = new Lazy<>(() -> logSegment); |
| 156 | + Lazy<Optional<CRCInfo>> lazyCrcInfo = new Lazy<>(Optional::empty); |
| 157 | + |
| 158 | + LogReplay logReplay = new LogReplay(engine, kernelDataPath, lazyLogSegment, lazyCrcInfo); |
| 159 | + |
| 160 | + SnapshotQueryContext queryContext = SnapshotQueryContext.forVersionSnapshot(dataPath, version); |
| 161 | + queryContext.setResolvedVersion(version); |
| 162 | + SnapshotReport snapshotReport = SnapshotReportImpl.forSuccess(queryContext); |
| 163 | + |
| 164 | + return new ScanImpl( |
| 165 | + metadata.getSchema(), |
| 166 | + metadata.getSchema(), |
| 167 | + protocol, |
| 168 | + metadata, |
| 169 | + logReplay, |
| 170 | + Optional.empty(), |
| 171 | + kernelDataPath, |
| 172 | + snapshotReport); |
| 173 | + } |
| 174 | +} |
0 commit comments