-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use arrow-ipc to communicate between remote server and client (#…
…552) * feat: use arrow-ipc to communicate bewteen remote server and client * add benchmarks * add zstd for ipc * add RemoteEngineVersion * fix message
- Loading branch information
1 parent
abb1bb5
commit 1e6d46b
Showing
15 changed files
with
284 additions
and
30 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0. | ||
|
||
use std::{sync::Arc, time::Instant}; | ||
|
||
use arrow::{ | ||
array::{Int32Array, StringArray}, | ||
datatypes::{DataType, Field, Schema}, | ||
record_batch::RecordBatch, | ||
}; | ||
use arrow_ext::ipc; | ||
use common_util::{avro, time::InstantExt}; | ||
|
||
fn create_batch(rows: usize) -> RecordBatch { | ||
let schema = Schema::new(vec![ | ||
Field::new("a", DataType::Int32, false), | ||
Field::new("b", DataType::Utf8, false), | ||
]); | ||
|
||
let a = Int32Array::from_iter_values(0..rows as i32); | ||
let b = StringArray::from_iter_values((0..rows).map(|i| i.to_string())); | ||
|
||
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a), Arc::new(b)]).unwrap() | ||
} | ||
|
||
pub fn main() { | ||
let arrow_batch = create_batch(102400); | ||
|
||
// Arrow IPC | ||
{ | ||
let batch = common_types::record_batch::RecordBatch::try_from(arrow_batch.clone()).unwrap(); | ||
let compression = ipc::Compression::Zstd; | ||
|
||
let begin = Instant::now(); | ||
let bytes = | ||
ipc::encode_record_batch(&batch.into_arrow_record_batch(), compression).unwrap(); | ||
println!("Arrow IPC encoded size:{}", bytes.len()); | ||
|
||
let decode_batch = ipc::decode_record_batch(bytes, compression).unwrap(); | ||
let _ = common_types::record_batch::RecordBatch::try_from(decode_batch).unwrap(); | ||
|
||
let cost = begin.saturating_elapsed(); | ||
println!("Arrow IPC encode/decode cost:{}ms", cost.as_millis()); | ||
} | ||
|
||
// Avro | ||
{ | ||
let record_schema = | ||
common_types::schema::RecordSchema::try_from(arrow_batch.schema()).unwrap(); | ||
let batch = common_types::record_batch::RecordBatch::try_from(arrow_batch).unwrap(); | ||
|
||
let begin = Instant::now(); | ||
let bytes = avro::record_batch_to_avro_rows(&batch).unwrap(); | ||
println!( | ||
"Avro encoded size:{}", | ||
bytes.iter().map(|row| row.len()).sum::<usize>() | ||
); | ||
|
||
let _decode_batch = avro::avro_rows_to_record_batch(bytes, record_schema); | ||
|
||
let cost = begin.saturating_elapsed(); | ||
println!("Avro encode/decode cost:{}ms", cost.as_millis()); | ||
} | ||
} |
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,36 @@ | ||
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0. | ||
|
||
//! Common type for remote engine rpc service | ||
|
||
use snafu::{Backtrace, Snafu}; | ||
|
||
#[derive(Debug, Snafu)] | ||
pub enum Error { | ||
#[snafu(display("Unknown version, value:{}.\nBacktrace:\n{}", version, backtrace))] | ||
UnknownVersion { version: u32, backtrace: Backtrace }, | ||
} | ||
|
||
type Result<T> = std::result::Result<T, Error>; | ||
|
||
pub enum Version { | ||
ArrowIPCWithZstd, | ||
} | ||
|
||
impl TryFrom<u32> for Version { | ||
type Error = Error; | ||
|
||
fn try_from(version: u32) -> Result<Self> { | ||
match version { | ||
0 => Ok(Self::ArrowIPCWithZstd), | ||
_ => UnknownVersion { version }.fail(), | ||
} | ||
} | ||
} | ||
|
||
impl Version { | ||
pub fn as_u32(&self) -> u32 { | ||
match self { | ||
Self::ArrowIPCWithZstd => 0, | ||
} | ||
} | ||
} |
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,100 @@ | ||
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0. | ||
|
||
//! Utilities for `RecordBatch` serialization using Arrow IPC | ||
|
||
use std::io::Cursor; | ||
|
||
use arrow::{ | ||
ipc::{reader::StreamReader, writer::StreamWriter}, | ||
record_batch::RecordBatch, | ||
}; | ||
use snafu::{Backtrace, OptionExt, ResultExt, Snafu}; | ||
|
||
#[derive(Snafu, Debug)] | ||
#[snafu(visibility(pub))] | ||
pub enum Error { | ||
#[snafu(display("Arror error, err:{}.\nBacktrace:\n{}", source, backtrace))] | ||
ArrowError { | ||
source: arrow::error::ArrowError, | ||
backtrace: Backtrace, | ||
}, | ||
|
||
#[snafu(display("Zstd decode error, err:{}.\nBacktrace:\n{}", source, backtrace))] | ||
ZstdError { | ||
source: std::io::Error, | ||
backtrace: Backtrace, | ||
}, | ||
|
||
#[snafu(display("Failed to decode record batch.\nBacktrace:\n{}", backtrace))] | ||
Decode { backtrace: Backtrace }, | ||
} | ||
|
||
type Result<T> = std::result::Result<T, Error>; | ||
|
||
#[derive(Copy, Clone)] | ||
pub enum Compression { | ||
None, | ||
Zstd, | ||
} | ||
|
||
// https://facebook.github.io/zstd/zstd_manual.html | ||
// The lower the level, the faster the speed (at the cost of compression). | ||
const ZSTD_LEVEL: i32 = 3; | ||
|
||
pub fn encode_record_batch(batch: &RecordBatch, compression: Compression) -> Result<Vec<u8>> { | ||
let buffer: Vec<u8> = Vec::new(); | ||
let mut stream_writer = StreamWriter::try_new(buffer, &batch.schema()).context(ArrowError)?; | ||
stream_writer.write(batch).context(ArrowError)?; | ||
stream_writer | ||
.into_inner() | ||
.context(ArrowError) | ||
.and_then(|bytes| match compression { | ||
Compression::None => Ok(bytes), | ||
Compression::Zstd => { | ||
zstd::stream::encode_all(Cursor::new(bytes), ZSTD_LEVEL).context(ZstdError) | ||
} | ||
}) | ||
} | ||
|
||
pub fn decode_record_batch(bytes: Vec<u8>, compression: Compression) -> Result<RecordBatch> { | ||
let bytes = match compression { | ||
Compression::None => bytes, | ||
Compression::Zstd => zstd::stream::decode_all(Cursor::new(bytes)).context(ZstdError)?, | ||
}; | ||
|
||
let mut stream_reader = StreamReader::try_new(Cursor::new(bytes), None).context(ArrowError)?; | ||
stream_reader.next().context(Decode)?.context(ArrowError) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::sync::Arc; | ||
|
||
use arrow::{ | ||
array::{Int32Array, StringArray}, | ||
datatypes::{DataType, Field, Schema}, | ||
}; | ||
|
||
use super::*; | ||
|
||
fn create_batch(rows: usize) -> RecordBatch { | ||
let schema = Schema::new(vec![ | ||
Field::new("a", DataType::Int32, false), | ||
Field::new("b", DataType::Utf8, false), | ||
]); | ||
|
||
let a = Int32Array::from_iter_values(0..rows as i32); | ||
let b = StringArray::from_iter_values((0..rows).map(|i| i.to_string())); | ||
|
||
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a), Arc::new(b)]).unwrap() | ||
} | ||
|
||
#[test] | ||
fn test_ipc_encode_decode() { | ||
let batch = create_batch(1024); | ||
for compression in &[Compression::None, Compression::Zstd] { | ||
let bytes = encode_record_batch(&batch, *compression).unwrap(); | ||
assert_eq!(batch, decode_record_batch(bytes, *compression).unwrap()); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0. | ||
|
||
pub mod display; | ||
pub mod ipc; | ||
pub mod operation; |
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
Oops, something went wrong.