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

chore: add metrics for write logs in wal #1166

Merged
merged 1 commit into from
Aug 22, 2023
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions wal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ codec = { workspace = true }
common_types = { workspace = true }
futures = { workspace = true, features = ["async-await"], optional = true }
generic_error = { workspace = true }
lazy_static = { workspace = true }
log = { workspace = true }
macros = { workspace = true }
message_queue = { workspace = true }
prometheus = { workspace = true }
prost = { workspace = true }
runtime = { workspace = true }
serde = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions wal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod kv_encoder;
pub mod log_batch;
pub mod manager;
pub mod message_queue_impl;
pub(crate) mod metrics;
pub mod rocks_impl;
pub mod table_kv_impl;

Expand Down
11 changes: 10 additions & 1 deletion wal/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ use generic_error::BoxError;
use runtime::Runtime;
use snafu::ResultExt;

use crate::log_batch::{LogEntry, LogWriteBatch, PayloadDecoder};
use crate::{
log_batch::{LogEntry, LogWriteBatch, PayloadDecoder},
metrics::WAL_WRITE_BYTES_HISTOGRAM,
};

pub mod error {
use generic_error::GenericError;
Expand Down Expand Up @@ -333,6 +336,12 @@ pub trait WalManager: Send + Sync + fmt::Debug + 'static {
async fn get_statistics(&self) -> Option<String>;
}

/// Used to collect the metrics about the write logs.
pub(crate) fn collect_write_log_metrics(batch: &LogWriteBatch) {
let total_bytes: usize = batch.entries.iter().map(|v| v.payload.len()).sum();
WAL_WRITE_BYTES_HISTOGRAM.observe(total_bytes as f64);
}

#[derive(Debug)]
enum LogIterator {
Sync {
Expand Down
5 changes: 3 additions & 2 deletions wal/src/message_queue_impl/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use snafu::ResultExt;
use crate::{
log_batch::{LogEntry, LogWriteBatch},
manager::{
error::*, AsyncLogIterator, BatchLogIteratorAdapter, ReadContext, ReadRequest, RegionId,
ScanContext, ScanRequest, WalLocation, WalManager, WriteContext,
self, error::*, AsyncLogIterator, BatchLogIteratorAdapter, ReadContext, ReadRequest,
RegionId, ScanContext, ScanRequest, WalLocation, WalManager, WriteContext,
},
message_queue_impl::{
config::Config,
Expand Down Expand Up @@ -105,6 +105,7 @@ impl<M: MessageQueue> WalManager for MessageQueueImpl<M> {
}

async fn write(&self, ctx: &WriteContext, batch: &LogWriteBatch) -> Result<SequenceNumber> {
manager::collect_write_log_metrics(batch);
self.0.write(ctx, batch).await.box_err().context(Write)
}

Expand Down
25 changes: 25 additions & 0 deletions wal/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2023 The CeresDB Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use lazy_static::lazy_static;
use prometheus::{exponential_buckets, register_histogram, Histogram};

lazy_static! {
pub static ref WAL_WRITE_BYTES_HISTOGRAM: Histogram = register_histogram!(
"wal_write_bytes_distribution",
"Bucketed histogram of wal write bytes",
exponential_buckets(64.0, 4.0, 10).unwrap()
)
.unwrap();
}
4 changes: 3 additions & 1 deletion wal/src/rocks_impl/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
kv_encoder::{CommonLogEncoding, CommonLogKey, MaxSeqMetaEncoding, MaxSeqMetaValue, MetaKey},
log_batch::{LogEntry, LogWriteBatch},
manager::{
error::*, BatchLogIteratorAdapter, ReadContext, ReadRequest, RegionId, ScanContext,
self, error::*, BatchLogIteratorAdapter, ReadContext, ReadRequest, RegionId, ScanContext,
ScanRequest, SyncLogIterator, WalLocation, WalManager, WriteContext,
},
};
Expand Down Expand Up @@ -204,6 +204,8 @@ impl TableUnit {
batch.entries.len()
);

manager::collect_write_log_metrics(batch);

let entries_num = batch.len() as u64;
let (wb, max_sequence_num) = {
let wb = WriteBatch::default();
Expand Down
2 changes: 2 additions & 0 deletions wal/src/table_kv_impl/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ impl<T: TableKv> WalManager for WalNamespaceImpl<T> {
ctx: &manager::WriteContext,
batch: &LogWriteBatch,
) -> Result<SequenceNumber> {
manager::collect_write_log_metrics(batch);

self.namespace
.write_log(ctx, batch)
.await
Expand Down