>() {};
return objectMapper.readValue(configJSON, typeRef);
diff --git a/src/main/java/org/tikv/common/TiSession.java b/src/main/java/org/tikv/common/TiSession.java
index 449d8765645..0838932ade6 100644
--- a/src/main/java/org/tikv/common/TiSession.java
+++ b/src/main/java/org/tikv/common/TiSession.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common;
@@ -24,11 +26,18 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.tikv.common.apiversion.RequestKeyCodec;
+import org.tikv.common.apiversion.RequestKeyV1RawCodec;
+import org.tikv.common.apiversion.RequestKeyV1TxnCodec;
+import org.tikv.common.apiversion.RequestKeyV2RawCodec;
+import org.tikv.common.apiversion.RequestKeyV2TxnCodec;
import org.tikv.common.catalog.Catalog;
import org.tikv.common.exception.TiKVException;
import org.tikv.common.importer.ImporterStoreClient;
@@ -37,25 +46,36 @@
import org.tikv.common.meta.TiTimestamp;
import org.tikv.common.region.RegionManager;
import org.tikv.common.region.RegionStoreClient;
-import org.tikv.common.region.RegionStoreClient.RegionStoreClientBuilder;
import org.tikv.common.region.TiRegion;
import org.tikv.common.region.TiStore;
-import org.tikv.common.util.*;
+import org.tikv.common.util.BackOffFunction;
+import org.tikv.common.util.BackOffer;
+import org.tikv.common.util.ChannelFactory;
+import org.tikv.common.util.ConcreteBackOffer;
+import org.tikv.common.util.Pair;
+import org.tikv.kvproto.Errorpb;
import org.tikv.kvproto.ImportSstpb;
import org.tikv.kvproto.Metapb;
+import org.tikv.kvproto.Pdpb;
import org.tikv.raw.RawKVClient;
+import org.tikv.raw.SmartRawKVClient;
+import org.tikv.service.failsafe.CircuitBreaker;
+import org.tikv.service.failsafe.CircuitBreakerImpl;
import org.tikv.txn.KVClient;
import org.tikv.txn.TxnKVClient;
/**
* TiSession is the holder for PD Client, Store pdClient and PD Cache All sessions share common
- * region store connection pool but separated PD conn and cache for better concurrency TiSession is
- * thread-safe but it's also recommended to have multiple session avoiding lock contention
+ * region store connection pool but separated PD conn and cache for better concurrency
+ *
+ * TiSession is thread-safe but it's also recommended to have multiple session avoiding lock
+ * contention
*/
public class TiSession implements AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(TiSession.class);
private static final Map sessionCachedMap = new HashMap<>();
private final TiConfiguration conf;
+ private final RequestKeyCodec keyCodec;
private final ChannelFactory channelFactory;
// below object creation is either heavy or making connection (pd), pending for lazy loading
private volatile PDClient client;
@@ -68,35 +88,184 @@ public class TiSession implements AutoCloseable {
private volatile ExecutorService batchScanThreadPool;
private volatile ExecutorService deleteRangeThreadPool;
private volatile RegionManager regionManager;
- private volatile boolean enableGrpcForward;
+ private final boolean enableGrpcForward;
private volatile RegionStoreClient.RegionStoreClientBuilder clientBuilder;
private volatile ImporterStoreClient.ImporterStoreClientBuilder importerClientBuilder;
private volatile boolean isClosed = false;
private volatile SwitchTiKVModeClient switchTiKVModeClient;
private final MetricsServer metricsServer;
+ private final CircuitBreaker circuitBreaker;
private static final int MAX_SPLIT_REGION_STACK_DEPTH = 6;
+ static {
+ logger.info("Welcome to TiKV Java Client {}", getVersionInfo());
+ }
+
+ private static class VersionInfo {
+
+ private final String buildVersion;
+ private final String commitHash;
+
+ public VersionInfo(String buildVersion, String commitHash) {
+ this.buildVersion = buildVersion;
+ this.commitHash = commitHash;
+ }
+
+ @Override
+ public String toString() {
+ return buildVersion + "@" + commitHash;
+ }
+ }
+
public TiSession(TiConfiguration conf) {
+ // may throw org.tikv.common.MetricsServer - http server not up
+ // put it at the beginning of this function to avoid unclosed Thread
+ this.metricsServer = MetricsServer.getInstance(conf);
+
this.conf = conf;
- this.channelFactory =
- conf.isTlsEnable()
- ? new ChannelFactory(
+
+ if (conf.getApiVersion().isV1()) {
+ if (conf.isRawKVMode()) {
+ keyCodec = new RequestKeyV1RawCodec();
+ } else {
+ keyCodec = new RequestKeyV1TxnCodec();
+ }
+ } else {
+ if (conf.isRawKVMode()) {
+ keyCodec = new RequestKeyV2RawCodec();
+ } else {
+ keyCodec = new RequestKeyV2TxnCodec();
+ }
+ }
+
+ if (conf.isTlsEnable()) {
+ if (conf.isJksEnable()) {
+ this.channelFactory =
+ new ChannelFactory(
conf.getMaxFrameSize(),
conf.getKeepaliveTime(),
conf.getKeepaliveTimeout(),
+ conf.getIdleTimeout(),
+ conf.getConnRecycleTimeInSeconds(),
+ conf.getCertReloadIntervalInSeconds(),
+ conf.getJksKeyPath(),
+ conf.getJksKeyPassword(),
+ conf.getJksTrustPath(),
+ conf.getJksTrustPassword());
+ } else {
+ this.channelFactory =
+ new ChannelFactory(
+ conf.getMaxFrameSize(),
+ conf.getKeepaliveTime(),
+ conf.getKeepaliveTimeout(),
+ conf.getIdleTimeout(),
+ conf.getConnRecycleTimeInSeconds(),
+ conf.getCertReloadIntervalInSeconds(),
conf.getTrustCertCollectionFile(),
conf.getKeyCertChainFile(),
- conf.getKeyFile())
- : new ChannelFactory(
- conf.getMaxFrameSize(), conf.getKeepaliveTime(), conf.getKeepaliveTimeout());
+ conf.getKeyFile());
+ }
+ } else {
+ this.channelFactory =
+ new ChannelFactory(
+ conf.getMaxFrameSize(),
+ conf.getKeepaliveTime(),
+ conf.getKeepaliveTimeout(),
+ conf.getIdleTimeout());
+ }
+
+ this.client = PDClient.createRaw(conf, keyCodec, channelFactory);
+ if (conf.getApiVersion().isV2() && !StoreVersion.minTiKVVersion(Version.API_V2, client)) {
+ throw new IllegalStateException(
+ "With API v2, store versions should not older than " + Version.API_V2);
+ }
- this.client = PDClient.createRaw(conf, channelFactory);
this.enableGrpcForward = conf.getEnableGrpcForward();
- this.metricsServer = MetricsServer.getInstance(conf);
if (this.enableGrpcForward) {
logger.info("enable grpc forward for high available");
}
- logger.info("TiSession initialized in " + conf.getKvMode() + " mode");
+ if (conf.isWarmUpEnable() && conf.isRawKVMode()) {
+ warmUp();
+ }
+ this.circuitBreaker = new CircuitBreakerImpl(conf, client.getClusterId());
+ logger.info(
+ "TiSession initialized in "
+ + conf.getKvMode()
+ + " mode in API version: "
+ + conf.getApiVersion());
+ }
+
+ private static VersionInfo getVersionInfo() {
+ VersionInfo info;
+ try {
+ final Properties properties = new Properties();
+ properties.load(TiSession.class.getClassLoader().getResourceAsStream("git.properties"));
+ String version = properties.getProperty("git.build.version");
+ String commitHash = properties.getProperty("git.commit.id.full");
+ info = new VersionInfo(version, commitHash);
+ } catch (Exception e) {
+ logger.info("Fail to read package info: " + e.getMessage());
+ info = new VersionInfo("unknown", "unknown");
+ }
+ return info;
+ }
+
+ @VisibleForTesting
+ public synchronized void warmUp() {
+ long warmUpStartTime = System.nanoTime();
+ BackOffer backOffer = ConcreteBackOffer.newRawKVBackOff(getPDClient().getClusterId());
+ try {
+ // let JVM ClassLoader load gRPC error related classes
+ // this operation may cost 100ms
+ Errorpb.Error.newBuilder().setNotLeader(Errorpb.NotLeader.newBuilder().build()).build();
+
+ this.client = getPDClient();
+ this.regionManager = getRegionManager();
+ List stores = this.client.getAllStores(backOffer);
+ // warm up store cache
+ for (Metapb.Store store : stores) {
+ this.regionManager.updateStore(
+ null, new TiStore(this.client.getStore(backOffer, store.getId())));
+ }
+
+ // use scan region to load region cache with limit
+ ByteString startKey = ByteString.EMPTY;
+ do {
+ List regions =
+ regionManager.scanRegions(
+ backOffer, startKey, ByteString.EMPTY, conf.getScanRegionsLimit());
+ if (regions == null || regions.isEmpty()) {
+ // something went wrong, but the warm-up process could continue
+ break;
+ }
+ for (Pdpb.Region region : regions) {
+ regionManager.insertRegionToCache(
+ regionManager.createRegion(region.getRegion(), backOffer));
+ }
+ startKey = regions.get(regions.size() - 1).getRegion().getEndKey();
+ } while (!startKey.isEmpty());
+
+ try (RawKVClient rawKVClient = createRawClient()) {
+ ByteString exampleKey = ByteString.EMPTY;
+ Optional prev = rawKVClient.get(exampleKey);
+ if (prev.isPresent()) {
+ rawKVClient.delete(exampleKey);
+ rawKVClient.putIfAbsent(exampleKey, prev.get());
+ rawKVClient.put(exampleKey, prev.get());
+ } else {
+ rawKVClient.putIfAbsent(exampleKey, ByteString.EMPTY);
+ rawKVClient.put(exampleKey, ByteString.EMPTY);
+ rawKVClient.delete(exampleKey);
+ }
+ }
+ } catch (Exception e) {
+ // ignore error
+ logger.info("warm up fails, ignored ", e);
+ } finally {
+ logger.info(
+ String.format(
+ "warm up duration %d ms", (System.nanoTime() - warmUpStartTime) / 1_000_000));
+ }
}
@VisibleForTesting
@@ -121,17 +290,18 @@ public static TiSession getInstance(TiConfiguration conf) {
public RawKVClient createRawClient() {
checkIsClosed();
- RegionStoreClientBuilder builder =
- new RegionStoreClientBuilder(conf, channelFactory, this.getRegionManager(), client);
- return new RawKVClient(this, builder);
+ return new RawKVClient(this, this.getRegionStoreClientBuilder());
+ }
+
+ public SmartRawKVClient createSmartRawClient() {
+ RawKVClient rawKVClient = createRawClient();
+ return new SmartRawKVClient(rawKVClient, circuitBreaker);
}
public KVClient createKVClient() {
checkIsClosed();
- RegionStoreClientBuilder builder =
- new RegionStoreClientBuilder(conf, channelFactory, this.getRegionManager(), client);
- return new KVClient(conf, builder, this);
+ return new KVClient(this.conf, this.getRegionStoreClientBuilder(), this);
}
public TxnKVClient createTxnClient() {
@@ -143,18 +313,19 @@ public TxnKVClient createTxnClient() {
public RegionStoreClient.RegionStoreClientBuilder getRegionStoreClientBuilder() {
checkIsClosed();
- RegionStoreClient.RegionStoreClientBuilder res = clientBuilder;
- if (res == null) {
- synchronized (this) {
- if (clientBuilder == null) {
- clientBuilder =
- new RegionStoreClient.RegionStoreClientBuilder(
- conf, this.channelFactory, this.getRegionManager(), this.getPDClient());
- }
- res = clientBuilder;
+ if (this.clientBuilder != null) {
+ return this.clientBuilder;
+ }
+
+ // lazily create the clientBuilder for the current TiSession
+ synchronized (this) {
+ if (this.clientBuilder == null) {
+ this.clientBuilder =
+ new RegionStoreClient.RegionStoreClientBuilder(
+ this.conf, this.channelFactory, this.getRegionManager(), this.getPDClient());
}
}
- return res;
+ return this.clientBuilder;
}
public ImporterStoreClient.ImporterStoreClientBuilder getImporterRegionStoreClientBuilder() {
@@ -189,7 +360,8 @@ public TiConfiguration getConf() {
public TiTimestamp getTimestamp() {
checkIsClosed();
- return getPDClient().getTimestamp(ConcreteBackOffer.newTsoBackOff());
+ return getPDClient()
+ .getTimestamp(ConcreteBackOffer.newTsoBackOff(getPDClient().getClusterId()));
}
public Snapshot createSnapshot() {
@@ -211,7 +383,7 @@ public PDClient getPDClient() {
if (res == null) {
synchronized (this) {
if (client == null) {
- client = PDClient.createRaw(this.getConf(), channelFactory);
+ client = PDClient.createRaw(this.getConf(), keyCodec, channelFactory);
}
res = client;
}
@@ -446,13 +618,16 @@ public void splitRegionAndScatter(
.stream()
.map(k -> Key.toRawKey(k).toByteString())
.collect(Collectors.toList()),
- ConcreteBackOffer.newCustomBackOff(splitRegionBackoffMS));
+ ConcreteBackOffer.newCustomBackOff(splitRegionBackoffMS, getPDClient().getClusterId()));
// scatter region
for (Metapb.Region newRegion : newRegions) {
try {
getPDClient()
- .scatterRegion(newRegion, ConcreteBackOffer.newCustomBackOff(scatterRegionBackoffMS));
+ .scatterRegion(
+ newRegion,
+ ConcreteBackOffer.newCustomBackOff(
+ scatterRegionBackoffMS, getPDClient().getClusterId()));
} catch (Exception e) {
logger.warn(String.format("failed to scatter region: %d", newRegion.getId()), e);
}
@@ -469,7 +644,9 @@ public void splitRegionAndScatter(
return;
}
getPDClient()
- .waitScatterRegionFinish(newRegion, ConcreteBackOffer.newCustomBackOff((int) remainMS));
+ .waitScatterRegionFinish(
+ newRegion,
+ ConcreteBackOffer.newCustomBackOff((int) remainMS, getPDClient().getClusterId()));
}
} else {
logger.info("skip to wait scatter region finish");
@@ -589,6 +766,10 @@ private synchronized void shutdown(boolean now) throws Exception {
if (metricsServer != null) {
metricsServer.close();
}
+
+ if (circuitBreaker != null) {
+ circuitBreaker.close();
+ }
}
if (now) {
diff --git a/src/main/java/org/tikv/common/Utils.java b/src/main/java/org/tikv/common/Utils.java
index a17a02c2764..dd541ad055f 100644
--- a/src/main/java/org/tikv/common/Utils.java
+++ b/src/main/java/org/tikv/common/Utils.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2021 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common;
diff --git a/src/main/java/org/tikv/common/Version.java b/src/main/java/org/tikv/common/Version.java
index 1b648236964..1c39580eed7 100644
--- a/src/main/java/org/tikv/common/Version.java
+++ b/src/main/java/org/tikv/common/Version.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -25,4 +25,6 @@ public class Version {
public static final String RESOLVE_LOCK_V4 = "4.0.0";
public static final String BATCH_WRITE = "3.0.14";
+
+ public static final String API_V2 = "6.1.0";
}
diff --git a/src/main/java/org/tikv/common/apiversion/CodecUtils.java b/src/main/java/org/tikv/common/apiversion/CodecUtils.java
new file mode 100644
index 00000000000..a2b0725be5f
--- /dev/null
+++ b/src/main/java/org/tikv/common/apiversion/CodecUtils.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2022 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.apiversion;
+
+import com.google.protobuf.ByteString;
+import org.tikv.common.codec.Codec.BytesCodec;
+import org.tikv.common.codec.CodecDataInput;
+import org.tikv.common.codec.CodecDataOutput;
+
+// TODO(iosmanthus): use ByteString.wrap to avoid once more copying.
+public class CodecUtils {
+ public static ByteString encode(ByteString key) {
+ CodecDataOutput cdo = new CodecDataOutput();
+ BytesCodec.writeBytes(cdo, key.toByteArray());
+ return cdo.toByteString();
+ }
+
+ public static ByteString decode(ByteString key) {
+ return ByteString.copyFrom(BytesCodec.readBytes(new CodecDataInput(key)));
+ }
+}
diff --git a/src/main/java/org/tikv/common/apiversion/RequestKeyCodec.java b/src/main/java/org/tikv/common/apiversion/RequestKeyCodec.java
new file mode 100644
index 00000000000..b70e660cd53
--- /dev/null
+++ b/src/main/java/org/tikv/common/apiversion/RequestKeyCodec.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2022 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.apiversion;
+
+import com.google.protobuf.ByteString;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.tikv.common.util.Pair;
+import org.tikv.kvproto.Kvrpcpb.KvPair;
+import org.tikv.kvproto.Kvrpcpb.Mutation;
+import org.tikv.kvproto.Metapb;
+import org.tikv.kvproto.Pdpb;
+
+public interface RequestKeyCodec {
+ ByteString encodeKey(ByteString key);
+
+ default List encodeKeys(List keys) {
+ return keys.stream().map(this::encodeKey).collect(Collectors.toList());
+ }
+
+ default List encodeMutations(List mutations) {
+ return mutations
+ .stream()
+ .map(mut -> Mutation.newBuilder().mergeFrom(mut).setKey(encodeKey(mut.getKey())).build())
+ .collect(Collectors.toList());
+ }
+
+ ByteString decodeKey(ByteString key);
+
+ default KvPair decodeKvPair(KvPair pair) {
+ return KvPair.newBuilder().mergeFrom(pair).setKey(decodeKey(pair.getKey())).build();
+ }
+
+ default List decodeKvPairs(List pairs) {
+ return pairs.stream().map(this::decodeKvPair).collect(Collectors.toList());
+ }
+
+ Pair encodeRange(ByteString start, ByteString end);
+
+ ByteString encodePdQuery(ByteString key);
+
+ Pair encodePdQueryRange(ByteString start, ByteString end);
+
+ Metapb.Region decodeRegion(Metapb.Region region);
+
+ default List decodePdRegions(List regions) {
+ return regions
+ .stream()
+ .map(
+ r ->
+ Pdpb.Region.newBuilder()
+ .mergeFrom(r)
+ .setRegion(this.decodeRegion(r.getRegion()))
+ .build())
+ .collect(Collectors.toList());
+ }
+}
diff --git a/src/main/java/org/tikv/common/apiversion/RequestKeyV1Codec.java b/src/main/java/org/tikv/common/apiversion/RequestKeyV1Codec.java
new file mode 100644
index 00000000000..9d9a92c6859
--- /dev/null
+++ b/src/main/java/org/tikv/common/apiversion/RequestKeyV1Codec.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2022 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.apiversion;
+
+import com.google.protobuf.ByteString;
+import java.util.List;
+import org.tikv.common.util.Pair;
+import org.tikv.kvproto.Kvrpcpb.KvPair;
+import org.tikv.kvproto.Kvrpcpb.Mutation;
+import org.tikv.kvproto.Metapb.Region;
+import org.tikv.kvproto.Pdpb;
+
+public class RequestKeyV1Codec implements RequestKeyCodec {
+ @Override
+ public ByteString encodeKey(ByteString key) {
+ return key;
+ }
+
+ @Override
+ public List encodeKeys(List keys) {
+ return keys;
+ }
+
+ @Override
+ public List encodeMutations(List mutations) {
+ return mutations;
+ }
+
+ @Override
+ public ByteString decodeKey(ByteString key) {
+ return key;
+ }
+
+ @Override
+ public KvPair decodeKvPair(KvPair pair) {
+ return pair;
+ }
+
+ @Override
+ public List decodeKvPairs(List pairs) {
+ return pairs;
+ }
+
+ @Override
+ public Pair encodeRange(ByteString start, ByteString end) {
+ return Pair.create(start, end);
+ }
+
+ @Override
+ public ByteString encodePdQuery(ByteString key) {
+ return key;
+ }
+
+ @Override
+ public Pair encodePdQueryRange(ByteString start, ByteString end) {
+ return Pair.create(start, end);
+ }
+
+ @Override
+ public Region decodeRegion(Region region) {
+ return region;
+ }
+
+ @Override
+ public List decodePdRegions(List regions) {
+ return regions;
+ }
+}
diff --git a/src/main/java/org/tikv/common/apiversion/RequestKeyV1RawCodec.java b/src/main/java/org/tikv/common/apiversion/RequestKeyV1RawCodec.java
new file mode 100644
index 00000000000..a72457d0bbe
--- /dev/null
+++ b/src/main/java/org/tikv/common/apiversion/RequestKeyV1RawCodec.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2022 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.apiversion;
+
+public class RequestKeyV1RawCodec extends RequestKeyV1Codec implements RequestKeyCodec {
+ public RequestKeyV1RawCodec() {}
+}
diff --git a/src/main/java/org/tikv/common/apiversion/RequestKeyV1TxnCodec.java b/src/main/java/org/tikv/common/apiversion/RequestKeyV1TxnCodec.java
new file mode 100644
index 00000000000..ea3949ddb0c
--- /dev/null
+++ b/src/main/java/org/tikv/common/apiversion/RequestKeyV1TxnCodec.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2022 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.apiversion;
+
+import com.google.protobuf.ByteString;
+import org.tikv.common.util.Pair;
+import org.tikv.kvproto.Metapb;
+
+public class RequestKeyV1TxnCodec extends RequestKeyV1Codec implements RequestKeyCodec {
+ public RequestKeyV1TxnCodec() {}
+
+ @Override
+ public ByteString encodePdQuery(ByteString key) {
+ return CodecUtils.encode(key);
+ }
+
+ @Override
+ public Pair encodePdQueryRange(ByteString start, ByteString end) {
+ if (!start.isEmpty()) {
+ start = CodecUtils.encode(start);
+ }
+
+ if (!end.isEmpty()) {
+ end = CodecUtils.encode(end);
+ }
+
+ return Pair.create(start, end);
+ }
+
+ @Override
+ public Metapb.Region decodeRegion(Metapb.Region region) {
+ Metapb.Region.Builder builder = Metapb.Region.newBuilder().mergeFrom(region);
+ ByteString start = region.getStartKey();
+ ByteString end = region.getEndKey();
+
+ if (!start.isEmpty()) {
+ start = CodecUtils.decode(start);
+ }
+
+ if (!end.isEmpty()) {
+ end = CodecUtils.decode(end);
+ }
+
+ return builder.setStartKey(start).setEndKey(end).build();
+ }
+}
diff --git a/src/main/java/org/tikv/common/apiversion/RequestKeyV2Codec.java b/src/main/java/org/tikv/common/apiversion/RequestKeyV2Codec.java
new file mode 100644
index 00000000000..ab86fb5e020
--- /dev/null
+++ b/src/main/java/org/tikv/common/apiversion/RequestKeyV2Codec.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2022 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.apiversion;
+
+import com.google.protobuf.ByteString;
+import org.tikv.common.util.Pair;
+import org.tikv.kvproto.Metapb;
+import org.tikv.kvproto.Metapb.Region;
+
+public class RequestKeyV2Codec implements RequestKeyCodec {
+ protected static final ByteString RAW_DEFAULT_PREFIX =
+ ByteString.copyFrom(new byte[] {'r', 0, 0, 0});
+ protected static final ByteString RAW_DEFAULT_END =
+ ByteString.copyFrom(new byte[] {'r', 0, 0, 1});
+ protected static final ByteString TXN_DEFAULT_PREFIX =
+ ByteString.copyFrom(new byte[] {'x', 0, 0, 0});
+ protected static final ByteString TXN_DEFAULT_END =
+ ByteString.copyFrom(new byte[] {'x', 0, 0, 1});
+ protected ByteString keyPrefix;
+ protected ByteString infiniteEndKey;
+
+ @Override
+ public ByteString encodeKey(ByteString key) {
+ return keyPrefix.concat(key);
+ }
+
+ @Override
+ public ByteString decodeKey(ByteString key) {
+ if (key.isEmpty()) {
+ return key;
+ }
+
+ if (!key.startsWith(keyPrefix)) {
+ throw new IllegalArgumentException("key corrupted, wrong prefix");
+ }
+
+ return key.substring(keyPrefix.size());
+ }
+
+ @Override
+ public Pair encodeRange(ByteString start, ByteString end) {
+ start = encodeKey(start);
+
+ end = end.isEmpty() ? infiniteEndKey : encodeKey(end);
+
+ return Pair.create(start, end);
+ }
+
+ @Override
+ public ByteString encodePdQuery(ByteString key) {
+ return CodecUtils.encode(encodeKey(key));
+ }
+
+ @Override
+ public Pair encodePdQueryRange(ByteString start, ByteString end) {
+ Pair range = encodeRange(start, end);
+ return Pair.create(CodecUtils.encode(range.first), CodecUtils.encode(range.second));
+ }
+
+ @Override
+ public Region decodeRegion(Region region) {
+ Metapb.Region.Builder builder = Metapb.Region.newBuilder().mergeFrom(region);
+
+ ByteString start = region.getStartKey();
+ ByteString end = region.getEndKey();
+
+ if (!start.isEmpty()) {
+ start = CodecUtils.decode(start);
+ }
+
+ if (!end.isEmpty()) {
+ end = CodecUtils.decode(end);
+ }
+
+ if (ByteString.unsignedLexicographicalComparator().compare(start, infiniteEndKey) >= 0
+ || (!end.isEmpty()
+ && ByteString.unsignedLexicographicalComparator().compare(end, keyPrefix) <= 0)) {
+ throw new IllegalArgumentException("region out of keyspace" + region.toString());
+ }
+
+ start = start.startsWith(keyPrefix) ? start.substring(keyPrefix.size()) : ByteString.EMPTY;
+ end = end.startsWith(keyPrefix) ? end.substring(keyPrefix.size()) : ByteString.EMPTY;
+
+ return builder.setStartKey(start).setEndKey(end).build();
+ }
+}
diff --git a/src/main/java/org/tikv/common/apiversion/RequestKeyV2RawCodec.java b/src/main/java/org/tikv/common/apiversion/RequestKeyV2RawCodec.java
new file mode 100644
index 00000000000..432f6d2e8a0
--- /dev/null
+++ b/src/main/java/org/tikv/common/apiversion/RequestKeyV2RawCodec.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2022 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.apiversion;
+
+public class RequestKeyV2RawCodec extends RequestKeyV2Codec {
+ public RequestKeyV2RawCodec() {
+ super();
+
+ this.keyPrefix = RAW_DEFAULT_PREFIX;
+ this.infiniteEndKey = RAW_DEFAULT_END;
+ }
+}
diff --git a/src/main/java/org/tikv/common/apiversion/RequestKeyV2TxnCodec.java b/src/main/java/org/tikv/common/apiversion/RequestKeyV2TxnCodec.java
new file mode 100644
index 00000000000..3690a46cf76
--- /dev/null
+++ b/src/main/java/org/tikv/common/apiversion/RequestKeyV2TxnCodec.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2022 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.apiversion;
+
+public class RequestKeyV2TxnCodec extends RequestKeyV2Codec {
+ public RequestKeyV2TxnCodec() {
+ super();
+
+ this.keyPrefix = TXN_DEFAULT_PREFIX;
+ this.infiniteEndKey = TXN_DEFAULT_END;
+ }
+}
diff --git a/src/main/java/org/tikv/common/catalog/Catalog.java b/src/main/java/org/tikv/common/catalog/Catalog.java
index dd8fb027f83..56f70c9388c 100644
--- a/src/main/java/org/tikv/common/catalog/Catalog.java
+++ b/src/main/java/org/tikv/common/catalog/Catalog.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.catalog;
diff --git a/src/main/java/org/tikv/common/catalog/CatalogTransaction.java b/src/main/java/org/tikv/common/catalog/CatalogTransaction.java
index e28335cc01a..8fcdee03f01 100644
--- a/src/main/java/org/tikv/common/catalog/CatalogTransaction.java
+++ b/src/main/java/org/tikv/common/catalog/CatalogTransaction.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.catalog;
@@ -38,6 +40,7 @@
public class CatalogTransaction {
protected static final Logger logger = LoggerFactory.getLogger(CatalogTransaction.class);
+ private static final ObjectMapper mapper = new ObjectMapper();
private final Snapshot snapshot;
CatalogTransaction(Snapshot snapshot) {
@@ -49,7 +52,6 @@ public static T parseFromJson(ByteString json, Class cls) {
Objects.requireNonNull(cls, "cls is null");
logger.debug(String.format("Parse Json %s : %s", cls.getSimpleName(), json.toStringUtf8()));
- ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json.toStringUtf8(), cls);
} catch (JsonParseException | JsonMappingException e) {
diff --git a/src/main/java/org/tikv/common/codec/Codec.java b/src/main/java/org/tikv/common/codec/Codec.java
index ac7b7cb1714..642b6fc8c70 100644
--- a/src/main/java/org/tikv/common/codec/Codec.java
+++ b/src/main/java/org/tikv/common/codec/Codec.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/CodecDataInput.java b/src/main/java/org/tikv/common/codec/CodecDataInput.java
index 15076dfa0a5..3035c5fbf15 100644
--- a/src/main/java/org/tikv/common/codec/CodecDataInput.java
+++ b/src/main/java/org/tikv/common/codec/CodecDataInput.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/CodecDataInputLittleEndian.java b/src/main/java/org/tikv/common/codec/CodecDataInputLittleEndian.java
index 9864cb67c1c..fbc0466f23b 100644
--- a/src/main/java/org/tikv/common/codec/CodecDataInputLittleEndian.java
+++ b/src/main/java/org/tikv/common/codec/CodecDataInputLittleEndian.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/CodecDataOutput.java b/src/main/java/org/tikv/common/codec/CodecDataOutput.java
index 74a0f4a6ca8..28e8ba4fb8c 100644
--- a/src/main/java/org/tikv/common/codec/CodecDataOutput.java
+++ b/src/main/java/org/tikv/common/codec/CodecDataOutput.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/CodecDataOutputLittleEndian.java b/src/main/java/org/tikv/common/codec/CodecDataOutputLittleEndian.java
index e399c0ea730..bf10b6d63f2 100644
--- a/src/main/java/org/tikv/common/codec/CodecDataOutputLittleEndian.java
+++ b/src/main/java/org/tikv/common/codec/CodecDataOutputLittleEndian.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/KeyUtils.java b/src/main/java/org/tikv/common/codec/KeyUtils.java
index cc10ab6f935..72e94c8f329 100644
--- a/src/main/java/org/tikv/common/codec/KeyUtils.java
+++ b/src/main/java/org/tikv/common/codec/KeyUtils.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/MetaCodec.java b/src/main/java/org/tikv/common/codec/MetaCodec.java
index 3be4a9695b1..268724f31ac 100644
--- a/src/main/java/org/tikv/common/codec/MetaCodec.java
+++ b/src/main/java/org/tikv/common/codec/MetaCodec.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/MyDecimal.java b/src/main/java/org/tikv/common/codec/MyDecimal.java
index 55d265e3ba7..a149ba5e2c2 100644
--- a/src/main/java/org/tikv/common/codec/MyDecimal.java
+++ b/src/main/java/org/tikv/common/codec/MyDecimal.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/RowDecoderV2.java b/src/main/java/org/tikv/common/codec/RowDecoderV2.java
index 01fda2615af..3b9ae4aabc4 100644
--- a/src/main/java/org/tikv/common/codec/RowDecoderV2.java
+++ b/src/main/java/org/tikv/common/codec/RowDecoderV2.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/RowEncoderV2.java b/src/main/java/org/tikv/common/codec/RowEncoderV2.java
index 7fb723d9cab..794f40ebe28 100644
--- a/src/main/java/org/tikv/common/codec/RowEncoderV2.java
+++ b/src/main/java/org/tikv/common/codec/RowEncoderV2.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/RowV2.java b/src/main/java/org/tikv/common/codec/RowV2.java
index c91ab90d1c5..6893894a7bd 100644
--- a/src/main/java/org/tikv/common/codec/RowV2.java
+++ b/src/main/java/org/tikv/common/codec/RowV2.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
@@ -145,7 +147,7 @@ private int binarySearch(int i, int j, long colID) {
if (this.large) {
v = this.colIDs32[h];
} else {
- v = this.colIDs[h];
+ v = this.colIDs[h] & 0xFF;
}
if (v < colID) {
i = h + 1;
diff --git a/src/main/java/org/tikv/common/codec/TableCodec.java b/src/main/java/org/tikv/common/codec/TableCodec.java
index ed835f5391d..c0d141d94d2 100644
--- a/src/main/java/org/tikv/common/codec/TableCodec.java
+++ b/src/main/java/org/tikv/common/codec/TableCodec.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/TableCodecV1.java b/src/main/java/org/tikv/common/codec/TableCodecV1.java
index f72022de287..b0a1811c054 100644
--- a/src/main/java/org/tikv/common/codec/TableCodecV1.java
+++ b/src/main/java/org/tikv/common/codec/TableCodecV1.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/codec/TableCodecV2.java b/src/main/java/org/tikv/common/codec/TableCodecV2.java
index d9fa8efc47b..1d90737fc43 100644
--- a/src/main/java/org/tikv/common/codec/TableCodecV2.java
+++ b/src/main/java/org/tikv/common/codec/TableCodecV2.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.codec;
diff --git a/src/main/java/org/tikv/common/columnar/BatchedTiChunkColumnVector.java b/src/main/java/org/tikv/common/columnar/BatchedTiChunkColumnVector.java
index 14a327c7079..3e6ffddb191 100644
--- a/src/main/java/org/tikv/common/columnar/BatchedTiChunkColumnVector.java
+++ b/src/main/java/org/tikv/common/columnar/BatchedTiChunkColumnVector.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar;
diff --git a/src/main/java/org/tikv/common/columnar/TiBlockColumnVector.java b/src/main/java/org/tikv/common/columnar/TiBlockColumnVector.java
index 0341447d5c8..2abc40af019 100644
--- a/src/main/java/org/tikv/common/columnar/TiBlockColumnVector.java
+++ b/src/main/java/org/tikv/common/columnar/TiBlockColumnVector.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar;
diff --git a/src/main/java/org/tikv/common/columnar/TiChunk.java b/src/main/java/org/tikv/common/columnar/TiChunk.java
index 8bfb8e9d49c..3d97bc839d6 100644
--- a/src/main/java/org/tikv/common/columnar/TiChunk.java
+++ b/src/main/java/org/tikv/common/columnar/TiChunk.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar;
diff --git a/src/main/java/org/tikv/common/columnar/TiChunkColumnVector.java b/src/main/java/org/tikv/common/columnar/TiChunkColumnVector.java
index 655b2cc8681..96e0b5c68a4 100644
--- a/src/main/java/org/tikv/common/columnar/TiChunkColumnVector.java
+++ b/src/main/java/org/tikv/common/columnar/TiChunkColumnVector.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar;
@@ -176,7 +178,7 @@ private long getLongFromBinary(int rowId) {
if (bytes.length == 0) return 0;
long result = 0;
for (byte b : bytes) {
- result = (result << 8) | b;
+ result = (result << 8) | (b & 0xff);
}
return result;
}
diff --git a/src/main/java/org/tikv/common/columnar/TiColumnVector.java b/src/main/java/org/tikv/common/columnar/TiColumnVector.java
index 4b56d506224..ba04578f142 100644
--- a/src/main/java/org/tikv/common/columnar/TiColumnVector.java
+++ b/src/main/java/org/tikv/common/columnar/TiColumnVector.java
@@ -1,18 +1,18 @@
/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
+ * Copyright 2021 TiKV Project Authors.
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar;
diff --git a/src/main/java/org/tikv/common/columnar/TiCoreTime.java b/src/main/java/org/tikv/common/columnar/TiCoreTime.java
index 299314714d9..9d3d9ff57f4 100644
--- a/src/main/java/org/tikv/common/columnar/TiCoreTime.java
+++ b/src/main/java/org/tikv/common/columnar/TiCoreTime.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar;
diff --git a/src/main/java/org/tikv/common/columnar/TiRowColumnVector.java b/src/main/java/org/tikv/common/columnar/TiRowColumnVector.java
index 9e7741166a8..09ebbcecec6 100644
--- a/src/main/java/org/tikv/common/columnar/TiRowColumnVector.java
+++ b/src/main/java/org/tikv/common/columnar/TiRowColumnVector.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/AutoGrowByteBuffer.java b/src/main/java/org/tikv/common/columnar/datatypes/AutoGrowByteBuffer.java
index 7975d2cd25e..28d9d9c5f88 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/AutoGrowByteBuffer.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/AutoGrowByteBuffer.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/CHType.java b/src/main/java/org/tikv/common/columnar/datatypes/CHType.java
index 3ec85c498d1..f0321dc2c93 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/CHType.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/CHType.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDate.java b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDate.java
index ca628121c18..fb433b1ebd7 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDate.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDate.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDateTime.java b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDateTime.java
index 1241a9b47c1..ceae03fa620 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDateTime.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDateTime.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDecimal.java b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDecimal.java
index 90e0f30d34c..410479fb6e0 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDecimal.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeDecimal.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeFixedString.java b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeFixedString.java
index ef39851c760..1856c7049ec 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeFixedString.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeFixedString.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeMyDate.java b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeMyDate.java
index 548ca27b5a4..ed70bcc0499 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeMyDate.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeMyDate.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeMyDateTime.java b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeMyDateTime.java
index ab41935c98e..37df40a4d92 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeMyDateTime.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeMyDateTime.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeNumber.java b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeNumber.java
index 7ee078f4410..eeea8e998b5 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeNumber.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeNumber.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeString.java b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeString.java
index bbaa542530a..76308ba8ffe 100644
--- a/src/main/java/org/tikv/common/columnar/datatypes/CHTypeString.java
+++ b/src/main/java/org/tikv/common/columnar/datatypes/CHTypeString.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.columnar.datatypes;
diff --git a/src/main/java/org/tikv/common/event/CacheInvalidateEvent.java b/src/main/java/org/tikv/common/event/CacheInvalidateEvent.java
index 1843de59567..ca7d73bac30 100644
--- a/src/main/java/org/tikv/common/event/CacheInvalidateEvent.java
+++ b/src/main/java/org/tikv/common/event/CacheInvalidateEvent.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.event;
@@ -95,6 +97,8 @@ public String toString() {
public enum CacheType implements Serializable {
REGION_STORE,
+ STORE,
+ REGION,
REQ_FAILED,
LEADER
}
diff --git a/src/main/java/org/tikv/common/exception/AllocateRowIDOverflowException.java b/src/main/java/org/tikv/common/exception/AllocateRowIDOverflowException.java
index 86ee5217d39..594c8fed8da 100644
--- a/src/main/java/org/tikv/common/exception/AllocateRowIDOverflowException.java
+++ b/src/main/java/org/tikv/common/exception/AllocateRowIDOverflowException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/CastingException.java b/src/main/java/org/tikv/common/exception/CastingException.java
index dde8062109e..a5cd35474ed 100644
--- a/src/main/java/org/tikv/common/exception/CastingException.java
+++ b/src/main/java/org/tikv/common/exception/CastingException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/CircuitBreakerOpenException.java b/src/main/java/org/tikv/common/exception/CircuitBreakerOpenException.java
new file mode 100644
index 00000000000..3a069f18223
--- /dev/null
+++ b/src/main/java/org/tikv/common/exception/CircuitBreakerOpenException.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.exception;
+
+public class CircuitBreakerOpenException extends RuntimeException {
+ public CircuitBreakerOpenException() {
+ super("Circuit Breaker Opened");
+ }
+}
diff --git a/src/main/java/org/tikv/common/exception/CodecException.java b/src/main/java/org/tikv/common/exception/CodecException.java
index db224430242..be4eb4f2c84 100644
--- a/src/main/java/org/tikv/common/exception/CodecException.java
+++ b/src/main/java/org/tikv/common/exception/CodecException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/ConvertNotSupportException.java b/src/main/java/org/tikv/common/exception/ConvertNotSupportException.java
index c00d3ed6c9c..bc77882f3a8 100644
--- a/src/main/java/org/tikv/common/exception/ConvertNotSupportException.java
+++ b/src/main/java/org/tikv/common/exception/ConvertNotSupportException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/ConvertOverflowException.java b/src/main/java/org/tikv/common/exception/ConvertOverflowException.java
index dad80c5a021..ecd66b6ed84 100644
--- a/src/main/java/org/tikv/common/exception/ConvertOverflowException.java
+++ b/src/main/java/org/tikv/common/exception/ConvertOverflowException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/DAGRequestException.java b/src/main/java/org/tikv/common/exception/DAGRequestException.java
index 8a2a9980199..36979181e94 100644
--- a/src/main/java/org/tikv/common/exception/DAGRequestException.java
+++ b/src/main/java/org/tikv/common/exception/DAGRequestException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/GrpcException.java b/src/main/java/org/tikv/common/exception/GrpcException.java
index 9f27a9ed04d..5d296789a55 100644
--- a/src/main/java/org/tikv/common/exception/GrpcException.java
+++ b/src/main/java/org/tikv/common/exception/GrpcException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/IgnoreUnsupportedTypeException.java b/src/main/java/org/tikv/common/exception/IgnoreUnsupportedTypeException.java
index c6e90f95ba3..2f07f422a6d 100644
--- a/src/main/java/org/tikv/common/exception/IgnoreUnsupportedTypeException.java
+++ b/src/main/java/org/tikv/common/exception/IgnoreUnsupportedTypeException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/InvalidCodecFormatException.java b/src/main/java/org/tikv/common/exception/InvalidCodecFormatException.java
index 6adb0b9cf89..797b5b60e96 100644
--- a/src/main/java/org/tikv/common/exception/InvalidCodecFormatException.java
+++ b/src/main/java/org/tikv/common/exception/InvalidCodecFormatException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/InvalidStoreException.java b/src/main/java/org/tikv/common/exception/InvalidStoreException.java
new file mode 100644
index 00000000000..7c0e705800a
--- /dev/null
+++ b/src/main/java/org/tikv/common/exception/InvalidStoreException.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.exception;
+
+public class InvalidStoreException extends TiKVException {
+
+ public InvalidStoreException(long storeId) {
+ super(String.format("Invalid storeId: %d", storeId));
+ }
+}
diff --git a/src/main/java/org/tikv/common/exception/KeyException.java b/src/main/java/org/tikv/common/exception/KeyException.java
index d4c9a25314b..22ddda982b9 100644
--- a/src/main/java/org/tikv/common/exception/KeyException.java
+++ b/src/main/java/org/tikv/common/exception/KeyException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/RawCASConflictException.java b/src/main/java/org/tikv/common/exception/RawCASConflictException.java
index a1b066beb44..1063d1696ea 100644
--- a/src/main/java/org/tikv/common/exception/RawCASConflictException.java
+++ b/src/main/java/org/tikv/common/exception/RawCASConflictException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2021 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/RegionException.java b/src/main/java/org/tikv/common/exception/RegionException.java
index 3a8dd97737f..f16267dc19f 100644
--- a/src/main/java/org/tikv/common/exception/RegionException.java
+++ b/src/main/java/org/tikv/common/exception/RegionException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/RegionTaskException.java b/src/main/java/org/tikv/common/exception/RegionTaskException.java
index 6e357a99d46..02b4caf1606 100644
--- a/src/main/java/org/tikv/common/exception/RegionTaskException.java
+++ b/src/main/java/org/tikv/common/exception/RegionTaskException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2018 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/SSTDecodeException.java b/src/main/java/org/tikv/common/exception/SSTDecodeException.java
new file mode 100644
index 00000000000..a66cf29afd1
--- /dev/null
+++ b/src/main/java/org/tikv/common/exception/SSTDecodeException.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.exception;
+
+public class SSTDecodeException extends RuntimeException {
+ public SSTDecodeException(Exception e) {
+ super(e);
+ }
+
+ public SSTDecodeException(String msg) {
+ super(msg);
+ }
+}
diff --git a/src/main/java/org/tikv/common/exception/SelectException.java b/src/main/java/org/tikv/common/exception/SelectException.java
index ea949329efc..884d25f3d7a 100644
--- a/src/main/java/org/tikv/common/exception/SelectException.java
+++ b/src/main/java/org/tikv/common/exception/SelectException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/TiBatchWriteException.java b/src/main/java/org/tikv/common/exception/TiBatchWriteException.java
index e6baba35602..dce7beb0ea7 100644
--- a/src/main/java/org/tikv/common/exception/TiBatchWriteException.java
+++ b/src/main/java/org/tikv/common/exception/TiBatchWriteException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/TiClientInternalException.java b/src/main/java/org/tikv/common/exception/TiClientInternalException.java
index 52c2fd07285..f207451a109 100644
--- a/src/main/java/org/tikv/common/exception/TiClientInternalException.java
+++ b/src/main/java/org/tikv/common/exception/TiClientInternalException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/TiDBConvertException.java b/src/main/java/org/tikv/common/exception/TiDBConvertException.java
index ab2dc7e21c0..4099ada7a24 100644
--- a/src/main/java/org/tikv/common/exception/TiDBConvertException.java
+++ b/src/main/java/org/tikv/common/exception/TiDBConvertException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/TiExpressionException.java b/src/main/java/org/tikv/common/exception/TiExpressionException.java
index e192f759837..70801c538d5 100644
--- a/src/main/java/org/tikv/common/exception/TiExpressionException.java
+++ b/src/main/java/org/tikv/common/exception/TiExpressionException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/TiInternalException.java b/src/main/java/org/tikv/common/exception/TiInternalException.java
index e00a68e1f21..a755978cdae 100644
--- a/src/main/java/org/tikv/common/exception/TiInternalException.java
+++ b/src/main/java/org/tikv/common/exception/TiInternalException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/TiKVException.java b/src/main/java/org/tikv/common/exception/TiKVException.java
index 365bb98b4f1..f9435904bd9 100644
--- a/src/main/java/org/tikv/common/exception/TiKVException.java
+++ b/src/main/java/org/tikv/common/exception/TiKVException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2018 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/TypeException.java b/src/main/java/org/tikv/common/exception/TypeException.java
index f75fd8cf1a0..53f2ec20a2b 100644
--- a/src/main/java/org/tikv/common/exception/TypeException.java
+++ b/src/main/java/org/tikv/common/exception/TypeException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/UnsupportedPartitionExprException.java b/src/main/java/org/tikv/common/exception/UnsupportedPartitionExprException.java
index d966b202bb1..26ecf5224bf 100644
--- a/src/main/java/org/tikv/common/exception/UnsupportedPartitionExprException.java
+++ b/src/main/java/org/tikv/common/exception/UnsupportedPartitionExprException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/UnsupportedSyntaxException.java b/src/main/java/org/tikv/common/exception/UnsupportedSyntaxException.java
index a43e617ce6a..9042109f274 100644
--- a/src/main/java/org/tikv/common/exception/UnsupportedSyntaxException.java
+++ b/src/main/java/org/tikv/common/exception/UnsupportedSyntaxException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/UnsupportedTypeException.java b/src/main/java/org/tikv/common/exception/UnsupportedTypeException.java
index 2be573eaf3a..37669778812 100644
--- a/src/main/java/org/tikv/common/exception/UnsupportedTypeException.java
+++ b/src/main/java/org/tikv/common/exception/UnsupportedTypeException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/exception/WriteConflictException.java b/src/main/java/org/tikv/common/exception/WriteConflictException.java
index 93e8196f988..856bf0e4001 100644
--- a/src/main/java/org/tikv/common/exception/WriteConflictException.java
+++ b/src/main/java/org/tikv/common/exception/WriteConflictException.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.exception;
diff --git a/src/main/java/org/tikv/common/expression/AggregateFunction.java b/src/main/java/org/tikv/common/expression/AggregateFunction.java
index 28f11370e4f..2abbd047b4f 100644
--- a/src/main/java/org/tikv/common/expression/AggregateFunction.java
+++ b/src/main/java/org/tikv/common/expression/AggregateFunction.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/ArithmeticBinaryExpression.java b/src/main/java/org/tikv/common/expression/ArithmeticBinaryExpression.java
index abe33340375..c30ae96c45b 100644
--- a/src/main/java/org/tikv/common/expression/ArithmeticBinaryExpression.java
+++ b/src/main/java/org/tikv/common/expression/ArithmeticBinaryExpression.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/Blocklist.java b/src/main/java/org/tikv/common/expression/Blocklist.java
index 8648a7d8af3..300697b3561 100644
--- a/src/main/java/org/tikv/common/expression/Blocklist.java
+++ b/src/main/java/org/tikv/common/expression/Blocklist.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
diff --git a/src/main/java/org/tikv/common/expression/ByItem.java b/src/main/java/org/tikv/common/expression/ByItem.java
index 392bc38a284..cf87be30b26 100644
--- a/src/main/java/org/tikv/common/expression/ByItem.java
+++ b/src/main/java/org/tikv/common/expression/ByItem.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/ColumnRef.java b/src/main/java/org/tikv/common/expression/ColumnRef.java
index cc4a9ac2261..61746cd2e11 100644
--- a/src/main/java/org/tikv/common/expression/ColumnRef.java
+++ b/src/main/java/org/tikv/common/expression/ColumnRef.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
@@ -121,9 +123,9 @@ public boolean equals(Object another) {
@Override
public int hashCode() {
if (isResolved()) {
- return Objects.hash(this.name, this.dataType);
+ return Objects.hash(this.name.toLowerCase(), this.dataType);
} else {
- return Objects.hashCode(name);
+ return Objects.hashCode(name.toLowerCase());
}
}
diff --git a/src/main/java/org/tikv/common/expression/ComparisonBinaryExpression.java b/src/main/java/org/tikv/common/expression/ComparisonBinaryExpression.java
index 0a292e93039..53fc75a7749 100644
--- a/src/main/java/org/tikv/common/expression/ComparisonBinaryExpression.java
+++ b/src/main/java/org/tikv/common/expression/ComparisonBinaryExpression.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/Constant.java b/src/main/java/org/tikv/common/expression/Constant.java
index 0a74423dd36..6a53d8b19e3 100644
--- a/src/main/java/org/tikv/common/expression/Constant.java
+++ b/src/main/java/org/tikv/common/expression/Constant.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/Expression.java b/src/main/java/org/tikv/common/expression/Expression.java
index 11f94834dc4..883a97ab4a9 100644
--- a/src/main/java/org/tikv/common/expression/Expression.java
+++ b/src/main/java/org/tikv/common/expression/Expression.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/ExpressionBlocklist.java b/src/main/java/org/tikv/common/expression/ExpressionBlocklist.java
index 8494dab9e16..392f799b31d 100644
--- a/src/main/java/org/tikv/common/expression/ExpressionBlocklist.java
+++ b/src/main/java/org/tikv/common/expression/ExpressionBlocklist.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/FuncCallExpr.java b/src/main/java/org/tikv/common/expression/FuncCallExpr.java
index 017e38bd78f..983bcb69241 100644
--- a/src/main/java/org/tikv/common/expression/FuncCallExpr.java
+++ b/src/main/java/org/tikv/common/expression/FuncCallExpr.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/FuncCallExprEval.java b/src/main/java/org/tikv/common/expression/FuncCallExprEval.java
index c88799c1ffd..621e15d4622 100644
--- a/src/main/java/org/tikv/common/expression/FuncCallExprEval.java
+++ b/src/main/java/org/tikv/common/expression/FuncCallExprEval.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/IsNull.java b/src/main/java/org/tikv/common/expression/IsNull.java
index 5ce177de5c6..90faffc51d6 100644
--- a/src/main/java/org/tikv/common/expression/IsNull.java
+++ b/src/main/java/org/tikv/common/expression/IsNull.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/LogicalBinaryExpression.java b/src/main/java/org/tikv/common/expression/LogicalBinaryExpression.java
index 30621b6fcff..bf75941fd98 100644
--- a/src/main/java/org/tikv/common/expression/LogicalBinaryExpression.java
+++ b/src/main/java/org/tikv/common/expression/LogicalBinaryExpression.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
// Generated by the protocol buffer compiler. DO NOT EDIT!
diff --git a/src/main/java/org/tikv/common/expression/Not.java b/src/main/java/org/tikv/common/expression/Not.java
index 6eb61d8c10d..d0ce88d4e21 100644
--- a/src/main/java/org/tikv/common/expression/Not.java
+++ b/src/main/java/org/tikv/common/expression/Not.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/PartitionPruner.java b/src/main/java/org/tikv/common/expression/PartitionPruner.java
index 72d5d736b13..e7c1aaf4d51 100644
--- a/src/main/java/org/tikv/common/expression/PartitionPruner.java
+++ b/src/main/java/org/tikv/common/expression/PartitionPruner.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/RangeColumnPartitionPruner.java b/src/main/java/org/tikv/common/expression/RangeColumnPartitionPruner.java
index d647ccccf97..dd145f0570a 100644
--- a/src/main/java/org/tikv/common/expression/RangeColumnPartitionPruner.java
+++ b/src/main/java/org/tikv/common/expression/RangeColumnPartitionPruner.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/RangePartitionPruner.java b/src/main/java/org/tikv/common/expression/RangePartitionPruner.java
index 78ce75b934f..1b4e62e6000 100644
--- a/src/main/java/org/tikv/common/expression/RangePartitionPruner.java
+++ b/src/main/java/org/tikv/common/expression/RangePartitionPruner.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/StringRegExpression.java b/src/main/java/org/tikv/common/expression/StringRegExpression.java
index 378188fa29e..c6a94a1941e 100644
--- a/src/main/java/org/tikv/common/expression/StringRegExpression.java
+++ b/src/main/java/org/tikv/common/expression/StringRegExpression.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression;
diff --git a/src/main/java/org/tikv/common/expression/TypeBlocklist.java b/src/main/java/org/tikv/common/expression/TypeBlocklist.java
index d74674dd67a..d72dece8b6f 100644
--- a/src/main/java/org/tikv/common/expression/TypeBlocklist.java
+++ b/src/main/java/org/tikv/common/expression/TypeBlocklist.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
diff --git a/src/main/java/org/tikv/common/expression/Visitor.java b/src/main/java/org/tikv/common/expression/Visitor.java
index 0befaf5d8a7..26e7ffc4c8d 100644
--- a/src/main/java/org/tikv/common/expression/Visitor.java
+++ b/src/main/java/org/tikv/common/expression/Visitor.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
diff --git a/src/main/java/org/tikv/common/expression/visitor/ColumnMatcher.java b/src/main/java/org/tikv/common/expression/visitor/ColumnMatcher.java
index 5c4411bcb46..a1f9f68b059 100644
--- a/src/main/java/org/tikv/common/expression/visitor/ColumnMatcher.java
+++ b/src/main/java/org/tikv/common/expression/visitor/ColumnMatcher.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2018 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/expression/visitor/DefaultVisitor.java b/src/main/java/org/tikv/common/expression/visitor/DefaultVisitor.java
index e8fac6466a5..9dfceae4ada 100644
--- a/src/main/java/org/tikv/common/expression/visitor/DefaultVisitor.java
+++ b/src/main/java/org/tikv/common/expression/visitor/DefaultVisitor.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/expression/visitor/IndexMatcher.java b/src/main/java/org/tikv/common/expression/visitor/IndexMatcher.java
index 00a5af314bd..343061effbd 100644
--- a/src/main/java/org/tikv/common/expression/visitor/IndexMatcher.java
+++ b/src/main/java/org/tikv/common/expression/visitor/IndexMatcher.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/expression/visitor/IndexRangeSetBuilder.java b/src/main/java/org/tikv/common/expression/visitor/IndexRangeSetBuilder.java
index 11d442a8f45..dcfdd1f53d1 100644
--- a/src/main/java/org/tikv/common/expression/visitor/IndexRangeSetBuilder.java
+++ b/src/main/java/org/tikv/common/expression/visitor/IndexRangeSetBuilder.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/expression/visitor/MetaResolver.java b/src/main/java/org/tikv/common/expression/visitor/MetaResolver.java
index c4a0cfdbc0e..6276eaa57af 100644
--- a/src/main/java/org/tikv/common/expression/visitor/MetaResolver.java
+++ b/src/main/java/org/tikv/common/expression/visitor/MetaResolver.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/expression/visitor/PartAndFilterExprRewriter.java b/src/main/java/org/tikv/common/expression/visitor/PartAndFilterExprRewriter.java
index 6d2e07fd527..3cc448f9e1f 100644
--- a/src/main/java/org/tikv/common/expression/visitor/PartAndFilterExprRewriter.java
+++ b/src/main/java/org/tikv/common/expression/visitor/PartAndFilterExprRewriter.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
@@ -26,9 +28,9 @@
* PartAndFilterExprRewriter takes partition expression as an input. Rewriting rule is based on the
* type of partition expression. 1. If partition expression is a columnRef, no rewriting will be
* performed. 2. If partition expression is year and the expression to be rewritten in the form of y
- * < '1995-10-10' then its right hand child will be replaced with "1995". 3. If partition expression
- * is year and the expression to be rewritten in the form of year(y) < '1995' then its left hand
- * child will be replaced with y.
+ * < '1995-10-10' then its right hand child will be replaced with "1995". 3. If partition
+ * expression is year and the expression to be rewritten in the form of year(y) < '1995' then its
+ * left hand child will be replaced with y.
*/
public class PartAndFilterExprRewriter extends DefaultVisitor {
private final Expression partExpr;
diff --git a/src/main/java/org/tikv/common/expression/visitor/ProtoConverter.java b/src/main/java/org/tikv/common/expression/visitor/ProtoConverter.java
index 8415d5e68a8..07b07f3c83c 100644
--- a/src/main/java/org/tikv/common/expression/visitor/ProtoConverter.java
+++ b/src/main/java/org/tikv/common/expression/visitor/ProtoConverter.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/expression/visitor/PrunedPartitionBuilder.java b/src/main/java/org/tikv/common/expression/visitor/PrunedPartitionBuilder.java
index 57a43a2dd37..d660394b92f 100644
--- a/src/main/java/org/tikv/common/expression/visitor/PrunedPartitionBuilder.java
+++ b/src/main/java/org/tikv/common/expression/visitor/PrunedPartitionBuilder.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/expression/visitor/PseudoCostCalculator.java b/src/main/java/org/tikv/common/expression/visitor/PseudoCostCalculator.java
index 1af00652eac..531e43694a8 100644
--- a/src/main/java/org/tikv/common/expression/visitor/PseudoCostCalculator.java
+++ b/src/main/java/org/tikv/common/expression/visitor/PseudoCostCalculator.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/expression/visitor/RangeSetBuilder.java b/src/main/java/org/tikv/common/expression/visitor/RangeSetBuilder.java
index 09a4b81ca10..a0e97777fc7 100644
--- a/src/main/java/org/tikv/common/expression/visitor/RangeSetBuilder.java
+++ b/src/main/java/org/tikv/common/expression/visitor/RangeSetBuilder.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/expression/visitor/SupportedExpressionValidator.java b/src/main/java/org/tikv/common/expression/visitor/SupportedExpressionValidator.java
index 926c80da1db..8e26663eebf 100644
--- a/src/main/java/org/tikv/common/expression/visitor/SupportedExpressionValidator.java
+++ b/src/main/java/org/tikv/common/expression/visitor/SupportedExpressionValidator.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.expression.visitor;
diff --git a/src/main/java/org/tikv/common/importer/ImporterClient.java b/src/main/java/org/tikv/common/importer/ImporterClient.java
index d0f17f304a0..6f45a9a4d17 100644
--- a/src/main/java/org/tikv/common/importer/ImporterClient.java
+++ b/src/main/java/org/tikv/common/importer/ImporterClient.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2021 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -29,8 +29,7 @@
import org.slf4j.LoggerFactory;
import org.tikv.common.TiConfiguration;
import org.tikv.common.TiSession;
-import org.tikv.common.codec.Codec;
-import org.tikv.common.codec.CodecDataOutput;
+import org.tikv.common.apiversion.RequestKeyCodec;
import org.tikv.common.exception.GrpcException;
import org.tikv.common.exception.RegionException;
import org.tikv.common.exception.TiKVException;
@@ -43,18 +42,19 @@
import org.tikv.common.util.Pair;
import org.tikv.kvproto.Errorpb.Error;
import org.tikv.kvproto.ImportSstpb;
+import org.tikv.kvproto.ImportSstpb.RawWriteBatch;
import org.tikv.kvproto.Metapb;
public class ImporterClient {
private static final Logger logger = LoggerFactory.getLogger(ImporterClient.class);
- private TiConfiguration tiConf;
- private TiSession tiSession;
- private ByteString uuid;
- private Key minKey;
- private Key maxKey;
+ private final TiConfiguration tiConf;
+ private final TiSession tiSession;
+ private final ByteString uuid;
+ private final Key minKey;
+ private final Key maxKey;
private TiRegion region;
- private Long ttl;
+ private final Long ttl;
private boolean deduplicate = false;
@@ -63,6 +63,8 @@ public class ImporterClient {
private List clientList;
private ImporterStoreClient clientLeader;
+ private final RequestKeyCodec codec;
+
public ImporterClient(
TiSession tiSession, ByteString uuid, Key minKey, Key maxKey, TiRegion region, Long ttl) {
this.uuid = uuid;
@@ -72,6 +74,7 @@ public ImporterClient(
this.maxKey = maxKey;
this.region = region;
this.ttl = ttl;
+ this.codec = tiSession.getPDClient().getCodec();
}
public boolean isDeduplicate() {
@@ -108,9 +111,9 @@ public void write(Iterator> iterator) throws TiKVEx
String.format("duplicate key found, key = %s", preKey.toStringUtf8()));
}
} else {
- pairs.add(
- ImportSstpb.Pair.newBuilder().setKey(pair.first).setValue(pair.second).build());
- totalBytes += (pair.first.size() + pair.second.size());
+ ByteString key = codec.encodeKey(pair.first);
+ pairs.add(ImportSstpb.Pair.newBuilder().setKey(key).setValue(pair.second).build());
+ totalBytes += (key.size() + pair.second.size());
preKey = pair.first;
}
}
@@ -137,19 +140,15 @@ public void write(Iterator> iterator) throws TiKVEx
private void init() {
long regionId = region.getId();
Metapb.RegionEpoch regionEpoch = region.getRegionEpoch();
+ Pair keyRange =
+ codec.encodePdQueryRange(minKey.toByteString(), maxKey.toByteString());
+
ImportSstpb.Range range =
- tiConf.isTxnKVMode()
- ? ImportSstpb.Range.newBuilder()
- .setStart(encode(minKey.toByteString()))
- .setEnd(encode(maxKey.toByteString()))
- .build()
- : ImportSstpb.Range.newBuilder()
- .setStart(minKey.toByteString())
- .setEnd(maxKey.toByteString())
- .build();
+ ImportSstpb.Range.newBuilder().setStart(keyRange.first).setEnd(keyRange.second).build();
sstMeta =
ImportSstpb.SSTMeta.newBuilder()
+ .setApiVersion(tiConf.getApiVersion().toPb())
.setUuid(uuid)
.setRegionId(regionId)
.setRegionEpoch(regionEpoch)
@@ -170,12 +169,6 @@ private void init() {
}
}
- private ByteString encode(ByteString key) {
- CodecDataOutput cdo = new CodecDataOutput();
- Codec.BytesCodec.writeBytes(cdo, key.toByteArray());
- return cdo.toByteString();
- }
-
private void startWrite() {
for (ImporterStoreClient client : clientList) {
client.startWrite();
@@ -216,11 +209,14 @@ private void writeBatch(List pairs) {
} else {
ImportSstpb.RawWriteBatch batch;
- if (ttl == null || ttl <= 0) {
- batch = ImportSstpb.RawWriteBatch.newBuilder().addAllPairs(pairs).build();
- } else {
- batch = ImportSstpb.RawWriteBatch.newBuilder().addAllPairs(pairs).setTtl(ttl).build();
+ RawWriteBatch.Builder batchBuilder = RawWriteBatch.newBuilder().addAllPairs(pairs);
+ if (ttl != null && ttl > 0) {
+ batchBuilder.setTtl(ttl);
+ }
+ if (tiConf.getApiVersion().isV2()) {
+ batchBuilder.setTs(tiSession.getTimestamp().getVersion());
}
+ batch = batchBuilder.build();
ImportSstpb.RawWriteRequest request =
ImportSstpb.RawWriteRequest.newBuilder().setBatch(batch).build();
@@ -259,7 +255,9 @@ private void ingest() throws GrpcException {
}
Object writeResponse = clientLeader.getWriteResponse();
- BackOffer backOffer = ConcreteBackOffer.newCustomBackOff(BackOffer.INGEST_BACKOFF);
+ BackOffer backOffer =
+ ConcreteBackOffer.newCustomBackOff(
+ BackOffer.INGEST_BACKOFF, tiSession.getPDClient().getClusterId());
ingestWithRetry(writeResponse, backOffer);
}
diff --git a/src/main/java/org/tikv/common/importer/ImporterStoreClient.java b/src/main/java/org/tikv/common/importer/ImporterStoreClient.java
index 87c57853608..4e652587dbc 100644
--- a/src/main/java/org/tikv/common/importer/ImporterStoreClient.java
+++ b/src/main/java/org/tikv/common/importer/ImporterStoreClient.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2021 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -41,17 +41,22 @@
import org.tikv.kvproto.Kvrpcpb;
public class ImporterStoreClient
- extends AbstractGRPCClient
+ extends AbstractGRPCClient<
+ ImportSSTGrpc.ImportSSTBlockingStub, ImportSSTGrpc.ImportSSTFutureStub>
implements StreamObserver {
private static final Logger logger = LoggerFactory.getLogger(ImporterStoreClient.class);
+ private final ImportSSTGrpc.ImportSSTStub stub;
+
protected ImporterStoreClient(
TiConfiguration conf,
ChannelFactory channelFactory,
ImportSSTGrpc.ImportSSTBlockingStub blockingStub,
- ImportSSTGrpc.ImportSSTStub asyncStub) {
+ ImportSSTGrpc.ImportSSTFutureStub asyncStub,
+ ImportSSTGrpc.ImportSSTStub stub) {
super(conf, channelFactory, blockingStub, asyncStub);
+ this.stub = stub;
}
private StreamObserver streamObserverRequest;
@@ -101,18 +106,16 @@ public void onCompleted() {
/**
* Ingest KV pairs to RawKV/Txn using gRPC streaming mode. This API should be called on both
* leader and followers.
- *
- * @return
*/
public void startWrite() {
if (conf.isRawKVMode()) {
streamObserverRequest =
(StreamObserver)
- getAsyncStub().rawWrite((StreamObserver) this);
+ getStub().rawWrite((StreamObserver) this);
} else {
streamObserverRequest =
(StreamObserver)
- getAsyncStub().write((StreamObserver) this);
+ getStub().write((StreamObserver) this);
}
}
@@ -174,10 +177,14 @@ protected ImportSSTGrpc.ImportSSTBlockingStub getBlockingStub() {
}
@Override
- protected ImportSSTGrpc.ImportSSTStub getAsyncStub() {
+ protected ImportSSTGrpc.ImportSSTFutureStub getAsyncStub() {
return asyncStub.withDeadlineAfter(conf.getIngestTimeout(), TimeUnit.MILLISECONDS);
}
+ protected ImportSSTGrpc.ImportSSTStub getStub() {
+ return stub.withDeadlineAfter(conf.getIngestTimeout(), TimeUnit.MILLISECONDS);
+ }
+
@Override
public void close() throws Exception {}
@@ -209,10 +216,11 @@ public synchronized ImporterStoreClient build(TiStore store) throws GrpcExceptio
ManagedChannel channel = channelFactory.getChannel(addressStr, pdClient.getHostMapping());
ImportSSTGrpc.ImportSSTBlockingStub blockingStub = ImportSSTGrpc.newBlockingStub(channel);
- ImportSSTGrpc.ImportSSTStub asyncStub = ImportSSTGrpc.newStub(channel);
+ ImportSSTGrpc.ImportSSTFutureStub asyncStub = ImportSSTGrpc.newFutureStub(channel);
+ ImportSSTGrpc.ImportSSTStub stub = ImportSSTGrpc.newStub(channel);
return new ImporterStoreClient(
- conf, channelFactory, blockingStub, asyncStub);
+ conf, channelFactory, blockingStub, asyncStub, stub);
}
}
}
diff --git a/src/main/java/org/tikv/common/importer/SwitchTiKVModeClient.java b/src/main/java/org/tikv/common/importer/SwitchTiKVModeClient.java
index e230217e435..12bb3a065fe 100644
--- a/src/main/java/org/tikv/common/importer/SwitchTiKVModeClient.java
+++ b/src/main/java/org/tikv/common/importer/SwitchTiKVModeClient.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2021 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -36,32 +36,36 @@ public class SwitchTiKVModeClient {
private final PDClient pdClient;
private final ImporterStoreClient.ImporterStoreClientBuilder builder;
- private final ScheduledExecutorService ingestScheduledExecutorService;
+ private ScheduledExecutorService ingestScheduledExecutorService;
public SwitchTiKVModeClient(
PDClient pdClient, ImporterStoreClient.ImporterStoreClientBuilder builder) {
this.pdClient = pdClient;
this.builder = builder;
-
- this.ingestScheduledExecutorService =
- Executors.newSingleThreadScheduledExecutor(
- new ThreadFactoryBuilder()
- .setNameFormat("switch-tikv-mode-pool-%d")
- .setDaemon(true)
- .build());
}
public void switchTiKVToNormalMode() {
doSwitchTiKVMode(ImportSstpb.SwitchMode.Normal);
}
- public void keepTiKVToImportMode() {
- ingestScheduledExecutorService.scheduleAtFixedRate(
- this::switchTiKVToImportMode, 0, KEEP_TIKV_TO_IMPORT_MODE_PERIOD, TimeUnit.SECONDS);
+ public synchronized void keepTiKVToImportMode() {
+ if (ingestScheduledExecutorService == null) {
+ ingestScheduledExecutorService =
+ Executors.newSingleThreadScheduledExecutor(
+ new ThreadFactoryBuilder()
+ .setNameFormat("switch-tikv-mode-pool-%d")
+ .setDaemon(true)
+ .build());
+ ingestScheduledExecutorService.scheduleAtFixedRate(
+ this::switchTiKVToImportMode, 0, KEEP_TIKV_TO_IMPORT_MODE_PERIOD, TimeUnit.SECONDS);
+ }
}
- public void stopKeepTiKVToImportMode() {
- ingestScheduledExecutorService.shutdown();
+ public synchronized void stopKeepTiKVToImportMode() {
+ if (ingestScheduledExecutorService != null) {
+ ingestScheduledExecutorService.shutdown();
+ ingestScheduledExecutorService = null;
+ }
}
private void switchTiKVToImportMode() {
@@ -69,7 +73,8 @@ private void switchTiKVToImportMode() {
}
private void doSwitchTiKVMode(ImportSstpb.SwitchMode mode) {
- BackOffer bo = ConcreteBackOffer.newCustomBackOff(BackOffer.PD_INFO_BACKOFF);
+ BackOffer bo =
+ ConcreteBackOffer.newCustomBackOff(BackOffer.PD_INFO_BACKOFF, pdClient.getClusterId());
List allStores = pdClient.getAllStores(bo);
for (Metapb.Store store : allStores) {
ImporterStoreClient client = builder.build(new TiStore(store));
diff --git a/src/main/java/org/tikv/common/key/CompoundKey.java b/src/main/java/org/tikv/common/key/CompoundKey.java
index 7ef182c92c7..d7f4598e24e 100644
--- a/src/main/java/org/tikv/common/key/CompoundKey.java
+++ b/src/main/java/org/tikv/common/key/CompoundKey.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.key;
diff --git a/src/main/java/org/tikv/common/key/IndexKey.java b/src/main/java/org/tikv/common/key/IndexKey.java
index caf598311f3..a222b2a94bd 100644
--- a/src/main/java/org/tikv/common/key/IndexKey.java
+++ b/src/main/java/org/tikv/common/key/IndexKey.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.key;
diff --git a/src/main/java/org/tikv/common/key/IndexScanKeyRangeBuilder.java b/src/main/java/org/tikv/common/key/IndexScanKeyRangeBuilder.java
index 3639678e141..7423220dec7 100644
--- a/src/main/java/org/tikv/common/key/IndexScanKeyRangeBuilder.java
+++ b/src/main/java/org/tikv/common/key/IndexScanKeyRangeBuilder.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.key;
diff --git a/src/main/java/org/tikv/common/key/Key.java b/src/main/java/org/tikv/common/key/Key.java
index efe79d2e020..6da4bd8acf9 100644
--- a/src/main/java/org/tikv/common/key/Key.java
+++ b/src/main/java/org/tikv/common/key/Key.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.key;
diff --git a/src/main/java/org/tikv/common/key/KeyRangeBuilder.java b/src/main/java/org/tikv/common/key/KeyRangeBuilder.java
index 7beee13fd4e..16e76fd515e 100644
--- a/src/main/java/org/tikv/common/key/KeyRangeBuilder.java
+++ b/src/main/java/org/tikv/common/key/KeyRangeBuilder.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.key;
diff --git a/src/main/java/org/tikv/common/key/RowKey.java b/src/main/java/org/tikv/common/key/RowKey.java
index 15265864d52..d752d7e2147 100644
--- a/src/main/java/org/tikv/common/key/RowKey.java
+++ b/src/main/java/org/tikv/common/key/RowKey.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.key;
diff --git a/src/main/java/org/tikv/common/key/StatisticsKeyRangeBuilder.java b/src/main/java/org/tikv/common/key/StatisticsKeyRangeBuilder.java
index 86ecc191624..d282c0908a9 100644
--- a/src/main/java/org/tikv/common/key/StatisticsKeyRangeBuilder.java
+++ b/src/main/java/org/tikv/common/key/StatisticsKeyRangeBuilder.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.key;
diff --git a/src/main/java/org/tikv/common/key/TypedKey.java b/src/main/java/org/tikv/common/key/TypedKey.java
index 40e5db7fdce..40b3f039ce1 100644
--- a/src/main/java/org/tikv/common/key/TypedKey.java
+++ b/src/main/java/org/tikv/common/key/TypedKey.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.key;
diff --git a/src/main/java/org/tikv/common/log/SlowLog.java b/src/main/java/org/tikv/common/log/SlowLog.java
new file mode 100644
index 00000000000..5d679a28019
--- /dev/null
+++ b/src/main/java/org/tikv/common/log/SlowLog.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.log;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+
+public interface SlowLog {
+
+ SlowLogSpan start(String name);
+
+ long getTraceId();
+
+ long getThresholdMS();
+
+ void setError(Throwable err);
+
+ SlowLog withFields(Map fields);
+
+ default SlowLog withField(String key, Object value) {
+ return withFields(ImmutableMap.of(key, value));
+ }
+
+ Object getField(String key);
+
+ void log();
+}
diff --git a/src/main/java/org/tikv/common/log/SlowLogEmptyImpl.java b/src/main/java/org/tikv/common/log/SlowLogEmptyImpl.java
new file mode 100644
index 00000000000..0e65cc137af
--- /dev/null
+++ b/src/main/java/org/tikv/common/log/SlowLogEmptyImpl.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.log;
+
+import java.util.Map;
+
+public class SlowLogEmptyImpl implements SlowLog {
+ public static final SlowLogEmptyImpl INSTANCE = new SlowLogEmptyImpl();
+
+ private SlowLogEmptyImpl() {}
+
+ @Override
+ public SlowLogSpan start(String name) {
+ return SlowLogSpanEmptyImpl.INSTANCE;
+ }
+
+ @Override
+ public long getTraceId() {
+ return 0;
+ }
+
+ @Override
+ public long getThresholdMS() {
+ return 0;
+ }
+
+ @Override
+ public void setError(Throwable err) {}
+
+ @Override
+ public SlowLog withFields(Map fields) {
+ return this;
+ }
+
+ @Override
+ public Object getField(String key) {
+ return null;
+ }
+
+ @Override
+ public void log() {}
+}
diff --git a/src/main/java/org/tikv/common/log/SlowLogImpl.java b/src/main/java/org/tikv/common/log/SlowLogImpl.java
new file mode 100644
index 00000000000..fbf38fad26b
--- /dev/null
+++ b/src/main/java/org/tikv/common/log/SlowLogImpl.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.log;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import java.math.BigInteger;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Random;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SlowLogImpl implements SlowLog {
+
+ private static final Logger logger = LoggerFactory.getLogger(SlowLogImpl.class);
+
+ private static final int MAX_SPAN_SIZE = 1024;
+
+ private static final Random random = new Random();
+
+ private final List slowLogSpans = new ArrayList<>();
+ private final HashMap fields = new HashMap<>();
+ private Throwable error = null;
+
+ private final long startMS;
+ /**
+ * use System.nanoTime() to calculate duration, cause System.currentTimeMillis() is not monotonic
+ */
+ private final long startNS;
+
+ private final long slowThresholdMS;
+
+ private final long traceId;
+
+ private long durationMS;
+
+ public SlowLogImpl(long slowThresholdMS) {
+ this.startMS = System.currentTimeMillis();
+ this.startNS = System.nanoTime();
+ this.slowThresholdMS = slowThresholdMS;
+ this.traceId = random.nextLong();
+ }
+
+ @Override
+ public synchronized SlowLogSpan start(String name) {
+ SlowLogSpan slowLogSpan = new SlowLogSpanImpl(name, startMS, startNS);
+ if (slowLogSpans.size() < MAX_SPAN_SIZE) {
+ slowLogSpans.add(slowLogSpan);
+ }
+ slowLogSpan.start();
+ return slowLogSpan;
+ }
+
+ @Override
+ public long getTraceId() {
+ return traceId;
+ }
+
+ @Override
+ public long getThresholdMS() {
+ return slowThresholdMS;
+ }
+
+ @Override
+ public void setError(Throwable err) {
+ this.error = err;
+ }
+
+ @Override
+ public SlowLog withFields(Map fields) {
+ this.fields.putAll(fields);
+ return this;
+ }
+
+ @Override
+ public Object getField(String key) {
+ return fields.get(key);
+ }
+
+ @Override
+ public void log() {
+ recordTime();
+ if (error != null || timeExceeded()) {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
+ logger.warn(
+ String.format(
+ "A request spent %s ms. start=%s, end=%s, SlowLog:%s",
+ durationMS,
+ dateFormat.format(startMS),
+ dateFormat.format(startMS + durationMS),
+ getSlowLogJson().toString()));
+ }
+ }
+
+ private void recordTime() {
+ long currentNS = System.nanoTime();
+ durationMS = (currentNS - startNS) / 1_000_000;
+ }
+
+ boolean timeExceeded() {
+ return slowThresholdMS >= 0 && durationMS > slowThresholdMS;
+ }
+
+ JsonObject getSlowLogJson() {
+ JsonObject jsonObject = new JsonObject();
+
+ jsonObject.addProperty("trace_id", toUnsignedBigInteger(traceId));
+
+ if (error != null) {
+ jsonObject.addProperty("error", error.getMessage());
+ }
+
+ JsonArray jsonArray = new JsonArray();
+ for (SlowLogSpan slowLogSpan : slowLogSpans) {
+ jsonArray.add(slowLogSpan.toJsonElement());
+ }
+ jsonObject.add("spans", jsonArray);
+
+ for (Entry entry : fields.entrySet()) {
+ Object value = entry.getValue();
+ if (value instanceof List) {
+ JsonArray field = new JsonArray();
+ for (Object o : (List>) value) {
+ field.add(o.toString());
+ }
+ jsonObject.add(entry.getKey(), field);
+ } else if (value instanceof Map) {
+ JsonObject field = new JsonObject();
+ for (Entry, ?> e : ((Map, ?>) value).entrySet()) {
+ field.addProperty(e.getKey().toString(), e.getValue().toString());
+ }
+ jsonObject.add(entry.getKey(), field);
+ } else {
+ jsonObject.addProperty(entry.getKey(), value.toString());
+ }
+ }
+
+ return jsonObject;
+ }
+
+ static BigInteger toUnsignedBigInteger(long i) {
+ if (i >= 0) {
+ return BigInteger.valueOf(i);
+ } else {
+ long withoutSign = i & ~(1L << 63);
+
+ return (BigInteger.valueOf(1)).shiftLeft(63).add(BigInteger.valueOf(withoutSign));
+ }
+ }
+}
diff --git a/src/main/java/org/tikv/common/log/SlowLogSpan.java b/src/main/java/org/tikv/common/log/SlowLogSpan.java
new file mode 100644
index 00000000000..e2b37f802d7
--- /dev/null
+++ b/src/main/java/org/tikv/common/log/SlowLogSpan.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.log;
+
+import com.google.gson.JsonElement;
+
+public interface SlowLogSpan {
+ void addProperty(String key, String value);
+
+ void start();
+
+ void end();
+
+ JsonElement toJsonElement();
+}
diff --git a/src/main/java/org/tikv/common/log/SlowLogSpanEmptyImpl.java b/src/main/java/org/tikv/common/log/SlowLogSpanEmptyImpl.java
new file mode 100644
index 00000000000..a8de6e4cab3
--- /dev/null
+++ b/src/main/java/org/tikv/common/log/SlowLogSpanEmptyImpl.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.log;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
+public class SlowLogSpanEmptyImpl implements SlowLogSpan {
+
+ public static final SlowLogSpanEmptyImpl INSTANCE = new SlowLogSpanEmptyImpl();
+
+ private SlowLogSpanEmptyImpl() {}
+
+ @Override
+ public void addProperty(String key, String value) {}
+
+ @Override
+ public void start() {}
+
+ @Override
+ public void end() {}
+
+ @Override
+ public JsonElement toJsonElement() {
+ return new JsonObject();
+ }
+}
diff --git a/src/main/java/org/tikv/common/log/SlowLogSpanImpl.java b/src/main/java/org/tikv/common/log/SlowLogSpanImpl.java
new file mode 100644
index 00000000000..39477f2d640
--- /dev/null
+++ b/src/main/java/org/tikv/common/log/SlowLogSpanImpl.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.tikv.common.log;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import java.text.SimpleDateFormat;
+import java.util.HashMap;
+import java.util.Map;
+
+public class SlowLogSpanImpl implements SlowLogSpan {
+ private final String name;
+ private final long requestStartInstantNS;
+ private final long requestStartUnixNS;
+
+ /** Key-Value pairs which will be logged, e.g. function name, key, region, etc. */
+ private final Map properties;
+
+ /**
+ * use System.nanoTime() to calculate duration, cause System.currentTimeMillis() is not monotonic
+ */
+ private long startInstantNS;
+
+ private long endInstantNS;
+
+ public SlowLogSpanImpl(String name, long requestStartMS, long requestStartInstantNS) {
+ this.name = name;
+ this.requestStartUnixNS = requestStartMS * 1_000_000;
+ this.requestStartInstantNS = requestStartInstantNS;
+ this.properties = new HashMap<>();
+ this.startInstantNS = 0;
+ this.endInstantNS = 0;
+ }
+
+ @Override
+ public void addProperty(String key, String value) {
+ properties.put(key, value);
+ }
+
+ @Override
+ public void start() {
+ startInstantNS = System.nanoTime();
+ }
+
+ @Override
+ public void end() {
+ endInstantNS = System.nanoTime();
+ }
+
+ @Override
+ public JsonElement toJsonElement() {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.addProperty("event", name);
+ jsonObject.addProperty("begin", dateFormat.format(getStartUnixNS() / 1_000_000));
+ jsonObject.addProperty("duration_ms", getDurationNS() / 1_000_000);
+
+ if (!properties.isEmpty()) {
+ JsonObject propertiesObject = new JsonObject();
+ for (Map.Entry entry : properties.entrySet()) {
+ propertiesObject.addProperty(entry.getKey(), entry.getValue());
+ }
+ jsonObject.add("properties", propertiesObject);
+ }
+
+ return jsonObject;
+ }
+
+ private long getStartUnixNS() {
+ return requestStartUnixNS + (startInstantNS - requestStartInstantNS);
+ }
+
+ private long getDurationNS() {
+ if (startInstantNS == 0 || endInstantNS == 0) {
+ return -1;
+ }
+ return endInstantNS - startInstantNS;
+ }
+}
diff --git a/src/main/java/org/tikv/common/meta/CIStr.java b/src/main/java/org/tikv/common/meta/CIStr.java
index 64168891bd2..6068dba0820 100644
--- a/src/main/java/org/tikv/common/meta/CIStr.java
+++ b/src/main/java/org/tikv/common/meta/CIStr.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/Collation.java b/src/main/java/org/tikv/common/meta/Collation.java
index ab0e7482089..b2b1bcfc1e4 100644
--- a/src/main/java/org/tikv/common/meta/Collation.java
+++ b/src/main/java/org/tikv/common/meta/Collation.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/IndexType.java b/src/main/java/org/tikv/common/meta/IndexType.java
index c799c2138b8..502fe7182de 100644
--- a/src/main/java/org/tikv/common/meta/IndexType.java
+++ b/src/main/java/org/tikv/common/meta/IndexType.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/SchemaState.java b/src/main/java/org/tikv/common/meta/SchemaState.java
index 75566c48d86..1d01622231b 100644
--- a/src/main/java/org/tikv/common/meta/SchemaState.java
+++ b/src/main/java/org/tikv/common/meta/SchemaState.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiColumnInfo.java b/src/main/java/org/tikv/common/meta/TiColumnInfo.java
index c1027cdf048..20227b89f3f 100644
--- a/src/main/java/org/tikv/common/meta/TiColumnInfo.java
+++ b/src/main/java/org/tikv/common/meta/TiColumnInfo.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiDAGRequest.java b/src/main/java/org/tikv/common/meta/TiDAGRequest.java
index 40966f1f916..5200bdef835 100644
--- a/src/main/java/org/tikv/common/meta/TiDAGRequest.java
+++ b/src/main/java/org/tikv/common/meta/TiDAGRequest.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiDBInfo.java b/src/main/java/org/tikv/common/meta/TiDBInfo.java
index 4bbc6e52cae..759f82c35f2 100644
--- a/src/main/java/org/tikv/common/meta/TiDBInfo.java
+++ b/src/main/java/org/tikv/common/meta/TiDBInfo.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiFlashReplicaInfo.java b/src/main/java/org/tikv/common/meta/TiFlashReplicaInfo.java
index 4d8aa03dfb8..07f22a47b8c 100644
--- a/src/main/java/org/tikv/common/meta/TiFlashReplicaInfo.java
+++ b/src/main/java/org/tikv/common/meta/TiFlashReplicaInfo.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiIndexColumn.java b/src/main/java/org/tikv/common/meta/TiIndexColumn.java
index 5b6e26d8ab9..ddea32b835b 100644
--- a/src/main/java/org/tikv/common/meta/TiIndexColumn.java
+++ b/src/main/java/org/tikv/common/meta/TiIndexColumn.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiIndexInfo.java b/src/main/java/org/tikv/common/meta/TiIndexInfo.java
index afb87053890..0f0c70b22cf 100644
--- a/src/main/java/org/tikv/common/meta/TiIndexInfo.java
+++ b/src/main/java/org/tikv/common/meta/TiIndexInfo.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiPartitionDef.java b/src/main/java/org/tikv/common/meta/TiPartitionDef.java
index 2b6d1c4497f..178f586ca5e 100644
--- a/src/main/java/org/tikv/common/meta/TiPartitionDef.java
+++ b/src/main/java/org/tikv/common/meta/TiPartitionDef.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2018 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiPartitionExpr.java b/src/main/java/org/tikv/common/meta/TiPartitionExpr.java
index dc62fb6852d..d25fd488155 100644
--- a/src/main/java/org/tikv/common/meta/TiPartitionExpr.java
+++ b/src/main/java/org/tikv/common/meta/TiPartitionExpr.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2018 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiPartitionInfo.java b/src/main/java/org/tikv/common/meta/TiPartitionInfo.java
index 84707ec5837..2c7e5050359 100644
--- a/src/main/java/org/tikv/common/meta/TiPartitionInfo.java
+++ b/src/main/java/org/tikv/common/meta/TiPartitionInfo.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2018 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiSequenceInfo.java b/src/main/java/org/tikv/common/meta/TiSequenceInfo.java
index 012e11238c7..1c2408672a8 100644
--- a/src/main/java/org/tikv/common/meta/TiSequenceInfo.java
+++ b/src/main/java/org/tikv/common/meta/TiSequenceInfo.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiTableInfo.java b/src/main/java/org/tikv/common/meta/TiTableInfo.java
index af45a2b4a4c..344cf5d9647 100644
--- a/src/main/java/org/tikv/common/meta/TiTableInfo.java
+++ b/src/main/java/org/tikv/common/meta/TiTableInfo.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiTimestamp.java b/src/main/java/org/tikv/common/meta/TiTimestamp.java
index ef26983ce91..a95a741bc7c 100644
--- a/src/main/java/org/tikv/common/meta/TiTimestamp.java
+++ b/src/main/java/org/tikv/common/meta/TiTimestamp.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiUserIdentity.java b/src/main/java/org/tikv/common/meta/TiUserIdentity.java
index afb3b932f02..559596d65d4 100644
--- a/src/main/java/org/tikv/common/meta/TiUserIdentity.java
+++ b/src/main/java/org/tikv/common/meta/TiUserIdentity.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/meta/TiViewInfo.java b/src/main/java/org/tikv/common/meta/TiViewInfo.java
index 0e8655204b8..1683330fa56 100644
--- a/src/main/java/org/tikv/common/meta/TiViewInfo.java
+++ b/src/main/java/org/tikv/common/meta/TiViewInfo.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.meta;
diff --git a/src/main/java/org/tikv/common/operation/ErrorHandler.java b/src/main/java/org/tikv/common/operation/ErrorHandler.java
index 48e964a2b7d..b0ff3c820ff 100644
--- a/src/main/java/org/tikv/common/operation/ErrorHandler.java
+++ b/src/main/java/org/tikv/common/operation/ErrorHandler.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
diff --git a/src/main/java/org/tikv/common/operation/KVErrorHandler.java b/src/main/java/org/tikv/common/operation/KVErrorHandler.java
index dfbe24c5a65..22d05658425 100644
--- a/src/main/java/org/tikv/common/operation/KVErrorHandler.java
+++ b/src/main/java/org/tikv/common/operation/KVErrorHandler.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -23,6 +23,7 @@
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.tikv.common.apiversion.RequestKeyCodec;
import org.tikv.common.exception.GrpcException;
import org.tikv.common.exception.KeyException;
import org.tikv.common.region.RegionErrorReceiver;
@@ -44,6 +45,8 @@ public class KVErrorHandler implements ErrorHandler {
private final boolean forWrite;
private final RegionErrorHandler regionHandler;
+ private final RequestKeyCodec codec;
+
public KVErrorHandler(
RegionManager regionManager,
RegionErrorReceiver recv,
@@ -59,6 +62,7 @@ public KVErrorHandler(
this.resolveLockResultCallback = resolveLockResultCallback;
this.callerStartTS = callerStartTS;
this.forWrite = forWrite;
+ this.codec = regionManager.getPDClient().getCodec();
}
private void resolveLock(BackOffer backOffer, Lock lock) {
@@ -94,15 +98,13 @@ public boolean handleResponseError(BackOffer backOffer, RespT resp) {
Errorpb.Error error = regionHandler.getRegionError(resp);
if (error != null) {
return regionHandler.handleRegionError(backOffer, error);
- } else {
- regionHandler.tryUpdateRegionStore();
}
// Key error handling logic
Kvrpcpb.KeyError keyError = getKeyError.apply(resp);
if (keyError != null) {
try {
- Lock lock = AbstractLockResolverClient.extractLockFromKeyErr(keyError);
+ Lock lock = AbstractLockResolverClient.extractLockFromKeyErr(keyError, codec);
resolveLock(backOffer, lock);
return true;
} catch (KeyException e) {
diff --git a/src/main/java/org/tikv/common/operation/NoopHandler.java b/src/main/java/org/tikv/common/operation/NoopHandler.java
index 4cb84dc9d6f..f25a8281292 100644
--- a/src/main/java/org/tikv/common/operation/NoopHandler.java
+++ b/src/main/java/org/tikv/common/operation/NoopHandler.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.operation;
diff --git a/src/main/java/org/tikv/common/operation/PDErrorHandler.java b/src/main/java/org/tikv/common/operation/PDErrorHandler.java
index 0d280da5eef..76c6a768644 100644
--- a/src/main/java/org/tikv/common/operation/PDErrorHandler.java
+++ b/src/main/java/org/tikv/common/operation/PDErrorHandler.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -19,12 +19,14 @@
import static org.tikv.common.pd.PDError.buildFromPdpbError;
+import io.grpc.StatusRuntimeException;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tikv.common.PDClient;
import org.tikv.common.exception.GrpcException;
import org.tikv.common.exception.TiClientInternalException;
+import org.tikv.common.log.SlowLogSpan;
import org.tikv.common.pd.PDError;
import org.tikv.common.util.BackOffFunction;
import org.tikv.common.util.BackOffer;
@@ -58,7 +60,12 @@ public boolean handleResponseError(BackOffer backOffer, RespT resp) {
case PD_ERROR:
backOffer.doBackOff(
BackOffFunction.BackOffFuncType.BoPDRPC, new GrpcException(error.toString()));
- client.updateLeaderOrforwardFollower();
+ SlowLogSpan tryUpdateLeaderSpan = backOffer.getSlowLog().start("try_update_leader");
+ try {
+ client.tryUpdateLeaderOrForwardFollower();
+ } finally {
+ tryUpdateLeaderSpan.end();
+ }
return true;
case REGION_PEER_NOT_ELECTED:
logger.debug(error.getMessage());
@@ -74,8 +81,17 @@ public boolean handleResponseError(BackOffer backOffer, RespT resp) {
@Override
public boolean handleRequestError(BackOffer backOffer, Exception e) {
+ // store id is not found
+ if (e instanceof StatusRuntimeException && e.getMessage().contains("invalid store ID")) {
+ return false;
+ }
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoPDRPC, e);
- client.updateLeaderOrforwardFollower();
+ SlowLogSpan updateLeaderSpan = backOffer.getSlowLog().start("try_update_leader");
+ try {
+ client.tryUpdateLeaderOrForwardFollower();
+ } finally {
+ updateLeaderSpan.end();
+ }
return true;
}
}
diff --git a/src/main/java/org/tikv/common/operation/RegionErrorHandler.java b/src/main/java/org/tikv/common/operation/RegionErrorHandler.java
index 452aad13d06..debbccf7eee 100644
--- a/src/main/java/org/tikv/common/operation/RegionErrorHandler.java
+++ b/src/main/java/org/tikv/common/operation/RegionErrorHandler.java
@@ -1,19 +1,43 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
package org.tikv.common.operation;
import com.google.protobuf.ByteString;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tikv.common.codec.KeyUtils;
+import org.tikv.common.event.CacheInvalidateEvent;
+import org.tikv.common.event.CacheInvalidateEvent.CacheType;
import org.tikv.common.exception.GrpcException;
+import org.tikv.common.exception.TiKVException;
import org.tikv.common.region.RegionErrorReceiver;
import org.tikv.common.region.RegionManager;
import org.tikv.common.region.TiRegion;
import org.tikv.common.util.BackOffFunction;
+import org.tikv.common.util.BackOffFunction.BackOffFuncType;
import org.tikv.common.util.BackOffer;
import org.tikv.kvproto.Errorpb;
+import org.tikv.kvproto.Metapb;
public class RegionErrorHandler implements ErrorHandler {
private static final Logger logger = LoggerFactory.getLogger(RegionErrorHandler.class);
@@ -22,6 +46,11 @@ public class RegionErrorHandler implements ErrorHandler {
private final Function getRegionError;
private final RegionManager regionManager;
private final RegionErrorReceiver recv;
+ private final List> cacheInvalidateCallBackList;
+
+ private final ExecutorService callBackThreadPool;
+ private final int INVALID_STORE_ID = 0;
+ private final int INVALID_REGION_ID = 0;
public RegionErrorHandler(
RegionManager regionManager,
@@ -30,6 +59,8 @@ public RegionErrorHandler(
this.recv = recv;
this.regionManager = regionManager;
this.getRegionError = getRegionError;
+ this.cacheInvalidateCallBackList = regionManager.getCacheInvalidateCallbackList();
+ this.callBackThreadPool = regionManager.getCallBackThreadPool();
}
@Override
@@ -42,16 +73,10 @@ public boolean handleResponseError(BackOffer backOffer, RespT resp) {
Errorpb.Error error = getRegionError(resp);
if (error != null) {
return handleRegionError(backOffer, error);
- } else {
- tryUpdateRegionStore();
}
return false;
}
- public void tryUpdateRegionStore() {
- recv.tryUpdateRegionStore();
- }
-
public boolean handleRegionError(BackOffer backOffer, Errorpb.Error error) {
if (error.hasNotLeader()) {
// this error is reported from raftstore:
@@ -78,7 +103,7 @@ public boolean handleRegionError(BackOffer backOffer, Errorpb.Error error) {
// onNotLeader is only needed when updateLeader succeeds, thus switch
// to a new store address.
TiRegion newRegion = this.regionManager.updateLeader(recv.getRegion(), newStoreId);
- retry = newRegion != null && recv.onNotLeader(newRegion);
+ retry = newRegion != null && recv.onNotLeader(newRegion, backOffer);
backOffFuncType = BackOffFunction.BackOffFuncType.BoUpdateLeader;
} else {
@@ -92,6 +117,7 @@ public boolean handleRegionError(BackOffer backOffer, Errorpb.Error error) {
if (!retry) {
this.regionManager.invalidateRegion(recv.getRegion());
+ notifyRegionLeaderError(recv.getRegion());
}
backOffer.doBackOff(backOffFuncType, new GrpcException(error.toString()));
@@ -101,24 +127,21 @@ public boolean handleRegionError(BackOffer backOffer, Errorpb.Error error) {
// this error is reported from raftstore:
// store_id requested at the moment is inconsistent with that expected
// Solution:re-fetch from PD
- long storeId = recv.getRegion().getLeader().getStoreId();
+ long storeId = error.getStoreNotMatch().getRequestStoreId();
long actualStoreId = error.getStoreNotMatch().getActualStoreId();
logger.warn(
String.format(
"Store Not Match happened with region id %d, store id %d, actual store id %d",
recv.getRegion().getId(), storeId, actualStoreId));
-
- this.regionManager.invalidateRegion(recv.getRegion());
- this.regionManager.invalidateStore(storeId);
+ // may request store which is not leader.
+ invalidateRegionStoreCache(recv.getRegion(), storeId);
// assume this is a low probability error, do not retry, just re-split the request by
// throwing it out.
return false;
} else if (error.hasEpochNotMatch()) {
- // this error is reported from raftstore:
- // region has outdated version,please try later.
- logger.warn(String.format("Stale Epoch encountered for region [%s]", recv.getRegion()));
- this.regionManager.onRegionStale(recv.getRegion());
- return false;
+ logger.warn(
+ String.format("tikv reports `EpochNotMatch` retry later, region: %s", recv.getRegion()));
+ return onRegionEpochNotMatch(backOffer, error.getEpochNotMatch().getCurrentRegionsList());
} else if (error.hasServerIsBusy()) {
// this error is reported from kv:
// will occur when write pressure is high. Please try later.
@@ -130,8 +153,6 @@ public boolean handleRegionError(BackOffer backOffer, Errorpb.Error error) {
BackOffFunction.BackOffFuncType.BoServerBusy,
new StatusRuntimeException(
Status.fromCode(Status.Code.UNAVAILABLE).withDescription(error.toString())));
- backOffer.doBackOff(
- BackOffFunction.BackOffFuncType.BoRegionMiss, new GrpcException(error.getMessage()));
return true;
} else if (error.hasStaleCommand()) {
// this error is reported from raftstore:
@@ -156,11 +177,17 @@ public boolean handleRegionError(BackOffer backOffer, Errorpb.Error error) {
regionManager.clearRegionCache();
throw new StatusRuntimeException(Status.UNKNOWN.withDescription(error.toString()));
}
+ // The tso cache is used up in TiKV servers, we should backoff and wait its cache is renewed.
+ else if (error.getMessage().contains("TsoBatchUsedUp")) {
+ logger.warn(String.format("tso batch used up for region [%s]", recv.getRegion()));
+ backOffer.doBackOff(BackOffFuncType.BoTsoBatchUsedUp, new GrpcException(error.getMessage()));
+ return true;
+ }
logger.warn(String.format("Unknown error %s for region [%s]", error, recv.getRegion()));
// For other errors, we only drop cache here.
// Upper level may split this task.
- invalidateRegionStoreCache(recv.getRegion());
+ invalidateRegionStoreCache(recv.getRegion(), recv.getRegion().getLeader().getStoreId());
// retry if raft proposal is dropped, it indicates the store is in the middle of transition
if (error.getMessage().contains("Raft ProposalDropped")) {
backOffer.doBackOff(
@@ -170,17 +197,83 @@ public boolean handleRegionError(BackOffer backOffer, Errorpb.Error error) {
return false;
}
+ // ref: https://github.com/tikv/client-go/blob/tidb-5.2/internal/locate/region_request.go#L985
+ // OnRegionEpochNotMatch removes the old region and inserts new regions into the cache.
+ // It returns whether retries the request because it's possible the region epoch is ahead of
+ // TiKV's due to slow appling.
+ private boolean onRegionEpochNotMatch(BackOffer backOffer, List currentRegions) {
+ if (currentRegions.size() == 0) {
+ this.regionManager.onRegionStale(recv.getRegion());
+ notifyRegionCacheInvalidate(recv.getRegion());
+ return false;
+ }
+
+ // Find whether the region epoch in `ctx` is ahead of TiKV's. If so, backoff.
+ for (Metapb.Region meta : currentRegions) {
+ if (meta.getId() == recv.getRegion().getId()
+ && (meta.getRegionEpoch().getConfVer() < recv.getRegion().getVerID().getConfVer()
+ || meta.getRegionEpoch().getVersion() < recv.getRegion().getVerID().getVer())) {
+ String errorMsg =
+ String.format(
+ "region epoch is ahead of tikv, region: %s, currentRegions: %s",
+ recv.getRegion(), currentRegions);
+ logger.info(errorMsg);
+ backOffer.doBackOff(
+ BackOffFunction.BackOffFuncType.BoRegionMiss, new TiKVException(errorMsg));
+ return true;
+ }
+ }
+
+ boolean needInvalidateOld = true;
+ List newRegions = new ArrayList<>(currentRegions.size());
+ // If the region epoch is not ahead of TiKV's, replace region meta in region cache.
+ for (Metapb.Region meta : currentRegions) {
+ // The region needs to be decoded to plain format.
+ try {
+ meta = regionManager.getPDClient().getCodec().decodeRegion(meta);
+ } catch (Exception e) {
+ logger.warn("ignore invalid region: " + meta.toString());
+ // if the region is invalid, ignore it since the following situation might appear.
+ // Assuming a region with range [r000, z), then it splits into:
+ // [r000, x) [x, z), the right region is invalid for keyspace `r000`.
+ // We should only care about the valid region.
+ continue;
+ }
+ TiRegion region = regionManager.createRegion(meta, backOffer);
+ newRegions.add(region);
+ if (recv.getRegion().getVerID() == region.getVerID()) {
+ needInvalidateOld = false;
+ }
+ }
+
+ if (needInvalidateOld) {
+ notifyRegionCacheInvalidate(recv.getRegion());
+ this.regionManager.onRegionStale(recv.getRegion());
+ }
+
+ for (TiRegion region : newRegions) {
+ regionManager.insertRegionToCache(region);
+ }
+
+ return false;
+ }
+
@Override
public boolean handleRequestError(BackOffer backOffer, Exception e) {
- if (recv.onStoreUnreachable()) {
+ if (recv.onStoreUnreachable(backOffer)) {
+ if (!backOffer.canRetryAfterSleep(BackOffFunction.BackOffFuncType.BoTiKVRPC)) {
+ regionManager.onRequestFail(recv.getRegion());
+ throw new GrpcException("retry is exhausted.", e);
+ }
return true;
}
logger.warn("request failed because of: " + e.getMessage());
- backOffer.doBackOff(
- BackOffFunction.BackOffFuncType.BoTiKVRPC,
- new GrpcException(
- "send tikv request error: " + e.getMessage() + ", try next peer later", e));
+ if (!backOffer.canRetryAfterSleep(BackOffFunction.BackOffFuncType.BoTiKVRPC)) {
+ regionManager.onRequestFail(recv.getRegion());
+ throw new GrpcException(
+ "send tikv request error: " + e.getMessage() + ", try next peer later", e);
+ }
// TiKV maybe down, so do not retry in `callWithRetry`
// should re-fetch the new leader from PD and send request to it
return false;
@@ -197,8 +290,51 @@ public TiRegion getRegion() {
return recv.getRegion();
}
- private void invalidateRegionStoreCache(TiRegion ctxRegion) {
+ private void notifyRegionRequestError(
+ TiRegion ctxRegion, long storeId, CacheInvalidateEvent.CacheType type) {
+ CacheInvalidateEvent event;
+ // When store(region) id is invalid,
+ // it implies that the error was not caused by store(region) error.
+ switch (type) {
+ case REGION:
+ case LEADER:
+ event = new CacheInvalidateEvent(ctxRegion.getId(), INVALID_STORE_ID, true, false, type);
+ break;
+ case REGION_STORE:
+ event = new CacheInvalidateEvent(ctxRegion.getId(), storeId, true, true, type);
+ break;
+ case REQ_FAILED:
+ event = new CacheInvalidateEvent(INVALID_REGION_ID, INVALID_STORE_ID, false, false, type);
+ break;
+ default:
+ throw new IllegalArgumentException("Unexpect invalid cache invalid type " + type);
+ }
+ if (cacheInvalidateCallBackList != null) {
+ for (Function cacheInvalidateCallBack :
+ cacheInvalidateCallBackList) {
+ callBackThreadPool.submit(
+ () -> {
+ try {
+ cacheInvalidateCallBack.apply(event);
+ } catch (Exception e) {
+ logger.error(String.format("CacheInvalidCallBack failed %s", e));
+ }
+ });
+ }
+ }
+ }
+
+ private void invalidateRegionStoreCache(TiRegion ctxRegion, long storeId) {
regionManager.invalidateRegion(ctxRegion);
- regionManager.invalidateStore(ctxRegion.getLeader().getStoreId());
+ regionManager.invalidateStore(storeId);
+ notifyRegionRequestError(ctxRegion, storeId, CacheType.REGION_STORE);
+ }
+
+ private void notifyRegionCacheInvalidate(TiRegion ctxRegion) {
+ notifyRegionRequestError(ctxRegion, 0, CacheType.REGION);
+ }
+
+ private void notifyRegionLeaderError(TiRegion ctxRegion) {
+ notifyRegionRequestError(ctxRegion, 0, CacheType.LEADER);
}
}
diff --git a/src/main/java/org/tikv/common/operation/SchemaInfer.java b/src/main/java/org/tikv/common/operation/SchemaInfer.java
index 833002ebcfb..7f22558fd6e 100644
--- a/src/main/java/org/tikv/common/operation/SchemaInfer.java
+++ b/src/main/java/org/tikv/common/operation/SchemaInfer.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.operation;
diff --git a/src/main/java/org/tikv/common/operation/iterator/ChunkIterator.java b/src/main/java/org/tikv/common/operation/iterator/ChunkIterator.java
index 754a77af1df..92792fe198a 100644
--- a/src/main/java/org/tikv/common/operation/iterator/ChunkIterator.java
+++ b/src/main/java/org/tikv/common/operation/iterator/ChunkIterator.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
diff --git a/src/main/java/org/tikv/common/operation/iterator/ConcreteScanIterator.java b/src/main/java/org/tikv/common/operation/iterator/ConcreteScanIterator.java
index 1f5ac6fcf36..72422736e76 100644
--- a/src/main/java/org/tikv/common/operation/iterator/ConcreteScanIterator.java
+++ b/src/main/java/org/tikv/common/operation/iterator/ConcreteScanIterator.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2018 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.operation.iterator;
@@ -73,9 +75,13 @@ TiRegion loadCurrentRegionToCache() throws GrpcException {
TiRegion region;
try (RegionStoreClient client = builder.build(startKey)) {
client.setTimeout(conf.getScanTimeout());
- region = client.getRegion();
BackOffer backOffer = ConcreteBackOffer.newScannerNextMaxBackOff();
currentCache = client.scan(backOffer, startKey, version);
+ // If we get region before scan, we will use region from cache which
+ // may have wrong end key. This may miss some regions that split from old region.
+ // Client will get the newest region during scan. So we need to
+ // update region after scan.
+ region = client.getRegion();
return region;
}
}
@@ -86,7 +92,8 @@ private ByteString resolveCurrentLock(Kvrpcpb.KvPair current) {
builder.getRegionManager().getRegionStorePairByKey(current.getKey());
TiRegion region = pair.first;
TiStore store = pair.second;
- BackOffer backOffer = ConcreteBackOffer.newGetBackOff();
+ BackOffer backOffer =
+ ConcreteBackOffer.newGetBackOff(builder.getRegionManager().getPDClient().getClusterId());
try (RegionStoreClient client = builder.build(region, store)) {
return client.get(backOffer, current.getKey(), version);
} catch (Exception e) {
diff --git a/src/main/java/org/tikv/common/operation/iterator/CoprocessorIterator.java b/src/main/java/org/tikv/common/operation/iterator/CoprocessorIterator.java
index 431cd18b2e0..5208878dd18 100644
--- a/src/main/java/org/tikv/common/operation/iterator/CoprocessorIterator.java
+++ b/src/main/java/org/tikv/common/operation/iterator/CoprocessorIterator.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.operation.iterator;
diff --git a/src/main/java/org/tikv/common/operation/iterator/DAGIterator.java b/src/main/java/org/tikv/common/operation/iterator/DAGIterator.java
index 36b9fbc14e5..f0d412c1126 100644
--- a/src/main/java/org/tikv/common/operation/iterator/DAGIterator.java
+++ b/src/main/java/org/tikv/common/operation/iterator/DAGIterator.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.operation.iterator;
diff --git a/src/main/java/org/tikv/common/operation/iterator/IndexScanIterator.java b/src/main/java/org/tikv/common/operation/iterator/IndexScanIterator.java
index 0d6b355b068..b8fcab5ac43 100644
--- a/src/main/java/org/tikv/common/operation/iterator/IndexScanIterator.java
+++ b/src/main/java/org/tikv/common/operation/iterator/IndexScanIterator.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.operation.iterator;
diff --git a/src/main/java/org/tikv/common/operation/iterator/RawScanIterator.java b/src/main/java/org/tikv/common/operation/iterator/RawScanIterator.java
index 9c2fbb724e1..08a64aaf87f 100644
--- a/src/main/java/org/tikv/common/operation/iterator/RawScanIterator.java
+++ b/src/main/java/org/tikv/common/operation/iterator/RawScanIterator.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.operation.iterator;
@@ -25,10 +27,10 @@
import org.tikv.common.region.TiRegion;
import org.tikv.common.util.BackOffFunction;
import org.tikv.common.util.BackOffer;
-import org.tikv.common.util.ConcreteBackOffer;
import org.tikv.kvproto.Kvrpcpb;
public class RawScanIterator extends ScanIterator {
+ private final BackOffer scanBackOffer;
public RawScanIterator(
TiConfiguration conf,
@@ -36,21 +38,28 @@ public RawScanIterator(
ByteString startKey,
ByteString endKey,
int limit,
- boolean keyOnly) {
+ boolean keyOnly,
+ BackOffer scanBackOffer) {
super(conf, builder, startKey, endKey, limit, keyOnly);
+
+ this.scanBackOffer = scanBackOffer;
}
+ @Override
TiRegion loadCurrentRegionToCache() throws GrpcException {
- BackOffer backOffer = ConcreteBackOffer.newScannerNextMaxBackOff();
+ BackOffer backOffer = scanBackOffer;
while (true) {
- try (RegionStoreClient client = builder.build(startKey)) {
- client.setTimeout(conf.getScanTimeout());
+ try (RegionStoreClient client = builder.build(startKey, backOffer)) {
+ client.setTimeout(conf.getRawKVScanTimeoutInMS());
TiRegion region = client.getRegion();
if (limit <= 0) {
currentCache = null;
} else {
try {
currentCache = client.rawScan(backOffer, startKey, limit, keyOnly);
+ // Client will get the newest region during scan. So we need to
+ // update region after scan.
+ region = client.getRegion();
} catch (final TiKVException e) {
backOffer.doBackOff(BackOffFunction.BackOffFuncType.BoRegionMiss, e);
continue;
@@ -61,11 +70,12 @@ TiRegion loadCurrentRegionToCache() throws GrpcException {
}
}
- private boolean notEndOfScan() {
- return limit > 0
- && !(processingLastBatch
- && (index >= currentCache.size()
- || Key.toRawKey(currentCache.get(index).getKey()).compareTo(endKey) >= 0));
+ private boolean endOfScan() {
+ if (!processingLastBatch) {
+ return false;
+ }
+ ByteString lastKey = currentCache.get(index).getKey();
+ return !lastKey.isEmpty() && Key.toRawKey(lastKey).compareTo(endKey) >= 0;
}
boolean isCacheDrained() {
@@ -84,7 +94,7 @@ public boolean hasNext() {
return false;
}
}
- return notEndOfScan();
+ return !endOfScan();
}
private Kvrpcpb.KvPair getCurrent() {
diff --git a/src/main/java/org/tikv/common/operation/iterator/ScanIterator.java b/src/main/java/org/tikv/common/operation/iterator/ScanIterator.java
index 8c284dcf661..69fd0217fd5 100644
--- a/src/main/java/org/tikv/common/operation/iterator/ScanIterator.java
+++ b/src/main/java/org/tikv/common/operation/iterator/ScanIterator.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2018 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.operation.iterator;
@@ -63,6 +65,7 @@ public abstract class ScanIterator implements Iterator {
*
* @return TiRegion of current data loaded to cache
* @throws GrpcException if scan still fails after backoff
+ * TODO : Add test to check it correctness
*/
abstract TiRegion loadCurrentRegionToCache() throws GrpcException;
@@ -88,16 +91,14 @@ boolean cacheLoadFails() {
Key lastKey = Key.EMPTY;
// Session should be single-threaded itself
// so that we don't worry about conf change in the middle
- // of a transaction. Otherwise below code might lose data
- if (currentCache.size() < limit) {
+ // of a transaction. Otherwise, below code might lose data
+ int scanLimit = Math.min(limit, conf.getScanBatchSize());
+ if (currentCache.size() < scanLimit) {
startKey = curRegionEndKey;
lastKey = Key.toRawKey(curRegionEndKey);
- } else if (currentCache.size() > limit) {
+ } else if (currentCache.size() > scanLimit) {
throw new IndexOutOfBoundsException(
- "current cache size = "
- + currentCache.size()
- + ", larger than "
- + conf.getScanBatchSize());
+ "current cache size = " + currentCache.size() + ", larger than " + scanLimit);
} else {
// Start new scan from exact next key in current region
lastKey = Key.toRawKey(currentCache.get(currentCache.size() - 1).getKey());
diff --git a/src/main/java/org/tikv/common/parser/AstBuilder.java b/src/main/java/org/tikv/common/parser/AstBuilder.java
index 1e0f4157b4b..f9a8966b451 100644
--- a/src/main/java/org/tikv/common/parser/AstBuilder.java
+++ b/src/main/java/org/tikv/common/parser/AstBuilder.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2020 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.parser;
diff --git a/src/main/java/org/tikv/common/parser/CaseChangingCharStream.java b/src/main/java/org/tikv/common/parser/CaseChangingCharStream.java
index 7abb5884aa8..0c4fd951aab 100644
--- a/src/main/java/org/tikv/common/parser/CaseChangingCharStream.java
+++ b/src/main/java/org/tikv/common/parser/CaseChangingCharStream.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
diff --git a/src/main/java/org/tikv/common/parser/TiParser.java b/src/main/java/org/tikv/common/parser/TiParser.java
index 15994f24265..e57142b3d09 100644
--- a/src/main/java/org/tikv/common/parser/TiParser.java
+++ b/src/main/java/org/tikv/common/parser/TiParser.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
diff --git a/src/main/java/org/tikv/common/pd/PDError.java b/src/main/java/org/tikv/common/pd/PDError.java
index 7772b59d80e..645d1eb2c3b 100644
--- a/src/main/java/org/tikv/common/pd/PDError.java
+++ b/src/main/java/org/tikv/common/pd/PDError.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2018 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
diff --git a/src/main/java/org/tikv/common/pd/PDUtils.java b/src/main/java/org/tikv/common/pd/PDUtils.java
index af693705a0d..2049af75430 100644
--- a/src/main/java/org/tikv/common/pd/PDUtils.java
+++ b/src/main/java/org/tikv/common/pd/PDUtils.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.pd;
diff --git a/src/main/java/org/tikv/common/policy/RetryMaxMs.java b/src/main/java/org/tikv/common/policy/RetryMaxMs.java
index 59a7a122467..888ccb1f1bc 100644
--- a/src/main/java/org/tikv/common/policy/RetryMaxMs.java
+++ b/src/main/java/org/tikv/common/policy/RetryMaxMs.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.policy;
diff --git a/src/main/java/org/tikv/common/policy/RetryPolicy.java b/src/main/java/org/tikv/common/policy/RetryPolicy.java
index 082b424345d..4dc05d95b84 100644
--- a/src/main/java/org/tikv/common/policy/RetryPolicy.java
+++ b/src/main/java/org/tikv/common/policy/RetryPolicy.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.policy;
@@ -21,23 +23,31 @@
import io.prometheus.client.Histogram;
import java.util.concurrent.Callable;
import org.tikv.common.exception.GrpcException;
+import org.tikv.common.log.SlowLogSpan;
import org.tikv.common.operation.ErrorHandler;
import org.tikv.common.util.BackOffer;
import org.tikv.common.util.ConcreteBackOffer;
+import org.tikv.common.util.HistogramUtils;
public abstract class RetryPolicy {
BackOffer backOffer = ConcreteBackOffer.newCopNextMaxBackOff();
public static final Histogram GRPC_SINGLE_REQUEST_LATENCY =
- Histogram.build()
+ HistogramUtils.buildDuration()
.name("client_java_grpc_single_requests_latency")
.help("grpc request latency.")
- .labelNames("type")
+ .labelNames("type", "cluster")
+ .register();
+ public static final Histogram CALL_WITH_RETRY_DURATION =
+ HistogramUtils.buildDuration()
+ .name("client_java_call_with_retry_duration")
+ .help("callWithRetry duration.")
+ .labelNames("type", "cluster")
.register();
public static final Counter GRPC_REQUEST_RETRY_NUM =
Counter.build()
.name("client_java_grpc_requests_retry_num")
.help("grpc request retry num.")
- .labelNames("type")
+ .labelNames("type", "cluster")
.register();
// handles PD and TiKV's error.
@@ -61,38 +71,51 @@ private void rethrowNotRecoverableException(Exception e) {
}
}
- public RespT callWithRetry(Callable proc, String methodName) {
- while (true) {
- RespT result = null;
- try {
- // add single request duration histogram
- Histogram.Timer requestTimer = GRPC_SINGLE_REQUEST_LATENCY.labels(methodName).startTimer();
+ public RespT callWithRetry(Callable proc, String methodName, BackOffer backOffer) {
+ String[] labels = new String[] {methodName, backOffer.getClusterId().toString()};
+ Histogram.Timer callWithRetryTimer = CALL_WITH_RETRY_DURATION.labels(labels).startTimer();
+ SlowLogSpan callWithRetrySlowLogSpan = backOffer.getSlowLog().start("callWithRetry");
+ callWithRetrySlowLogSpan.addProperty("method", methodName);
+ try {
+ while (true) {
+ RespT result = null;
try {
- result = proc.call();
- } finally {
- requestTimer.observeDuration();
+ // add single request duration histogram
+ Histogram.Timer requestTimer = GRPC_SINGLE_REQUEST_LATENCY.labels(labels).startTimer();
+ SlowLogSpan slowLogSpan = backOffer.getSlowLog().start("gRPC");
+ slowLogSpan.addProperty("method", methodName);
+ try {
+ result = proc.call();
+ } finally {
+ slowLogSpan.end();
+ requestTimer.observeDuration();
+ }
+ } catch (Exception e) {
+ rethrowNotRecoverableException(e);
+ // Handle request call error
+ backOffer.checkTimeout();
+ boolean retry = handler.handleRequestError(backOffer, e);
+ if (retry) {
+ GRPC_REQUEST_RETRY_NUM.labels(labels).inc();
+ continue;
+ } else {
+ return result;
+ }
}
- } catch (Exception e) {
- rethrowNotRecoverableException(e);
- // Handle request call error
- boolean retry = handler.handleRequestError(backOffer, e);
- if (retry) {
- GRPC_REQUEST_RETRY_NUM.labels(methodName).inc();
- continue;
- } else {
- return result;
- }
- }
- // Handle response error
- if (handler != null) {
- boolean retry = handler.handleResponseError(backOffer, result);
- if (retry) {
- GRPC_REQUEST_RETRY_NUM.labels(methodName).inc();
- continue;
+ // Handle response error
+ if (handler != null) {
+ boolean retry = handler.handleResponseError(backOffer, result);
+ if (retry) {
+ GRPC_REQUEST_RETRY_NUM.labels(labels).inc();
+ continue;
+ }
}
+ return result;
}
- return result;
+ } finally {
+ callWithRetryTimer.observeDuration();
+ callWithRetrySlowLogSpan.end();
}
}
diff --git a/src/main/java/org/tikv/common/predicates/IndexRange.java b/src/main/java/org/tikv/common/predicates/IndexRange.java
index 2986f2037bd..3ab76d15a87 100644
--- a/src/main/java/org/tikv/common/predicates/IndexRange.java
+++ b/src/main/java/org/tikv/common/predicates/IndexRange.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.predicates;
diff --git a/src/main/java/org/tikv/common/predicates/PredicateUtils.java b/src/main/java/org/tikv/common/predicates/PredicateUtils.java
index bab456cbe58..76dd9d0d747 100644
--- a/src/main/java/org/tikv/common/predicates/PredicateUtils.java
+++ b/src/main/java/org/tikv/common/predicates/PredicateUtils.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.predicates;
diff --git a/src/main/java/org/tikv/common/predicates/ScanSpec.java b/src/main/java/org/tikv/common/predicates/ScanSpec.java
index 7668d0cdc29..7f56595003c 100644
--- a/src/main/java/org/tikv/common/predicates/ScanSpec.java
+++ b/src/main/java/org/tikv/common/predicates/ScanSpec.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.predicates;
diff --git a/src/main/java/org/tikv/common/predicates/SelectivityCalculator.java b/src/main/java/org/tikv/common/predicates/SelectivityCalculator.java
index be3e3566bcd..964b420771f 100644
--- a/src/main/java/org/tikv/common/predicates/SelectivityCalculator.java
+++ b/src/main/java/org/tikv/common/predicates/SelectivityCalculator.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.predicates;
diff --git a/src/main/java/org/tikv/common/predicates/TiKVScanAnalyzer.java b/src/main/java/org/tikv/common/predicates/TiKVScanAnalyzer.java
index 4e133a9356d..c7f9b092c54 100644
--- a/src/main/java/org/tikv/common/predicates/TiKVScanAnalyzer.java
+++ b/src/main/java/org/tikv/common/predicates/TiKVScanAnalyzer.java
@@ -1,16 +1,18 @@
/*
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
*/
package org.tikv.common.predicates;
diff --git a/src/main/java/org/tikv/common/region/AbstractRegionStoreClient.java b/src/main/java/org/tikv/common/region/AbstractRegionStoreClient.java
index c47bb854746..6e5eecf565f 100644
--- a/src/main/java/org/tikv/common/region/AbstractRegionStoreClient.java
+++ b/src/main/java/org/tikv/common/region/AbstractRegionStoreClient.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2019 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -20,9 +20,13 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.protobuf.ByteString;
import io.grpc.ManagedChannel;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;
+import io.prometheus.client.Histogram;
+import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@@ -30,24 +34,42 @@
import org.slf4j.LoggerFactory;
import org.tikv.common.AbstractGRPCClient;
import org.tikv.common.TiConfiguration;
+import org.tikv.common.apiversion.RequestKeyCodec;
import org.tikv.common.exception.GrpcException;
+import org.tikv.common.log.SlowLog;
+import org.tikv.common.log.SlowLogSpan;
+import org.tikv.common.util.BackOffer;
import org.tikv.common.util.ChannelFactory;
+import org.tikv.common.util.HistogramUtils;
import org.tikv.kvproto.Kvrpcpb;
import org.tikv.kvproto.Metapb;
import org.tikv.kvproto.TikvGrpc;
+import org.tikv.kvproto.Tracepb;
public abstract class AbstractRegionStoreClient
- extends AbstractGRPCClient
+ extends AbstractGRPCClient
implements RegionErrorReceiver {
+
private static final Logger logger = LoggerFactory.getLogger(AbstractRegionStoreClient.class);
+ public static final Histogram SEEK_LEADER_STORE_DURATION =
+ HistogramUtils.buildDuration()
+ .name("client_java_seek_leader_store_duration")
+ .help("seek leader store duration.")
+ .labelNames("cluster")
+ .register();
+
+ public static final Histogram SEEK_PROXY_STORE_DURATION =
+ HistogramUtils.buildDuration()
+ .name("client_java_seek_proxy_store_duration")
+ .help("seek proxy store duration.")
+ .labelNames("cluster")
+ .register();
+
protected final RegionManager regionManager;
+ protected final RequestKeyCodec codec;
protected TiRegion region;
- protected TiStore targetStore;
- protected TiStore originStore;
- private long retryForwardTimes;
- private long retryLeaderTimes;
- private Metapb.Peer candidateLeader;
+ protected TiStore store;
protected AbstractRegionStoreClient(
TiConfiguration conf,
@@ -55,7 +77,7 @@ protected AbstractRegionStoreClient(
TiStore store,
ChannelFactory channelFactory,
TikvGrpc.TikvBlockingStub blockingStub,
- TikvGrpc.TikvStub asyncStub,
+ TikvGrpc.TikvFutureStub asyncStub,
RegionManager regionManager) {
super(conf, channelFactory, blockingStub, asyncStub);
checkNotNull(region, "Region is empty");
@@ -63,15 +85,10 @@ protected AbstractRegionStoreClient(
checkArgument(region.getLeader() != null, "Leader Peer is null");
this.region = region;
this.regionManager = regionManager;
- this.targetStore = store;
- this.originStore = null;
- this.candidateLeader = null;
- this.retryForwardTimes = 0;
- this.retryLeaderTimes = 0;
- if (this.targetStore.getProxyStore() != null) {
+ this.store = store;
+ this.codec = regionManager.getPDClient().getCodec();
+ if (this.store.getProxyStore() != null) {
this.timeout = conf.getForwardTimeout();
- } else if (!this.targetStore.isReachable() && !this.targetStore.canForwardFirst()) {
- onStoreUnreachable();
}
}
@@ -86,7 +103,7 @@ protected TikvGrpc.TikvBlockingStub getBlockingStub() {
}
@Override
- protected TikvGrpc.TikvStub getAsyncStub() {
+ protected TikvGrpc.TikvFutureStub getAsyncStub() {
return asyncStub.withDeadlineAfter(getTimeout(), TimeUnit.MILLISECONDS);
}
@@ -100,7 +117,7 @@ public void close() throws GrpcException {}
* @return false when re-split is needed.
*/
@Override
- public boolean onNotLeader(TiRegion newRegion) {
+ public boolean onNotLeader(TiRegion newRegion, BackOffer backOffer) {
if (logger.isDebugEnabled()) {
logger.debug(region + ", new leader = " + newRegion.getLeader().getStoreId());
}
@@ -110,204 +127,311 @@ public boolean onNotLeader(TiRegion newRegion) {
return false;
}
- // If we try one peer but find the leader has not changed, we do not need try other peers.
- if (candidateLeader != null
- && region.getLeader().getStoreId() == newRegion.getLeader().getStoreId()) {
- retryLeaderTimes = newRegion.getFollowerList().size();
- originStore = null;
+ // If we try one peer but find the leader has not changed, we do not need to try other peers.
+ if (region.getLeader().getStoreId() == newRegion.getLeader().getStoreId()) {
+ store = null;
}
- candidateLeader = null;
region = newRegion;
- targetStore = regionManager.getStoreById(region.getLeader().getStoreId());
+ store = regionManager.getStoreById(region.getLeader().getStoreId(), backOffer);
updateClientStub();
return true;
}
@Override
- public boolean onStoreUnreachable() {
- if (!targetStore.isValid()) {
- logger.warn(
- String.format("store [%d] has been invalid", region.getId(), targetStore.getId()));
- targetStore = regionManager.getStoreById(targetStore.getId());
+ public boolean onStoreUnreachable(BackOffer backOffer) {
+ if (!store.isValid()) {
+ logger.warn(String.format("store [%d] has been invalid", store.getId()));
+ store = regionManager.getStoreById(store.getId(), backOffer);
updateClientStub();
return true;
}
- if (targetStore.getProxyStore() == null) {
- if (targetStore.isReachable()) {
- return true;
- }
+ // seek an available leader store to send request
+ backOffer.checkTimeout();
+ Boolean result = seekLeaderStore(backOffer);
+ if (result != null) {
+ return result;
}
-
- // If this store has failed to forward request too many times, we shall try other peer at first
- // so that we can
- // reduce the latency cost by fail requests.
- if (targetStore.canForwardFirst()) {
- if (conf.getEnableGrpcForward() && retryForwardTimes <= region.getFollowerList().size()) {
- return retryOtherStoreByProxyForward();
- }
- if (retryOtherStoreLeader()) {
- return true;
- }
- } else {
- if (retryOtherStoreLeader()) {
- return true;
- }
- if (conf.getEnableGrpcForward() && retryForwardTimes <= region.getFollowerList().size()) {
- return retryOtherStoreByProxyForward();
- }
- return true;
+ if (conf.getEnableGrpcForward()) {
+ // seek an available proxy store to forward request
+ backOffer.checkTimeout();
+ return seekProxyStore(backOffer);
}
-
- logger.warn(
- String.format(
- "retry time exceed for region[%d], invalid this region[%d]",
- region.getId(), targetStore.getId()));
- regionManager.onRequestFail(region);
return false;
}
- protected Kvrpcpb.Context makeContext(TiStoreType storeType) {
- if (candidateLeader != null && storeType == TiStoreType.TiKV) {
- return region.getReplicaContext(candidateLeader, java.util.Collections.emptySet());
- } else {
- return region.getReplicaContext(java.util.Collections.emptySet(), storeType);
+ private Kvrpcpb.Context addTraceId(Kvrpcpb.Context context, SlowLog slowLog) {
+ if (slowLog.getThresholdMS() < 0) {
+ // disable tikv tracing
+ return context;
}
+ long traceId = slowLog.getTraceId();
+ return Kvrpcpb.Context.newBuilder(context)
+ .setTraceContext(
+ Tracepb.TraceContext.newBuilder()
+ .setDurationThresholdMs(
+ (int) (slowLog.getThresholdMS() * conf.getRawKVServerSlowLogFactor()))
+ .addRemoteParentSpans(Tracepb.RemoteParentSpan.newBuilder().setTraceId(traceId)))
+ .build();
}
- protected Kvrpcpb.Context makeContext(Set resolvedLocks, TiStoreType storeType) {
- if (candidateLeader != null && storeType == TiStoreType.TiKV) {
- return region.getReplicaContext(candidateLeader, resolvedLocks);
- } else {
- return region.getReplicaContext(resolvedLocks, storeType);
+ protected Kvrpcpb.Context makeContext(TiStoreType storeType, SlowLog slowLog) {
+ Kvrpcpb.Context context = region.getReplicaContext(java.util.Collections.emptySet(), storeType);
+ return addTraceId(context, slowLog);
+ }
+
+ protected Kvrpcpb.Context makeContext(
+ Set resolvedLocks, TiStoreType storeType, SlowLog slowLog) {
+ Kvrpcpb.Context context = region.getReplicaContext(resolvedLocks, storeType);
+ return addTraceId(context, slowLog);
+ }
+
+ protected Kvrpcpb.Context makeContext() {
+ return region.getLeaderContext();
+ }
+
+ protected Kvrpcpb.Context makeContext(Metapb.Peer peer) {
+ return region.getReplicaContext(peer);
+ }
+
+ private void updateClientStub() {
+ String addressStr = store.getStore().getAddress();
+ long deadline = timeout;
+ if (store.getProxyStore() != null) {
+ addressStr = store.getProxyStore().getAddress();
+ deadline = conf.getForwardTimeout();
+ }
+ ManagedChannel channel =
+ channelFactory.getChannel(addressStr, regionManager.getPDClient().getHostMapping());
+ blockingStub =
+ TikvGrpc.newBlockingStub(channel).withDeadlineAfter(deadline, TimeUnit.MILLISECONDS);
+ asyncStub = TikvGrpc.newFutureStub(channel).withDeadlineAfter(deadline, TimeUnit.MILLISECONDS);
+ if (store.getProxyStore() != null) {
+ Metadata header = new Metadata();
+ header.put(TiConfiguration.FORWARD_META_DATA_KEY, store.getStore().getAddress());
+ blockingStub =
+ blockingStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(header));
+ asyncStub = asyncStub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(header));
}
}
- @Override
- public void tryUpdateRegionStore() {
- if (originStore != null) {
- if (originStore.getId() == targetStore.getId()) {
+ private Boolean seekLeaderStore(BackOffer backOffer) {
+ Histogram.Timer switchLeaderDurationTimer =
+ SEEK_LEADER_STORE_DURATION
+ .labels(regionManager.getPDClient().getClusterId().toString())
+ .startTimer();
+ SlowLogSpan slowLogSpan = backOffer.getSlowLog().start("seekLeaderStore");
+ try {
+ List peers = region.getFollowerList();
+ if (peers.isEmpty()) {
+ // no followers available, retry
+ logger.warn(String.format("no followers of region[%d] available, retry", region.getId()));
+ regionManager.onRequestFail(region);
+ return false;
+ }
+
+ logger.info(String.format("try switch leader: region[%d]", region.getId()));
+
+ Metapb.Peer peer = switchLeaderStore(backOffer);
+ if (peer != null) {
+ // we found a leader
+ TiStore currentLeaderStore = regionManager.getStoreById(peer.getStoreId(), backOffer);
+ if (currentLeaderStore.isReachable()) {
+ logger.info(
+ String.format(
+ "update leader using switchLeader logic from store[%d] to store[%d]",
+ region.getLeader().getStoreId(), peer.getStoreId()));
+ // update region cache
+ TiRegion result = regionManager.updateLeader(region, peer.getStoreId());
+ if (result != null) {
+ region = result;
+ // switch to leader store
+ store = currentLeaderStore;
+ updateClientStub();
+ return true;
+ }
+ return false;
+ }
+ } else {
+ // no leader found, some response does not return normally, there may be network partition.
logger.warn(
String.format(
- "update store [%s] by proxy-store [%s]",
- targetStore.getStore().getAddress(), targetStore.getProxyStore().getAddress()));
- // We do not need to mark the store can-forward, because if one store has grpc forward
- // successfully, it will
- // create a new store object, which is can-forward.
- regionManager.updateStore(originStore, targetStore);
- } else {
- // If we try to forward request to leader by follower failed, it means that the store of old
- // leader may be
- // unavailable but the new leader has not been report to PD. So we can ban this store for a
- // short time to
- // avoid too many request try forward rather than try other peer.
- originStore.forwardFail();
+ "leader for region[%d] is not found, it is possible that network partition occurred",
+ region.getId()));
}
+ } finally {
+ switchLeaderDurationTimer.observeDuration();
+ slowLogSpan.end();
}
- if (candidateLeader != null) {
- logger.warn(
- String.format(
- "update leader to store [%d] for region[%d]",
- candidateLeader.getStoreId(), region.getId()));
- this.regionManager.updateLeader(region, candidateLeader.getStoreId());
+ return null;
+ }
+
+ private boolean seekProxyStore(BackOffer backOffer) {
+ SlowLogSpan slowLogSpan = backOffer.getSlowLog().start("seekProxyStore");
+ Histogram.Timer grpcForwardDurationTimer =
+ SEEK_PROXY_STORE_DURATION
+ .labels(regionManager.getPDClient().getClusterId().toString())
+ .startTimer();
+ try {
+ logger.info(String.format("try grpc forward: region[%d]", region.getId()));
+ // when current leader cannot be reached
+ TiStore storeWithProxy = switchProxyStore(backOffer);
+ if (storeWithProxy == null) {
+ // no store available, retry
+ logger.warn(String.format("No store available, retry: region[%d]", region.getId()));
+ return false;
+ }
+ // use proxy store to forward requests
+ regionManager.updateStore(store, storeWithProxy);
+ store = storeWithProxy;
+ updateClientStub();
+ return true;
+ } finally {
+ grpcForwardDurationTimer.observeDuration();
+ slowLogSpan.end();
}
}
- private boolean retryOtherStoreLeader() {
- List peers = region.getFollowerList();
- if (retryLeaderTimes >= peers.size()) {
- return false;
+ // first: leader peer, second: true if any responses returned with grpc error
+ private Metapb.Peer switchLeaderStore(BackOffer backOffer) {
+ List responses = new LinkedList<>();
+ for (Metapb.Peer peer : region.getFollowerList()) {
+ ByteString key = region.getStartKey();
+ try {
+ TiStore peerStore = regionManager.getStoreById(peer.getStoreId(), backOffer);
+ ManagedChannel channel =
+ channelFactory.getChannel(
+ peerStore.getAddress(), regionManager.getPDClient().getHostMapping());
+ TikvGrpc.TikvFutureStub stub =
+ TikvGrpc.newFutureStub(channel).withDeadlineAfter(timeout, TimeUnit.MILLISECONDS);
+ Kvrpcpb.RawGetRequest rawGetRequest =
+ Kvrpcpb.RawGetRequest.newBuilder()
+ .setContext(makeContext(peer))
+ .setKey(codec.encodeKey(key))
+ .build();
+ ListenableFuture task = stub.rawGet(rawGetRequest);
+ responses.add(new SwitchLeaderTask(task, peer));
+ } catch (Exception e) {
+ logger.warn(
+ "switch region[{}] leader store to {} failed: {}",
+ region.getId(),
+ peer.getStoreId(),
+ e);
+ }
}
- retryLeaderTimes += 1;
- boolean hasVisitedStore = false;
- for (Metapb.Peer cur : peers) {
- if (candidateLeader == null || hasVisitedStore) {
- TiStore store = regionManager.getStoreById(cur.getStoreId());
- if (store != null && store.isReachable()) {
- targetStore = store;
- candidateLeader = cur;
- logger.warn(
- String.format(
- "try store [%d],peer[%d] for region[%d], which may be new leader",
- targetStore.getId(), candidateLeader.getId(), region.getId()));
- updateClientStub();
- return true;
- } else {
+ while (true) {
+ try {
+ Thread.sleep(2);
+ } catch (InterruptedException e) {
+ throw new GrpcException(e);
+ }
+ List unfinished = new LinkedList<>();
+ for (SwitchLeaderTask task : responses) {
+ if (!task.task.isDone()) {
+ unfinished.add(task);
continue;
}
- } else if (candidateLeader.getId() == cur.getId()) {
- hasVisitedStore = true;
+ try {
+ Kvrpcpb.RawGetResponse resp = task.task.get();
+ if (resp != null) {
+ if (!resp.hasRegionError()) {
+ // the peer is leader
+ logger.info(
+ String.format("rawGet response indicates peer[%d] is leader", task.peer.getId()));
+ return task.peer;
+ }
+ }
+ } catch (Exception ignored) {
+ }
}
+ if (unfinished.isEmpty()) {
+ return null;
+ }
+ responses = unfinished;
}
- candidateLeader = null;
- retryLeaderTimes = peers.size();
- return false;
}
- private void updateClientStub() {
- String addressStr = targetStore.getStore().getAddress();
- if (targetStore.getProxyStore() != null) {
- addressStr = targetStore.getProxyStore().getAddress();
+ private TiStore switchProxyStore(BackOffer backOffer) {
+ long forwardTimeout = conf.getForwardTimeout();
+ List responses = new LinkedList<>();
+ for (Metapb.Peer peer : region.getFollowerList()) {
+ ByteString key = region.getStartKey();
+ try {
+ TiStore peerStore = regionManager.getStoreById(peer.getStoreId(), backOffer);
+ ManagedChannel channel =
+ channelFactory.getChannel(
+ peerStore.getAddress(), regionManager.getPDClient().getHostMapping());
+ TikvGrpc.TikvFutureStub stub =
+ TikvGrpc.newFutureStub(channel)
+ .withDeadlineAfter(forwardTimeout, TimeUnit.MILLISECONDS);
+ Metadata header = new Metadata();
+ header.put(TiConfiguration.FORWARD_META_DATA_KEY, store.getStore().getAddress());
+ Kvrpcpb.RawGetRequest rawGetRequest =
+ Kvrpcpb.RawGetRequest.newBuilder()
+ .setContext(makeContext())
+ .setKey(codec.encodeKey(key))
+ .build();
+ ListenableFuture task =
+ stub.withInterceptors(MetadataUtils.newAttachHeadersInterceptor(header))
+ .rawGet(rawGetRequest);
+ responses.add(new ForwardCheckTask(task, peerStore.getStore()));
+ } catch (Exception e) {
+ logger.warn(
+ "switch region[{}] leader store to {} failed: {}",
+ region.getId(),
+ peer.getStoreId(),
+ e);
+ }
}
- ManagedChannel channel =
- channelFactory.getChannel(addressStr, regionManager.getPDClient().getHostMapping());
- blockingStub = TikvGrpc.newBlockingStub(channel);
- asyncStub = TikvGrpc.newStub(channel);
- if (targetStore.getProxyStore() != null) {
- Metadata header = new Metadata();
- header.put(TiConfiguration.FORWARD_META_DATA_KEY, targetStore.getStore().getAddress());
- blockingStub = MetadataUtils.attachHeaders(blockingStub, header);
- asyncStub = MetadataUtils.attachHeaders(asyncStub, header);
+ while (true) {
+ try {
+ Thread.sleep(2);
+ } catch (InterruptedException e) {
+ throw new GrpcException(e);
+ }
+ List unfinished = new LinkedList<>();
+ for (ForwardCheckTask task : responses) {
+ if (!task.task.isDone()) {
+ unfinished.add(task);
+ continue;
+ }
+ try {
+ // any answer will do
+ Kvrpcpb.RawGetResponse resp = task.task.get();
+ logger.info(
+ String.format(
+ "rawGetResponse indicates forward from [%s] to [%s]",
+ task.store.getAddress(), store.getAddress()));
+ return store.withProxy(task.store);
+ } catch (Exception ignored) {
+ }
+ }
+ if (unfinished.isEmpty()) {
+ return null;
+ }
+ responses = unfinished;
}
}
- private boolean retryOtherStoreByProxyForward() {
- TiStore proxyStore = switchProxyStore();
- if (proxyStore == null) {
- logger.warn(
- String.format(
- "no forward store can be selected for store [%s] and region[%d]",
- targetStore.getStore().getAddress(), region.getId()));
- return false;
- }
- if (originStore == null) {
- originStore = targetStore;
- if (this.targetStore.getProxyStore() != null) {
- this.timeout = conf.getForwardTimeout();
- }
+ private static class SwitchLeaderTask {
+
+ private final ListenableFuture task;
+ private final Metapb.Peer peer;
+
+ private SwitchLeaderTask(ListenableFuture task, Metapb.Peer peer) {
+ this.task = task;
+ this.peer = peer;
}
- targetStore = proxyStore;
- retryForwardTimes += 1;
- updateClientStub();
- logger.warn(
- String.format(
- "forward request to store [%s] by store [%s] for region[%d]",
- targetStore.getStore().getAddress(),
- targetStore.getProxyStore().getAddress(),
- region.getId()));
- return true;
}
- private TiStore switchProxyStore() {
- boolean hasVisitedStore = false;
- List peers = region.getFollowerList();
- if (peers.isEmpty()) {
- return null;
- }
- Metapb.Store proxyStore = targetStore.getProxyStore();
- if (proxyStore == null || peers.get(peers.size() - 1).getStoreId() == proxyStore.getId()) {
- hasVisitedStore = true;
- }
- for (Metapb.Peer peer : peers) {
- if (hasVisitedStore) {
- TiStore store = regionManager.getStoreById(peer.getStoreId());
- if (store.isReachable()) {
- return targetStore.withProxy(store.getStore());
- }
- } else if (peer.getStoreId() == proxyStore.getId()) {
- hasVisitedStore = true;
- }
+ private static class ForwardCheckTask {
+
+ private final ListenableFuture task;
+ private final Metapb.Store store;
+
+ private ForwardCheckTask(ListenableFuture task, Metapb.Store store) {
+ this.task = task;
+ this.store = store;
}
- return null;
}
}
diff --git a/src/main/java/org/tikv/common/region/RegionCache.java b/src/main/java/org/tikv/common/region/RegionCache.java
index 77f41010a67..c7192bef473 100644
--- a/src/main/java/org/tikv/common/region/RegionCache.java
+++ b/src/main/java/org/tikv/common/region/RegionCache.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2021 TiKV Project Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
package org.tikv.common.region;
import static org.tikv.common.codec.KeyUtils.formatBytesUTF8;
@@ -104,6 +120,18 @@ public synchronized void invalidateRegion(TiRegion region) {
}
}
+ public synchronized void insertRegionToCache(TiRegion region) {
+ try {
+ TiRegion oldRegion = regionCache.get(region.getId());
+ if (oldRegion != null) {
+ keyToRegionIdCache.remove(makeRange(oldRegion.getStartKey(), oldRegion.getEndKey()));
+ }
+ regionCache.put(region.getId(), region);
+ keyToRegionIdCache.put(makeRange(region.getStartKey(), region.getEndKey()), region.getId());
+ } catch (Exception ignore) {
+ }
+ }
+
public synchronized boolean updateRegion(TiRegion expected, TiRegion region) {
try {
if (logger.isDebugEnabled()) {
@@ -129,8 +157,12 @@ public synchronized boolean updateStore(TiStore oldStore, TiStore newStore) {
if (!newStore.isValid()) {
return false;
}
+ if (oldStore == null) {
+ storeCache.put(newStore.getId(), newStore);
+ return true;
+ }
TiStore originStore = storeCache.get(oldStore.getId());
- if (originStore == oldStore) {
+ if (originStore.equals(oldStore)) {
storeCache.put(newStore.getId(), newStore);
oldStore.markInvalid();
return true;
diff --git a/src/main/java/org/tikv/common/region/RegionErrorReceiver.java b/src/main/java/org/tikv/common/region/RegionErrorReceiver.java
index 0a5bcabf037..edd1d5d7ca0 100644
--- a/src/main/java/org/tikv/common/region/RegionErrorReceiver.java
+++ b/src/main/java/org/tikv/common/region/RegionErrorReceiver.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -17,13 +17,13 @@
package org.tikv.common.region;
+import org.tikv.common.util.BackOffer;
+
public interface RegionErrorReceiver {
- boolean onNotLeader(TiRegion region);
+ boolean onNotLeader(TiRegion region, BackOffer backOffer);
/// return whether we need to retry this request.
- boolean onStoreUnreachable();
-
- void tryUpdateRegionStore();
+ boolean onStoreUnreachable(BackOffer backOffer);
TiRegion getRegion();
}
diff --git a/src/main/java/org/tikv/common/region/RegionManager.java b/src/main/java/org/tikv/common/region/RegionManager.java
index ada9bb73052..9678d9e813b 100644
--- a/src/main/java/org/tikv/common/region/RegionManager.java
+++ b/src/main/java/org/tikv/common/region/RegionManager.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -21,32 +21,49 @@
import com.google.protobuf.ByteString;
import io.prometheus.client.Histogram;
+import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tikv.common.ReadOnlyPDClient;
import org.tikv.common.TiConfiguration;
+import org.tikv.common.event.CacheInvalidateEvent;
import org.tikv.common.exception.GrpcException;
+import org.tikv.common.exception.InvalidStoreException;
import org.tikv.common.exception.TiClientInternalException;
+import org.tikv.common.log.SlowLogSpan;
import org.tikv.common.util.BackOffer;
import org.tikv.common.util.ChannelFactory;
import org.tikv.common.util.ConcreteBackOffer;
+import org.tikv.common.util.HistogramUtils;
import org.tikv.common.util.Pair;
import org.tikv.kvproto.Metapb;
import org.tikv.kvproto.Metapb.Peer;
import org.tikv.kvproto.Metapb.StoreState;
+import org.tikv.kvproto.Pdpb;
@SuppressWarnings("UnstableApiUsage")
public class RegionManager {
+
private static final Logger logger = LoggerFactory.getLogger(RegionManager.class);
public static final Histogram GET_REGION_BY_KEY_REQUEST_LATENCY =
- Histogram.build()
+ HistogramUtils.buildDuration()
.name("client_java_get_region_by_requests_latency")
.help("getRegionByKey request latency.")
+ .labelNames("cluster")
+ .register();
+ public static final Histogram SCAN_REGIONS_REQUEST_LATENCY =
+ HistogramUtils.buildDuration()
+ .name("client_java_scan_regions_request_latency")
+ .help("scanRegions request latency.")
+ .labelNames("cluster")
.register();
// TODO: the region cache logic need rewrite.
@@ -56,9 +73,36 @@ public class RegionManager {
private final TiConfiguration conf;
private final ScheduledExecutorService executor;
private final StoreHealthyChecker storeChecker;
+ private final CopyOnWriteArrayList>
+ cacheInvalidateCallbackList;
+ private final ExecutorService callBackThreadPool;
+ private AtomicInteger tiflashStoreIndex = new AtomicInteger(0);
public RegionManager(
TiConfiguration conf, ReadOnlyPDClient pdClient, ChannelFactory channelFactory) {
+ this(conf, pdClient, channelFactory, 1);
+ }
+
+ public RegionManager(TiConfiguration conf, ReadOnlyPDClient pdClient) {
+ this(conf, pdClient, 1);
+ }
+
+ public RegionManager(
+ TiConfiguration conf, ReadOnlyPDClient pdClient, int callBackExecutorThreadNum) {
+ this.cache = new RegionCache();
+ this.pdClient = pdClient;
+ this.conf = conf;
+ this.storeChecker = null;
+ this.executor = null;
+ this.cacheInvalidateCallbackList = new CopyOnWriteArrayList<>();
+ this.callBackThreadPool = Executors.newFixedThreadPool(callBackExecutorThreadNum);
+ }
+
+ public RegionManager(
+ TiConfiguration conf,
+ ReadOnlyPDClient pdClient,
+ ChannelFactory channelFactory,
+ int callBackExecutorThreadNum) {
this.cache = new RegionCache();
this.pdClient = pdClient;
this.conf = conf;
@@ -69,36 +113,63 @@ public RegionManager(
this.storeChecker = storeChecker;
this.executor = Executors.newScheduledThreadPool(1);
this.executor.scheduleAtFixedRate(storeChecker, period, period, TimeUnit.MILLISECONDS);
- }
-
- public RegionManager(TiConfiguration conf, ReadOnlyPDClient pdClient) {
- this.cache = new RegionCache();
- this.pdClient = pdClient;
- this.conf = conf;
- this.storeChecker = null;
- this.executor = null;
+ this.cacheInvalidateCallbackList = new CopyOnWriteArrayList<>();
+ this.callBackThreadPool = Executors.newFixedThreadPool(callBackExecutorThreadNum);
}
public synchronized void close() {
if (this.executor != null) {
this.executor.shutdownNow();
}
+ this.callBackThreadPool.shutdownNow();
}
public ReadOnlyPDClient getPDClient() {
return this.pdClient;
}
+ public ExecutorService getCallBackThreadPool() {
+ return callBackThreadPool;
+ }
+
+ public List> getCacheInvalidateCallbackList() {
+ return cacheInvalidateCallbackList;
+ }
+
+ public void addCacheInvalidateCallback(
+ Function cacheInvalidateCallback) {
+ this.cacheInvalidateCallbackList.add(cacheInvalidateCallback);
+ }
+
public void invalidateAll() {
cache.invalidateAll();
}
+ public List scanRegions(
+ BackOffer backOffer, ByteString startKey, ByteString endKey, int limit) {
+ Long clusterId = pdClient.getClusterId();
+ Histogram.Timer requestTimer =
+ SCAN_REGIONS_REQUEST_LATENCY.labels(clusterId.toString()).startTimer();
+ SlowLogSpan slowLogSpan = backOffer.getSlowLog().start("scanRegions");
+ try {
+ return pdClient.scanRegions(backOffer, startKey, endKey, limit);
+ } catch (Exception e) {
+ return new ArrayList<>();
+ } finally {
+ requestTimer.observeDuration();
+ slowLogSpan.end();
+ }
+ }
+
public TiRegion getRegionByKey(ByteString key) {
- return getRegionByKey(key, ConcreteBackOffer.newGetBackOff());
+ return getRegionByKey(key, defaultBackOff());
}
public TiRegion getRegionByKey(ByteString key, BackOffer backOffer) {
- Histogram.Timer requestTimer = GET_REGION_BY_KEY_REQUEST_LATENCY.startTimer();
+ Long clusterId = pdClient.getClusterId();
+ Histogram.Timer requestTimer =
+ GET_REGION_BY_KEY_REQUEST_LATENCY.labels(clusterId.toString()).startTimer();
+ SlowLogSpan slowLogSpan = backOffer.getSlowLog().start("getRegionByKey");
TiRegion region = cache.getRegionByKey(key, backOffer);
try {
if (region == null) {
@@ -106,11 +177,17 @@ public TiRegion getRegionByKey(ByteString key, BackOffer backOffer) {
Pair regionAndLeader = pdClient.getRegionByKey(backOffer, key);
region =
cache.putRegion(createRegion(regionAndLeader.first, regionAndLeader.second, backOffer));
+ logger.debug(
+ String.format(
+ "get region id: %d with leader: %d",
+ region.getId(), region.getLeader().getStoreId()));
}
} catch (Exception e) {
+ logger.warn("Get region failed: ", e);
return null;
} finally {
requestTimer.observeDuration();
+ slowLogSpan.end();
}
return region;
@@ -124,7 +201,7 @@ public TiRegion getRegionByKey(ByteString key, BackOffer backOffer) {
// Consider region A, B. After merge of (A, B) -> A, region ID B does not exist.
// This request is unrecoverable.
public TiRegion getRegionById(long regionId) {
- BackOffer backOffer = ConcreteBackOffer.newGetBackOff();
+ BackOffer backOffer = defaultBackOff();
TiRegion region = cache.getRegionById(regionId);
if (region == null) {
Pair regionAndLeader =
@@ -144,66 +221,96 @@ public Pair getRegionStorePairByKey(ByteString key) {
}
public Pair getRegionStorePairByKey(ByteString key, TiStoreType storeType) {
- return getRegionStorePairByKey(key, storeType, ConcreteBackOffer.newGetBackOff());
+ return getRegionStorePairByKey(key, storeType, defaultBackOff());
}
public Pair getRegionStorePairByKey(
ByteString key, TiStoreType storeType, BackOffer backOffer) {
TiRegion region = getRegionByKey(key, backOffer);
- if (!region.isValid()) {
+ if (region == null || !region.isValid()) {
throw new TiClientInternalException("Region invalid: " + region);
}
TiStore store = null;
if (storeType == TiStoreType.TiKV) {
- Peer peer = region.getCurrentReplica();
- store = getStoreById(peer.getStoreId(), backOffer);
- if (store == null) {
- cache.clearAll();
+ // check from the first replica in case it recovers
+ List replicaList = region.getReplicaList();
+ for (int i = 0; i < replicaList.size(); i++) {
+ Peer peer = replicaList.get(i);
+ store = getStoreById(peer.getStoreId(), backOffer);
+ if (store.isReachable()) {
+ // update replica's index
+ region.setReplicaIdx(i);
+ break;
+ }
+ logger.info("Store {} is unreachable, try to get the next replica", peer.getStoreId());
+ }
+ // Does not set unreachable store to null in case it is incompatible with GrpcForward
+ if (store == null || !store.isReachable()) {
+ logger.warn("No TiKV store available for region: " + region);
}
} else {
- outerLoop:
+ List tiflashStores = new ArrayList<>();
for (Peer peer : region.getLearnerList()) {
TiStore s = getStoreById(peer.getStoreId(), backOffer);
- for (Metapb.StoreLabel label : s.getStore().getLabelsList()) {
- if (label.getKey().equals(storeType.getLabelKey())
- && label.getValue().equals(storeType.getLabelValue())) {
- store = s;
- break outerLoop;
- }
+ if (!s.isReachable()) {
+ continue;
+ }
+ if (s.isTiFlash()) {
+ tiflashStores.add(s);
}
}
+ // select a tiflash with Round-Robin strategy
+ if (tiflashStores.size() > 0) {
+ store =
+ tiflashStores.get(
+ Math.floorMod(tiflashStoreIndex.getAndIncrement(), tiflashStores.size()));
+ }
+
if (store == null) {
- // clear the region cache so we may get the learner peer next time
+ // clear the region cache, so we may get the learner peer next time
cache.invalidateRegion(region);
}
}
-
- if (store == null) {
- throw new TiClientInternalException(
- "Cannot find valid store on " + storeType + " for region " + region);
- }
-
return Pair.create(region, store);
}
- private TiRegion createRegion(Metapb.Region region, Metapb.Peer leader, BackOffer backOffer) {
- List peers = region.getPeersList();
- List stores = getRegionStore(peers, backOffer);
- return new TiRegion(conf, region, leader, peers, stores);
+ public TiRegion createRegion(Metapb.Region region, BackOffer backOffer) {
+ return createRegion(region, null, backOffer);
}
- private List getRegionStore(List peers, BackOffer backOffer) {
- return peers.stream().map(p -> getStoreById(p.getStoreId())).collect(Collectors.toList());
+ private TiRegion createRegion(Metapb.Region region, Metapb.Peer leader, BackOffer backOffer) {
+ List peers = new ArrayList<>();
+ List stores = new ArrayList<>();
+ for (Metapb.Peer peer : region.getPeersList()) {
+ try {
+ stores.add(getStoreById(peer.getStoreId(), backOffer));
+ peers.add(peer);
+ } catch (Exception e) {
+ logger.warn("Store {} not found: {}", peer.getStoreId(), e.toString());
+ }
+ }
+ Metapb.Region newRegion =
+ Metapb.Region.newBuilder().mergeFrom(region).clearPeers().addAllPeers(peers).build();
+ return new TiRegion(conf, newRegion, leader, peers, stores);
}
- public TiStore getStoreById(long id, BackOffer backOffer) {
+ private TiStore getStoreByIdWithBackOff(long id, BackOffer backOffer) {
try {
TiStore store = cache.getStoreById(id);
if (store == null) {
store = new TiStore(pdClient.getStore(backOffer, id));
+ } else {
+ return store;
+ }
+ // if we did not get store info from pd, remove store from cache
+ if (store.getStore() == null) {
+ logger.warn(String.format("failed to get store %d from pd", id));
+ return null;
}
+ // if the store is already tombstone, remove store from cache
if (store.getStore().getState().equals(StoreState.Tombstone)) {
+ logger.warn(String.format("store %d is tombstone", id));
return null;
}
if (cache.putStore(id, store) && storeChecker != null) {
@@ -216,7 +323,17 @@ public TiStore getStoreById(long id, BackOffer backOffer) {
}
public TiStore getStoreById(long id) {
- return getStoreById(id, ConcreteBackOffer.newGetBackOff());
+ return getStoreById(id, defaultBackOff());
+ }
+
+ public TiStore getStoreById(long id, BackOffer backOffer) {
+ TiStore store = getStoreByIdWithBackOff(id, backOffer);
+ if (store == null) {
+ logger.warn(String.format("failed to fetch store %d, the store may be missing", id));
+ cache.clearAll();
+ throw new InvalidStoreException(id);
+ }
+ return store;
}
public void onRegionStale(TiRegion region) {
@@ -264,4 +381,13 @@ public void invalidateStore(long storeId) {
public void invalidateRegion(TiRegion region) {
cache.invalidateRegion(region);
}
+
+ public void insertRegionToCache(TiRegion region) {
+ cache.insertRegionToCache(region);
+ }
+
+ private BackOffer defaultBackOff() {
+ return ConcreteBackOffer.newCustomBackOff(
+ conf.getRawKVDefaultBackoffInMS(), pdClient.getClusterId());
+ }
}
diff --git a/src/main/java/org/tikv/common/region/RegionStoreClient.java b/src/main/java/org/tikv/common/region/RegionStoreClient.java
index bfe655e9129..9a4ed807503 100644
--- a/src/main/java/org/tikv/common/region/RegionStoreClient.java
+++ b/src/main/java/org/tikv/common/region/RegionStoreClient.java
@@ -1,15 +1,15 @@
/*
- *
- * Copyright 2017 PingCAP, Inc.
+ * Copyright 2021 TiKV Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
@@ -18,7 +18,9 @@
package org.tikv.common.region;
import static org.tikv.common.region.RegionStoreClient.RequestTypes.REQ_TYPE_DAG;
-import static org.tikv.common.util.BackOffFunction.BackOffFuncType.*;
+import static org.tikv.common.util.BackOffFunction.BackOffFuncType.BoRegionMiss;
+import static org.tikv.common.util.BackOffFunction.BackOffFuncType.BoTxnLock;
+import static org.tikv.common.util.BackOffFunction.BackOffFuncType.BoTxnLockFast;
import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.ByteString;
@@ -29,26 +31,88 @@
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;
import io.prometheus.client.Histogram;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.Set;
import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tikv.common.PDClient;
import org.tikv.common.StoreVersion;
import org.tikv.common.TiConfiguration;
import org.tikv.common.Version;
-import org.tikv.common.exception.*;
+import org.tikv.common.exception.GrpcException;
+import org.tikv.common.exception.KeyException;
+import org.tikv.common.exception.RawCASConflictException;
+import org.tikv.common.exception.RegionException;
+import org.tikv.common.exception.SelectException;
+import org.tikv.common.exception.TiClientInternalException;
+import org.tikv.common.exception.TiKVException;
+import org.tikv.common.log.SlowLogEmptyImpl;
import org.tikv.common.operation.KVErrorHandler;
import org.tikv.common.operation.RegionErrorHandler;
import org.tikv.common.streaming.StreamingResponse;
-import org.tikv.common.util.*;
+import org.tikv.common.util.BackOffFunction;
+import org.tikv.common.util.BackOffer;
+import org.tikv.common.util.Batch;
+import org.tikv.common.util.ChannelFactory;
+import org.tikv.common.util.ConcreteBackOffer;
+import org.tikv.common.util.HistogramUtils;
+import org.tikv.common.util.Pair;
+import org.tikv.common.util.RangeSplitter;
import org.tikv.kvproto.Coprocessor;
import org.tikv.kvproto.Errorpb;
-import org.tikv.kvproto.Kvrpcpb.*;
+import org.tikv.kvproto.Kvrpcpb.BatchGetRequest;
+import org.tikv.kvproto.Kvrpcpb.BatchGetResponse;
+import org.tikv.kvproto.Kvrpcpb.CommitRequest;
+import org.tikv.kvproto.Kvrpcpb.CommitResponse;
+import org.tikv.kvproto.Kvrpcpb.GetRequest;
+import org.tikv.kvproto.Kvrpcpb.GetResponse;
+import org.tikv.kvproto.Kvrpcpb.KeyError;
+import org.tikv.kvproto.Kvrpcpb.KvPair;
+import org.tikv.kvproto.Kvrpcpb.Mutation;
+import org.tikv.kvproto.Kvrpcpb.PrewriteRequest;
+import org.tikv.kvproto.Kvrpcpb.PrewriteResponse;
+import org.tikv.kvproto.Kvrpcpb.RawBatchDeleteRequest;
+import org.tikv.kvproto.Kvrpcpb.RawBatchDeleteResponse;
+import org.tikv.kvproto.Kvrpcpb.RawBatchGetRequest;
+import org.tikv.kvproto.Kvrpcpb.RawBatchGetResponse;
+import org.tikv.kvproto.Kvrpcpb.RawBatchPutRequest;
+import org.tikv.kvproto.Kvrpcpb.RawBatchPutResponse;
+import org.tikv.kvproto.Kvrpcpb.RawCASRequest;
+import org.tikv.kvproto.Kvrpcpb.RawCASResponse;
+import org.tikv.kvproto.Kvrpcpb.RawDeleteRangeRequest;
+import org.tikv.kvproto.Kvrpcpb.RawDeleteRangeResponse;
+import org.tikv.kvproto.Kvrpcpb.RawDeleteRequest;
+import org.tikv.kvproto.Kvrpcpb.RawDeleteResponse;
+import org.tikv.kvproto.Kvrpcpb.RawGetKeyTTLRequest;
+import org.tikv.kvproto.Kvrpcpb.RawGetKeyTTLResponse;
+import org.tikv.kvproto.Kvrpcpb.RawGetRequest;
+import org.tikv.kvproto.Kvrpcpb.RawGetResponse;
+import org.tikv.kvproto.Kvrpcpb.RawPutRequest;
+import org.tikv.kvproto.Kvrpcpb.RawPutResponse;
+import org.tikv.kvproto.Kvrpcpb.RawScanRequest;
+import org.tikv.kvproto.Kvrpcpb.RawScanResponse;
+import org.tikv.kvproto.Kvrpcpb.ScanRequest;
+import org.tikv.kvproto.Kvrpcpb.ScanResponse;
+import org.tikv.kvproto.Kvrpcpb.SplitRegionRequest;
+import org.tikv.kvproto.Kvrpcpb.SplitRegionResponse;
+import org.tikv.kvproto.Kvrpcpb.TxnHeartBeatRequest;
+import org.tikv.kvproto.Kvrpcpb.TxnHeartBeatResponse;
import org.tikv.kvproto.Metapb;
import org.tikv.kvproto.TikvGrpc;
import org.tikv.kvproto.TikvGrpc.TikvBlockingStub;
-import org.tikv.kvproto.TikvGrpc.TikvStub;
+import org.tikv.kvproto.TikvGrpc.TikvFutureStub;
import org.tikv.txn.AbstractLockResolverClient;
import org.tikv.txn.Lock;
import org.tikv.txn.ResolveLockResult;
@@ -73,10 +137,10 @@ public class RegionStoreClient extends AbstractRegionStoreClient {
private Boolean isV4 = null;
public static final Histogram GRPC_RAW_REQUEST_LATENCY =
- Histogram.build()
+ HistogramUtils.buildDuration()
.name("client_java_grpc_raw_requests_latency")
.help("grpc raw request latency.")
- .labelNames("type")
+ .labelNames("type", "cluster")
.register();
private synchronized Boolean getIsV4() {
@@ -93,7 +157,7 @@ private RegionStoreClient(
TiStoreType storeType,
ChannelFactory channelFactory,
TikvBlockingStub blockingStub,
- TikvStub asyncStub,
+ TikvFutureStub asyncStub,
RegionManager regionManager,
PDClient pdClient,
RegionStoreClient.RegionStoreClientBuilder clientBuilder) {
@@ -124,7 +188,7 @@ private RegionStoreClient(
ManagedChannel channel = channelFactory.getChannel(addressStr, pdClient.getHostMapping());
TikvBlockingStub tikvBlockingStub = TikvGrpc.newBlockingStub(channel);
- TikvStub tikvAsyncStub = TikvGrpc.newStub(channel);
+ TikvGrpc.TikvFutureStub tikvAsyncStub = TikvGrpc.newFutureStub(channel);
this.lockResolverClient =
AbstractLockResolverClient.getInstance(
@@ -171,8 +235,9 @@ public ByteString get(BackOffer backOffer, ByteString key, long version)
Supplier factory =
() ->
GetRequest.newBuilder()
- .setContext(makeContext(getResolvedLocks(version), this.storeType))
- .setKey(key)
+ .setContext(
+ makeContext(getResolvedLocks(version), this.storeType, backOffer.getSlowLog()))
+ .setKey(codec.encodeKey(key))
.setVersion(version)
.build();
@@ -211,13 +276,14 @@ private void handleGetResponse(GetResponse resp) throws TiClientInternalExceptio
}
}
- public List batchGet(BackOffer backOffer, Iterable keys, long version) {
+ public List batchGet(BackOffer backOffer, List keys, long version) {
boolean forWrite = false;
Supplier request =
() ->
BatchGetRequest.newBuilder()
- .setContext(makeContext(getResolvedLocks(version), this.storeType))
- .addAllKeys(keys)
+ .setContext(
+ makeContext(getResolvedLocks(version), this.storeType, backOffer.getSlowLog()))
+ .addAllKeys(codec.encodeKeys(keys))
.setVersion(version)
.build();
KVErrorHandler handler =
@@ -250,7 +316,7 @@ private List handleBatchGetResponse(
for (KvPair pair : resp.getPairsList()) {
if (pair.hasError()) {
if (pair.getError().hasLocked()) {
- Lock lock = new Lock(pair.getError().getLocked());
+ Lock lock = new Lock(pair.getError().getLocked(), codec);
locks.add(lock);
} else {
throw new KeyException(pair.getError());
@@ -264,23 +330,22 @@ private List handleBatchGetResponse(
addResolvedLocks(version, resolveLockResult.getResolvedLocks());
// resolveLocks already retried, just throw error to upper logic.
throw new TiKVException("locks not resolved, retry");
- } else {
- return resp.getPairsList();
}
+
+ return codec.decodeKvPairs(resp.getPairsList());
}
public List scan(
BackOffer backOffer, ByteString startKey, long version, boolean keyOnly) {
boolean forWrite = false;
while (true) {
- // we should refresh region
- region = regionManager.getRegionByKey(startKey);
-
Supplier request =
() ->
ScanRequest.newBuilder()
- .setContext(makeContext(getResolvedLocks(version), this.storeType))
- .setStartKey(startKey)
+ .setContext(
+ makeContext(
+ getResolvedLocks(version), this.storeType, backOffer.getSlowLog()))
+ .setStartKey(codec.encodeKey(startKey))
.setVersion(version)
.setKeyOnly(keyOnly)
.setLimit(getConf().getScanBatchSize())
@@ -292,18 +357,23 @@ public List scan(
this,
lockResolverClient,
resp -> resp.hasRegionError() ? resp.getRegionError() : null,
- resp -> null,
+ resp -> resp.hasError() ? resp.getError() : null,
resolveLockResult -> addResolvedLocks(version, resolveLockResult.getResolvedLocks()),
version,
forWrite);
ScanResponse resp = callWithRetry(backOffer, TikvGrpc.getKvScanMethod(), request, handler);
- if (isScanSuccess(backOffer, resp)) {
- return doScan(resp);
+ // retry may refresh region info
+ // we need to update region after retry
+ region = regionManager.getRegionByKey(startKey, backOffer);
+
+ if (handleScanResponse(backOffer, resp, version, forWrite)) {
+ return resp.getPairsList();
}
}
}
- private boolean isScanSuccess(BackOffer backOffer, ScanResponse resp) {
+ private boolean handleScanResponse(
+ BackOffer backOffer, ScanResponse resp, long version, boolean forWrite) {
if (resp == null) {
this.regionManager.onRequestFail(region);
throw new TiClientInternalException("ScanResponse failed without a cause");
@@ -312,28 +382,35 @@ private boolean isScanSuccess(BackOffer backOffer, ScanResponse resp) {
backOffer.doBackOff(BoRegionMiss, new RegionException(resp.getRegionError()));
return false;
}
- return true;
- }
- // TODO: resolve locks after scan
- private List doScan(ScanResponse resp) {
- // Check if kvPair contains error, it should be a Lock if hasError is true.
- List kvPairs = resp.getPairsList();
- List newKvPairs = new ArrayList<>();
- for (KvPair kvPair : kvPairs) {
+ // Resolve locks
+ // Note: Memory lock conflict is returned by both `ScanResponse.error` &
+ // `ScanResponse.pairs[0].error`, while other key errors are returned by
+ // `ScanResponse.pairs.error`
+ // See https://github.com/pingcap/kvproto/pull/697
+ List locks = new ArrayList<>();
+ for (KvPair kvPair : resp.getPairsList()) {
if (kvPair.hasError()) {
- Lock lock = AbstractLockResolverClient.extractLockFromKeyErr(kvPair.getError());
- newKvPairs.add(
- KvPair.newBuilder()
- .setError(kvPair.getError())
- .setValue(kvPair.getValue())
- .setKey(lock.getKey())
- .build());
- } else {
- newKvPairs.add(kvPair);
+ Lock lock = AbstractLockResolverClient.extractLockFromKeyErr(kvPair.getError(), codec);
+ locks.add(lock);
+ }
+ }
+ if (!locks.isEmpty()) {
+ ResolveLockResult resolveLockResult =
+ lockResolverClient.resolveLocks(backOffer, version, locks, forWrite);
+ addResolvedLocks(version, resolveLockResult.getResolvedLocks());
+
+ long msBeforeExpired = resolveLockResult.getMsBeforeTxnExpired();
+ if (msBeforeExpired > 0) {
+ // if not resolve all locks, we wait and retry
+ backOffer.doBackOffWithMaxSleep(
+ BoTxnLockFast, msBeforeExpired, new KeyException(locks.toString()));
}
+
+ return false;
}
- return Collections.unmodifiableList(newKvPairs);
+
+ return true;
}
public List scan(BackOffer backOffer, ByteString startKey, long version) {
@@ -353,11 +430,7 @@ public List scan(BackOffer backOffer, ByteString startKey, long version)
* @throws RegionException region error occurs
*/
public void prewrite(
- BackOffer backOffer,
- ByteString primary,
- Iterable mutations,
- long startTs,
- long lockTTL)
+ BackOffer backOffer, ByteString primary, List mutations, long startTs, long lockTTL)
throws TiClientInternalException, KeyException, RegionException {
this.prewrite(backOffer, primary, mutations, startTs, lockTTL, false);
}
@@ -370,7 +443,7 @@ public void prewrite(
public void prewrite(
BackOffer bo,
ByteString primaryLock,
- Iterable mutations,
+ List mutations,
long startTs,
long ttl,
boolean skipConstraintCheck)
@@ -381,17 +454,17 @@ public void prewrite(
() ->
getIsV4()
? PrewriteRequest.newBuilder()
- .setContext(makeContext(storeType))
+ .setContext(makeContext(storeType, bo.getSlowLog()))
.setStartVersion(startTs)
- .setPrimaryLock(primaryLock)
- .addAllMutations(mutations)
+ .setPrimaryLock(codec.encodeKey(primaryLock))
+ .addAllMutations(codec.encodeMutations(mutations))
.setLockTtl(ttl)
.setSkipConstraintCheck(skipConstraintCheck)
.setMinCommitTs(startTs)
.setTxnSize(16)
.build()
: PrewriteRequest.newBuilder()
- .setContext(makeContext(storeType))
+ .setContext(makeContext(storeType, bo.getSlowLog()))
.setStartVersion(startTs)
.setPrimaryLock(primaryLock)
.addAllMutations(mutations)
@@ -443,7 +516,7 @@ private boolean isPrewriteSuccess(BackOffer backOffer, PrewriteResponse resp, lo
for (KeyError err : resp.getErrorsList()) {
if (err.hasLocked()) {
isSuccess = false;
- Lock lock = new Lock(err.getLocked());
+ Lock lock = new Lock(err.getLocked(), codec);
locks.add(lock);
} else {
throw new KeyException(err.toString());
@@ -471,7 +544,7 @@ public void txnHeartBeat(BackOffer bo, ByteString primaryLock, long startTs, lon
Supplier factory =
() ->
TxnHeartBeatRequest.newBuilder()
- .setContext(makeContext(storeType))
+ .setContext(makeContext(storeType, bo.getSlowLog()))
.setStartVersion(startTs)
.setPrimaryLock(primaryLock)
.setAdviseLockTtl(ttl)
@@ -528,8 +601,11 @@ public void commit(BackOffer backOffer, Iterable keys, long startTs,
CommitRequest.newBuilder()
.setStartVersion(startTs)
.setCommitVersion(commitTs)
- .addAllKeys(keys)
- .setContext(makeContext(storeType))
+ .addAllKeys(
+ StreamSupport.stream(keys.spliterator(), false)
+ .map(codec::encodeKey)
+ .collect(Collectors.toList()))
+ .setContext(makeContext(storeType, backOffer.getSlowLog()))
.build();
KVErrorHandler handler =
new KVErrorHandler<>(
@@ -590,7 +666,8 @@ public List coprocess(
Supplier reqToSend =
() ->
Coprocessor.Request.newBuilder()
- .setContext(makeContext(getResolvedLocks(startTs), this.storeType))
+ .setContext(
+ makeContext(getResolvedLocks(startTs), this.storeType, backOffer.getSlowLog()))
.setTp(REQ_TYPE_DAG.getValue())
.setStartTs(startTs)
.setData(req.toByteString())
@@ -646,7 +723,7 @@ private List handleCopResponse(
}
if (response.hasLocked()) {
- Lock lock = new Lock(response.getLocked());
+ Lock lock = new Lock(response.getLocked(), codec);
logger.debug(String.format("coprocessor encounters locks: %s", lock));
ResolveLockResult resolveLockResult =
lockResolverClient.resolveLocks(
@@ -713,7 +790,9 @@ public Iterator coprocessStreaming(
Supplier reqToSend =
() ->
Coprocessor.Request.newBuilder()
- .setContext(makeContext(getResolvedLocks(startTs), this.storeType))
+ .setContext(
+ makeContext(
+ getResolvedLocks(startTs), this.storeType, SlowLogEmptyImpl.INSTANCE))
// TODO: If no executors...?
.setTp(REQ_TYPE_DAG.getValue())
.setData(req.toByteString())
@@ -733,7 +812,7 @@ public Iterator coprocessStreaming(
StreamingResponse responseIterator =
this.callServerStreamingWithRetry(
- ConcreteBackOffer.newCopNextMaxBackOff(),
+ ConcreteBackOffer.newCopNextMaxBackOff(pdClient.getClusterId()),
TikvGrpc.getCoprocessorStreamMethod(),
reqToSend,
handler);
@@ -747,12 +826,12 @@ public Iterator coprocessStreaming(
* @param splitKeys is the split points for a specific region.
* @return a split region info.
*/
- public List splitRegion(Iterable splitKeys) {
+ public List splitRegion(List splitKeys) {
Supplier request =
() ->
SplitRegionRequest.newBuilder()
- .setContext(makeContext(storeType))
- .addAllSplitKeys(splitKeys)
+ .setContext(makeContext(storeType, SlowLogEmptyImpl.INSTANCE))
+ .addAllSplitKeys(codec.encodeKeys(splitKeys))
.setIsRawKv(conf.isRawKVMode())
.build();
@@ -769,7 +848,10 @@ public List splitRegion(Iterable splitKeys) {
SplitRegionResponse resp =
callWithRetry(
- ConcreteBackOffer.newGetBackOff(), TikvGrpc.getSplitRegionMethod(), request, handler);
+ ConcreteBackOffer.newGetBackOff(pdClient.getClusterId()),
+ TikvGrpc.getSplitRegionMethod(),
+ request,
+ handler);
if (resp == null) {
this.regionManager.onRequestFail(region);
@@ -779,21 +861,28 @@ public List splitRegion(Iterable splitKeys) {
if (resp.hasRegionError()) {
throw new TiClientInternalException(
String.format(
- "failed to split region %d because %s",
- region.getId(), resp.getRegionError().toString()));
+ "failed to split region %d because %s", region.getId(), resp.getRegionError()));
}
- return resp.getRegionsList();
+ if (conf.getApiVersion().isV1()) {
+ return resp.getRegionsList();
+ }
+ return resp.getRegionsList().stream().map(codec::decodeRegion).collect(Collectors.toList());
}
// APIs for Raw Scan/Put/Get/Delete
public Optional rawGet(BackOffer backOffer, ByteString key) {
+ Long clusterId = pdClient.getClusterId();
Histogram.Timer requestTimer =
- GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_get").startTimer();
+ GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_get", clusterId.toString()).startTimer();
try {
Supplier factory =
- () -> RawGetRequest.newBuilder().setContext(makeContext(storeType)).setKey(key).build();
+ () ->
+ RawGetRequest.newBuilder()
+ .setContext(makeContext(storeType, backOffer.getSlowLog()))
+ .setKey(codec.encodeKey(key))
+ .build();
RegionErrorHandler handler =
new RegionErrorHandler(
regionManager, this, resp -> resp.hasRegionError() ? resp.getRegionError() : null);
@@ -824,14 +913,17 @@ private Optional rawGetHelper(RawGetResponse resp) {
}
public Optional rawGetKeyTTL(BackOffer backOffer, ByteString key) {
+ Long clusterId = pdClient.getClusterId();
Histogram.Timer requestTimer =
- GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_get_key_ttl").startTimer();
+ GRPC_RAW_REQUEST_LATENCY
+ .labels("client_grpc_raw_get_key_ttl", clusterId.toString())
+ .startTimer();
try {
Supplier factory =
() ->
RawGetKeyTTLRequest.newBuilder()
- .setContext(makeContext(storeType))
- .setKey(key)
+ .setContext(makeContext(storeType, backOffer.getSlowLog()))
+ .setKey(codec.encodeKey(key))
.build();
RegionErrorHandler handler =
new RegionErrorHandler(
@@ -863,14 +955,17 @@ private Optional rawGetKeyTTLHelper(RawGetKeyTTLResponse resp) {
}
public void rawDelete(BackOffer backOffer, ByteString key, boolean atomicForCAS) {
+ Long clusterId = pdClient.getClusterId();
Histogram.Timer requestTimer =
- GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_delete").startTimer();
+ GRPC_RAW_REQUEST_LATENCY
+ .labels("client_grpc_raw_delete", clusterId.toString())
+ .startTimer();
try {
Supplier factory =
() ->
RawDeleteRequest.newBuilder()
- .setContext(makeContext(storeType))
- .setKey(key)
+ .setContext(makeContext(storeType, backOffer.getSlowLog()))
+ .setKey(codec.encodeKey(key))
.setForCas(atomicForCAS)
.build();
@@ -901,14 +996,15 @@ private void rawDeleteHelper(RawDeleteResponse resp, TiRegion region) {
public void rawPut(
BackOffer backOffer, ByteString key, ByteString value, long ttl, boolean atomicForCAS) {
+ Long clusterId = pdClient.getClusterId();
Histogram.Timer requestTimer =
- GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_put").startTimer();
+ GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_put", clusterId.toString()).startTimer();
try {
Supplier factory =
() ->
RawPutRequest.newBuilder()
- .setContext(makeContext(storeType))
- .setKey(key)
+ .setContext(makeContext(storeType, backOffer.getSlowLog()))
+ .setKey(codec.encodeKey(key))
.setValue(value)
.setTtl(ttl)
.setForCas(atomicForCAS)
@@ -945,14 +1041,17 @@ public void rawCompareAndSet(
ByteString value,
long ttl)
throws RawCASConflictException {
+ Long clusterId = pdClient.getClusterId();
Histogram.Timer requestTimer =
- GRPC_RAW_REQUEST_LATENCY.labels("client_grpc_raw_put_if_absent").startTimer();
+ GRPC_RAW_REQUEST_LATENCY
+ .labels("client_grpc_raw_put_if_absent", clusterId.toString())
+ .startTimer();
try {
Supplier factory =
() ->
RawCASRequest.newBuilder()
- .setContext(makeContext(storeType))
- .setKey(key)
+ .setContext(makeContext(storeType, backOffer.getSlowLog()))
+ .setKey(codec.encodeKey(key))
.setValue(value)
.setPreviousValue(prevValue.orElse(ByteString.EMPTY))
.setPreviousNotExist(!prevValue.isPresent())
@@ -975,7 +1074,7 @@ private void rawCompareAndSetHelper(
throws RawCASConflictException {
if (resp == null) {
this.regionManager.onRequestFail(region);
- throw new TiClientInternalException("RawPutResponse failed without a cause");
+ throw new TiClientInternalException("RawCASResponse failed without a cause");
}
String error = resp.getError();
if (!error.isEmpty()) {
@@ -995,8 +1094,11 @@ private void rawCompareAndSetHelper(
}
public List