sandugood commented on code in PR #1578: URL: https://github.com/apache/datafusion-ballista/pull/1578#discussion_r3160775784
########## ballista/executor/src/client_pool.rs: ########## @@ -0,0 +1,351 @@ +// 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. + +//! Connection pool for `BallistaClient` instances. +//! +//! `DefaultBallistaClientPool` maintains a `VecDeque`` of idle clients per +//! `(host, port, config)` key backed by a `DashMap`. Callers `BallistaClientPool::acquire` +//! a `PooledClient` guard; when the guard is dropped the underlying client is +//! returned to the idle deque automatically. +//! +//! Connections could be discarded calling `PooledClient::discard` which will result +//! of dropping connection rather than returning it to the pool. This could be +//! used for error handling. +//! +//! A optional background tokio task evicts idle connections that have not been used +//! within the configured `idle_timeout`. + +use async_trait::async_trait; +use ballista_core::client::BallistaClient; +use ballista_core::client_pool::{BallistaClientPool, PooledClient}; +use ballista_core::error::Result; +use ballista_core::extension::BallistaConfigGrpcEndpoint; +use ballista_core::utils::GrpcClientConfig; +use dashmap::DashMap; +use std::collections::VecDeque; +use std::fmt::Debug; +use std::sync::{Arc, Weak}; +use std::time::{Duration, Instant}; + +// --------------------------------------------------------------------------- +// DefaultBallistaClientPool +// --------------------------------------------------------------------------- + +struct IdleEntry { + client: BallistaClient, + idle_since: Instant, +} + +type IdleMap = DashMap<(String, u16, GrpcClientConfig), VecDeque<IdleEntry>>; + +struct Inner { + idle: IdleMap, + idle_timeout: Duration, +} + +/// Default pool implementation. +/// +/// Keeps a `VecDeque<BallistaClient>` per `(host, port, config)`. Idle clients are Review Comment: ```suggestion /// Keeps a `VecDeque<IdleEntry>` per `(host, port, config)`, which is itself a specification of a BallistaClient and duration of its idle status. ``` ########## ballista/executor/src/client_pool.rs: ########## @@ -0,0 +1,351 @@ +// 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. + +//! Connection pool for `BallistaClient` instances. +//! +//! `DefaultBallistaClientPool` maintains a `VecDeque`` of idle clients per +//! `(host, port, config)` key backed by a `DashMap`. Callers `BallistaClientPool::acquire` +//! a `PooledClient` guard; when the guard is dropped the underlying client is +//! returned to the idle deque automatically. +//! +//! Connections could be discarded calling `PooledClient::discard` which will result +//! of dropping connection rather than returning it to the pool. This could be +//! used for error handling. +//! +//! A optional background tokio task evicts idle connections that have not been used +//! within the configured `idle_timeout`. + +use async_trait::async_trait; +use ballista_core::client::BallistaClient; +use ballista_core::client_pool::{BallistaClientPool, PooledClient}; +use ballista_core::error::Result; +use ballista_core::extension::BallistaConfigGrpcEndpoint; +use ballista_core::utils::GrpcClientConfig; +use dashmap::DashMap; +use std::collections::VecDeque; +use std::fmt::Debug; +use std::sync::{Arc, Weak}; +use std::time::{Duration, Instant}; + +// --------------------------------------------------------------------------- +// DefaultBallistaClientPool +// --------------------------------------------------------------------------- + +struct IdleEntry { + client: BallistaClient, + idle_since: Instant, +} + +type IdleMap = DashMap<(String, u16, GrpcClientConfig), VecDeque<IdleEntry>>; + +struct Inner { + idle: IdleMap, + idle_timeout: Duration, +} + +/// Default pool implementation. +/// +/// Keeps a `VecDeque<BallistaClient>` per `(host, port, config)`. Idle clients are +/// evicted by a background tokio task that runs at `idle_timeout / 3` +/// intervals (minimum 15 s). The task exits automatically when the pool `Arc` +/// is dropped. +/// +/// The `DefaultBallistaClientPool` uses the (host, port, config) to identify a connection. +/// Therefore changing connection config might leave pooled connections +/// with older config unused until they expire. + +#[derive(Clone)] +pub struct DefaultBallistaClientPool { + inner: Arc<Inner>, +} + +impl Debug for DefaultBallistaClientPool { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DefaultBallistaClientPool").finish() + } +} + +impl DefaultBallistaClientPool { + /// Create a pool that evicts connections idle longer + /// than defined `idle_timeout`. + pub fn with_eviction_thread(idle_timeout: Duration) -> Self { + Self::new(idle_timeout, true) + } + + /// Create a pool that evicts connections idle longer than `idle_timeout`, + /// if `enable_eviction_thread` is enabled + pub fn new(idle_timeout: Duration, enable_eviction_thread: bool) -> Self { + let inner = Arc::new(Inner { + idle: DashMap::new(), + idle_timeout, + }); + + let weak: Weak<Inner> = Arc::downgrade(&inner); + // TODO: do we limit minimum interval here? Review Comment: Since we add this in the user-facing config: If the user sets the `client_ttl` in the (1, 45) range it will be transformed into 15. Maybe add a warning when user sets this value in the config and it is in the (1, 45) range? -- 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]
