Struct tikv_client::TransactionClient
source · 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 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<ApiV1TxnCodec>
impl Client<ApiV1TxnCodec>
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();
source§impl Client<ApiV2TxnCodec>
impl Client<ApiV2TxnCodec>
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>
impl<Cod: Codec> Client<Cod>
pub async fn new_with_codec<S: Into<String>>( pd_endpoints: Vec<S>, config: Config, codec: Cod ) -> Result<Client<Cod>>
sourcepub async fn begin_optimistic(
&self
) -> Result<Transaction<Cod, PdRpcClient<Cod>>>
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();
sourcepub async fn begin_pessimistic(
&self
) -> Result<Transaction<Cod, PdRpcClient<Cod>>>
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();
sourcepub async fn begin_with_options(
&self,
options: TransactionOptions
) -> Result<Transaction<Cod, PdRpcClient<Cod>>>
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();
sourcepub fn snapshot(
&self,
timestamp: Timestamp,
options: TransactionOptions
) -> Snapshot<Cod, PdRpcClient<Cod>>
pub fn snapshot( &self, timestamp: Timestamp, options: TransactionOptions ) -> Snapshot<Cod, PdRpcClient<Cod>>
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<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> 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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§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