luoyuxia commented on code in PR #103: URL: https://github.com/apache/fluss-rust/pull/103#discussion_r2637519758
########## crates/fluss/src/client/table/log_fetch_buffer.rs: ########## @@ -0,0 +1,367 @@ +// 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. + +use crate::error::Result; +use crate::metadata::TableBucket; +use crate::record::{ + LogRecordBatch, LogRecordIterator, LogRecordsBatches, ReadContext, ScanRecord, +}; +use parking_lot::Mutex; +use std::collections::{HashMap, VecDeque}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::Duration; +use tokio::sync::Notify; + +/// Represents a completed fetch that can be consumed +pub trait CompletedFetch: Send + Sync { + fn table_bucket(&self) -> &TableBucket; + fn fetch_records(&mut self, max_records: usize) -> Result<Vec<ScanRecord>>; + fn is_consumed(&self) -> bool; + fn drain(&mut self); + fn size_in_bytes(&self) -> usize; + fn high_watermark(&self) -> i64; + fn is_initialized(&self) -> bool; + fn set_initialized(&mut self); + fn next_fetch_offset(&self) -> i64; +} + +/// Represents a pending fetch that is waiting to be completed +pub trait PendingFetch: Send + Sync { + fn table_bucket(&self) -> &TableBucket; + fn is_completed(&self) -> bool; + fn to_completed_fetch(self: Box<Self>) -> Result<Box<dyn CompletedFetch>>; +} + +/// Thread-safe buffer for completed fetches +pub struct LogFetchBuffer { + completed_fetches: Mutex<VecDeque<Box<dyn CompletedFetch>>>, + pending_fetches: Mutex<HashMap<TableBucket, VecDeque<Box<dyn PendingFetch>>>>, + next_in_line_fetch: Mutex<Option<Box<dyn CompletedFetch>>>, + not_empty_notify: Notify, + woken_up: Arc<AtomicBool>, +} + +impl LogFetchBuffer { + pub fn new() -> Self { + Self { + completed_fetches: Mutex::new(VecDeque::new()), + pending_fetches: Mutex::new(HashMap::new()), + next_in_line_fetch: Mutex::new(None), + not_empty_notify: Notify::new(), + woken_up: Arc::new(AtomicBool::new(false)), + } + } + + /// Check if the buffer is empty + pub fn is_empty(&self) -> bool { + self.completed_fetches.lock().is_empty() + } + + /// Wait for the buffer to become non-empty, with timeout + /// Returns true if data became available, false if timeout + pub async fn await_not_empty(&self, timeout: Duration) -> bool { + let deadline = std::time::Instant::now() + timeout; + + loop { + // Check if buffer is not empty + if !self.is_empty() { Review Comment: I think we can keep it simple now -- 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]
