-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'multi-stream-integration-v5' of https://github.com/open…
…observe/openobserve into multi-stream-integration-v5
- Loading branch information
Showing
21 changed files
with
620 additions
and
113 deletions.
There are no files selected for viewing
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
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
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
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,73 @@ | ||
// Copyright 2024 Zinc Labs Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use async_trait::async_trait; | ||
use config::{ | ||
meta::{meta_store::MetaStore, stream::StreamType}, | ||
CONFIG, | ||
}; | ||
use datafusion::arrow::datatypes::Schema; | ||
use once_cell::sync::Lazy; | ||
|
||
use crate::errors::Result; | ||
|
||
pub mod mysql; | ||
pub mod postgres; | ||
pub mod sqlite; | ||
|
||
static CLIENT: Lazy<Box<dyn SchemaHistory>> = Lazy::new(connect); | ||
|
||
pub fn connect() -> Box<dyn SchemaHistory> { | ||
match CONFIG.common.meta_store.as_str().into() { | ||
MetaStore::Sqlite => Box::<sqlite::SqliteSchemaHistory>::default(), | ||
MetaStore::Etcd => Box::<sqlite::SqliteSchemaHistory>::default(), | ||
MetaStore::Nats => Box::<sqlite::SqliteSchemaHistory>::default(), | ||
MetaStore::MySQL => Box::<mysql::MysqlSchemaHistory>::default(), | ||
MetaStore::PostgreSQL => Box::<postgres::PostgresSchemaHistory>::default(), | ||
} | ||
} | ||
|
||
#[async_trait] | ||
pub trait SchemaHistory: Sync + Send + 'static { | ||
async fn create_table(&self) -> Result<()>; | ||
async fn create_table_index(&self) -> Result<()>; | ||
async fn create( | ||
&self, | ||
org_id: &str, | ||
stream_type: StreamType, | ||
stream_name: &str, | ||
start_dt: i64, | ||
schema: Schema, | ||
) -> Result<()>; | ||
} | ||
|
||
pub async fn init() -> Result<()> { | ||
CLIENT.create_table().await?; | ||
CLIENT.create_table_index().await?; | ||
Ok(()) | ||
} | ||
|
||
#[inline] | ||
pub async fn create( | ||
org_id: &str, | ||
stream_type: StreamType, | ||
stream_name: &str, | ||
start_dt: i64, | ||
schema: Schema, | ||
) -> Result<()> { | ||
CLIENT | ||
.create(org_id, stream_type, stream_name, start_dt, schema) | ||
.await | ||
} |
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,135 @@ | ||
// Copyright 2024 Zinc Labs Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use async_trait::async_trait; | ||
use config::{meta::stream::StreamType, utils::json}; | ||
use datafusion::arrow::datatypes::Schema; | ||
|
||
use crate::{ | ||
db::mysql::CLIENT, | ||
errors::{Error, Result}, | ||
}; | ||
|
||
pub struct MysqlSchemaHistory {} | ||
|
||
impl MysqlSchemaHistory { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
impl Default for MysqlSchemaHistory { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl super::SchemaHistory for MysqlSchemaHistory { | ||
async fn create_table(&self) -> Result<()> { | ||
create_table().await | ||
} | ||
|
||
async fn create_table_index(&self) -> Result<()> { | ||
create_table_index().await | ||
} | ||
|
||
async fn create( | ||
&self, | ||
org_id: &str, | ||
stream_type: StreamType, | ||
stream_name: &str, | ||
start_dt: i64, | ||
schema: Schema, | ||
) -> Result<()> { | ||
let value = json::to_string(&schema)?; | ||
let pool = CLIENT.clone(); | ||
match sqlx::query( | ||
r#" | ||
INSERT IGNORE INTO schema_history (org, stream_type, stream_name, start_dt, value) | ||
VALUES (?, ?, ?, ?, ?); | ||
"#, | ||
) | ||
.bind(org_id) | ||
.bind(stream_type.to_string()) | ||
.bind(stream_name) | ||
.bind(start_dt) | ||
.bind(value) | ||
.execute(&pool) | ||
.await | ||
{ | ||
Err(sqlx::Error::Database(e)) => { | ||
if e.is_unique_violation() { | ||
Ok(()) | ||
} else { | ||
Err(Error::Message(e.to_string())) | ||
} | ||
} | ||
Err(e) => Err(e.into()), | ||
Ok(_) => Ok(()), | ||
} | ||
} | ||
} | ||
|
||
pub async fn create_table() -> Result<()> { | ||
let pool = CLIENT.clone(); | ||
sqlx::query( | ||
r#" | ||
CREATE TABLE IF NOT EXISTS schema_history | ||
( | ||
id BIGINT not null primary key AUTO_INCREMENT, | ||
org VARCHAR(100) not null, | ||
stream_type VARCHAR(32) not null, | ||
stream_name VARCHAR(256) not null, | ||
start_dt BIGINT not null, | ||
value LONGTEXT not null | ||
); | ||
"#, | ||
) | ||
.execute(&pool) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn create_table_index() -> Result<()> { | ||
let pool = CLIENT.clone(); | ||
let sqls = vec![ | ||
( | ||
"schema_history", | ||
"CREATE INDEX schema_history_org_idx on schema_history (org);", | ||
), | ||
( | ||
"schema_history", | ||
"CREATE INDEX schema_history_stream_idx on schema_history (org, stream_type, stream_name);", | ||
), | ||
( | ||
"schema_history", | ||
"CREATE UNIQUE INDEX schema_history_stream_version_idx on schema_history (org, stream_type, stream_name, start_dt);", | ||
), | ||
]; | ||
for (table, sql) in sqls { | ||
if let Err(e) = sqlx::query(sql).execute(&pool).await { | ||
if e.to_string().contains("Duplicate key") { | ||
// index already exists | ||
continue; | ||
} | ||
log::error!("[MYSQL] create table {} index error: {}", table, e); | ||
return Err(e.into()); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.