From 192492ec2c54beea3e5665de31985a9b4fc4ddf1 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Fri, 10 Jan 2025 23:57:16 +0800 Subject: [PATCH] Refactor InstanceMetaDataFactory (#34303) * Refactor ComputeNodeData * Refactor InstanceMetaDataFactory --- .../infra/instance/ComputeNodeData.java | 4 ++-- .../instance/metadata/InstanceMetaDataFactory.java | 11 ++++++----- .../infra/instance/yaml/YamlComputeNodeData.java | 4 ++-- .../instance/yaml/YamlComputeNodeDataSwapper.java | 4 ++-- .../metadata/InstanceMetaDataFactoryTest.java | 5 +++-- .../service/unified/ComputeNodePersistService.java | 11 ++++++----- .../handler/global/ComputeNodeOnlineHandler.java | 3 +-- 7 files changed, 22 insertions(+), 20 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeData.java index 0f3d8c779a9f2..eb0f4d46a5342 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/ComputeNodeData.java @@ -27,9 +27,9 @@ @Getter public final class ComputeNodeData { + private final String databaseName; + private final String attribute; private final String version; - - private final String databaseName; } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataFactory.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataFactory.java index 4fe6a173fba27..4eee8cc2d6002 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataFactory.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataFactory.java @@ -19,6 +19,7 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.instance.ComputeNodeData; import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData; import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData; @@ -33,12 +34,12 @@ public final class InstanceMetaDataFactory { * * @param instanceId instance ID * @param instanceType instance type - * @param attributes attributes - * @param version version - * @param databaseName database name + * @param computeNodeData compute node data * @return created instance meta data */ - public static InstanceMetaData create(final String instanceId, final InstanceType instanceType, final String attributes, final String version, final String databaseName) { - return InstanceType.JDBC == instanceType ? new JDBCInstanceMetaData(instanceId, attributes, version, databaseName) : new ProxyInstanceMetaData(instanceId, attributes, version); + public static InstanceMetaData create(final String instanceId, final InstanceType instanceType, final ComputeNodeData computeNodeData) { + return InstanceType.JDBC == instanceType + ? new JDBCInstanceMetaData(instanceId, computeNodeData.getAttribute(), computeNodeData.getVersion(), computeNodeData.getDatabaseName()) + : new ProxyInstanceMetaData(instanceId, computeNodeData.getAttribute(), computeNodeData.getVersion()); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/yaml/YamlComputeNodeData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/yaml/YamlComputeNodeData.java index efc857e49181a..5c09a9e6de2f5 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/yaml/YamlComputeNodeData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/yaml/YamlComputeNodeData.java @@ -28,9 +28,9 @@ @Setter public final class YamlComputeNodeData implements YamlConfiguration { + private String databaseName; + private String attribute; private String version; - - private String databaseName; } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/yaml/YamlComputeNodeDataSwapper.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/yaml/YamlComputeNodeDataSwapper.java index e451277a1c5fa..c0d2349c4b8a3 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/yaml/YamlComputeNodeDataSwapper.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/yaml/YamlComputeNodeDataSwapper.java @@ -28,14 +28,14 @@ public final class YamlComputeNodeDataSwapper implements YamlConfigurationSwappe @Override public YamlComputeNodeData swapToYamlConfiguration(final ComputeNodeData data) { YamlComputeNodeData result = new YamlComputeNodeData(); + result.setDatabaseName(data.getDatabaseName()); result.setAttribute(data.getAttribute()); result.setVersion(data.getVersion()); - result.setDatabaseName(data.getDatabaseName()); return result; } @Override public ComputeNodeData swapToObject(final YamlComputeNodeData yamlConfig) { - return new ComputeNodeData(yamlConfig.getAttribute(), yamlConfig.getVersion(), yamlConfig.getDatabaseName()); + return new ComputeNodeData(yamlConfig.getDatabaseName(), yamlConfig.getAttribute(), yamlConfig.getVersion()); } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataFactoryTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataFactoryTest.java index de5e15a5b6d4d..bfb2fc7fa7fed 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataFactoryTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/InstanceMetaDataFactoryTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.infra.instance.metadata; +import org.apache.shardingsphere.infra.instance.ComputeNodeData; import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData; import org.junit.jupiter.api.Test; @@ -28,7 +29,7 @@ class InstanceMetaDataFactoryTest { @Test void assertCreateJDBCInstanceMetaDataWithInstanceId() { - InstanceMetaData actual = InstanceMetaDataFactory.create("foo_id", InstanceType.JDBC, "127.0.0.1", "foo_version", "foo_db"); + InstanceMetaData actual = InstanceMetaDataFactory.create("foo_id", InstanceType.JDBC, new ComputeNodeData("foo_db", "127.0.0.1", "foo_version")); assertThat(actual.getId(), is("foo_id")); assertNotNull(actual.getIp()); assertThat(actual.getAttributes(), is("127.0.0.1")); @@ -38,7 +39,7 @@ void assertCreateJDBCInstanceMetaDataWithInstanceId() { @Test void assertCreateProxyInstanceMetaDataWithInstanceId() { - ProxyInstanceMetaData actual = (ProxyInstanceMetaData) InstanceMetaDataFactory.create("foo_id", InstanceType.PROXY, "127.0.0.1@3307", "foo_version", "foo_db"); + ProxyInstanceMetaData actual = (ProxyInstanceMetaData) InstanceMetaDataFactory.create("foo_id", InstanceType.PROXY, new ComputeNodeData("foo_db", "127.0.0.1@3307", "foo_version")); assertThat(actual.getId(), is("foo_id")); assertThat(actual.getIp(), is("127.0.0.1")); assertThat(actual.getPort(), is(3307)); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ComputeNodePersistService.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ComputeNodePersistService.java index 5c2b20548e37b..bdc19463920b9 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ComputeNodePersistService.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ComputeNodePersistService.java @@ -55,9 +55,10 @@ public final class ComputeNodePersistService { */ public void registerOnline(final ComputeNodeInstance computeNodeInstance) { String instanceId = computeNodeInstance.getMetaData().getId(); - repository.persistEphemeral(ComputeNodePath.getOnlinePath(instanceId, computeNodeInstance.getMetaData().getType()), YamlEngine.marshal( - new YamlComputeNodeDataSwapper().swapToYamlConfiguration(new ComputeNodeData(computeNodeInstance.getMetaData().getAttributes(), - computeNodeInstance.getMetaData().getVersion(), computeNodeInstance.getMetaData().getDatabaseName())))); + ComputeNodeData computeNodeData = new ComputeNodeData( + computeNodeInstance.getMetaData().getDatabaseName(), computeNodeInstance.getMetaData().getAttributes(), computeNodeInstance.getMetaData().getVersion()); + repository.persistEphemeral(ComputeNodePath.getOnlinePath(instanceId, computeNodeInstance.getMetaData().getType()), + YamlEngine.marshal(new YamlComputeNodeDataSwapper().swapToYamlConfiguration(computeNodeData))); repository.persistEphemeral(ComputeNodePath.getStatePath(instanceId), computeNodeInstance.getState().getCurrentState().name()); persistInstanceLabels(instanceId, computeNodeInstance.getLabels()); } @@ -136,8 +137,8 @@ private Collection loadComputeNodeInstances(final InstanceT if (Strings.isNullOrEmpty(value)) { continue; } - ComputeNodeData computeNodeData = new YamlComputeNodeDataSwapper().swapToObject(YamlEngine.unmarshal(value, YamlComputeNodeData.class)); - result.add(loadComputeNodeInstance(InstanceMetaDataFactory.create(each, instanceType, computeNodeData.getAttribute(), computeNodeData.getVersion(), computeNodeData.getDatabaseName()))); + result.add(loadComputeNodeInstance( + InstanceMetaDataFactory.create(each, instanceType, new YamlComputeNodeDataSwapper().swapToObject(YamlEngine.unmarshal(value, YamlComputeNodeData.class))))); } return result; } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ComputeNodeOnlineHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ComputeNodeOnlineHandler.java index 569ef53dcd213..281d145c1d986 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ComputeNodeOnlineHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ComputeNodeOnlineHandler.java @@ -58,8 +58,7 @@ public void handle(final ContextManager contextManager, final DataChangedEvent e return; } ComputeNodeData computeNodeData = new YamlComputeNodeDataSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlComputeNodeData.class)); - InstanceMetaData instanceMetaData = InstanceMetaDataFactory.create( - matcher.group(2), InstanceType.valueOf(matcher.group(1).toUpperCase()), computeNodeData.getAttribute(), computeNodeData.getVersion(), computeNodeData.getDatabaseName()); + InstanceMetaData instanceMetaData = InstanceMetaDataFactory.create(matcher.group(2), InstanceType.valueOf(matcher.group(1).toUpperCase()), computeNodeData); if (Type.ADDED == event.getType()) { contextManager.getComputeNodeInstanceContext().getClusterInstanceRegistry() .add(contextManager.getPersistServiceFacade().getComputeNodePersistService().loadComputeNodeInstance(instanceMetaData));