-
Notifications
You must be signed in to change notification settings - Fork 118
[to #439] introduce MockThreeStoresTest to onStoreUnreachable #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
035befc
a8a4fa2
e485695
0aff000
1e0736a
fa7ca88
a374f01
a5438fa
838a1b7
05e1089
1175bde
81058eb
3f2a55b
ffeaa1f
cdc9d57
152e3d2
5cee439
569f925
fe21429
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ | |
| import org.tikv.kvproto.Metapb.Region; | ||
|
|
||
| public class TiRegion implements Serializable { | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can do code format in another PR |
||
| private static final Logger logger = LoggerFactory.getLogger(TiRegion.class); | ||
|
|
||
| private final Region meta; | ||
|
|
@@ -269,6 +270,7 @@ public String toString() { | |
| } | ||
|
|
||
| public class RegionVerID { | ||
|
|
||
| final long id; | ||
| final long confVer; | ||
| final long ver; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,10 +27,22 @@ | |
| import io.grpc.Server; | ||
| import io.grpc.ServerBuilder; | ||
| import io.grpc.Status; | ||
| import io.grpc.health.v1.HealthCheckRequest; | ||
| import io.grpc.health.v1.HealthCheckResponse; | ||
| import io.grpc.health.v1.HealthCheckResponse.ServingStatus; | ||
| import io.grpc.health.v1.HealthGrpc.HealthImplBase; | ||
| import io.grpc.stub.StreamObserver; | ||
| import java.io.IOException; | ||
| import java.net.ServerSocket; | ||
| import java.util.*; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.SortedMap; | ||
| import java.util.TreeMap; | ||
| import java.util.stream.Collectors; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.tikv.common.key.Key; | ||
| import org.tikv.common.region.TiRegion; | ||
| import org.tikv.kvproto.Coprocessor; | ||
|
|
@@ -45,9 +57,11 @@ | |
|
|
||
| public class KVMockServer extends TikvGrpc.TikvImplBase { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(KVMockServer.class); | ||
| private int port; | ||
| private Server server; | ||
| private TiRegion region; | ||
| private State state = State.Normal; | ||
| private final TreeMap<Key, ByteString> dataMap = new TreeMap<>(); | ||
| private final Map<ByteString, Integer> errorMap = new HashMap<>(); | ||
|
|
||
|
|
@@ -64,10 +78,23 @@ public class KVMockServer extends TikvGrpc.TikvImplBase { | |
| public static final int STORE_NOT_MATCH = 9; | ||
| public static final int RAFT_ENTRY_TOO_LARGE = 10; | ||
|
|
||
| public enum State { | ||
| Normal, | ||
| Fail | ||
marsishandsome marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public void setState(State state) { | ||
| this.state = state; | ||
| } | ||
|
|
||
| public int getPort() { | ||
| return port; | ||
| } | ||
|
|
||
| public void setRegion(TiRegion region) { | ||
| this.region = region; | ||
| } | ||
|
|
||
| public void put(ByteString key, ByteString value) { | ||
| dataMap.put(toRawKey(key), value); | ||
| } | ||
|
|
@@ -97,7 +124,7 @@ private void verifyContext(Context context) throws Exception { | |
| if (context.getRegionId() != region.getId() | ||
| || !context.getRegionEpoch().equals(region.getRegionEpoch()) | ||
| || !context.getPeer().equals(region.getLeader())) { | ||
| throw new Exception(); | ||
| throw new Exception("context doesn't match"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be better to specifically point out the mismatched field in context. |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -106,6 +133,11 @@ public void rawGet( | |
| org.tikv.kvproto.Kvrpcpb.RawGetRequest request, | ||
| io.grpc.stub.StreamObserver<org.tikv.kvproto.Kvrpcpb.RawGetResponse> responseObserver) { | ||
| try { | ||
| switch (state) { | ||
| case Fail: | ||
| throw new Exception(State.Fail.toString()); | ||
| default: | ||
| } | ||
| verifyContext(request.getContext()); | ||
| ByteString key = request.getKey(); | ||
|
|
||
|
|
@@ -116,7 +148,12 @@ public void rawGet( | |
| setErrorInfo(errorCode, errBuilder); | ||
| builder.setRegionError(errBuilder.build()); | ||
| } else { | ||
| builder.setValue(dataMap.get(toRawKey(key))); | ||
| Key rawKey = toRawKey(key); | ||
| ByteString value = dataMap.get(rawKey); | ||
| if (value == null) { | ||
| value = ByteString.EMPTY; | ||
| } | ||
| builder.setValue(value); | ||
| } | ||
| responseObserver.onNext(builder.build()); | ||
| responseObserver.onCompleted(); | ||
|
|
@@ -139,7 +176,6 @@ public void rawPut( | |
| if (errorCode != null) { | ||
| setErrorInfo(errorCode, errBuilder); | ||
| builder.setRegionError(errBuilder.build()); | ||
| // builder.setError(""); | ||
| } | ||
| responseObserver.onNext(builder.build()); | ||
| responseObserver.onCompleted(); | ||
|
|
@@ -349,14 +385,33 @@ public void coprocessor( | |
| } | ||
|
|
||
| public int start(TiRegion region) throws IOException { | ||
| int port; | ||
| try (ServerSocket s = new ServerSocket(0)) { | ||
| port = s.getLocalPort(); | ||
| } | ||
| server = ServerBuilder.forPort(port).addService(this).build().start(); | ||
| start(region, port); | ||
| return port; | ||
| } | ||
|
|
||
| private static class HealCheck extends HealthImplBase { | ||
|
|
||
| @Override | ||
| public void check( | ||
| HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) { | ||
| responseObserver.onNext( | ||
| HealthCheckResponse.newBuilder().setStatus(ServingStatus.SERVING).build()); | ||
| responseObserver.onCompleted(); | ||
| } | ||
| } | ||
|
|
||
| public void start(TiRegion region, int port) throws IOException { | ||
| this.port = port; | ||
| this.region = region; | ||
|
|
||
| logger.info("start mock server on port: " + port); | ||
| server = | ||
| ServerBuilder.forPort(port).addService(new HealCheck()).addService(this).build().start(); | ||
| Runtime.getRuntime().addShutdownHook(new Thread(KVMockServer.this::stop)); | ||
| return port; | ||
| } | ||
|
|
||
| public void stop() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.protobuf.ByteString; | ||
| import java.io.IOException; | ||
| import java.net.ServerSocket; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.tikv.common.region.TiRegion; | ||
| import org.tikv.common.region.TiStore; | ||
| import org.tikv.kvproto.Metapb; | ||
| import org.tikv.kvproto.Pdpb; | ||
|
|
||
| public class MockThreeStoresTest extends PDMockServerTest { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just want to keep it simple enough, for now, refactoring will be introduced in the future.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create an issue to describe this refactoring?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could I just add some comments to this class?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||
|
|
||
| protected TiRegion region; | ||
| protected List<KVMockServer> servers = new ArrayList<>(); | ||
| protected List<Metapb.Store> stores; | ||
|
|
||
| @Before | ||
| @Override | ||
| public void setup() throws IOException { | ||
| super.setup(); | ||
|
|
||
| int basePort; | ||
| try (ServerSocket s = new ServerSocket(0)) { | ||
| basePort = s.getLocalPort(); | ||
| } | ||
|
|
||
| ImmutableList<Metapb.Peer> peers = | ||
| ImmutableList.of( | ||
| Metapb.Peer.newBuilder().setId(0x1).setStoreId(0x1).build(), | ||
| Metapb.Peer.newBuilder().setId(0x2).setStoreId(0x2).build(), | ||
| Metapb.Peer.newBuilder().setId(0x3).setStoreId(0x3).build()); | ||
|
|
||
| Metapb.Region region = | ||
| Metapb.Region.newBuilder() | ||
| .setRegionEpoch(Metapb.RegionEpoch.newBuilder().setConfVer(1).setVersion(2)) | ||
| .setId(0xff) | ||
| .setStartKey(ByteString.EMPTY) | ||
| .setEndKey(ByteString.EMPTY) | ||
| .addAllPeers(peers) | ||
| .build(); | ||
|
|
||
| stores = | ||
| ImmutableList.of( | ||
| Metapb.Store.newBuilder() | ||
| .setAddress("127.0.0.1:" + basePort) | ||
| .setVersion("5.0.0") | ||
| .setId(0x1) | ||
| .build(), | ||
| Metapb.Store.newBuilder() | ||
| .setAddress("127.0.0.1:" + (basePort + 1)) | ||
| .setVersion("5.0.0") | ||
| .setId(0x2) | ||
| .build(), | ||
| Metapb.Store.newBuilder() | ||
| .setAddress("127.0.0.1:" + (basePort + 2)) | ||
| .setVersion("5.0.0") | ||
| .setId(0x3) | ||
| .build()); | ||
|
|
||
| for (PDMockServer server : pdServers) { | ||
| server.addGetRegionListener( | ||
| request -> | ||
| Pdpb.GetRegionResponse.newBuilder() | ||
| .setLeader(peers.get(0)) | ||
| .setRegion(region) | ||
| .build()); | ||
| server.addGetStoreListener( | ||
| (request) -> { | ||
| int i = (int) request.getStoreId() - 1; | ||
| return Pdpb.GetStoreResponse.newBuilder().setStore(stores.get(i)).build(); | ||
| }); | ||
| } | ||
|
|
||
| this.region = | ||
| new TiRegion( | ||
| session.getConf(), | ||
| region, | ||
| region.getPeers(0), | ||
| region.getPeersList(), | ||
| stores.stream().map(TiStore::new).collect(Collectors.toList())); | ||
| for (int i = 0; i < 3; i++) { | ||
| KVMockServer server = new KVMockServer(); | ||
| server.start(this.region, basePort + i); | ||
| servers.add(server); | ||
| } | ||
| } | ||
|
|
||
| public void put(ByteString key, ByteString value) { | ||
| for (KVMockServer server : servers) { | ||
| server.put(key, value); | ||
| } | ||
| } | ||
|
|
||
| public void remove(ByteString key, ByteString value) { | ||
| for (KVMockServer server : servers) { | ||
| server.remove(key); | ||
| } | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| for (KVMockServer server : servers) { | ||
| server.stop(); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.