-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dde731
commit 0b16ddf
Showing
19 changed files
with
1,342 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2024 Golem Cloud | ||
// | ||
// Licensed 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 super::Cassandra; | ||
use crate::components::{DOCKER, NETWORK}; | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use testcontainers::{Container, GenericImage, RunnableImage}; | ||
use tracing::info; | ||
|
||
pub struct DockerCassandra { | ||
container: Container<'static, GenericImage>, | ||
keep_container: bool, | ||
valid: AtomicBool, | ||
public_port: u16, | ||
} | ||
|
||
impl DockerCassandra { | ||
const NAME: &'static str = "golem_cassandra"; | ||
|
||
pub fn new(keep_container: bool) -> Self { | ||
let image = GenericImage::new("cassandra", "latest") | ||
.with_exposed_port(super::DEFAULT_PORT) | ||
.with_wait_for(testcontainers::core::WaitFor::message_on_stdout( | ||
"Starting listening for CQL clients on", | ||
)); | ||
let cassandra_image: RunnableImage<_> = RunnableImage::from(image) | ||
.with_container_name(Self::NAME) | ||
.with_network(NETWORK); | ||
|
||
let container = DOCKER.run(cassandra_image); | ||
let public_port: u16 = container.get_host_port_ipv4(super::DEFAULT_PORT); | ||
|
||
DockerCassandra { | ||
container, | ||
keep_container, | ||
valid: AtomicBool::new(true), | ||
public_port, | ||
} | ||
} | ||
} | ||
|
||
impl Cassandra for DockerCassandra { | ||
fn assert_valid(&self) { | ||
if !self.valid.load(Ordering::Acquire) { | ||
std::panic!("Cassandra has been closed") | ||
} | ||
} | ||
|
||
fn private_known_nodes(&self) -> Vec<String> { | ||
vec![format!("{}:{}", Self::NAME, super::DEFAULT_PORT)] | ||
} | ||
|
||
fn kill(&self) { | ||
info!("Stopping Cassandra container"); | ||
if self.keep_container { | ||
self.container.stop() | ||
} else { | ||
self.container.rm() | ||
} | ||
} | ||
|
||
fn public_known_nodes(&self) -> Vec<String> { | ||
vec![format!("localhost:{}", self.public_port)] | ||
} | ||
} | ||
|
||
impl Drop for DockerCassandra { | ||
fn drop(&mut self) { | ||
self.kill() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2024 Golem Cloud | ||
// | ||
// Licensed 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 scylla::{transport::session::PoolSize, Session, SessionBuilder}; | ||
use std::{num::NonZeroUsize, sync::Arc}; | ||
use tonic::async_trait; | ||
|
||
pub mod docker; | ||
|
||
#[async_trait] | ||
pub trait Cassandra { | ||
fn assert_valid(&self); | ||
|
||
fn private_known_nodes(&self) -> Vec<String>; | ||
|
||
fn public_known_nodes(&self) -> Vec<String>; | ||
|
||
fn kill(&self); | ||
|
||
async fn try_get_session(&self, keyspace: Option<&str>) -> Result<Arc<Session>, String> { | ||
let mut session_builder = SessionBuilder::new() | ||
.known_nodes(self.public_known_nodes()) | ||
.pool_size(PoolSize::PerHost(NonZeroUsize::new(10).unwrap())); | ||
|
||
if let Some(keyspace) = keyspace { | ||
session_builder = session_builder.use_keyspace(keyspace, false) | ||
}; | ||
|
||
let session = session_builder | ||
.build() | ||
.await | ||
.map_err(|e| e.to_string()) | ||
.unwrap(); | ||
|
||
Ok(Arc::new(session)) | ||
} | ||
|
||
async fn get_session(&self, keyspace: Option<&str>) -> Arc<Session> { | ||
self.assert_valid(); | ||
self.try_get_session(keyspace).await.unwrap() | ||
} | ||
|
||
async fn flush_keyspace(&self, keyspace: &str) { | ||
let session = self.get_session(Some(keyspace)).await; | ||
session | ||
.query_unpaged(format!("TRUNCATE {}.{};", keyspace, "kv_store"), &[]) | ||
.await | ||
.unwrap(); | ||
session | ||
.query_unpaged(format!("TRUNCATE {}.{};", keyspace, "kv_sets"), &[]) | ||
.await | ||
.unwrap(); | ||
session | ||
.query_unpaged(format!("TRUNCATE {}.{};", keyspace, "kv_sorted_sets"), &[]) | ||
.await | ||
.unwrap(); | ||
} | ||
} | ||
|
||
const DEFAULT_PORT: u16 = 9042; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.