-
Notifications
You must be signed in to change notification settings - Fork 118
Support select replica with rich meta data #171
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
bfca20b
Support select replica with rich meta data
sunxiaoguang a3b56b0
Use string helper
sunxiaoguang 42f10cd
Print debug log when failed to get member
sunxiaoguang fb10732
Merge remote-tracking branch 'origin/master' into rich_replica_select…
sunxiaoguang de050bf
Support customized HostMapping implementation
sunxiaoguang 58668af
Use ImmutableList as List.of only exists since Java 9
sunxiaoguang 12512f0
Use this.leader in TiRegion
sunxiaoguang 0171bc9
Fix couple of integration tests
sunxiaoguang 26a8f53
Fix one more integration test
sunxiaoguang 6336492
Fix replica read peer mismatch
sunxiaoguang ecb8af1
Use passed in peer as it already selected peer
sunxiaoguang 6697a99
Merge remote-tracking branch 'origin/master' into rich_replica_select…
sunxiaoguang 0fe131e
Refactor after merge master
sunxiaoguang 7ebaa55
Merge remote-tracking branch 'origin/master' into rich_replica_select…
sunxiaoguang c73df3c
Try fix integration test
sunxiaoguang 2a63421
Workaround lint issue with duplicated dependency for different scopes
sunxiaoguang f418882
Merge branch 'master' into rich_replica_select
ti-srebot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Copyright 2021 PingCAP, Inc. | ||
| * | ||
| * 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, | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.tikv.common; | ||
|
|
||
| import static org.tikv.common.pd.PDUtils.addrToUri; | ||
|
|
||
| import com.google.common.annotations.Beta; | ||
| import io.etcd.jetcd.ByteSequence; | ||
| import io.etcd.jetcd.Client; | ||
| import io.etcd.jetcd.KeyValue; | ||
| import io.etcd.jetcd.kv.GetResponse; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import java.util.concurrent.ExecutionException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class DefaultHostMapping implements HostMapping { | ||
| private static final String NETWORK_MAPPING_PATH = "/client/url-mapping"; | ||
| private final Client etcdClient; | ||
| private final String networkMappingName; | ||
| private final ConcurrentMap<String, String> hostMapping; | ||
| private final Logger logger = LoggerFactory.getLogger(DefaultHostMapping.class); | ||
|
|
||
| public DefaultHostMapping(Client etcdClient, String networkMappingName) { | ||
| this.etcdClient = etcdClient; | ||
| this.networkMappingName = networkMappingName; | ||
| this.hostMapping = new ConcurrentHashMap<>(); | ||
| } | ||
|
|
||
| private ByteSequence hostToNetworkMappingKey(String host) { | ||
| String path = NETWORK_MAPPING_PATH + "/" + networkMappingName + "/" + host; | ||
| return ByteSequence.from(path, StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| @Beta | ||
| private String getMappedHostFromPD(String host) { | ||
| ByteSequence hostKey = hostToNetworkMappingKey(host); | ||
| for (int i = 0; i < 5; i++) { | ||
| CompletableFuture<GetResponse> future = etcdClient.getKVClient().get(hostKey); | ||
| try { | ||
| GetResponse resp = future.get(); | ||
| List<KeyValue> kvs = resp.getKvs(); | ||
| if (kvs.size() != 1) { | ||
| break; | ||
| } | ||
| return kvs.get(0).getValue().toString(StandardCharsets.UTF_8); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } catch (ExecutionException e) { | ||
| logger.info("failed to get mapped Host from PD: " + host, e); | ||
| break; | ||
| } catch (Exception ignore) { | ||
| // ignore | ||
| break; | ||
| } | ||
| } | ||
| return host; | ||
| } | ||
|
|
||
| public URI getMappedURI(URI uri) { | ||
| if (networkMappingName.isEmpty()) { | ||
| return uri; | ||
| } | ||
| return addrToUri( | ||
| hostMapping.computeIfAbsent(uri.getHost(), this::getMappedHostFromPD) | ||
| + ":" | ||
| + uri.getPort()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.