-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from crossoverJie/refator-metastore
Refactor Metastore
- Loading branch information
Showing
20 changed files
with
486 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
cim-common/src/main/java/com/crossoverjie/cim/common/metastore/AbstractConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.crossoverjie.cim.common.metastore; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
/** | ||
* @author crossverJie | ||
*/ | ||
@Data | ||
@Builder | ||
public class AbstractConfiguration<RETRY> { | ||
|
||
private String metaServiceUri; | ||
private int timeoutMs; | ||
private RETRY retryPolicy; | ||
} |
49 changes: 49 additions & 0 deletions
49
cim-common/src/main/java/com/crossoverjie/cim/common/metastore/MetaStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.crossoverjie.cim.common.metastore; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author crossoverJie | ||
*/ | ||
public interface MetaStore { | ||
|
||
void initialize(AbstractConfiguration<?> configuration) throws Exception; | ||
|
||
/** | ||
* Get available server list | ||
* @return available server list | ||
* @throws Exception exception | ||
*/ | ||
Set<String> getAvailableServerList() throws Exception; | ||
|
||
/** | ||
* Add server to meta store | ||
* @throws Exception exception | ||
*/ | ||
void addServer(String ip, int cimServerPort, int httpPort) throws Exception; | ||
|
||
/** | ||
* Subscribe server list | ||
* @param childListener child listener | ||
* @throws Exception exception | ||
*/ | ||
void listenServerList(ChildListener childListener) throws Exception; | ||
|
||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
void rebuildCache() throws Exception; | ||
|
||
interface ChildListener { | ||
/** | ||
* Child changed | ||
* @param parentPath parent path(eg. for zookeeper: [/cim]) | ||
* @param currentChildren current children | ||
* @throws Exception exception | ||
*/ | ||
void childChanged(String parentPath, List<String> currentChildren) throws Exception; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
cim-common/src/main/java/com/crossoverjie/cim/common/metastore/ZkConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.crossoverjie.cim.common.metastore; | ||
|
||
import org.apache.curator.RetryPolicy; | ||
|
||
/** | ||
* @author crossoverJie | ||
*/ | ||
public class ZkConfiguration extends AbstractConfiguration<RetryPolicy> { | ||
ZkConfiguration(String metaServiceUri, int timeout, RetryPolicy retryPolicy) { | ||
super(metaServiceUri, timeout, retryPolicy); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
cim-common/src/main/java/com/crossoverjie/cim/common/metastore/ZkMetaStoreImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.crossoverjie.cim.common.metastore; | ||
|
||
import com.crossoverjie.cim.common.pojo.RouteInfo; | ||
import com.crossoverjie.cim.common.util.RouteInfoParseUtil; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.I0Itec.zkclient.ZkClient; | ||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.zookeeper.CreateMode; | ||
import org.apache.zookeeper.Watcher; | ||
|
||
/** | ||
* @author crossovreJie | ||
*/ | ||
@Slf4j | ||
public class ZkMetaStoreImpl implements MetaStore { | ||
public static final String ROOT = "/cim"; | ||
|
||
private ZkClient client; | ||
|
||
LoadingCache<String, String> cache; | ||
|
||
@Override | ||
public void initialize(AbstractConfiguration<?> configuration) throws Exception { | ||
// TODO: 2024/8/19 Change to set or caffeine? | ||
cache = CacheBuilder.newBuilder() | ||
.concurrencyLevel(3) | ||
.build(new CacheLoader<>() { | ||
@Override | ||
public String load(String s) { | ||
return null; | ||
} | ||
}); | ||
client = new ZkClient(configuration.getMetaServiceUri(), configuration.getTimeoutMs()); | ||
} | ||
|
||
@Override | ||
public Set<String> getAvailableServerList() throws Exception { | ||
if (cache.size() > 0) { | ||
return cache.asMap().keySet(); | ||
} | ||
List<String> coll = client.getChildren(ROOT); | ||
Map<String, String> voidMap = coll.stream().collect(Collectors.toMap( | ||
Function.identity(), | ||
Function.identity() | ||
)); | ||
cache.putAll(voidMap); | ||
return voidMap.keySet(); | ||
} | ||
|
||
@Override | ||
public void addServer(String ip, int cimServerPort, int httpPort) throws Exception { | ||
boolean exists = client.exists(ROOT); | ||
if (!exists) { | ||
client.createPersistent(ROOT); | ||
} | ||
String zkParse = RouteInfoParseUtil.parse(RouteInfo.builder() | ||
.ip(ip) | ||
.cimServerPort(cimServerPort) | ||
.httpPort(httpPort) | ||
.build()); | ||
String serverPath = String.format("%s/%s", ROOT, zkParse); | ||
client.createEphemeral(serverPath); | ||
log.info("Add server to zk [{}]", serverPath); | ||
} | ||
|
||
@Override | ||
public void listenServerList(ChildListener childListener) throws Exception { | ||
client.subscribeChildChanges(ROOT, (parentPath, currentChildren) -> { | ||
log.info("Clear and update local cache parentPath=[{}],currentChildren=[{}]", parentPath, currentChildren.toString()); | ||
childListener.childChanged(parentPath, currentChildren); | ||
|
||
// TODO: 2024/8/19 maybe can reuse currentChildren. | ||
// Because rebuildCache() will re-fetch the server list from zk. | ||
rebuildCache(); | ||
}); | ||
} | ||
|
||
@Override | ||
public synchronized void rebuildCache() throws Exception { | ||
cache.invalidateAll(); | ||
|
||
// Because of calling invalidateAll, this method will re-fetch the server list from zk. | ||
this.getAvailableServerList(); | ||
|
||
} | ||
|
||
|
||
private List<String> watchedGetChildren(CuratorFramework client, String path) throws Exception { | ||
/** | ||
* Get children and set a watcher on the node. The watcher notification will come through the | ||
* CuratorListener (see setDataAsync() above). | ||
*/ | ||
return client.getChildren().watched().forPath(path); | ||
} | ||
|
||
private void createEphemeral(CuratorFramework client, String path, byte[] payload) throws Exception { | ||
// this will create the given EPHEMERAL ZNode with the given data | ||
client.create().withMode(CreateMode.EPHEMERAL).forPath(path, payload); | ||
} | ||
|
||
private void create(CuratorFramework client, String path, byte[] payload) throws Exception { | ||
// this will create the given ZNode with the given data | ||
client.create().forPath(path, payload); | ||
} | ||
|
||
private void watchedGetChildren(CuratorFramework client, String path, Watcher watcher) | ||
throws Exception { | ||
// Get children and set the given watcher on the node. | ||
client.getChildren().usingWatcher(watcher).forPath(path); | ||
} | ||
} |
37 changes: 7 additions & 30 deletions
37
cim-common/src/main/java/com/crossoverjie/cim/common/pojo/RouteInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,22 @@ | ||
package com.crossoverjie.cim.common.pojo; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
/** | ||
* Function: | ||
* | ||
* @author crossoverJie | ||
* Date: 2020-04-12 20:48 | ||
* @since JDK 1.8 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@Builder | ||
public final class RouteInfo { | ||
|
||
private String ip ; | ||
private Integer cimServerPort; | ||
private Integer httpPort; | ||
|
||
public RouteInfo(String ip, Integer cimServerPort, Integer httpPort) { | ||
this.ip = ip; | ||
this.cimServerPort = cimServerPort; | ||
this.httpPort = httpPort; | ||
} | ||
|
||
public String getIp() { | ||
return ip; | ||
} | ||
|
||
public void setIp(String ip) { | ||
this.ip = ip; | ||
} | ||
|
||
public Integer getCimServerPort() { | ||
return cimServerPort; | ||
} | ||
|
||
public void setCimServerPort(Integer cimServerPort) { | ||
this.cimServerPort = cimServerPort; | ||
} | ||
|
||
public Integer getHttpPort() { | ||
return httpPort; | ||
} | ||
|
||
public void setHttpPort(Integer httpPort) { | ||
this.httpPort = httpPort; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.