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 current Timestamp from PD.
  • snapshot: get a Snapshot of the database at a specified timestamp. A Snapshot is a read-only transaction.

The returned results of transactional requests are Futures that must be awaited to execute.

Implementations§

source§

impl Client

source

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();
source

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();
source

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();
source

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();
source

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();
source

pub fn snapshot( &self, timestamp: Timestamp, options: TransactionOptions, ) -> Snapshot

Create a new Snapshot at the given Timestamp.

source

pub async fn current_timestamp(&self) -> Result<Timestamp>

Retrieve the current Timestamp.

§Examples
let client = TransactionClient::new(vec!["192.168.0.100"]).await.unwrap();
let timestamp = client.current_timestamp().await.unwrap();
source

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:

  1. resolving all locks with timestamp <= safepoint
  2. 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.

source

pub async fn cleanup_locks( &self, range: impl Into<BoundRange>, safepoint: &Timestamp, options: ResolveLocksOptions, ) -> Result<CleanupLocksResult>

source

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§

source§

impl Clone for Client

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromRef<T> for T
where T: Clone,

source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
source§

impl<T> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more