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

Refactor InstanceMetaDataFactory #34303

Merged
merged 2 commits into from
Jan 10, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
@Setter
public final class YamlComputeNodeData implements YamlConfiguration {

private String databaseName;

private String attribute;

private String version;

private String databaseName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"));
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -136,8 +137,8 @@ private Collection<ComputeNodeInstance> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading