Skip to content

Commit 81a6efd

Browse files
authored
[close #497] let JVM ClassLoader load gRPC error related classes during warmup (#496) (#498)
1 parent 7f4fb95 commit 81a6efd

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,14 @@
539539
<autoReleaseAfterClose>true</autoReleaseAfterClose>
540540
</configuration>
541541
</plugin>
542+
<plugin>
543+
<groupId>org.apache.maven.plugins</groupId>
544+
<artifactId>maven-surefire-plugin</artifactId>
545+
<version>3.0.0-M5</version>
546+
<configuration>
547+
<forkMode>always</forkMode>
548+
</configuration>
549+
</plugin>
542550
</plugins>
543551
</build>
544552
</project>

src/main/java/org/tikv/common/TiSession.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.tikv.common.region.TiRegion;
4040
import org.tikv.common.region.TiStore;
4141
import org.tikv.common.util.*;
42+
import org.tikv.kvproto.Errorpb;
4243
import org.tikv.kvproto.Metapb;
4344
import org.tikv.kvproto.Pdpb;
4445
import org.tikv.raw.RawKVClient;
@@ -99,6 +100,13 @@ private synchronized void warmUp() {
99100
BackOffer backOffer = ConcreteBackOffer.newRawKVBackOff();
100101

101102
try {
103+
// let JVM ClassLoader load gRPC error related classes
104+
// this operation may cost 100ms
105+
Errorpb.Error.newBuilder()
106+
.setNotLeader(Errorpb.NotLeader.newBuilder().build())
107+
.build()
108+
.toString();
109+
102110
this.client = getPDClient();
103111
this.regionManager = getRegionManager();
104112
List<Metapb.Store> stores = this.client.getAllStores(backOffer);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2021 TiKV Project Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.tikv.raw;
19+
20+
import static org.junit.Assert.assertTrue;
21+
22+
import org.junit.Test;
23+
import org.tikv.BaseRawKVTest;
24+
import org.tikv.common.TiConfiguration;
25+
import org.tikv.common.TiSession;
26+
import org.tikv.kvproto.Errorpb;
27+
28+
public class WarmupTest extends BaseRawKVTest {
29+
30+
@Test
31+
public void testErrorpbToStringEnableWarmup() throws Exception {
32+
TiConfiguration conf = createTiConfiguration();
33+
TiSession session = TiSession.create(conf);
34+
35+
long start = System.currentTimeMillis();
36+
Errorpb.Error.newBuilder()
37+
.setNotLeader(Errorpb.NotLeader.newBuilder().build())
38+
.build()
39+
.toString();
40+
long end = System.currentTimeMillis();
41+
assertTrue(end - start < 10);
42+
session.close();
43+
}
44+
45+
@Test
46+
public void testErrorpbToStringDisableWarmup() throws Exception {
47+
TiConfiguration conf = createTiConfiguration();
48+
conf.setWarmUpEnable(false);
49+
TiSession session = TiSession.create(conf);
50+
51+
long start = System.currentTimeMillis();
52+
Errorpb.Error.newBuilder()
53+
.setNotLeader(Errorpb.NotLeader.newBuilder().build())
54+
.build()
55+
.toString();
56+
long end = System.currentTimeMillis();
57+
assertTrue(end - start > 10);
58+
session.close();
59+
}
60+
}

0 commit comments

Comments
 (0)