You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hugegraph.apache.org by ji...@apache.org on 2023/08/07 15:19:09 UTC
[incubator-hugegraph] branch revert-2270-merge-pd created (now cdcf13e2a)
This is an automated email from the ASF dual-hosted git repository.
jin pushed a change to branch revert-2270-merge-pd
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git
at cdcf13e2a Revert "refact: merge pd into hugegraph (#2270)"
This branch includes the following new commits:
new cdcf13e2a Revert "refact: merge pd into hugegraph (#2270)"
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[incubator-hugegraph] 01/01: Revert "refact: merge pd into hugegraph (#2270)"
Posted by ji...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
jin pushed a commit to branch revert-2270-merge-pd
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git
commit cdcf13e2a95b789d08eb120e00f20ef46b1ae944
Author: imbajin <ji...@apache.org>
AuthorDate: Mon Aug 7 23:18:53 2023 +0800
Revert "refact: merge pd into hugegraph (#2270)"
This reverts commit a891780c36a069a38348c33d79242add3f5e267b.
---
hugegraph-pd/.gitattributes | 1 -
hugegraph-pd/.gitignore | 13 -
.../.mvn/wrapper/MavenWrapperDownloader.java | 121 --
hugegraph-pd/.mvn/wrapper/maven-wrapper.jar | Bin 50710 -> 0 bytes
hugegraph-pd/.mvn/wrapper/maven-wrapper.properties | 18 -
hugegraph-pd/README.md | 940 ----------
hugegraph-pd/build-pre.sh | 54 -
hugegraph-pd/build.sh | 39 -
hugegraph-pd/ci.yml | 43 -
hugegraph-pd/conf/hugegraph.license | Bin 856 -> 0 bytes
hugegraph-pd/conf/verify-license.json | 6 -
hugegraph-pd/deploy-release.sh | 25 -
hugegraph-pd/deploy-snapshot.sh | 25 -
hugegraph-pd/hg-pd-client/pom.xml | 73 -
.../apache/hugegraph/pd/client/AbstractClient.java | 265 ---
.../pd/client/AbstractClientStubProxy.java | 72 -
.../org/apache/hugegraph/pd/client/Channels.java | 44 -
.../apache/hugegraph/pd/client/ClientCache.java | 338 ----
.../apache/hugegraph/pd/client/Discoverable.java | 30 -
.../hugegraph/pd/client/DiscoveryClient.java | 221 ---
.../hugegraph/pd/client/DiscoveryClientImpl.java | 140 --
.../org/apache/hugegraph/pd/client/KvClient.java | 352 ----
.../apache/hugegraph/pd/client/LicenseClient.java | 71 -
.../org/apache/hugegraph/pd/client/PDClient.java | 1347 ---------------
.../org/apache/hugegraph/pd/client/PDConfig.java | 82 -
.../org/apache/hugegraph/pd/client/PDPulse.java | 152 --
.../apache/hugegraph/pd/client/PDPulseImpl.java | 196 ---
.../org/apache/hugegraph/pd/client/PDWatch.java | 137 --
.../apache/hugegraph/pd/client/PDWatchImpl.java | 202 ---
.../apache/hugegraph/pd/pulse/PartitionNotice.java | 49 -
.../hugegraph/pd/pulse/PulseServerNotice.java | 35 -
.../org/apache/hugegraph/pd/watch/NodeEvent.java | 99 --
.../org/apache/hugegraph/pd/watch/PDWatcher.java | 22 -
.../apache/hugegraph/pd/watch/PartitionEvent.java | 93 -
.../org/apache/hugegraph/pd/watch/WatchType.java | 30 -
.../apache/hugegraph/pd/PartitionCacheTest.java | 102 --
.../org/apache/hugegraph/pd/StoreRegisterTest.java | 135 --
.../pd/client/DiscoveryClientImplTest.java | 147 --
.../hugegraph/pd/client/LicenseClientImplTest.java | 127 --
.../apache/hugegraph/pd/client/PDPulseTest.java | 109 --
.../apache/hugegraph/pd/client/PDWatchTest.java | 86 -
.../hugegraph/pd/client/test/HgPDTestUtil.java | 92 -
hugegraph-pd/hg-pd-clitools/pom.xml | 74 -
.../org/apache/hugegraph/pd/clitools/Main.java | 85 -
.../org/apache/hugegraph/pd/clitools/MainTest.java | 80 -
hugegraph-pd/hg-pd-common/pom.xml | 54 -
.../org/apache/hugegraph/pd/common/GraphCache.java | 62 -
.../org/apache/hugegraph/pd/common/HgAssert.java | 117 --
.../org/apache/hugegraph/pd/common/KVPair.java | 132 --
.../apache/hugegraph/pd/common/PDException.java | 47 -
.../hugegraph/pd/common/PDRuntimeException.java | 49 -
.../apache/hugegraph/pd/common/PartitionCache.java | 460 -----
.../apache/hugegraph/pd/common/PartitionUtils.java | 47 -
hugegraph-pd/hg-pd-core/pom.xml | 94 -
.../org/apache/hugegraph/pd/ConfigService.java | 135 --
.../java/org/apache/hugegraph/pd/IdService.java | 82 -
.../java/org/apache/hugegraph/pd/KvService.java | 317 ----
.../java/org/apache/hugegraph/pd/LogService.java | 67 -
.../hugegraph/pd/PartitionInstructionListener.java | 52 -
.../org/apache/hugegraph/pd/PartitionService.java | 1563 -----------------
.../hugegraph/pd/PartitionStatusListener.java | 29 -
.../org/apache/hugegraph/pd/RegistryService.java | 44 -
.../hugegraph/pd/ShardGroupStatusListener.java | 26 -
.../hugegraph/pd/StoreMonitorDataService.java | 266 ---
.../org/apache/hugegraph/pd/StoreNodeService.java | 1074 ------------
.../apache/hugegraph/pd/StoreStatusListener.java | 31 -
.../apache/hugegraph/pd/TaskScheduleService.java | 853 ----------
.../org/apache/hugegraph/pd/config/PDConfig.java | 277 ---
.../apache/hugegraph/pd/meta/ConfigMetaStore.java | 73 -
.../hugegraph/pd/meta/DiscoveryMetaStore.java | 105 --
.../org/apache/hugegraph/pd/meta/IdMetaStore.java | 255 ---
.../java/org/apache/hugegraph/pd/meta/LogMeta.java | 48 -
.../apache/hugegraph/pd/meta/MetadataFactory.java | 87 -
.../hugegraph/pd/meta/MetadataKeyHelper.java | 378 ----
.../hugegraph/pd/meta/MetadataRocksDBStore.java | 184 --
.../hugegraph/pd/meta/MetadataStoreBase.java | 124 --
.../apache/hugegraph/pd/meta/PartitionMeta.java | 295 ----
.../org/apache/hugegraph/pd/meta/QueueStore.java | 56 -
.../apache/hugegraph/pd/meta/StoreInfoMeta.java | 206 ---
.../org/apache/hugegraph/pd/meta/TaskInfoMeta.java | 132 --
.../hugegraph/pd/raft/FutureClosureAdapter.java | 48 -
.../org/apache/hugegraph/pd/raft/KVOperation.java | 161 --
.../apache/hugegraph/pd/raft/KVStoreClosure.java | 33 -
.../org/apache/hugegraph/pd/raft/RaftEngine.java | 377 ----
.../apache/hugegraph/pd/raft/RaftRpcClient.java | 87 -
.../apache/hugegraph/pd/raft/RaftRpcProcessor.java | 127 --
.../hugegraph/pd/raft/RaftStateListener.java | 22 -
.../apache/hugegraph/pd/raft/RaftStateMachine.java | 330 ----
.../apache/hugegraph/pd/raft/RaftTaskHandler.java | 27 -
.../org/apache/hugegraph/pd/raft/ZipUtils.java | 93 -
.../hugegraph/pd/store/BaseKVStoreClosure.java | 48 -
.../org/apache/hugegraph/pd/store/HgKVStore.java | 58 -
.../apache/hugegraph/pd/store/HgKVStoreImpl.java | 343 ----
.../java/org/apache/hugegraph/pd/store/KV.java | 45 -
.../org/apache/hugegraph/pd/store/RaftKVStore.java | 324 ----
.../apache/hugegraph/pd/MonitorServiceTest.java | 112 --
.../apache/hugegraph/pd/PartitionServiceTest.java | 45 -
.../apache/hugegraph/pd/StoreNodeServiceTest.java | 485 ------
.../java/org/apache/hugegraph/pd/UnitTestBase.java | 31 -
.../hugegraph/pd/common/PartitionUtilsTest.java | 47 -
.../hugegraph/pd/store/HgKVStoreImplTest.java | 105 --
.../hg-pd-core/src/test/resources/log4j2.xml | 139 --
hugegraph-pd/hg-pd-dist/pom.xml | 151 --
.../src/assembly/descriptor/server-assembly.xml | 57 -
.../src/assembly/static/bin/start-hugegraph-pd.sh | 123 --
.../src/assembly/static/bin/stop-hugegraph-pd.sh | 49 -
.../hg-pd-dist/src/assembly/static/bin/util.sh | 372 ----
.../src/assembly/static/conf/application.yml | 78 -
.../assembly/static/conf/application.yml.template | 71 -
.../src/assembly/static/conf/hugegraph.license | Bin 856 -> 0 bytes
.../hg-pd-dist/src/assembly/static/conf/log4j2.xml | 134 --
.../src/assembly/static/conf/verify-license.json | 6 -
hugegraph-pd/hg-pd-grpc/pom.xml | 138 --
.../hg-pd-grpc/src/main/proto/discovery.proto | 54 -
hugegraph-pd/hg-pd-grpc/src/main/proto/kv.proto | 126 --
.../hg-pd-grpc/src/main/proto/metaTask.proto | 48 -
.../hg-pd-grpc/src/main/proto/metapb.proto | 377 ----
.../hg-pd-grpc/src/main/proto/pd_common.proto | 36 -
.../hg-pd-grpc/src/main/proto/pd_pulse.proto | 155 --
.../hg-pd-grpc/src/main/proto/pd_watch.proto | 86 -
hugegraph-pd/hg-pd-grpc/src/main/proto/pdpb.proto | 590 -------
hugegraph-pd/hg-pd-service/pom.xml | 151 --
.../org/apache/hugegraph/pd/boot/HugePDServer.java | 43 -
.../pd/license/LicenseVerifierService.java | 426 -----
.../hugegraph/pd/license/LicenseVerifyManager.java | 77 -
.../apache/hugegraph/pd/metrics/MetricsConfig.java | 44 -
.../org/apache/hugegraph/pd/metrics/PDMetrics.java | 114 --
.../org/apache/hugegraph/pd/model/DemoModel.java | 73 -
.../hugegraph/pd/model/GraphRestRequest.java | 26 -
.../hugegraph/pd/model/GraphSpaceRestRequest.java | 25 -
.../apache/hugegraph/pd/model/PeerRestRequest.java | 25 -
.../hugegraph/pd/model/PromTargetsModel.java | 90 -
.../pd/model/RegistryQueryRestRequest.java | 30 -
.../hugegraph/pd/model/RegistryRestRequest.java | 33 -
.../hugegraph/pd/model/RegistryRestResponse.java | 33 -
.../apache/hugegraph/pd/model/RestApiResponse.java | 53 -
.../hugegraph/pd/model/StoreRestRequest.java | 25 -
.../hugegraph/pd/model/TimeRangeRequest.java | 26 -
.../hugegraph/pd/notice/NoticeBroadcaster.java | 173 --
.../pd/pulse/AbstractObserverSubject.java | 223 ---
.../apache/hugegraph/pd/pulse/PDPulseSubject.java | 444 -----
.../pd/pulse/PartitionHeartbeatSubject.java | 60 -
.../hugegraph/pd/pulse/PdInstructionSubject.java | 56 -
.../apache/hugegraph/pd/pulse/PulseListener.java | 39 -
.../java/org/apache/hugegraph/pd/rest/API.java | 186 --
.../org/apache/hugegraph/pd/rest/GraphAPI.java | 288 ----
.../apache/hugegraph/pd/rest/GraphSpaceAPI.java | 99 --
.../org/apache/hugegraph/pd/rest/IndexAPI.java | 255 ---
.../org/apache/hugegraph/pd/rest/MemberAPI.java | 239 ---
.../org/apache/hugegraph/pd/rest/PartitionAPI.java | 475 ------
.../apache/hugegraph/pd/rest/PromTargetsAPI.java | 92 -
.../org/apache/hugegraph/pd/rest/RegistryAPI.java | 202 ---
.../org/apache/hugegraph/pd/rest/ShardAPI.java | 119 --
.../org/apache/hugegraph/pd/rest/StoreAPI.java | 355 ----
.../java/org/apache/hugegraph/pd/rest/TaskAPI.java | 101 --
.../java/org/apache/hugegraph/pd/rest/TestAPI.java | 163 --
.../hugegraph/pd/service/DiscoveryService.java | 147 --
.../hugegraph/pd/service/KvServiceGrpcImpl.java | 592 -------
.../hugegraph/pd/service/PDPulseService.java | 115 --
.../apache/hugegraph/pd/service/PDRestService.java | 272 ---
.../org/apache/hugegraph/pd/service/PDService.java | 1796 --------------------
.../hugegraph/pd/service/PDWatchService.java | 37 -
.../hugegraph/pd/service/PromTargetsService.java | 257 ---
.../apache/hugegraph/pd/service/ServiceGrpc.java | 94 -
.../hugegraph/pd/service/UpgradeService.java | 110 --
.../hugegraph/pd/upgrade/VersionScriptFactory.java | 57 -
.../hugegraph/pd/upgrade/VersionUpgradeScript.java | 59 -
.../pd/upgrade/scripts/PartitionMetaUpgrade.java | 120 --
.../pd/upgrade/scripts/TaskCleanUpgrade.java | 65 -
.../org/apache/hugegraph/pd/util/DateUtil.java | 71 -
.../apache/hugegraph/pd/util/HgExecutorUtil.java | 180 --
.../org/apache/hugegraph/pd/util/HgMapCache.java | 103 --
.../java/org/apache/hugegraph/pd/util/IdUtil.java | 47 -
.../hugegraph/pd/util/grpc/GRpcServerConfig.java | 44 -
.../hugegraph/pd/util/grpc/StreamObserverUtil.java | 49 -
.../hugegraph/pd/watch/AbstractWatchSubject.java | 164 --
.../apache/hugegraph/pd/watch/KvWatchSubject.java | 288 ----
.../hugegraph/pd/watch/NodeChangeSubject.java | 65 -
.../apache/hugegraph/pd/watch/PDWatchSubject.java | 216 ---
.../hugegraph/pd/watch/PartitionChangeSubject.java | 63 -
.../pd/watch/ShardGroupChangeSubject.java | 55 -
.../src/main/resources/application.yml | 80 -
.../hg-pd-service/src/main/resources/banner.txt | 6 -
.../hg-pd-service/src/main/resources/log4j2.xml | 139 --
.../src/test/java/live/PDServer0.java | 48 -
.../src/test/java/live/PDServer1.java | 47 -
.../src/test/java/live/PDServer2.java | 47 -
.../src/test/java/live/PDServer3.java | 47 -
.../src/test/resources/application-server0.yml | 71 -
.../src/test/resources/application-server1.yml | 71 -
.../src/test/resources/application-server2.yml | 73 -
.../src/test/resources/application-server3.yml | 73 -
.../hg-pd-service/src/test/resources/banner.txt | 6 -
.../hg-pd-service/src/test/resources/log4j2.xml | 139 --
hugegraph-pd/hg-pd-test/pom.xml | 370 ----
.../apache/hugegraph/pd/client/BaseClientTest.java | 44 -
.../hugegraph/pd/client/DiscoveryClientTest.java | 79 -
.../apache/hugegraph/pd/client/KvClientTest.java | 121 --
.../hugegraph/pd/client/PDClientSuiteTest.java | 36 -
.../apache/hugegraph/pd/client/PDClientTest.java | 418 -----
.../hugegraph/pd/clitools/BaseCliToolsTest.java | 34 -
.../hugegraph/pd/clitools/CliToolsSuiteTest.java | 35 -
.../org/apache/hugegraph/pd/clitools/MainTest.java | 89 -
.../apache/hugegraph/pd/common/BaseCommonTest.java | 33 -
.../hugegraph/pd/common/CommonSuiteTest.java | 43 -
.../apache/hugegraph/pd/common/HgAssertTest.java | 134 --
.../org/apache/hugegraph/pd/common/KVPairTest.java | 73 -
.../hugegraph/pd/common/MetadataKeyHelperTest.java | 217 ---
.../hugegraph/pd/common/PartitionCacheTest.java | 392 -----
.../hugegraph/pd/common/PartitionUtilsTest.java | 35 -
.../org/apache/hugegraph/pd/core/BaseCoreTest.java | 74 -
.../apache/hugegraph/pd/core/PDCoreSuiteTest.java | 37 -
.../hugegraph/pd/core/StoreNodeServiceTest.java | 119 --
.../pd/core/meta/MetadataKeyHelperTest.java | 34 -
.../org/apache/hugegraph/pd/grpc/BaseGrpcTest.java | 36 -
.../apache/hugegraph/pd/grpc/GrpcSuiteTest.java | 33 -
.../hugegraph/pd/service/BaseServerTest.java | 57 -
.../hugegraph/pd/service/ConfigServiceTest.java | 106 --
.../apache/hugegraph/pd/service/IdServiceTest.java | 109 --
.../apache/hugegraph/pd/service/KvServiceTest.java | 60 -
.../hugegraph/pd/service/LogServiceTest.java | 54 -
.../hugegraph/pd/service/PartitionServiceTest.java | 133 --
.../apache/hugegraph/pd/service/PdTestBase.java | 219 ---
.../apache/hugegraph/pd/service/RestApiTest.java | 120 --
.../hugegraph/pd/service/ServerSuiteTest.java | 42 -
.../pd/service/StoreMonitorDataServiceTest.java | 82 -
.../pd/service/StoreNodeServiceNewTest.java | 64 -
.../hugegraph/pd/service/StoreServiceTest.java | 833 ---------
.../pd/service/TaskScheduleServiceTest.java | 114 --
hugegraph-pd/local-release.sh | 25 -
hugegraph-pd/mvnw | 308 ----
hugegraph-pd/mvnw.cmd | 182 --
hugegraph-pd/pom.xml | 318 ----
hugegraph-pd/settings.xml | 133 --
hugegraph-pd/start_pd_server.sh | 55 -
pom.xml | 2 +-
236 files changed, 1 insertion(+), 36208 deletions(-)
diff --git a/hugegraph-pd/.gitattributes b/hugegraph-pd/.gitattributes
deleted file mode 100644
index 4fd3cf5c2..000000000
--- a/hugegraph-pd/.gitattributes
+++ /dev/null
@@ -1 +0,0 @@
-* text=auto eol=lf
diff --git a/hugegraph-pd/.gitignore b/hugegraph-pd/.gitignore
deleted file mode 100644
index 689a7a80b..000000000
--- a/hugegraph-pd/.gitignore
+++ /dev/null
@@ -1,13 +0,0 @@
-/hg-pd-grpc/src/main/java/
-/.idea/
-/hg-pd-grpc/target/
-/dist/
-**/target/
-.DS_Store
-**/tmp/
-*.log
-*.iml
-/hg-pd-common/target_B000000405016P_Oct-28-114458-2021_conflict_parent/
-
-dist/
-.flattened-pom.xml
diff --git a/hugegraph-pd/.mvn/wrapper/MavenWrapperDownloader.java b/hugegraph-pd/.mvn/wrapper/MavenWrapperDownloader.java
deleted file mode 100644
index 574feef6b..000000000
--- a/hugegraph-pd/.mvn/wrapper/MavenWrapperDownloader.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * Copyright 2007-present the original author or 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.
- */
-
-import java.net.*;
-import java.io.*;
-import java.nio.channels.*;
-import java.util.Properties;
-
-public class MavenWrapperDownloader {
-
- private static final String WRAPPER_VERSION = "0.5.6";
- /**
- * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
- */
- private static final String DEFAULT_DOWNLOAD_URL =
- "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
- + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
-
- /**
- * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
- * use instead of the default one.
- */
- private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
- ".mvn/wrapper/maven-wrapper.properties";
-
- /**
- * Path where the maven-wrapper.jar will be saved to.
- */
- private static final String MAVEN_WRAPPER_JAR_PATH =
- ".mvn/wrapper/maven-wrapper.jar";
-
- /**
- * Name of the property which should be used to override the default download url for the
- * wrapper.
- */
- private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
-
- public static void main(String args[]) {
- System.out.println("- Downloader started");
- File baseDirectory = new File(args[0]);
- System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
-
- // If the maven-wrapper.properties exists, read it and check if it contains a custom
- // wrapperUrl parameter.
- File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
- String url = DEFAULT_DOWNLOAD_URL;
- if (mavenWrapperPropertyFile.exists()) {
- FileInputStream mavenWrapperPropertyFileInputStream = null;
- try {
- mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
- Properties mavenWrapperProperties = new Properties();
- mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
- url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
- } catch (IOException e) {
- System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
- } finally {
- try {
- if (mavenWrapperPropertyFileInputStream != null) {
- mavenWrapperPropertyFileInputStream.close();
- }
- } catch (IOException e) {
- // Ignore ...
- }
- }
- }
- System.out.println("- Downloading from: " + url);
-
- File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
- if (!outputFile.getParentFile().exists()) {
- if (!outputFile.getParentFile().mkdirs()) {
- System.out.println(
- "- ERROR creating output directory '" +
- outputFile.getParentFile().getAbsolutePath() + "'");
- }
- }
- System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
- try {
- downloadFileFromURL(url, outputFile);
- System.out.println("Done");
- System.exit(0);
- } catch (Throwable e) {
- System.out.println("- Error downloading");
- e.printStackTrace();
- System.exit(1);
- }
- }
-
- private static void downloadFileFromURL(String urlString, File destination) throws Exception {
- if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
- String username = System.getenv("MVNW_USERNAME");
- char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
- Authenticator.setDefault(new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- }
- URL website = new URL(urlString);
- ReadableByteChannel rbc;
- rbc = Channels.newChannel(website.openStream());
- FileOutputStream fos = new FileOutputStream(destination);
- fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
- fos.close();
- rbc.close();
- }
-
-}
diff --git a/hugegraph-pd/.mvn/wrapper/maven-wrapper.jar b/hugegraph-pd/.mvn/wrapper/maven-wrapper.jar
deleted file mode 100644
index 2cc7d4a55..000000000
Binary files a/hugegraph-pd/.mvn/wrapper/maven-wrapper.jar and /dev/null differ
diff --git a/hugegraph-pd/.mvn/wrapper/maven-wrapper.properties b/hugegraph-pd/.mvn/wrapper/maven-wrapper.properties
deleted file mode 100644
index 9c2bd3772..000000000
--- a/hugegraph-pd/.mvn/wrapper/maven-wrapper.properties
+++ /dev/null
@@ -1,18 +0,0 @@
-#
-# 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
-#
-# 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.
-#
-distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip
-wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
diff --git a/hugegraph-pd/README.md b/hugegraph-pd/README.md
index c01d5a26b..e69de29bb 100644
--- a/hugegraph-pd/README.md
+++ b/hugegraph-pd/README.md
@@ -1,940 +0,0 @@
-# PD部署说明
-
-## PD配置
-
-- 配置文件在application.yml
-
-````
-license:
- # 验证使用的配置文件所在目录,包括主题、密码等
- verify-path: 'conf/verify-license.json'
- # license文件所在目录,通过hugegraph-signature项目生成
- license-path: 'conf/hugegraph.license'
-pd:
- # 存储路径
- data-path: ./pd_data
- # 自动扩容的检查周期,定时检查每个Store的分区数量,自动进行分区数量平衡
- patrol-interval: 1800
- # 是否允许批量单副本入库
- enable-batch-load: false
-store:
- # store下线时间。超过该时间,认为store永久不可用,分配副本到其他机器,单位秒
- max-down-time: 172800
-partition:
- # 默认每个分区副本数
- default-shard-count: 3
- # 默认每机器最大副本数,初始分区数= store-max-shard-count * store-number / default-shard-count
- store-max-shard-count: 12
-````
-
-##store配置
--配置文件在application.yml,配置pdserver的address
-
-````
-pdserver:
- # pd服务地址,多个pd地址用逗号分割
- address: pdserver ip:端口
-````
-
-## Hugegraph配置
-
-- 配置项在hugegraph的启动脚本start-hugegraph.sh中
-
-````
-if [ -z "$META_SERVERS" ];then
- META_SERVERS="pdserver ip:端口"
-fi
-if [ -z "$PD_PEERS" ];then
- PD_PEERS="pdserver ip:端口"
-fi
-````
-
-## RESTFUL API
-
-- pd提供了一些restful API可以获取集群分区,图,存储节点等一系列信息
-
-###获取集群统计信息
-
-#### 获取集群统计信息
-
-###### Method & Url
-
-```
-GET http://localhost:8620/v1/cluster
-```
-
-###### Response Status
-
-```json
-200
-```
-
-###### Response Body
-
-```json
-{
- "message": "OK",
- "data": {
- "state": "Cluster_OK",
- "pdList": [
- {
- "raftUrl": "10.232.132.38:8610",
- "grpcUrl": "10.232.132.38:8686",
- "restUrl": "10.232.132.38:8620",
- "state": "Up",
- "dataPath": "./pd_data",
- "role": "Leader",
- "serviceName": "10.232.132.38:8686-PD",
- "serviceVersion": "",
- "startTimeStamp": 0
- }
- ],
- "pdLeader": {
- "raftUrl": "10.232.132.38:8610",
- "grpcUrl": "10.232.132.38:8686",
- "restUrl": "10.232.132.38:8620",
- "state": "Up",
- "dataPath": "./pd_data",
- "role": "Leader",
- "serviceName": "10.232.132.38:8686-PD",
- "serviceVersion": "",
- "startTimeStamp": 0
- },
- "memberSize": 1,
- "stores": [
- {
- "storeId": 110645464809417136,
- "address": "10.232.132.38:8500",
- "raftAddress": "10.232.132.38:8510",
- "version": "3.6.3",
- "state": "Up"
- }
- ],
- "storeSize": 1,
- "onlineStoreSize": 1,
- "offlineStoreSize": 0,
- "graphSize": 3,
- "partitionSize": 4,
- "shardCount": 3,
- "keyCount": 1707,
- "dataSize": 19
- },
- "status": 0
-}
-```
-
-#### 获取pd集群成员信息
-
-###### Method & Url
-
-```
-GET http://localhost:8620/v1/member
-```
-
-###### Response Status
-
-```json
-200
-```
-
-###### Response Body
-
-```json
-{
- "message": "OK",
- "data": {
- "pdLeader": {
- "raftUrl": "10.232.132.38:8610",
- "grpcUrl": "10.232.132.38:8686",
- "restUrl": "10.232.132.38:8620",
- "state": "Up",
- "dataPath": "./pd_data",
- "role": "Leader",
- "serviceName": "10.232.132.38:8686-PD",
- "serviceVersion": "",
- "startTimeStamp": 0
- },
- "pdList": [
- {
- "raftUrl": "10.232.132.38:8610",
- "grpcUrl": "10.232.132.38:8686",
- "restUrl": "10.232.132.38:8620",
- "state": "Up",
- "dataPath": "./pd_data",
- "role": "Leader",
- "serviceName": "10.232.132.38:8686-PD",
- "serviceVersion": "",
- "startTimeStamp": 0
- }
- ],
- "state": "Cluster_OK"
- },
- "status": 0
-}
-```
-
-###存储节点相关
-
-#### 获取集群所有的store的信息
-
-###### Method & Url
-
-```
-GET http://localhost:8620/v1/stores
-```
-
-###### Response Status
-
-```json
-200
-```
-
-###### Request Body
-
-```json
-{
- "message": "OK",
- "data": {
- "stores": [
- {
- "storeId": 110645464809417136,
- "address": "10.232.132.38:8500",
- "raftAddress": "10.232.132.38:8510",
- "version": "3.6.3",
- "state": "Up",
- "deployPath": "",
- "startTimeStamp": 1658491024,
- "lastHeatBeat": 1658491748560,
- "capacity": 1968740712448,
- "available": 1959665557504,
- "partitionCount": 4,
- "graphSize": 3,
- "keyCount": 1128,
- "leaderCount": 4,
- "serviceName": "10.232.132.38:8500-store",
- "serviceVersion": "3.6.3",
- "partitions": [
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/s",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/g",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 1,
- "graphName": "DEFAULT/hugegraph/g",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 2,
- "graphName": "DEFAULT/hugegraph/g",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 3,
- "graphName": "DEFAULT/hugegraph/g",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/m",
- "role": "Leader",
- "workState": "PState_Normal"
- }
- ]
- }
- ]
- },
- "status": 0
-}
-```
-
-#### 获取单个store的信息
-
-###### Method & Url
-
-```
-GET http://localhost:8620/v1/store/{storeId}
-```
-
-###### Response Status
-
-```json
-200
-```
-
-###### Request Body
-
-```json
-{
- "message": "OK",
- "data": {
- "storeId": 110645464809417136,
- "address": "10.232.132.38:8500",
- "raftAddress": "10.232.132.38:8510",
- "version": "3.6.3",
- "state": "Up",
- "deployPath": "",
- "startTimeStamp": 1658491024,
- "lastHeatBeat": 1658491838632,
- "capacity": 1968740712448,
- "available": 1959665549312,
- "partitionCount": 4,
- "graphSize": 3,
- "keyCount": 1128,
- "leaderCount": 4,
- "serviceName": "10.232.132.38:8500-store",
- "serviceVersion": "3.6.3",
- "partitions": [
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/s",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/g",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 1,
- "graphName": "DEFAULT/hugegraph/g",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 2,
- "graphName": "DEFAULT/hugegraph/g",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 3,
- "graphName": "DEFAULT/hugegraph/g",
- "role": "Leader",
- "workState": "PState_Normal"
- },
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/m",
- "role": "Leader",
- "workState": "PState_Normal"
- }
- ]
- },
- "status": 0
-}
-```
-
-### 分区相关
-
-#### 获取分区信息
-
-###### Method & Url
-
-```
-GET http://localhost:8620/v1/highLevelPartitions
-```
-
-###### Response Status
-
-```json
-200
-```
-
-###### Response Body
-
-```json
-{
- "message": "OK",
- "data": {
- "partitions": [
- {
- "partitionId": 0,
- "state": "PState_Normal",
- "leaderAddress": "10.232.132.38:8500",
- "keyCount": 0,
- "dataSize": 0,
- "shardState": "SState_Normal",
- "graphs": [
- {
- "graphName": "DEFAULT/hugegraph/g",
- "keyCount": 361,
- "startKey": 0,
- "endKey": 0,
- "dataSize": 8,
- "workState": "PState_Normal",
- "partitionId": 0
- },
- {
- "graphName": "DEFAULT/hugegraph/m",
- "keyCount": 361,
- "startKey": 0,
- "endKey": 0,
- "dataSize": 13,
- "workState": "PState_Normal",
- "partitionId": 0
- },
- {
- "graphName": "DEFAULT/hugegraph/s",
- "keyCount": 361,
- "startKey": 0,
- "endKey": 65535,
- "dataSize": 6,
- "workState": "PState_Normal",
- "partitionId": 0
- }
- ],
- "shards": [
- {
- "storeId": 110645464809417136,
- "role": "Leader",
- "state": "SState_Normal",
- "progress": 0,
- "partitionId": 0,
- "address": "10.232.132.38:8500"
- }
- ]
- },
- {
- "partitionId": 1,
- "state": "PState_Normal",
- "leaderAddress": "10.232.132.38:8500",
- "keyCount": 0,
- "dataSize": 0,
- "shardState": "SState_Normal",
- "graphs": [
- {
- "graphName": "DEFAULT/hugegraph/g",
- "keyCount": 8,
- "startKey": 16384,
- "endKey": 32768,
- "dataSize": 5,
- "workState": "PState_Normal",
- "partitionId": 1
- }
- ],
- "shards": [
- {
- "storeId": 110645464809417136,
- "role": "Leader",
- "state": "SState_Normal",
- "progress": 0,
- "partitionId": 1,
- "address": "10.232.132.38:8500"
- }
- ]
- },
- {
- "partitionId": 2,
- "state": "PState_Normal",
- "leaderAddress": "10.232.132.38:8500",
- "keyCount": 0,
- "dataSize": 0,
- "shardState": "SState_Normal",
- "graphs": [
- {
- "graphName": "DEFAULT/hugegraph/g",
- "keyCount": 18,
- "startKey": 32768,
- "endKey": 49152,
- "dataSize": 8,
- "workState": "PState_Normal",
- "partitionId": 2
- }
- ],
- "shards": [
- {
- "storeId": 110645464809417136,
- "role": "Leader",
- "state": "SState_Normal",
- "progress": 0,
- "partitionId": 2,
- "address": "10.232.132.38:8500"
- }
- ]
- },
- {
- "partitionId": 3,
- "state": "PState_Normal",
- "leaderAddress": "10.232.132.38:8500",
- "keyCount": 0,
- "dataSize": 0,
- "shardState": "SState_Normal",
- "graphs": [
- {
- "graphName": "DEFAULT/hugegraph/g",
- "keyCount": 19,
- "startKey": 49152,
- "endKey": 65536,
- "dataSize": 8,
- "workState": "PState_Normal",
- "partitionId": 3
- }
- ],
- "shards": [
- {
- "storeId": 110645464809417136,
- "role": "Leader",
- "state": "SState_Normal",
- "progress": 0,
- "partitionId": 3,
- "address": "10.232.132.38:8500"
- }
- ]
- }
- ]
- },
- "status": 0
-}
-```
-
-###获取图信息
-
-#### 获取所有的图信息
-
-###### Method & Url
-
-```
-GET http://localhost:8620/v1/graphs
-```
-
-###### Response Status
-
-```json
-200
-```
-
-###### Response Body
-
-```json
-{
- "message": "OK",
- "data": {
- "graphs": [
- {
- "graphName": "DEFAULT/hugegraph/g",
- "partitionCount": 4,
- "state": "PState_Normal",
- "partitions": [
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/g",
- "workState": "PState_Normal",
- "startKey": 0,
- "endKey": 16384,
- "shards": [
- {
- "partitionId": 0,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- },
- {
- "partitionId": 1,
- "graphName": "DEFAULT/hugegraph/g",
- "workState": "PState_Normal",
- "startKey": 16384,
- "endKey": 32768,
- "shards": [
- {
- "partitionId": 1,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- },
- {
- "partitionId": 2,
- "graphName": "DEFAULT/hugegraph/g",
- "workState": "PState_Normal",
- "startKey": 32768,
- "endKey": 49152,
- "shards": [
- {
- "partitionId": 2,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- },
- {
- "partitionId": 3,
- "graphName": "DEFAULT/hugegraph/g",
- "workState": "PState_Normal",
- "startKey": 49152,
- "endKey": 65536,
- "shards": [
- {
- "partitionId": 3,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- }
- ],
- "dataSize": 48,
- "keyCount": 1128,
- "nodeCount": 0,
- "edgeCount": 0
- },
- {
- "graphName": "DEFAULT/hugegraph/m",
- "partitionCount": 1,
- "state": "PState_Normal",
- "partitions": [
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/m",
- "workState": "PState_Normal",
- "startKey": 0,
- "endKey": 65535,
- "shards": [
- {
- "partitionId": 0,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- }
- ],
- "dataSize": 48,
- "keyCount": 1128,
- "nodeCount": 0,
- "edgeCount": 0
- },
- {
- "graphName": "DEFAULT/hugegraph/s",
- "partitionCount": 1,
- "state": "PState_Normal",
- "partitions": [
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/s",
- "workState": "PState_Normal",
- "startKey": 0,
- "endKey": 65535,
- "shards": [
- {
- "partitionId": 0,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- }
- ],
- "dataSize": 48,
- "keyCount": 1128,
- "nodeCount": 0,
- "edgeCount": 0
- }
- ]
- },
- "status": 0
-}
-```
-
-#### 获取单个图信息
-
-###### Method & Url
-
-```
-GET http://localhost:8620/v1/graph/{graphName}
-```
-
-###### Response Status
-
-```json
-200
-```
-
-###### Response Body
-
-```json
-{
- "message": "OK",
- "data": {
- "graphName": "DEFAULT/hugegraph/g",
- "partitionCount": 4,
- "state": "PState_Normal",
- "partitions": [
- {
- "partitionId": 0,
- "graphName": "DEFAULT/hugegraph/g",
- "workState": "PState_Normal",
- "startKey": 0,
- "endKey": 16384,
- "shards": [
- {
- "partitionId": 0,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- },
- {
- "partitionId": 1,
- "graphName": "DEFAULT/hugegraph/g",
- "workState": "PState_Normal",
- "startKey": 16384,
- "endKey": 32768,
- "shards": [
- {
- "partitionId": 1,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- },
- {
- "partitionId": 2,
- "graphName": "DEFAULT/hugegraph/g",
- "workState": "PState_Normal",
- "startKey": 32768,
- "endKey": 49152,
- "shards": [
- {
- "partitionId": 2,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- },
- {
- "partitionId": 3,
- "graphName": "DEFAULT/hugegraph/g",
- "workState": "PState_Normal",
- "startKey": 49152,
- "endKey": 65536,
- "shards": [
- {
- "partitionId": 3,
- "storeId": 110645464809417136,
- "state": "SState_Normal",
- "role": "Leader",
- "progress": 0
- }
- ]
- }
- ],
- "dataSize": 48,
- "keyCount": 1128,
- "nodeCount": 0,
- "edgeCount": 0
- },
- "status": 0
-}
-```
-
-###获取shard的信息
-
-#### 获取所有shard的信息
-
-###### Method & Url
-
-```
-GET http://localhost:8620/v1/shards
-```
-
-###### Response Status
-
-```json
-200
-```
-
-###### Response Body
-
-```json
-{
- "message": "OK",
- "data": {
- "shards": [
- {
- "storeId": 110645464809417136,
- "partitionId": 0,
- "role": "Leader",
- "state": "SState_Normal",
- "graphName": "DEFAULT/hugegraph/g",
- "progress": 0
- },
- {
- "storeId": 110645464809417136,
- "partitionId": 1,
- "role": "Leader",
- "state": "SState_Normal",
- "graphName": "DEFAULT/hugegraph/g",
- "progress": 0
- },
- {
- "storeId": 110645464809417136,
- "partitionId": 2,
- "role": "Leader",
- "state": "SState_Normal",
- "graphName": "DEFAULT/hugegraph/g",
- "progress": 0
- },
- {
- "storeId": 110645464809417136,
- "partitionId": 3,
- "role": "Leader",
- "state": "SState_Normal",
- "graphName": "DEFAULT/hugegraph/g",
- "progress": 0
- },
- {
- "storeId": 110645464809417136,
- "partitionId": 0,
- "role": "Leader",
- "state": "SState_Normal",
- "graphName": "DEFAULT/hugegraph/m",
- "progress": 0
- },
- {
- "storeId": 110645464809417136,
- "partitionId": 0,
- "role": "Leader",
- "state": "SState_Normal",
- "graphName": "DEFAULT/hugegraph/s",
- "progress": 0
- }
- ]
- },
- "status": 0
-}
-```
-
-###服务注册
-
-#### 注册服务
-
-###### Method & Url
-
-```
-POST http://127.0.0.1:8620/v1/registry
-```
-
-###### Request Body
-
-```json
-200
-```
-
-###### Response Status
-
-```json
-{
- "appName":"aaaa",
- "version":"version1",
- "address":"address1",
- "interval":"9223372036854775807",
- "labels": {
- "aaa": "aaaavalue"
- }
-}
-```
-
-appName:所属服务名
-version:所属服务版本号
-address:服务实例地址+端口
-interval:实例心跳间隔,字符串,最大9223372036854775807
-labels: 自定义标签,若服务名为'hg'即hugeserver时,需要提供key为cores的项,进行cpu核数的验证
-
-###### Response Body
-
-```json
-{
- "errorType": "OK",
- "message": "",
- "data": null
-}
-```
-
-errorType:状态码
-message:状态码为错误时的具体出错信息
-data:无返回数据
-
-#### 服务实例获取
-
-###### Method & Url
-
-```
-POST http://127.0.0.1:8620/v1/registryInfo
-```
-
-###### Request Body
-
-```json
-200
-```
-
-###### Response Status
-
-```json
-{
- "appName":"aaaa",
- "version":"version1",
- "labels": {
- "aaa": "aaaavalue"
- }
-}
-```
-
-以下三项可全部为空,则获取所有服务节点的信息:
--- appName:过滤所属服务名的条件
--- version:过滤所属服务版本号的条件,此项有值,则appName不能为空
--- labels: 过滤自定义标签的条件
-
-###### Response Body
-
-```json
-{
- "errorType": "OK",
- "message": null,
- "data": [
- {
- "id": null,
- "appName": "aaaa",
- "version": "version1",
- "address": "address1",
- "interval": "9223372036854775807",
- "labels": {
- "aaa": "aaaavalue"
- }
- }
- ]
-}
-```
-
-errorType:状态码
-message:状态码为错误时的具体出错信息
-data:获取的服务节点信息
\ No newline at end of file
diff --git a/hugegraph-pd/build-pre.sh b/hugegraph-pd/build-pre.sh
deleted file mode 100644
index f92d104ee..000000000
--- a/hugegraph-pd/build-pre.sh
+++ /dev/null
@@ -1,54 +0,0 @@
-#!/bin/bash
-#
-# 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
-#
-# 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.
-#
-
-mkdir output
-touch output/1
-export JAVA_HOME=$ORACLEJDK_1_8_0_HOME
-
-readonly VER=3.6.3
-readonly REPO_URL=http://127.0.0.1:8081/artifactory/star-local
-
-$MAVEN_3_5_3_BIN/mvn -DremoveSnapshot=true -DprocessAllModules=true -DgenerateBackupPoms=true versions:set
-$MAVEN_3_5_3_BIN/mvn --settings ./settings.xml -Dmaven.test.skip=true -DaltDeploymentRepository=star-local::default::${REPO_URL} clean deploy
-$MAVEN_3_5_3_BIN/mvn versions:revert
-
-
-#------------------repo-----------------------
-readonly FILE_NAME=hugegraph-pd-3.6.3.tar.gz
-readonly REPO_URL_FILE=http://127.0.0.1:8081/artifactory/star-file
-
-localFilePath=dist/${FILE_NAME}
-targetFolder="${REPO_URL_FILE}/dist/$(date '+%Y-%m-%d')/"
-artifactoryUser="admin"
-artifactoryPassword="JFrog12345"
-
-md5Value="$(md5sum "$localFilePath")"
-md5Value="${md5Value:0:32}"
-sha1Value="$(sha1sum "$localFilePath")"
-sha1Value="${sha1Value:0:40}"
-sha256Value="$(sha256sum "$localFilePath")"
-sha256Value="${sha256Value:0:65}"
-
-#curl -X PUT -u admin:JFrog12345 -T ${localFilePath} "${REPO_URL_FILE}/dist/${data_folder}/"
-echo "INFO: Uploading $localFilePath to $targetFolder"
-curl -i -X PUT -u "$artifactoryUser:$artifactoryPassword" \
- -H "X-Checksum-Md5: $md5Value" \
- -H "X-Checksum-Sha1: $sha1Value" \
- -H "X-Checksum-Sha256: $sha256Value" \
- -T "$localFilePath" \
- "$targetFolder"
diff --git a/hugegraph-pd/build.sh b/hugegraph-pd/build.sh
deleted file mode 100644
index f77358e1c..000000000
--- a/hugegraph-pd/build.sh
+++ /dev/null
@@ -1,39 +0,0 @@
-#!/bin/bash
-#
-# 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
-#
-# 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.
-#
-
-export PATH=$MAVEN_3_5_3_BIN:$ORACLEJDK_11_0_7_BIN:$PATH
-export JAVA_HOME=$ORACLEJDK_11_0_7_HOME
-export MAVEN_HOME=$MAVEN_3_5_3_HOME
-
-# TODO: remove later
-readonly REPO_URL=http://maven.baidu-int.com/nexus/content/repositories/Baidu_Local_Snapshots
-
-if [ ! -n "$1" ] ;then
- GOAL=package
-else
- GOAL=$1
-fi
-
-$MAVEN_3_5_3_BIN/mvn -Dmaven.test.skip=true -DaltDeploymentRepository=Baidu_Local_Snapshots::default::${REPO_URL} clean ${GOAL}
-echo "mv dist...."
-mv dist output
-ls output
-echo "mv dist done"
-echo "show output...."
-ls output
-echo "show output done"
diff --git a/hugegraph-pd/ci.yml b/hugegraph-pd/ci.yml
deleted file mode 100644
index 0eb7515ae..000000000
--- a/hugegraph-pd/ci.yml
+++ /dev/null
@@ -1,43 +0,0 @@
-#
-# 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
-#
-# 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.
-#
-
-Global:
- tool: build_submitter
- languages:
- # Java Version: 1.7, 1.8, 9, 10
- - language: java
- version: 11
- envs:
- # Maven Version: 3.0.4, 3.1.1, 3.2.5, 3.3.9, 3.5.3
- - env: maven
- version: 3.5.3
-Default:
- profile: [ dev ]
-Profiles:
- - profile:
- name: dev
- tool: build_submitter
- env: DECK_CENTOS7U5_K3
- command: sh build.sh
- release: true
-
- - profile:
- name: deploy
- tool: build_submitter
- env: DECK_CENTOS7U5_K3
- command: sh build.sh deploy
- release: true
diff --git a/hugegraph-pd/conf/hugegraph.license b/hugegraph-pd/conf/hugegraph.license
deleted file mode 100644
index bca71af9a..000000000
Binary files a/hugegraph-pd/conf/hugegraph.license and /dev/null differ
diff --git a/hugegraph-pd/conf/verify-license.json b/hugegraph-pd/conf/verify-license.json
deleted file mode 100644
index 868ccbebb..000000000
--- a/hugegraph-pd/conf/verify-license.json
+++ /dev/null
@@ -1,6 +0,0 @@
-{
- "subject": "hugegraph-license",
- "public_alias": "publiccert",
- "store_ticket": "803b6cc3-d144-47e8-948f-ec8b39c8881e",
- "publickey_path": "/public-certs.store"
-}
diff --git a/hugegraph-pd/deploy-release.sh b/hugegraph-pd/deploy-release.sh
deleted file mode 100644
index 118a214d7..000000000
--- a/hugegraph-pd/deploy-release.sh
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/bin/bash
-#
-# 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
-#
-# 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.
-#
-
-readonly VER=3.6.3
-readonly REPO_URL=http://127.0.0.1:8081/artifactory/star-local
-#mvn -DnewVersion=${VER}-SNAPSHOT -DprocessAllModules=true -DgenerateBackupPoms=false versions:set
-
-./mvnw -DremoveSnapshot=true -DprocessAllModules=true -DgenerateBackupPoms=true versions:set
-./mvnw --settings ./settings.xml -Dmaven.test.skip=true -DaltDeploymentRepository=star-local::default::${REPO_URL} clean deploy
-./mvnw versions:revert
diff --git a/hugegraph-pd/deploy-snapshot.sh b/hugegraph-pd/deploy-snapshot.sh
deleted file mode 100644
index 118a214d7..000000000
--- a/hugegraph-pd/deploy-snapshot.sh
+++ /dev/null
@@ -1,25 +0,0 @@
-#!/bin/bash
-#
-# 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
-#
-# 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.
-#
-
-readonly VER=3.6.3
-readonly REPO_URL=http://127.0.0.1:8081/artifactory/star-local
-#mvn -DnewVersion=${VER}-SNAPSHOT -DprocessAllModules=true -DgenerateBackupPoms=false versions:set
-
-./mvnw -DremoveSnapshot=true -DprocessAllModules=true -DgenerateBackupPoms=true versions:set
-./mvnw --settings ./settings.xml -Dmaven.test.skip=true -DaltDeploymentRepository=star-local::default::${REPO_URL} clean deploy
-./mvnw versions:revert
diff --git a/hugegraph-pd/hg-pd-client/pom.xml b/hugegraph-pd/hg-pd-client/pom.xml
deleted file mode 100644
index c2d67ff92..000000000
--- a/hugegraph-pd/hg-pd-client/pom.xml
+++ /dev/null
@@ -1,73 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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
-
- 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.
- -->
-
-<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://maven.apache.org/POM/4.0.0"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hugegraph-pd</artifactId>
- <version>${revision}</version>
- <relativePath>../pom.xml</relativePath>
- </parent>
- <artifactId>hg-pd-client</artifactId>
-
- <dependencies>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.20</version>
- </dependency>
- <dependency>
- <groupId>org.apache.logging.log4j</groupId>
- <artifactId>log4j-slf4j-impl</artifactId>
- <version>2.17.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hg-pd-grpc</artifactId>
- <version>${revision}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hg-pd-common</artifactId>
- <version>${revision}</version>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.13.2</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.8.0</version>
- </dependency>
- <dependency>
- <groupId>org.yaml</groupId>
- <artifactId>snakeyaml</artifactId>
- <version>1.28</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
-</project>
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/AbstractClient.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/AbstractClient.java
deleted file mode 100644
index db48eeee3..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/AbstractClient.java
+++ /dev/null
@@ -1,265 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.io.Closeable;
-import java.util.LinkedList;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
-import java.util.function.Predicate;
-import java.util.stream.Stream;
-
-import org.apache.hugegraph.pd.common.KVPair;
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.grpc.Metapb;
-import org.apache.hugegraph.pd.grpc.PDGrpc;
-import org.apache.hugegraph.pd.grpc.PDGrpc.PDBlockingStub;
-import org.apache.hugegraph.pd.grpc.Pdpb;
-import org.apache.hugegraph.pd.grpc.Pdpb.GetMembersRequest;
-import org.apache.hugegraph.pd.grpc.Pdpb.GetMembersResponse;
-
-import io.grpc.Channel;
-import io.grpc.ClientCall;
-import io.grpc.ManagedChannel;
-import io.grpc.ManagedChannelBuilder;
-import io.grpc.MethodDescriptor;
-import io.grpc.StatusRuntimeException;
-import io.grpc.stub.AbstractBlockingStub;
-import io.grpc.stub.AbstractStub;
-import io.grpc.stub.ClientCalls;
-import io.grpc.stub.StreamObserver;
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-public abstract class AbstractClient implements Closeable {
-
- private static final ConcurrentHashMap<String, ManagedChannel> chs = new ConcurrentHashMap<>();
- public static Pdpb.ResponseHeader okHeader = Pdpb.ResponseHeader.newBuilder().setError(
- Pdpb.Error.newBuilder().setType(Pdpb.ErrorType.OK)).build();
- protected final Pdpb.RequestHeader header;
- protected final AbstractClientStubProxy stubProxy;
- protected final PDConfig config;
- protected ManagedChannel channel = null;
- protected volatile ConcurrentMap<String, AbstractBlockingStub> stubs = null;
-
- protected AbstractClient(PDConfig config) {
- String[] hosts = config.getServerHost().split(",");
- this.stubProxy = new AbstractClientStubProxy(hosts);
- this.header = Pdpb.RequestHeader.getDefaultInstance();
- this.config = config;
- }
-
- public static Pdpb.ResponseHeader newErrorHeader(int errorCode, String errorMsg) {
- Pdpb.ResponseHeader header = Pdpb.ResponseHeader.newBuilder().setError(
- Pdpb.Error.newBuilder().setTypeValue(errorCode).setMessage(errorMsg)).build();
- return header;
- }
-
- protected static void handleErrors(Pdpb.ResponseHeader header) throws PDException {
- if (header.hasError() && header.getError().getType() != Pdpb.ErrorType.OK) {
- throw new PDException(header.getError().getTypeValue(),
- String.format("PD request error, error code = %d, msg = %s",
- header.getError().getTypeValue(),
- header.getError().getMessage()));
- }
- }
-
- protected AbstractBlockingStub getBlockingStub() throws PDException {
- if (stubProxy.getBlockingStub() == null) {
- synchronized (this) {
- if (stubProxy.getBlockingStub() == null) {
- String host = resetStub();
- if (host.isEmpty()) {
- throw new PDException(Pdpb.ErrorType.PD_UNREACHABLE_VALUE,
- "PD unreachable, pd.peers=" +
- config.getServerHost());
- }
- }
- }
- }
- return (AbstractBlockingStub) stubProxy.getBlockingStub()
- .withDeadlineAfter(config.getGrpcTimeOut(),
- TimeUnit.MILLISECONDS);
- }
-
- protected AbstractStub getStub() throws PDException {
- if (stubProxy.getStub() == null) {
- synchronized (this) {
- if (stubProxy.getStub() == null) {
- String host = resetStub();
- if (host.isEmpty()) {
- throw new PDException(Pdpb.ErrorType.PD_UNREACHABLE_VALUE,
- "PD unreachable, pd.peers=" +
- config.getServerHost());
- }
- }
- }
- }
- return stubProxy.getStub();
- }
-
- protected abstract AbstractStub createStub();
-
- protected abstract AbstractBlockingStub createBlockingStub();
-
- private String resetStub() {
- String leaderHost = "";
- for (int i = 0; i < stubProxy.getHostCount(); i++) {
- String host = stubProxy.nextHost();
- channel = ManagedChannelBuilder.forTarget(host).usePlaintext().build();
- PDBlockingStub blockingStub = PDGrpc.newBlockingStub(channel)
- .withDeadlineAfter(config.getGrpcTimeOut(),
- TimeUnit.MILLISECONDS);
- try {
- GetMembersRequest request = Pdpb.GetMembersRequest.newBuilder()
- .setHeader(header).build();
- GetMembersResponse members = blockingStub.getMembers(request);
- Metapb.Member leader = members.getLeader();
- leaderHost = leader.getGrpcUrl();
- close();
- channel = ManagedChannelBuilder.forTarget(leaderHost).usePlaintext().build();
- stubProxy.setBlockingStub(createBlockingStub());
- stubProxy.setStub(createStub());
- log.info("PDClient connect to host = {} success", leaderHost);
- break;
- } catch (Exception e) {
- log.error("PDClient connect to {} exception {}, {}", host, e.getMessage(),
- e.getCause() != null ? e.getCause().getMessage() : "");
- }
- }
- return leaderHost;
- }
-
- protected <ReqT, RespT, StubT extends AbstractBlockingStub<StubT>> RespT blockingUnaryCall(
- MethodDescriptor<ReqT, RespT> method, ReqT req) throws PDException {
- return blockingUnaryCall(method, req, 5);
- }
-
- protected <ReqT, RespT, StubT extends AbstractBlockingStub<StubT>> RespT blockingUnaryCall(
- MethodDescriptor<ReqT, RespT> method, ReqT req, int retry) throws PDException {
- AbstractBlockingStub stub = getBlockingStub();
- try {
- RespT resp =
- ClientCalls.blockingUnaryCall(stub.getChannel(), method, stub.getCallOptions(),
- req);
- return resp;
- } catch (Exception e) {
- log.error(method.getFullMethodName() + " exception, {}", e.getMessage());
- if (e instanceof StatusRuntimeException) {
- if (retry < stubProxy.getHostCount()) {
- // 网络不通,关掉之前连接,换host重新连接
- synchronized (this) {
- stubProxy.setBlockingStub(null);
- }
- return blockingUnaryCall(method, req, ++retry);
- }
- }
- }
- return null;
- }
-
- // this.stubs = new ConcurrentHashMap<String,AbstractBlockingStub>(hosts.length);
- private AbstractBlockingStub getConcurrentBlockingStub(String address) {
- AbstractBlockingStub stub = stubs.get(address);
- if (stub != null) {
- return stub;
- }
- Channel ch = ManagedChannelBuilder.forTarget(address).usePlaintext().build();
- PDBlockingStub blockingStub =
- PDGrpc.newBlockingStub(ch).withDeadlineAfter(config.getGrpcTimeOut(),
- TimeUnit.MILLISECONDS);
- stubs.put(address, blockingStub);
- return blockingStub;
-
- }
-
- protected <ReqT, RespT> KVPair<Boolean, RespT> concurrentBlockingUnaryCall(
- MethodDescriptor<ReqT, RespT> method, ReqT req, Predicate<RespT> predicate) {
- LinkedList<String> hostList = this.stubProxy.getHostList();
- if (this.stubs == null) {
- synchronized (this) {
- if (this.stubs == null) {
- this.stubs = new ConcurrentHashMap<>(hostList.size());
- }
- }
- }
- Stream<RespT> respTStream = hostList.parallelStream().map((address) -> {
- AbstractBlockingStub stub = getConcurrentBlockingStub(address);
- RespT resp = ClientCalls.blockingUnaryCall(stub.getChannel(),
- method, stub.getCallOptions(), req);
- return resp;
- });
- KVPair<Boolean, RespT> pair;
- AtomicReference<RespT> response = new AtomicReference<>();
- boolean result = respTStream.anyMatch((r) -> {
- response.set(r);
- return predicate.test(r);
- });
- if (result) {
- pair = new KVPair<>(true, null);
- } else {
- pair = new KVPair<>(false, response.get());
- }
- return pair;
- }
-
- protected <ReqT, RespT> void streamingCall(MethodDescriptor<ReqT, RespT> method, ReqT request,
- StreamObserver<RespT> responseObserver,
- int retry) throws PDException {
- AbstractStub stub = getStub();
- try {
- ClientCall<ReqT, RespT> call = stub.getChannel().newCall(method, stub.getCallOptions());
- ClientCalls.asyncServerStreamingCall(call, request, responseObserver);
- } catch (Exception e) {
- if (e instanceof StatusRuntimeException) {
- if (retry < stubProxy.getHostCount()) {
- synchronized (this) {
- stubProxy.setStub(null);
- }
- streamingCall(method, request, responseObserver, ++retry);
- return;
- }
- }
- log.error("rpc call with exception, {}", e.getMessage());
- }
- }
-
- @Override
- public void close() {
- closeChannel(channel);
- if (stubs != null) {
- for (AbstractBlockingStub stub : stubs.values()) {
- closeChannel((ManagedChannel) stub.getChannel());
- }
- }
-
- }
-
- private void closeChannel(ManagedChannel channel) {
- try {
- while (channel != null &&
- !channel.shutdownNow().awaitTermination(100, TimeUnit.MILLISECONDS)) {
- continue;
- }
- } catch (Exception e) {
- log.info("Close channel with error : ", e);
- }
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/AbstractClientStubProxy.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/AbstractClientStubProxy.java
deleted file mode 100644
index 150633ba2..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/AbstractClientStubProxy.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.util.LinkedList;
-
-import io.grpc.stub.AbstractBlockingStub;
-import io.grpc.stub.AbstractStub;
-
-public class AbstractClientStubProxy {
-
- private final LinkedList<String> hostList = new LinkedList<>();
- private AbstractBlockingStub blockingStub;
- private AbstractStub stub;
-
- public AbstractClientStubProxy(String[] hosts) {
- for (String host : hosts) {
- if (!host.isEmpty()) {
- hostList.offer(host);
- }
- }
- }
-
- public LinkedList<String> getHostList() {
- return hostList;
- }
-
- public String nextHost() {
- String host = hostList.poll();
- hostList.offer(host); //移到尾部
- return host;
- }
-
- public AbstractBlockingStub getBlockingStub() {
- return this.blockingStub;
- }
-
- public void setBlockingStub(AbstractBlockingStub stub) {
- this.blockingStub = stub;
- }
-
- public String getHost() {
- return hostList.peek();
- }
-
- public int getHostCount() {
- return hostList.size();
- }
-
- public AbstractStub getStub() {
- return stub;
- }
-
- public void setStub(AbstractStub stub) {
- this.stub = stub;
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/Channels.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/Channels.java
deleted file mode 100644
index c441fb876..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/Channels.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.util.concurrent.ConcurrentHashMap;
-
-import io.grpc.ManagedChannel;
-import io.grpc.ManagedChannelBuilder;
-
-public class Channels {
-
- private static final ConcurrentHashMap<String, ManagedChannel> chs = new ConcurrentHashMap<>();
-
- public static ManagedChannel getChannel(String target) {
-
- ManagedChannel channel;
- if ((channel = chs.get(target)) == null || channel.isShutdown() || channel.isTerminated()) {
- synchronized (chs) {
- if ((channel = chs.get(target)) == null || channel.isShutdown() ||
- channel.isTerminated()) {
- channel = ManagedChannelBuilder.forTarget(target).usePlaintext().build();
- chs.put(target, channel);
- }
- }
- }
-
- return channel;
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/ClientCache.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/ClientCache.java
deleted file mode 100644
index 92d8cc95d..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/ClientCache.java
+++ /dev/null
@@ -1,338 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Objects;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicBoolean;
-
-import org.apache.hugegraph.pd.common.GraphCache;
-import org.apache.hugegraph.pd.common.KVPair;
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.common.PartitionUtils;
-import org.apache.hugegraph.pd.grpc.Metapb;
-import org.apache.hugegraph.pd.grpc.Metapb.Partition;
-import org.apache.hugegraph.pd.grpc.Metapb.Shard;
-import org.apache.hugegraph.pd.grpc.Metapb.ShardGroup;
-import org.apache.hugegraph.pd.grpc.Pdpb.CachePartitionResponse;
-import org.apache.hugegraph.pd.grpc.Pdpb.CacheResponse;
-
-import com.google.common.collect.Range;
-import com.google.common.collect.RangeMap;
-
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-public class ClientCache {
-
- private final AtomicBoolean initialized = new AtomicBoolean(false);
- private final org.apache.hugegraph.pd.client.PDClient client;
- private volatile Map<Integer, KVPair<ShardGroup, Shard>> groups;
- private volatile Map<Long, Metapb.Store> stores;
- private volatile Map<String, GraphCache> caches = new ConcurrentHashMap<>();
-
- public ClientCache(org.apache.hugegraph.pd.client.PDClient pdClient) {
- groups = new ConcurrentHashMap<>();
- stores = new ConcurrentHashMap<>();
- client = pdClient;
- }
-
- private GraphCache getGraphCache(String graphName) {
- GraphCache graph;
- if ((graph = caches.get(graphName)) == null) {
- synchronized (caches) {
- if ((graph = caches.get(graphName)) == null) {
- graph = new GraphCache();
- caches.put(graphName, graph);
- }
- }
- }
- return graph;
- }
-
- public KVPair<Partition, Shard> getPartitionById(String graphName, int partId) {
- try {
- GraphCache graph = initGraph(graphName);
- Partition partition = graph.getPartition(partId);
- Shard shard = groups.get(partId).getValue();
- if (partition == null || shard == null) {
- return null;
- }
- return new KVPair<>(partition, shard);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- private KVPair<Partition, Shard> getPair(int partId, GraphCache graph) {
- Partition p = graph.getPartition(partId);
- KVPair<ShardGroup, Shard> pair = groups.get(partId);
- if (p != null && pair != null) {
- Shard s = pair.getValue();
- if (s == null) {
- pair.setValue(getLeader(partId));
- return new KVPair<>(p, pair.getValue());
- } else {
- return new KVPair<>(p, s);
- }
- }
- return null;
- }
-
- /**
- * 根据key的hashcode返回分区信息
- *
- * @param graphName
- * @param code
- * @return
- */
- public KVPair<Partition, Shard> getPartitionByCode(String graphName, long code) {
- try {
- GraphCache graph = initGraph(graphName);
- RangeMap<Long, Integer> range = graph.getRange();
- Integer pId = range.get(code);
- if (pId != null) {
- return getPair(pId, graph);
- }
- return null;
- } catch (PDException e) {
- throw new RuntimeException(e);
- }
- }
-
- private GraphCache initGraph(String graphName) throws PDException {
- initCache();
- GraphCache graph = getGraphCache(graphName);
- if (!graph.getInitialized().get()) {
- synchronized (graph) {
- if (!graph.getInitialized().get()) {
- CachePartitionResponse pc = client.getPartitionCache(graphName);
- RangeMap<Long, Integer> range = graph.getRange();
- List<Partition> ps = pc.getPartitionsList();
- HashMap<Integer, Partition> gps = new HashMap<>(ps.size(), 1);
- for (Partition p : ps) {
- gps.put(p.getId(), p);
- range.put(Range.closedOpen(p.getStartKey(), p.getEndKey()), p.getId());
- }
- graph.setPartitions(gps);
- graph.getInitialized().set(true);
- }
- }
- }
- return graph;
- }
-
- private void initCache() throws PDException {
- if (!initialized.get()) {
- synchronized (this) {
- if (!initialized.get()) {
- CacheResponse cache = client.getClientCache();
- List<ShardGroup> shardGroups = cache.getShardsList();
- for (ShardGroup s : shardGroups) {
- this.groups.put(s.getId(), new KVPair<>(s, getLeader(s.getId())));
- }
- List<Metapb.Store> stores = cache.getStoresList();
- for (Metapb.Store store : stores) {
- this.stores.put(store.getId(), store);
- }
- List<Metapb.Graph> graphs = cache.getGraphsList();
- for (Metapb.Graph g : graphs) {
- GraphCache c = new GraphCache(g);
- caches.put(g.getGraphName(), c);
- }
- initialized.set(true);
- }
- }
- }
- }
-
- /**
- * 返回key所在的分区信息
- *
- * @param key
- * @return
- */
- public KVPair<Partition, Shard> getPartitionByKey(String graphName, byte[] key) {
- int code = PartitionUtils.calcHashcode(key);
- return getPartitionByCode(graphName, code);
- }
-
- public boolean update(String graphName, int partId, Partition partition) {
- GraphCache graph = getGraphCache(graphName);
- try {
- Partition p = graph.getPartition(partId);
- if (p != null && p.equals(partition)) {
- return false;
- }
- RangeMap<Long, Integer> range = graph.getRange();
- graph.addPartition(partId, partition);
- if (p != null) {
- // old [1-3) 被 [2-3)覆盖了。当 [1-3) 变成[1-2) 不应该删除原先的[1-3)
- // 当确认老的 start, end 都是自己的时候,才可以删除老的. (即还没覆盖)
- if (Objects.equals(partition.getId(), range.get(partition.getStartKey())) &&
- Objects.equals(partition.getId(), range.get(partition.getEndKey() - 1))) {
- range.remove(range.getEntry(partition.getStartKey()).getKey());
- }
- }
- range.put(Range.closedOpen(partition.getStartKey(), partition.getEndKey()), partId);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- return true;
- }
-
- public void removePartition(String graphName, int partId) {
- GraphCache graph = getGraphCache(graphName);
- Partition p = graph.removePartition(partId);
- if (p != null) {
- RangeMap<Long, Integer> range = graph.getRange();
- if (Objects.equals(p.getId(), range.get(p.getStartKey())) &&
- Objects.equals(p.getId(), range.get(p.getEndKey() - 1))) {
- range.remove(range.getEntry(p.getStartKey()).getKey());
- }
- }
- }
-
- /**
- * remove all partitions
- */
- public void removePartitions() {
- for (Entry<String, GraphCache> entry : caches.entrySet()) {
- removePartitions(entry.getValue());
- }
- }
-
- private void removePartitions(GraphCache graph) {
- graph.getState().clear();
- graph.getRange().clear();
- }
-
- /**
- * remove partition cache of graphName
- *
- * @param graphName
- */
- public void removeAll(String graphName) {
- GraphCache graph = caches.get(graphName);
- if (graph != null) {
- removePartitions(graph);
- }
- }
-
- public boolean updateShardGroup(ShardGroup shardGroup) {
- KVPair<ShardGroup, Shard> old = groups.get(shardGroup.getId());
- Shard leader = getLeader(shardGroup);
- if (old != null) {
- old.setKey(shardGroup);
- old.setValue(leader);
- return false;
- }
- groups.put(shardGroup.getId(), new KVPair<>(shardGroup, leader));
- return true;
- }
-
- public void deleteShardGroup(int shardGroupId) {
- groups.remove(shardGroupId);
- }
-
- public ShardGroup getShardGroup(int groupId) {
- KVPair<ShardGroup, Shard> pair = groups.get(groupId);
- if (pair != null) {
- return pair.getKey();
- }
- return null;
- }
-
- public boolean addStore(Long storeId, Metapb.Store store) {
- Metapb.Store oldStore = stores.get(storeId);
- if (oldStore != null && oldStore.equals(store)) {
- return false;
- }
- stores.put(storeId, store);
- return true;
- }
-
- public Metapb.Store getStoreById(Long storeId) {
- return stores.get(storeId);
- }
-
- public void removeStore(Long storeId) {
- stores.remove(storeId);
- }
-
- public void reset() {
- groups = new ConcurrentHashMap<>();
- stores = new ConcurrentHashMap<>();
- caches = new ConcurrentHashMap<>();
- }
-
- public Shard getLeader(int partitionId) {
- KVPair<ShardGroup, Shard> pair = groups.get(partitionId);
- if (pair != null) {
- if (pair.getValue() != null) {
- return pair.getValue();
- }
- for (Shard shard : pair.getKey().getShardsList()) {
- if (shard.getRole() == Metapb.ShardRole.Leader) {
- pair.setValue(shard);
- return shard;
- }
- }
- }
-
- return null;
- }
-
- public Shard getLeader(ShardGroup shardGroup) {
- if (shardGroup != null) {
- for (Shard shard : shardGroup.getShardsList()) {
- if (shard.getRole() == Metapb.ShardRole.Leader) {
- return shard;
- }
- }
- }
-
- return null;
- }
-
- public void updateLeader(int partitionId, Shard leader) {
- KVPair<ShardGroup, Shard> pair = groups.get(partitionId);
- if (pair != null && leader != null) {
- Shard l = getLeader(partitionId);
- if (l == null || leader.getStoreId() != l.getStoreId()) {
- ShardGroup shardGroup = pair.getKey();
- ShardGroup.Builder builder = ShardGroup.newBuilder(shardGroup).clearShards();
- for (var shard : shardGroup.getShardsList()) {
- builder.addShards(
- Shard.newBuilder()
- .setStoreId(shard.getStoreId())
- .setRole(shard.getStoreId() == leader.getStoreId() ?
- Metapb.ShardRole.Leader : Metapb.ShardRole.Follower)
- .build()
- );
- }
- pair.setKey(builder.build());
- pair.setValue(leader);
- }
- }
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/Discoverable.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/Discoverable.java
deleted file mode 100644
index 4222bfbe5..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/Discoverable.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import org.apache.hugegraph.pd.grpc.discovery.NodeInfos;
-import org.apache.hugegraph.pd.grpc.discovery.Query;
-
-public interface Discoverable {
-
- NodeInfos getNodeInfos(Query query);
-
- void scheduleTask();
-
- void cancelTask();
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClient.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClient.java
deleted file mode 100644
index aa80ec606..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClient.java
+++ /dev/null
@@ -1,221 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.io.Closeable;
-import java.util.LinkedList;
-import java.util.Timer;
-import java.util.TimerTask;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-import java.util.function.Consumer;
-import java.util.function.Function;
-
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.grpc.discovery.DiscoveryServiceGrpc;
-import org.apache.hugegraph.pd.grpc.discovery.NodeInfo;
-import org.apache.hugegraph.pd.grpc.discovery.NodeInfos;
-import org.apache.hugegraph.pd.grpc.discovery.Query;
-import org.apache.hugegraph.pd.grpc.discovery.RegisterInfo;
-
-import io.grpc.ManagedChannel;
-import io.grpc.ManagedChannelBuilder;
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-public abstract class DiscoveryClient implements Closeable, Discoverable {
-
- private final Timer timer = new Timer("serverHeartbeat", true);
- private final AtomicBoolean requireResetStub = new AtomicBoolean(false);
- protected int period; //心跳周期
- LinkedList<String> pdAddresses = new LinkedList<>();
- ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
- private volatile int currentIndex; // 当前在用pd地址位置
- private int maxTime = 6;
- private ManagedChannel channel = null;
- private DiscoveryServiceGrpc.DiscoveryServiceBlockingStub registerStub;
- private DiscoveryServiceGrpc.DiscoveryServiceBlockingStub blockingStub;
-
- public DiscoveryClient(String centerAddress, int delay) {
- String[] addresses = centerAddress.split(",");
- for (int i = 0; i < addresses.length; i++) {
- String singleAddress = addresses[i];
- if (singleAddress == null || singleAddress.length() <= 0) {
- continue;
- }
- pdAddresses.add(addresses[i]);
- }
- this.period = delay;
- if (maxTime < addresses.length) {
- maxTime = addresses.length;
- }
- }
-
- private <V, R> R tryWithTimes(Function<V, R> function, V v) {
- R r;
- Exception ex = null;
- for (int i = 0; i < maxTime; i++) {
- try {
- r = function.apply(v);
- return r;
- } catch (Exception e) {
- requireResetStub.set(true);
- resetStub();
- ex = e;
- }
- }
- if (ex != null) {
- log.error("Try discovery method with error: {}", ex.getMessage());
- }
- return null;
- }
-
- /***
- * 按照pd列表重置stub
- */
- private void resetStub() {
- String errLog = null;
- for (int i = currentIndex + 1; i <= pdAddresses.size() + currentIndex; i++) {
- currentIndex = i % pdAddresses.size();
- String singleAddress = pdAddresses.get(currentIndex);
- try {
- if (requireResetStub.get()) {
- resetChannel(singleAddress);
- }
- errLog = null;
- break;
- } catch (Exception e) {
- requireResetStub.set(true);
- if (errLog == null) {
- errLog = e.getMessage();
- }
- continue;
- }
- }
- if (errLog != null) {
- log.error(errLog);
- }
- }
-
- /***
- * 按照某个pd的地址重置channel和stub
- * @param singleAddress
- * @throws PDException
- */
- private void resetChannel(String singleAddress) throws PDException {
-
- readWriteLock.writeLock().lock();
- try {
- if (requireResetStub.get()) {
- while (channel != null && !channel.shutdownNow().awaitTermination(
- 100, TimeUnit.MILLISECONDS)) {
- continue;
- }
- channel = ManagedChannelBuilder.forTarget(
- singleAddress).usePlaintext().build();
- this.registerStub = DiscoveryServiceGrpc.newBlockingStub(
- channel);
- this.blockingStub = DiscoveryServiceGrpc.newBlockingStub(
- channel);
- requireResetStub.set(false);
- }
- } catch (Exception e) {
- throw new PDException(-1, String.format(
- "Reset channel with error : %s.", e.getMessage()));
- } finally {
- readWriteLock.writeLock().unlock();
- }
- }
-
- /***
- * 获取注册节点信息
- * @param query
- * @return
- */
- @Override
- public NodeInfos getNodeInfos(Query query) {
- return tryWithTimes((q) -> {
- this.readWriteLock.readLock().lock();
- NodeInfos nodes;
- try {
- nodes = this.blockingStub.getNodes(q);
- } catch (Exception e) {
- throw e;
- } finally {
- this.readWriteLock.readLock().unlock();
- }
- return nodes;
- }, query);
- }
-
- /***
- * 启动心跳任务
- */
- @Override
- public void scheduleTask() {
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- NodeInfo nodeInfo = getRegisterNode();
- tryWithTimes((t) -> {
- RegisterInfo register;
- readWriteLock.readLock().lock();
- try {
- register = registerStub.register(t);
- log.debug("Discovery Client work done.");
- Consumer<RegisterInfo> consumer = getRegisterConsumer();
- if (consumer != null) {
- consumer.accept(register);
- }
- } catch (Exception e) {
- throw e;
- } finally {
- readWriteLock.readLock().unlock();
- }
- return register;
- }, nodeInfo);
- }
- }, 0, period);
- }
-
- abstract NodeInfo getRegisterNode();
-
- abstract Consumer<RegisterInfo> getRegisterConsumer();
-
- @Override
- public void cancelTask() {
- this.timer.cancel();
- }
-
- @Override
- public void close() {
- this.timer.cancel();
- readWriteLock.writeLock().lock();
- try {
- while (channel != null && !channel.shutdownNow().awaitTermination(
- 100, TimeUnit.MILLISECONDS)) {
- continue;
- }
- } catch (Exception e) {
- log.info("Close channel with error : {}.", e);
- } finally {
- readWriteLock.writeLock().unlock();
- }
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClientImpl.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClientImpl.java
deleted file mode 100644
index 77ec9a36b..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClientImpl.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.util.Map;
-import java.util.function.Consumer;
-
-import org.apache.hugegraph.pd.grpc.discovery.NodeInfo;
-import org.apache.hugegraph.pd.grpc.discovery.RegisterType;
-
-public class DiscoveryClientImpl extends DiscoveryClient {
-
- private final String id;
- private final RegisterType type; // 心跳类型,备用
- private final String version;
- private final String appName;
- private final int times; // 心跳过期次数,备用
- private final String address;
- private final Map labels;
- private final Consumer registerConsumer;
-
-
- private DiscoveryClientImpl(Builder builder) {
- super(builder.centerAddress, builder.delay);
- period = builder.delay;
- id = builder.id;
- type = builder.type;
- version = builder.version;
- appName = builder.appName;
- times = builder.times;
- address = builder.address;
- labels = builder.labels;
- registerConsumer = builder.registerConsumer;
- }
-
- public static Builder newBuilder() {
- return new Builder();
- }
-
-
- @Override
- NodeInfo getRegisterNode() {
- return NodeInfo.newBuilder().setAddress(this.address)
- .setVersion(this.version)
- .setAppName(this.appName).setInterval(this.period)
- .setId(this.id).putAllLabels(labels).build();
- }
-
- @Override
- Consumer getRegisterConsumer() {
- return registerConsumer;
- }
-
-
- public static final class Builder {
-
- private int delay;
- private String centerAddress;
- private String id;
- private RegisterType type;
- private String address;
- private Map labels;
- private String version;
- private String appName;
- private int times;
- private Consumer registerConsumer;
-
- private Builder() {
- }
-
- public Builder setDelay(int val) {
- delay = val;
- return this;
- }
-
- public Builder setCenterAddress(String val) {
- centerAddress = val;
- return this;
- }
-
- public Builder setId(String val) {
- id = val;
- return this;
- }
-
- public Builder setType(RegisterType val) {
- type = val;
- return this;
- }
-
- public Builder setAddress(String val) {
- address = val;
- return this;
- }
-
- public Builder setLabels(Map val) {
- labels = val;
- return this;
- }
-
- public Builder setVersion(String val) {
- version = val;
- return this;
- }
-
- public Builder setAppName(String val) {
- appName = val;
- return this;
- }
-
- public Builder setTimes(int val) {
- times = val;
- return this;
- }
-
- public Builder setRegisterConsumer(Consumer registerConsumer) {
- this.registerConsumer = registerConsumer;
- return this;
- }
-
- public DiscoveryClientImpl build() {
- return new DiscoveryClientImpl(this);
- }
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/KvClient.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/KvClient.java
deleted file mode 100644
index 27975ca57..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/KvClient.java
+++ /dev/null
@@ -1,352 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.io.Closeable;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.BiConsumer;
-import java.util.function.Consumer;
-
-import org.apache.hugegraph.pd.client.AbstractClient;
-import org.apache.hugegraph.pd.client.PDConfig;
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.grpc.kv.K;
-import org.apache.hugegraph.pd.grpc.kv.KResponse;
-import org.apache.hugegraph.pd.grpc.kv.Kv;
-import org.apache.hugegraph.pd.grpc.kv.KvResponse;
-import org.apache.hugegraph.pd.grpc.kv.KvServiceGrpc;
-import org.apache.hugegraph.pd.grpc.kv.LockRequest;
-import org.apache.hugegraph.pd.grpc.kv.LockResponse;
-import org.apache.hugegraph.pd.grpc.kv.ScanPrefixResponse;
-import org.apache.hugegraph.pd.grpc.kv.TTLRequest;
-import org.apache.hugegraph.pd.grpc.kv.TTLResponse;
-import org.apache.hugegraph.pd.grpc.kv.WatchEvent;
-import org.apache.hugegraph.pd.grpc.kv.WatchKv;
-import org.apache.hugegraph.pd.grpc.kv.WatchRequest;
-import org.apache.hugegraph.pd.grpc.kv.WatchResponse;
-import org.apache.hugegraph.pd.grpc.kv.WatchType;
-
-import io.grpc.stub.AbstractBlockingStub;
-import io.grpc.stub.AbstractStub;
-import io.grpc.stub.StreamObserver;
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-public class KvClient<T extends WatchResponse> extends AbstractClient implements Closeable {
-
- private final AtomicLong clientId = new AtomicLong(0);
- private final Semaphore semaphore = new Semaphore(1);
- private final ConcurrentHashMap<Long, StreamObserver> observers = new ConcurrentHashMap<>();
-
- public KvClient(PDConfig pdConfig) {
- super(pdConfig);
- }
-
- @Override
- protected AbstractStub createStub() {
- return KvServiceGrpc.newStub(channel);
- }
-
- @Override
- protected AbstractBlockingStub createBlockingStub() {
- return KvServiceGrpc.newBlockingStub(channel);
- }
-
- public KvResponse put(String key, String value) throws PDException {
- Kv kv = Kv.newBuilder().setKey(key).setValue(value).build();
- KvResponse response = blockingUnaryCall(KvServiceGrpc.getPutMethod(), kv);
- handleErrors(response.getHeader());
- return response;
- }
-
-
- public KResponse get(String key) throws PDException {
- K k = K.newBuilder().setKey(key).build();
- KResponse response = blockingUnaryCall(KvServiceGrpc.getGetMethod(), k);
- handleErrors(response.getHeader());
- return response;
- }
-
-
- public KvResponse delete(String key) throws PDException {
- K k = K.newBuilder().setKey(key).build();
- KvResponse response = blockingUnaryCall(KvServiceGrpc.getDeleteMethod(), k);
- handleErrors(response.getHeader());
- return response;
- }
-
-
- public KvResponse deletePrefix(String prefix) throws PDException {
- K k = K.newBuilder().setKey(prefix).build();
- KvResponse response = blockingUnaryCall(KvServiceGrpc.getDeletePrefixMethod(), k);
- handleErrors(response.getHeader());
- return response;
- }
-
-
- public ScanPrefixResponse scanPrefix(String prefix) throws PDException {
- K k = K.newBuilder().setKey(prefix).build();
- ScanPrefixResponse response = blockingUnaryCall(KvServiceGrpc.getScanPrefixMethod(), k);
- handleErrors(response.getHeader());
- return response;
- }
-
- public TTLResponse keepTTLAlive(String key) throws PDException {
- TTLRequest request = TTLRequest.newBuilder().setKey(key).build();
- TTLResponse response = blockingUnaryCall(KvServiceGrpc.getKeepTTLAliveMethod(), request);
- handleErrors(response.getHeader());
- return response;
- }
-
- public TTLResponse putTTL(String key, String value, long ttl) throws PDException {
- TTLRequest request =
- TTLRequest.newBuilder().setKey(key).setValue(value).setTtl(ttl).build();
- TTLResponse response = blockingUnaryCall(KvServiceGrpc.getPutTTLMethod(), request);
- handleErrors(response.getHeader());
- return response;
- }
-
- private void onEvent(WatchResponse value, Consumer<T> consumer) {
- log.info("receive message for {},event Count:{}", value, value.getEventsCount());
- clientId.compareAndSet(0L, value.getClientId());
- if (value.getEventsCount() != 0) {
- consumer.accept((T) value);
- }
- }
-
- private StreamObserver<WatchResponse> getObserver(String key, Consumer<T> consumer,
- BiConsumer<String, Consumer> listenWrapper,
- long client) {
- StreamObserver<WatchResponse> observer;
- if ((observer = observers.get(client)) == null) {
- synchronized (this) {
- if ((observer = observers.get(client)) == null) {
- observer = getObserver(key, consumer, listenWrapper);
- observers.put(client, observer);
- }
- }
- }
- return observer;
- }
-
- private StreamObserver<WatchResponse> getObserver(String key, Consumer<T> consumer,
- BiConsumer<String, Consumer> listenWrapper) {
- return new StreamObserver<WatchResponse>() {
- @Override
- public void onNext(WatchResponse value) {
- switch (value.getState()) {
- case Starting:
- boolean b = clientId.compareAndSet(0, value.getClientId());
- if (b) {
- observers.put(value.getClientId(), this);
- log.info("set watch client id to :{}", value.getClientId());
- }
- semaphore.release();
- break;
- case Started:
- onEvent(value, consumer);
- break;
- case Leader_Changed:
- listenWrapper.accept(key, consumer);
- break;
- case Alive:
- // only for check client is alive, do nothing
- break;
- default:
- break;
- }
- }
-
- @Override
- public void onError(Throwable t) {
- listenWrapper.accept(key, consumer);
- }
-
-
- @Override
- public void onCompleted() {
-
- }
- };
- }
-
- public void listen(String key, Consumer<T> consumer) throws PDException {
- long value = clientId.get();
- StreamObserver<WatchResponse> observer = getObserver(key, consumer, listenWrapper, value);
- acquire();
- WatchRequest k = WatchRequest.newBuilder().setClientId(value).setKey(key).build();
- streamingCall(KvServiceGrpc.getWatchMethod(), k, observer, 1);
- }
-
- public void listenPrefix(String prefix, Consumer<T> consumer) throws PDException {
- long value = clientId.get();
- StreamObserver<WatchResponse> observer =
- getObserver(prefix, consumer, prefixListenWrapper, value);
- acquire();
- WatchRequest k =
- WatchRequest.newBuilder().setClientId(clientId.get()).setKey(prefix).build();
- streamingCall(KvServiceGrpc.getWatchPrefixMethod(), k, observer, 1);
- }
-
- private void acquire() {
- if (clientId.get() == 0L) {
- try {
- semaphore.acquire();
- if (clientId.get() != 0L) {
- semaphore.release();
- }
- } catch (InterruptedException e) {
- log.error("get semaphore with error:", e);
- }
- }
- }
-
- public List<String> getWatchList(T response) {
- List<String> values = new LinkedList<>();
- List<WatchEvent> eventsList = response.getEventsList();
- for (WatchEvent event : eventsList) {
- if (event.getType() != WatchType.Put) {
- return null;
- }
- String value = event.getCurrent().getValue();
- values.add(value);
- }
- return values;
- }
-
- public Map<String, String> getWatchMap(T response) {
- Map<String, String> values = new HashMap<>();
- List<WatchEvent> eventsList = response.getEventsList();
- for (WatchEvent event : eventsList) {
- if (event.getType() != WatchType.Put) {
- return null;
- }
- WatchKv current = event.getCurrent();
- String key = current.getKey();
- String value = current.getValue();
- values.put(key, value);
- }
- return values;
- }
-
- public LockResponse lock(String key, long ttl) throws PDException {
- acquire();
- LockResponse response;
- try {
- LockRequest k =
- LockRequest.newBuilder().setKey(key).setClientId(clientId.get()).setTtl(ttl)
- .build();
- response = blockingUnaryCall(KvServiceGrpc.getLockMethod(), k);
- handleErrors(response.getHeader());
- if (clientId.compareAndSet(0L, response.getClientId())) {
- semaphore.release();
- }
- } catch (Exception e) {
- if (clientId.get() == 0L) {
- semaphore.release();
- }
- throw e;
- }
- return response;
- } BiConsumer<String, Consumer> listenWrapper = (key, consumer) -> {
- try {
- listen(key, consumer);
- } catch (PDException e) {
- try {
- log.warn("start listen with warning:", e);
- Thread.sleep(1000);
- } catch (InterruptedException ex) {
- }
- }
- };
-
- public LockResponse lockWithoutReentrant(String key, long ttl) throws PDException {
- acquire();
- LockResponse response;
- try {
- LockRequest k =
- LockRequest.newBuilder().setKey(key).setClientId(clientId.get()).setTtl(ttl)
- .build();
- response = blockingUnaryCall(KvServiceGrpc.getLockWithoutReentrantMethod(), k);
- handleErrors(response.getHeader());
- if (clientId.compareAndSet(0L, response.getClientId())) {
- semaphore.release();
- }
- } catch (Exception e) {
- if (clientId.get() == 0L) {
- semaphore.release();
- }
- throw e;
- }
- return response;
- }
-
- public LockResponse isLocked(String key) throws PDException {
- LockRequest k = LockRequest.newBuilder().setKey(key).setClientId(clientId.get()).build();
- LockResponse response = blockingUnaryCall(KvServiceGrpc.getIsLockedMethod(), k);
- handleErrors(response.getHeader());
- return response;
- }
-
- public LockResponse unlock(String key) throws PDException {
- assert clientId.get() != 0;
- LockRequest k = LockRequest.newBuilder().setKey(key).setClientId(clientId.get()).build();
- LockResponse response = blockingUnaryCall(KvServiceGrpc.getUnlockMethod(), k);
- handleErrors(response.getHeader());
- clientId.compareAndSet(0L, response.getClientId());
- assert clientId.get() == response.getClientId();
- return response;
- }
-
- public LockResponse keepAlive(String key) throws PDException {
- assert clientId.get() != 0;
- LockRequest k = LockRequest.newBuilder().setKey(key).setClientId(clientId.get()).build();
- LockResponse response = blockingUnaryCall(KvServiceGrpc.getKeepAliveMethod(), k);
- handleErrors(response.getHeader());
- clientId.compareAndSet(0L, response.getClientId());
- assert clientId.get() == response.getClientId();
- return response;
- }
-
- @Override
- public void close() {
- super.close();
- }
-
-
-
- BiConsumer<String, Consumer> prefixListenWrapper = (key, consumer) -> {
- try {
- listenPrefix(key, consumer);
- } catch (PDException e) {
- try {
- log.warn("start listenPrefix with warning:", e);
- Thread.sleep(1000);
- } catch (InterruptedException ex) {
- }
- }
- };
-
-
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/LicenseClient.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/LicenseClient.java
deleted file mode 100644
index b86d6b394..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/LicenseClient.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import org.apache.hugegraph.pd.common.KVPair;
-import org.apache.hugegraph.pd.grpc.PDGrpc;
-import org.apache.hugegraph.pd.grpc.Pdpb;
-
-import com.google.protobuf.ByteString;
-
-import io.grpc.stub.AbstractBlockingStub;
-import io.grpc.stub.AbstractStub;
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-public class LicenseClient extends AbstractClient {
-
- public LicenseClient(PDConfig config) {
- super(config);
- }
-
- @Override
- protected AbstractStub createStub() {
- return PDGrpc.newStub(channel);
- }
-
- @Override
- protected AbstractBlockingStub createBlockingStub() {
- return PDGrpc.newBlockingStub(channel);
- }
-
- public Pdpb.PutLicenseResponse putLicense(byte[] content) {
- Pdpb.PutLicenseRequest request = Pdpb.PutLicenseRequest.newBuilder()
- .setContent(
- ByteString.copyFrom(content))
- .build();
- try {
- KVPair<Boolean, Pdpb.PutLicenseResponse> pair = concurrentBlockingUnaryCall(
- PDGrpc.getPutLicenseMethod(), request,
- (rs) -> rs.getHeader().getError().getType().equals(Pdpb.ErrorType.OK));
- if (pair.getKey()) {
- Pdpb.PutLicenseResponse.Builder builder = Pdpb.PutLicenseResponse.newBuilder();
- builder.setHeader(okHeader);
- return builder.build();
- } else {
- return pair.getValue();
- }
- } catch (Exception e) {
- e.printStackTrace();
- log.debug("put license with error:{} ", e);
- Pdpb.ResponseHeader rh =
- newErrorHeader(Pdpb.ErrorType.LICENSE_ERROR_VALUE, e.getMessage());
- return Pdpb.PutLicenseResponse.newBuilder().setHeader(rh).build();
- }
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDClient.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDClient.java
deleted file mode 100644
index ae57e622c..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDClient.java
+++ /dev/null
@@ -1,1347 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import static org.apache.hugegraph.pd.watch.NodeEvent.EventType.NODE_PD_LEADER_CHANGE;
-
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Objects;
-import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.hugegraph.pd.common.KVPair;
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.common.PartitionUtils;
-import org.apache.hugegraph.pd.grpc.MetaTask;
-import org.apache.hugegraph.pd.grpc.Metapb;
-import org.apache.hugegraph.pd.grpc.Metapb.ShardGroup;
-import org.apache.hugegraph.pd.grpc.PDGrpc;
-import org.apache.hugegraph.pd.grpc.Pdpb;
-import org.apache.hugegraph.pd.grpc.Pdpb.CachePartitionResponse;
-import org.apache.hugegraph.pd.grpc.Pdpb.CacheResponse;
-import org.apache.hugegraph.pd.grpc.Pdpb.GetGraphRequest;
-import org.apache.hugegraph.pd.grpc.Pdpb.GetPartitionByCodeRequest;
-import org.apache.hugegraph.pd.grpc.Pdpb.GetPartitionRequest;
-import org.apache.hugegraph.pd.grpc.Pdpb.GetPartitionResponse;
-import org.apache.hugegraph.pd.grpc.watch.WatchResponse;
-import org.apache.hugegraph.pd.watch.NodeEvent;
-import org.apache.hugegraph.pd.watch.PartitionEvent;
-
-import com.google.protobuf.ByteString;
-
-import io.grpc.ManagedChannel;
-import io.grpc.MethodDescriptor;
-import io.grpc.StatusRuntimeException;
-import io.grpc.stub.AbstractBlockingStub;
-import lombok.extern.slf4j.Slf4j;
-
-/**
- * PD客户端实现类
- *
- * @author yanjinbing
- */
-@Slf4j
-public class PDClient {
- private final PDConfig config;
- private final Pdpb.RequestHeader header;
- private final ClientCache cache;
- private final StubProxy stubProxy;
- private final List<PDEventListener> eventListeners;
- private PDWatch.Watcher partitionWatcher;
- private PDWatch.Watcher storeWatcher;
- private PDWatch.Watcher graphWatcher;
- private PDWatch.Watcher shardGroupWatcher;
- private PDWatch pdWatch;
-
- private PDClient(PDConfig config) {
- this.config = config;
- this.header = Pdpb.RequestHeader.getDefaultInstance();
- this.stubProxy = new StubProxy(config.getServerHost().split(","));
- this.eventListeners = new CopyOnWriteArrayList<>();
- this.cache = new ClientCache(this);
- }
-
- /**
- * 创建PDClient对象,并初始化stub
- *
- * @param config
- * @return
- */
- public static PDClient create(PDConfig config) {
- return new PDClient(config);
- }
-
- private synchronized void newBlockingStub() throws PDException {
- if (stubProxy.get() != null) {
- return;
- }
-
- String host = newLeaderStub();
- if (host.isEmpty()) {
- throw new PDException(Pdpb.ErrorType.PD_UNREACHABLE_VALUE,
- "PD unreachable, pd.peers=" + config.getServerHost());
- }
-
- log.info("PDClient enable cache, init PDWatch object");
- connectPdWatch(host);
- }
-
- public void connectPdWatch(String leader) {
-
- if (pdWatch != null && Objects.equals(pdWatch.getCurrentHost(), leader) &&
- pdWatch.checkChannel()) {
- return;
- }
-
- log.info("PDWatch client connect host:{}", leader);
- pdWatch = new PDWatchImpl(leader);
-
- partitionWatcher = pdWatch.watchPartition(new PDWatch.Listener<>() {
- @Override
- public void onNext(PartitionEvent response) {
- // log.info("PDClient receive partition event {}-{} {}",
- // response.getGraph(), response.getPartitionId(), response.getChangeType());
- invalidPartitionCache(response.getGraph(), response.getPartitionId());
-
- if (response.getChangeType() == PartitionEvent.ChangeType.DEL) {
- cache.removeAll(response.getGraph());
- }
-
- eventListeners.forEach(listener -> {
- listener.onPartitionChanged(response);
- });
- }
-
- @Override
- public void onError(Throwable throwable) {
- log.error("watchPartition exception {}", throwable.getMessage());
- closeStub(false);
- }
- });
-
- storeWatcher = pdWatch.watchNode(new PDWatch.Listener<>() {
- @Override
- public void onNext(NodeEvent response) {
- log.info("PDClient receive store event {} {}",
- response.getEventType(), Long.toHexString(response.getNodeId()));
-
- if (response.getEventType() == NODE_PD_LEADER_CHANGE) {
- // pd raft change
- var leaderIp = response.getGraph();
- log.info("watchNode: pd leader changed to {}, current watch:{}",
- leaderIp, pdWatch.getCurrentHost());
- closeStub(!Objects.equals(pdWatch.getCurrentHost(), leaderIp));
- connectPdWatch(leaderIp);
- }
-
- invalidStoreCache(response.getNodeId());
- eventListeners.forEach(listener -> {
- listener.onStoreChanged(response);
- });
- }
-
- @Override
- public void onError(Throwable throwable) {
- log.error("watchNode exception {}", throwable.getMessage());
- closeStub(false);
- }
-
- });
-
- graphWatcher = pdWatch.watchGraph(new PDWatch.Listener<>() {
- @Override
- public void onNext(WatchResponse response) {
- eventListeners.forEach(listener -> {
- listener.onGraphChanged(response);
- });
- }
-
- @Override
- public void onError(Throwable throwable) {
- log.warn("graphWatcher exception {}", throwable.getMessage());
- }
- });
-
- shardGroupWatcher = pdWatch.watchShardGroup(new PDWatch.Listener<>() {
- @Override
- public void onNext(WatchResponse response) {
- var shardResponse = response.getShardGroupResponse();
- // log.info("PDClient receive shard group event: raft {}-{}", shardResponse
- // .getShardGroupId(),
- // shardResponse.getType());
- if (config.isEnableCache()) {
- switch (shardResponse.getType()) {
- case WATCH_CHANGE_TYPE_DEL:
- cache.deleteShardGroup(shardResponse.getShardGroupId());
- break;
- case WATCH_CHANGE_TYPE_ALTER:
- cache.updateShardGroup(
- response.getShardGroupResponse().getShardGroup());
- break;
- default:
- break;
- }
- }
- eventListeners.forEach(listener -> listener.onShardGroupChanged(response));
- }
-
- @Override
- public void onError(Throwable throwable) {
- log.warn("shardGroupWatcher exception {}", throwable.getMessage());
- }
- });
-
- }
-
- private synchronized void closeStub(boolean closeWatcher) {
- // TODO ManagedChannel 没有正常关闭
- stubProxy.set(null);
- cache.reset();
-
- if (closeWatcher) {
- if (partitionWatcher != null) {
- partitionWatcher.close();
- partitionWatcher = null;
- }
- if (storeWatcher != null) {
- storeWatcher.close();
- storeWatcher = null;
- }
- if (graphWatcher != null) {
- graphWatcher.close();
- graphWatcher = null;
- }
-
- if (shardGroupWatcher != null) {
- shardGroupWatcher.close();
- shardGroupWatcher = null;
- }
-
- pdWatch = null;
- }
- }
-
- private PDGrpc.PDBlockingStub getStub() throws PDException {
- if (stubProxy.get() == null) {
- newBlockingStub();
- }
- return stubProxy.get().withDeadlineAfter(config.getGrpcTimeOut(), TimeUnit.MILLISECONDS);
- }
-
- private PDGrpc.PDBlockingStub newStub() throws PDException {
- if (stubProxy.get() == null) {
- newBlockingStub();
- }
- return PDGrpc.newBlockingStub(stubProxy.get().getChannel())
- .withDeadlineAfter(config.getGrpcTimeOut(),
- TimeUnit.MILLISECONDS);
- }
-
- private String newLeaderStub() {
- String leaderHost = "";
- for (int i = 0; i < stubProxy.getHostCount(); i++) {
- String host = stubProxy.nextHost();
- ManagedChannel channel = Channels.getChannel(host);
-
- PDGrpc.PDBlockingStub stub = PDGrpc.newBlockingStub(channel)
- .withDeadlineAfter(config.getGrpcTimeOut(),
- TimeUnit.MILLISECONDS);
- try {
- var leaderIp = getLeaderIp(stub);
- if (!leaderIp.equalsIgnoreCase(host)) {
- leaderHost = leaderIp;
- stubProxy.set(PDGrpc.newBlockingStub(channel)
- .withDeadlineAfter(config.getGrpcTimeOut(),
- TimeUnit.MILLISECONDS));
- } else {
- stubProxy.set(stub);
- leaderHost = host;
- }
- stubProxy.setLeader(leaderIp);
-
- log.info("PDClient connect to host = {} success", leaderHost);
- break;
- } catch (Exception e) {
- log.error("PDClient connect to {} exception {}, {}", host, e.getMessage(),
- e.getCause() != null ? e.getCause().getMessage() : "");
- }
- }
- return leaderHost;
- }
-
- public String getLeaderIp() {
-
- return getLeaderIp(stubProxy.get());
- }
-
- private String getLeaderIp(PDGrpc.PDBlockingStub stub) {
- if (stub == null) {
- try {
- getStub();
- return stubProxy.getLeader();
- } catch (PDException e) {
- throw new RuntimeException(e);
- }
- }
-
- Pdpb.GetMembersRequest request = Pdpb.GetMembersRequest.newBuilder()
- .setHeader(header)
- .build();
- Metapb.Member leader = stub.getMembers(request).getLeader();
- return leader.getGrpcUrl();
- }
-
- /**
- * Store注册,返回storeID,初次注册会返回新ID
- *
- * @param store
- * @return
- */
- public long registerStore(Metapb.Store store) throws PDException {
- Pdpb.RegisterStoreRequest request = Pdpb.RegisterStoreRequest.newBuilder()
- .setHeader(header)
- .setStore(store).build();
-
- Pdpb.RegisterStoreResponse response =
- blockingUnaryCall(PDGrpc.getRegisterStoreMethod(), request);
- handleResponseError(response.getHeader());
- return response.getStoreId();
- }
-
- /**
- * 根据storeId返回Store对象
- *
- * @param storeId
- * @return
- * @throws PDException
- */
- public Metapb.Store getStore(long storeId) throws PDException {
- Metapb.Store store = cache.getStoreById(storeId);
- if (store == null) {
- Pdpb.GetStoreRequest request = Pdpb.GetStoreRequest.newBuilder()
- .setHeader(header)
- .setStoreId(storeId).build();
- Pdpb.GetStoreResponse response = getStub().getStore(request);
- handleResponseError(response.getHeader());
- store = response.getStore();
- if (config.isEnableCache()) {
- cache.addStore(storeId, store);
- }
- }
- return store;
- }
-
- /**
- * 更新Store信息,包括上下线等
- *
- * @param store
- * @return
- */
- public Metapb.Store updateStore(Metapb.Store store) throws PDException {
- Pdpb.SetStoreRequest request = Pdpb.SetStoreRequest.newBuilder()
- .setHeader(header)
- .setStore(store).build();
-
- Pdpb.SetStoreResponse response = getStub().setStore(request);
- handleResponseError(response.getHeader());
- store = response.getStore();
- if (config.isEnableCache()) {
- cache.addStore(store.getId(), store);
- }
- return store;
- }
-
- /**
- * 返回活跃的Store
- *
- * @param graphName
- * @return
- */
- public List<Metapb.Store> getActiveStores(String graphName) throws PDException {
- List<Metapb.Store> stores = new ArrayList<>();
- KVPair<Metapb.Partition, Metapb.Shard> ptShard = this.getPartitionByCode(graphName, 0);
- while (ptShard != null) {
- stores.add(this.getStore(ptShard.getValue().getStoreId()));
- if (ptShard.getKey().getEndKey() < PartitionUtils.MAX_VALUE) {
- ptShard = this.getPartitionByCode(graphName, ptShard.getKey().getEndKey());
- } else {
- ptShard = null;
- }
- }
- return stores;
- }
-
- public List<Metapb.Store> getActiveStores() throws PDException {
- Pdpb.GetAllStoresRequest request = Pdpb.GetAllStoresRequest.newBuilder()
- .setHeader(header)
- .setGraphName("")
- .setExcludeOfflineStores(true)
- .build();
- Pdpb.GetAllStoresResponse response = getStub().getAllStores(request);
- handleResponseError(response.getHeader());
- return response.getStoresList();
-
- }
-
- /**
- * 返回活跃的Store
- *
- * @param graphName
- * @return
- */
- public List<Metapb.Store> getAllStores(String graphName) throws PDException {
- Pdpb.GetAllStoresRequest request = Pdpb.GetAllStoresRequest.newBuilder()
- .setHeader(header)
- .setGraphName(graphName)
- .setExcludeOfflineStores(false)
- .build();
- Pdpb.GetAllStoresResponse response = getStub().getAllStores(request);
- handleResponseError(response.getHeader());
- return response.getStoresList();
-
- }
-
- /**
- * Store心跳,定期调用,保持在线状态
- *
- * @param stats
- * @throws PDException
- */
- public Metapb.ClusterStats storeHeartbeat(Metapb.StoreStats stats) throws PDException {
- Pdpb.StoreHeartbeatRequest request = Pdpb.StoreHeartbeatRequest.newBuilder()
- .setHeader(header)
- .setStats(stats).build();
- Pdpb.StoreHeartbeatResponse response = getStub().storeHeartbeat(request);
- handleResponseError(response.getHeader());
- return response.getClusterStats();
- }
-
- private KVPair<Metapb.Partition, Metapb.Shard> getKvPair(String graphName, byte[] key,
- KVPair<Metapb.Partition,
- Metapb.Shard> partShard) throws
- PDException {
- if (partShard == null) {
- GetPartitionRequest request = GetPartitionRequest.newBuilder()
- .setHeader(header)
- .setGraphName(graphName)
- .setKey(ByteString.copyFrom(key))
- .build();
- GetPartitionResponse response =
- blockingUnaryCall(PDGrpc.getGetPartitionMethod(), request);
- handleResponseError(response.getHeader());
- partShard = new KVPair<>(response.getPartition(), response.getLeader());
- cache.update(graphName, partShard.getKey().getId(), partShard.getKey());
- }
- return partShard;
- }
-
- /**
- * 查询Key所属分区信息
- *
- * @param graphName
- * @param key
- * @return
- * @throws PDException
- */
- public KVPair<Metapb.Partition, Metapb.Shard> getPartition(String graphName, byte[] key) throws
- PDException {
- // 先查cache,cache没有命中,在调用PD
- KVPair<Metapb.Partition, Metapb.Shard> partShard = cache.getPartitionByKey(graphName, key);
- partShard = getKvPair(graphName, key, partShard);
- return partShard;
- }
-
- public KVPair<Metapb.Partition, Metapb.Shard> getPartition(String graphName, byte[] key,
- int code) throws
- PDException {
- KVPair<Metapb.Partition, Metapb.Shard> partShard =
- cache.getPartitionByCode(graphName, code);
- partShard = getKvPair(graphName, key, partShard);
- return partShard;
- }
-
- /**
- * 根据hashcode查询所属分区信息
- *
- * @param graphName
- * @param hashCode
- * @return
- * @throws PDException
- */
- public KVPair<Metapb.Partition, Metapb.Shard> getPartitionByCode(String graphName,
- long hashCode)
- throws PDException {
- // 先查cache,cache没有命中,在调用PD
- KVPair<Metapb.Partition, Metapb.Shard> partShard =
- cache.getPartitionByCode(graphName, hashCode);
- if (partShard == null) {
- GetPartitionByCodeRequest request = GetPartitionByCodeRequest.newBuilder()
- .setHeader(header)
- .setGraphName(graphName)
- .setCode(hashCode).build();
- GetPartitionResponse response =
- blockingUnaryCall(PDGrpc.getGetPartitionByCodeMethod(), request);
- handleResponseError(response.getHeader());
- partShard = new KVPair<>(response.getPartition(), response.getLeader());
- cache.update(graphName, partShard.getKey().getId(), partShard.getKey());
- cache.updateShardGroup(getShardGroup(partShard.getKey().getId()));
- }
-
- if (partShard.getValue() == null) {
- ShardGroup shardGroup = getShardGroup(partShard.getKey().getId());
- if (shardGroup != null) {
- for (var shard : shardGroup.getShardsList()) {
- if (shard.getRole() == Metapb.ShardRole.Leader) {
- partShard.setValue(shard);
- }
- }
- } else {
- log.error("getPartitionByCode: get shard group failed, {}",
- partShard.getKey().getId());
- }
- }
- return partShard;
- }
-
- /**
- * 获取Key的哈希值
- */
- public int keyToCode(String graphName, byte[] key) {
- return PartitionUtils.calcHashcode(key);
- }
-
- /**
- * 根据分区id返回分区信息, RPC请求
- *
- * @param graphName
- * @param partId
- * @return
- * @throws PDException
- */
- public KVPair<Metapb.Partition, Metapb.Shard> getPartitionById(String graphName,
- int partId) throws PDException {
- KVPair<Metapb.Partition, Metapb.Shard> partShard =
- cache.getPartitionById(graphName, partId);
- if (partShard == null) {
- Pdpb.GetPartitionByIDRequest request = Pdpb.GetPartitionByIDRequest.newBuilder()
- .setHeader(header)
- .setGraphName(
- graphName)
- .setPartitionId(
- partId)
- .build();
- GetPartitionResponse response =
- blockingUnaryCall(PDGrpc.getGetPartitionByIDMethod(), request);
- handleResponseError(response.getHeader());
- partShard = new KVPair<>(response.getPartition(), response.getLeader());
- if (config.isEnableCache()) {
- cache.update(graphName, partShard.getKey().getId(), partShard.getKey());
- cache.updateShardGroup(getShardGroup(partShard.getKey().getId()));
- }
- }
- if (partShard.getValue() == null) {
- var shardGroup = getShardGroup(partShard.getKey().getId());
- if (shardGroup != null) {
- for (var shard : shardGroup.getShardsList()) {
- if (shard.getRole() == Metapb.ShardRole.Leader) {
- partShard.setValue(shard);
- }
- }
- } else {
- log.error("getPartitionById: get shard group failed, {}",
- partShard.getKey().getId());
- }
- }
- return partShard;
- }
-
- public ShardGroup getShardGroup(int partId) throws PDException {
- ShardGroup group = cache.getShardGroup(partId);
- if (group == null) {
- Pdpb.GetShardGroupRequest request = Pdpb.GetShardGroupRequest.newBuilder()
- .setHeader(header)
- .setGroupId(partId)
- .build();
- Pdpb.GetShardGroupResponse response =
- blockingUnaryCall(PDGrpc.getGetShardGroupMethod(), request);
- handleResponseError(response.getHeader());
- group = response.getShardGroup();
- if (config.isEnableCache()) {
- cache.updateShardGroup(group);
- }
- }
- return group;
- }
-
- public void updateShardGroup(ShardGroup shardGroup) throws PDException {
- Pdpb.UpdateShardGroupRequest request = Pdpb.UpdateShardGroupRequest.newBuilder()
- .setHeader(header)
- .setShardGroup(
- shardGroup)
- .build();
- Pdpb.UpdateShardGroupResponse response =
- blockingUnaryCall(PDGrpc.getUpdateShardGroupMethod(), request);
- handleResponseError(response.getHeader());
-
- if (config.isEnableCache()) {
- cache.updateShardGroup(shardGroup);
- }
- }
-
- /**
- * 返回startKey和endKey跨越的所有分区信息
- *
- * @param graphName
- * @param startKey
- * @param endKey
- * @return
- * @throws PDException
- */
- public List<KVPair<Metapb.Partition, Metapb.Shard>> scanPartitions(String graphName,
- byte[] startKey,
- byte[] endKey) throws
- PDException {
- List<KVPair<Metapb.Partition, Metapb.Shard>> partitions = new ArrayList<>();
- KVPair<Metapb.Partition, Metapb.Shard> startPartShard = getPartition(graphName, startKey);
- KVPair<Metapb.Partition, Metapb.Shard> endPartShard = getPartition(graphName, endKey);
- if (startPartShard == null || endPartShard == null) {
- return null;
- }
-
- partitions.add(startPartShard);
- while (startPartShard.getKey().getEndKey() < endPartShard.getKey().getEndKey()
- && startPartShard.getKey().getEndKey() <
- PartitionUtils.MAX_VALUE /*排除最后一个分区*/) {
- startPartShard = getPartitionByCode(graphName, startPartShard.getKey().getEndKey());
- partitions.add(startPartShard);
- }
- return partitions;
- }
-
- /**
- * 根据条件查询分区信息
- *
- * @return
- * @throws PDException
- */
- public List<Metapb.Partition> getPartitionsByStore(long storeId) throws PDException {
-
- Metapb.PartitionQuery query = Metapb.PartitionQuery.newBuilder()
- .setStoreId(storeId)
- .build();
- Pdpb.QueryPartitionsRequest request = Pdpb.QueryPartitionsRequest.newBuilder()
- .setQuery(query).build();
- Pdpb.QueryPartitionsResponse response =
- blockingUnaryCall(PDGrpc.getQueryPartitionsMethod(), request);
-
- handleResponseError(response.getHeader());
- return response.getPartitionsList();
- }
-
- /**
- * 查找指定store上的指定partitionId
- *
- * @return
- * @throws PDException
- */
- public List<Metapb.Partition> queryPartitions(long storeId, int partitionId) throws
- PDException {
-
- Metapb.PartitionQuery query = Metapb.PartitionQuery.newBuilder()
- .setStoreId(storeId)
- .setPartitionId(partitionId)
- .build();
- Pdpb.QueryPartitionsRequest request = Pdpb.QueryPartitionsRequest.newBuilder()
- .setQuery(query).build();
- Pdpb.QueryPartitionsResponse response =
- blockingUnaryCall(PDGrpc.getQueryPartitionsMethod(), request);
-
- handleResponseError(response.getHeader());
- return response.getPartitionsList();
- }
-
- public List<Metapb.Partition> getPartitions(long storeId, String graphName) throws PDException {
-
- Metapb.PartitionQuery query = Metapb.PartitionQuery.newBuilder()
- .setStoreId(storeId)
- .setGraphName(graphName).build();
- Pdpb.QueryPartitionsRequest request = Pdpb.QueryPartitionsRequest.newBuilder()
- .setQuery(query).build();
- Pdpb.QueryPartitionsResponse response =
- blockingUnaryCall(PDGrpc.getQueryPartitionsMethod(), request);
-
- handleResponseError(response.getHeader());
- return response.getPartitionsList();
-
- }
-
- public Metapb.Graph setGraph(Metapb.Graph graph) throws PDException {
- Pdpb.SetGraphRequest request = Pdpb.SetGraphRequest.newBuilder()
- .setGraph(graph)
- .build();
- Pdpb.SetGraphResponse response =
- blockingUnaryCall(PDGrpc.getSetGraphMethod(), request);
-
- handleResponseError(response.getHeader());
- return response.getGraph();
- }
-
- public Metapb.Graph getGraph(String graphName) throws PDException {
- GetGraphRequest request = GetGraphRequest.newBuilder()
- .setGraphName(graphName)
- .build();
- Pdpb.GetGraphResponse response =
- blockingUnaryCall(PDGrpc.getGetGraphMethod(), request);
-
- handleResponseError(response.getHeader());
- return response.getGraph();
- }
-
- public Metapb.Graph getGraphWithOutException(String graphName) throws
- PDException {
- GetGraphRequest request = GetGraphRequest.newBuilder()
- .setGraphName(
- graphName)
- .build();
- Pdpb.GetGraphResponse response = blockingUnaryCall(
- PDGrpc.getGetGraphMethod(), request);
- return response.getGraph();
- }
-
- public Metapb.Graph delGraph(String graphName) throws PDException {
- Pdpb.DelGraphRequest request = Pdpb.DelGraphRequest.newBuilder()
- .setGraphName(graphName)
- .build();
- Pdpb.DelGraphResponse response =
- blockingUnaryCall(PDGrpc.getDelGraphMethod(), request);
-
- handleResponseError(response.getHeader());
- return response.getGraph();
- }
-
- public List<Metapb.Partition> updatePartition(List<Metapb.Partition> partitions) throws
- PDException {
-
- Pdpb.UpdatePartitionRequest request = Pdpb.UpdatePartitionRequest.newBuilder()
- .addAllPartition(
- partitions)
- .build();
- Pdpb.UpdatePartitionResponse response =
- blockingUnaryCall(PDGrpc.getUpdatePartitionMethod(), request);
- handleResponseError(response.getHeader());
- invalidPartitionCache();
-
- return response.getPartitionList();
- }
-
- public Metapb.Partition delPartition(String graphName, int partitionId) throws PDException {
-
- Pdpb.DelPartitionRequest request = Pdpb.DelPartitionRequest.newBuilder()
- .setGraphName(graphName)
- .setPartitionId(partitionId)
- .build();
- Pdpb.DelPartitionResponse response =
- blockingUnaryCall(PDGrpc.getDelPartitionMethod(), request);
-
- handleResponseError(response.getHeader());
- invalidPartitionCache(graphName, partitionId);
- return response.getPartition();
- }
-
- /**
- * 删除分区缓存
- */
- public void invalidPartitionCache(String graphName, int partitionId) {
- // 检查是否存在缓存
- if (null != cache.getPartitionById(graphName, partitionId)) {
- cache.removePartition(graphName, partitionId);
- }
- }
-
- /**
- * 删除分区缓存
- */
- public void invalidPartitionCache() {
- // 检查是否存在缓存
- cache.removePartitions();
- }
-
- /**
- * 删除分区缓存
- */
- public void invalidStoreCache(long storeId) {
- cache.removeStore(storeId);
- }
-
- /**
- * Hugegraph server 调用,Leader发生改变,更新缓存
- */
- public void updatePartitionLeader(String graphName, int partId, long leaderStoreId) {
- KVPair<Metapb.Partition, Metapb.Shard> partShard = null;
- try {
- partShard = this.getPartitionById(graphName, partId);
-
- if (partShard != null && partShard.getValue().getStoreId() != leaderStoreId) {
- var shardGroup = this.getShardGroup(partId);
- Metapb.Shard shard = null;
- List<Metapb.Shard> shards = new ArrayList<>();
-
- for (Metapb.Shard s : shardGroup.getShardsList()) {
- if (s.getStoreId() == leaderStoreId) {
- shard = s;
- shards.add(Metapb.Shard.newBuilder(s)
- .setStoreId(s.getStoreId())
- .setRole(Metapb.ShardRole.Leader).build());
- } else {
- shards.add(Metapb.Shard.newBuilder(s)
- .setStoreId(s.getStoreId())
- .setRole(Metapb.ShardRole.Follower).build());
- }
- }
-
- if (config.isEnableCache()) {
- if (shard == null) {
- // 分区的shard中未找到leader,说明分区发生了迁移
- cache.removePartition(graphName, partId);
- }
- }
- }
- } catch (PDException e) {
- log.error("getPartitionException: {}", e.getMessage());
- }
- }
-
- /**
- * Hugegraph-store调用,更新缓存
- *
- * @param partition
- */
- public void updatePartitionCache(Metapb.Partition partition, Metapb.Shard leader) {
- if (config.isEnableCache()) {
- cache.update(partition.getGraphName(), partition.getId(), partition);
- cache.updateLeader(partition.getId(), leader);
- }
- }
-
- public Pdpb.GetIdResponse getIdByKey(String key, int delta) throws PDException {
- Pdpb.GetIdRequest request = Pdpb.GetIdRequest.newBuilder()
- .setHeader(header)
- .setKey(key)
- .setDelta(delta)
- .build();
- Pdpb.GetIdResponse response = blockingUnaryCall(PDGrpc.getGetIdMethod(), request);
- handleResponseError(response.getHeader());
- return response;
- }
-
- public Pdpb.ResetIdResponse resetIdByKey(String key) throws PDException {
- Pdpb.ResetIdRequest request = Pdpb.ResetIdRequest.newBuilder()
- .setHeader(header)
- .setKey(key)
- .build();
- Pdpb.ResetIdResponse response = blockingUnaryCall(PDGrpc.getResetIdMethod(), request);
- handleResponseError(response.getHeader());
- return response;
- }
-
- public Metapb.Member getLeader() throws PDException {
- Pdpb.GetMembersRequest request = Pdpb.GetMembersRequest.newBuilder()
- .setHeader(header)
- .build();
- Pdpb.GetMembersResponse response = blockingUnaryCall(PDGrpc.getGetMembersMethod(), request);
- handleResponseError(response.getHeader());
- return response.getLeader();
- }
-
- public Pdpb.GetMembersResponse getMembers() throws PDException {
- Pdpb.GetMembersRequest request = Pdpb.GetMembersRequest.newBuilder()
- .setHeader(header)
- .build();
- Pdpb.GetMembersResponse response = blockingUnaryCall(PDGrpc.getGetMembersMethod(), request);
- handleResponseError(response.getHeader());
- return response;
- }
-
- public Metapb.ClusterStats getClusterStats() throws PDException {
- Pdpb.GetClusterStatsRequest request = Pdpb.GetClusterStatsRequest.newBuilder()
- .setHeader(header)
- .build();
- Pdpb.GetClusterStatsResponse response =
- blockingUnaryCall(PDGrpc.getGetClusterStatsMethod(), request);
- handleResponseError(response.getHeader());
- return response.getCluster();
- }
-
- private <ReqT, RespT, StubT extends AbstractBlockingStub<StubT>> RespT
- blockingUnaryCall(MethodDescriptor<ReqT, RespT> method, ReqT req) throws PDException {
- return blockingUnaryCall(method, req, 1);
- }
-
- private <ReqT, RespT, StubT extends AbstractBlockingStub<StubT>> RespT
- blockingUnaryCall(MethodDescriptor<ReqT, RespT> method, ReqT req, int retry) throws
- PDException {
- io.grpc.stub.AbstractBlockingStub<StubT> stub = (AbstractBlockingStub<StubT>) getStub();
- try {
- RespT resp = io.grpc.stub.ClientCalls.blockingUnaryCall(stub.getChannel(), method,
- stub.getCallOptions(), req);
- return resp;
- } catch (Exception e) {
- log.error(method.getFullMethodName() + " exception, {}", e.getMessage());
- if (e instanceof StatusRuntimeException) {
- StatusRuntimeException se = (StatusRuntimeException) e;
- //se.getStatus() == Status.UNAVAILABLE &&
- if (retry < stubProxy.getHostCount()) {
- // 网络不通,关掉之前连接,换host重新连接
- closeStub(true);
- return blockingUnaryCall(method, req, ++retry);
- }
- }
- }
- return null;
- }
-
- private void handleResponseError(Pdpb.ResponseHeader header) throws
- PDException {
- var errorType = header.getError().getType();
- if (header.hasError() && errorType != Pdpb.ErrorType.OK) {
-
- throw new PDException(header.getError().getTypeValue(),
- String.format(
- "PD request error, error code = %d, msg = %s",
- header.getError().getTypeValue(),
- header.getError().getMessage()));
- }
- }
-
- public void addEventListener(PDEventListener listener) {
- eventListeners.add(listener);
- }
-
- public PDWatch getWatchClient() {
- return new PDWatchImpl(stubProxy.getHost());
- }
-
- /**
- * 返回Store状态信息
- */
- public List<Metapb.Store> getStoreStatus(boolean offlineExcluded) throws PDException {
- Pdpb.GetAllStoresRequest request = Pdpb.GetAllStoresRequest.newBuilder()
- .setHeader(header)
- .setExcludeOfflineStores(
- offlineExcluded)
- .build();
- Pdpb.GetAllStoresResponse response = getStub().getStoreStatus(request);
- handleResponseError(response.getHeader());
- List<Metapb.Store> stores = response.getStoresList();
- return stores;
- }
-
- public void setGraphSpace(String graphSpaceName, long storageLimit) throws PDException {
- Metapb.GraphSpace graphSpace = Metapb.GraphSpace.newBuilder().setName(graphSpaceName)
- .setStorageLimit(storageLimit)
- .setTimestamp(System.currentTimeMillis())
- .build();
- Pdpb.SetGraphSpaceRequest request = Pdpb.SetGraphSpaceRequest.newBuilder()
- .setHeader(header)
- .setGraphSpace(graphSpace)
- .build();
- Pdpb.SetGraphSpaceResponse response = getStub().setGraphSpace(request);
- handleResponseError(response.getHeader());
- }
-
- public List<Metapb.GraphSpace> getGraphSpace(String graphSpaceName) throws
- PDException {
- Pdpb.GetGraphSpaceRequest.Builder builder = Pdpb.GetGraphSpaceRequest.newBuilder();
- Pdpb.GetGraphSpaceRequest request;
- builder.setHeader(header);
- if (graphSpaceName != null && graphSpaceName.length() > 0) {
- builder.setGraphSpaceName(graphSpaceName);
- }
- request = builder.build();
- Pdpb.GetGraphSpaceResponse response = getStub().getGraphSpace(request);
- List<Metapb.GraphSpace> graphSpaceList = response.getGraphSpaceList();
- handleResponseError(response.getHeader());
- return graphSpaceList;
- }
-
- public void setPDConfig(int partitionCount, String peerList, int shardCount,
- long version) throws PDException {
- Metapb.PDConfig pdConfig = Metapb.PDConfig.newBuilder().setPartitionCount(partitionCount)
- .setPeersList(peerList).setShardCount(shardCount)
- .setVersion(version)
- .setTimestamp(System.currentTimeMillis())
- .build();
- Pdpb.SetPDConfigRequest request = Pdpb.SetPDConfigRequest.newBuilder()
- .setHeader(header)
- .setPdConfig(pdConfig)
- .build();
- Pdpb.SetPDConfigResponse response = getStub().setPDConfig(request);
- handleResponseError(response.getHeader());
- }
-
- public Metapb.PDConfig getPDConfig() throws PDException {
- Pdpb.GetPDConfigRequest request = Pdpb.GetPDConfigRequest.newBuilder()
- .setHeader(header)
- .build();
- Pdpb.GetPDConfigResponse response = getStub().getPDConfig(request);
- handleResponseError(response.getHeader());
- return response.getPdConfig();
- }
-
- public void setPDConfig(Metapb.PDConfig pdConfig) throws PDException {
- Pdpb.SetPDConfigRequest request = Pdpb.SetPDConfigRequest.newBuilder()
- .setHeader(header)
- .setPdConfig(pdConfig)
- .build();
- Pdpb.SetPDConfigResponse response = getStub().setPDConfig(request);
- handleResponseError(response.getHeader());
- }
-
- public Metapb.PDConfig getPDConfig(long version) throws PDException {
- Pdpb.GetPDConfigRequest request = Pdpb.GetPDConfigRequest.newBuilder().setHeader(
- header).setVersion(version).build();
- Pdpb.GetPDConfigResponse response = getStub().getPDConfig(request);
- handleResponseError(response.getHeader());
- return response.getPdConfig();
- }
-
- public void changePeerList(String peerList) throws PDException {
- Pdpb.ChangePeerListRequest request = Pdpb.ChangePeerListRequest.newBuilder()
- .setPeerList(peerList)
- .setHeader(header).build();
- Pdpb.getChangePeerListResponse response =
- blockingUnaryCall(PDGrpc.getChangePeerListMethod(), request);
- handleResponseError(response.getHeader());
- }
-
- /**
- * 工作模式
- * Auto:自动分裂,每个Store上分区数达到最大值
- *
- * @throws PDException
- */
- public void splitData() throws PDException {
- Pdpb.SplitDataRequest request = Pdpb.SplitDataRequest.newBuilder()
- .setHeader(header)
- .setMode(Pdpb.OperationMode.Auto)
- .build();
- Pdpb.SplitDataResponse response = getStub().splitData(request);
- handleResponseError(response.getHeader());
- }
-
- /**
- * 工作模式
- * Auto:自动分裂,每个Store上分区数达到最大值
- * Expert:专家模式,需要指定splitParams
- *
- * @param mode
- * @param params
- * @throws PDException
- */
- public void splitData(Pdpb.OperationMode mode, List<Pdpb.SplitDataParam> params) throws
- PDException {
- Pdpb.SplitDataRequest request = Pdpb.SplitDataRequest.newBuilder()
- .setHeader(header)
- .setMode(mode)
- .addAllParam(params).build();
- Pdpb.SplitDataResponse response = getStub().splitData(request);
- handleResponseError(response.getHeader());
- }
-
- public void splitGraphData(String graphName, int toCount) throws PDException {
- Pdpb.SplitGraphDataRequest request = Pdpb.SplitGraphDataRequest.newBuilder()
- .setHeader(header)
- .setGraphName(graphName)
- .setToCount(toCount)
- .build();
- Pdpb.SplitDataResponse response = getStub().splitGraphData(request);
- handleResponseError(response.getHeader());
- }
-
- /**
- * 自动转移,达到每个Store上分区数量相同
- *
- * @throws PDException
- */
- public void balancePartition() throws PDException {
- Pdpb.MovePartitionRequest request = Pdpb.MovePartitionRequest.newBuilder()
- .setHeader(header)
- .setMode(
- Pdpb.OperationMode.Auto)
- .build();
- Pdpb.MovePartitionResponse response = getStub().movePartition(request);
- handleResponseError(response.getHeader());
- }
-
- /**
- * //工作模式
- * // Auto:自动转移,达到每个Store上分区数量相同
- * // Expert:专家模式,需要指定transferParams
- *
- * @param mode
- * @param params
- * @throws PDException
- */
- public void movePartition(Pdpb.OperationMode mode, List<Pdpb.MovePartitionParam> params) throws
- PDException {
- Pdpb.MovePartitionRequest request = Pdpb.MovePartitionRequest.newBuilder()
- .setHeader(header)
- .setMode(mode)
- .addAllParam(params).build();
- Pdpb.MovePartitionResponse response = getStub().movePartition(request);
- handleResponseError(response.getHeader());
- }
-
- public void reportTask(MetaTask.Task task) throws PDException {
- Pdpb.ReportTaskRequest request = Pdpb.ReportTaskRequest.newBuilder()
- .setHeader(header)
- .setTask(task).build();
- Pdpb.ReportTaskResponse response = blockingUnaryCall(PDGrpc.getReportTaskMethod(), request);
- handleResponseError(response.getHeader());
- }
-
- public Metapb.PartitionStats getPartitionsStats(String graph, int partId) throws PDException {
- Pdpb.GetPartitionStatsRequest request = Pdpb.GetPartitionStatsRequest.newBuilder()
- .setHeader(header)
- .setGraphName(graph)
- .setPartitionId(partId)
- .build();
- Pdpb.GetPartitionStatsResponse response = getStub().getPartitionStats(request);
- handleResponseError(response.getHeader());
- return response.getPartitionStats();
- }
-
- /**
- * 平衡不同store中leader的数量
- */
- public void balanceLeaders() throws PDException {
- Pdpb.BalanceLeadersRequest request = Pdpb.BalanceLeadersRequest.newBuilder()
- .setHeader(header)
- .build();
- Pdpb.BalanceLeadersResponse response = getStub().balanceLeaders(request);
- handleResponseError(response.getHeader());
- }
-
- /**
- * 从pd中删除store
- */
- public Metapb.Store delStore(long storeId) throws PDException {
- Pdpb.DetStoreRequest request = Pdpb.DetStoreRequest.newBuilder()
- .setHeader(header)
- .setStoreId(storeId)
- .build();
- Pdpb.DetStoreResponse response = getStub().delStore(request);
- handleResponseError(response.getHeader());
- return response.getStore();
- }
-
- /**
- * 对rocksdb整体进行compaction
- *
- * @throws PDException
- */
- public void dbCompaction() throws PDException {
- Pdpb.DbCompactionRequest request = Pdpb.DbCompactionRequest
- .newBuilder()
- .setHeader(header)
- .build();
- Pdpb.DbCompactionResponse response = getStub().dbCompaction(request);
- handleResponseError(response.getHeader());
- }
-
- /**
- * 对rocksdb指定表进行compaction
- *
- * @param tableName
- * @throws PDException
- */
- public void dbCompaction(String tableName) throws PDException {
- Pdpb.DbCompactionRequest request = Pdpb.DbCompactionRequest
- .newBuilder()
- .setHeader(header)
- .setTableName(tableName)
- .build();
- Pdpb.DbCompactionResponse response = getStub().dbCompaction(request);
- handleResponseError(response.getHeader());
- }
-
- /**
- * 分区合并,把当前的分区缩容至toCount个
- *
- * @param toCount 缩容到分区的个数
- * @throws PDException
- */
- public void combineCluster(int toCount) throws PDException {
- Pdpb.CombineClusterRequest request = Pdpb.CombineClusterRequest
- .newBuilder()
- .setHeader(header)
- .setToCount(toCount)
- .build();
- Pdpb.CombineClusterResponse response = getStub().combineCluster(request);
- handleResponseError(response.getHeader());
- }
-
- /**
- * 将单图缩容到 toCount个
- *
- * @param graphName graph name
- * @param toCount target count
- * @throws PDException
- */
- public void combineGraph(String graphName, int toCount) throws PDException {
- Pdpb.CombineGraphRequest request = Pdpb.CombineGraphRequest
- .newBuilder()
- .setHeader(header)
- .setGraphName(graphName)
- .setToCount(toCount)
- .build();
- Pdpb.CombineGraphResponse response = getStub().combineGraph(request);
- handleResponseError(response.getHeader());
- }
-
- public void deleteShardGroup(int groupId) throws PDException {
- Pdpb.DeleteShardGroupRequest request = Pdpb.DeleteShardGroupRequest
- .newBuilder()
- .setHeader(header)
- .setGroupId(groupId)
- .build();
- Pdpb.DeleteShardGroupResponse response =
- blockingUnaryCall(PDGrpc.getDeleteShardGroupMethod(), request);
-
- handleResponseError(response.getHeader());
- }
-
- /**
- * 用于 store的 shard list重建
- *
- * @param groupId shard group id
- * @param shards shard list,delete when shards size is 0
- */
- public void updateShardGroupOp(int groupId, List<Metapb.Shard> shards) throws PDException {
- Pdpb.ChangeShardRequest request = Pdpb.ChangeShardRequest.newBuilder()
- .setHeader(header)
- .setGroupId(groupId)
- .addAllShards(shards)
- .build();
- Pdpb.ChangeShardResponse response = getStub().updateShardGroupOp(request);
- handleResponseError(response.getHeader());
- }
-
- /**
- * invoke fireChangeShard command
- *
- * @param groupId shard group id
- * @param shards shard list
- */
- public void changeShard(int groupId, List<Metapb.Shard> shards) throws PDException {
- Pdpb.ChangeShardRequest request = Pdpb.ChangeShardRequest.newBuilder()
- .setHeader(header)
- .setGroupId(groupId)
- .addAllShards(shards)
- .build();
- Pdpb.ChangeShardResponse response = getStub().changeShard(request);
- handleResponseError(response.getHeader());
- }
-
- public ClientCache getCache() {
- return cache;
- }
-
- public CacheResponse getClientCache() throws PDException {
- GetGraphRequest request = GetGraphRequest.newBuilder().setHeader(header).build();
- CacheResponse cache = getStub().getCache(request);
- handleResponseError(cache.getHeader());
- return cache;
- }
-
- public CachePartitionResponse getPartitionCache(String graph) throws PDException {
- GetGraphRequest request =
- GetGraphRequest.newBuilder().setHeader(header).setGraphName(graph).build();
- CachePartitionResponse ps = getStub().getPartitions(request);
- handleResponseError(ps.getHeader());
- return ps;
- }
-
- public void updatePdRaft(String raftConfig) throws PDException {
- Pdpb.UpdatePdRaftRequest request = Pdpb.UpdatePdRaftRequest.newBuilder()
- .setHeader(header)
- .setConfig(raftConfig)
- .build();
- Pdpb.UpdatePdRaftResponse response = getStub().updatePdRaft(request);
- handleResponseError(response.getHeader());
- }
-
- public interface PDEventListener {
- void onStoreChanged(NodeEvent event);
-
- void onPartitionChanged(PartitionEvent event);
-
- void onGraphChanged(WatchResponse event);
-
- default void onShardGroupChanged(WatchResponse event) {
- }
-
- }
-
- static class StubProxy {
-
- private final LinkedList<String> hostList = new LinkedList<>();
- private volatile PDGrpc.PDBlockingStub stub;
- private String leader;
-
- public StubProxy(String[] hosts) {
- for (String host : hosts) {
- if (!host.isEmpty()) {
- hostList.offer(host);
- }
- }
- }
-
- public String nextHost() {
- String host = hostList.poll();
- hostList.offer(host); //移到尾部
- return host;
- }
-
- public void set(PDGrpc.PDBlockingStub stub) {
- this.stub = stub;
- }
-
- public PDGrpc.PDBlockingStub get() {
- return this.stub;
- }
-
- public String getHost() {
- return hostList.peek();
- }
-
- public int getHostCount() {
- return hostList.size();
- }
-
- public String getLeader() {
- return leader;
- }
-
- public void setLeader(String leader) {
- this.leader = leader;
- }
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDConfig.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDConfig.java
deleted file mode 100644
index 64558436e..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDConfig.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-public final class PDConfig {
- //TODO multi-server
- private String serverHost = "localhost:9000";
- private long grpcTimeOut = 60000; // grpc调用超时时间 10秒
-
- // 是否接收PD异步通知
- private boolean enablePDNotify = false;
-
- private boolean enableCache = false;
-
- private PDConfig() {
- }
-
- public static PDConfig of() {
- return new PDConfig();
- }
-
- public static PDConfig of(String serverHost) {
- PDConfig config = new PDConfig();
- config.serverHost = serverHost;
- return config;
- }
-
- public static PDConfig of(String serverHost, long timeOut) {
- PDConfig config = new PDConfig();
- config.serverHost = serverHost;
- config.grpcTimeOut = timeOut;
- return config;
- }
-
- public String getServerHost() {
- return serverHost;
- }
-
- public long getGrpcTimeOut() {
- return grpcTimeOut;
- }
-
- @Deprecated
- public PDConfig setEnablePDNotify(boolean enablePDNotify) {
- this.enablePDNotify = enablePDNotify;
-
- // TODO 临时代码,hugegraph修改完后删除
- this.enableCache = enablePDNotify;
- return this;
- }
-
- public boolean isEnableCache() {
- return enableCache;
- }
-
- public PDConfig setEnableCache(boolean enableCache) {
- this.enableCache = enableCache;
- return this;
- }
-
- @Override
- public String toString() {
- return "PDConfig{" +
- "serverHost='" + serverHost + '\'' +
- '}';
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDPulse.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDPulse.java
deleted file mode 100644
index 025d6f7ae..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDPulse.java
+++ /dev/null
@@ -1,152 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.io.Closeable;
-import java.util.function.Consumer;
-
-import org.apache.hugegraph.pd.grpc.pulse.PartitionHeartbeatRequest;
-import org.apache.hugegraph.pd.grpc.pulse.PulseResponse;
-import org.apache.hugegraph.pd.pulse.PulseServerNotice;
-
-/**
- * Bidirectional communication interface of pd-client and pd-server
- */
-public interface PDPulse {
-
- /*** inner static methods ***/
- static <T> Listener<T> listener(Consumer<T> onNext) {
- return listener(onNext, t -> {
- }, () -> {
- });
- }
-
- static <T> Listener<T> listener(Consumer<T> onNext, Consumer<Throwable> onError) {
- return listener(onNext, onError, () -> {
- });
- }
-
- static <T> Listener<T> listener(Consumer<T> onNext, Runnable onCompleted) {
- return listener(onNext, t -> {
- }, onCompleted);
- }
-
- static <T> Listener<T> listener(Consumer<T> onNext, Consumer<Throwable> onError,
- Runnable onCompleted) {
- return new Listener<>() {
- @Override
- public void onNext(T response) {
- onNext.accept(response);
- }
-
- @Override
- public void onNotice(PulseServerNotice<T> notice) {
-
- }
-
- @Override
- public void onError(Throwable throwable) {
- onError.accept(throwable);
- }
-
- @Override
- public void onCompleted() {
- onCompleted.run();
- }
- };
- }
-
- /**
- * @param listener
- * @return
- */
- Notifier<PartitionHeartbeatRequest.Builder> connectPartition(Listener<PulseResponse> listener);
-
- /**
- * 切换成新的host。做 channel/host的检查,如果需要关闭,notifier调用close方法。
- *
- * @param host new host
- * @param notifier notifier
- * @return true if create new stub, otherwise false
- */
- boolean resetStub(String host, Notifier notifier);
-
- /**
- * Interface of pulse.
- */
- interface Listener<T> {
- /**
- * Invoked on new events.
- *
- * @param response the response.
- */
- @Deprecated
- default void onNext(T response) {
- }
-
- /**
- * Invoked on new events.
- *
- * @param notice a wrapper of response
- */
- default void onNotice(PulseServerNotice<T> notice) {
- notice.ack();
- }
-
- /**
- * Invoked on errors.
- *
- * @param throwable the error.
- */
- void onError(Throwable throwable);
-
- /**
- * Invoked on completion.
- */
- void onCompleted();
-
- }
-
- /**
- * Interface of notifier that can send notice to server.
- *
- * @param <T>
- */
- interface Notifier<T> extends Closeable {
- /**
- * closes this watcher and all its resources.
- */
- @Override
- void close();
-
- /**
- * Send notice to pd-server.
- *
- * @return
- */
- void notifyServer(T t);
-
- /**
- * Send an error report to pd-server.
- *
- * @param error
- */
- void crash(String error);
-
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDPulseImpl.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDPulseImpl.java
deleted file mode 100644
index 2d1ccb743..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDPulseImpl.java
+++ /dev/null
@@ -1,196 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.util.Objects;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
-import org.apache.hugegraph.pd.grpc.pulse.HgPdPulseGrpc;
-import org.apache.hugegraph.pd.grpc.pulse.PartitionHeartbeatRequest;
-import org.apache.hugegraph.pd.grpc.pulse.PulseAckRequest;
-import org.apache.hugegraph.pd.grpc.pulse.PulseCreateRequest;
-import org.apache.hugegraph.pd.grpc.pulse.PulseNoticeRequest;
-import org.apache.hugegraph.pd.grpc.pulse.PulseRequest;
-import org.apache.hugegraph.pd.grpc.pulse.PulseResponse;
-import org.apache.hugegraph.pd.grpc.pulse.PulseType;
-import org.apache.hugegraph.pd.pulse.PartitionNotice;
-
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
-
-import io.grpc.ManagedChannel;
-import io.grpc.stub.StreamObserver;
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-public final class PDPulseImpl implements PDPulse {
-
- private static final ConcurrentHashMap<String, ManagedChannel> chs = new ConcurrentHashMap<>();
- private final ExecutorService threadPool;
- private HgPdPulseGrpc.HgPdPulseStub stub;
- private String pdServerAddress;
-
- // TODO: support several servers.
- public PDPulseImpl(String pdServerAddress) {
- this.pdServerAddress = pdServerAddress;
- this.stub = HgPdPulseGrpc.newStub(Channels.getChannel(pdServerAddress));
- var namedThreadFactory =
- new ThreadFactoryBuilder().setNameFormat("ack-notice-pool-%d").build();
- threadPool = Executors.newSingleThreadExecutor(namedThreadFactory);
- }
-
-
- private String getCurrentHost() {
- return this.pdServerAddress;
- }
-
- private boolean checkChannel() {
- return stub != null && !((ManagedChannel) stub.getChannel()).isShutdown();
- }
-
- /* TODO: handle this override problem */
- @Override
- public Notifier<PartitionHeartbeatRequest.Builder> connectPartition(Listener<PulseResponse>
- listener) {
- return new PartitionHeartbeat(listener);
- }
-
- @Override
- public boolean resetStub(String host, Notifier notifier) {
- log.info("reset stub: current, {}, new: {}, channel state:{}", getCurrentHost(), host,
- checkChannel());
- if (Objects.equals(host, getCurrentHost()) && checkChannel()) {
- return false;
- }
-
- if (notifier != null) {
- notifier.close();
- }
-
- this.stub = HgPdPulseGrpc.newStub(Channels.getChannel(host));
- log.info("pd pulse connect to {}", host);
- this.pdServerAddress = host;
- return true;
- }
-
- /*** PartitionHeartbeat's implement ***/
- private class PartitionHeartbeat extends
- AbstractConnector<PartitionHeartbeatRequest.Builder,
- PulseResponse> {
- private long observerId = -1;
-
- PartitionHeartbeat(Listener<PulseResponse> listener) {
- super(listener, PulseType.PULSE_TYPE_PARTITION_HEARTBEAT);
- }
-
- private void setObserverId(long observerId) {
- if (this.observerId == -1) {
- this.observerId = observerId;
- }
- }
-
- @Override
- public void notifyServer(PartitionHeartbeatRequest.Builder requestBuilder) {
- this.reqStream.onNext(PulseRequest.newBuilder()
- .setNoticeRequest(
- PulseNoticeRequest.newBuilder()
- .setPartitionHeartbeatRequest(
- requestBuilder.build()
- ).build()
- ).build()
- );
- }
-
- @Override
- public void onNext(PulseResponse pulseResponse) {
- this.setObserverId(pulseResponse.getObserverId());
- long noticeId = pulseResponse.getNoticeId();
- this.listener.onNext(pulseResponse);
- this.listener.onNotice(new PartitionNotice(noticeId,
- e -> super.ackNotice(e, observerId),
- pulseResponse));
- }
-
- }
-
- private abstract class AbstractConnector<N, L> implements Notifier<N>,
- StreamObserver<PulseResponse> {
- Listener<L> listener;
- StreamObserver<PulseRequest> reqStream;
- PulseType pulseType;
- PulseRequest.Builder reqBuilder = PulseRequest.newBuilder();
- PulseAckRequest.Builder ackBuilder = PulseAckRequest.newBuilder();
-
- private AbstractConnector(Listener<L> listener, PulseType pulseType) {
- this.listener = listener;
- this.pulseType = pulseType;
- this.init();
- }
-
- void init() {
- PulseCreateRequest.Builder builder = PulseCreateRequest.newBuilder()
- .setPulseType(this.pulseType);
-
- this.reqStream = PDPulseImpl.this.stub.pulse(this);
- this.reqStream.onNext(reqBuilder.clear().setCreateRequest(builder).build());
- }
-
- /*** notifier ***/
- @Override
- public void close() {
- this.reqStream.onCompleted();
- }
-
- @Override
- public abstract void notifyServer(N t);
-
- @Override
- public void crash(String error) {
- this.reqStream.onError(new Throwable(error));
- }
-
- /*** listener ***/
- @Override
- public abstract void onNext(PulseResponse pulseResponse);
-
- @Override
- public void onError(Throwable throwable) {
- this.listener.onError(throwable);
- }
-
- @Override
- public void onCompleted() {
- this.listener.onCompleted();
- }
-
- protected void ackNotice(long noticeId, long observerId) {
- threadPool.execute(() -> {
- // log.info("send ack: {}, ts: {}", noticeId, System.currentTimeMillis());
- this.reqStream.onNext(reqBuilder.clear()
- .setAckRequest(
- this.ackBuilder.clear()
- .setNoticeId(noticeId)
- .setObserverId(observerId)
- .build()
- ).build()
- );
- });
- }
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDWatch.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDWatch.java
deleted file mode 100644
index 3da255a82..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDWatch.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.io.Closeable;
-import java.util.function.Consumer;
-
-import org.apache.hugegraph.pd.grpc.watch.WatchResponse;
-import org.apache.hugegraph.pd.watch.NodeEvent;
-import org.apache.hugegraph.pd.watch.PartitionEvent;
-
-public interface PDWatch {
-
- /**
- * Watch the events of all store-nodes registered in the remote PD-Server.
- *
- * @param listener
- * @return
- */
- //PDWatcher watchNode(Listener<NodeEvent> listener);
-
- /**
- * Watch the events of the store-nodes assigned to a specified graph.
- *
- * @param graph the graph name which you want to watch
- * @param listener
- * @return
- */
- //PDWatcher watchNode(String graph, Listener<NodeEvent> listener);
-
- String getCurrentHost();
-
- boolean checkChannel();
-
- /*** inner static methods ***/
- static <T> Listener<T> listener(Consumer<T> onNext) {
- return listener(onNext, t -> {
- }, () -> {
- });
- }
-
- static <T> Listener<T> listener(Consumer<T> onNext, Consumer<Throwable> onError) {
- return listener(onNext, onError, () -> {
- });
- }
-
- static <T> Listener<T> listener(Consumer<T> onNext, Runnable onCompleted) {
- return listener(onNext, t -> {
- }, onCompleted);
- }
-
- static <T> Listener<T> listener(Consumer<T> onNext, Consumer<Throwable> onError,
- Runnable onCompleted) {
- return new Listener<T>() {
- @Override
- public void onNext(T response) {
- onNext.accept(response);
- }
-
- @Override
- public void onError(Throwable throwable) {
- onError.accept(throwable);
- }
-
- @Override
- public void onCompleted() {
- onCompleted.run();
- }
- };
- }
-
- /**
- * @param listener
- * @return
- */
- Watcher watchPartition(Listener<PartitionEvent> listener);
-
- Watcher watchNode(Listener<NodeEvent> listener);
-
- Watcher watchGraph(Listener<WatchResponse> listener);
-
- Watcher watchShardGroup(Listener<WatchResponse> listener);
-
-
- /**
- * Interface of Watcher.
- */
- interface Listener<T> {
- /**
- * Invoked on new events.
- *
- * @param response the response.
- */
- void onNext(T response);
-
- /**
- * Invoked on errors.
- *
- * @param throwable the error.
- */
- void onError(Throwable throwable);
-
- /**
- * Invoked on completion.
- */
- default void onCompleted() {};
- }
-
- interface Watcher extends Closeable {
- /**
- * closes this watcher and all its resources.
- */
- @Override
- void close();
-
- /**
- * Requests the latest revision processed and propagates it to listeners
- */
- // TODO: what's it for?
- //void requestProgress();
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDWatchImpl.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDWatchImpl.java
deleted file mode 100644
index 73796b53f..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/client/PDWatchImpl.java
+++ /dev/null
@@ -1,202 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.util.function.Supplier;
-
-import org.apache.hugegraph.pd.grpc.watch.HgPdWatchGrpc;
-import org.apache.hugegraph.pd.grpc.watch.WatchCreateRequest;
-import org.apache.hugegraph.pd.grpc.watch.WatchNodeResponse;
-import org.apache.hugegraph.pd.grpc.watch.WatchPartitionResponse;
-import org.apache.hugegraph.pd.grpc.watch.WatchRequest;
-import org.apache.hugegraph.pd.grpc.watch.WatchResponse;
-import org.apache.hugegraph.pd.grpc.watch.WatchType;
-import org.apache.hugegraph.pd.watch.NodeEvent;
-import org.apache.hugegraph.pd.watch.PartitionEvent;
-
-import io.grpc.ManagedChannel;
-import io.grpc.stub.StreamObserver;
-
-final class PDWatchImpl implements PDWatch {
-
- private final HgPdWatchGrpc.HgPdWatchStub stub;
-
- private final String pdServerAddress;
-
- // TODO: support several servers.
- PDWatchImpl(String pdServerAddress) {
- this.pdServerAddress = pdServerAddress;
- this.stub = HgPdWatchGrpc.newStub(Channels.getChannel(pdServerAddress));
- }
-
- @Override
- public String getCurrentHost() {
- return this.pdServerAddress;
- }
-
- @Override
- public boolean checkChannel() {
- return stub != null && !((ManagedChannel) stub.getChannel()).isShutdown();
- }
-
- /**
- * Get Partition change watcher.
- *
- * @param listener
- * @return
- */
- @Override
- public Watcher watchPartition(Listener<PartitionEvent> listener) {
- return new PartitionWatcher(listener);
- }
-
- /**
- * Get Store-Node change watcher.
- *
- * @param listener
- * @return
- */
- @Override
- public Watcher watchNode(Listener<NodeEvent> listener) {
- return new NodeWatcher(listener);
- }
-
- @Override
- public Watcher watchGraph(Listener<WatchResponse> listener) {
- return new GraphWatcher(listener);
- }
-
- @Override
- public Watcher watchShardGroup(Listener<WatchResponse> listener) {
- return new ShardGroupWatcher(listener);
- }
-
- private class GraphWatcher extends AbstractWatcher<WatchResponse> {
-
- private GraphWatcher(Listener listener) {
- super(listener,
- () -> WatchCreateRequest
- .newBuilder()
- .setWatchType(WatchType.WATCH_TYPE_GRAPH_CHANGE)
- .build()
- );
- }
-
- @Override
- public void onNext(WatchResponse watchResponse) {
- this.listener.onNext(watchResponse);
- }
- }
-
- private class ShardGroupWatcher extends AbstractWatcher<WatchResponse> {
-
- private ShardGroupWatcher(Listener listener) {
- super(listener,
- () -> WatchCreateRequest
- .newBuilder()
- .setWatchType(WatchType.WATCH_TYPE_SHARD_GROUP_CHANGE)
- .build()
- );
- }
-
- @Override
- public void onNext(WatchResponse watchResponse) {
- this.listener.onNext(watchResponse);
- }
- }
-
- private class PartitionWatcher extends AbstractWatcher<PartitionEvent> {
-
- private PartitionWatcher(Listener listener) {
- super(listener,
- () -> WatchCreateRequest
- .newBuilder()
- .setWatchType(WatchType.WATCH_TYPE_PARTITION_CHANGE)
- .build()
- );
- }
-
- @Override
- public void onNext(WatchResponse watchResponse) {
- WatchPartitionResponse res = watchResponse.getPartitionResponse();
- PartitionEvent event = new PartitionEvent(res.getGraph(), res.getPartitionId(),
- PartitionEvent.ChangeType.grpcTypeOf(
- res.getChangeType()));
- this.listener.onNext(event);
- }
- }
-
- private class NodeWatcher extends AbstractWatcher<NodeEvent> {
- private NodeWatcher(Listener listener) {
- super(listener,
- () -> WatchCreateRequest
- .newBuilder()
- .setWatchType(WatchType.WATCH_TYPE_STORE_NODE_CHANGE)
- .build()
- );
- }
-
- @Override
- public void onNext(WatchResponse watchResponse) {
- WatchNodeResponse res = watchResponse.getNodeResponse();
- NodeEvent event = new NodeEvent(res.getGraph(), res.getNodeId(),
- NodeEvent.EventType.grpcTypeOf(res.getNodeEventType()));
- this.listener.onNext(event);
- }
- }
-
- private abstract class AbstractWatcher<T> implements Watcher, StreamObserver<WatchResponse> {
- Listener<T> listener;
- StreamObserver<WatchRequest> reqStream;
- Supplier<WatchCreateRequest> requestSupplier;
-
- private AbstractWatcher(Listener<T> listener,
- Supplier<WatchCreateRequest> requestSupplier) {
- this.listener = listener;
- this.requestSupplier = requestSupplier;
- this.init();
- }
-
- void init() {
- this.reqStream = PDWatchImpl.this.stub.watch(this);
- this.reqStream.onNext(WatchRequest.newBuilder().setCreateRequest(
- this.requestSupplier.get()
- ).build());
- }
-
- @Override
- public void close() {
- this.reqStream.onCompleted();
- }
-
- @Override
- public abstract void onNext(WatchResponse watchResponse);
-
- @Override
- public void onError(Throwable throwable) {
-
- this.listener.onError(throwable);
- }
-
- @Override
- public void onCompleted() {
- this.listener.onCompleted();
- }
- }
-
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/pulse/PartitionNotice.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/pulse/PartitionNotice.java
deleted file mode 100644
index b6e5555e6..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/pulse/PartitionNotice.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.pulse;
-
-import java.util.function.Consumer;
-
-import org.apache.hugegraph.pd.grpc.pulse.PulseResponse;
-
-public class PartitionNotice implements PulseServerNotice<PulseResponse> {
- private final long noticeId;
- private final Consumer<Long> ackConsumer;
- private final PulseResponse content;
-
- public PartitionNotice(long noticeId, Consumer<Long> ackConsumer, PulseResponse content) {
- this.noticeId = noticeId;
- this.ackConsumer = ackConsumer;
- this.content = content;
- }
-
- @Override
- public void ack() {
- this.ackConsumer.accept(this.noticeId);
- }
-
- @Override
- public long getNoticeId() {
- return this.noticeId;
- }
-
- @Override
- public PulseResponse getContent() {
- return this.content;
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/pulse/PulseServerNotice.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/pulse/PulseServerNotice.java
deleted file mode 100644
index 251bab07f..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/pulse/PulseServerNotice.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.pulse;
-
-public interface PulseServerNotice<T> {
- /**
- * @throws RuntimeException when failed to send ack-message to pd-server
- */
- void ack();
-
- long getNoticeId();
-
- /**
- * Return a response object of gRPC stream.
- *
- * @return
- */
- T getContent();
-
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/NodeEvent.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/NodeEvent.java
deleted file mode 100644
index 893566b9f..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/NodeEvent.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.watch;
-
-import java.util.Objects;
-
-import org.apache.hugegraph.pd.grpc.watch.NodeEventType;
-
-public class NodeEvent {
- private final String graph;
- private final long nodeId;
- private final EventType eventType;
-
- public NodeEvent(String graph, long nodeId, EventType eventType) {
- this.graph = graph;
- this.nodeId = nodeId;
- this.eventType = eventType;
- }
-
- public String getGraph() {
- return graph;
- }
-
- public long getNodeId() {
- return nodeId;
- }
-
- public EventType getEventType() {
- return eventType;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- NodeEvent nodeEvent = (NodeEvent) o;
- return nodeId == nodeEvent.nodeId && Objects.equals(graph,
- nodeEvent.graph) &&
- eventType == nodeEvent.eventType;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(graph, nodeId, eventType);
- }
-
- @Override
- public String toString() {
- return "NodeEvent{" +
- "graph='" + graph + '\'' +
- ", nodeId=" + nodeId +
- ", eventType=" + eventType +
- '}';
- }
-
- public enum EventType {
- UNKNOWN,
- NODE_ONLINE,
- NODE_OFFLINE,
- NODE_RAFT_CHANGE,
- NODE_PD_LEADER_CHANGE;
-
- public static EventType grpcTypeOf(NodeEventType grpcType) {
- switch (grpcType) {
- case NODE_EVENT_TYPE_NODE_ONLINE:
- return NODE_ONLINE;
- case NODE_EVENT_TYPE_NODE_OFFLINE:
- return NODE_OFFLINE;
- case NODE_EVENT_TYPE_NODE_RAFT_CHANGE:
- return NODE_RAFT_CHANGE;
- case NODE_EVENT_TYPE_PD_LEADER_CHANGE:
- return NODE_PD_LEADER_CHANGE;
- default:
- return UNKNOWN;
- }
-
- }
-
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/PDWatcher.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/PDWatcher.java
deleted file mode 100644
index c4ff91b10..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/PDWatcher.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.watch;
-
-public class PDWatcher {
-
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/PartitionEvent.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/PartitionEvent.java
deleted file mode 100644
index 76a4fdc7d..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/PartitionEvent.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.watch;
-
-import java.util.Objects;
-
-import org.apache.hugegraph.pd.grpc.watch.WatchChangeType;
-
-public class PartitionEvent {
- private final String graph;
- private final int partitionId;
- private final ChangeType changeType;
-
- public PartitionEvent(String graph, int partitionId, ChangeType changeType) {
- this.graph = graph;
- this.partitionId = partitionId;
- this.changeType = changeType;
- }
-
- public String getGraph() {
- return this.graph;
- }
-
- public int getPartitionId() {
- return this.partitionId;
- }
-
- public ChangeType getChangeType() {
- return this.changeType;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- PartitionEvent that = (PartitionEvent) o;
- return partitionId == that.partitionId && Objects.equals(graph, that.graph) &&
- changeType == that.changeType;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(graph, partitionId, changeType);
- }
-
- @Override
- public String toString() {
- return "PartitionEvent{" +
- "graph='" + graph + '\'' +
- ", partitionId=" + partitionId +
- ", changeType=" + changeType +
- '}';
- }
-
- public enum ChangeType {
- UNKNOWN,
- ADD,
- ALTER,
- DEL;
-
- public static ChangeType grpcTypeOf(WatchChangeType grpcType) {
- switch (grpcType) {
- case WATCH_CHANGE_TYPE_ADD:
- return ADD;
- case WATCH_CHANGE_TYPE_ALTER:
- return ALTER;
- case WATCH_CHANGE_TYPE_DEL:
- return DEL;
- default:
- return UNKNOWN;
- }
- }
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/WatchType.java b/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/WatchType.java
deleted file mode 100644
index e14bfafdc..000000000
--- a/hugegraph-pd/hg-pd-client/src/main/java/org/apache/hugegraph/pd/watch/WatchType.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.watch;
-
-enum WatchType {
-
- PARTITION_CHANGE(10);
-
- private final int value;
-
- WatchType(int value) {
- this.value = value;
- }
-
-}
diff --git a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/PartitionCacheTest.java b/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/PartitionCacheTest.java
deleted file mode 100644
index 0b937f4ed..000000000
--- a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/PartitionCacheTest.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.hugegraph.pd.common.KVPair;
-import org.apache.hugegraph.pd.common.PartitionCache;
-import org.apache.hugegraph.pd.grpc.Metapb;
-
-import com.google.common.collect.Range;
-import com.google.common.collect.RangeMap;
-import com.google.common.collect.TreeRangeMap;
-
-public class PartitionCacheTest {
-
- // @Test
- public void test() {
- PartitionCache cache = new PartitionCache();
- for (int i = 0; i < 10; i++) {
- KVPair<Metapb.Partition, Metapb.Shard> partShards =
- new KVPair<>(Metapb.Partition.newBuilder()
- .setStartKey(i * 10)
- .setEndKey((i + 1) * 10)
- .build(), null);
- cache.updatePartition("aa", i, partShards.getKey());
- }
-
- for (int i = 0; i < 100; i++) {
- KVPair<Metapb.Partition, Metapb.Shard> partShards = cache.getPartitionByCode("aa", i);
- System.out.println(" " + i + " " + partShards.getKey().getStartKey());
- }
- }
-
-
- // @Test
- public void test1() {
- Map<String, RangeMap<Long, Integer>> keyToPartIdCache = new HashMap<>();
- // graphName + PartitionID组成key
- Map<String, KVPair<Metapb.Partition, Metapb.Shard>> partitionCache = new HashMap<>();
-
- // 缓存全部Store,用于全库查询,需要优化
- Map<String, List<Metapb.Store>> allStoresCache = new HashMap<>();
-
- keyToPartIdCache.put("a", TreeRangeMap.create());
-
- keyToPartIdCache.get("a")
- .put(Range.closedOpen(1L, 2L), 1);
-
- allStoresCache.put("a", new ArrayList<>());
- allStoresCache.get("a").add(Metapb.Store.newBuilder().setId(34).build());
-
-
- Map<String, RangeMap<Long, Integer>> keyToPartIdCache2 =
- cloneKeyToPartIdCache(keyToPartIdCache);
- System.out.println(keyToPartIdCache2.size());
- }
-
- public Map<String, RangeMap<Long, Integer>> cloneKeyToPartIdCache(
- Map<String, RangeMap<Long, Integer>> cache) {
- Map<String, RangeMap<Long, Integer>> cacheClone = new HashMap<>();
- cache.forEach((k1, v1) -> {
- cacheClone.put(k1, TreeRangeMap.create());
- v1.asMapOfRanges().forEach((k2, v2) -> {
- cacheClone.get(k1).put(k2, v2);
- });
- });
- return cacheClone;
- }
-
- public Map<String, KVPair<Metapb.Partition, Metapb.Shard>>
- clonePartitionCache(Map<String, KVPair<Metapb.Partition, Metapb.Shard>> cache) {
- Map<String, KVPair<Metapb.Partition, Metapb.Shard>> cacheClone = new HashMap<>();
- cacheClone.putAll(cache);
- return cacheClone;
- }
-
- public Map<String, List<Metapb.Store>>
- cloneStoreCache(Map<String, List<Metapb.Store>> cache) {
- Map<String, List<Metapb.Store>> cacheClone = new HashMap<>();
- cacheClone.putAll(cache);
- return cacheClone;
- }
-}
diff --git a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/StoreRegisterTest.java b/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/StoreRegisterTest.java
deleted file mode 100644
index bc5e3879a..000000000
--- a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/StoreRegisterTest.java
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd;
-
-import java.nio.charset.StandardCharsets;
-import java.util.List;
-
-import org.apache.hugegraph.pd.client.PDClient;
-import org.apache.hugegraph.pd.client.PDConfig;
-import org.apache.hugegraph.pd.client.PDPulse;
-import org.apache.hugegraph.pd.client.PDPulseImpl;
-import org.apache.hugegraph.pd.common.KVPair;
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.grpc.Metapb;
-import org.apache.hugegraph.pd.grpc.pulse.PartitionHeartbeatRequest;
-import org.apache.hugegraph.pd.grpc.pulse.PulseResponse;
-import org.apache.hugegraph.pd.pulse.PulseServerNotice;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-
-public class StoreRegisterTest {
- private static PDClient pdClient;
- private final String storeAddr = "localhost";
- private final String graphName = "default/hugegraph/g";
- private long storeId = 0;
-
- @BeforeClass
- public static void beforeClass() throws Exception {
- PDConfig config = PDConfig.of("localhost:8686");
- config.setEnableCache(true);
- pdClient = PDClient.create(config);
- }
-
- // @Test
- public void testRegisterStore() throws PDException {
- Metapb.Store store = Metapb.Store.newBuilder().setAddress(storeAddr).build();
- try {
- storeId = pdClient.registerStore(store);
- } catch (Exception e) {
- e.printStackTrace();
- }
- Assert.assertTrue("RegisterStore store_id = " + storeId, storeId != 0);
- }
-
- // @Test
- public void testGetStore() throws PDException {
- testRegisterStore();
- Metapb.Store store = pdClient.getStore(storeId);
- Assert.assertEquals(storeAddr, store.getAddress());
- System.out.println(store);
- }
-
- // @Test
- public void testGetActiveStores() throws PDException {
- testRegisterStore();
- List<Metapb.Store> stores = pdClient.getActiveStores(graphName);
- stores.forEach((e) -> {
- System.out.println("-------------------------------------");
- System.out.println(e);
- });
- }
-
-
- // @Test
- public void testStoreHeartbeat() throws PDException {
- testRegisterStore();
- Metapb.StoreStats stats = Metapb.StoreStats.newBuilder().setStoreId(storeId).build();
- pdClient.storeHeartbeat(stats);
- List<Metapb.Store> stores = pdClient.getActiveStores(graphName);
- boolean exist = false;
- for (Metapb.Store store : stores) {
- if (store.getId() == storeId) {
- exist = true;
- break;
- }
- }
- Assert.assertTrue(exist);
- }
-
-
- // @Test
- public void testPartitionHeartbeat() throws InterruptedException, PDException {
- testRegisterStore();
- PDPulse pdPulse = new PDPulseImpl(pdClient.getLeaderIp());
-
- PDPulse.Notifier<PartitionHeartbeatRequest.Builder> notifier = pdPulse.connectPartition(
- new PDPulse.Listener<PulseResponse>() {
-
- @Override
- public void onNext(PulseResponse response) {
-
- }
-
- @Override
- public void onNotice(PulseServerNotice<PulseResponse> notice) {
-
- }
-
- @Override
- public void onError(Throwable throwable) {
-
- }
-
- @Override
- public void onCompleted() {
-
- }
- });
- KVPair<Metapb.Partition, Metapb.Shard> partShard =
- pdClient.getPartition("test", "1".getBytes(StandardCharsets.UTF_8));
- notifier.notifyServer(PartitionHeartbeatRequest.newBuilder().setStates(
- Metapb.PartitionStats.newBuilder().addGraphName("test")
- .setId(partShard.getKey().getId())
- .setLeader(Metapb.Shard.newBuilder().setStoreId(1).build())));
-
-
- Thread.sleep(10000);
- }
-
-}
diff --git a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/DiscoveryClientImplTest.java b/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/DiscoveryClientImplTest.java
deleted file mode 100644
index 8952cc116..000000000
--- a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/DiscoveryClientImplTest.java
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Vector;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.atomic.AtomicLong;
-
-import org.apache.hugegraph.pd.grpc.discovery.NodeInfos;
-import org.apache.hugegraph.pd.grpc.discovery.Query;
-import org.junit.Assert;
-
-public class DiscoveryClientImplTest {
-
- private static final AtomicLong label = new AtomicLong();
- String address = "localhost:80";
- int delay = 1000;
- int wait = delay * 3 + 500;
-
- // @Test
- public void registerStore() throws InterruptedException {
-
- HashMap<String, String> labels = new HashMap<>();
-
- labels.put("metrics", "/actuator/prometheus");
- labels.put("target", "10.81.116.77:8520");
- labels.put("scheme", "http");
- labels.put("__relabeling", "http");
- labels.put("no_relabeling", "http");
- getClient("store", "address1", labels);
-
- labels.put("metrics", "/actuator/prometheus");
- labels.put("target", "10.81.116.78:8520");
- labels.put("scheme", "http");
- getClient("store", "address2", labels);
-
- labels.put("metrics", "/actuator/prometheus");
- labels.put("target", "10.81.116.79:8520");
- labels.put("scheme", "http");
- getClient("store", "address3", labels);
-
- labels.put("metrics", "/actuator/prometheus");
- labels.put("target", "10.81.116.78:8620");
- labels.put("scheme", "http");
- getClient("pd", "address1", labels);
-
- labels.put("metrics", "/graph/metrics");
- labels.put("target", "10.37.1.1:9200");
- labels.put("scheme", "https");
- getClient("hugegraph", "address1", labels);
- }
-
- // @Test
- public void testNodes() throws InterruptedException {
- String appName = "hugegraph";
- register(appName, address);
- }
-
- // @Test
- public void testMultiNode() throws InterruptedException {
- for (int i = 0; i < 2; i++) {
- register("app" + i, address + i);
- }
- }
-
- // @Test
- public void testParallelMultiNode() throws InterruptedException {
- CountDownLatch latch = new CountDownLatch(30);
- Vector<Exception> exceptions = new Vector<>();
- for (int i = 0; i < 30; i++) {
- int finalI = i;
- new Thread(() -> {
- try {
- for (int j = 0; j < 3; j++) {
- register("app" + finalI, address + j);
- }
- } catch (Exception e) {
- exceptions.add(e);
- } finally {
- latch.countDown();
- }
- }).start();
- }
- latch.await();
- Assert.assertEquals(0, exceptions.size());
- }
-
- private void register(String appName, String address) throws InterruptedException {
-
- HashMap<String, String> labels = new HashMap<>();
- String labelValue = String.valueOf(label.incrementAndGet());
- labels.put("address", labelValue);
- labels.put("address1", labelValue);
- Query query = Query.newBuilder().setAppName(
- appName).setVersion("0.13.0").putAllLabels(labels).build();
- DiscoveryClientImpl discoveryClient = getClient(appName, address, labels);
- Thread.sleep(10000);
- NodeInfos nodeInfos1 = discoveryClient.getNodeInfos(query);
- Assert.assertEquals(1, nodeInfos1.getInfoCount());
- DiscoveryClientImpl discoveryClient1 = getClient(appName, address + 0, labels);
- Thread.sleep(10000);
- Assert.assertEquals(2, discoveryClient.getNodeInfos(query).getInfoCount());
- Query query1 = Query.newBuilder().setAppName(
- appName).setVersion("0.12.0").putAllLabels(labels).build();
- Assert.assertEquals(0, discoveryClient.getNodeInfos(query1).getInfoCount());
- discoveryClient.cancelTask();
- discoveryClient1.cancelTask();
- Thread.sleep(wait);
- NodeInfos nodeInfos = discoveryClient.getNodeInfos(query);
- System.out.println(nodeInfos);
- Assert.assertEquals(0, nodeInfos.getInfoCount());
- discoveryClient.close();
- discoveryClient1.close();
- }
-
- private DiscoveryClientImpl getClient(String appName, String address, Map labels) {
- DiscoveryClientImpl discoveryClient = null;
- try {
- discoveryClient = DiscoveryClientImpl.newBuilder().setCenterAddress(
- "localhost:8687,localhost:8686,localhost:8688").setAddress(address).setAppName(
- appName).setDelay(delay).setVersion("0.13.0").setId(
- "0").setLabels(labels).build();
- discoveryClient.scheduleTask();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- return discoveryClient;
- }
-}
\ No newline at end of file
diff --git a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/LicenseClientImplTest.java b/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/LicenseClientImplTest.java
deleted file mode 100644
index 390821662..000000000
--- a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/LicenseClientImplTest.java
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.io.File;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Properties;
-
-import org.apache.commons.io.FileUtils;
-import org.apache.hugegraph.pd.grpc.Pdpb;
-import org.apache.hugegraph.pd.grpc.kv.KResponse;
-import org.apache.hugegraph.pd.grpc.kv.KvResponse;
-import org.yaml.snakeyaml.Yaml;
-
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-public class LicenseClientImplTest {
-
- // @Test
- public void putLicense() {
- PDConfig pdConfig = PDConfig.of("localhost:8686,localhost:8687,localhost:8688");
- //PDConfig pdConfig = PDConfig.of("localhost:8686");
- pdConfig.setEnableCache(true);
- try (LicenseClient c = new LicenseClient(pdConfig)) {
- File file = new File("../conf/hugegraph.license");
- byte[] bytes = FileUtils.readFileToByteArray(file);
- Pdpb.PutLicenseResponse putLicenseResponse = c.putLicense(bytes);
- Pdpb.Error error = putLicenseResponse.getHeader().getError();
- log.info(error.getMessage());
- assert error.getType().equals(Pdpb.ErrorType.OK);
- } catch (Exception e) {
- log.error("put license with error: ", e);
- }
- }
-
- // @Test
- public void getKv() {
- PDConfig pdConfig = PDConfig.of("10.157.12.36:8686");
- pdConfig.setEnableCache(true);
- try (KvClient c = new KvClient(pdConfig)) {
- KResponse kResponse = c.get("S:FS");
- Pdpb.Error error = kResponse.getHeader().getError();
- log.info(error.getMessage());
- assert error.getType().equals(Pdpb.ErrorType.OK);
- Properties ymlConfig = getYmlConfig(kResponse.getValue());
- Object property = ymlConfig.get("rocksdb.write_buffer_size");
- assert property.toString().equals("32000000");
- } catch (Exception e) {
- log.error("put license with error: ", e);
- }
- }
-
- // @Test
- public void putKv() {
- PDConfig pdConfig = PDConfig.of("127.0.0.1.70:8688");
- pdConfig.setEnableCache(true);
- try (KvClient c = new KvClient(pdConfig)) {
- long l = System.currentTimeMillis();
- KvResponse kvResponse = c.put("S:Timestamp", String.valueOf(l));
- Pdpb.Error error = kvResponse.getHeader().getError();
- log.info(error.getMessage());
- assert error.getType().equals(Pdpb.ErrorType.OK);
- } catch (Exception e) {
- log.error("put license with error: ", e);
- }
- }
-
- // @Test
- public void putKvLocal() {
- PDConfig pdConfig = PDConfig.of("localhost:8686");
- pdConfig.setEnableCache(true);
- try (KvClient c = new KvClient(pdConfig)) {
- long l = System.currentTimeMillis();
- KvResponse kvResponse = c.put("S:Timestamp", String.valueOf(l));
- Pdpb.Error error = kvResponse.getHeader().getError();
- log.info(error.getMessage());
- assert error.getType().equals(Pdpb.ErrorType.OK);
- } catch (Exception e) {
- log.error("put license with error: ", e);
- }
- }
-
- private Properties getYmlConfig(String yml) {
- Yaml yaml = new Yaml();
- Iterable<Object> load = yaml.loadAll(yml);
- Iterator<Object> iterator = load.iterator();
- Properties properties = new Properties();
- while (iterator.hasNext()) {
- Map<String, Object> next = (Map<String, Object>) iterator.next();
- map2Properties(next, "", properties);
- }
- return properties;
- }
-
- private void map2Properties(Map<String, Object> map, String prefix, Properties properties) {
-
- for (Map.Entry<String, Object> entry : map.entrySet()) {
- String key = entry.getKey();
- String newPrefix = prefix == null || prefix.length() == 0 ? key : prefix + "." + key;
- Object value = entry.getValue();
- if (!(value instanceof Map)) {
- properties.put(newPrefix, value);
- } else {
- map2Properties((Map<String, Object>) value, newPrefix, properties);
- }
-
- }
- }
-
-}
diff --git a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/PDPulseTest.java b/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/PDPulseTest.java
deleted file mode 100644
index dfdc63cf3..000000000
--- a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/PDPulseTest.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.hugegraph.pd.client.test.HgPDTestUtil;
-import org.apache.hugegraph.pd.grpc.pulse.PartitionHeartbeatRequest;
-import org.apache.hugegraph.pd.pulse.PulseServerNotice;
-import org.junit.BeforeClass;
-
-public class PDPulseTest {
- private static PDClient pdClient;
-
- private final long storeId = 0;
- private final String storeAddress = "localhost";
- private final String graphName = "graph1";
-
- @BeforeClass
- public static void beforeClass() throws Exception {
- PDConfig pdConfig = PDConfig.of("localhost:8686");
- pdConfig.setEnableCache(true);
- pdClient = PDClient.create(pdConfig);
- pdClient.getLeader();
- }
-
- // @Test
- public void listen() {
-
- PDPulse pulse = new PDPulseImpl(pdClient.getLeaderIp());
- CountDownLatch latch = new CountDownLatch(60);
-
- PDPulse.Notifier<PartitionHeartbeatRequest.Builder> notifier1 =
- pulse.connectPartition(new PulseListener(latch, "listener1"));
- PDPulse.Notifier<PartitionHeartbeatRequest.Builder> notifier2 =
- pulse.connectPartition(new PulseListener(latch, "listener2"));
- PDPulse.Notifier<PartitionHeartbeatRequest.Builder> notifier3 =
- pulse.connectPartition(new PulseListener(latch, "listener3"));
-
- try {
- latch.await(120, TimeUnit.SECONDS);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- PartitionHeartbeatRequest.Builder builder = PartitionHeartbeatRequest.newBuilder();
-
- notifier1.notifyServer(builder);
-
-
- notifier2.notifyServer(builder);
-
- notifier3.notifyServer(builder);
-
- notifier1.close();
- notifier2.close();
- notifier3.close();
- }
-
-
- private class PulseListener<T> implements PDPulse.Listener<T> {
- private final String listenerName;
- CountDownLatch latch = new CountDownLatch(10);
-
- private PulseListener(CountDownLatch latch, String listenerName) {
- this.latch = latch;
- this.listenerName = listenerName;
- }
-
- @Override
- public void onNext(T response) {
- // println(this.listenerName+" res: "+response);
- // this.latch.countDown();
- }
-
- @Override
- public void onNotice(PulseServerNotice<T> notice) {
- HgPDTestUtil.println(this.listenerName + " ---> res: " + notice.getContent());
-
- notice.ack();
- this.latch.countDown();
- }
-
- @Override
- public void onError(Throwable throwable) {
- HgPDTestUtil.println(this.listenerName + " error: " + throwable.toString());
- }
-
- @Override
- public void onCompleted() {
- HgPDTestUtil.println(this.listenerName + " is completed");
- }
- }
-}
\ No newline at end of file
diff --git a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/PDWatchTest.java b/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/PDWatchTest.java
deleted file mode 100644
index 675577596..000000000
--- a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/PDWatchTest.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client;
-
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.hugegraph.pd.client.test.HgPDTestUtil;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-@Deprecated
-public class PDWatchTest {
- private static PDClient pdClient;
-
- private final long storeId = 0;
- private final String storeAddr = "localhost";
- private final String graphName = "graph1";
-
- @BeforeClass
- public static void beforeClass() {
- pdClient = PDClient.create(PDConfig.of("localhost:9000"));
- }
-
- @Test
- public void watch() {
- PDWatch watch = pdClient.getWatchClient();
- CountDownLatch latch = new CountDownLatch(10);
-
- PDWatch.Watcher watcher1 = watch.watchPartition(new WatchListener<>(latch, "watcher1"));
- PDWatch.Watcher watcher2 = watch.watchPartition(new WatchListener<>(latch, "watcher2"));
- PDWatch.Watcher watcher3 = watch.watchPartition(new WatchListener<>(latch, "watcher3"));
-
- PDWatch.Watcher nodeWatcher1 = watch.watchNode(new WatchListener<>(latch, "nodeWatcher1"));
-
- try {
- latch.await(15, TimeUnit.SECONDS);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- watcher1.close();
- watcher2.close();
- watcher3.close();
- }
-
- private class WatchListener<T> implements PDWatch.Listener<T> {
- private final String watcherName;
- CountDownLatch latch;
-
- private WatchListener(CountDownLatch latch, String watcherName) {
- this.latch = latch;
- this.watcherName = watcherName;
- }
-
- @Override
- public void onNext(T response) {
- HgPDTestUtil.println(this.watcherName + " res: " + response);
- this.latch.countDown();
- }
-
- @Override
- public void onError(Throwable throwable) {
- HgPDTestUtil.println(this.watcherName + " error: " + throwable.toString());
- }
-
- @Override
- public void onCompleted() {
- HgPDTestUtil.println(this.watcherName + " is completed");
- }
- }
-}
\ No newline at end of file
diff --git a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/test/HgPDTestUtil.java b/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/test/HgPDTestUtil.java
deleted file mode 100644
index 99e5f8336..000000000
--- a/hugegraph-pd/hg-pd-client/src/test/java/org/apache/hugegraph/pd/client/test/HgPDTestUtil.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.client.test;
-
-import java.nio.ByteBuffer;
-import java.nio.charset.StandardCharsets;
-import java.util.Iterator;
-import java.util.List;
-
-public class HgPDTestUtil {
-
- public static void println(Object str) {
- System.out.println(str);
- }
-
- public static String toStr(byte[] b) {
- if (b == null) return "";
- if (b.length == 0) return "";
- return new String(b, StandardCharsets.UTF_8);
- }
-
- public static byte[] toBytes(String str) {
- if (str == null) return null;
- return str.getBytes(StandardCharsets.UTF_8);
- }
-
- public static byte[] toBytes(long l) {
- ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
- buffer.putLong(l);
- return buffer.array();
- }
-
- private static byte[] toBytes(final int i) {
- ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
- buffer.putInt(i);
- return buffer.array();
- }
-
- public static long toLong(byte[] bytes) {
- ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
- buffer.put(bytes);
- buffer.flip();//need flip
- return buffer.getLong();
- }
-
- public static long toInt(byte[] bytes) {
- ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
- buffer.put(bytes);
- buffer.flip();//need flip
- return buffer.getInt();
- }
-
- public static String padLeftZeros(String str, int n) {
- return String.format("%1$" + n + "s", str).replace(' ', '0');
- }
-
- public static String toSuffix(int num, int length) {
- return "-" + padLeftZeros(String.valueOf(num), length);
- }
-
- public static int amountOf(List list) {
- if (list == null) {
- return 0;
- }
- return list.size();
- }
-
- public static int amountOf(Iterator iterator) {
- if (iterator == null) return 0;
- int count = 0;
- while (iterator.hasNext()) {
- iterator.next();
- count++;
- }
- return count;
- }
-}
diff --git a/hugegraph-pd/hg-pd-clitools/pom.xml b/hugegraph-pd/hg-pd-clitools/pom.xml
deleted file mode 100644
index d408b45ba..000000000
--- a/hugegraph-pd/hg-pd-clitools/pom.xml
+++ /dev/null
@@ -1,74 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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
-
- 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.
- -->
-
-<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://maven.apache.org/POM/4.0.0"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>hugegraph-pd</artifactId>
- <groupId>org.apache.hugegraph</groupId>
- <version>${revision}</version>
- <relativePath>../pom.xml</relativePath>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>hg-pd-clitools</artifactId>
- <dependencies>
- <dependency>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hg-pd-client</artifactId>
- <version>${revision}</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.13.2</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
-
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-assembly-plugin</artifactId>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- <configuration>
- <archive>
- <manifest>
- <mainClass>
- org.apache.hugegraph.pd.clitools.Main
- </mainClass>
- </manifest>
- </archive>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
-</project>
diff --git a/hugegraph-pd/hg-pd-clitools/src/main/java/org/apache/hugegraph/pd/clitools/Main.java b/hugegraph-pd/hg-pd-clitools/src/main/java/org/apache/hugegraph/pd/clitools/Main.java
deleted file mode 100644
index f1db9cc8d..000000000
--- a/hugegraph-pd/hg-pd-clitools/src/main/java/org/apache/hugegraph/pd/clitools/Main.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.clitools;
-
-import org.apache.hugegraph.pd.client.PDClient;
-import org.apache.hugegraph.pd.client.PDConfig;
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.grpc.Metapb;
-
-public class Main {
-
- public static void main(String[] args) throws PDException {
- if (args.length < 3) {
- String error = " usage: pd-address config key[=value] \n key list: " +
- "\n\tenableBatchLoad";
- System.out.println(error);
- System.exit(0);
- }
- String pd = args[0];
- String cmd = args[1];
- String param = args[2];
- System.out.println(pd + " " + cmd + " " + param);
- System.out.println("Result: \n");
- switch (cmd) {
- case "config":
- doConfig(pd, param);
- case "change_raft":
- doChangeRaft(pd, param);
- }
- }
-
- private static void doChangeRaft(String pd, String param) throws PDException {
- PDClient pdClient = PDClient.create(PDConfig.of(pd));
- pdClient.updatePdRaft(param);
- }
-
- public static void doConfig(String pd, String param) throws PDException {
- PDClient pdClient = PDClient.create(PDConfig.of(pd));
- String[] pair = param.split("=");
- String key = pair[0].trim();
- Object value = null;
- if (pair.length > 1) {
- value = pair[1].trim();
- }
- if (value == null) {
- Metapb.PDConfig pdConfig = pdClient.getPDConfig();
- switch (key) {
- case "enableBatchLoad":
- // value = pdConfig.getEnableBatchLoad();
- break;
- case "shardCount":
- value = pdConfig.getShardCount();
- break;
- }
-
- System.out.println("Get config " + key + "=" + value);
- } else {
- Metapb.PDConfig.Builder builder = Metapb.PDConfig.newBuilder();
- switch (key) {
- case "enableBatchLoad":
- // builder.setEnableBatchLoad(Boolean.valueOf((String)value));
- case "shardCount":
- builder.setShardCount(Integer.valueOf((String) value));
- }
- pdClient.setPDConfig(builder.build());
- System.out.println("Set config " + key + "=" + value);
- }
- }
-
-}
diff --git a/hugegraph-pd/hg-pd-clitools/src/test/java/org/apache/hugegraph/pd/clitools/MainTest.java b/hugegraph-pd/hg-pd-clitools/src/test/java/org/apache/hugegraph/pd/clitools/MainTest.java
deleted file mode 100644
index 4fa7eaebb..000000000
--- a/hugegraph-pd/hg-pd-clitools/src/test/java/org/apache/hugegraph/pd/clitools/MainTest.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.clitools;
-
-import java.util.Arrays;
-import java.util.List;
-
-import org.apache.hugegraph.pd.common.PDException;
-
-public class MainTest {
- public static boolean test2sup(List<Integer> arrays, int tail, int res) {
- System.out.printf("%d %d%n", tail, res);
- if (tail == 0) {
- System.out.printf("a = %d %d%n", tail, res);
- return false;
- } else if (tail == 1) {
- System.out.printf("b = %d %d%n", arrays.get(0), res);
- return (arrays.get(0) == res);
- } else if (tail == 2) {
- System.out.printf("c = %d %d %d%n", arrays.get(0), arrays.get(1), res);
- return (arrays.get(0) + arrays.get(1) == Math.abs(res)) ||
- (Math.abs(arrays.get(0) - arrays.get(1)) == Math.abs(res));
- } else {
- return test2sup(arrays, tail - 1, res + arrays.get(tail - 1)) ||
- test2sup(arrays, tail - 1, res - arrays.get(tail - 1));
- }
- }
-
- // @Test
- public void getConfig() throws PDException {
- Main.main(new String[]{"127.0.0.1:8686", "config", "enableBatchLoad"});
- }
-
- // @Test
- public void setBatchTrue() throws PDException {
- Main.main(new String[]{"127.0.0.1:8686", "config", "enableBatchLoad= true "});
- }
-
- // @Test
- public void setBatchFalse() throws PDException {
- Main.main(new String[]{"127.0.0.1:8686", "config", "enableBatchLoad=false"});
- }
-
- // @Test
- public void getConfig2() throws PDException {
- Main.main(new String[]{"127.0.0.1:8686", "config", "shardCount"});
- }
-
- // @Test
- public void setShardCount1() throws PDException {
- Main.main(new String[]{"127.0.0.1:8686", "config", "shardCount=1"});
- }
-
- // @Test
- public void setShardCount3() throws PDException {
- Main.main(new String[]{"127.0.0.1:8686", "config", "shardCount=3"});
- }
-
- // @Test
- public void test2() {
- Integer[] a = new Integer[]{1, 0, 3, 2};
- List<Integer> aa = Arrays.asList(a);
- System.out.printf(test2sup(aa, aa.size(), 0) ? "TRUE" : "FALSE");
- }
-}
diff --git a/hugegraph-pd/hg-pd-common/pom.xml b/hugegraph-pd/hg-pd-common/pom.xml
deleted file mode 100644
index 199766009..000000000
--- a/hugegraph-pd/hg-pd-common/pom.xml
+++ /dev/null
@@ -1,54 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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
-
- 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.
- -->
-
-<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://maven.apache.org/POM/4.0.0"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hugegraph-pd</artifactId>
- <version>${revision}</version>
- <relativePath>../pom.xml</relativePath>
- </parent>
- <artifactId>hg-pd-common</artifactId>
-
- <properties>
- <maven.compiler.source>11</maven.compiler.source>
- <maven.compiler.target>11</maven.compiler.target>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hg-pd-grpc</artifactId>
- <version>${revision}</version>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.24</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-collections4</artifactId>
- <version>4.4</version>
- </dependency>
- </dependencies>
-</project>
diff --git a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/GraphCache.java b/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/GraphCache.java
deleted file mode 100644
index 46da3b75e..000000000
--- a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/GraphCache.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.common;
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-
-import org.apache.hugegraph.pd.grpc.Metapb.Graph;
-import org.apache.hugegraph.pd.grpc.Metapb.Partition;
-
-import com.google.common.collect.RangeMap;
-import com.google.common.collect.TreeRangeMap;
-
-import lombok.Data;
-
-@Data
-public class GraphCache {
-
- private Graph graph;
- private AtomicBoolean initialized = new AtomicBoolean(false);
- private AtomicBoolean writing = new AtomicBoolean(false);
- private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
- private Map<Integer, AtomicBoolean> state = new ConcurrentHashMap<>();
- private Map<Integer, Partition> partitions = new ConcurrentHashMap<>();
- private RangeMap<Long, Integer> range = TreeRangeMap.create();
-
- public GraphCache(Graph graph) {
- this.graph = graph;
- }
-
- public GraphCache() {
- }
-
- public Partition getPartition(Integer id) {
- return partitions.get(id);
- }
-
- public Partition addPartition(Integer id, Partition p) {
- return partitions.put(id, p);
- }
-
- public Partition removePartition(Integer id) {
- return partitions.remove(id);
- }
-}
diff --git a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/HgAssert.java b/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/HgAssert.java
deleted file mode 100644
index bb1fccd02..000000000
--- a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/HgAssert.java
+++ /dev/null
@@ -1,117 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.common;
-
-import java.util.Collection;
-import java.util.Map;
-
-public final class HgAssert {
-
- public static void isTrue(boolean expression, String message) {
- if (message == null) {
- throw new IllegalArgumentException("message is null");
- }
-
- if (!expression) {
- throw new IllegalArgumentException(message);
- }
- }
-
- public static void isFalse(boolean expression, String message) {
- isTrue(!expression, message);
- }
-
- public static void isArgumentValid(byte[] bytes, String parameter) {
- isFalse(isInvalid(bytes), "The argument is invalid: " + parameter);
- }
-
- public static void isArgumentValid(String str, String parameter) {
- isFalse(isInvalid(str), "The argument is invalid: " + parameter);
- }
-
- public static void isArgumentNotNull(Object obj, String parameter) {
- isTrue(obj != null, "The argument is null: " + parameter);
- }
-
- public static void istValid(byte[] bytes, String msg) {
- isFalse(isInvalid(bytes), msg);
- }
-
- public static void isValid(String str, String msg) {
- isFalse(isInvalid(str), msg);
- }
-
- public static void isNotNull(Object obj, String msg) {
- isTrue(obj != null, msg);
- }
-
- public static boolean isContains(Object[] objs, Object obj) {
- if (objs == null || objs.length == 0 || obj == null) {
- return false;
- }
- for (Object item : objs) {
- if (obj.equals(item)) {
- return true;
- }
- }
- return false;
- }
-
- public static boolean isInvalid(String... strs) {
- if (strs == null || strs.length == 0) {
- return true;
- }
- for (String item : strs) {
- if (item == null || "".equals(item.trim())) {
- return true;
- }
- }
- return false;
- }
-
- public static boolean isInvalid(byte[] bytes) {
- return bytes == null || bytes.length == 0;
- }
-
- public static boolean isInvalid(Map<?, ?> map) {
- return map == null || map.isEmpty();
- }
-
- public static boolean isInvalid(Collection<?> list) {
- return list == null || list.isEmpty();
- }
-
- public static <T> boolean isContains(Collection<T> list, T item) {
- if (list == null || item == null) {
- return false;
- }
- return list.contains(item);
- }
-
- public static boolean isNull(Object... objs) {
- if (objs == null) {
- return true;
- }
- for (Object item : objs) {
- if (item == null) {
- return true;
- }
- }
- return false;
- }
-}
\ No newline at end of file
diff --git a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/KVPair.java b/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/KVPair.java
deleted file mode 100644
index b560911ea..000000000
--- a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/KVPair.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.common;
-
-import java.io.Serializable;
-import java.util.Objects;
-
-public class KVPair<K, V> implements Serializable {
-
- /**
- * Key of this <code>Pair</code>.
- */
- private K key;
- /**
- * Value of this this <code>Pair</code>.
- */
- private V value;
-
- /**
- * Creates a new pair
- *
- * @param key The key for this pair
- * @param value The value to use for this pair
- */
- public KVPair(K key, V value) {
- this.key = key;
- this.value = value;
- }
-
- /**
- * Gets the key for this pair.
- *
- * @return key for this pair
- */
- public K getKey() {
- return key;
- }
-
- public void setKey(K key) {
- this.key = key;
- }
-
- /**
- * Gets the value for this pair.
- *
- * @return value for this pair
- */
- public V getValue() {
- return value;
- }
-
- public void setValue(V value) {
- this.value = value;
- }
-
- /**
- * <p><code>String</code> representation of this
- * <code>Pair</code>.</p>
- *
- * <p>The default name/value delimiter '=' is always used.</p>
- *
- * @return <code>String</code> representation of this <code>Pair</code>
- */
- @Override
- public String toString() {
- return key + "=" + value;
- }
-
- /**
- * <p>Generate a hash code for this <code>Pair</code>.</p>
- *
- * <p>The hash code is calculated using both the name and
- * the value of the <code>Pair</code>.</p>
- *
- * @return hash code for this <code>Pair</code>
- */
- @Override
- public int hashCode() {
- // name's hashCode is multiplied by an arbitrary prime number (13)
- // in order to make sure there is a difference in the hashCode between
- // these two parameters:
- // name: a value: aa
- // name: aa value: a
- return key.hashCode() * 13 + (value == null ? 0 : value.hashCode());
- }
-
- /**
- * <p>Test this <code>Pair</code> for equality with another
- * <code>Object</code>.</p>
- *
- * <p>If the <code>Object</code> to be tested is not a
- * <code>Pair</code> or is <code>null</code>, then this method
- * returns <code>false</code>.</p>
- *
- * <p>Two <code>Pair</code>s are considered equal if and only if
- * both the names and values are equal.</p>
- *
- * @param o the <code>Object</code> to test for
- * equality with this <code>Pair</code>
- * @return <code>true</code> if the given <code>Object</code> is
- * equal to this <code>Pair</code> else <code>false</code>
- */
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o instanceof KVPair) {
- KVPair pair = (KVPair) o;
- if (!Objects.equals(key, pair.key)) {
- return false;
- }
- return Objects.equals(value, pair.value);
- }
- return false;
- }
-}
\ No newline at end of file
diff --git a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PDException.java b/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PDException.java
deleted file mode 100644
index 3efc7d138..000000000
--- a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PDException.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.common;
-
-public class PDException extends Exception {
-
- private final int errorCode;
-
- public PDException(int error) {
- super(String.format("Error code = %d", error));
- this.errorCode = error;
- }
-
- public PDException(int error, String msg) {
- super(msg);
- this.errorCode = error;
- }
-
- public PDException(int error, Throwable e) {
- super(e);
- this.errorCode = error;
- }
-
- public PDException(int error, String msg, Throwable e) {
- super(msg, e);
- this.errorCode = error;
- }
-
- public int getErrorCode() {
- return errorCode;
- }
-}
diff --git a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PDRuntimeException.java b/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PDRuntimeException.java
deleted file mode 100644
index c7efb8403..000000000
--- a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PDRuntimeException.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.common;
-
-public class PDRuntimeException extends RuntimeException {
-
- // public static final int LICENSE_ERROR = -11;
-
- private int errorCode = 0;
-
- public PDRuntimeException(int error) {
- super(String.format("Error code = %d", error));
- this.errorCode = error;
- }
-
- public PDRuntimeException(int error, String msg) {
- super(msg);
- this.errorCode = error;
- }
-
- public PDRuntimeException(int error, Throwable e) {
- super(e);
- this.errorCode = error;
- }
-
- public PDRuntimeException(int error, String msg, Throwable e) {
- super(msg, e);
- this.errorCode = error;
- }
-
- public int getErrorCode() {
- return errorCode;
- }
-}
diff --git a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PartitionCache.java b/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PartitionCache.java
deleted file mode 100644
index 7c9199dd0..000000000
--- a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PartitionCache.java
+++ /dev/null
@@ -1,460 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.common;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-
-import org.apache.hugegraph.pd.grpc.Metapb;
-
-import com.google.common.collect.Range;
-import com.google.common.collect.RangeMap;
-import com.google.common.collect.TreeRangeMap;
-
-/**
- * 放弃 copy on write 的方式
- * 1. 在 graph * partition 数量极多的时候,效率严重下降,不能用
- */
-public class PartitionCache {
-
- // 读写锁对象
- private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
- Lock writeLock = readWriteLock.writeLock();
- // 每张图一个缓存
- private volatile Map<String, RangeMap<Long, Integer>> keyToPartIdCache;
- // graphName + PartitionID 组成 key
- private volatile Map<String, Map<Integer, Metapb.Partition>> partitionCache;
-
- private volatile Map<Integer, Metapb.ShardGroup> shardGroupCache;
- private volatile Map<Long, Metapb.Store> storeCache;
- private volatile Map<String, Metapb.Graph> graphCache;
-
- private final Map<String, AtomicBoolean> locks = new HashMap<>();
-
- public PartitionCache() {
- keyToPartIdCache = new HashMap<>();
- partitionCache = new HashMap<>();
- shardGroupCache = new ConcurrentHashMap<>();
- storeCache = new ConcurrentHashMap<>();
- graphCache = new ConcurrentHashMap<>();
- }
-
- private AtomicBoolean getOrCreateGraphLock(String graphName) {
- var lock = this.locks.get(graphName);
- if (lock == null) {
- try {
- writeLock.lock();
- if ((lock = this.locks.get(graphName)) == null) {
- lock = new AtomicBoolean();
- locks.put(graphName, lock);
- }
- } finally {
- writeLock.unlock();
- }
- }
- return lock;
- }
-
- public void waitGraphLock(String graphName) {
- var lock = getOrCreateGraphLock(graphName);
- while (lock.get()) {
- Thread.onSpinWait();
- }
- }
-
- public void lockGraph(String graphName) {
- var lock = getOrCreateGraphLock(graphName);
- while (lock.compareAndSet(false, true)) {
- Thread.onSpinWait();
- }
- }
-
- public void unlockGraph(String graphName) {
- var lock = getOrCreateGraphLock(graphName);
- lock.set(false);
- }
-
- /**
- * 根据 partitionId 返回分区信息
- *
- * @param graphName
- * @param partId
- * @return
- */
- public KVPair<Metapb.Partition, Metapb.Shard> getPartitionById(String graphName, int partId) {
- waitGraphLock(graphName);
- var graphs = partitionCache.get(graphName);
- if (graphs != null) {
- var partition = graphs.get(partId);
- if (partition != null) {
- return new KVPair<>(partition, getLeaderShard(partId));
- }
- }
-
- return null;
- }
-
- /**
- * 返回 key 所在的分区信息
- *
- * @param key
- * @return
- */
- public KVPair<Metapb.Partition, Metapb.Shard> getPartitionByKey(String graphName, byte[] key) {
- int code = PartitionUtils.calcHashcode(key);
- return getPartitionByCode(graphName, code);
- }
-
- /**
- * 根据 key 的 hashcode 返回分区信息
- *
- * @param graphName
- * @param code
- * @return
- */
- public KVPair<Metapb.Partition, Metapb.Shard> getPartitionByCode(String graphName, long code) {
- waitGraphLock(graphName);
- RangeMap<Long, Integer> rangeMap = keyToPartIdCache.get(graphName);
- if (rangeMap != null) {
- Integer partId = rangeMap.get(code);
- if (partId != null) {
- return getPartitionById(graphName, partId);
- }
- }
- return null;
- }
-
- public List<Metapb.Partition> getPartitions(String graphName) {
- waitGraphLock(graphName);
-
- List<Metapb.Partition> partitions = new ArrayList<>();
- if (!partitionCache.containsKey(graphName)) {
- return partitions;
- }
- partitionCache.get(graphName).forEach((k, v) -> {
- partitions.add(v);
- });
-
- return partitions;
- }
-
- public boolean addPartition(String graphName, int partId, Metapb.Partition partition) {
- waitGraphLock(graphName);
- Metapb.Partition old = null;
-
- if (partitionCache.containsKey(graphName)) {
- old = partitionCache.get(graphName).get(partId);
- }
-
- if (old != null && old.equals(partition)) {
- return false;
- }
- try {
-
- lockGraph(graphName);
-
- partitionCache.computeIfAbsent(graphName, k -> new HashMap<>()).put(partId, partition);
-
- if (old != null) {
- // old [1-3) 被 [2-3) 覆盖了。当 [1-3) 变成 [1-2) 不应该删除原先的 [1-3)
- // 当确认老的 start, end 都是自己的时候,才可以删除老的。(即还没覆盖)
- var graphRange = keyToPartIdCache.get(graphName);
- if (Objects.equals(partition.getId(), graphRange.get(partition.getStartKey())) &&
- Objects.equals(partition.getId(), graphRange.get(partition.getEndKey() - 1))) {
- graphRange.remove(graphRange.getEntry(partition.getStartKey()).getKey());
- }
- }
-
- keyToPartIdCache.computeIfAbsent(graphName, k -> TreeRangeMap.create())
- .put(Range.closedOpen(partition.getStartKey(),
- partition.getEndKey()), partId);
- } finally {
- unlockGraph(graphName);
- }
- return true;
- }
-
- public void updatePartition(String graphName, int partId, Metapb.Partition partition) {
- try {
- lockGraph(graphName);
- Metapb.Partition old = null;
- var graphs = partitionCache.get(graphName);
- if (graphs != null) {
- old = graphs.get(partId);
- }
-
- if (old != null) {
- var graphRange = keyToPartIdCache.get(graphName);
- if (Objects.equals(partition.getId(), graphRange.get(partition.getStartKey())) &&
- Objects.equals(partition.getId(), graphRange.get(partition.getEndKey() - 1))) {
- graphRange.remove(graphRange.getEntry(partition.getStartKey()).getKey());
- }
- }
-
- partitionCache.computeIfAbsent(graphName, k -> new HashMap<>()).put(partId, partition);
- keyToPartIdCache.computeIfAbsent(graphName, k -> TreeRangeMap.create())
- .put(Range.closedOpen(partition.getStartKey(), partition.getEndKey()),
- partId);
- } finally {
- unlockGraph(graphName);
- }
- }
-
- public boolean updatePartition(Metapb.Partition partition) {
-
- var graphName = partition.getGraphName();
- var partitionId = partition.getId();
-
- var old = getPartitionById(graphName, partitionId);
- if (old != null && Objects.equals(partition, old.getKey())) {
- return false;
- }
-
- updatePartition(graphName, partitionId, partition);
- return true;
- }
-
- public void removePartition(String graphName, int partId) {
- try {
- lockGraph(graphName);
- var partition = partitionCache.get(graphName).remove(partId);
- if (partition != null) {
- var graphRange = keyToPartIdCache.get(graphName);
-
- if (Objects.equals(partition.getId(), graphRange.get(partition.getStartKey())) &&
- Objects.equals(partition.getId(), graphRange.get(partition.getEndKey() - 1))) {
- graphRange.remove(graphRange.getEntry(partition.getStartKey()).getKey());
- }
- }
- } finally {
- unlockGraph(graphName);
- }
- }
-
- /**
- * remove partition id of graph name
- *
- * @param graphName
- * @param id
- */
- public void remove(String graphName, int id) {
- removePartition(graphName, id);
- }
-
- /**
- * remove all partitions
- */
- public void removePartitions() {
- writeLock.lock();
- try {
- partitionCache = new HashMap<>();
- keyToPartIdCache = new HashMap<>();
- locks.clear();
- } finally {
- writeLock.unlock();
- }
- }
-
- /**
- * remove partition cache of graphName
- *
- * @param graphName
- */
- public void removeAll(String graphName) {
- try {
- lockGraph(graphName);
- partitionCache.remove(graphName);
- keyToPartIdCache.remove(graphName);
- locks.remove(graphName);
- } finally {
- unlockGraph(graphName);
- }
- }
-
- private String makePartitionKey(String graphName, int partId) {
- return graphName + "/" + partId;
- }
-
- public boolean updateShardGroup(Metapb.ShardGroup shardGroup) {
- Metapb.ShardGroup oldShardGroup = shardGroupCache.get(shardGroup.getId());
- if (oldShardGroup != null && oldShardGroup.equals(shardGroup)) {
- return false;
- }
- shardGroupCache.put(shardGroup.getId(), shardGroup);
- return true;
- }
-
- public void deleteShardGroup(int shardGroupId) {
- shardGroupCache.remove(shardGroupId);
- }
-
- public Metapb.ShardGroup getShardGroup(int groupId) {
- return shardGroupCache.get(groupId);
- }
-
- public boolean addStore(Long storeId, Metapb.Store store) {
- Metapb.Store oldStore = storeCache.get(storeId);
- if (oldStore != null && oldStore.equals(store)) {
- return false;
- }
- storeCache.put(storeId, store);
- return true;
- }
-
- public Metapb.Store getStoreById(Long storeId) {
- return storeCache.get(storeId);
- }
-
- public void removeStore(Long storeId) {
- storeCache.remove(storeId);
- }
-
- public boolean hasGraph(String graphName) {
- return getPartitions(graphName).size() > 0;
- }
-
- public void updateGraph(Metapb.Graph graph) {
- if (Objects.equals(graph, getGraph(graph.getGraphName()))) {
- return;
- }
- graphCache.put(graph.getGraphName(), graph);
- }
-
- public Metapb.Graph getGraph(String graphName) {
- return graphCache.get(graphName);
- }
-
- public List<Metapb.Graph> getGraphs() {
- List<Metapb.Graph> graphs = new ArrayList<>();
- graphCache.forEach((k, v) -> {
- graphs.add(v);
- });
- return graphs;
- }
-
- public void reset() {
- writeLock.lock();
- try {
- partitionCache = new HashMap<>();
- keyToPartIdCache = new HashMap<>();
- shardGroupCache = new ConcurrentHashMap<>();
- storeCache = new ConcurrentHashMap<>();
- graphCache = new ConcurrentHashMap<>();
- locks.clear();
- } finally {
- writeLock.unlock();
- }
- }
-
- public void clear() {
- reset();
- }
-
- public String debugCacheByGraphName(String graphName) {
- StringBuilder builder = new StringBuilder();
- builder.append("Graph:").append(graphName).append(", cache info: range info: {");
- var rangeMap = keyToPartIdCache.get(graphName);
- builder.append(rangeMap == null ? "" : rangeMap).append("}");
-
- if (rangeMap != null) {
- builder.append(", partition info : {");
- rangeMap.asMapOfRanges().forEach((k, v) -> {
- var partition = partitionCache.get(graphName).get(v);
- builder.append("[part_id:").append(v);
- if (partition != null) {
- builder.append(", start_key:").append(partition.getStartKey())
- .append(", end_key:").append(partition.getEndKey())
- .append(", state:").append(partition.getState().name());
- }
- builder.append("], ");
- });
- builder.append("}");
- }
-
- builder.append(", graph info:{");
- var graph = graphCache.get(graphName);
- if (graph != null) {
- builder.append("partition_count:").append(graph.getPartitionCount())
- .append(", state:").append(graph.getState().name());
- }
- builder.append("}]");
- return builder.toString();
- }
-
- public Metapb.Shard getLeaderShard(int partitionId) {
- var shardGroup = shardGroupCache.get(partitionId);
- if (shardGroup != null) {
- for (Metapb.Shard shard : shardGroup.getShardsList()) {
- if (shard.getRole() == Metapb.ShardRole.Leader) {
- return shard;
- }
- }
- }
-
- return null;
- }
-
- public void updateShardGroupLeader(int partitionId, Metapb.Shard leader) {
- if (shardGroupCache.containsKey(partitionId) && leader != null) {
- if (!Objects.equals(getLeaderShard(partitionId), leader)) {
- var shardGroup = shardGroupCache.get(partitionId);
- var builder = Metapb.ShardGroup.newBuilder(shardGroup).clearShards();
- for (var shard : shardGroup.getShardsList()) {
- builder.addShards(
- Metapb.Shard.newBuilder()
- .setStoreId(shard.getStoreId())
- .setRole(shard.getStoreId() == leader.getStoreId() ?
- Metapb.ShardRole.Leader :
- Metapb.ShardRole.Follower)
- .build()
- );
- }
- shardGroupCache.put(partitionId, builder.build());
- }
- }
- }
-
- public String debugShardGroup() {
- StringBuilder builder = new StringBuilder();
- builder.append("shard group cache:{");
- shardGroupCache.forEach((partitionId, shardGroup) -> {
- builder.append(partitionId).append("::{")
- .append("version:").append(shardGroup.getVersion())
- .append(", conf_version:").append(shardGroup.getConfVer())
- .append(", state:").append(shardGroup.getState().name())
- .append(", shards:[");
-
- for (var shard : shardGroup.getShardsList()) {
- builder.append("{store_id:").append(shard.getStoreId())
- .append(", role:").append(shard.getRole().name())
- .append("},");
- }
- builder.append("], ");
- });
- builder.append("}");
- return builder.toString();
- }
-}
diff --git a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PartitionUtils.java b/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PartitionUtils.java
deleted file mode 100644
index 869a68693..000000000
--- a/hugegraph-pd/hg-pd-common/src/main/java/org/apache/hugegraph/pd/common/PartitionUtils.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd.common;
-
-public class PartitionUtils {
-
- public static final int MAX_VALUE = 0xffff;
-
- /**
- * 计算key的hashcode
- *
- * @param key
- * @return hashcode
- */
- public static int calcHashcode(byte[] key) {
- final int p = 16777619;
- int hash = (int) 2166136261L;
- for (byte element : key) {
- hash = (hash ^ element) * p;
- }
- hash += hash << 13;
- hash ^= hash >> 7;
- hash += hash << 3;
- hash ^= hash >> 17;
- hash += hash << 5;
- hash = hash & PartitionUtils.MAX_VALUE;
- if (hash == PartitionUtils.MAX_VALUE) {
- hash = PartitionUtils.MAX_VALUE - 1;
- }
- return hash;
- }
-}
diff --git a/hugegraph-pd/hg-pd-core/pom.xml b/hugegraph-pd/hg-pd-core/pom.xml
deleted file mode 100644
index 374d9a93c..000000000
--- a/hugegraph-pd/hg-pd-core/pom.xml
+++ /dev/null
@@ -1,94 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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
-
- 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.
- -->
-
-<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://maven.apache.org/POM/4.0.0"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hugegraph-pd</artifactId>
- <version>${revision}</version>
- <relativePath>../pom.xml</relativePath>
- </parent>
-
- <artifactId>hg-pd-core</artifactId>
-
- <properties>
- <jetcd-version>0.5.10</jetcd-version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>com.alipay.sofa</groupId>
- <artifactId>jraft-core</artifactId>
- <!-- TODO: use open source version & adopt the code later -->
- <version>1.3.13</version>
- <exclusions>
- <exclusion>
- <groupId>org.rocksdb</groupId>
- <artifactId>rocksdbjni</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.rocksdb</groupId>
- <artifactId>rocksdbjni</artifactId>
- <version>6.29.5</version>
- </dependency>
- <dependency>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hg-pd-grpc</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>5.3.20</version>
- </dependency>
- <dependency>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hg-pd-common</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot</artifactId>
- <version>2.5.14</version>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.24</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.12.0</version>
- </dependency>
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.8.9</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.hugegraph</groupId>
- <artifactId>hg-store-grpc</artifactId>
- <version>${revision}</version>
- </dependency>
- </dependencies>
-</project>
diff --git a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/ConfigService.java b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/ConfigService.java
deleted file mode 100644
index 197e15510..000000000
--- a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/ConfigService.java
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd;
-
-import java.util.List;
-
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.config.PDConfig;
-import org.apache.hugegraph.pd.grpc.Metapb;
-import org.apache.hugegraph.pd.meta.ConfigMetaStore;
-import org.apache.hugegraph.pd.meta.MetadataFactory;
-import org.apache.hugegraph.pd.raft.RaftStateListener;
-
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-public class ConfigService implements RaftStateListener {
-
- private final ConfigMetaStore meta;
- private PDConfig pdConfig;
-
- public ConfigService(PDConfig config) {
- this.pdConfig = config;
- config.setConfigService(this);
- meta = MetadataFactory.newConfigMeta(config);
- }
-
-
- public Metapb.PDConfig getPDConfig(long version) throws PDException {
- return this.meta.getPdConfig(version);
- }
-
- public Metapb.PDConfig getPDConfig() throws PDException {
- return this.meta.getPdConfig(0);
- }
-
- public Metapb.PDConfig setPDConfig(Metapb.PDConfig mConfig) throws PDException {
- Metapb.PDConfig oldCfg = getPDConfig();
- Metapb.PDConfig.Builder builder = oldCfg.toBuilder().mergeFrom(mConfig)
- .setVersion(oldCfg.getVersion() + 1)
- .setTimestamp(System.currentTimeMillis());
- mConfig = this.meta.setPdConfig(builder.build());
- log.info("PDConfig has been modified, new PDConfig is {}", mConfig);
- updatePDConfig(mConfig);
- return mConfig;
- }
-
- public List<Metapb.GraphSpace> getGraphSpace(String graphSpaceName) throws PDException {
- return this.meta.getGraphSpace(graphSpaceName);
- }
-
- public Metapb.GraphSpace setGraphSpace(Metapb.GraphSpace graphSpace) throws PDException {
- return this.meta.setGraphSpace(graphSpace.toBuilder()
- .setTimestamp(System.currentTimeMillis())
- .build());
- }
-
- /**
- * 从存储中读取配置项,并覆盖全局的PDConfig对象
- *
- * @return
- */
- public PDConfig loadConfig() {
- try {
- Metapb.PDConfig mConfig = this.meta.getPdConfig(0);
- if (mConfig == null) {
- mConfig = Metapb.PDConfig.newBuilder()
- .setPartitionCount(pdConfig.getInitialPartitionCount())
- .setShardCount(pdConfig.getPartition().getShardCount())
- .setVersion(1)
- .setTimestamp(System.currentTimeMillis())
- .setMaxShardsPerStore(
- pdConfig.getPartition().getMaxShardsPerStore())
- .build();
- this.meta.setPdConfig(mConfig);
- }
- pdConfig = updatePDConfig(mConfig);
- } catch (Exception e) {
- log.error("ConfigService loadConfig exception {}", e);
- }
- return pdConfig;
- }
-
- public synchronized PDConfig updatePDConfig(Metapb.PDConfig mConfig) {
- log.info("update pd config: mConfig:{}", mConfig);
- pdConfig.getPartition().setShardCount(mConfig.getShardCount());
- pdConfig.getPartition().setTotalCount(mConfig.getPartitionCount());
- pdConfig.getPartition().setMaxShardsPerStore(mConfig.getMaxShardsPerStore());
- return pdConfig;
- }
-
- public synchronized PDConfig setPartitionCount(int count) {
- Metapb.PDConfig mConfig = null;
- try {
- mConfig = getPDConfig();
- mConfig = mConfig.toBuilder().setPartitionCount(count).build();
- setPDConfig(mConfig);
- } catch (PDException e) {
- log.error("ConfigService exception {}", e);
- e.printStackTrace();
- }
- return pdConfig;
- }
-
- /**
- * meta store中的数量
- * 由于可能会受分区分裂/合并的影响,原始的partition count不推荐使用
- *
- * @return partition count of cluster
- * @throws PDException when io error
- */
- public int getPartitionCount() throws PDException {
- return getPDConfig().getPartitionCount();
- }
-
- @Override
- public void onRaftLeaderChanged() {
- loadConfig();
- }
-}
diff --git a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/IdService.java b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/IdService.java
deleted file mode 100644
index 2e4a63603..000000000
--- a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/IdService.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd;
-
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.config.PDConfig;
-import org.apache.hugegraph.pd.meta.IdMetaStore;
-import org.apache.hugegraph.pd.meta.MetadataFactory;
-
-public class IdService {
-
- private final IdMetaStore meta;
- private PDConfig pdConfig;
-
- public IdService(PDConfig config) {
- this.pdConfig = config;
- meta = MetadataFactory.newHugeServerMeta(config);
- }
-
- public PDConfig getPdConfig() {
- return pdConfig;
- }
-
- public void setPdConfig(PDConfig pdConfig) {
- this.pdConfig = pdConfig;
- }
-
- public long getId(String key, int delta) throws PDException {
- return meta.getId(key, delta);
- }
-
- public void resetId(String key) throws PDException {
- meta.resetId(key);
- }
-
- /**
- * 获取自增循环不重复id, 达到上限后从0开始自增.自动跳过正在使用的cid
- *
- * @param key
- * @param max
- * @return
- * @throws PDException
- */
- public long getCId(String key, long max) throws PDException {
- return meta.getCId(key, max);
- }
-
- public long getCId(String key, String name, long max) throws PDException {
- return meta.getCId(key, name, max);
- }
-
- /**
- * 删除一个自增循环id
- *
- * @param key
- * @param cid
- * @return
- * @throws PDException
- */
- public long delCId(String key, long cid) throws PDException {
- return meta.delCId(key, cid);
- }
-
- public long delCIdDelay(String key, String name, long cid) throws PDException {
- return meta.delCIdDelay(key, name, cid);
- }
-}
diff --git a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/KvService.java b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/KvService.java
deleted file mode 100644
index 5c8ef3c8d..000000000
--- a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/KvService.java
+++ /dev/null
@@ -1,317 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd;
-
-import java.nio.charset.Charset;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.config.PDConfig;
-import org.apache.hugegraph.pd.grpc.kv.Kv;
-import org.apache.hugegraph.pd.grpc.kv.V;
-import org.apache.hugegraph.pd.meta.MetadataKeyHelper;
-import org.apache.hugegraph.pd.meta.MetadataRocksDBStore;
-import org.apache.hugegraph.pd.store.KV;
-import org.springframework.stereotype.Service;
-
-import com.google.protobuf.InvalidProtocolBufferException;
-
-import lombok.extern.slf4j.Slf4j;
-
-/**
- *
- **/
-@Slf4j
-@Service
-public class KvService {
-
-
- public static final char KV_DELIMITER = '@';
- // TODO 主前缀之后,增加类名做区分
- private static final String TTL_PREFIX = "T";
- private static final String KV_PREFIX = "K";
- private static final String LOCK_PREFIX = "L";
- private static final String KV_PREFIX_DELIMITER = KV_PREFIX + KV_DELIMITER;
- private static final byte[] EMPTY_VALUE = new byte[0];
- private final MetadataRocksDBStore meta;
- private PDConfig pdConfig;
-
- public KvService(PDConfig config) {
- this.pdConfig = config;
- meta = new MetadataRocksDBStore(config);
- }
-
- public static String getKey(Object... keys) {
- StringBuilder builder = MetadataKeyHelper.getStringBuilderHelper();
- builder.append(KV_PREFIX).append(KV_DELIMITER);
- for (Object key : keys) {
- builder.append(key == null ? "" : key).append(KV_DELIMITER);
- }
- return builder.substring(0, builder.length() - 1);
- }
-
- public static byte[] getKeyBytes(Object... keys) {
- String key = getKey(keys);
- return key.getBytes(Charset.defaultCharset());
- }
-
- public static String getKeyWithoutPrefix(Object... keys) {
- StringBuilder builder = MetadataKeyHelper.getStringBuilderHelper();
- for (Object key : keys) {
- builder.append(key == null ? "" : key).append(KV_DELIMITER);
- }
- return builder.substring(0, builder.length() - 1);
- }
-
- public static String getDelimiter() {
- return String.valueOf(KV_DELIMITER);
- }
-
- public PDConfig getPdConfig() {
- return pdConfig;
- }
-
- public void setPdConfig(PDConfig pdConfig) {
- this.pdConfig = pdConfig;
- }
-
- public void put(String key, String value) throws PDException {
- V storeValue = V.newBuilder().setValue(value).setTtl(0).build();
- meta.put(getStoreKey(key), storeValue.toByteArray());
- // log.warn("add key with key-{}:value-{}", key, value);
- }
-
- public void put(String key, String value, long ttl) throws PDException {
- long curTime = System.currentTimeMillis();
- curTime += ttl;
- V storeValue = V.newBuilder().setValue(value).setSt(ttl).setTtl(curTime).build();
- meta.put(getStoreKey(key), storeValue.toByteArray());
- meta.put(getTTLStoreKey(key, curTime), EMPTY_VALUE);
- // log.warn("add key with key-{}:value-{}:ttl-{}", key, value, ttl);
- }
-
- public String get(String key) throws PDException {
- byte[] storeKey = getStoreKey(key);
- return get(storeKey);
- }
-
- public String get(byte[] keyBytes) throws PDException {
- byte[] bytes = meta.getOne(keyBytes);
- String v = getValue(keyBytes, bytes);
- return v;
- }
-
- private String getValue(byte[] keyBytes, byte[] valueBytes) throws PDException {
- if (valueBytes == null || valueBytes.length == 0) {
- return "";
- }
- try {
- V v = V.parseFrom(valueBytes);
- if (v.getTtl() == 0 || v.getTtl() >= System.currentTimeMillis()) {
- return v.getValue();
- } else {
- meta.remove(keyBytes);
- meta.remove(getTTLStoreKey(new String(keyBytes), v.getTtl()));
- }
- } catch (Exception e) {
- log.error("parse value with error:{}", e.getMessage());
- throw new PDException(-1, e.getMessage());
- }
- return null;
- }
-
- public boolean keepAlive(String key) throws PDException {
- byte[] bytes = meta.getOne(getStoreKey(key));
- try {
- if (bytes == null || bytes.length == 0) {
- return false;
- }
- V v = V.parseFrom(bytes);
- if (v != null) {
- long ttl = v.getTtl();
- long st = v.getSt();
- meta.remove(getTTLStoreKey(key, ttl));
- put(key, v.getValue(), st);
- return true;
- } else {
- return false;
- }
- } catch (InvalidProtocolBufferException e) {
- throw new PDException(-1, e.getMessage());
- }
- }
-
- public Kv delete(String key) throws PDException {
- byte[] storeKey = getStoreKey(key);
- String value = this.get(storeKey);
- meta.remove(storeKey);
- Kv.Builder builder = Kv.newBuilder().setKey(key);
- if (value != null) {
- builder.setValue(value);
- }
- Kv kv = builder.build();
- // log.warn("delete kv with key :{}", key);
- return kv;
- }
-
- public List<Kv> deleteWithPrefix(String key) throws PDException {
- byte[] storeKey = getStoreKey(key);
- //TODO to many rows for scan
- List<KV> kvList = meta.scanPrefix(storeKey);
- LinkedList<Kv> kvs = new LinkedList<>();
- for (KV kv : kvList) {
- String kvKey = new String(kv.getKey()).replaceFirst(KV_PREFIX_DELIMITER, "");
- String kvValue = getValue(kv.getKey(), kv.getValue());
- if (kvValue != null) {
- kvs.add(Kv.newBuilder().setKey(kvKey).setValue(kvValue).build());
- }
- }
- meta.removeByPrefix(storeKey);
- // log.warn("delete kv with key prefix :{}", key);
- return kvs;
- }
-
- /**
- * scan result ranged from key start and key end
- *
- * @param keyStart
- * @param keyEnd
- * @return Records
- * @throws PDException
- */
- public Map<String, String> scanRange(String keyStart, String keyEnd) throws PDException {
- List<KV> list = meta.scanRange(getStoreKey(keyStart), getStoreKey(keyEnd));
- Map<String, String> map = new HashMap<>();
- for (KV kv : list) {
- String kvKey = new String(kv.getKey()).replaceFirst(KV_PREFIX_DELIMITER, "");
- String kvValue = getValue(kv.getKey(), kv.getValue());
- if (kvValue != null) {
- map.put(kvKey, kvValue);
- }
- }
- return map;
- }
-
- public Map<String, String> scanWithPrefix(String key) throws PDException {
- List<KV> kvList = meta.scanPrefix(getStoreKey(key));
- HashMap<String, String> map = new HashMap<>();
- for (KV kv : kvList) {
- String kvKey = new String(kv.getKey()).replaceFirst(KV_PREFIX_DELIMITER, "");
- String kvValue = getValue(kv.getKey(), kv.getValue());
- if (kvValue != null) {
- map.put(kvKey, kvValue);
- }
- }
- return map;
- }
-
- public boolean locked(String key) throws PDException {
- String lockKey = KvService.getKeyWithoutPrefix(KvService.LOCK_PREFIX, key);
- Map<String, String> allLock = scanWithPrefix(lockKey);
- return allLock != null && allLock.size() != 0;
- }
-
- private boolean owned(String key, long clientId) throws PDException {
- String lockKey = KvService.getKeyWithoutPrefix(KvService.LOCK_PREFIX, key);
- Map<String, String> allLock = scanWithPrefix(lockKey);
- if (allLock.size() == 0) {
- return true;
- }
- for (Map.Entry<String, String> entry : allLock.entrySet()) {
- String entryKey = entry.getKey();
- String[] split = entryKey.split(String.valueOf(KV_DELIMITER));
- if (Long.valueOf(split[split.length - 1]).equals(clientId)) {
- return true;
- }
- }
- return false;
- }
-
- public boolean lock(String key, long ttl, long clientId) throws PDException {
- //TODO lock improvement
- synchronized (KvService.class) {
- if (!owned(key, clientId)) {
- return false;
- }
- put(getLockKey(key, clientId), " ", ttl);
- return true;
- }
- }
-
- public boolean lockWithoutReentrant(String key, long ttl,
- long clientId) throws PDException {
- synchronized (KvService.class) {
- if (locked(key)) {
- return false;
- }
- put(getLockKey(key, clientId), " ", ttl);
- return true;
- }
- }
-
- public boolean unlock(String key, long clientId) throws PDException {
- synchronized (KvService.class) {
- if (!owned(key, clientId)) {
- return false;
- }
- delete(getLockKey(key, clientId));
- return true;
- }
- }
-
- public boolean keepAlive(String key, long clientId) throws PDException {
- String lockKey = getLockKey(key, clientId);
- return keepAlive(lockKey);
- }
-
- public String getLockKey(String key, long clientId) {
- return getKeyWithoutPrefix(LOCK_PREFIX, key, clientId);
- }
-
- public byte[] getStoreKey(String key) {
- return getKeyBytes(key);
- }
-
- public byte[] getTTLStoreKey(String key, long time) {
- return getKeyBytes(TTL_PREFIX, time, key);
- }
-
- public void clearTTLData() {
- try {
- byte[] ttlStartKey = getTTLStoreKey("", 0);
- byte[] ttlEndKey = getTTLStoreKey("", System.currentTimeMillis());
- List<KV> kvList = meta.scanRange(ttlStartKey, ttlEndKey);
- for (KV kv : kvList) {
- String key = new String(kv.getKey());
- int index = key.indexOf(KV_DELIMITER, 2);
- String delKey = key.substring(index + 1);
- delete(delKey);
- meta.remove(kv.getKey());
- }
- } catch (Exception e) {
- log.error("clear ttl data with error :", e);
- }
- }
-
- public MetadataRocksDBStore getMeta() {
- return meta;
- }
-}
diff --git a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/LogService.java b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/LogService.java
deleted file mode 100644
index 664b6b8de..000000000
--- a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/LogService.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd;
-
-import java.util.List;
-
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.config.PDConfig;
-import org.apache.hugegraph.pd.grpc.Metapb;
-import org.apache.hugegraph.pd.meta.LogMeta;
-import org.apache.hugegraph.pd.meta.MetadataFactory;
-import org.springframework.stereotype.Service;
-
-import com.google.protobuf.Any;
-import com.google.protobuf.GeneratedMessageV3;
-
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-@Service
-public class LogService {
-
- public static final String GRPC = "GRPC";
- public static final String REST = "REST";
- public static final String TASK = "TASK";
- public static final String NODE_CHANGE = "NODE_CHANGE";
- public static final String PARTITION_CHANGE = "PARTITION_CHANGE";
- private final LogMeta logMeta;
-
- public LogService(PDConfig pdConfig) {
- logMeta = MetadataFactory.newLogMeta(pdConfig);
- }
-
- public List<Metapb.LogRecord> getLog(String action, Long start, Long end) throws PDException {
- return logMeta.getLog(action, start, end);
- }
-
- public void insertLog(String action, String message, GeneratedMessageV3 target) {
- try {
- Metapb.LogRecord logRecord = Metapb.LogRecord.newBuilder()
- .setAction(action)
- .setMessage(message)
- .setTimestamp(System.currentTimeMillis())
- .setObject(Any.pack(target))
- .build();
- logMeta.insertLog(logRecord);
- } catch (PDException e) {
- log.debug("Insert log with error:{}", e);
- }
-
- }
-}
diff --git a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/PartitionInstructionListener.java b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/PartitionInstructionListener.java
deleted file mode 100644
index 4b7184f4b..000000000
--- a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/PartitionInstructionListener.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd;
-
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.grpc.Metapb;
-import org.apache.hugegraph.pd.grpc.pulse.ChangeShard;
-import org.apache.hugegraph.pd.grpc.pulse.CleanPartition;
-import org.apache.hugegraph.pd.grpc.pulse.DbCompaction;
-import org.apache.hugegraph.pd.grpc.pulse.MovePartition;
-import org.apache.hugegraph.pd.grpc.pulse.PartitionKeyRange;
-import org.apache.hugegraph.pd.grpc.pulse.SplitPartition;
-import org.apache.hugegraph.pd.grpc.pulse.TransferLeader;
-
-/**
- * 分区命令监听
- */
-public interface PartitionInstructionListener {
- void changeShard(Metapb.Partition partition, ChangeShard changeShard) throws PDException;
-
- void transferLeader(Metapb.Partition partition, TransferLeader transferLeader) throws
- PDException;
-
- void splitPartition(Metapb.Partition partition, SplitPartition splitPartition) throws
- PDException;
-
- void dbCompaction(Metapb.Partition partition, DbCompaction dbCompaction) throws PDException;
-
- void movePartition(Metapb.Partition partition, MovePartition movePartition) throws PDException;
-
- void cleanPartition(Metapb.Partition partition, CleanPartition cleanPartition) throws
- PDException;
-
- void changePartitionKeyRange(Metapb.Partition partition,
- PartitionKeyRange partitionKeyRange) throws PDException;
-
-}
diff --git a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/PartitionService.java b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/PartitionService.java
deleted file mode 100644
index 95c8f8832..000000000
--- a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/PartitionService.java
+++ /dev/null
@@ -1,1563 +0,0 @@
-/*
- * 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
- *
- * 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.apache.hugegraph.pd;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
-
-import org.apache.commons.lang3.StringUtils;
-import org.apache.hugegraph.pd.common.KVPair;
-import org.apache.hugegraph.pd.common.PDException;
-import org.apache.hugegraph.pd.common.PartitionUtils;
-import org.apache.hugegraph.pd.config.PDConfig;
-import org.apache.hugegraph.pd.grpc.MetaTask;
-import org.apache.hugegraph.pd.grpc.Metapb;
-import org.apache.hugegraph.pd.grpc.Pdpb;
-import org.apache.hugegraph.pd.grpc.pulse.ChangeShard;
-import org.apache.hugegraph.pd.grpc.pulse.CleanPartition;
-import org.apache.hugegraph.pd.grpc.pulse.CleanType;
-import org.apache.hugegraph.pd.grpc.pulse.ConfChangeType;
-import org.apache.hugegraph.pd.grpc.pulse.DbCompaction;
-import org.apache.hugegraph.pd.grpc.pulse.MovePartition;
-import org.apache.hugegraph.pd.grpc.pulse.PartitionKeyRange;
-import org.apache.hugegraph.pd.grpc.pulse.SplitPartition;
-import org.apache.hugegraph.pd.grpc.pulse.TransferLeader;
-import org.apache.hugegraph.pd.meta.MetadataFactory;
-import org.apache.hugegraph.pd.meta.PartitionMeta;
-import org.apache.hugegraph.pd.meta.TaskInfoMeta;
-import org.apache.hugegraph.pd.raft.RaftStateListener;
-
-import lombok.extern.slf4j.Slf4j;
-
-/**
- * 分区管理
- */
-@Slf4j
-public class PartitionService implements RaftStateListener {
-
- private final long Partition_Version_Skip = 0x0F;
- private final StoreNodeService storeService;
- private final PartitionMeta partitionMeta;
- private final PDConfig pdConfig;
- // 分区命令监听
- private final List<PartitionInstructionListener> instructionListeners;
-
- // 分区状态监听
- private final List<PartitionStatusListener> statusListeners;
-
- public PartitionService(PDConfig config, StoreNodeService storeService) {
- this.pdConfig = config;
- this.storeService = storeService;
- partitionMeta = MetadataFactory.newPartitionMeta(config);
- instructionListeners =
- Collections.synchronizedList(new ArrayList<PartitionInstructionListener>());
- statusListeners = Collections.synchronizedList(new ArrayList<PartitionStatusListener>());
- }
-
- public void init() throws PDException {
- partitionMeta.init();
- storeService.addStatusListener(new StoreStatusListener() {
- @Override
- public void onStoreStatusChanged(Metapb.Store store, Metapb.StoreState old,
- Metapb.StoreState status) {
- if (status == Metapb.StoreState.Tombstone) {
- // Store被停机,通知所有该store所有分区,迁移数据
- storeOffline(store);
- }
- }
-
- @Override
- public void onGraphChange(Metapb.Graph graph,
- Metapb.GraphState stateOld,
- Metapb.GraphState stateNew) {
-
- }
-
- @Override
- public void onStoreRaftChanged(Metapb.Store store) {
-
- }
- });
- }
-
- /**
- * 返回Key所属的partition
- *
- * @param graphName
- * @param key
- * @return
- */
- public Metapb.PartitionShard getPartitionShard(String graphName, byte[] key) throws
- PDException {
- long code = PartitionUtils.calcHashcode(key);
- return getPartitionByCode(graphName, code);
- }
-
- /**
- * 根据hashcode返回所属的partition
- *
- * @param graphName
- * @param code
- * @return
- */
- public Metapb.PartitionShard getPartitionByCode(String graphName, long code) throws
- PDException {
- if (code < 0 || code >= PartitionUtils.MAX_VALUE) {
- throw new PDException(Pdpb.ErrorType.NOT_FOUND_VALUE, "code error");
- }
- // 根据Code查找分区id,如果没有找到,创建新的分区
- Metapb.Partition partition = partitionMeta.getPartitionByCode(graphName, code);
-
- if (partition == null) {
- synchronized (this) {
- if (partition == null) {
- partition = newPartition(graphName, code);
- }
- }
- }
-
- Metapb.PartitionShard partShard = Metapb.PartitionShard.newBuilder()
- .setPartition(partition)
- .setLeader(storeService.getLeader(
- partition, 0))
- .build();
- log.debug(
- "{} Partition get code = {}, partition id = {}, start = {}, end = {}, leader = {}",
- graphName, (code), partition.getId(), partition.getStartKey(),
- partition.getEndKey(), partShard.getLeader());
-
- return partShard;
- }
-
- /**
- * 根据ID返回分区信息
- *
- * @param graphName
- * @param partId
- * @return
- * @throws PDException
- */
- public Metapb.PartitionShard getPartitionShardById(String graphName, int partId) throws
- PDException {
- Metapb.Partition partition = partitionMeta.getPartitionById(graphName, partId);
- if (partition == null) {
- return null;
- }
-
- Metapb.PartitionShard partShard = Metapb.PartitionShard.newBuilder()
- .setPartition(partition)
- // 此处需要返回正确的leader,暂时默认取第一个
- .setLeader(storeService.getLeader(
- partition, 0))
- .build();
-
- return partShard;
- }
-
- public Metapb.Partition getPartitionById(String graphName, int partId) throws PDException {
- return partitionMeta.getPartitionById(graphName, partId);
- }
-
- public List<Metapb.Partition> getPartitionById(int partId) throws PDException {
- return partitionMeta.getPartitionById(partId);
- }
-
- /**
- * 获取图的所有分区
- */
- public List<Metapb.Partition> getPartitions() {
- return partitionMeta.getPartitions();
- }
-
- public List<Metapb.Partition> getPartitions(String graphName) {
- if (StringUtils.isAllEmpty(graphName)) {
- return partitionMeta.getPartitions();
- }
- return partitionMeta.getPartitions(graphName);
- }
-
- /**
- * 查找在store上的所有分区
- *
- * @param store
- * @return
- */
- public List<Metapb.Partition> getPartitionByStore(Metapb.Store store) throws PDException {
- List<Metapb.Partition> partitions = new ArrayList<>();
- getGraphs().forEach(graph -> {
- getPartitions(graph.getGraphName()).forEach(partition -> {
- try {
- storeService.getShardGroup(partition.getId()).getShardsList().forEach(shard -> {
- if (shard.getStoreId() == store.getId()) {
- partitions.add(partition);
- }
- });
- } catch (PDException e) {
- throw new RuntimeException(e);
- }
- });
- });
- return partitions;
- }
-
- /**
- * 产生一个新的分区
- *
- * @param graphName
- * @return
- */
- private Metapb.Partition newPartition(String graphName, long code) throws PDException {
- Metapb.Graph graph = partitionMeta.getAndCreateGraph(graphName);
- int partitionSize = PartitionUtils.MAX_VALUE / graph.getPartitionCount();
- if (PartitionUtils.MAX_VALUE % graph.getPartitionCount() != 0) {
- // 有余数,分区除不尽
- partitionSize++;
- }
-
- int partitionId = (int) (code / partitionSize);
- long startKey = (long) partitionSize * partitionId;
- long endKey = (long) partitionSize * (partitionId + 1);
-
- // 检查本地
- Metapb.Partition partition = partitionMeta.getPartitionById(graphName, partitionId);
- if (partition == null) {
- storeService.allocShards(null, partitionId);
-
- // 分配store
- partition = Metapb.Partition.newBuilder()
- .setId(partitionId)
- .setVersion(0)
- .setState(Metapb.PartitionState.PState_Normal)
- .setStartKey(startKey)
- .setEndKey(endKey)
- .setGraphName(graphName)
- .build();
-
- log.info("Create newPartition {}", partition);
- }
-
- partitionMeta.updatePartition(partition);
-
- return partition;
- }
-
- /**
- * 计算Key所属的分区,此处采用Hash映射的方法。
- *
- * @param graphName
- * @param key
- * @return
- */
- protected int getPartitionId(String graphName, byte[] key) throws PDException {
- int code = PartitionUtils.calcHashcode(key);
- Metapb.Partition partition = partitionMeta.getPartitionByCode(graphName, code);
- return partition != null ? partition.getId() : -1;
- }
-
- /**
- * 获取key范围所跨越的所有分区
- * 暂时使用hashcode计算,正常做法,基于key进行查询
- *
- * @param graphName
- * @param startKey
- * @param endKey
- */
- public List<Metapb.PartitionShard> scanPartitions(String graphName, byte[] startKey,
- byte[] endKey)
- throws PDException {
- int startPartId = getPartitionId(graphName, startKey);
- int endPartId = getPartitionId(graphName, endKey);
-
- List<Metapb.PartitionShard> partShards = new ArrayList<>();
- for (int id = startPartId; id <= endPartId; id++) {
- Metapb.Partition partition = partitionMeta.getPartitionById(graphName, id);
- partShards.add(
- Metapb.PartitionShard.newBuilder()
- .setPartition(partition)
- // 此处需要返回正确的leader,暂时默认取第一个
- .setLeader(storeService.getLeader(partition, 0))
- .build()
- );
- }
- return partShards;
- }
-
- public synchronized long updatePartition(List<Metapb.Partition> partitions) throws PDException {
- for (Metapb.Partition pt : partitions) {
- Metapb.Partition oldPt = getPartitionById(pt.getGraphName(), pt.getId());
- partitionMeta.updatePartition(pt);
- onPartitionChanged(oldPt, pt);
- }
- return partitions.size();
- }
-
- /**
- * 更新分区以及图的状态
- *
- * @param graph
- * @param partId
- * @param state
- * @throws PDException
- */
- public synchronized void updatePartitionState(String graph, int partId,
- Metapb.PartitionState state) throws PDException {
- Metapb.Partition partition = getPartitionById(graph, partId);
-
- if (partition.getState() != state) {
- Metapb.Partition newPartition = partitionMeta.updatePartition(partition.toBuilder()
- .setState(state)
- .build());
-
- onPartitionChanged(partition, newPartition);
- }
- }
-
- public synchronized void updateGraphState(String graphName, Metapb.PartitionState state) throws
- PDException {
- Metapb.Graph graph = getGraph(graphName);
- if (graph != null) {
- partitionMeta.updateGraph(graph.toBuilder()
- .setState(state).build());
- }
- }
-
- public synchronized long removePartition(String graphName, int partId) throws PDException {
- log.info("Partition {}-{} removePartition", graphName, partId);
- Metapb.Partition partition = partitionMeta.getPartitionById(graphName, partId);
- var ret = partitionMeta.removePartition(graphName, partId);
- partitionMeta.reload();
- onPartitionRemoved(partition);
-
- // source中有些是 offline的,删除后,需要更新图的状态
- try {
- Metapb.PartitionState state = Metapb.PartitionState.PState_Normal;
- for (Metapb.Partition pt : partitionMeta.getPartitions(partition.getGraphName())) {
- if (pt.getState().getNumber() > state.getNumber()) {
- state = pt.getState();
- }
- }
- updateGraphState(partition.getGraphName(), state);
-
- state = Metapb.PartitionState.PState_Normal;
- for (Metapb.ShardGroup group : storeService.getShardGroups()) {
- if (group.getState().getNumber() > state.getNumber()) {
- state = group.getState();
- }
- }
- storeService.updateClusterStatus(state);
-
- } catch (PDException e) {
- log.error("onPartitionChanged", e);
- }
-
- return ret;
- }
-
- public Metapb.PartitionStats getPartitionStats(String graphName, int partitionId) throws
- PDException {
- return partitionMeta.getPartitionStats(graphName, partitionId);
- }
-
- /**
- * 获取图的分区状态
- */
- public List<Metapb.PartitionStats> getPartitionStatus(String graphName)
- throws PDException {
- return partitionMeta.getPartitionStats(graphName);
- }
-
- /**
- * 返回图的信息
- */
- public List<Metapb.Graph> getGraphs() throws PDException {
- return partitionMeta.getGraphs();
- }
-
- public Metapb.Graph getGraph(String graphName) throws PDException {
- return partitionMeta.getGraph(graphName);
- }
-
- /**
- * 删除图以及图的所有分区
- */
- public Metapb.Graph delGraph(String graphName) throws PDException {
- log.info("delGraph {}", graphName);
- Metapb.Graph graph = getGraph(graphName);
- getPartitions(graphName).forEach(partition -> {
- onPartitionRemoved(partition);
- });
- partitionMeta.removeAllPartitions(graphName);
- partitionMeta.removeGraph(graphName);
- return graph;
- }
-
- /**
- * 修改图信息,需要通知到store
- */
- public synchronized Metapb.Graph updateGraph(Metapb.Graph graph) throws PDException {
- Metapb.Graph lastGraph = partitionMeta.getAndCreateGraph(graph.getGraphName());
- log.info("updateGraph graph: {}, last: {}", graph, lastGraph);
-
- int partCount =
- (graph.getGraphName().endsWith("/s") || graph.getGraphName().endsWith("/m")) ?
- 1 : pdConfig.getPartition().getTotalCount();
-
- // set the partition count to specified if legal.
- if (graph.getPartitionCount() <= partCount && graph.getPartitionCount() > 0) {
- partCount = graph.getPartitionCount();
- }
-
- if (partCount == 0) {
- throw new PDException(10010, "update graph error, partition count = 0");
- }
-
- graph = lastGraph.toBuilder()
- .mergeFrom(graph)
- .setPartitionCount(partCount)
- .build();
- partitionMeta.updateGraph(graph);
-
- // 分区数发生改变
- if (lastGraph.getPartitionCount() != graph.getPartitionCount()) {
- log.info("updateGraph graph: {}, partition count changed from {} to {}",
- graph.getGraphName(), lastGraph.getPartitionCount(),
- graph.getPartitionCount());
- // TODO 修改图的分区数,需要进行数据迁移。
- }
- return graph;
- }
-
- // partitionId -> (storeId -> shard committedIndex)
- public Map<Integer, Map<Long, Long>> getCommittedIndexStats() throws PDException {
- Map<Integer, Map<Long, Long>> map = new HashMap<>();
- for (Metapb.Store store : storeService.getActiveStores()) {
- for (Metapb.RaftStats raftStats : store.getStats().getRaftStatsList()) {
- int partitionID = raftStats.getPartitionId();
- if (!map.containsKey(partitionID)) {
- map.put(partitionID, new HashMap<>());
- }
- Map<Long, Long> storeMap = map.get(partitionID);
- if (!storeMap.containsKey(store.getId())) {
- storeMap.put(store.getId(), raftStats.getCommittedIndex());
- }
- }
- }
- return map;
- }
-
- /**
- * 存储被下线,迁移分区数据
- *
- * @param store
- */
- public void storeOffline(Metapb.Store store) {
- try {
- log.info("storeOffline store id: {}, address: {}, state: {}",
- store.getId(), store.getAddress(), store.getState());
- List<Metapb.Partition> partitions = getPartitionByStore(store);
- var partIds = new HashSet<Integer>();
- for (Metapb.Partition p : partitions) {
- if (partIds.contains(p.getId())) {
- continue;
- }
- shardOffline(p, store.getId());
- partIds.add(p.getId());
- }
- } catch (PDException e) {
- log.error("storeOffline exception: ", e);
- }
- }
-
- /**
- * 存储被下线,迁移分区数据
- */
- public synchronized void shardOffline(Metapb.Partition partition, long storeId) {
- try {
- log.info("shardOffline Partition {} - {} shardOffline store : {}",
- partition.getGraphName(), partition.getId(), storeId);
- // partition = getPartitionById(partition.getGraphName(), partition.getId());
- // Metapb.Partition.Builder builder = Metapb.Partition.newBuilder(partition);
- // builder.clearShards();
- // partition.getShardsList().forEach(shard -> {
- // if (shard.getStoreId() != storeId)
- // builder.addShards(shard);
- // });
- // partition = builder.build();
- Metapb.Graph graph = getGraph(partition.getGraphName());
- reallocPartitionShards(graph, partition);
-
- } catch (PDException e) {
- log.error("storeOffline exception: ", e);
- }
- }
-
- private boolean isShardListEquals(List<Metapb.Shard> list1, List<Metapb.Shard> list2) {
- if (list1 == list2) {
- return true;
- } else if (list1 != null && list2 != null) {
-
- var s1 = list1.stream().map(Metapb.Shard::getStoreId).sorted(Long::compare)
- .collect(Collectors.toList());
- var s2 = list2.stream().map(Metapb.Shard::getStoreId).sorted(Long::compare)
- .collect(Collectors.toList());
-
- if (s1.size() == s2.size()) {
- for (int i = 0; i < s1.size(); i++) {
- if (s1.get(i) != s2.get(i)) {
- return false;
- }
- }
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * 重新分配shard
- *
- * @param graph
- * @param partition
- * @throws PDException
- */
- public void reallocPartitionShards(Metapb.Graph graph, Metapb.Partition partition) throws
- PDException {
- if (partition == null) {
- return;
- }
- List<Metapb.Shard> originalShards = storeService.getShardList(partition.getId());
-
- var shardGroup = storeService.getShardGroup(partition.getId());
-
- List<Metapb.Shard> shards = storeService.reallocShards(shardGroup);
-
- if (isShardListEquals(originalShards, shards)) {
- log.info("reallocPartitionShards:{} vs {}", shardGroup, shards);
- // partition = Metapb.Partition.newBuilder(partition)
- // .clearShards().addAllShards(shards)
- // .build();
- // partitionMeta.updatePartition(partition);
- fireChangeShard(partition, shards, ConfChangeType.CONF_CHANGE_TYPE_ADJUST);
- }
- }
-
- public synchronized void reallocPartitionShards(String graphName, int partitionId) throws
- PDException {
- reallocPartitionShards(partitionMeta.getGraph(graphName),
- partitionMeta.getPartitionById(graphName, partitionId));
- }
-
- /**
- * 迁移分区副本
- */
- public synchronized void movePartitionsShard(Integer partitionId, long fromStore,
- long toStore) {
- try {
- log.info("movePartitionsShard partitionId {} from store {} to store {}", partitionId,
- fromStore, toStore);
- for (Metapb.Graph graph : getGraphs()) {
- Metapb.Partition partition =
- this.getPartitionById(graph.getGraphName(), partitionId);
- if (partition == null) {
- continue;
- }
-
- var shardGroup = storeService.getShardGroup(partitionId);
- List<Metapb.Shard> shards = new ArrayList<>();
- shardGroup.getShardsList().forEach(shard -> {
- if (shard.getStoreId() != fromStore) {
- shards.add(shard);
- }
- });
-
- shards.add(Metapb.Shard.newBuilder().setStoreId(toStore)
- .setRole(Metapb.ShardRole.Follower).build());
-
- // storeService.updateShardGroup(partitionId, shards, -1, -1);
- // storeService.onShardGroupStatusChanged(shardGroup, newShardGroup);
- fireChangeShard(partition, shards, ConfChangeType.CONF_CHANGE_TYPE_ADJUST);
- // shard group和 graph无关,迁移一个就够了
- break;
- }
- } catch (PDException e) {
- log.error("Partition {} movePartitionsShard exception {}", partitionId, e);
- }
- }
-
- /**
- * 把集群中所有的分区,拆成split
- *
- * @param splits 拆分分区
- */
- public synchronized void splitPartition(List<KVPair<Integer, Integer>> splits) throws
- PDException {
- var tasks = new HashMap<String, List<KVPair<Integer, Integer>>>();
-
- for (var pair : splits) {
- for (var partition : getPartitionById(pair.getKey())) {
- if (!tasks.containsKey(partition.getGraphName())) {
- tasks.put(partition.getGraphName(), new ArrayList<>());
- }
- tasks.get(partition.getGraphName()).add(pair);
- }
- }
-
- for (var entry : tasks.entrySet()) {
- splitPartition(getGraph(entry.getKey()), entry.getValue());
- }
- }
-
- /**
- * 分区分裂, 把一个图拆分到N 个
- *
- * @param graph graph
- * @param toCount target count
- * @throws PDException
- */
-
- public synchronized void splitPartition(Metapb.Graph graph, int toCount) throws PDException {
-
- var partitionCount = getPartitions(graph.getGraphName()).size();
- var maxShardsPerStore = pdConfig.getPartition().getMaxShardsPerStore();
- var shardCount = pdConfig.getPartition().getShardCount();
-
- if (shardCount * toCount > storeService.getActiveStores().size() * maxShardsPerStore) {
- throw new PDException(Pdpb.ErrorType.Too_Many_Partitions_Per_Store_VALUE,
- "can't satisfy target shard group count, reached the upper " +
- "limit of the cluster");
- }
-
- if (toCount % partitionCount != 0 || toCount <= partitionCount) {
- throw new PDException(Pdpb.ErrorType.Invalid_Split_Partition_Count_VALUE,
- "invalid split partition count, make sure to count is N time of" +
- " current partition count");
- }
-
- // 由于是整数倍数,扩充因子为 toCount / current count
- var splitCount = toCount / partitionCount;
- var list = new ArrayList<KVPair<Integer, Integer>>();
- for (int i = 0; i < partitionCount; i++) {
- list.add(new KVPair<>(i, splitCount));
- }
-
- splitPartition(graph, list);
- }
-
- private synchronized void splitPartition(Metapb.Graph graph,
- List<KVPair<Integer, Integer>> splits)
- throws PDException {
- var taskInfoMeta = storeService.getTaskInfoMeta();
- if (taskInfoMeta.scanSplitTask(graph.getGraphName()).size() > 0) {
- return;
- }
-
- splits.sort(Comparator.comparing(KVPair::getKey));
- log.info("split partition, graph: {}, splits:{}", graph, splits);
-
- // 从最后一个partition下标开始
- var i = getPartitions(graph.getGraphName()).size();
-
- for (var pair : splits) {
- Metapb.Partition partition =
- partitionMeta.getPartitionById(graph.getGraphName(), pair.getKey());
- if (partition != null) {
- var splitCount = pair.getValue();
- long splitLen = (partition.getEndKey() - partition.getStartKey()) / splitCount;
-
- List<Metapb.Partition> newPartitions = new ArrayList<>();
- // 第一个分区也就是原分区
- newPartitions.add(partition.toBuilder()
- .setStartKey(partition.getStartKey())
- .setEndKey(partition.getStartKey() + splitLen)
- .setId(partition.getId())
- .setState(Metapb.PartitionState.PState_Offline)
- .build());
-
- int idx = 0;
-
- for (; idx < splitCount - 2; idx++) {
- newPartitions.add(partition.toBuilder()
- .setStartKey(newPartitions.get(idx).getEndKey())
- .setEndKey(newPartitions.get(idx).getEndKey() +
- splitLen)
- .setId(i)
- .setState(Metapb.PartitionState.PState_Offline)
- .build());
- i += 1;
- }
-
- newPartitions.add(partition.toBuilder()
- .setStartKey(newPartitions.get(idx).getEndKey())
- .setEndKey(partition.getEndKey())
- .setId(i)
- .setState(Metapb.PartitionState.PState_Offline)
- .build());
- i += 1;
-
- // try to save new partitions, and repair shard group
- for (int j = 0; j < newPartitions.size(); j++) {
- var newPartition = newPartitions.get(j);
-
- if (j != 0) {
- partitionMeta.updatePartition(newPartition);
- }
- // 创建shard group,如果为空,则按照partition的shard group为蓝本,去创建,保证在一个机器上
- // 如果存在,则由于各个图的分区数量不一样,需要store端复制到其他机器上
- var shardGroup = storeService.getShardGroup(newPartition.getId());
- if (shardGroup == null) {
- shardGroup = storeService.getShardGroup(partition.getId()).toBuilder()
- .setId(newPartition.getId())
- .build();
- storeService.getStoreInfoMeta().updateShardGroup(shardGroup);
- updateShardGroupCache(shardGroup);
- }
-
- // 做shard list的检查
- if (shardGroup.getShardsCount() != pdConfig.getPartition().getShardCount()) {
- storeService.reallocShards(shardGroup);
- }
- }
-
- SplitPartition splitPartition = SplitPartition.newBuilder()
- .addAllNewPartition(newPartitions)
- .build();
-
- fireSplitPartition(partition, splitPartition);
- // 修改Partition状态为下线,任务完成后恢复为上线
- updatePartitionState(partition.getGraphName(), partition.getId(),
- Metapb.PartitionState.PState_Offline);
-
- // 记录事务
- var task = MetaTask.Task.newBuilder().setPartition(partition)
- .setSplitPartition(splitPartition)
- .build();
- taskInfoMeta.addSplitTask(pair.getKey(), task.getPartition(),
- task.getSplitPartition());
- }
- }
- }
-
- /**
- * 转移leader到其他shard上.
- * 转移一个partition即可
- */
- public void transferLeader(Integer partId, Metapb.Shard shard) {
- try {
- var partitions = getPartitionById(partId);
- if (partitions.size() > 0) {
- fireTransferLeader(partitions.get(0),
- TransferLeader.newBuilder().setShard(shard).build());
- }
-// for (Metapb.Graph graph : getGraphs()) {
-// Metapb.Partition partition = this.getPartitionById(graph.getGraphName(), partId);
-// if (partition != null) {
-// fireTransferLeader(partition, TransferLeader.newBuilder().setShard(shard)
-// .build());
-// }
-// }
- } catch (PDException e) {
- log.error("Partition {} transferLeader exception {}", partId, e);
- }
- }
-
- /**
- * 分区合并,将整个集群的分区数,合并到toCount个
- *
- * @param toCount 目标分区数
- * @throws PDException when query errors
- */
- public void combinePartition(int toCount) throws PDException {
-
- int shardsTotalCount = getShardGroupCount();
- for (var graph : getGraphs()) {
- // 对所有大于toCount分区的图,都进行缩容
- if (graph.getPartitionCount() > toCount) {
- combineGraphPartition(graph, toCount, shardsTotalCount);
- }
- }
- }
-
- /**
- * 针对单个图,进行分区合并
- *
- * @param graphName the name of the graph
- * @param toCount the target partition count
- * @throws PDException when query errors
- */
-
- public void combineGraphPartition(String graphName, int toCount) throws PDException {
- combineGraphPartition(getGraph(graphName), toCount, getShardGroupCount());
- }
-
- /**
- * 单图合并的内部实现
- *
- * @param graph the name of the graph
- * @param toCount the target partition count
- * @param shardCount the shard count of the clusters
- * @throws PDException when query errors
- */
- private synchronized void combineGraphPartition(Metapb.Graph graph, int toCount, int shardCount)
- throws PDException {
- if (graph == null) {
- throw new PDException(1,
- "Graph not exists, try to use full graph name, like " +
- "/DEFAULT/GRAPH_NAME/g");
- }
-
- log.info("Combine graph {} partition, from {}, to {}, with shard count:{}",
- graph.getGraphName(), graph.getPartitionCount(), toCount, shardCount);
-
- if (!checkTargetCount(graph.getPartitionCount(), toCount, shardCount)) {
- log.error("Combine partition, illegal toCount:{}, graph:{}", toCount,
- graph.getGraphName());
- throw new PDException(2,
- "illegal partition toCount, should between 1 ~ shard group " +
- "count and " +
- " can be dived by shard group count");
- }
-
- var taskInfoMeta = storeService.getTaskInfoMeta();
- if (taskInfoMeta.scanMoveTask(graph.getGraphName()).size() > 0) {
- throw new PDException(3, "Graph Combine process exists");
- }
-
- // 按照 key start 排序,合并后的key range 是连续的
- var partitions = getPartitions(graph.getGraphName()).stream()
- .sorted(Comparator.comparing(
- Metapb.Partition::getStartKey))
- .collect(Collectors.toList());
-
- // 分区编号不一定是连续的
- var sortPartitions = getPartitions(graph.getGraphName())
- .stream()
- .sorted(Comparator.comparing(Metapb.Partition::getId))
- .collect(Collectors.toList());
-
- var groupSize = partitions.size() / toCount; // merge group size
- // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 共12个分区, 合并成4个
- // 方案:0,1,2 => 0, 3,4,5 -> 1, 6,7,8 ->2, 9,10,11 -> 3
- // 保证分区的连续性.
- for (int i = 0; i < toCount; i++) {
- var startKey = partitions.get(i * groupSize).getStartKey();
- var endKey = partitions.get(i * groupSize + groupSize - 1).getEndKey();
- // compose the key range
- // the start key and end key should be changed if combine success.
-
- var targetPartition = Metapb.Partition.newBuilder(sortPartitions.get(i))
- .setStartKey(startKey)
- .setEndKey(endKey)
- .build();
-
- for (int j = 0; j < groupSize; j++) {
- var partition = partitions.get(i * groupSize + j);
- // 分区id相同,就跳过
- if (i == partition.getId()) {
- continue;
- }
-
- log.info("combine partition of graph :{}, from part id {} to {}",
- partition.getGraphName(),
- partition.getId(), targetPartition.getId());
- MovePartition movePartition = MovePartition.newBuilder()
- .setTargetPartition(targetPartition)
- .setKeyStart(partition.getStartKey())
- .setKeyEnd(partition.getEndKey())
- .build();
- taskInfoMeta.addMovePartitionTask(partition, movePartition);
- // source 下线
- updatePartitionState(partition.getGraphName(), partition.getId(),
- Metapb.PartitionState.PState_Offline);
- fireMovePartition(partition, movePartition);
- }
- // target 下线
- updatePartitionState(targetPartition.getGraphName(), targetPartition.getId(),
- Metapb.PartitionState.PState_Offline);
- }
-
- storeService.updateClusterStatus(Metapb.ClusterState.Cluster_Offline);
- }
-
- /**
- * 通过 storeService 获取 raft group 总数
- *
- * @return the count of raft groups
- */
- private int getShardGroupCount() {
- try {
- return Optional.ofNullable(storeService.getShardGroups()).orElseGet(ArrayList::new)
- .size();
- } catch (PDException e) {
- log.error("get shard group failed, error: {}", e);
- }
- return 0;
- }
-
- /**
- * 判断图分区是否能够从from合并到to个
- *
- * @param fromCount 现在的分区数
- * @param toCount 目标分区数
- * @return true when available , or otherwise
- */
- private boolean checkTargetCount(int fromCount, int toCount, int shardCount) {
- // 要介于 1 ~ N 中间,而且可以整除
- return toCount >= 1 && toCount < fromCount && fromCount % toCount == 0 &&
- toCount < shardCount;
- }
-
- /**
- * 处理分区心跳, 记录Leader信息
- * 检查term和version,比较是否是最新的消息
- *
- * @param stats
- */
- public void partitionHeartbeat(Metapb.PartitionStats stats) throws PDException {
-
- Metapb.ShardGroup shardGroup = storeService.getShardGroup(stats.getId());
- // shard group version changes
- // (shard group 由pd控制, 在分裂等操作后,可能出现短暂不一致的情况,以pd为准)
- // store控制shard leader
- if (shardGroup != null &&
- (shardGroup.getVersion() < stats.getLeaderTerm() ||
- shardGroup.getConfVer() < stats.getConfVer())) {
- storeService.updateShardGroup(stats.getId(),
- stats.getShardList(), stats.getLeaderTerm(),
- stats.getConfVer());
- }
-
- List<Metapb.Partition> partitions = getPartitionById(stats.getId());
- for (Metapb.Partition partition : partitions) {
- // partitionMeta.getAndCreateGraph(partition.getGraphName());
- checkShardState(partition, stats);
- }
- // 统计信息
- partitionMeta.updatePartitionStats(stats.toBuilder()
- .setTimestamp(System.currentTimeMillis()).build());
- }
-
- /**
- * 检查shard状态,离线shard影响到分区状态
- *
- * @param stats
- */
- private void checkShardState(Metapb.Partition partition, Metapb.PartitionStats stats) {
-
- try {
- int offCount = 0;
- for (Metapb.ShardStats shard : stats.getShardStatsList()) {
- if (shard.getState() == Metapb.ShardState.SState_Offline) {
- offCount++;
- }
- }
- if (partition.getState() != Metapb.PartitionState.PState_Offline) {
- if (offCount == 0) {
- updatePartitionState(partition.getGraphName(), partition.getId(),
- Metapb.PartitionState.PState_Normal);
- } else if (offCount * 2 < stats.getShardStatsCount()) {
- updatePartitionState(partition.getGraphName(), partition.getId(),
- Metapb.PartitionState.PState_Warn);
- } else {
- updatePartitionState(partition.getGraphName(), partition.getId(),
- Metapb.PartitionState.PState_Warn);
- }
- }
- } catch (Exception e) {
- log.error("Partition {}-{} checkShardState exception {}",
- partition.getGraphName(), partition.getId(), e);
- }
- }
-
-
- public void addInstructionListener(PartitionInstructionListener event) {
- instructionListeners.add(event);
- }
-
- public void addStatusListener(PartitionStatusListener listener) {
- statusListeners.add(listener);
- }
-
- /**
- * 发起改变shard命令
- *
- * @param changeType
- */
- protected void fireChangeShard(Metapb.Partition partition, List<Metapb.Shard> shards,
- ConfChangeType changeType) {
- log.info("fireChangeShard partition: {}-{}, changeType:{} {}", partition.getGraphName(),
- partition.getId(), changeType, shards);
- instructionListeners.forEach(cmd -> {
- try {
- cmd.changeShard(partition, ChangeShard.newBuilder()
- .addAllShard(shards).setChangeType(changeType)
- .build());
- } catch (Exception e) {
- log.error("fireChangeShard", e);
- }
- });
- }
-
- public void changeShard(int groupId, List<Metapb.Shard> shards) throws PDException {
- var partitions = getPartitionById(groupId);
- if (partitions.size() == 0) {
- return;
- }
- fireChangeShard(partitions.get(0), shards, ConfChangeType.CONF_CHANGE_TYPE_ADJUST);
- }
-
- /**
- * 发送分区分裂消息
- *
- * @param partition
- */
- protected void fireSplitPartition(Metapb.Partition partition, SplitPartition splitPartition) {
- log.info("fireSplitPartition partition: {}-{}, split :{}",
- partition.getGraphName(), partition.getId(), splitPartition);
- instructionListeners.forEach(cmd -> {
- try {
- cmd.splitPartition(partition, splitPartition);
- } catch (Exception e) {
- log.error("fireSplitPartition", e);
- }
- });
- }
-
- /**
- * 发送Leader切换消息
- */
- protected void fireTransferLeader(Metapb.Partition partition, TransferLeader transferLeader) {
- log.info("fireTransferLeader partition: {}-{}, leader :{}",
- partition.getGraphName(), partition.getId(), transferLeader);
- instructionListeners.forEach(cmd -> {
- try {
- cmd.transferLeader(partition, transferLeader);
- } catch (Exception e) {
- log.error("fireSplitPartition", e);
- }
- });
- }
-
- /**
- * 发送分区移动数据的消息
- *
- * @param partition 原分区
- * @param movePartition 目标分区,包含 key range
- */
- protected void fireMovePartition(Metapb.Partition partition, MovePartition movePartition) {
- log.info("fireMovePartition partition: {} -> {}",
- partition, movePartition);
-
- instructionListeners.forEach(cmd -> {
- try {
- cmd.movePartition(partition, movePartition);
- } catch (Exception e) {
- log.error("fireMovePartition", e);
- }
- });
- }
-
- protected void fireCleanPartition(Metapb.Partition partition, CleanPartition cleanPartition) {
- log.info("fireCleanPartition partition: {} -> just keep : {}->{}",
- partition.getId(), cleanPartition.getKeyStart(), cleanPartition.getKeyEnd());
-
- instructionListeners.forEach(cmd -> {
- try {
- cmd.cleanPartition(partition, cleanPartition);
- } catch (Exception e) {
- log.error("cleanPartition", e);
- }
- });
- }
-
- protected void fireChangePartitionKeyRange(Metapb.Partition partition,
- PartitionKeyRange partitionKeyRange) {
- log.info("fireChangePartitionKeyRange partition: {}-{} -> key range {}",
- partition.getGraphName(), partition.getId(), partitionKeyRange);
-
- instructionListeners.forEach(cmd -> {
- try {
- cmd.changePartitionKeyRange(partition, partitionKeyRange);
- } catch (Exception e) {
- log.error("cleanPartition", e);
- }
- });
- }
-
- /**
- * 处理图迁移任务
- *
- * @param task
- */
- public synchronized void handleMoveTask(MetaTask.Task task) throws PDException {
- var taskInfoMeta = storeService.getTaskInfoMeta();
- var partition = task.getPartition();
- var movePartition = task.getMovePartition();
-
- MetaTask.Task pdMetaTask = taskInfoMeta.getMovePartitionTask(partition.getGraphName(),
- movePartition.getTargetPartition()
- .getId(),
- partition.getId());
-
- log.info("report move task, graph:{}, pid : {}->{}, state: {}",
- task.getPartition().getGraphName(),
- task.getPartition().getId(), task.getMovePartition().getTargetPartition().getId(),
- task.getState());
-
- // 已经被处理(前面有failed)
- if (pdMetaTask != null) {
- var newTask = pdMetaTask.toBuilder().setState(task.getState()).build();
- taskInfoMeta.updateMovePartitionTask(newTask);
-
- List<MetaTask.Task> subTasks = taskInfoMeta.scanMoveTask(partition.getGraphName());
-
- var finished = subTasks.stream().allMatch(t ->
- t.getState() ==
- MetaTask.TaskState.Task_Success ||
- t.getState() ==
- MetaTask.TaskState.Task_Failure);
-
- if (finished) {
- var allSuccess = subTasks.stream().allMatch(
- t -> t.getState() == MetaTask.TaskState.Task_Success);
- if (allSuccess) {
- log.info("graph:{} combine task all success!", partition.getGraphName());
- handleMoveTaskAllSuccess(subTasks, partition.getGraphName(), taskInfoMeta);
- } else {
- log.info("graph:{} combine task failed!", partition.getGraphName());
- handleMoveTaskIfFailed(partition.getGraphName(), taskInfoMeta);
- }
- }
- }
- }
-
- /**
- * 当所有的迁移子任务成功:
- * 1. 发送清理source分区指令
- * 2. 设置target上线, 更新key range, 更新 graph partition count
- * 3. 删除move task,任务结束
- *
- * @param subTasks all move sub tasks
- * @param graphName graph name
- * @param taskInfoMeta task info meta
- * @throws PDException returns if write db failed
- */
- private void handleMoveTaskAllSuccess(List<MetaTask.Task> subTasks, String graphName,
- TaskInfoMeta taskInfoMeta) throws PDException {
-
- var targetPartitionIds = new HashSet<Integer>();
- var targetPartitions = new ArrayList<Metapb.Partition>();
- var deleteFlags =
- subTasks.stream().map(task -> task.getMovePartition().getTargetPartition().getId())
- .collect(Collectors.toSet());
-
- for (MetaTask.Task subTask : subTasks) {
- var source = subTask.getPartition();
- var targetPartition = subTask.getMovePartition().getTargetPartition();
- // 是否处理过
- if (!targetPartitionIds.contains(targetPartition.getId())) {
- // 更新range
- var old = getPartitionById(targetPartition.getGraphName(), targetPartition.getId());
- var newPartition = Metapb.Partition.newBuilder(old)
- .setStartKey(targetPartition.getStartKey())
- .setEndKey(targetPartition.getEndKey())
- .setState(Metapb.PartitionState.PState_Normal)
- .build();
- // 在 key range之前更新,避免store没有分区的问题, 需要到pd查询
- updatePartition(List.of(newPartition));
- targetPartitions.add(newPartition);
-
- // 发送key range 变更消息
- PartitionKeyRange partitionKeyRange = PartitionKeyRange.newBuilder()
- .setPartitionId(old.getId())
- .setKeyStart(
- targetPartition.getStartKey())
- .setKeyEnd(
- targetPartition.getEndKey())
- .build();
- // 通知store
- fireChangePartitionKeyRange(
- old.toBuilder().setState(Metapb.PartitionState.PState_Normal).build(),
- partitionKeyRange);
-
- // 将 target 设置为上线. source 理论上可能被删掉,所以不处理
- updatePartitionState(newPartition.getGraphName(), newPartition.getId(),
- Metapb.PartitionState.PState_Normal);
-
- targetPartitionIds.add(targetPartition.getId());
- }
-
- CleanPartition cleanPartition = CleanPartition.newBuilder()
- .setKeyStart(source.getStartKey())
- .setKeyEnd(source.getEndKey())
- .setCleanType(
- CleanType.CLEAN_TYPE_EXCLUDE_RANGE)
- // target 的 partition只需要清理数据,不需要删除分区
- .setDeletePartition(!deleteFlags.contains(
- source.getId()))
- .build();
-
- log.info("pd clean data: {}-{}, key range:{}-{}, type:{}, delete partition:{}",
- source.getGraphName(),
- source.getId(),
- cleanPartition.getKeyStart(),
- cleanPartition.getKeyEnd(),
- CleanType.CLEAN_TYPE_EXCLUDE_RANGE,
- cleanPartition.getDeletePartition());
-
- // 清理掉被移动分区的数据
- fireCleanPartition(source, cleanPartition);
- }
-
- // 更新key range, 本地更新,client更新
- // updatePartition(targetPartitions);
-
- // 更新target 分区状态,source 可能被删掉,所以不处理
- targetPartitions.forEach(p -> {
- try {
- updatePartitionState(p.getGraphName(), p.getId(),
- Metapb.PartitionState.PState_Normal);
- } catch (PDException e) {
- throw new RuntimeException(e);
- }
- });
-
- partitionMeta.reload();
-
- // 更新graph partition count
- var graph = getGraph(graphName).toBuilder()
- .setPartitionCount(targetPartitionIds.size())
- .build();
- updateGraph(graph);
-
- // 事务完成
- taskInfoMeta.removeMoveTaskPrefix(graphName);
- }
-
- /**
- * 如果缩容任务有失败的,回滚合并操作
- * 1. 清理原来的target 分区,将迁移过来的数据再删掉
- * 2. 将source/target 分区设置为上线
- * 3. 删除task,任务结束
- *
- * @param graphName graph name
- * @param taskInfoMeta task info meta
- * @throws PDException return if write to db failed
- */
- private void handleMoveTaskIfFailed(String graphName, TaskInfoMeta taskInfoMeta) throws
- PDException {
- // 发送清理target分区的任务, 回滚target分区
- var targetPartitionIds = new HashSet<Integer>();
- for (var metaTask : taskInfoMeta.scanMoveTask(graphName)) {
-
- var source = metaTask.getPartition();
- // 设置 source 为上线
- updatePartitionState(source.getGraphName(), source.getId(),
- Metapb.PartitionState.PState_Normal);
- var movedPartition = metaTask.getMovePartition().getTargetPartition();
-
- if (targetPartitionIds.contains(movedPartition.getId())) {
- continue;
- }
-
- var targetPartition = getPartitionById(graphName, movedPartition.getId());
-
- CleanPartition cleanPartition = CleanPartition.newBuilder()
- .setKeyStart(
- targetPartition.getStartKey())
- .setKeyEnd(targetPartition.getEndKey())
- .setCleanType(
- CleanType.CLEAN_TYPE_KEEP_RANGE)
- .setDeletePartition(false)
- .build();
- fireCleanPartition(targetPartition, cleanPartition);
- targetPartitionIds.add(targetPartition.getId());
-
- // 设置target 上线
- updatePartitionState(targetPartition.getGraphName(), targetPartition.getId(),
- Metapb.PartitionState.PState_Normal);
- }
- // 清理掉任务列表
- taskInfoMeta.removeMoveTaskPrefix(graphName);
- }
-
- /**
... 27901 lines suppressed ...