Skip to content

Commit

Permalink
avoid using reflection in meta clone (#758)
Browse files Browse the repository at this point in the history
Co-authored-by: lishanglin <[email protected]>
  • Loading branch information
2 people authored and hailu committed Dec 28, 2023
1 parent 24b2c57 commit 4b1116f
Show file tree
Hide file tree
Showing 48 changed files with 671 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.ctrip.xpipe.redis.core.AbstractRedisTest;
import com.ctrip.xpipe.redis.core.entity.*;
import com.ctrip.xpipe.redis.core.meta.MetaCache;
import com.ctrip.xpipe.redis.core.meta.MetaClone;
import com.ctrip.xpipe.redis.core.meta.clone.MetaCloneFacade;
import com.ctrip.xpipe.redis.core.meta.comparator.ClusterMetaComparator;
import com.google.common.collect.Sets;
import org.junit.Assert;
Expand Down Expand Up @@ -86,7 +86,7 @@ private DcMeta cloneDcMeta(String dc) {
}

private DcMeta cloneDcMeta(DcMeta source){
DcMeta dcMeta = MetaClone.clone(source);
DcMeta dcMeta = MetaCloneFacade.INSTANCE.clone(source);
for (ClusterMeta clusterMeta: dcMeta.getClusters().values()) {
clusterMeta.setParent(dcMeta);
for (ShardMeta shardMeta: clusterMeta.getShards().values()) {
Expand All @@ -106,7 +106,7 @@ private DcMeta cloneDcMeta(DcMeta source){
@Test
public void visitModified() {
ClusterMeta clusterMeta = getDcMeta("oy").findCluster("cluster2");
ClusterMeta clone = MetaClone.clone(clusterMeta);
ClusterMeta clone = MetaCloneFacade.INSTANCE.clone(clusterMeta);
clone.getShards().get("shard2").addRedis(new RedisMeta());
manager.visitModified(new ClusterMetaComparator(clusterMeta, clone));
verify(instanceManager, never()).getOrCreate(any(RedisMeta.class));
Expand Down Expand Up @@ -140,7 +140,7 @@ public void testShardConfigChange() throws Exception {
public void testActiveDcInterestedNotChange() {
// active dc is always not current dc jq
ClusterMeta cluster = getDcMeta("oy").findCluster("cluster2");
ClusterMeta newCluster = MetaClone.clone(cluster);
ClusterMeta newCluster = MetaCloneFacade.INSTANCE.clone(cluster);
newCluster.setActiveDc("rb");
manager.visitModified(new ClusterMetaComparator(cluster, newCluster));

Expand All @@ -149,7 +149,7 @@ public void testActiveDcInterestedNotChange() {

// active dc is always current dc jq
cluster = getDcMeta("oy").findCluster("cluster1");
newCluster = MetaClone.clone(cluster);
newCluster = MetaCloneFacade.INSTANCE.clone(cluster);
manager.visitModified(new ClusterMetaComparator(cluster, newCluster));
Mockito.verify(instanceManager, never()).getOrCreate(any(RedisMeta.class));
Mockito.verify(instanceManager, never()).remove(any(HostPort.class));
Expand Down Expand Up @@ -184,7 +184,7 @@ public void testActiveDcJQ2OY() {
public void testDcsInterestedNotChange() {
// current dc is always not in dcs
ClusterMeta cluster = getDcMeta("oy").findCluster("cluster4");
ClusterMeta newCluster = MetaClone.clone(cluster);
ClusterMeta newCluster = MetaCloneFacade.INSTANCE.clone(cluster);
newCluster.setDcs("rb");
manager.visitModified(new ClusterMetaComparator(cluster, newCluster));

Expand All @@ -193,7 +193,7 @@ public void testDcsInterestedNotChange() {

// current dc is always in dcs
cluster = getDcMeta("oy").findCluster("cluster3");
newCluster = MetaClone.clone(cluster);
newCluster = MetaCloneFacade.INSTANCE.clone(cluster);
newCluster.setDcs("jq,oy,rb");
manager.visitModified(new ClusterMetaComparator(cluster, newCluster));

Expand Down Expand Up @@ -244,7 +244,7 @@ public void visitRemoved() {
verify(manager, never()).visitAdded(any());
verify(manager, never()).visitRemoved(any());

DcMeta dcMeta = MetaClone.clone(getDcMeta("oy"));
DcMeta dcMeta = MetaCloneFacade.INSTANCE.clone(getDcMeta("oy"));

ClusterMeta clusterMeta = dcMeta.getClusters().remove("cluster1");
clusterMeta.setId("cluster5").setDbId(Math.abs(randomLong())).getShards().values().forEach(shardMeta -> {
Expand All @@ -266,7 +266,7 @@ public void visitRemovedClusterActiveDc(){
manager = spy(new DefaultDcMetaChangeManager("jq", instanceManager, factory, checkerConsoleService, checkerConfig));
manager.compare(getDcMeta("jq"), null);

DcMeta dcMeta = MetaClone.clone(getDcMeta("jq"));
DcMeta dcMeta = MetaCloneFacade.INSTANCE.clone(getDcMeta("jq"));
dcMeta.getClusters().remove("cluster1");

manager.compare(dcMeta, null);
Expand Down Expand Up @@ -482,7 +482,7 @@ public void generateHeteroHealthCheckInstancesTest() throws Exception {
@Test
public void visitModified1() {
ClusterMeta clusterMeta = getDcMeta("oy").findCluster("cluster2");
ClusterMeta clone = MetaClone.clone(clusterMeta);
ClusterMeta clone = MetaCloneFacade.INSTANCE.clone(clusterMeta);
clone.getShards().get("shard2").addRedis(new RedisMeta());
manager.visitModified(new ClusterMetaComparator(clusterMeta, clone));
verify(instanceManager, never()).getOrCreate(any(RedisMeta.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ctrip.xpipe.redis.core.meta;

/**
* @author lishanglin
* date 2023/12/13
*/
public interface InnerMetaClone<T> {

T clone(T o);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ctrip.xpipe.redis.core.meta.clone;

import com.ctrip.xpipe.redis.core.entity.ApplierContainerMeta;
import com.ctrip.xpipe.redis.core.meta.InnerMetaClone;

/**
* @author lishanglin
* date 2023/12/14
*/
public class ApplierContainerMetaClone implements InnerMetaClone<ApplierContainerMeta> {

@Override
public ApplierContainerMeta clone(ApplierContainerMeta o) {
ApplierContainerMeta clone = new ApplierContainerMeta();
clone.setId(o.getId());
clone.mergeAttributes(o);
return clone;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ctrip.xpipe.redis.core.meta.clone;

import com.ctrip.xpipe.redis.core.entity.ApplierMeta;
import com.ctrip.xpipe.redis.core.meta.InnerMetaClone;

/**
* @author lishanglin
* date 2023/12/14
*/
public class ApplierMetaClone implements InnerMetaClone<ApplierMeta> {

@Override
public ApplierMeta clone(ApplierMeta o) {
ApplierMeta clone = new ApplierMeta();
clone.setId(o.getId());
clone.mergeAttributes(o);
return clone;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ctrip.xpipe.redis.core.meta.clone;

import com.ctrip.xpipe.redis.core.entity.AzMeta;
import com.ctrip.xpipe.redis.core.meta.InnerMetaClone;

/**
* @author lishanglin
* date 2023/12/13
*/
public class AzMetaClone implements InnerMetaClone<AzMeta> {

@Override
public AzMeta clone(AzMeta o) {
AzMeta clone = new AzMeta();
clone.setId(o.getId());
clone.mergeAttributes(o);
return clone;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ctrip.xpipe.redis.core.meta.clone;

import com.ctrip.xpipe.redis.core.BaseEntity;
import com.ctrip.xpipe.redis.core.meta.InnerMetaClone;

/**
* @author lishanglin
* date 2023/12/13
*/
public class BaseEntityClone<T extends BaseEntity> implements InnerMetaClone<T> {

@Override
public T clone(T o) {
try {
T clone = (T)o.getClass().newInstance();
clone.mergeAttributes(o);
return clone;
} catch (InstantiationException|IllegalAccessException e) {
throw new MetaCloneException(o.getClass().getSimpleName(), e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ctrip.xpipe.redis.core.meta.clone;

import com.ctrip.xpipe.redis.core.entity.ClusterMeta;
import com.ctrip.xpipe.redis.core.entity.ShardMeta;
import com.ctrip.xpipe.redis.core.entity.SourceMeta;
import com.ctrip.xpipe.redis.core.meta.InnerMetaClone;

/**
* @author lishanglin
* date 2023/12/13
*/
public class ClusterMetaClone implements InnerMetaClone<ClusterMeta> {

@Override
public ClusterMeta clone(ClusterMeta o) {
ClusterMeta clone = new ClusterMeta();
clone.setId(o.getId());
clone.mergeAttributes(o);

if (null != o.getSources()) {
for (SourceMeta sourceMeta: o.getSources()) {
SourceMeta cloneSource = MetaCloneFacade.INSTANCE.clone(sourceMeta);
clone.addSource(cloneSource);
}
}

if (null != o.getShards()) {
for (ShardMeta shardMeta: o.getShards().values()) {
ShardMeta cloneShard = MetaCloneFacade.INSTANCE.clone(shardMeta);
clone.addShard(cloneShard);
}
}

return clone;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.ctrip.xpipe.redis.core.meta.clone;

import com.ctrip.xpipe.redis.core.entity.*;
import com.ctrip.xpipe.redis.core.meta.InnerMetaClone;

/**
* @author lishanglin
* date 2023/12/13
*/
public class DcMetaClone implements InnerMetaClone<DcMeta> {

@Override
public DcMeta clone(DcMeta o) {
DcMeta clone = new DcMeta();
clone.setId(o.getId());
clone.mergeAttributes(o);

if (null != o.getClusters()) {
for (ClusterMeta clusterMeta: o.getClusters().values()) {
ClusterMeta cloneCluster = MetaCloneFacade.INSTANCE.clone(clusterMeta);
clone.addCluster(cloneCluster);
}
}

if (null != o.getRoutes()) {
for (RouteMeta routeMeta: o.getRoutes()) {
RouteMeta cloneRoute = MetaCloneFacade.INSTANCE.clone(routeMeta);
clone.addRoute(cloneRoute);
}
}

if (null != o.getKeeperContainers()) {
for (KeeperContainerMeta keeperContainerMeta: o.getKeeperContainers()) {
KeeperContainerMeta cloneKeeperContainer = MetaCloneFacade.INSTANCE.clone(keeperContainerMeta);
clone.addKeeperContainer(cloneKeeperContainer);
}
}

if (null != o.getApplierContainers()) {
for (ApplierContainerMeta applierContainerMeta: o.getApplierContainers()) {
ApplierContainerMeta cloneApplierContainer = MetaCloneFacade.INSTANCE.clone(applierContainerMeta);
clone.addApplierContainer(cloneApplierContainer);
}
}

if (null != o.getSentinels()) {
for (SentinelMeta sentinelMeta: o.getSentinels().values()) {
SentinelMeta cloneSentinel = MetaCloneFacade.INSTANCE.clone(sentinelMeta);
clone.addSentinel(cloneSentinel);
}
}

if (null != o.getMetaServers()) {
for (MetaServerMeta metaServerMeta: o.getMetaServers()) {
MetaServerMeta cloneMetaServer = MetaCloneFacade.INSTANCE.clone(metaServerMeta);
clone.addMetaServer(cloneMetaServer);
}
}

if (null != o.getAzs()) {
for (AzMeta azMeta: o.getAzs()) {
AzMeta cloneAz = MetaCloneFacade.INSTANCE.clone(azMeta);
clone.addAz(cloneAz);
}
}

if (null != o.getZkServer()) {
ZkServerMeta cloneZkServer = MetaCloneFacade.INSTANCE.clone(o.getZkServer());
cloneZkServer.setParent(clone);
clone.setZkServer(cloneZkServer);
}

return clone;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ctrip.xpipe.redis.core.meta.clone;

import com.ctrip.xpipe.redis.core.entity.KeeperContainerMeta;
import com.ctrip.xpipe.redis.core.meta.InnerMetaClone;

/**
* @author lishanglin
* date 2023/12/14
*/
public class KeeperContainerMetaClone implements InnerMetaClone<KeeperContainerMeta> {

@Override
public KeeperContainerMeta clone(KeeperContainerMeta o) {
KeeperContainerMeta clone = new KeeperContainerMeta();
clone.setId(o.getId());
clone.mergeAttributes(o);
return clone;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ctrip.xpipe.redis.core.meta.clone;

import com.ctrip.xpipe.redis.core.entity.KeeperMeta;
import com.ctrip.xpipe.redis.core.meta.InnerMetaClone;

/**
* @author lishanglin
* date 2023/12/14
*/
public class KeeperMetaClone implements InnerMetaClone<KeeperMeta> {

@Override
public KeeperMeta clone(KeeperMeta o) {
KeeperMeta clone = new KeeperMeta();
clone.setId(o.getId());
clone.mergeAttributes(o);
return clone;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ctrip.xpipe.redis.core.meta.clone;

import com.ctrip.xpipe.exception.XpipeRuntimeException;

/**
* @author lishanglin
* date 2023/12/13
*/
public class MetaCloneException extends XpipeRuntimeException {

public MetaCloneException(String message) {
super(message);
}

public MetaCloneException(String message, Throwable th) {
super(message, th);
}

}
Loading

0 comments on commit 4b1116f

Please sign in to comment.