Skip to content

Commit 95543ed

Browse files
rrwang7Convex, Inc.
authored andcommitted
remove rounding for bandwidth and db storage (#25583)
We've decided to no longer round up usage to the nearest 1KB! GitOrigin-RevId: dcff0ed46a69d7017f0d144060559fe476074225
1 parent 92a7ef9 commit 95543ed

File tree

6 files changed

+24
-82
lines changed

6 files changed

+24
-82
lines changed

crates/application/src/export_worker.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,6 @@ mod tests {
877877
};
878878
use headers::ContentType;
879879
use keybroker::Identity;
880-
use maplit::btreemap;
881880
use model::{
882881
exports::types::{
883882
Export,
@@ -1056,14 +1055,9 @@ mod tests {
10561055
assert_eq!(zip_entries, expected_export_entries);
10571056

10581057
let usage = usage.gather_user_stats();
1059-
assert_eq!(
1060-
*usage.database_egress_size,
1061-
btreemap! {
1062-
"table_0".to_string() => 1024,
1063-
"table_1".to_string() => 1024,
1064-
"table_2".to_string() => 1024,
1065-
}
1066-
);
1058+
assert!(usage.database_egress_size["table_0"] > 0);
1059+
assert!(usage.database_egress_size["table_1"] > 0);
1060+
assert!(usage.database_egress_size["table_2"] > 0);
10671061

10681062
Ok(())
10691063
}

crates/application/src/snapshot_import.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,10 +3018,7 @@ a
30183018
.await?;
30193019

30203020
let stats = usage.gather_user_stats();
3021-
assert_eq!(
3022-
*stats.database_ingress_size,
3023-
btreemap! {table_name.to_string() => 2048}
3024-
);
3021+
assert!(stats.database_ingress_size[&table_name.to_string()] > 0);
30253022
assert_eq!(stats.storage_ingress_size, 9);
30263023

30273024
Ok(())

crates/database/src/database.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ impl<RT: Runtime> Database<RT> {
17801780

17811781
let mut document_storage_by_table = BTreeMap::new();
17821782
for (table_name, summary) in snapshot.iter_user_table_summaries() {
1783-
let table_size = summary.total_size_rounded() as usize;
1783+
let table_size = summary.total_size();
17841784
document_storage_by_table.insert(table_name, (table_size, 0));
17851785
}
17861786

crates/database/src/table_summary.rs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ use shape_inference::{
6363
Shape,
6464
ShapeEnum,
6565
};
66-
use usage_tracking::{
67-
round_up,
68-
KB,
69-
};
7066

7167
#[cfg(any(test, feature = "testing"))]
7268
use crate::IndexModel;
@@ -83,17 +79,14 @@ use crate::{
8379
pub struct TableSummary {
8480
inferred_type: CountedShape<ProdConfigWithOptionalFields>,
8581
total_size: i64,
86-
// Used for metered billing, we charge for database storage rounded up to
87-
// the nearest KB per document
88-
total_size_rounded: i64,
8982
}
9083

9184
impl fmt::Display for TableSummary {
9285
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9386
write!(
9487
f,
95-
"TableSummary {{ inferred_type: {}, total_size: {}, total_size_rounded: {} }}",
96-
self.inferred_type, self.total_size, self.total_size_rounded
88+
"TableSummary {{ inferred_type: {}, total_size: {} }}",
89+
self.inferred_type, self.total_size
9790
)
9891
}
9992
}
@@ -103,22 +96,17 @@ impl TableSummary {
10396
Self {
10497
inferred_type: Shape::empty(),
10598
total_size: 0,
106-
total_size_rounded: 0,
10799
}
108100
}
109101

110102
pub fn is_empty(&self) -> bool {
111-
self.inferred_type.is_empty() && self.total_size == 0 && self.total_size_rounded == 0
103+
self.inferred_type.is_empty() && self.total_size == 0
112104
}
113105

114106
pub fn total_size(&self) -> usize {
115107
self.total_size as usize
116108
}
117109

118-
pub fn total_size_rounded(&self) -> i64 {
119-
self.total_size_rounded
120-
}
121-
122110
pub fn num_values(&self) -> usize {
123111
*self.inferred_type.num_values() as usize
124112
}
@@ -129,27 +117,18 @@ impl TableSummary {
129117

130118
pub fn insert(&self, object: &ConvexObject) -> Self {
131119
let total_size = self.total_size + object.size() as i64;
132-
let total_size_rounded =
133-
self.total_size_rounded + round_up(object.size() as u64, KB) as i64;
134120
Self {
135121
inferred_type: self.inferred_type.insert(object),
136122
total_size,
137-
total_size_rounded,
138123
}
139124
}
140125

141126
pub fn remove(&self, object: &ConvexObject) -> anyhow::Result<Self> {
142127
let size = object.size() as i64;
143-
let size_rounded = round_up(object.size() as u64, KB) as i64;
144128
anyhow::ensure!(self.total_size >= size, "Negative size due to {object}");
145-
anyhow::ensure!(
146-
self.total_size_rounded >= size_rounded,
147-
"Negative size due to {object}"
148-
);
149129
Ok(Self {
150130
inferred_type: self.inferred_type.remove(object)?,
151131
total_size: self.total_size - size,
152-
total_size_rounded: self.total_size_rounded - size_rounded,
153132
})
154133
}
155134

@@ -166,7 +145,6 @@ impl From<&TableSummary> for JsonValue {
166145
fn from(summary: &TableSummary) -> Self {
167146
json!({
168147
"totalSize": JsonInteger::encode(summary.total_size),
169-
"totalSizeRounded": JsonInteger::encode(summary.total_size_rounded),
170148
"inferredTypeWithOptionalFields": JsonValue::from(&summary.inferred_type)
171149
})
172150
}
@@ -183,19 +161,13 @@ impl TryFrom<JsonValue> for TableSummary {
183161
_ => anyhow::bail!("Invalid totalSize"),
184162
};
185163
anyhow::ensure!(total_size >= 0);
186-
let total_size_rounded = match v.remove("totalSizeRounded") {
187-
Some(JsonValue::String(s)) => JsonInteger::decode(s)?,
188-
_ => anyhow::bail!("Invalid totalSizeRounded"),
189-
};
190-
anyhow::ensure!(total_size_rounded >= 0);
191164
let inferred_type = match v.remove("inferredTypeWithOptionalFields") {
192165
Some(v) => CountedShape::<ProdConfigWithOptionalFields>::try_from(v)?,
193166
None => anyhow::bail!("Missing field inferredTypeWithOptionalFields"),
194167
};
195168
Ok(TableSummary {
196169
inferred_type,
197170
total_size,
198-
total_size_rounded,
199171
})
200172
},
201173
_ => anyhow::bail!("Wrong type of json value for TableSummaryJson"),

crates/database/src/tests/usage_tracking.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use runtime::testing::TestRuntime;
2525
use usage_tracking::{
2626
CallType,
2727
FunctionUsageTracker,
28-
KB,
2928
};
3029
use vector::VectorSearch;
3130

@@ -99,8 +98,7 @@ async fn vector_insert_counts_usage_for_backfilling_indexes(rt: TestRuntime) ->
9998
.get(&(*index_name.table()).to_string())
10099
.cloned();
101100

102-
// round up.
103-
assert_eq!(value, Some(1024));
101+
assert!(value.unwrap() > 0);
104102
Ok(())
105103
}
106104

@@ -133,8 +131,7 @@ async fn vector_insert_counts_usage_for_enabled_indexes(rt: TestRuntime) -> anyh
133131
.recent_vector_ingress_size
134132
.get(&(*index_name.table()).to_string())
135133
.cloned();
136-
// We round up to the nearest KB
137-
assert_eq!(value, Some(1024_u64));
134+
assert!(value.unwrap() > 0);
138135
Ok(())
139136
}
140137

@@ -214,16 +211,12 @@ async fn vector_query_counts_bandwidth(rt: TestRuntime) -> anyhow::Result<()> {
214211
let stats = fixtures.db.usage_counter().collect();
215212
let vector_egress = stats.recent_vector_egress_size;
216213
let bandwidth_egress = stats.recent_database_egress_size;
217-
// Rounded up.
218-
assert_eq!(
219-
*vector_egress.get(&index_name.table().to_string()).unwrap(),
220-
KB
221-
);
222-
assert_eq!(
214+
assert!(*vector_egress.get(&index_name.table().to_string()).unwrap() > 0);
215+
assert!(
223216
*bandwidth_egress
224217
.get(&index_name.table().to_string())
225-
.unwrap(),
226-
KB
218+
.unwrap()
219+
> 0
227220
);
228221
Ok(())
229222
}
@@ -254,7 +247,7 @@ async fn test_usage_tracking_basic_insert_and_get(rt: TestRuntime) -> anyhow::Re
254247
let database_ingress = stats.recent_database_ingress_size;
255248
assert_eq!(database_ingress.len(), 1);
256249
assert!(database_ingress.contains_key("my_table"));
257-
assert_eq!(*database_ingress.get("my_table").unwrap(), KB);
250+
assert!(*database_ingress.get("my_table").unwrap() > 0);
258251
let database_egress = stats.recent_database_egress_size;
259252
assert!(database_egress.is_empty());
260253

@@ -280,7 +273,7 @@ async fn test_usage_tracking_basic_insert_and_get(rt: TestRuntime) -> anyhow::Re
280273
let database_egress = stats.recent_database_egress_size;
281274
assert_eq!(database_egress.len(), 1);
282275
assert!(database_egress.contains_key("my_table"));
283-
assert_eq!(*database_egress.get("my_table").unwrap(), KB);
276+
assert!(*database_egress.get("my_table").unwrap() > 0);
284277

285278
Ok(())
286279
}
@@ -336,11 +329,10 @@ async fn test_usage_tracking_insert_with_index(rt: TestRuntime) -> anyhow::Resul
336329
);
337330

338331
let stats = db.usage_counter().collect();
339-
// Database bandwidth ingress is 6 KB from 3 document writes and 3 index writes
340332
let database_ingress = stats.recent_database_ingress_size;
341333
assert_eq!(database_ingress.len(), 1);
342334
assert!(database_ingress.contains_key("my_table"));
343-
assert_eq!(*database_ingress.get("my_table").unwrap(), 6 * KB);
335+
assert!(*database_ingress.get("my_table").unwrap() > 0);
344336
let database_egress = stats.recent_database_egress_size;
345337
assert!(database_egress.is_empty());
346338

@@ -364,13 +356,12 @@ async fn test_usage_tracking_insert_with_index(rt: TestRuntime) -> anyhow::Resul
364356
);
365357

366358
let stats = db.usage_counter().collect();
367-
// Database bandwidth egress is 4 KB from 2 document writes and 2 index writes
368359
let database_ingress = stats.recent_database_ingress_size;
369360
assert!(database_ingress.is_empty());
370361
let database_egress = stats.recent_database_egress_size;
371362
assert_eq!(database_egress.len(), 1);
372363
assert!(database_egress.contains_key("my_table"));
373-
assert_eq!(*database_egress.get("my_table").unwrap(), 4 * KB);
364+
assert!(*database_egress.get("my_table").unwrap() > 0);
374365

375366
Ok(())
376367
}

