Struct tikv_client::TransactionClient
source · 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 currentTimestamp
from PD.snapshot
: get aSnapshot
of the database at a specified timestamp. ASnapshot
is a read-only transaction.
The returned results of transactional requests are Future
s 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 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>
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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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