Skip to content

Commit

Permalink
fix: switch version restart all node
Browse files Browse the repository at this point in the history
  • Loading branch information
discord9 committed Dec 10, 2024
1 parent a7d531b commit 462dd4e
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 15 deletions.
83 changes: 68 additions & 15 deletions tests/runner/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ impl Env {

let mut greptimedb = self.connect_db(&Default::default()).await;

greptimedb.metasrv_process = Some(meta_server);
greptimedb.metasrv_process = Some(meta_server).into();
greptimedb.server_processes = Some(Arc::new(Mutex::new(vec![
datanode_1, datanode_2, datanode_3,
])));
greptimedb.frontend_process = Some(frontend);
greptimedb.flownode_process = Some(flownode);
greptimedb.frontend_process = Some(frontend).into();
greptimedb.flownode_process = Some(flownode).into();
greptimedb.is_standalone = false;
greptimedb.ctx = db_ctx;

Expand Down Expand Up @@ -245,9 +245,9 @@ impl Env {
pg_client: TokioMutex::new(pg_client),
mysql_client: TokioMutex::new(mysql_client),
server_processes: None,
metasrv_process: None,
frontend_process: None,
flownode_process: None,
metasrv_process: None.into(),
frontend_process: None.into(),
flownode_process: None.into(),
ctx: GreptimeDBContext {
time: 0,
datanode_id: Default::default(),
Expand Down Expand Up @@ -460,14 +460,31 @@ impl Env {
}

/// stop and restart the server process
async fn restart_server(&self, db: &GreptimeDB) {
async fn restart_server(&self, db: &GreptimeDB, is_full_restart: bool) {
{
if let Some(server_process) = db.server_processes.clone() {
let mut server_processes = server_process.lock().unwrap();
for server_process in server_processes.iter_mut() {
Env::stop_server(server_process);
}
}
if is_full_restart {
if let Some(mut metasrv_process) =
db.metasrv_process.lock().expect("poisoned lock").take()
{
Env::stop_server(&mut metasrv_process);
}
if let Some(mut frontend_process) =
db.frontend_process.lock().expect("poisoned lock").take()
{
Env::stop_server(&mut frontend_process);
}
if let Some(mut flownode_process) =
db.flownode_process.lock().expect("poisoned lock").take()
{
Env::stop_server(&mut flownode_process);
}
}
}

// check if the server is distributed or standalone
Expand All @@ -476,12 +493,33 @@ impl Env {
vec![new_server_process]
} else {
db.ctx.reset_datanode_id();
if is_full_restart {
let metasrv = self.start_server("metasrv", &db.ctx, false).await;
db.metasrv_process
.lock()
.expect("lock poisoned")
.replace(metasrv);
}

let mut processes = vec![];
for _ in 0..3 {
let new_server_process = self.start_server("datanode", &db.ctx, false).await;
processes.push(new_server_process);
}

if is_full_restart {
let frontend = self.start_server("frontend", &db.ctx, false).await;
db.frontend_process
.lock()
.expect("lock poisoned")
.replace(frontend);

let flownode = self.start_server("flownode", &db.ctx, false).await;
db.flownode_process
.lock()
.expect("lock poisoned")
.replace(flownode);
}
processes
};

Expand Down Expand Up @@ -588,9 +626,9 @@ impl Env {

pub struct GreptimeDB {
server_processes: Option<Arc<Mutex<Vec<Child>>>>,
metasrv_process: Option<Child>,
frontend_process: Option<Child>,
flownode_process: Option<Child>,
metasrv_process: Mutex<Option<Child>>,
frontend_process: Mutex<Option<Child>>,
flownode_process: Mutex<Option<Child>>,
grpc_client: TokioMutex<DB>,
pg_client: TokioMutex<PgClient>,
mysql_client: TokioMutex<MySqlClient>,
Expand Down Expand Up @@ -701,7 +739,7 @@ impl GreptimeDB {
impl Database for GreptimeDB {
async fn query(&self, ctx: QueryContext, query: String) -> Box<dyn Display> {
if ctx.context.contains_key("restart") && self.env.server_addrs.server_addr.is_none() {
self.env.restart_server(self).await;
self.env.restart_server(self, false).await;
} else if let Some(version) = ctx.context.get("version") {
if self.env.old_bins_dir.lock().unwrap().is_none() {
// save old bins dir
Expand All @@ -718,7 +756,7 @@ impl Database for GreptimeDB {
*self.env.bins_dir.lock().unwrap() = Some(new_path);
}

self.env.restart_server(self).await;
self.env.restart_server(self, true).await;
}

if let Some(protocol) = ctx.context.get(PROTOCOL_KEY) {
Expand Down Expand Up @@ -746,15 +784,30 @@ impl GreptimeDB {
);
}
}
if let Some(mut metasrv) = self.metasrv_process.take() {
if let Some(mut metasrv) = self
.metasrv_process
.lock()
.expect("someone else panic when holding lock")
.take()
{
Env::stop_server(&mut metasrv);
println!("Metasrv (pid = {}) is stopped", metasrv.id());
}
if let Some(mut frontend) = self.frontend_process.take() {
if let Some(mut frontend) = self
.frontend_process
.lock()
.expect("someone else panic when holding lock")
.take()
{
Env::stop_server(&mut frontend);
println!("Frontend (pid = {}) is stopped", frontend.id());
}
if let Some(mut flownode) = self.flownode_process.take() {
if let Some(mut flownode) = self
.flownode_process
.lock()
.expect("someone else panic when holding lock")
.take()
{
Env::stop_server(&mut flownode);
println!("Flownode (pid = {}) is stopped", flownode.id());
}
Expand Down
153 changes: 153 additions & 0 deletions tests/upgrade-compat/standalone/common/test_ttl.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
-- SQLNESS ARG version=v0.9.5
CREATE TABLE test_ttl_0s(ts TIMESTAMP TIME INDEX, val INT) WITH (ttl = '0 second');

Affected Rows: 0

CREATE TABLE test_ttl_1s(ts TIMESTAMP TIME INDEX, val INT) WITH (ttl = '1 second');

Affected Rows: 0

CREATE TABLE test_ttl_none(ts TIMESTAMP TIME INDEX, val INT);

Affected Rows: 0

CREATE DATABASE ttl_db_1s WITH (ttl = '1 second');

Affected Rows: 1

CREATE DATABASE ttl_db_0s WITH (ttl = '0 second');

Affected Rows: 1

CREATE DATABASE ttl_db_none;

Affected Rows: 1

-- SQLNESS ARG version=latest
SHOW TABLES;

+---------------+
| Tables |
+---------------+
| numbers |
| test_ttl_0s |
| test_ttl_1s |
| test_ttl_none |
+---------------+

SHOW CREATE TABLE test_ttl_1s;

+-------------+--------------------------------------------+
| Table | Create Table |
+-------------+--------------------------------------------+
| test_ttl_1s | CREATE TABLE IF NOT EXISTS "test_ttl_1s" ( |
| | "ts" TIMESTAMP(3) NOT NULL, |
| | "val" INT NULL, |
| | TIME INDEX ("ts") |
| | ) |
| | |
| | ENGINE=mito |
| | WITH( |
| | ttl = '1s' |
| | ) |
+-------------+--------------------------------------------+

SHOW CREATE TABLE test_ttl_0s;

+-------------+--------------------------------------------+
| Table | Create Table |
+-------------+--------------------------------------------+
| test_ttl_0s | CREATE TABLE IF NOT EXISTS "test_ttl_0s" ( |
| | "ts" TIMESTAMP(3) NOT NULL, |
| | "val" INT NULL, |
| | TIME INDEX ("ts") |
| | ) |
| | |
| | ENGINE=mito |
| | WITH( |
| | ttl = '0s' |
| | ) |
+-------------+--------------------------------------------+

SHOW CREATE TABLE test_ttl_none;

+---------------+----------------------------------------------+
| Table | Create Table |
+---------------+----------------------------------------------+
| test_ttl_none | CREATE TABLE IF NOT EXISTS "test_ttl_none" ( |
| | "ts" TIMESTAMP(3) NOT NULL, |
| | "val" INT NULL, |
| | TIME INDEX ("ts") |
| | ) |
| | |
| | ENGINE=mito |
| | |
+---------------+----------------------------------------------+

DROP TABLE test_ttl_1s;

Affected Rows: 0

DROP TABLE test_ttl_0s;

Affected Rows: 0

DROP TABLE test_ttl_none;

Affected Rows: 0

SHOW DATABASES;

+--------------------+
| Database |
+--------------------+
| greptime_private |
| information_schema |
| public |
| ttl_db_0s |
| ttl_db_1s |
| ttl_db_none |
+--------------------+

SHOW CREATE DATABASE ttl_db_1s;

+-----------+-----------------------------------------+
| Database | Create Database |
+-----------+-----------------------------------------+
| ttl_db_1s | CREATE DATABASE IF NOT EXISTS ttl_db_1s |
| | WITH( |
| | ttl = '1s' |
| | ) |
+-----------+-----------------------------------------+

SHOW CREATE DATABASE ttl_db_0s;

+-----------+-----------------------------------------+
| Database | Create Database |
+-----------+-----------------------------------------+
| ttl_db_0s | CREATE DATABASE IF NOT EXISTS ttl_db_0s |
| | WITH( |
| | ttl = '0s' |
| | ) |
+-----------+-----------------------------------------+

SHOW CREATE DATABASE ttl_db_none;

+-------------+-------------------------------------------+
| Database | Create Database |
+-------------+-------------------------------------------+
| ttl_db_none | CREATE DATABASE IF NOT EXISTS ttl_db_none |
+-------------+-------------------------------------------+

DROP DATABASE ttl_db_1s;

Affected Rows: 0

DROP DATABASE ttl_db_0s;

Affected Rows: 0

DROP DATABASE ttl_db_none;

Affected Rows: 0

42 changes: 42 additions & 0 deletions tests/upgrade-compat/standalone/common/test_ttl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

-- SQLNESS ARG version=v0.9.5
CREATE TABLE test_ttl_0s(ts TIMESTAMP TIME INDEX, val INT) WITH (ttl = '0 second');

CREATE TABLE test_ttl_1s(ts TIMESTAMP TIME INDEX, val INT) WITH (ttl = '1 second');

CREATE TABLE test_ttl_none(ts TIMESTAMP TIME INDEX, val INT);

CREATE DATABASE ttl_db_1s WITH (ttl = '1 second');

CREATE DATABASE ttl_db_0s WITH (ttl = '0 second');

CREATE DATABASE ttl_db_none;

-- SQLNESS ARG version=latest
SHOW TABLES;

SHOW CREATE TABLE test_ttl_1s;

SHOW CREATE TABLE test_ttl_0s;

SHOW CREATE TABLE test_ttl_none;

DROP TABLE test_ttl_1s;

DROP TABLE test_ttl_0s;

DROP TABLE test_ttl_none;

SHOW DATABASES;

SHOW CREATE DATABASE ttl_db_1s;

SHOW CREATE DATABASE ttl_db_0s;

SHOW CREATE DATABASE ttl_db_none;

DROP DATABASE ttl_db_1s;

DROP DATABASE ttl_db_0s;

DROP DATABASE ttl_db_none;

0 comments on commit 462dd4e

Please sign in to comment.