crates/usage_tracking/src/lib.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@ impl HeapSize for UsageCounter {
111111
}
112112
}
113113

114-
pub const KB: u64 = 1 << 10;
115-
116-
pub fn round_up(n: u64, k: u64) -> u64 {
117-
(n + k - 1) / k * k
118-
}
119-
120114
impl UsageCounter {
121115
pub fn new(usage_logger: Arc<dyn UsageEventLogger>) -> Self {
122116
let state = Arc::new(Mutex::new(UsageCounterState::default()));
@@ -570,9 +564,7 @@ impl FunctionUsageTracker {
570564
let mut state = self.state.lock();
571565
state
572566
.database_ingress_size
573-
.mutate_entry_or_default(table_name.clone(), |count| {
574-
*count += round_up(ingress_size, KB)
575-
});
567+
.mutate_entry_or_default(table_name.clone(), |count| *count += ingress_size);
576568
}
577569

578570
pub fn track_database_egress_size(
@@ -588,9 +580,7 @@ impl FunctionUsageTracker {
588580
let mut state = self.state.lock();
589581
state
590582
.database_egress_size
591-
.mutate_entry_or_default(table_name.clone(), |count| {
592-
*count += round_up(egress_size, KB)
593-
});
583+
.mutate_entry_or_default(table_name.clone(), |count| *count += egress_size);
594584
}
595585

596586
// Tracks the vector ingress surcharge and database usage for documents
@@ -618,16 +608,15 @@ impl FunctionUsageTracker {
618608
// Note that vector search counts as both database and vector bandwidth
619609
// per the comment above.
620610
let mut state = self.state.lock();
621-
let rounded_size = round_up(ingress_size, KB);
622611
state
623612
.database_ingress_size
624613
.mutate_entry_or_default(table_name.clone(), |count| {
625-
*count += rounded_size;
614+
*count += ingress_size;
626615
});
627616
state
628617
.vector_ingress_size
629618
.mutate_entry_or_default(table_name.clone(), |count| {
630-
*count += rounded_size;
619+
*count += ingress_size;
631620
});
632621
}
633622

@@ -657,13 +646,12 @@ impl FunctionUsageTracker {
657646
// Note that vector search counts as both database and vector bandwidth
658647
// per the comment above.
659648
let mut state = self.state.lock();
660-
let rounded_size = round_up(egress_size, KB);
661649
state
662650
.database_egress_size
663-
.mutate_entry_or_default(table_name.clone(), |count| *count += rounded_size);
651+
.mutate_entry_or_default(table_name.clone(), |count| *count += egress_size);
664652
state
665653
.vector_egress_size
666-
.mutate_entry_or_default(table_name.clone(), |count| *count += rounded_size);
654+
.mutate_entry_or_default(table_name.clone(), |count| *count += egress_size);
667655
}
668656
}
669657

0 commit comments

Comments
 (0)