Skip to content

Commit b7b8b8b

Browse files
shashwatj07ekexium
andauthored
added logs (#294)
Signed-off-by: Shashwat Jaiswal <[email protected]> Co-authored-by: Ziqian Qin <[email protected]>
1 parent ad01f59 commit b7b8b8b

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

src/raw/client.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
request::{Collect, Plan},
1111
BoundRange, ColumnFamily, Key, KvPair, Result, Value,
1212
};
13+
use log::debug;
1314
use std::{sync::Arc, u32};
1415

1516
const MAX_RAW_KV_SCAN_LIMIT: u32 = 10240;
@@ -147,6 +148,7 @@ impl Client {
147148
/// # });
148149
/// ```
149150
pub async fn get(&self, key: impl Into<Key>) -> Result<Option<Value>> {
151+
debug!("invoking raw get request");
150152
let request = new_raw_get_request(key.into(), self.cf.clone());
151153
let plan = crate::request::PlanBuilder::new(self.rpc.clone(), request)
152154
.single_region()
@@ -179,6 +181,7 @@ impl Client {
179181
&self,
180182
keys: impl IntoIterator<Item = impl Into<Key>>,
181183
) -> Result<Vec<KvPair>> {
184+
debug!("invoking raw batch_get request");
182185
let request = new_raw_batch_get_request(keys.into_iter().map(Into::into), self.cf.clone());
183186
let plan = crate::request::PlanBuilder::new(self.rpc.clone(), request)
184187
.multi_region()
@@ -207,6 +210,7 @@ impl Client {
207210
/// # });
208211
/// ```
209212
pub async fn put(&self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> {
213+
debug!("invoking raw put request");
210214
let request = new_raw_put_request(key.into(), value.into(), self.cf.clone(), self.atomic);
211215
let plan = crate::request::PlanBuilder::new(self.rpc.clone(), request)
212216
.single_region()
@@ -239,6 +243,7 @@ impl Client {
239243
&self,
240244
pairs: impl IntoIterator<Item = impl Into<KvPair>>,
241245
) -> Result<()> {
246+
debug!("invoking raw batch_put request");
242247
let request = new_raw_batch_put_request(
243248
pairs.into_iter().map(Into::into),
244249
self.cf.clone(),
@@ -271,6 +276,7 @@ impl Client {
271276
/// # });
272277
/// ```
273278
pub async fn delete(&self, key: impl Into<Key>) -> Result<()> {
279+
debug!("invoking raw delete request");
274280
let request = new_raw_delete_request(key.into(), self.cf.clone(), self.atomic);
275281
let plan = crate::request::PlanBuilder::new(self.rpc.clone(), request)
276282
.single_region()
@@ -300,6 +306,7 @@ impl Client {
300306
/// # });
301307
/// ```
302308
pub async fn batch_delete(&self, keys: impl IntoIterator<Item = impl Into<Key>>) -> Result<()> {
309+
debug!("invoking raw batch_delete request");
303310
self.assert_non_atomic()?;
304311
let request =
305312
new_raw_batch_delete_request(keys.into_iter().map(Into::into), self.cf.clone());
@@ -328,6 +335,7 @@ impl Client {
328335
/// # });
329336
/// ```
330337
pub async fn delete_range(&self, range: impl Into<BoundRange>) -> Result<()> {
338+
debug!("invoking raw delete_range request");
331339
self.assert_non_atomic()?;
332340
let request = new_raw_delete_range_request(range.into(), self.cf.clone());
333341
let plan = crate::request::PlanBuilder::new(self.rpc.clone(), request)
@@ -359,6 +367,7 @@ impl Client {
359367
/// # });
360368
/// ```
361369
pub async fn scan(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<KvPair>> {
370+
debug!("invoking raw scan request");
362371
self.scan_inner(range.into(), limit, false).await
363372
}
364373

@@ -382,6 +391,7 @@ impl Client {
382391
/// # });
383392
/// ```
384393
pub async fn scan_keys(&self, range: impl Into<BoundRange>, limit: u32) -> Result<Vec<Key>> {
394+
debug!("invoking raw scan_keys request");
385395
Ok(self
386396
.scan_inner(range, limit, true)
387397
.await?
@@ -418,6 +428,7 @@ impl Client {
418428
ranges: impl IntoIterator<Item = impl Into<BoundRange>>,
419429
each_limit: u32,
420430
) -> Result<Vec<KvPair>> {
431+
debug!("invoking raw batch_scan request");
421432
self.batch_scan_inner(ranges, each_limit, false).await
422433
}
423434

src/transaction/client.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
transaction::{Snapshot, Transaction, TransactionOptions},
1111
Result,
1212
};
13+
use log::debug;
1314
use std::{mem, sync::Arc};
1415
use tikv_client_proto::{kvrpcpb, pdpb::Timestamp};
1516

@@ -53,6 +54,7 @@ impl Client {
5354
/// # });
5455
/// ```
5556
pub async fn new<S: Into<String>>(pd_endpoints: Vec<S>) -> Result<Client> {
57+
debug!("creating transactional client");
5658
Self::new_with_config(pd_endpoints, Config::default()).await
5759
}
5860

@@ -79,6 +81,7 @@ impl Client {
7981
pd_endpoints: Vec<S>,
8082
config: Config,
8183
) -> Result<Client> {
84+
debug!("creating transactional client with custom configuration");
8285
let pd_endpoints: Vec<String> = pd_endpoints.into_iter().map(Into::into).collect();
8386
let pd = Arc::new(PdRpcClient::connect(&pd_endpoints, &config, true).await?);
8487
Ok(Client { pd })
@@ -105,6 +108,7 @@ impl Client {
105108
/// # });
106109
/// ```
107110
pub async fn begin_optimistic(&self) -> Result<Transaction> {
111+
debug!("creating new optimistic transaction");
108112
let timestamp = self.current_timestamp().await?;
109113
Ok(self.new_transaction(timestamp, TransactionOptions::new_optimistic()))
110114
}
@@ -127,6 +131,7 @@ impl Client {
127131
/// # });
128132
/// ```
129133
pub async fn begin_pessimistic(&self) -> Result<Transaction> {
134+
debug!("creating new pessimistic transaction");
130135
let timestamp = self.current_timestamp().await?;
131136
Ok(self.new_transaction(timestamp, TransactionOptions::new_pessimistic()))
132137
}
@@ -149,12 +154,14 @@ impl Client {
149154
/// # });
150155
/// ```
151156
pub async fn begin_with_options(&self, options: TransactionOptions) -> Result<Transaction> {
157+
debug!("creating new customized transaction");
152158
let timestamp = self.current_timestamp().await?;
153159
Ok(self.new_transaction(timestamp, options))
154160
}
155161

156162
/// Create a new [`Snapshot`](Snapshot) at the given [`Timestamp`](Timestamp).
157163
pub fn snapshot(&self, timestamp: Timestamp, options: TransactionOptions) -> Snapshot {
164+
debug!("creating new snapshot");
158165
Snapshot::new(self.new_transaction(timestamp, options.read_only()))
159166
}
160167

@@ -187,6 +194,7 @@ impl Client {
187194
/// This is a simplified version of [GC in TiDB](https://docs.pingcap.com/tidb/stable/garbage-collection-overview).
188195
/// We skip the second step "delete ranges" which is an optimization for TiDB.
189196
pub async fn gc(&self, safepoint: Timestamp) -> Result<bool> {
197+
debug!("invoking transactional gc request");
190198
// scan all locks with ts <= safepoint
191199
let mut locks: Vec<kvrpcpb::LockInfo> = vec![];
192200
let mut start_key = vec![];

src/transaction/lock.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
transaction::requests,
1010
Error, Result,
1111
};
12+
use log::debug;
1213
use std::{
1314
collections::{HashMap, HashSet},
1415
sync::Arc,
@@ -28,6 +29,7 @@ pub async fn resolve_locks(
2829
locks: Vec<kvrpcpb::LockInfo>,
2930
pd_client: Arc<impl PdClient>,
3031
) -> Result<bool> {
32+
debug!("resolving locks");
3133
let ts = pd_client.clone().get_timestamp().await?;
3234
let mut has_live_locks = false;
3335
let expired_locks = locks.into_iter().filter(|lock| {
@@ -94,9 +96,11 @@ async fn resolve_lock_with_retry(
9496
commit_version: u64,
9597
pd_client: Arc<impl PdClient>,
9698
) -> Result<RegionVerId> {
99+
debug!("resolving locks with retry");
97100
// FIXME: Add backoff
98101
let mut error = None;
99-
for _ in 0..RESOLVE_LOCK_RETRY_LIMIT {
102+
for i in 0..RESOLVE_LOCK_RETRY_LIMIT {
103+
debug!("resolving locks: attempt {}", (i + 1));
100104
let store = pd_client.clone().store_for_key(key.into()).await?;
101105
let ver_id = store.region.ver_id();
102106
let request = requests::new_resolve_lock_request(start_version, commit_version);

src/transaction/snapshot.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::{BoundRange, Key, KvPair, Result, Transaction, Value};
44
use derive_new::new;
55
use futures::stream::BoxStream;
6+
use log::debug;
67
use std::ops::RangeBounds;
78

89
/// A read-only transaction which reads at the given timestamp.
@@ -20,11 +21,13 @@ pub struct Snapshot {
2021
impl Snapshot {
2122
/// Get the value associated with the given key.
2223
pub async fn get(&mut self, key: impl Into<Key>) -> Result<Option<Value>> {
24+
debug!("invoking get request on snapshot");
2325
self.transaction.get(key).await
2426
}
2527

2628
/// Check whether the key exists.
2729
pub async fn key_exists(&mut self, key: impl Into<Key>) -> Result<bool> {
30+
debug!("invoking key_exists request on snapshot");
2831
self.transaction.key_exists(key).await
2932
}
3033

@@ -33,6 +36,7 @@ impl Snapshot {
3336
&mut self,
3437
keys: impl IntoIterator<Item = impl Into<Key>>,
3538
) -> Result<impl Iterator<Item = KvPair>> {
39+
debug!("invoking batch_get request on snapshot");
3640
self.transaction.batch_get(keys).await
3741
}
3842

@@ -42,6 +46,7 @@ impl Snapshot {
4246
range: impl Into<BoundRange>,
4347
limit: u32,
4448
) -> Result<impl Iterator<Item = KvPair>> {
49+
debug!("invoking scan request on snapshot");
4550
self.transaction.scan(range, limit).await
4651
}
4752

@@ -51,12 +56,14 @@ impl Snapshot {
5156
range: impl Into<BoundRange>,
5257
limit: u32,
5358
) -> Result<impl Iterator<Item = Key>> {
59+
debug!("invoking scan_keys request on snapshot");
5460
self.transaction.scan_keys(range, limit).await
5561
}
5662

5763
/// Unimplemented. Similar to scan, but in the reverse direction.
5864
#[allow(dead_code)]
5965
fn scan_reverse(&mut self, range: impl RangeBounds<Key>) -> BoxStream<Result<KvPair>> {
66+
debug!("invoking scan_reverse request on snapshot");
6067
self.transaction.scan_reverse(range)
6168
}
6269
}

src/transaction/transaction.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
use derive_new::new;
1212
use fail::fail_point;
1313
use futures::{prelude::*, stream::BoxStream};
14+
use log::debug;
1415
use std::{iter, ops::RangeBounds, sync::Arc, time::Instant};
1516
use tikv_client_proto::{kvrpcpb, pdpb::Timestamp};
1617
use tokio::{sync::RwLock, time::Duration};
@@ -104,6 +105,7 @@ impl<PdC: PdClient> Transaction<PdC> {
104105
/// # });
105106
/// ```
106107
pub async fn get(&mut self, key: impl Into<Key>) -> Result<Option<Value>> {
108+
debug!("invoking transactional get request");
107109
self.check_allow_operation().await?;
108110
let timestamp = self.timestamp.clone();
109111
let rpc = self.rpc.clone();
@@ -168,6 +170,7 @@ impl<PdC: PdClient> Transaction<PdC> {
168170
/// # });
169171
/// ```
170172
pub async fn get_for_update(&mut self, key: impl Into<Key>) -> Result<Option<Value>> {
173+
debug!("invoking transactional get_for_update request");
171174
self.check_allow_operation().await?;
172175
if !self.is_pessimistic() {
173176
let key = key.into();
@@ -198,6 +201,7 @@ impl<PdC: PdClient> Transaction<PdC> {
198201
/// # });
199202
/// ```
200203
pub async fn key_exists(&mut self, key: impl Into<Key>) -> Result<bool> {
204+
debug!("invoking transactional key_exists request");
201205
let key = key.into();
202206
Ok(self.scan_keys(key.clone()..=key, 1).await?.next().is_some())
203207
}
@@ -234,6 +238,7 @@ impl<PdC: PdClient> Transaction<PdC> {
234238
&mut self,
235239
keys: impl IntoIterator<Item = impl Into<Key>>,
236240
) -> Result<impl Iterator<Item = KvPair>> {
241+
debug!("invoking transactional batch_get request");
237242
self.check_allow_operation().await?;
238243
let timestamp = self.timestamp.clone();
239244
let rpc = self.rpc.clone();
@@ -286,6 +291,7 @@ impl<PdC: PdClient> Transaction<PdC> {
286291
&mut self,
287292
keys: impl IntoIterator<Item = impl Into<Key>>,
288293
) -> Result<Vec<KvPair>> {
294+
debug!("invoking transactional batch_get_for_update request");
289295
self.check_allow_operation().await?;
290296
let keys: Vec<Key> = keys.into_iter().map(|k| k.into()).collect();
291297
if !self.is_pessimistic() {
@@ -329,6 +335,7 @@ impl<PdC: PdClient> Transaction<PdC> {
329335
range: impl Into<BoundRange>,
330336
limit: u32,
331337
) -> Result<impl Iterator<Item = KvPair>> {
338+
debug!("invoking transactional scan request");
332339
self.scan_inner(range, limit, false).await
333340
}
334341

@@ -364,6 +371,7 @@ impl<PdC: PdClient> Transaction<PdC> {
364371
range: impl Into<BoundRange>,
365372
limit: u32,
366373
) -> Result<impl Iterator<Item = Key>> {
374+
debug!("invoking transactional scan_keys request");
367375
Ok(self
368376
.scan_inner(range, limit, true)
369377
.await?
@@ -374,6 +382,7 @@ impl<PdC: PdClient> Transaction<PdC> {
374382
///
375383
/// Similar to [`scan`](Transaction::scan), but scans in the reverse direction.
376384
pub(crate) fn scan_reverse(&self, _range: impl RangeBounds<Key>) -> BoxStream<Result<KvPair>> {
385+
debug!("invoking transactional scan_reverse request");
377386
unimplemented!()
378387
}
379388

@@ -394,6 +403,7 @@ impl<PdC: PdClient> Transaction<PdC> {
394403
/// # });
395404
/// ```
396405
pub async fn put(&mut self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> {
406+
debug!("invoking transactional put request");
397407
self.check_allow_operation().await?;
398408
let key = key.into();
399409
if self.is_pessimistic() {
@@ -424,6 +434,7 @@ impl<PdC: PdClient> Transaction<PdC> {
424434
/// # });
425435
/// ```
426436
pub async fn insert(&mut self, key: impl Into<Key>, value: impl Into<Value>) -> Result<()> {
437+
debug!("invoking transactional insert request");
427438
self.check_allow_operation().await?;
428439
let key = key.into();
429440
if self.buffer.get(&key).is_some() {
@@ -458,6 +469,7 @@ impl<PdC: PdClient> Transaction<PdC> {
458469
/// # });
459470
/// ```
460471
pub async fn delete(&mut self, key: impl Into<Key>) -> Result<()> {
472+
debug!("invoking transactional delete request");
461473
self.check_allow_operation().await?;
462474
let key = key.into();
463475
if self.is_pessimistic() {
@@ -495,6 +507,7 @@ impl<PdC: PdClient> Transaction<PdC> {
495507
&mut self,
496508
keys: impl IntoIterator<Item = impl Into<Key>>,
497509
) -> Result<()> {
510+
debug!("invoking transactional lock_keys request");
498511
self.check_allow_operation().await?;
499512
match self.options.kind {
500513
TransactionKind::Optimistic => {
@@ -526,6 +539,7 @@ impl<PdC: PdClient> Transaction<PdC> {
526539
/// # });
527540
/// ```
528541
pub async fn commit(&mut self) -> Result<Option<Timestamp>> {
542+
debug!("commiting transaction");
529543
{
530544
let mut status = self.status.write().await;
531545
if !matches!(
@@ -582,6 +596,7 @@ impl<PdC: PdClient> Transaction<PdC> {
582596
/// # });
583597
/// ```
584598
pub async fn rollback(&mut self) -> Result<()> {
599+
debug!("rolling back transaction");
585600
{
586601
let status = self.status.read().await;
587602
if !matches!(
@@ -625,6 +640,7 @@ impl<PdC: PdClient> Transaction<PdC> {
625640
/// Returns the TTL set on the transaction's locks by TiKV.
626641
#[doc(hidden)]
627642
pub async fn send_heart_beat(&mut self) -> Result<u64> {
643+
debug!("sending heart_beat");
628644
self.check_allow_operation().await?;
629645
let primary_key = match self.buffer.get_primary_key() {
630646
Some(k) => k,
@@ -686,6 +702,7 @@ impl<PdC: PdClient> Transaction<PdC> {
686702
keys: impl IntoIterator<Item = impl PessimisticLock>,
687703
need_value: bool,
688704
) -> Result<Vec<KvPair>> {
705+
debug!("acquiring pessimistic lock");
689706
assert!(
690707
matches!(self.options.kind, TransactionKind::Pessimistic(_)),
691708
"`pessimistic_lock` is only valid to use with pessimistic transactions"
@@ -752,6 +769,7 @@ impl<PdC: PdClient> Transaction<PdC> {
752769
}
753770

754771
async fn start_auto_heartbeat(&mut self) {
772+
debug!("starting auto_heartbeat");
755773
if !self.options.heartbeat_option.is_auto_heartbeat() || self.is_heartbeat_started {
756774
return;
757775
}
@@ -810,6 +828,7 @@ impl<PdC: PdClient> Transaction<PdC> {
810828

811829
impl<PdC: PdClient> Drop for Transaction<PdC> {
812830
fn drop(&mut self) {
831+
debug!("dropping transaction");
813832
if std::thread::panicking() {
814833
return;
815834
}

0 commit comments

Comments
 (0)