diff --git a/.github/workflows/pd-store.yml b/.github/workflows/pd-store.yml index b3abff59d0..8848683168 100644 --- a/.github/workflows/pd-store.yml +++ b/.github/workflows/pd-store.yml @@ -62,10 +62,6 @@ jobs: run: | mvn test -pl hugegraph-pd/hg-pd-test -am -P pd-client-test - - name: Run cli-tools test - run: | - mvn test -pl hugegraph-pd/hg-pd-test -am -P pd-cli-tools-test - - name: Run rest test run: | mvn test -pl hugegraph-pd/hg-pd-test -am -P pd-rest-test 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 index d4fd50ffe9..868f8fae3a 100644 --- 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 @@ -73,8 +73,15 @@ public KVPair 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) { + if (partition == null) { + return null; + } + KVPair group = groups.get(partId); + if (group == null) { + return null; + } + Shard shard = group.getValue(); + if (shard == null) { return null; } return new KVPair<>(partition, shard); diff --git a/hugegraph-pd/hg-pd-clitools/pom.xml b/hugegraph-pd/hg-pd-clitools/pom.xml deleted file mode 100644 index b6178530eb..0000000000 --- a/hugegraph-pd/hg-pd-clitools/pom.xml +++ /dev/null @@ -1,74 +0,0 @@ - - - - - - hugegraph-pd - org.apache.hugegraph - ${revision} - ../pom.xml - - 4.0.0 - - hg-pd-clitools - - - org.apache.hugegraph - hg-pd-client - ${revision} - - - junit - junit - 4.13.2 - test - - - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - package - - single - - - - - - org.apache.hugegraph.pd.clitools.Main - - - - - jar-with-dependencies - - - - - - - - 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 440ec2e5f1..0000000000 --- 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-core/src/main/java/org/apache/hugegraph/pd/meta/IdMetaStore.java b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/meta/IdMetaStore.java index 70e4c501f9..7380547435 100644 --- a/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/meta/IdMetaStore.java +++ b/hugegraph-pd/hg-pd-core/src/main/java/org/apache/hugegraph/pd/meta/IdMetaStore.java @@ -47,7 +47,7 @@ public class IdMetaStore extends MetadataRocksDBStore { private static final String CID_DEL_SLOT_PREFIX = "@CID_DEL_SLOT@"; private static final String SEPARATOR = "@"; private static final ConcurrentHashMap SEQUENCES = new ConcurrentHashMap<>(); - public static long CID_DEL_TIMEOUT = 24 * 3600 * 1000; + private static long CID_DEL_TIMEOUT = 24 * 3600 * 1000; private final long clusterId; public IdMetaStore(PDConfig pdConfig) { diff --git a/hugegraph-pd/hg-pd-test/pom.xml b/hugegraph-pd/hg-pd-test/pom.xml index b41c5a2a2e..b8de18e2be 100644 --- a/hugegraph-pd/hg-pd-test/pom.xml +++ b/hugegraph-pd/hg-pd-test/pom.xml @@ -164,16 +164,10 @@ - - org.apache.hugegraph - hg-pd-clitools - ${revision} - org.apache.hugegraph hg-pd-common ${revision} - org.apache.hugegraph diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/PartitionCacheTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/PartitionCacheTest.java deleted file mode 100644 index f01ba966d2..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/PartitionCacheTest.java +++ /dev/null @@ -1,104 +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.common.Useless; -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; - -@Useless("can be merged to org.apache.hugegraph.pd.common.PartitionCacheTest") -public class PartitionCacheTest { - - // @Test - public void test() { - PartitionCache cache = new PartitionCache(); - for (int i = 0; i < 10; i++) { - KVPair 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 partShards = cache.getPartitionByCode("aa", i); - System.out.println(" " + i + " " + partShards.getKey().getStartKey()); - } - } - - - // @Test - public void test1() { - Map> keyToPartIdCache = new HashMap<>(); - // graphName + PartitionID组成key - Map> partitionCache = new HashMap<>(); - - // 缓存全部Store,用于全库查询,需要优化 - Map> 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> keyToPartIdCache2 = - cloneKeyToPartIdCache(keyToPartIdCache); - System.out.println(keyToPartIdCache2.size()); - } - - public Map> cloneKeyToPartIdCache( - Map> cache) { - Map> 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> - clonePartitionCache(Map> cache) { - Map> cacheClone = new HashMap<>(); - cacheClone.putAll(cache); - return cacheClone; - } - - public Map> - cloneStoreCache(Map> cache) { - Map> cacheClone = new HashMap<>(); - cacheClone.putAll(cache); - return cacheClone; - } -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/UnitTestBase.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/UnitTestBase.java deleted file mode 100644 index e1ca5adaf9..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/UnitTestBase.java +++ /dev/null @@ -1,34 +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.io.File; - -import org.apache.hugegraph.pd.common.Useless; - -@Useless -public class UnitTestBase { - public static boolean deleteDir(File dir) { - if (dir.isDirectory()) { - for (File file : dir.listFiles()) { - deleteDir(file); - } - } - return dir.delete(); - } -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/BaseClientTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/BaseClientTest.java index 3fca39ade9..ef3152fa11 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/BaseClientTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/BaseClientTest.java @@ -24,21 +24,18 @@ @RunWith(MockitoJUnitRunner.class) public class BaseClientTest { - public static PDClient pdClient; - public final String storeAddr = "localhost"; - public final String graphName = "default/hugegraph/g"; - public long storeId = 0; + + protected static PDClient pdClient; @BeforeClass - public static void beforeClass() throws Exception { + public static void beforeClass() { PDConfig config = PDConfig.of("localhost:8686"); -// PDConfig config = PDConfig.of("10.81.116.77:8986"); config.setEnableCache(true); pdClient = PDClient.create(config); } @After - public void teardown() throws Exception { + public void teardown() { // pass } } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/ChangingLeader.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/ChangingLeader.java new file mode 100644 index 0000000000..afc3d20edc --- /dev/null +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/ChangingLeader.java @@ -0,0 +1,58 @@ +/* + * 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.Executors; +import java.util.concurrent.TimeUnit; + +import org.apache.hugegraph.pd.common.Useless; + +import com.alipay.sofa.jraft.CliService; +import com.alipay.sofa.jraft.RaftServiceFactory; +import com.alipay.sofa.jraft.Status; +import com.alipay.sofa.jraft.conf.Configuration; +import com.alipay.sofa.jraft.entity.PeerId; +import com.alipay.sofa.jraft.option.CliOptions; + +@Useless("used for development") +public class ChangingLeader { + + private static final CliService cliService = + RaftServiceFactory.createAndInitCliService(new CliOptions()); + + public static void main(String[] args) { + var conf = new Configuration(); + conf.addPeer(PeerId.parsePeer("127.0.0.1:8610")); + conf.addPeer(PeerId.parsePeer("127.0.0.1:8611")); + conf.addPeer(PeerId.parsePeer("127.0.0.1:8612")); + CountDownLatch latch = new CountDownLatch(100); + + Executors.newScheduledThreadPool(1).scheduleAtFixedRate(() -> { + Status status = cliService.transferLeader("pd_raft", conf, PeerId.ANY_PEER); + System.out.println("trigger change leader status: " + status); + latch.countDown(); + }, 1, 3, TimeUnit.SECONDS); + + try { + latch.await(); + } catch (Exception e) { + System.out.println(e); + } + } +} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClientImplTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClientImplTest.java deleted file mode 100644 index 6d42c5ea73..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/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 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 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 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; - } -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClientTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClientTest.java deleted file mode 100644 index 928f1dcba5..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/DiscoveryClientTest.java +++ /dev/null @@ -1,79 +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.function.Consumer; - -import org.apache.hugegraph.pd.client.DiscoveryClientImpl; -import org.apache.hugegraph.pd.grpc.discovery.NodeInfo; -import org.apache.hugegraph.pd.grpc.discovery.Query; -import org.junit.Before; -import org.junit.Test; - -public class DiscoveryClientTest { - - private DiscoveryClientImpl client; - - @Before - public void setUp() { - this.client = getClient("appName", "localhost:8654", new HashMap()); - } - - @Test - public void testGetRegisterNode() { - // Setup - try { - Consumer result = this.client.getRegisterConsumer(); - final NodeInfo expectedResult = NodeInfo.newBuilder() - .setAppName("appName") - .build(); - - Thread.sleep(3000); - Query query = Query.newBuilder().setAppName("appName") - .setVersion("0.13.0").build(); - - // Run the test - this.client.getNodeInfos(query); - } catch (InterruptedException e) { - e.printStackTrace(); - } finally { - this.client.close(); - } - - } - - private DiscoveryClientImpl getClient(String appName, String address, - Map labels) { - DiscoveryClientImpl discoveryClient = null; - try { - discoveryClient = DiscoveryClientImpl.newBuilder().setCenterAddress( - "localhost:8686").setAddress(address).setAppName(appName) - .setDelay(2000) - .setVersion("0.13.0") - .setId("0").setLabels(labels) - .build(); - discoveryClient.scheduleTask(); - } catch (Exception e) { - e.printStackTrace(); - } - - return discoveryClient; - } -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/KvClientTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/KvClientTest.java index c61413b8cd..66993f2815 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/KvClientTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/KvClientTest.java @@ -23,8 +23,6 @@ import java.util.function.Consumer; import org.apache.commons.lang3.StringUtils; -import org.apache.hugegraph.pd.client.KvClient; -import org.apache.hugegraph.pd.client.PDConfig; import org.apache.hugegraph.pd.grpc.kv.KResponse; import org.apache.hugegraph.pd.grpc.kv.ScanPrefixResponse; import org.apache.hugegraph.pd.grpc.kv.WatchEvent; @@ -59,7 +57,6 @@ public void testCreateStub() { } - // Verify the results } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/LicenseClientImplTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/LicenseClientImplTest.java deleted file mode 100644 index 4ed11b9b21..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/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 load = yaml.loadAll(yml); - Iterator iterator = load.iterator(); - Properties properties = new Properties(); - while (iterator.hasNext()) { - Map next = (Map) iterator.next(); - map2Properties(next, "", properties); - } - return properties; - } - - private void map2Properties(Map map, String prefix, Properties properties) { - - for (Map.Entry 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) value, newPrefix, properties); - } - - } - } - -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDClientSuiteTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDClientSuiteTest.java index a9d3ae1406..ce27623c9a 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDClientSuiteTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDClientSuiteTest.java @@ -22,14 +22,12 @@ import lombok.extern.slf4j.Slf4j; - @RunWith(Suite.class) @Suite.SuiteClasses({ PDClientTest.class, KvClientTest.class, - DiscoveryClientTest.class + StoreRegisterTest.class, }) - @Slf4j public class PDClientSuiteTest { diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDClientTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDClientTest.java index 470e3548f1..c745f42355 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDClientTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDClientTest.java @@ -25,23 +25,20 @@ import org.apache.hugegraph.pd.grpc.MetaTask; import org.apache.hugegraph.pd.grpc.Metapb; import org.apache.hugegraph.pd.grpc.Pdpb; -import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; +// TODO: Exceptions should be thrown rather than silenced. public class PDClientTest extends BaseClientTest { + @Test public void testDbCompaction() { - System.out.println("testDbCompaction start"); - try { pdClient.dbCompaction(""); pdClient.dbCompaction(); } catch (PDException e) { e.printStackTrace(); } - - System.out.println("pdclienttest testDbCompaction end"); } @Test @@ -110,15 +107,15 @@ public void testGetAllStores() { } } -// @Test -// public void testStoreHeartbeat(){ -// Metapb.StoreStats stats = Metapb.StoreStats.newBuilder().build(); -// try { -// pdClient.storeHeartbeat(stats); -// } catch (PDException e) { -// e.printStackTrace(); -// } -// } + @Test + public void testStoreHeartbeat() { + Metapb.StoreStats stats = Metapb.StoreStats.newBuilder().build(); + try { + pdClient.storeHeartbeat(stats); + } catch (PDException e) { + e.printStackTrace(); + } + } @Test public void testKeyToCode() { @@ -162,11 +159,8 @@ public void testGetPartitions() { } } - @Ignore @Test public void testUpdatePartitionLeader() { - System.out.println("updatePartitionLeader start"); - pdClient.updatePartitionLeader("aaa", 0, 0L); } @@ -362,7 +356,6 @@ public void testReportTask() { } } - @Test public void testBalanceLeaders() { try { @@ -400,7 +393,6 @@ public void testUpdatePartition() { } } - @Ignore @Test public void testDelPartition() { try { diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDPulseTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDPulseTest.java index d27c4b5e11..7b3825c133 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDPulseTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDPulseTest.java @@ -20,18 +20,16 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import org.apache.hugegraph.pd.client.test.HgPDTestUtil; +import org.apache.hugegraph.pd.common.Useless; import org.apache.hugegraph.pd.grpc.pulse.PartitionHeartbeatRequest; import org.apache.hugegraph.pd.pulse.PulseServerNotice; import org.junit.BeforeClass; import org.junit.Test; +@Useless("used for development") public class PDPulseTest { - private static PDClient pdClient; - private final long storeId = 0; - private final String storeAddress = "localhost"; - private final String graphName = "graph1"; + private static PDClient pdClient; @BeforeClass public static void beforeClass() throws Exception { @@ -43,29 +41,26 @@ public static void beforeClass() throws Exception { @Test public void listen() { - PDPulse pulse = new PDPulseImpl(pdClient.getLeaderIp()); CountDownLatch latch = new CountDownLatch(60); PDPulse.Notifier notifier1 = - pulse.connectPartition(new PulseListener(latch, "listener1")); + pulse.connectPartition(new PulseListener<>(latch, "listener1")); PDPulse.Notifier notifier2 = - pulse.connectPartition(new PulseListener(latch, "listener2")); + pulse.connectPartition(new PulseListener<>(latch, "listener2")); PDPulse.Notifier notifier3 = - pulse.connectPartition(new PulseListener(latch, "listener3")); + 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(); @@ -73,10 +68,10 @@ public void listen() { notifier3.close(); } + private static class PulseListener implements PDPulse.Listener { - private class PulseListener implements PDPulse.Listener { private final String listenerName; - CountDownLatch latch = new CountDownLatch(10); + private final CountDownLatch latch; private PulseListener(CountDownLatch latch, String listenerName) { this.latch = latch; @@ -85,26 +80,25 @@ private PulseListener(CountDownLatch latch, String listenerName) { @Override public void onNext(T response) { - // println(this.listenerName+" res: "+response); - // this.latch.countDown(); + System.out.println(this.listenerName + " ---> res: " + response); + this.latch.countDown(); } @Override public void onNotice(PulseServerNotice notice) { - HgPDTestUtil.println(this.listenerName + " ---> res: " + notice.getContent()); - + System.out.println(this.listenerName + " ---> res: " + notice.getContent()); notice.ack(); this.latch.countDown(); } @Override public void onError(Throwable throwable) { - HgPDTestUtil.println(this.listenerName + " error: " + throwable.toString()); + System.out.println(this.listenerName + " error: " + throwable.toString()); } @Override public void onCompleted() { - HgPDTestUtil.println(this.listenerName + " is completed"); + System.out.println(this.listenerName + " is completed"); } } } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDWatchTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDWatchTest.java index 180b725555..5af89b539f 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDWatchTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/PDWatchTest.java @@ -20,21 +20,18 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import org.apache.hugegraph.pd.client.test.HgPDTestUtil; +import org.apache.hugegraph.pd.common.Useless; import org.junit.BeforeClass; import org.junit.Test; -@Deprecated +@Useless("used for development") public class PDWatchTest { - private static PDClient pdClient; - private final long storeId = 0; - private final String storeAddr = "localhost"; - private final String graphName = "graph1"; + private static PDClient pdClient; @BeforeClass public static void beforeClass() { - pdClient = PDClient.create(PDConfig.of("localhost:9000")); + pdClient = PDClient.create(PDConfig.of("localhost:8686")); } @Test @@ -46,8 +43,6 @@ public void watch() { 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) { @@ -59,6 +54,7 @@ public void watch() { } private class WatchListener implements PDWatch.Listener { + private final String watcherName; CountDownLatch latch; @@ -69,18 +65,18 @@ private WatchListener(CountDownLatch latch, String watcherName) { @Override public void onNext(T response) { - HgPDTestUtil.println(this.watcherName + " res: " + response); + System.out.println(this.watcherName + " res: " + response); this.latch.countDown(); } @Override public void onError(Throwable throwable) { - HgPDTestUtil.println(this.watcherName + " error: " + throwable.toString()); + System.out.println(this.watcherName + " error: " + throwable.toString()); } @Override public void onCompleted() { - HgPDTestUtil.println(this.watcherName + " is completed"); + System.out.println(this.watcherName + " is completed"); } } } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/StoreRegisterTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/StoreRegisterTest.java index 5826b3858f..55e59d574e 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/StoreRegisterTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/StoreRegisterTest.java @@ -28,32 +28,31 @@ import org.apache.hugegraph.pd.pulse.PulseServerNotice; import org.junit.Assert; import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; 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 { + public static void beforeClass() { PDConfig config = PDConfig.of("localhost:8686"); config.setEnableCache(true); pdClient = PDClient.create(config); } - // @Test + @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(); - } + storeId = pdClient.registerStore(store); Assert.assertTrue("RegisterStore store_id = " + storeId, storeId != 0); } - // @Test + @Test public void testGetStore() throws PDException { testRegisterStore(); Metapb.Store store = pdClient.getStore(storeId); @@ -61,7 +60,8 @@ public void testGetStore() throws PDException { System.out.println(store); } - // @Test + @Ignore // no active store + @Test public void testGetActiveStores() throws PDException { testRegisterStore(); List stores = pdClient.getActiveStores(graphName); @@ -71,8 +71,8 @@ public void testGetActiveStores() throws PDException { }); } - - // @Test + @Ignore // no active store + @Test public void testStoreHeartbeat() throws PDException { testRegisterStore(); Metapb.StoreStats stats = Metapb.StoreStats.newBuilder().setStoreId(storeId).build(); @@ -88,14 +88,14 @@ public void testStoreHeartbeat() throws PDException { Assert.assertTrue(exist); } - - // @Test - public void testPartitionHeartbeat() throws InterruptedException, PDException { + @Ignore // no active store + @Test + public void testPartitionHeartbeat() throws PDException { testRegisterStore(); PDPulse pdPulse = new PDPulseImpl(pdClient.getLeaderIp()); PDPulse.Notifier notifier = pdPulse.connectPartition( - new PDPulse.Listener() { + new PDPulse.Listener<>() { @Override public void onNext(PulseResponse response) { @@ -123,9 +123,5 @@ public void onCompleted() { 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-test/src/main/java/org/apache/hugegraph/pd/client/test/HgPDTestUtil.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/client/test/HgPDTestUtil.java deleted file mode 100644 index 2c581ea6ff..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/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-test/src/main/java/org/apache/hugegraph/pd/clitools/BaseCliToolsTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/clitools/BaseCliToolsTest.java deleted file mode 100644 index 146cffb139..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/clitools/BaseCliToolsTest.java +++ /dev/null @@ -1,34 +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.junit.After; -import org.junit.BeforeClass; - - -public class BaseCliToolsTest { - @BeforeClass - public static void init() { - - } - - @After - public void teardown() { - // pass - } -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/clitools/CliToolsSuiteTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/clitools/CliToolsSuiteTest.java deleted file mode 100644 index abdcbea08f..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/clitools/CliToolsSuiteTest.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.clitools; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -import lombok.extern.slf4j.Slf4j; - - -@RunWith(Suite.class) -@Suite.SuiteClasses({ - MainTest.class -}) - -@Slf4j -public class CliToolsSuiteTest { - - -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/clitools/MainTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/clitools/MainTest.java deleted file mode 100644 index 8b2f5f8318..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/clitools/MainTest.java +++ /dev/null @@ -1,91 +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; -import org.junit.Ignore; -import org.junit.Test; - -import lombok.extern.slf4j.Slf4j; - -@Slf4j -public class MainTest extends BaseCliToolsTest { - - - public static boolean test2sup(List 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)); - } - } - - @Ignore - @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"}); - } - - @Ignore - @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 aa = Arrays.asList(a); - System.out.printf(test2sup(aa, aa.size(), 0) ? "TRUE" : "FALSE"); - } - - -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/BaseCommonTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/BaseCommonTest.java deleted file mode 100644 index 591779c0d5..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/BaseCommonTest.java +++ /dev/null @@ -1,33 +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 org.junit.After; -import org.junit.BeforeClass; - -public class BaseCommonTest { - @BeforeClass - public static void init() { - - } - - @After - public void teardown() { - // pass - } -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/CommonSuiteTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/CommonSuiteTest.java index 0395711caa..fde560d78f 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/CommonSuiteTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/CommonSuiteTest.java @@ -22,7 +22,6 @@ import lombok.extern.slf4j.Slf4j; - @RunWith(Suite.class) @Suite.SuiteClasses({ PartitionUtilsTest.class, @@ -30,9 +29,7 @@ HgAssertTest.class, KVPairTest.class, }) - @Slf4j public class CommonSuiteTest { - } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/HgAssertTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/HgAssertTest.java index 253a9c67a0..3e61dd0a94 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/HgAssertTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/HgAssertTest.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.HashMap; -import org.apache.hugegraph.pd.common.HgAssert; import org.junit.Test; public class HgAssertTest { @@ -78,7 +77,6 @@ public void testIsNotNull() { HgAssert.isNotNull(null, ""); } - @Test public void testIsInvalid() { assertFalse(HgAssert.isInvalid("abc", "test")); diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/KVPairTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/KVPairTest.java index b3e1c15adf..9fb676d392 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/KVPairTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/KVPairTest.java @@ -17,7 +17,6 @@ package org.apache.hugegraph.pd.common; -import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.Assert; diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/PartitionCacheTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/PartitionCacheTest.java index d4126967f6..c070c80e00 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/PartitionCacheTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/PartitionCacheTest.java @@ -230,7 +230,6 @@ public void testRange() { var partition6 = createPartition(1, "graph0", 0, 1); this.cache.updatePartition(partition6); - System.out.println(this.cache.debugCacheByGraphName("graph0")); var partition5 = createPartition(1, "graph0", 0, 3); diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/PartitionUtilsTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/PartitionUtilsTest.java index 551bd40ecc..198d18b837 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/PartitionUtilsTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/common/PartitionUtilsTest.java @@ -18,13 +18,14 @@ package org.apache.hugegraph.pd.common; import java.nio.charset.StandardCharsets; -import lombok.extern.slf4j.Slf4j; + import org.junit.Assert; import org.junit.Test; +import lombok.extern.slf4j.Slf4j; @Slf4j -public class PartitionUtilsTest extends BaseCommonTest { +public class PartitionUtilsTest { @Test public void testCalcHashcode() { @@ -33,7 +34,7 @@ public void testCalcHashcode() { Assert.assertEquals(code, 31912L); } - // @Test + @Test public void testHashCode() { int partCount = 10; int partSize = PartitionUtils.MAX_VALUE / partCount + 1; @@ -41,11 +42,8 @@ public void testHashCode() { for (int i = 0; i < 10000; i++) { String s = String.format("BATCH-GET-UNIT-%02d", i); int c = PartitionUtils.calcHashcode(s.getBytes(StandardCharsets.UTF_8)); - counter[c / partSize]++; - } - for (int i = 0; i < counter.length; i++) { System.out.println(i + " " + counter[i]); } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/BaseCoreTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/BaseCoreTest.java deleted file mode 100644 index c2e7f652a0..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/BaseCoreTest.java +++ /dev/null @@ -1,75 +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.core; - -import java.io.File; -import java.io.IOException; - -import org.apache.commons.io.FileUtils; -import org.apache.hugegraph.pd.ConfigService; -import org.apache.hugegraph.pd.common.Useless; -import org.apache.hugegraph.pd.config.PDConfig; -import org.junit.After; -import org.junit.BeforeClass; - -@Useless -public class BaseCoreTest { - - static org.apache.hugegraph.pd.config.PDConfig pdConfig; - - @BeforeClass - public static void init() throws Exception { - String path = "tmp/unitTest"; - deleteDirectory(new File(path)); - pdConfig = new org.apache.hugegraph.pd.config.PDConfig() {{ - this.setClusterId(100); - this.setInitialStoreList("127.0.0.1:8500,127.0.0.1:8501,127.0.0.1:8502," + - "127.0.0.1:8503,127.0.0.1:8504,127.0.0.1:8505"); - }}; - - pdConfig.setStore(new org.apache.hugegraph.pd.config.PDConfig().new Store() {{ - this.setMaxDownTime(3600); - this.setKeepAliveTimeout(3600); - }}); - - pdConfig.setPartition(new org.apache.hugegraph.pd.config.PDConfig().new Partition() {{ - this.setShardCount(3); - this.setMaxShardsPerStore(3); - }}); - pdConfig.setRaft(new org.apache.hugegraph.pd.config.PDConfig().new Raft() {{ - this.setEnable(false); - }}); - pdConfig.setDiscovery(new PDConfig().new Discovery()); - pdConfig.setDataPath(path); - ConfigService configService = new ConfigService(pdConfig); - pdConfig = configService.loadConfig(); - } - - public static void deleteDirectory(File dir) { - try { - FileUtils.deleteDirectory(dir); - } catch (IOException e) { - System.out.printf("Failed to start ....,%s%n", e.getMessage()); - } - } - - @After - public void teardown() throws Exception { - // pass - } -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/ConfigServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/ConfigServiceTest.java index c6a7c12c8d..7ac5509bb1 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/ConfigServiceTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/ConfigServiceTest.java @@ -23,24 +23,21 @@ import org.apache.hugegraph.pd.IdService; import org.apache.hugegraph.pd.config.PDConfig; import org.apache.hugegraph.pd.grpc.Metapb; -import org.apache.hugegraph.pd.rest.BaseServerTest; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -public class ConfigServiceTest { - - private final PDConfig config = BaseServerTest.getConfig(); +public class ConfigServiceTest extends PDCoreTestBase { private ConfigService service; @Before public void setUp() { - this.service = new ConfigService(this.config); + this.service = new ConfigService(getPdConfig()); } @Test - public void testGetPDConfig() throws Exception { + public void testGetPDConfig() { // Setup try { final Metapb.PDConfig config = Metapb.PDConfig.newBuilder() @@ -50,17 +47,17 @@ public void testGetPDConfig() throws Exception { .setMaxShardsPerStore(0) .setTimestamp(0L).build(); this.service.setPDConfig(config); + // Run the test Metapb.PDConfig result = this.service.getPDConfig(0L); // Verify the results - Assert.assertTrue(result.getShardCount() == 55); + Assert.assertEquals(55, result.getShardCount()); result = this.service.getPDConfig(); - Assert.assertTrue(result.getShardCount() == 55); + Assert.assertEquals(55, result.getShardCount()); } catch (Exception e) { - + e.printStackTrace(); } - } @Test @@ -69,13 +66,14 @@ public void testGetGraphSpace() throws Exception { Metapb.GraphSpace space = Metapb.GraphSpace.newBuilder() .setName("gs1") .setTimestamp(0L).build(); - final List expectedResult = List.of(space); this.service.setGraphSpace(space); + // Run the test - final List result = this.service.getGraphSpace( - "gs1"); + final List result = this.service.getGraphSpace("gs1"); + // Verify the results Assert.assertEquals(1, result.size()); + Assert.assertEquals(space.getName(), result.get(0).getName()); } @Test @@ -101,7 +99,7 @@ public void testUpdatePDConfig() { expectedResult.setLicensePath("licensePath"); this.service.updatePDConfig(mConfig); } catch (Exception e) { - + e.printStackTrace(); } } } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/IdServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/IdServiceTest.java index dae690d898..48f20d0485 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/IdServiceTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/IdServiceTest.java @@ -17,21 +17,17 @@ package org.apache.hugegraph.pd.core; -import java.io.File; - -import org.apache.commons.io.FileUtils; import org.apache.hugegraph.pd.IdService; import org.apache.hugegraph.pd.config.PDConfig; -import org.apache.hugegraph.pd.meta.IdMetaStore; -import org.apache.hugegraph.pd.rest.BaseServerTest; import org.junit.Assert; import org.junit.Test; -public class IdServiceTest { +public class IdServiceTest extends PDCoreTestBase { + @Test public void testCid() { try { - PDConfig pdConfig = BaseServerTest.getConfig(); + PDConfig pdConfig = getPdConfig(); int max = 0x2000; IdService idService = new IdService(pdConfig); for (int i = 0; i < max; i++) { @@ -62,7 +58,7 @@ public void testCid() { Thread.sleep(5000); long cid3 = idService.getCId("test", "name", max); } catch (Exception e) { - + e.printStackTrace(); } // MetadataFactory.closeStore(); } @@ -70,16 +66,7 @@ public void testCid() { @Test public void testId() { try { - FileUtils.deleteQuietly(new File("tmp/testId/")); - IdMetaStore.CID_DEL_TIMEOUT = 2000; - PDConfig pdConfig = new PDConfig() {{ - this.setClusterId(100); - this.setPatrolInterval(1); - this.setRaft(new Raft() {{ - setEnable(false); - }}); - this.setDataPath("tmp/testId/"); - }}; + PDConfig pdConfig = getPdConfig(); IdService idService = new IdService(pdConfig); long first = idService.getId("abc", 100); Assert.assertEquals(first, 0L); @@ -88,8 +75,8 @@ public void testId() { idService.resetId("abc"); first = idService.getId("abc", 100); Assert.assertEquals(first, 0L); - } catch (Exception ignored) { - + } catch (Exception e) { + e.printStackTrace(); } // MetadataFactory.closeStore(); } @@ -97,7 +84,7 @@ public void testId() { @Test public void testMember() { try { - PDConfig pdConfig = BaseServerTest.getConfig(); + PDConfig pdConfig = getPdConfig(); IdService idService = new IdService(pdConfig); idService.setPdConfig(pdConfig); PDConfig config = idService.getPdConfig(); diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/KvServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/KvServiceTest.java index 8b8175013c..80cf6a2d2b 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/KvServiceTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/KvServiceTest.java @@ -19,16 +19,15 @@ import org.apache.hugegraph.pd.KvService; import org.apache.hugegraph.pd.config.PDConfig; -import org.apache.hugegraph.pd.rest.BaseServerTest; import org.junit.Assert; import org.junit.Test; -public class KvServiceTest { +public class KvServiceTest extends PDCoreTestBase { @Test public void testKv() { try { - PDConfig pdConfig = BaseServerTest.getConfig(); + PDConfig pdConfig = getPdConfig(); KvService service = new KvService(pdConfig); String key = "kvTest"; String kvTest = service.get(key); @@ -43,17 +42,18 @@ public void testKv() { service.put(key, "kvTestValue", 1000L); service.keepAlive(key); } catch (Exception e) { - + e.printStackTrace(); } } @Test public void testMember() { try { - PDConfig pdConfig = BaseServerTest.getConfig(); + PDConfig pdConfig = getPdConfig(); KvService service = new KvService(pdConfig); service.setPdConfig(pdConfig); PDConfig config = service.getPdConfig(); + // TODO } catch (Exception e) { e.printStackTrace(); } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/LogServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/LogServiceTest.java index ad6c7bfbbd..9588d6d171 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/LogServiceTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/LogServiceTest.java @@ -20,34 +20,29 @@ import java.util.List; import org.apache.hugegraph.pd.LogService; -import org.apache.hugegraph.pd.config.PDConfig; import org.apache.hugegraph.pd.grpc.Metapb; -import org.apache.hugegraph.pd.rest.BaseServerTest; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import com.google.protobuf.Any; -public class LogServiceTest { - - private final PDConfig mockPdConfig = BaseServerTest.getConfig(); +public class LogServiceTest extends PDCoreTestBase { private LogService logServiceUnderTest; @Before public void setUp() { - this.logServiceUnderTest = new LogService(this.mockPdConfig); + this.logServiceUnderTest = new LogService(getPdConfig()); } @Test public void testGetLog() throws Exception { - this.logServiceUnderTest.insertLog("action", "message", - Any.newBuilder().build()); + this.logServiceUnderTest.insertLog("action", "message", Any.newBuilder().build()); // Run the test - final List result = this.logServiceUnderTest.getLog( - "action", 0L, System.currentTimeMillis()); + final List result = + this.logServiceUnderTest.getLog("action", 0L, System.currentTimeMillis()); // Verify the results Assert.assertEquals(result.size(), 1); diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/MonitorServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/MonitorServiceTest.java deleted file mode 100644 index 546d288f2e..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/MonitorServiceTest.java +++ /dev/null @@ -1,122 +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.core; - -import java.util.concurrent.ExecutionException; - -import org.apache.hugegraph.pd.PartitionService; -import org.apache.hugegraph.pd.StoreNodeService; -import org.apache.hugegraph.pd.TaskScheduleService; -import org.apache.hugegraph.pd.common.PDException; -import org.apache.hugegraph.pd.config.PDConfig; -import org.apache.hugegraph.pd.grpc.Metapb; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -public class MonitorServiceTest { - static PDConfig pdConfig; - - @BeforeClass - public static void init() throws ExecutionException, InterruptedException { - pdConfig = new PDConfig() {{ - this.setClusterId(100); - this.setPatrolInterval(1); - this.setInitialStoreList("127.0.0.1:8500,127.0.0.1:8501,127.0.0.1:8502," + - "127.0.0.1:8503,127.0.0.1:8504,127.0.0.1:8505"); - }}; - - //pdConfig.setEtcd(new PDConfig().new Etcd() {{ - // this.setAddress("http://localhost:2379"); - // - //}}); - pdConfig.setStore(new PDConfig().new Store() {{ - this.setMaxDownTime(1); - this.setKeepAliveTimeout(5); - }}); - - pdConfig.setPartition(new PDConfig().new Partition() {{ - this.setShardCount(3); - this.setTotalCount(10); - }}); - - pdConfig.setRaft(new PDConfig().new Raft() {{ - this.setEnable(false); - }}); - - clearClusterData(); - } - - public static void clearClusterData() throws ExecutionException, InterruptedException { - //Client client = Client.builder().endpoints(pdConfig.getEtcd().getAddress()).build(); - //KV kvClient = client.getKVClient(); - // - //ByteSequence key = ByteSequence.from("HUGEGRAPH/" + pdConfig.getClusterId(), Charset - // .forName("utf-8")); - //CompletableFuture rsp = kvClient.delete(key, DeleteOption.newBuilder() - // .isPrefix(true).build()); - //System.out.println("删除数量 : " + rsp.get().getDeleted()); - //kvClient.close(); - //client.close(); - } - - @Ignore - @Test - public void testPatrolStores() throws PDException, InterruptedException { - StoreNodeService storeService = new StoreNodeService(pdConfig); - PartitionService partitionService = new PartitionService(pdConfig, storeService); - TaskScheduleService monitorService = - new TaskScheduleService(pdConfig, storeService, partitionService); - storeService.init(partitionService); - partitionService.init(); - monitorService.init(); - - int count = 6; - Metapb.Store[] stores = new Metapb.Store[count]; - for (int i = 0; i < count; i++) { - Metapb.Store store = Metapb.Store.newBuilder() - .setId(0) - .setAddress(String.valueOf(i)) - .setDeployPath("/data") - .addLabels(Metapb.StoreLabel.newBuilder() - .setKey("namespace") - .setValue("default") - .build()) - .build(); - stores[i] = storeService.register(store); - System.out.println("新注册store, id = " + Long.toHexString(stores[i].getId())); - } - Metapb.Graph graph = Metapb.Graph.newBuilder() - .setGraphName("defaultGH") - - .setPartitionCount(10) - .build(); - partitionService.updateGraph(graph); - Thread.sleep(10000); - count = 0; - count += storeService.getStores("").stream() - .filter(store -> store.getState() == Metapb.StoreState.Tombstone) - .count(); - - Assert.assertEquals(6, count); - - } - - -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PDCoreSuiteTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PDCoreSuiteTest.java index 71a8d972a1..3d785360d0 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PDCoreSuiteTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PDCoreSuiteTest.java @@ -24,7 +24,6 @@ import lombok.extern.slf4j.Slf4j; - @RunWith(Suite.class) @Suite.SuiteClasses({ MetadataKeyHelperTest.class, @@ -33,17 +32,12 @@ IdServiceTest.class, KvServiceTest.class, LogServiceTest.class, - MonitorServiceTest.class, PartitionServiceTest.class, StoreMonitorDataServiceTest.class, - StoreNodeServiceNewTest.class, - StoreNodeServiceTest.class, StoreServiceTest.class, TaskScheduleServiceTest.class }) - @Slf4j public class PDCoreSuiteTest { - } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PDCoreTestBase.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PDCoreTestBase.java index 244fe2e9b2..9e7b03d98e 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PDCoreTestBase.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PDCoreTestBase.java @@ -43,6 +43,7 @@ import org.junit.BeforeClass; public class PDCoreTestBase { + private static final String DATA_PATH = "/tmp/pd_data"; private static PDConfig pdConfig; private static StoreNodeService storeNodeService; @@ -61,6 +62,7 @@ public static void initService() throws PDException { config.setHost("127.0.0.1"); config.setVerifyPath(""); config.setLicensePath(""); + PDConfig.Raft raft = new PDConfig().new Raft(); raft.setAddress("127.0.0.1:8601"); raft.setPeersList("127.0.0.1:8601"); @@ -70,7 +72,6 @@ public static void initService() throws PDException { raft.setPort(8621); config.setRaft(raft); - config.setStore(new PDConfig().new Store()); config.setPartition(new PDConfig().new Partition() {{ setShardCount(1); @@ -97,7 +98,6 @@ public static void initService() throws PDException { RaftEngine.getInstance().addStateListener(partitionService); pdConfig.setIdService(idService); - storeNodeService.init(partitionService); partitionService.init(); partitionService.addInstructionListener(new PartitionInstructionListener() { diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PartitionServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PartitionServiceTest.java index 47815318a6..3514fcbe1d 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PartitionServiceTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/PartitionServiceTest.java @@ -44,7 +44,11 @@ public void init() { @Test public void testCombinePartition() throws PDException { buildEnv(); - // 0, 1, 2-> 0, 3,4,5->1, 6,7,8 ->2, 9,10, 11-> 3 + + // 0, 1, 2 -> 0 + // 3, 4, 5 -> 1 + // 6, 7, 8 -> 2 + // 9, 10, 11 -> 3 this.service.combinePartition(4); var partition = this.service.getPartitionById("graph0", 0); @@ -66,7 +70,11 @@ public void testCombinePartition() throws PDException { @Test public void testCombinePartition2() throws PDException { buildEnv(); - // 0, 1, 2-> 0, 3,4,5->1, 6,7,8 ->2, 9,10, 11-> 3 + + // 0, 1, 2 -> 0 + // 3, 4, 5 -> 1 + // 6, 7, 8 -> 2 + // 9, 10, 11 -> 3 this.service.combinePartition(4); var partition = this.service.getPartitionById("graph0", 0); @@ -130,7 +138,6 @@ private void buildEnv() throws PDException { lastId = partitionShard.getPartition().getEndKey(); } } - } @Test @@ -144,10 +151,6 @@ public void testPartitionHeartbeat() { .addAllShard(shardList).build(); List shardList2 = new ArrayList<>(stats.getShardList()); Collections.shuffle(shardList2); - shardList2.forEach(shard -> { - System.out.println(shard.getStoreId()); - }); - - + shardList2.forEach(shard -> System.out.println(shard.getStoreId())); } } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreMonitorDataServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreMonitorDataServiceTest.java index 8b17a38eb6..9caf9eaaeb 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreMonitorDataServiceTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreMonitorDataServiceTest.java @@ -31,7 +31,7 @@ public class StoreMonitorDataServiceTest extends PDCoreTestBase { - StoreMonitorDataService service; + private StoreMonitorDataService service; @Before public void init() { @@ -63,12 +63,10 @@ public void test() throws InterruptedException, PDException { assertNotNull(this.service.getStoreMonitorDataText(1)); - this.service.removeExpiredMonitorData(1, now + 1); assertEquals(0, this.service.getStoreMonitorData(1).size()); } - private Metapb.StoreStats genStats() { return Metapb.StoreStats.newBuilder() .setStoreId(1) @@ -78,5 +76,4 @@ private Metapb.StoreStats genStats() { .build(); } - } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreNodeServiceNewTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreNodeServiceNewTest.java deleted file mode 100644 index 199a02e65f..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreNodeServiceNewTest.java +++ /dev/null @@ -1,64 +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.core; - -import static org.junit.Assert.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.apache.hugegraph.pd.StoreNodeService; -import org.apache.hugegraph.pd.common.PDException; -import org.apache.hugegraph.pd.grpc.Metapb; -import org.junit.Before; -import org.junit.Test; - -public class StoreNodeServiceNewTest extends PDCoreTestBase { - private StoreNodeService service; - - @Before - public void init() { - this.service = getStoreNodeService(); - } - - @Test - public void testGetTaskInfoMeta() { - assertNotNull(this.service.getTaskInfoMeta()); - } - - public void testGetStoreInfoMeta() { - assertNotNull(this.service.getStoreInfoMeta()); - } - - @Test - public void testRemoveShardGroup() throws PDException { - for (int i = 0; i < 12; i++) { - Metapb.ShardGroup group = Metapb.ShardGroup.newBuilder() - .setId(i) - .setState( - Metapb.PartitionState.PState_Offline) - .build(); - this.service.getStoreInfoMeta().updateShardGroup(group); - } - - this.service.deleteShardGroup(11); - this.service.deleteShardGroup(10); - - assertEquals(10, getPdConfig().getConfigService().getPDConfig().getPartitionCount()); - // restore - getPdConfig().getConfigService().setPartitionCount(12); - } -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreNodeServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreNodeServiceTest.java deleted file mode 100644 index 8180ab8626..0000000000 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreNodeServiceTest.java +++ /dev/null @@ -1,479 +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.core; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.atomic.AtomicReference; - -import org.apache.commons.io.FileUtils; -import org.apache.hugegraph.pd.ConfigService; -import org.apache.hugegraph.pd.PartitionInstructionListener; -import org.apache.hugegraph.pd.PartitionService; -import org.apache.hugegraph.pd.PartitionStatusListener; -import org.apache.hugegraph.pd.StoreNodeService; -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.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; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -public class StoreNodeServiceTest { - static PDConfig pdConfig; - - @BeforeClass - public static void init() throws Exception { - String path = "tmp/unitTest"; - deleteDirectory(new File(path)); - pdConfig = new PDConfig() {{ - this.setClusterId(100); - this.setInitialStoreList( - "127.0.0.1:8500,127.0.0.1:8501,127.0.0.1:8502,127.0.0.1:8503,127.0.0.1:8504," + - "127.0.0.1:8505"); - }}; - - pdConfig.setStore(new PDConfig().new Store() {{ - this.setMaxDownTime(3600); - this.setKeepAliveTimeout(3600); - }}); - - pdConfig.setPartition(new PDConfig().new Partition() {{ - this.setShardCount(3); - this.setMaxShardsPerStore(3); - }}); - pdConfig.setRaft(new PDConfig().new Raft() {{ - this.setEnable(false); - }}); - pdConfig.setDiscovery(new PDConfig().new Discovery()); - pdConfig.setDataPath(path); - ConfigService configService = new ConfigService(pdConfig); - pdConfig = configService.loadConfig(); - } - - public static byte[] intToByteArray(int i) { - byte[] result = new byte[4]; - result[0] = (byte) ((i >> 24) & 0xFF); - result[1] = (byte) ((i >> 16) & 0xFF); - result[2] = (byte) ((i >> 8) & 0xFF); - result[3] = (byte) (i & 0xFF); - return result; - } - - public static void deleteDirectory(File dir) { - try { - FileUtils.deleteDirectory(dir); - } catch (IOException e) { - System.out.printf("Failed to start ....,%s%n", e.getMessage()); - } - } - - @Ignore - @Test - public void testStoreNodeService() throws PDException { - Assert.assertEquals(pdConfig.getPartition().getTotalCount(), - (long) pdConfig.getInitialStoreMap().size() * - pdConfig.getPartition().getMaxShardsPerStore() - / pdConfig.getPartition().getShardCount()); - StoreNodeService storeService = new StoreNodeService(pdConfig); - PartitionService partitionService = new PartitionService(pdConfig, storeService); - storeService.init(partitionService); - - int count = 6; - Metapb.Store[] stores = new Metapb.Store[count]; - for (int i = 0; i < count; i++) { - Metapb.Store store = Metapb.Store.newBuilder() - .setId(0) - .setAddress("127.0.0.1:850" + i) - .setDeployPath("/data") - .addLabels(Metapb.StoreLabel.newBuilder() - .setKey("namespace") - .setValue("default") - .build()) - .build(); - stores[i] = storeService.register(store); - System.out.println("新注册store, id = " + stores[i].getId()); - } - Assert.assertEquals(count, storeService.getStores("").size()); - - for (Metapb.Store store : stores) { - Metapb.StoreStats stats = Metapb.StoreStats.newBuilder() - .setStoreId(store.getId()) - .build(); - storeService.heartBeat(stats); - } - - Assert.assertEquals(6, storeService.getActiveStores("").size()); - - Metapb.Graph graph = Metapb.Graph.newBuilder() - .setGraphName("defaultGH") - .setPartitionCount(10) - .build(); - // 分配shard - List shards = storeService.allocShards(graph, 1); - - - Assert.assertEquals(3, shards.size()); - - Assert.assertEquals(pdConfig.getPartition().getTotalCount(), - storeService.getShardGroups().size()); // 设置leader - Metapb.Shard leader = Metapb.Shard.newBuilder(shards.get(0)) - .setRole(Metapb.ShardRole.Leader).build(); - shards = new ArrayList<>(shards); - shards.set(0, leader); - // 增加shard - pdConfig.getPartition().setShardCount(5); - - Metapb.ShardGroup shardGroup = Metapb.ShardGroup.newBuilder() - .setId(1) - .addAllShards(shards).build(); - shards = storeService.reallocShards(shardGroup); - - Assert.assertEquals(5, shards.size()); - // 减少shard - pdConfig.getPartition().setShardCount(3); - shards = storeService.reallocShards(shardGroup); - Assert.assertEquals(3, shards.size()); - // 包含leader,leader不能被删除 - Assert.assertTrue(shards.contains(leader)); - - // 减少shard - pdConfig.getPartition().setShardCount(1); - graph = Metapb.Graph.newBuilder(graph).build(); - shards = storeService.reallocShards(shardGroup); - Assert.assertEquals(1, shards.size()); - // 包含leader,leader不能被删除 - Assert.assertTrue(shards.contains(leader)); - - for (Metapb.Store store : stores) { - storeService.removeStore(store.getId()); - } - Assert.assertEquals(0, storeService.getStores("").size()); - - - } - - // @Test - public void testSplitPartition() throws PDException { - StoreNodeService storeService = new StoreNodeService(pdConfig); - PartitionService partitionService = new PartitionService(pdConfig, storeService); - storeService.init(partitionService); - partitionService.addInstructionListener(new PartitionInstructionListener() { - - @Override - public void changeShard(Metapb.Partition partition, ChangeShard changeShard) throws - PDException { - - } - - @Override - public void transferLeader(Metapb.Partition partition, - TransferLeader transferLeader) throws PDException { - - } - - @Override - public void splitPartition(Metapb.Partition partition, - SplitPartition splitPartition) throws PDException { - splitPartition.getNewPartitionList().forEach(p -> { - System.out.println("SplitPartition " + p.getId() + " " + p.getStartKey() + "," + - p.getEndKey()); - }); - } - - @Override - public void dbCompaction(Metapb.Partition partition, DbCompaction dbCompaction) throws - PDException { - - } - - @Override - public void movePartition(Metapb.Partition partition, - MovePartition movePartition) throws PDException { - - } - - @Override - public void cleanPartition(Metapb.Partition partition, - CleanPartition cleanPartition) throws PDException { - - } - - @Override - public void changePartitionKeyRange(Metapb.Partition partition, - PartitionKeyRange partitionKeyRange) throws - PDException { - - } - }); - int count = 6; - Metapb.Store[] stores = new Metapb.Store[count]; - for (int i = 0; i < count; i++) { - Metapb.Store store = Metapb.Store.newBuilder() - .setId(0) - .setAddress("127.0.0.1:850" + i) - .setDeployPath("/data") - .addLabels(Metapb.StoreLabel.newBuilder() - .setKey("namespace") - .setValue("default") - .build()) - .build(); - stores[i] = storeService.register(store); - System.out.println("新注册store, id = " + Long.toHexString(stores[i].getId())); - } - Assert.assertEquals(count, storeService.getStores().size()); - - Metapb.Graph graph = Metapb.Graph.newBuilder() - .setGraphName("defaultGH") - .build(); - Metapb.PartitionShard ptShard = - partitionService.getPartitionByCode(graph.getGraphName(), 0); - System.out.println(ptShard.getPartition().getId()); - { - Metapb.Partition pt = ptShard.getPartition(); - System.out.println(pt.getId() + " " + pt.getStartKey() + "," + pt.getEndKey()); - } - - Assert.assertEquals(6, storeService.getShardGroups().size()); - // storeService.splitShardGroups(ptShard.getPartition().getId(), 4); - Assert.assertEquals(9, storeService.getShardGroups().size()); - storeService.getShardGroups().forEach(shardGroup -> { - System.out.println("shardGroup id = " + shardGroup.getId()); - }); - } - - // @Test - public void testPartitionService() throws PDException, ExecutionException, - InterruptedException { - StoreNodeService storeService = new StoreNodeService(pdConfig); - int count = 6; - Metapb.Store[] stores = new Metapb.Store[count]; - for (int i = 0; i < count; i++) { - Metapb.Store store = Metapb.Store.newBuilder() - .setId(0) - .setAddress("127.0.0.1:850" + i) - .setDeployPath("/data") - .addLabels(Metapb.StoreLabel.newBuilder() - .setKey("namespace") - .setValue("default") - .build()) - .build(); - stores[i] = storeService.register(store); - System.out.println("新注册store, id = " + Long.toHexString(stores[i].getId())); - } - Assert.assertEquals(count, storeService.getStores("").size()); - - - PartitionService partitionService = new PartitionService(pdConfig, storeService); - - Metapb.Graph graph = Metapb.Graph.newBuilder() - .setGraphName("defaultGH") - - .setPartitionCount(10) - .build(); - // 申请分区 - Metapb.PartitionShard[] partitions = new Metapb.PartitionShard[10]; - for (int i = 0; i < partitions.length; i++) { - partitions[i] = - partitionService.getPartitionShard(graph.getGraphName(), intToByteArray(i)); - Assert.assertEquals(3, storeService.getShardGroup(i).getShardsCount()); - } - System.out.println( - "分区数量: " + partitionService.getPartitions(graph.getGraphName()).size()); - - int[] caseNo = {0}; //1 测试增加shard, 2 //测试store下线 - - Metapb.Shard leader = null; - int[] finalCaseNo = caseNo; - - partitionService.addInstructionListener(new PartitionInstructionListener() { - - @Override - public void changeShard(Metapb.Partition partition, ChangeShard changeShard) throws - PDException { - switch (finalCaseNo[0]) { - case 2: - Assert.assertEquals(5, storeService.getShardGroup(partition.getId()) - .getShardsCount()); - break; - case 3: - storeService.getShardGroup(partition.getId()).getShardsList() - .forEach(shard -> { - Assert.assertNotEquals(shard.getStoreId(), - stores[0].getId()); - }); - break; - } - - } - - @Override - public void transferLeader(Metapb.Partition partition, TransferLeader transferLeader) { - - } - - @Override - public void splitPartition(Metapb.Partition partition, SplitPartition splitPartition) { - } - - @Override - public void dbCompaction(Metapb.Partition partition, DbCompaction dbCompaction) throws - PDException { - - } - - @Override - public void movePartition(Metapb.Partition partition, - MovePartition movePartition) throws PDException { - - } - - @Override - public void cleanPartition(Metapb.Partition partition, - CleanPartition cleanPartition) throws PDException { - - } - - @Override - public void changePartitionKeyRange(Metapb.Partition partition, - PartitionKeyRange partitionKeyRange) - throws PDException { - - } - }); - Metapb.Partition partition = partitions[0].getPartition(); - leader = Metapb.Shard.newBuilder( - storeService.getShardGroup(partition.getId()).getShardsList().get(0)).build(); - Metapb.Shard finalLeader = leader; - partitionService.addStatusListener(new PartitionStatusListener() { - @Override - public void onPartitionChanged(Metapb.Partition partition, - Metapb.Partition newPartition) { - - } - - @Override - public void onPartitionRemoved(Metapb.Partition partition) { - - } - }); - // 测试修改图 - caseNo[0] = 1; - partitionService.updateGraph(graph); - for (int i = 0; i < partitions.length; i++) { - partitions[i] = - partitionService.getPartitionShard(graph.getGraphName(), intToByteArray(i)); - Assert.assertEquals(3, storeService.getShardGroup(i).getShardsCount()); - } - - graph = Metapb.Graph.newBuilder(graph) - .setGraphName("defaultGH") - - .setPartitionCount(10) - .build(); - caseNo[0] = 2; - partitionService.updateGraph(graph); - - // 测试store离线 - caseNo[0] = 3; - partitionService.storeOffline(stores[0]); - - - Metapb.PartitionStats stats = Metapb.PartitionStats.newBuilder() - .addGraphName(partition.getGraphName()) - .setId(partition.getId()) - .setLeader( - Metapb.Shard.newBuilder(leader) - .setRole( - Metapb.ShardRole.Leader)) - .build(); - // 测试leader飘移 - caseNo[0] = 4; - partitionService.partitionHeartbeat(stats); - AtomicReference shard = new AtomicReference<>(); - Metapb.PartitionShard ss = - partitionService.getPartitionShardById(partition.getGraphName(), partition.getId()); - storeService.getShardList(partition.getId()).forEach(s -> { - if (s.getRole() == Metapb.ShardRole.Leader) { - Assert.assertNull(shard.get()); - shard.set(s); - } - }); - - Assert.assertEquals(leader.getStoreId(), shard.get().getStoreId()); - - } - - // @Test - public void testMergeGraphParams() throws PDException { - StoreNodeService storeService = new StoreNodeService(pdConfig); - PartitionService partitionService = new PartitionService(pdConfig, storeService); - - Metapb.Graph dfGraph = Metapb.Graph.newBuilder() - - .setPartitionCount( - pdConfig.getPartition().getTotalCount()) - - .build(); - - Metapb.Graph graph1 = Metapb.Graph.newBuilder() - .setGraphName("test") - .setPartitionCount(20) - - .build(); - - Metapb.Graph graph2 = Metapb.Graph.newBuilder() - .setGraphName("test") - .setPartitionCount(7).build(); - Metapb.Graph graph3 = Metapb.Graph.newBuilder() - .setGraphName("test") - .build(); - Metapb.Graph graph4 = Metapb.Graph.newBuilder() - .setGraphName("test") - .build(); - - Metapb.Graph graph = Metapb.Graph.newBuilder(dfGraph).mergeFrom(graph2).build(); - Assert.assertEquals(graph2.getGraphName(), graph.getGraphName()); - - Assert.assertEquals(graph2.getPartitionCount(), graph.getPartitionCount()); - - - graph = Metapb.Graph.newBuilder(dfGraph).mergeFrom(graph3).build(); - Assert.assertEquals(graph3.getGraphName(), graph.getGraphName()); - - Assert.assertEquals(dfGraph.getPartitionCount(), graph.getPartitionCount()); - - - graph = Metapb.Graph.newBuilder(dfGraph).mergeFrom(graph4).build(); - Assert.assertEquals(graph4.getGraphName(), graph.getGraphName()); - - Assert.assertEquals(dfGraph.getPartitionCount(), graph.getPartitionCount()); - - } -} diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreServiceTest.java index 73f8b62233..a57b95f0c3 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreServiceTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/StoreServiceTest.java @@ -18,6 +18,7 @@ package org.apache.hugegraph.pd.core; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -25,20 +26,18 @@ import java.util.Map; import java.util.function.Consumer; -import org.apache.hugegraph.pd.ConfigService; -import org.apache.hugegraph.pd.IdService; import org.apache.hugegraph.pd.PartitionService; import org.apache.hugegraph.pd.StoreNodeService; import org.apache.hugegraph.pd.StoreStatusListener; +import org.apache.hugegraph.pd.common.PDException; 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.rest.BaseServerTest; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -public class StoreServiceTest { +public class StoreServiceTest extends PDCoreTestBase { private PDConfig config; @@ -46,18 +45,17 @@ public class StoreServiceTest { @Before public void setUp() { - this.config = getConfig(); + this.config = getPdConfig(); this.service = new StoreNodeService(this.config); } @Test public void testInit() { // Setup - PDConfig pdConfig = getConfig(); - final PDConfig pdConfig1 = getConfig(); + PDConfig pdConfig = getPdConfig(); final PartitionService partitionService = new PartitionService(pdConfig, new StoreNodeService( - pdConfig1)); + pdConfig)); // Run the test this.service.init(partitionService); @@ -65,30 +63,6 @@ public void testInit() { // Verify the results } - private PDConfig getConfig() { - PDConfig pdConfig = new PDConfig(); - pdConfig.setConfigService( - new ConfigService(BaseServerTest.getConfig())); - pdConfig.setIdService(new IdService(BaseServerTest.getConfig())); - pdConfig.setClusterId(0L); - pdConfig.setPatrolInterval(0L); - pdConfig.setDataPath("dataPath"); - pdConfig.setMinStoreCount(0); - pdConfig.setInitialStoreList("initialStoreList"); - pdConfig.setHost("host"); - pdConfig.setVerifyPath("verifyPath"); - pdConfig.setLicensePath("licensePath"); - PDConfig.Raft raft = new PDConfig().new Raft(); - raft.setEnable(false); - pdConfig.setRaft(raft); - final PDConfig.Partition partition = new PDConfig().new Partition(); - partition.setTotalCount(0); - partition.setShardCount(0); - pdConfig.setPartition(partition); - pdConfig.setInitialStoreMap(Map.ofEntries(Map.entry("value", "value"))); - return pdConfig; - } - @Test public void testIsOK() { // Setup @@ -215,7 +189,7 @@ public void testGetStore() throws Exception { // Run the test final Metapb.Store result = this.service.getStore(0L); } catch (Exception e) { - + e.printStackTrace(); } } @@ -289,43 +263,45 @@ public void testUpdateStore() throws Exception { final Metapb.Store result = this.service.updateStore(store); } - @Ignore @Test public void testStoreTurnoff() throws Exception { // Setup - final Metapb.Store store = Metapb.Store.newBuilder().setId(0L) - .setAddress("address") - .setRaftAddress("raftAddress") - .addLabels(Metapb.StoreLabel - .newBuilder() - .build()) - .setVersion("version").setState( - Metapb.StoreState.Unknown).setStartTimestamp(0L) - .setDeployPath("deployPath") - .setLastHeartbeat(0L).setStats( - Metapb.StoreStats.newBuilder().setStoreId(0L) - .setPartitionCount(0).addGraphStats( - Metapb.GraphStats.newBuilder() - .setGraphName("value") - .setApproximateSize(0L) - .setRole(Metapb.ShardRole.None) - .build()).build()) - .setDataVersion(0).setCores(0) - .setDataPath("dataPath").build(); + try { + final Metapb.Store store = Metapb.Store.newBuilder().setId(0L) + .setAddress("address") + .setRaftAddress("raftAddress") + .addLabels(Metapb.StoreLabel + .newBuilder() + .build()) + .setVersion("version").setState( + Metapb.StoreState.Unknown).setStartTimestamp(0L) + .setDeployPath("deployPath") + .setLastHeartbeat(0L).setStats( + Metapb.StoreStats.newBuilder().setStoreId(0L) + .setPartitionCount(0).addGraphStats( + Metapb.GraphStats.newBuilder() + .setGraphName("value") + .setApproximateSize(0L) + .setRole(Metapb.ShardRole.None) + .build()).build()) + .setDataVersion(0).setCores(0) + .setDataPath("dataPath").build(); - // Configure PDConfig.getPartition(...). - final PDConfig.Partition partition = new PDConfig().new Partition(); - partition.setTotalCount(0); - partition.setMaxShardsPerStore(0); - partition.setShardCount(0); + // Configure PDConfig.getPartition(...). + final PDConfig.Partition partition = new PDConfig().new Partition(); + partition.setTotalCount(0); + partition.setMaxShardsPerStore(0); + partition.setShardCount(0); - // Run the test - this.service.storeTurnoff(store); + // Run the test + this.service.storeTurnoff(store); - // Verify the results + // Verify the results + } catch (Exception e) { + e.printStackTrace(); + } } - @Test public void testGetStores1() throws Exception { // Setup @@ -376,7 +352,6 @@ public void testGetStores2() throws Exception { final List result = this.service.getStores("graphName"); } - @Test public void testGetStoreStatus() throws Exception { // Setup @@ -441,7 +416,6 @@ public void testGetShardGroup() throws Exception { // Verify the results } - @Test public void testGetShardGroupsByStore() throws Exception { // Setup @@ -488,13 +462,13 @@ public void testGetActiveStores1() throws Exception { public void testGetActiveStores1ThrowsPDException() { try { List stores = this.service.getActiveStores(); - assertThat(stores.size() == 0); + assertThat(stores.size()).isEqualTo(0); } catch (Exception e) { - + e.printStackTrace(); } } - @Ignore + @Ignore // state is Pending instead of Tombstone @Test public void testGetTombStores() throws Exception { // Setup @@ -521,13 +495,12 @@ public void testGetTombStores() throws Exception { final List result = this.service.getTombStores(); // Verify the results - assertThat(result.size() == 1); + assertThat(result.size()).isEqualTo(1); this.service.removeStore(result.get(0).getId()); List stores = this.service.getStores(); - assertThat(stores.size() == 0); + assertThat(stores.size()).isEqualTo(0); } - @Test public void testAllocShards() throws Exception { // Setup @@ -556,7 +529,7 @@ public void testAllocShards() throws Exception { // Run the test final List result = this.service.allocShards(graph, 0); } catch (Exception e) { - + e.printStackTrace(); } } @@ -595,7 +568,7 @@ public void testReallocShards() throws Exception { // Verify the results assertThat(result).isEqualTo(expectedResult); } catch (Exception e) { - + e.printStackTrace(); } } @@ -610,7 +583,7 @@ public void testUpdateShardGroup() { // Run the test this.service.updateShardGroup(0, shards, 0, 0); } catch (Exception e) { - + e.printStackTrace(); } } @@ -619,7 +592,7 @@ public void testUpdateShardGroupState() throws Exception { try { this.service.updateShardGroupState(0, Metapb.PartitionState.PState_None); } catch (Exception e) { - + e.printStackTrace(); } } @@ -661,11 +634,10 @@ public void testHeartBeat() throws Exception { // Verify the results assertThat(result).isEqualTo(expectedResult); } catch (Exception e) { - + e.printStackTrace(); } } - @Test public void testUpdateClusterStatus1() { // Setup @@ -764,6 +736,7 @@ public void testOnShardGroupSplit() { // Verify the results } + @Ignore // active stores are fewer than min store count in pd config @Test public void testCheckStoreCanOffline() { // Setup @@ -818,7 +791,7 @@ public void testShardGroupsDbCompaction() throws Exception { try { this.service.shardGroupsDbCompaction(0, "tableName"); } catch (Exception e) { - + e.printStackTrace(); } // Verify the results @@ -831,7 +804,27 @@ public void testGetQuota() throws Exception { try { this.service.getQuota(); } catch (Exception e) { + e.printStackTrace(); + } + } + // migrated from StoreNodeServiceNewTest + @Test + public void testRemoveShardGroup() throws PDException { + for (int i = 0; i < 12; i++) { + Metapb.ShardGroup group = Metapb.ShardGroup.newBuilder() + .setId(i) + .setState( + Metapb.PartitionState.PState_Offline) + .build(); + this.service.getStoreInfoMeta().updateShardGroup(group); } + + this.service.deleteShardGroup(11); + this.service.deleteShardGroup(10); + + assertEquals(10, getPdConfig().getConfigService().getPDConfig().getPartitionCount()); + // restore + getPdConfig().getConfigService().setPartitionCount(12); } } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/TaskScheduleServiceTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/TaskScheduleServiceTest.java index 4b70845ad8..fbccb47a11 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/TaskScheduleServiceTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/TaskScheduleServiceTest.java @@ -17,7 +17,7 @@ package org.apache.hugegraph.pd.core; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import java.util.ArrayList; import java.util.List; @@ -37,26 +37,28 @@ public void init() { this.service = getTaskService(); } - @Test + // TODO public void testStoreOffline() { } + // TODO public void testPatrolStores() { } + // TODO public void testPatrolPartitions() { } + // TODO public void testBalancePartitionShard() { } @Test public void testBalancePartitionLeader() throws PDException { - var list = new ArrayList(); for (int i = 0; i < 6; i++) { getStoreNodeService().getStoreInfoMeta().updateShardGroup(genShardGroup(i)); @@ -67,20 +69,23 @@ public void testBalancePartitionLeader() throws PDException { getPartitionService().updatePartition(list); var rst = this.service.balancePartitionLeader(true); - assertTrue(rst.size() > 0); + assertFalse(rst.isEmpty()); // recover getPdConfig().getPartition().setShardCount(1); getStoreNodeService().getStoreInfoMeta().removeAll(); } + // TODO public void testSplitPartition() { } + // TODO public void testSplitPartition2() { } + // TODO public void testCanAllPartitionsMovedOut() { } @@ -97,8 +102,8 @@ private Metapb.Partition genPartition(int groupId) { .setId(groupId) .setState(Metapb.PartitionState.PState_Normal) .setGraphName("graph1") - .setStartKey(groupId * 10) - .setEndKey(groupId * 10 + 10) + .setStartKey(groupId * 10L) + .setEndKey(groupId * 10L + 10) .build(); } diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/store/HgKVStoreImplTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/store/HgKVStoreImplTest.java index cfd5299ea0..5e77b6a829 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/store/HgKVStoreImplTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/core/store/HgKVStoreImplTest.java @@ -31,8 +31,9 @@ import org.junit.Test; public class HgKVStoreImplTest { - static final String testPath = "tmp/test"; - static PDConfig pdConfig; + + private static final String testPath = "tmp/test"; + private static PDConfig pdConfig; @BeforeClass public static void init() throws IOException { diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/grpc/BaseGrpcTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/grpc/BaseGrpcTest.java index 4ed8b39357..fbc2f3d8b3 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/grpc/BaseGrpcTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/grpc/BaseGrpcTest.java @@ -21,7 +21,7 @@ import org.junit.After; import org.junit.BeforeClass; -@Useless +@Useless("empty now") public class BaseGrpcTest { @BeforeClass diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/grpc/GrpcSuiteTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/grpc/GrpcSuiteTest.java index 44ffdf0649..0e2ab5db86 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/grpc/GrpcSuiteTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/grpc/GrpcSuiteTest.java @@ -23,11 +23,10 @@ import lombok.extern.slf4j.Slf4j; -@Useless +@Useless("empty now") @RunWith(Suite.class) @Suite.SuiteClasses({ }) - @Slf4j public class GrpcSuiteTest { diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/BaseServerTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/BaseServerTest.java index 5c397e8098..4aff85d1e9 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/BaseServerTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/BaseServerTest.java @@ -17,18 +17,15 @@ package org.apache.hugegraph.pd.rest; -import java.io.File; import java.net.http.HttpClient; -import org.apache.commons.io.FileUtils; -import org.apache.hugegraph.pd.config.PDConfig; import org.junit.After; import org.junit.BeforeClass; - public class BaseServerTest { - public static HttpClient client; - public static String pdRestAddr; + + protected static HttpClient client; + protected static String pdRestAddr; @BeforeClass public static void init() { @@ -36,19 +33,6 @@ public static void init() { pdRestAddr = "http://127.0.0.1:8620"; } - public static PDConfig getConfig() { - FileUtils.deleteQuietly(new File("tmp/test/")); - PDConfig pdConfig = new PDConfig() {{ - this.setClusterId(100); - this.setPatrolInterval(1); - this.setRaft(new Raft() {{ - setEnable(false); - }}); - this.setDataPath("tmp/test/"); - }}; - return pdConfig; - } - @After public void teardown() { // pass diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/PDRestSuiteTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/PDRestSuiteTest.java index d16d21208c..f804290d5a 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/PDRestSuiteTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/PDRestSuiteTest.java @@ -26,7 +26,6 @@ @Suite.SuiteClasses({ RestApiTest.class, }) - @Slf4j public class PDRestSuiteTest { diff --git a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/RestApiTest.java b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/RestApiTest.java index b3165ab30b..813d7f0656 100644 --- a/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/RestApiTest.java +++ b/hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/RestApiTest.java @@ -28,6 +28,7 @@ import org.junit.Test; public class RestApiTest extends BaseServerTest { + @Test public void testQueryClusterInfo() throws URISyntaxException, IOException, InterruptedException, JSONException { diff --git a/hugegraph-pd/pom.xml b/hugegraph-pd/pom.xml index 11b508024e..a43767cceb 100644 --- a/hugegraph-pd/pom.xml +++ b/hugegraph-pd/pom.xml @@ -38,9 +38,7 @@ hg-pd-service hg-pd-common hg-pd-dist - hg-pd-clitools hg-pd-test - @@ -196,30 +194,6 @@ - - pd-cli-tools-test - - true - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.20 - - - pd-cli-tools-test - - test - - test - - - - - - pd-common-test