You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/03/04 23:04:34 UTC

[GitHub] [arrow-datafusion] yahoNanJing commented on a change in pull request #1912: Refactor the event channel

yahoNanJing commented on a change in pull request #1912:
URL: https://github.com/apache/arrow-datafusion/pull/1912#discussion_r819974010



##########
File path: ballista/rust/core/src/event_loop.rs
##########
@@ -0,0 +1,128 @@
+// 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 std::sync::atomic::{AtomicBool, Ordering};
+use std::sync::Arc;
+
+use async_trait::async_trait;
+use log::{error, info, warn};
+use tokio::sync::mpsc;
+
+use crate::error::{BallistaError, Result};
+
+#[async_trait]
+pub trait EventAction<E>: Send + Sync {
+    fn on_start(&self);
+
+    fn on_stop(&self);
+
+    async fn on_receive(&self, event: E) -> Result<Option<E>>;
+
+    fn on_error(&self, error: BallistaError);
+}
+
+pub struct EventLoop<E> {
+    name: String,
+    stopped: Arc<AtomicBool>,
+    buffer_size: usize,
+    action: Arc<dyn EventAction<E>>,
+    tx_event: Option<mpsc::Sender<E>>,
+}
+
+impl<E: Send + 'static> EventLoop<E> {
+    pub fn new(
+        name: String,
+        buffer_size: usize,
+        action: Arc<dyn EventAction<E>>,
+    ) -> Self {
+        Self {
+            name,
+            stopped: Arc::new(AtomicBool::new(false)),
+            buffer_size,
+            action,
+            tx_event: None,
+        }
+    }
+
+    fn run(&self, mut rx_event: mpsc::Receiver<E>) {
+        assert!(
+            self.tx_event.is_some(),
+            "The event sender should be initialized first!"
+        );
+        let tx_event = self.tx_event.as_ref().unwrap().clone();
+        let name = self.name.clone();
+        let stopped = self.stopped.clone();
+        let action = self.action.clone();
+        tokio::spawn(async move {
+            info!("Starting the event loop {}", name);
+            while !stopped.load(Ordering::SeqCst) {
+                let event = rx_event.recv().await.unwrap();

Review comment:
       Yes. In general, the channel won't be closed until the scheduler is down. The dangling task should be handled by another way, like speculative task execution.




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org