Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[to #734] add time unit for ttl param #646

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/main/java/org/tikv/raw/RawKVClientBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.tikv.common.TiSession;
import org.tikv.common.util.Pair;
import org.tikv.common.util.ScanOption;
import org.tikv.kvproto.Kvrpcpb;

public interface RawKVClientBase extends AutoCloseable {

// https://www.github.com/pingcap/tidb/blob/master/store/tikv/rawkv.go
int MAX_RAW_SCAN_LIMIT = 10240;
int MAX_RAW_BATCH_LIMIT = 1024;
Expand All @@ -51,6 +53,20 @@ public interface RawKVClientBase extends AutoCloseable {
*/
void put(ByteString key, ByteString value, long ttl);

/**
* Put a raw key-value pair to TiKV
*
* @see #put(ByteString, ByteString, long)
* @param key raw key
* @param value raw value
* @param duration the duration of the key, 0 means the key will never be outdated
* @param timeUnit the time unit of duration
*/
default void put(ByteString key, ByteString value, long duration, TimeUnit timeUnit) {
Long seconds = timeUnit.toSeconds(duration);
put(key, value, seconds);
}

/**
* Put a key-value pair if it does not exist. This API is atomic.
*
Expand All @@ -76,6 +92,25 @@ public interface RawKVClientBase extends AutoCloseable {
*/
Optional<ByteString> putIfAbsent(ByteString key, ByteString value, long ttl);

/**
* Put a key-value pair with TTL if it does not exist. This API is atomic.
*
* <p>To use this API, please enable `tikv.enable_atomic_for_cas`.
*
* @see #putIfAbsent(ByteString, ByteString, long)
* @param key key
* @param value value
* @param duration duration of key, 0 means the key will never be outdated.
* @param timeUnit the time unit of duration
* @return a ByteString. returns Optional.EMPTY if the value is written successfully. returns the
* previous key if the value already exists, and does not write to TiKV.
*/
default Optional<ByteString> putIfAbsent(
ByteString key, ByteString value, long duration, TimeUnit timeUnit) {
Long seconds = timeUnit.toSeconds(duration);
return putIfAbsent(key, value, seconds);
}

/**
* Put a key-value pair if the prevValue matched the value in TiKV. This API is atomic.
*
Expand All @@ -97,6 +132,27 @@ public interface RawKVClientBase extends AutoCloseable {
*/
void compareAndSet(ByteString key, Optional<ByteString> prevValue, ByteString value, long ttl);

/**
* pair if the prevValue matched the value in TiKV. This API is atomic.
*
* <p>To use this API, please enable `tikv.enable_atomic_for_cas`.
*
* @see #compareAndSet(ByteString, Optional, ByteString, long)
* @param key key
* @param value value
* @param duration duration of key , 0 means the key will never be outdated.
* @param timeUnit time unit of duration
*/
default void compareAndSet(
ByteString key,
Optional<ByteString> prevValue,
ByteString value,
long duration,
TimeUnit timeUnit) {
long seconds = timeUnit.toSeconds(duration);
compareAndSet(key, prevValue, value, seconds);
}

/**
* Put a set of raw key-value pair to TiKV.
*
Expand All @@ -112,6 +168,19 @@ public interface RawKVClientBase extends AutoCloseable {
*/
void batchPut(Map<ByteString, ByteString> kvPairs, long ttl);

/**
* Put a set of raw key-value pair to TiKV.
*
* @see #batchPut(Map, long)
* @param kvPairs kvPairs
* @param duration the duration of keys to be put, 0 means the keys will never be outdated
* @param timeUnit time unit of duration
*/
default void batchPut(Map<ByteString, ByteString> kvPairs, long duration, TimeUnit timeUnit) {
long seconds = timeUnit.toSeconds(duration);
batchPut(kvPairs, seconds);
}

/**
* Get a raw key-value pair from TiKV if key exists
*
Expand Down
13 changes: 8 additions & 5 deletions src/test/java/org/tikv/raw/CASTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.google.protobuf.ByteString;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -34,6 +35,7 @@
import org.tikv.common.exception.RawCASConflictException;

public class CASTest extends BaseRawKVTest {

private RawKVClient client;
private boolean initialized;
private static final Logger logger = LoggerFactory.getLogger(RawKVClientTest.class);
Expand Down Expand Up @@ -81,21 +83,22 @@ public void rawCASTest() {

@Test
public void rawPutIfAbsentTest() {
long ttl = 10;
long duration = 10;
TimeUnit timeUnit = TimeUnit.SECONDS;
ByteString key = ByteString.copyFromUtf8("key_atomic");
ByteString value = ByteString.copyFromUtf8("value");
ByteString value2 = ByteString.copyFromUtf8("value2");
client.delete(key);
Optional<ByteString> res1 = client.putIfAbsent(key, value, ttl);
Optional<ByteString> res1 = client.putIfAbsent(key, value, duration, timeUnit);
assertFalse(res1.isPresent());
Optional<ByteString> res2 = client.putIfAbsent(key, value2, ttl);
Optional<ByteString> res2 = client.putIfAbsent(key, value2, duration, timeUnit);
assertEquals(res2.get(), value);
try {
Thread.sleep(ttl * 1000 + 100);
Thread.sleep(duration * 1000 + 100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
Optional<ByteString> res3 = client.putIfAbsent(key, value, ttl);
Optional<ByteString> res3 = client.putIfAbsent(key, value, duration, timeUnit);
assertFalse(res3.isPresent());
}
}
3 changes: 2 additions & 1 deletion src/test/java/org/tikv/raw/RawKVClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.tikv.kvproto.Kvrpcpb.KvPair;

public class RawKVClientTest extends BaseRawKVTest {

private static final String RAW_PREFIX = "raw_\u0001_";
private static final int KEY_POOL_SIZE = 1000000;
private static final int TEST_CASES = 10000;
Expand Down Expand Up @@ -143,7 +144,7 @@ public void getKeyTTLTest() {
long ttl = 10;
ByteString key = ByteString.copyFromUtf8("key_ttl");
ByteString value = ByteString.copyFromUtf8("value");
client.put(key, value, ttl);
client.put(key, value, ttl, TimeUnit.SECONDS);
for (int i = 0; i < 9; i++) {
Optional<Long> t = client.getKeyTTL(key);
logger.info("current ttl of key is " + t.orElse(null));
Expand Down