Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pd): integrate pd-core into hugegraph #2478

Merged
merged 9 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions hugegraph-pd/hg-pd-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.hugegraph</groupId>
<artifactId>hugegraph-pd</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>hg-pd-core</artifactId>

<properties>
<jetcd-version>0.5.10</jetcd-version>
</properties>
<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>jraft-core</artifactId>
<!-- TODO: use open source version & adopt the code later -->
<version>1.3.13</version>
<exclusions>
<exclusion>
<groupId>org.rocksdb</groupId>
<artifactId>rocksdbjni</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.rocksdb</groupId>
<artifactId>rocksdbjni</artifactId>
<version>6.29.5</version>
</dependency>
<dependency>
<groupId>org.apache.hugegraph</groupId>
<artifactId>hg-pd-grpc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.apache.hugegraph</groupId>
<artifactId>hg-pd-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.5.14</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hugegraph.pd;

import java.util.List;

import org.apache.hugegraph.pd.common.PDException;
import org.apache.hugegraph.pd.config.PDConfig;
import org.apache.hugegraph.pd.grpc.Metapb;
import org.apache.hugegraph.pd.meta.ConfigMetaStore;
import org.apache.hugegraph.pd.meta.MetadataFactory;
import org.apache.hugegraph.pd.raft.RaftEngine;
import org.apache.hugegraph.pd.raft.RaftStateListener;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ConfigService implements RaftStateListener {

private final ConfigMetaStore meta;
private PDConfig pdConfig;

public ConfigService(PDConfig config) {
this.pdConfig = config;
config.setConfigService(this);
meta = MetadataFactory.newConfigMeta(config);
}

public Metapb.PDConfig getPDConfig(long version) throws PDException {
return this.meta.getPdConfig(version);
}

public Metapb.PDConfig getPDConfig() throws PDException {
return this.meta.getPdConfig(0);
}

public Metapb.PDConfig setPDConfig(Metapb.PDConfig mConfig) throws PDException {
Metapb.PDConfig oldCfg = getPDConfig();
Metapb.PDConfig.Builder builder = oldCfg.toBuilder().mergeFrom(mConfig)
.setVersion(oldCfg.getVersion() + 1)
.setTimestamp(System.currentTimeMillis());
mConfig = this.meta.setPdConfig(builder.build());
log.info("PDConfig has been modified, new PDConfig is {}", mConfig);
updatePDConfig(mConfig);
return mConfig;
}

public List<Metapb.GraphSpace> getGraphSpace(String graphSpaceName) throws PDException {
return this.meta.getGraphSpace(graphSpaceName);
}

public Metapb.GraphSpace setGraphSpace(Metapb.GraphSpace graphSpace) throws PDException {
return this.meta.setGraphSpace(graphSpace.toBuilder()
.setTimestamp(System.currentTimeMillis())
.build());
}

/**
* 从存储中读取配置项,并覆盖全局的PDConfig对象
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be translated

*
* @return
*/
public PDConfig loadConfig() {
try {
Metapb.PDConfig mConfig = this.meta.getPdConfig(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to also keep name pdConfig

if (mConfig == null) {
mConfig = Metapb.PDConfig.newBuilder()
.setPartitionCount(pdConfig.getInitialPartitionCount())
.setShardCount(pdConfig.getPartition().getShardCount())
.setVersion(1)
.setTimestamp(System.currentTimeMillis())
.setMaxShardsPerStore(
pdConfig.getPartition().getMaxShardsPerStore())
.build();
}
if (RaftEngine.getInstance().isLeader()) {
this.meta.setPdConfig(mConfig);
}
pdConfig = updatePDConfig(mConfig);
} catch (Exception e) {
log.error("ConfigService loadConfig exception {}", e);
}
return pdConfig;
}

public synchronized PDConfig updatePDConfig(Metapb.PDConfig mConfig) {
log.info("update pd config: mConfig:{}", mConfig);
pdConfig.getPartition().setShardCount(mConfig.getShardCount());
pdConfig.getPartition().setTotalCount(mConfig.getPartitionCount());
pdConfig.getPartition().setMaxShardsPerStore(mConfig.getMaxShardsPerStore());
return pdConfig;
}

public synchronized PDConfig setPartitionCount(int count) {
Metapb.PDConfig mConfig = null;
try {
mConfig = getPDConfig();
mConfig = mConfig.toBuilder().setPartitionCount(count).build();
setPDConfig(mConfig);
} catch (PDException e) {
log.error("ConfigService exception {}", e);
e.printStackTrace();
}
return pdConfig;
}

/**
* meta store中的数量
* 由于可能会受分区分裂/合并的影响,原始的partition count不推荐使用
*
* @return partition count of cluster
* @throws PDException when io error
*/
public int getPartitionCount() throws PDException {
return getPDConfig().getPartitionCount();
}

@Override
public void onRaftLeaderChanged() {
loadConfig();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hugegraph.pd;

import org.apache.hugegraph.pd.common.PDException;
import org.apache.hugegraph.pd.config.PDConfig;
import org.apache.hugegraph.pd.meta.IdMetaStore;
import org.apache.hugegraph.pd.meta.MetadataFactory;

public class IdService {

private final IdMetaStore meta;
private PDConfig pdConfig;

public IdService(PDConfig config) {
this.pdConfig = config;
meta = MetadataFactory.newHugeServerMeta(config);
}

public PDConfig getPdConfig() {
return pdConfig;
}

public void setPdConfig(PDConfig pdConfig) {
this.pdConfig = pdConfig;
}

public long getId(String key, int delta) throws PDException {
return meta.getId(key, delta);
}

public void resetId(String key) throws PDException {
meta.resetId(key);
}

/**
* 获取自增循环不重复id, 达到上限后从0开始自增.自动跳过正在使用的cid
*
* @param key
* @param max
* @return
* @throws PDException
*/
public long getCId(String key, long max) throws PDException {
return meta.getCId(key, max);
}

public long getCId(String key, String name, long max) throws PDException {
return meta.getCId(key, name, max);
}

/**
* 删除一个自增循环id
*
* @param key
* @param cid
* @return
* @throws PDException
*/
public long delCId(String key, long cid) throws PDException {
return meta.delCId(key, cid);
}

public long delCIdDelay(String key, String name, long cid) throws PDException {
return meta.delCIdDelay(key, name, cid);
}
}
Loading
Loading