NoahKusaba commented on code in PR #2638: URL: https://github.com/apache/datafusion-ballista/pull/2638#discussion_r4507339540
########## integrations/iceberg/src/wire.rs: ########## @@ -0,0 +1,181 @@ +// 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. + +//! What the codecs send in place of a live [`Table`], and how they encode it. + +use std::collections::VecDeque; +use std::fmt; +use std::sync::{Arc, LazyLock, Mutex}; + +use datafusion::common::{DataFusionError, exec_datafusion_err}; +use iceberg::io::FileIO; +use iceberg::spec::{TableMetadata, TableMetadataRef}; +use iceberg::table::Table; +use iceberg::{Runtime, TableIdent}; +use iceberg_datafusion::to_datafusion_error; +use serde::de::DeserializeOwned; +use serde::{Deserialize, Serialize}; +use tokio::runtime::{Handle, RuntimeFlavor}; + +/// Runs the codecs' I/O and owns the tables they rebuild: Iceberg plans scans on +/// a table's runtime, which must outlive the table, and a codec's caller may not. +static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new(|| { + tokio::runtime::Builder::new_multi_thread() + .thread_name("ballista-iceberg") + .enable_all() + .build() + .expect("failed to build the ballista-iceberg runtime") +}); + +/// Runs `fut` on [`RUNTIME`] and waits for it, from the synchronous codec API. +pub(crate) fn block_on<T: Send + 'static>( + fut: impl Future<Output = T> + Send + 'static, +) -> Result<T, DataFusionError> { + let wait = || futures::executor::block_on(RUNTIME.spawn(fut)); + let joined = match Handle::try_current().map(|handle| handle.runtime_flavor()) { + Ok(RuntimeFlavor::MultiThread) => tokio::task::block_in_place(wait), + _ => wait(), + }; + joined.map_err(|e| DataFusionError::External(Box::new(e))) +} + +/// Bounds memory rather than tuning hits: a query touches a few table versions, +/// but each can hold megabytes of metadata. +const TABLE_CACHE_CAPACITY: usize = 16; + +/// Tables rebuilt by [`TableWire::load`], least recently used first, so each +/// process rebuilds a version once. Metadata files never change, so entries +/// can't go stale. +static TABLES: LazyLock<Mutex<VecDeque<(TableWire, Table)>>> = + LazyLock::new(|| Mutex::new(VecDeque::new())); + +/// A [`Table`] as its metadata file and `FileIO`, from which any node can +/// rebuild exactly that version without a catalog. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct TableWire { + identifier: TableIdent, + metadata_location: String, + file_io: serde_json::Value, + readonly: bool, + /// The content of `metadata_location`, sent from the client so the Review Comment: Good catch. I went further than a threshold and stopped sending the metadata altogether in 56cd7bd3: the table cache already keeps the scheduler's read to once per version, so it saved little, and a plan's size no longer grows with the table's history. Bit sad this didn't work. -- 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]
