support putAtomic and deleteAtomic API (#214)

Signed-off-by: marsishandsome <marsishandsome@gmail.com>
This commit is contained in:
Liangliang Gu 2021-06-25 11:59:06 +08:00 committed by GitHub
parent 41b24bb877
commit 81d4981c80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 4 deletions

View File

@ -872,7 +872,7 @@ public class RegionStoreClient extends AbstractRegionStoreClient {
return resp.getTtl(); return resp.getTtl();
} }
public void rawDelete(BackOffer backOffer, ByteString key) { public void rawDelete(BackOffer backOffer, ByteString key, boolean atomic) {
Histogram.Timer requestTimer = Histogram.Timer requestTimer =
GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_delete").startTimer(); GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_delete").startTimer();
try { try {
@ -881,6 +881,7 @@ public class RegionStoreClient extends AbstractRegionStoreClient {
RawDeleteRequest.newBuilder() RawDeleteRequest.newBuilder()
.setContext(region.getReplicaContext(storeType)) .setContext(region.getReplicaContext(storeType))
.setKey(key) .setKey(key)
.setForCas(atomic)
.build(); .build();
KVErrorHandler<RawDeleteResponse> handler = KVErrorHandler<RawDeleteResponse> handler =
@ -908,7 +909,8 @@ public class RegionStoreClient extends AbstractRegionStoreClient {
} }
} }
public void rawPut(BackOffer backOffer, ByteString key, ByteString value, long ttl) { public void rawPut(
BackOffer backOffer, ByteString key, ByteString value, long ttl, boolean atomic) {
Histogram.Timer requestTimer = Histogram.Timer requestTimer =
GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_put").startTimer(); GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_put").startTimer();
try { try {
@ -919,6 +921,7 @@ public class RegionStoreClient extends AbstractRegionStoreClient {
.setKey(key) .setKey(key)
.setValue(value) .setValue(value)
.setTtl(ttl) .setTtl(ttl)
.setForCas(atomic)
.build(); .build();
KVErrorHandler<RawPutResponse> handler = KVErrorHandler<RawPutResponse> handler =

View File

@ -112,6 +112,21 @@ public class RawKVClient implements AutoCloseable {
* @param ttl the ttl of the key (in seconds), 0 means the key will never be outdated * @param ttl the ttl of the key (in seconds), 0 means the key will never be outdated
*/ */
public void put(ByteString key, ByteString value, long ttl) { public void put(ByteString key, ByteString value, long ttl) {
put(key, value, ttl, false);
}
/**
* Put a raw key-value pair to TiKV. This API is atomic.
*
* @param key raw key
* @param value raw value
* @param ttl the ttl of the key (in seconds), 0 means the key will never be outdated
*/
public void putAtomic(ByteString key, ByteString value, long ttl) {
put(key, value, ttl, true);
}
private void put(ByteString key, ByteString value, long ttl, boolean atomic) {
String label = "client_raw_put"; String label = "client_raw_put";
Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(label).startTimer(); Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(label).startTimer();
try { try {
@ -119,7 +134,7 @@ public class RawKVClient implements AutoCloseable {
while (true) { while (true) {
RegionStoreClient client = clientBuilder.build(key); RegionStoreClient client = clientBuilder.build(key);
try { try {
client.rawPut(backOffer, key, value, ttl); client.rawPut(backOffer, key, value, ttl, atomic);
RAW_REQUEST_SUCCESS.labels(label).inc(); RAW_REQUEST_SUCCESS.labels(label).inc();
return; return;
} catch (final TiKVException e) { } catch (final TiKVException e) {
@ -528,6 +543,19 @@ public class RawKVClient implements AutoCloseable {
* @param key raw key to be deleted * @param key raw key to be deleted
*/ */
public void delete(ByteString key) { public void delete(ByteString key) {
delete(key, false);
}
/**
* Delete a raw key-value pair from TiKV if key exists. This API is atomic.
*
* @param key raw key to be deleted
*/
public void deleteAtomic(ByteString key) {
delete(key, true);
}
private void delete(ByteString key, boolean atomic) {
String label = "client_raw_delete"; String label = "client_raw_delete";
Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(label).startTimer(); Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(label).startTimer();
try { try {
@ -535,7 +563,7 @@ public class RawKVClient implements AutoCloseable {
while (true) { while (true) {
RegionStoreClient client = clientBuilder.build(key); RegionStoreClient client = clientBuilder.build(key);
try { try {
client.rawDelete(defaultBackOff(), key); client.rawDelete(defaultBackOff(), key, atomic);
RAW_REQUEST_SUCCESS.labels(label).inc(); RAW_REQUEST_SUCCESS.labels(label).inc();
return; return;
} catch (final TiKVException e) { } catch (final TiKVException e) {