gengliangwang commented on code in PR #59520: URL: https://github.com/apache/spark/pull/59520#discussion_r4519081556
########## sql/native/rust/src/write.rs: ########## @@ -0,0 +1,290 @@ +// 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. + +//! Driver write coordination and task-local writers restored from serialized state, for batch +//! writes and for the writes of micro-batches. + +use std::ffi::c_void; + +use arrow_array::{ffi, Array, RecordBatch, StructArray}; +use arrow_schema::{DataType, SchemaRef}; +use serde::Serialize; + +use crate::abi::{self, ArrowArray, SparkDsTaskContext}; +use crate::bridge::stream::validate_schema; +use crate::bridge::{decode, describe, drop_provider, encode, provider_call}; +use crate::{ + BatchWrite, DataWriter, DataWriterFactory, PartitionWriter, Result, StreamingWrite, + TaskContext, WriterContext, +}; + +/// A batch write, which a driver writer drives as a streaming write whose epoch ID is always +/// -1 and is ignored. +pub struct Batch<W>(pub W); + +impl<W: BatchWrite> StreamingWrite for Batch<W> { + type Factory = W::Factory; + type Message = W::Message; + + fn writer_factory(&self) -> Result<W::Factory> { + self.0.writer_factory() + } + + fn commit(&mut self, _: i64, messages: Vec<W::Message>) -> Result<()> { + self.0.commit(messages) + } + + fn abort(&mut self, _: i64, messages: Vec<Option<W::Message>>) -> Result<()> { + self.0.abort(messages) + } +} + +/// A driver writer that decodes its own commit messages, so that one handle type holds the +/// driver writers of batch writes and of the writes of micro-batches, which share the writer +/// handles of the ABI. +pub trait DriverWrite: Send { + fn commit(&mut self, epoch_id: i64, messages: &[Option<&[u8]>]) -> Result<()>; + + fn abort(&mut self, epoch_id: i64, messages: &[Option<&[u8]>]) -> Result<()>; +} + +/// The driver keeps this coordinator until a successful commit or an abort, after which Spark Review Comment: Thanks, confirmed: `NativeDataSourceWriter` closes its handle after a successful commit and in a `finally` of abort, but `NativeDataSourceStreamWriter` leaves it to `NativeStreamWriters.register`, which runs after `create_writer` of the next micro-batch. SPARK-61284 (#60262) closes the stream writer as the batch writer does, and updates the lifecycle in spark_datasource.h and the docs. Until it is in, the early drop of fdf0cb1c9ddf covers the connectors of this SDK; once it is, `DriverWriter` can drop the coordinator at close again, and the C++ SDK needs no change. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
