pub struct TransactionClient<Cod: Codec = ApiV1TxnCodec> { /* 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<ApiV1TxnCodec>

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§

impl Client<ApiV2TxnCodec>

source

pub async fn new_with_config_v2<S: Into<String>>( _keyspace_name: &str, pd_endpoints: Vec<S>, config: Config ) -> Result<Client<ApiV2TxnCodec>>

source§

impl<Cod: Codec> Client<Cod>

source

pub async fn new_with_codec<S: Into<String>>( pd_endpoints: Vec<S>, config: Config, codec: Cod ) -> Result<Client<Cod>>

source

pub async fn begin_optimistic( &self ) -> Result<Transaction<Cod, PdRpcClient<Cod>>>

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<Cod, PdRpcClient<Cod>>>

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<Cod, PdRpcClient<Cod>>>

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<Cod, PdRpcClient<Cod>>

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<Cod: Codec> Clone for Client<Cod>

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<Cod = ApiV1TxnCodec> !RefUnwindSafe for Client<Cod>

§

impl<Cod> Send for Client<Cod>

§

impl<Cod> Sync for Client<Cod>

§

impl<Cod> Unpin for Client<Cod>

§

impl<Cod = ApiV1TxnCodec> !UnwindSafe for Client<Cod>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn from_ref(input: &T) -> T

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

impl<T> Instrument for T

§

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

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

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 Twhere 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> 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 Twhere 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 Twhere 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 Twhere 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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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