Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ingestion performance #1135

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ pub struct Options {
)]
pub ingestor_endpoint: String,

#[arg(
long,
env = "P_JSON_FLATTEN_DEPTH_LIMIT",
default_value = "3",
help = "Set the depth limit for flattening nested JSON"
)]
pub json_flatten_depth_limit: usize,

#[command(flatten)]
oidc: Option<OidcConfig>,

Expand Down
4 changes: 2 additions & 2 deletions src/event/format/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ impl EventFormat for Event {
};

if value_arr
.iter()
.any(|value| fields_mismatch(&schema, value, schema_version))
.iter()
.any(|value| fields_mismatch(&schema, value, schema_version))
{
return Err(anyhow!(
"Could not process this event due to mismatch in datatype"
Expand Down
7 changes: 2 additions & 5 deletions src/event/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,8 @@ pub trait EventFormat: Sized {
time_partition: Option<&String>,
schema_version: SchemaVersion,
) -> Result<(RecordBatch, bool), AnyError> {
let (data, mut schema, is_first) = self.to_data(
storage_schema,
time_partition,
schema_version,
)?;
let (data, mut schema, is_first) =
self.to_data(storage_schema, time_partition, schema_version)?;

if get_field(&schema, DEFAULT_TIMESTAMP_KEY).is_some() {
return Err(anyhow!(
Expand Down
172 changes: 75 additions & 97 deletions src/handlers/http/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,12 @@ mod tests {
use arrow::datatypes::Int64Type;
use arrow_array::{ArrayRef, Float64Array, Int64Array, ListArray, StringArray};
use arrow_schema::{DataType, Field};
use serde_json::json;
use serde_json::{json, Value};
use std::{collections::HashMap, sync::Arc};

use crate::{
handlers::http::modal::utils::ingest_utils::into_event_batch,
metadata::SchemaVersion,
utils::json::{convert_array_to_object, flatten::convert_to_array},
handlers::http::modal::utils::ingest_utils::into_event_batch, metadata::SchemaVersion,
utils::json::flatten_json_body,
};

trait TestExt {
Expand Down Expand Up @@ -534,21 +533,6 @@ mod tests {
assert_eq!(rb.num_columns(), 1);
}

#[test]
fn non_object_arr_is_err() {
let json = json!([1]);

assert!(convert_array_to_object(
json,
None,
None,
None,
SchemaVersion::V0,
&crate::event::format::LogSource::default()
)
.is_err())
}

#[test]
fn array_into_recordbatch_inffered_schema() {
let json = json!([
Expand Down Expand Up @@ -717,11 +701,11 @@ mod tests {
let json = json!([
{
"a": 1,
"b": "hello",
"b": "hello"
},
{
"a": 1,
"b": "hello",
"b": "hello"
},
{
"a": 1,
Expand All @@ -732,72 +716,66 @@ mod tests {
"a": 1,
"b": "hello",
"c": [{"a": 1, "b": 2}]
},
}
]);
let flattened_json = convert_to_array(
convert_array_to_object(
json,
None,
None,
None,
SchemaVersion::V0,
&crate::event::format::LogSource::default(),
)
.unwrap(),
)
.unwrap();

let (rb, _) = into_event_batch(
flattened_json,
HashMap::default(),
false,
let data = flatten_json_body(
json,
None,
None,
None,
SchemaVersion::V0,
&crate::event::format::LogSource::default(),
3,
)
.unwrap();
assert_eq!(rb.num_rows(), 4);
assert_eq!(rb.num_columns(), 5);
assert_eq!(
rb.column_by_name("a").unwrap().as_int64_arr().unwrap(),
&Int64Array::from(vec![Some(1), Some(1), Some(1), Some(1)])
);
assert_eq!(
rb.column_by_name("b").unwrap().as_utf8_arr().unwrap(),
&StringArray::from(vec![
Some("hello"),
Some("hello"),
Some("hello"),
Some("hello")
])
);

assert_eq!(
rb.column_by_name("c_a")
.unwrap()
.as_any()
.downcast_ref::<ListArray>()
.unwrap(),
&ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
None,
None,
Some(vec![Some(1i64)]),
Some(vec![Some(1)])
])
);

assert_eq!(
rb.column_by_name("c_b")
.unwrap()
.as_any()
.downcast_ref::<ListArray>()
.unwrap(),
&ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
None,
None,
None,
Some(vec![Some(2i64)])
])
);
for value in data {
let (rb, _) =
into_event_batch(value, HashMap::default(), false, None, SchemaVersion::V0)
.unwrap();
assert_eq!(rb.num_rows(), 4);
assert_eq!(rb.num_columns(), 5);
assert_eq!(
rb.column_by_name("a").unwrap().as_int64_arr().unwrap(),
&Int64Array::from(vec![Some(1), Some(1), Some(1), Some(1)])
);
assert_eq!(
rb.column_by_name("b").unwrap().as_utf8_arr().unwrap(),
&StringArray::from(vec![
Some("hello"),
Some("hello"),
Some("hello"),
Some("hello")
])
);

assert_eq!(
rb.column_by_name("c_a")
.unwrap()
.as_any()
.downcast_ref::<ListArray>()
.unwrap(),
&ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
None,
None,
Some(vec![Some(1i64)]),
Some(vec![Some(1)])
])
);

assert_eq!(
rb.column_by_name("c_b")
.unwrap()
.as_any()
.downcast_ref::<ListArray>()
.unwrap(),
&ListArray::from_iter_primitive::<Int64Type, _, _>(vec![
None,
None,
None,
Some(vec![Some(2i64)])
])
);
}
}

#[test]
Expand All @@ -822,52 +800,52 @@ mod tests {
"c": [{"a": 1, "b": 2}]
},
]);
let flattened_json = convert_to_array(
convert_array_to_object(
json,
None,
None,
None,
SchemaVersion::V1,
&crate::event::format::LogSource::default(),
)
.unwrap(),
let flattened_json = flatten_json_body(
json,
None,
None,
None,
SchemaVersion::V1,
&crate::event::format::LogSource::default(),
3,
)
.unwrap();
let arr_flattened_json = Value::Array(flattened_json);

let (rb, _) = into_event_batch(
flattened_json,
arr_flattened_json,
HashMap::default(),
false,
None,
SchemaVersion::V1,
)
.unwrap();

assert_eq!(rb.num_rows(), 4);
assert_eq!(rb.num_rows(), 5);
assert_eq!(rb.num_columns(), 5);
assert_eq!(
rb.column_by_name("a").unwrap().as_float64_arr().unwrap(),
&Float64Array::from(vec![Some(1.0), Some(1.0), Some(1.0), Some(1.0)])
&Float64Array::from(vec![Some(1.0), Some(1.0), Some(1.0), Some(1.0), Some(1.0)])
);
assert_eq!(
rb.column_by_name("b").unwrap().as_utf8_arr().unwrap(),
&StringArray::from(vec![
Some("hello"),
Some("hello"),
Some("hello"),
Some("hello"),
Some("hello")
])
);

assert_eq!(
rb.column_by_name("c_a").unwrap().as_float64_arr().unwrap(),
&Float64Array::from(vec![None, None, Some(1.0), Some(1.0)])
&Float64Array::from(vec![None, None, Some(1.0), Some(1.0), None])
);

assert_eq!(
rb.column_by_name("c_b").unwrap().as_float64_arr().unwrap(),
&Float64Array::from(vec![None, None, None, Some(2.0)])
&Float64Array::from(vec![None, None, None, None, Some(2.0)])
);
}
}
2 changes: 1 addition & 1 deletion src/handlers/http/modal/ingest_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl IngestServer {
web::put()
.to(ingestor_logstream::put_stream)
.authorize_for_stream(Action::CreateStream),
)
),
)
.service(
// GET "/logstream/{logstream}/info" ==> Get info for given log stream
Expand Down
19 changes: 11 additions & 8 deletions src/handlers/http/modal/utils/ingest_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ use crate::{
kinesis::{flatten_kinesis_logs, Message},
},
metadata::{SchemaVersion, STREAM_INFO},
option::CONFIG,
storage::StreamType,
utils::json::{convert_array_to_object, flatten::convert_to_array},
utils::json::flatten_json_body,
};

pub async fn flatten_and_push_logs(
Expand Down Expand Up @@ -69,25 +70,27 @@ pub async fn push_logs(
let static_schema_flag = STREAM_INFO.get_static_schema_flag(stream_name)?;
let custom_partition = STREAM_INFO.get_custom_partition(stream_name)?;
let schema_version = STREAM_INFO.get_schema_version(stream_name)?;

let json_flatten_depth_limit = CONFIG.options.json_flatten_depth_limit;
let data = if time_partition.is_some() || custom_partition.is_some() {
convert_array_to_object(
flatten_json_body(
json,
time_partition.as_ref(),
time_partition_limit,
custom_partition.as_ref(),
schema_version,
log_source,
json_flatten_depth_limit,
)?
} else {
vec![convert_to_array(convert_array_to_object(
vec![Value::Array(flatten_json_body(
json,
None,
None,
None,
time_partition.as_ref(),
time_partition_limit,
custom_partition.as_ref(),
schema_version,
log_source,
)?)?]
json_flatten_depth_limit,
)?)]
};

for value in data {
Expand Down
Loading
Loading