pub struct TransactionClient { /* private fields */ }Expand description
The TiKV transactional Client is used to interact with TiKV using transactional requests.
Transactions support optimistic and pessimistic modes. For more details see the SIG-transaction docs.
Begin a Transaction by calling begin_optimistic or
begin_pessimistic. A transaction must be rolled back or committed.
Besides transactions, the client provides some further functionality:
gc: trigger a GC process which clears stale data in the cluster.current_timestamp: get the currentTimestampfrom PD.snapshot: get aSnapshotof the database at a specified timestamp. ASnapshotis a read-only transaction.
The returned results of transactional requests are Futures that must be
awaited to execute.
Implementations§
Source§impl Client
impl Client
Sourcepub async fn new<S: Into<String>>(pd_endpoints: Vec<S>) -> Result<Client>
pub async fn new<S: Into<String>>(pd_endpoints: Vec<S>) -> Result<Client>
Create a transactional Client and connect to the TiKV cluster.
Because TiKV is managed by a PD cluster, the endpoints for PD must be provided, not the TiKV nodes. It’s important to include more than one PD endpoint (include all endpoints, if possible), this helps avoid having a single point of failure.
§Examples
let client = TransactionClient::new(vec!["192.168.0.100"]).await.unwrap();Sourcepub async fn new_with_config<S: Into<String>>(
pd_endpoints: Vec<S>,
config: Config,
) -> Result<Client>
pub async fn new_with_config<S: Into<String>>( pd_endpoints: Vec<S>, config: Config, ) -> Result<Client>
Create a transactional Client with a custom configuration, and connect to the TiKV cluster.
Because TiKV is managed by a PD cluster, the endpoints for PD must be provided, not the TiKV nodes. It’s important to include more than one PD endpoint (include all endpoints, if possible), this helps avoid having a single point of failure.
§Examples
let client = TransactionClient::new_with_config(
vec!["192.168.0.100"],
Config::default().with_timeout(Duration::from_secs(60)),
)
.await
.unwrap();Sourcepub async fn new_with_config_api_v2_no_prefix<S: Into<String>>(
pd_endpoints: Vec<S>,
config: Config,
) -> Result<Client>
pub async fn new_with_config_api_v2_no_prefix<S: Into<String>>( pd_endpoints: Vec<S>, config: Config, ) -> Result<Client>
Create a transactional Client that uses API V2 without adding or removing any API V2
keyspace/key-mode prefix, with a custom configuration.
This is intended for server-side embedding use cases. config.keyspace must be unset.
Sourcepub async fn begin_optimistic(&self) -> Result<Transaction>
pub async fn begin_optimistic(&self) -> Result<Transaction>
Creates a new optimistic Transaction.
Use the transaction to issue requests like get or
put.
Write operations do not lock data in TiKV, thus the commit request may fail due to a write conflict.
§Examples
let client = TransactionClient::new(vec!["192.168.0.100"]).await.unwrap();
let mut transaction = client.begin_optimistic().await.unwrap();
// ... Issue some commands.
transaction.commit().await.unwrap();Sourcepub async fn begin_pessimistic(&self) -> Result<Transaction>
pub async fn begin_pessimistic(&self) -> Result<Transaction>
Creates a new pessimistic Transaction.
Write operations will lock the data until committed, thus commit requests should not suffer from write conflicts.
§Examples
let client = TransactionClient::new(vec!["192.168.0.100"]).await.unwrap();
let mut transaction = client.begin_pessimistic().await.unwrap();
// ... Issue some commands.
transaction.commit().await.unwrap();Sourcepub async fn begin_with_options(
&self,
options: TransactionOptions,
) -> Result<Transaction>
pub async fn begin_with_options( &self, options: TransactionOptions, ) -> Result<Transaction>
Create a new customized Transaction.
§Examples
let client = TransactionClient::new(vec!["192.168.0.100"]).await.unwrap();
let mut transaction = client
.begin_with_options(TransactionOptions::default().use_async_commit())
.await
.unwrap();
// ... Issue some commands.
transaction.commit().await.unwrap();Sourcepub fn snapshot(
&self,
timestamp: Timestamp,
options: TransactionOptions,
) -> Snapshot
pub fn snapshot( &self, timestamp: Timestamp, options: TransactionOptions, ) -> Snapshot
Sourcepub async fn current_timestamp(&self) -> Result<Timestamp>
pub async fn current_timestamp(&self) -> Result<Timestamp>
Sourcepub async fn gc(&self, safepoint: Timestamp) -> Result<bool>
pub async fn gc(&self, safepoint: Timestamp) -> Result<bool>
Request garbage collection (GC) of the TiKV cluster.
GC deletes MVCC records whose timestamp is lower than the given safepoint. We must guarantee
that all transactions started before this timestamp had committed. We can keep an active
transaction list in application to decide which is the minimal start timestamp of them.
For each key, the last mutation record (unless it’s a deletion) before safepoint is retained.
GC is performed by:
- resolving all locks with timestamp <=
safepoint - updating PD’s known safepoint
This is a simplified version of GC in TiDB. We skip the second step “delete ranges” which is an optimization for TiDB.
pub async fn cleanup_locks( &self, range: impl Into<BoundRange>, safepoint: &Timestamp, options: ResolveLocksOptions, ) -> Result<CleanupLocksResult>
pub async fn scan_locks( &self, safepoint: &Timestamp, range: impl Into<BoundRange>, batch_size: u32, ) -> Result<Vec<ProtoLockInfo>>
Sourcepub async fn resolve_locks(
&self,
locks: Vec<ProtoLockInfo>,
timestamp: Timestamp,
backoff: Backoff,
) -> Result<Vec<ProtoLockInfo>>
pub async fn resolve_locks( &self, locks: Vec<ProtoLockInfo>, timestamp: Timestamp, backoff: Backoff, ) -> Result<Vec<ProtoLockInfo>>
Resolves the given locks and returns any that remain live.
This method retries until either all locks are resolved or the provided
backoff is exhausted. The timestamp is used as the caller start
timestamp when checking transaction status.
Sourcepub async fn unsafe_destroy_range(
&self,
range: impl Into<BoundRange>,
) -> Result<()>
pub async fn unsafe_destroy_range( &self, range: impl Into<BoundRange>, ) -> Result<()>
Cleans up all keys in a range and quickly reclaim disk space.
The range can span over multiple regions.
Note that the request will directly delete data from RocksDB, and all MVCC will be erased.
This interface is intended for special scenarios that resemble operations like “drop table” or “drop database” in TiDB.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request