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

br-stream: don't clone the key & value in encode_event() #4

Merged
merged 2 commits into from
Jan 5, 2022
Merged
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
22 changes: 11 additions & 11 deletions components/br-stream/src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.

use std::convert::AsRef;
use std::fmt;
use std::path::PathBuf;

Expand All @@ -18,7 +19,7 @@ use raftstore::coprocessor::CmdBatch;
use tikv::config::BackupStreamConfig;

use tikv_util::worker::{Runnable, Scheduler};
use tikv_util::{debug, error, info};
use tikv_util::{debug, error, info, Either};

use super::metrics::{HANDLE_EVENT_DURATION_HISTOGRAM, HANDLE_KV_HISTOGRAM};

Expand Down Expand Up @@ -137,17 +138,16 @@ where
}
}
}
// TODO use a more efficent encode kv event format
// TODO move this function to a indepentent module.
pub fn encode_event(key: &[u8], value: &[u8]) -> Vec<u8> {
let mut buf = vec![];
let key_len = (key.len() as u32).to_ne_bytes();
let val_len = value.len().to_ne_bytes();
buf.extend_from_slice(&key_len);
buf.extend_from_slice(key);
buf.extend_from_slice(&val_len);
buf.extend_from_slice(value);
buf
pub fn encode_event<'e>(key: &'e [u8], value: &'e [u8]) -> [impl AsRef<[u8]> + 'e; 4] {
Copy link
Collaborator

@YuJuncen YuJuncen Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(BTW, I would prefer make it something like encode_event_to(w: impl Write, key: &[u8], value: &[u8]) -> Result<usize>, the current version is a little... uh... magical...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YuJuncen the problem is, the w isn't a simple impl Write but an AsyncWrite plus a SHA-256 sink. (why are we using async to write to local temp file 😂)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the w isn't a simple impl Write but an AsyncWrite plus a SHA-256 sink

I guess we can make something like Sha256Io<T>, which likes Sha256Reader but impls Write / Read / AsyncRead / AsyncWrite when the inner type implements corresponding traits. 🤔 (Probably not for this PR)

why are we using async to write to local temp file 😂

Just PTSD of Sync I/O in nodejs. 😂

let key_len = (key.len() as u32).to_le_bytes();
let val_len = (value.len() as u32).to_le_bytes();
[
Either::Left(key_len),
Either::Right(key),
Either::Left(val_len),
Either::Right(value),
]
}

fn backup_batch(&self, batch: CmdBatch) {
Expand Down
14 changes: 9 additions & 5 deletions components/br-stream/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,16 +539,20 @@ impl DataFile {
/// Add a new KV pair to the file, returning its size.
async fn on_event(&mut self, mut kv: ApplyEvent) -> Result<usize> {
let encoded = super::endpoint::Endpoint::<EtcdStore>::encode_event(&kv.key, &kv.value);
let size = encoded.len();
let mut size = 0;
for slice in encoded {
let slice = slice.as_ref();
self.inner.write_all(slice).await?;
self.sha256.update(slice).map_err(|err| {
Error::Other(box_err!("openssl hasher failed to update: {}", err))
})?;
size += slice.len();
}
let key = Key::from_encoded(std::mem::take(&mut kv.key));
let ts = key.decode_ts().expect("key without ts");
self.min_ts = self.min_ts.min(ts);
self.max_ts = self.max_ts.max(ts);
self.resolved_ts = self.resolved_ts.max(kv.region_resolved_ts.into());
self.inner.write_all(encoded.as_slice()).await?;
self.sha256
.update(encoded.as_slice())
.map_err(|err| Error::Other(box_err!("openssl hasher failed to update: {}", err)))?;
self.update_key_bound(key.into_encoded());
Ok(size)
}
Expand Down
15 changes: 15 additions & 0 deletions components/tikv_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate test;

use std::collections::hash_map::Entry;
use std::collections::vec_deque::{Iter, VecDeque};
use std::convert::AsRef;
use std::fs::File;
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -329,6 +330,20 @@ impl<L, R> Either<L, R> {
}
}

impl<L, R, T> AsRef<T> for Either<L, R>
where
T: ?Sized,
L: AsRef<T>,
R: AsRef<T>,
{
fn as_ref(&self) -> &T {
match self {
Self::Left(l) => l.as_ref(),
Self::Right(r) => r.as_ref(),
}
}
}

/// A simple ring queue with fixed capacity.
pub struct RingQueue<T> {
buf: VecDeque<T>,
Expand Down