Skip to content

Commit

Permalink
Remove useless QualifiedDataSourceStatus.role (#31186)
Browse files Browse the repository at this point in the history
* Remove useless QualifiedDataSourceStatus.role

* Remove useless QualifiedDataSourceStatus.role
  • Loading branch information
terrymanu authored May 9, 2024
1 parent ed78411 commit 12e9e74
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,5 @@
@Getter
public final class QualifiedDataSourceStatus {

private final Role role;

private final DataSourceState status;

/**
* Storage node role.
*/
public enum Role {

PRIMARY, MEMBER
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus;
import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus.Role;
import org.apache.shardingsphere.mode.storage.node.QualifiedDataSourceNode;
import org.apache.shardingsphere.mode.storage.yaml.YamlQualifiedDataSourceStatus;
import org.apache.shardingsphere.mode.storage.yaml.YamlQualifiedDataSourceStatusSwapper;
Expand Down Expand Up @@ -67,7 +66,7 @@ public Map<String, QualifiedDataSourceStatus> loadQualifiedDataSourceStatus() {
* @param dataSourceState data source state
*/
public void changeMemberQualifiedDataSourceStatus(final String databaseName, final String groupName, final String storageUnitName, final DataSourceState dataSourceState) {
QualifiedDataSourceStatus status = new QualifiedDataSourceStatus(Role.MEMBER, dataSourceState);
QualifiedDataSourceStatus status = new QualifiedDataSourceStatus(dataSourceState);
repository.persist(QualifiedDataSourceNode.getQualifiedDataSourceNodePath(new QualifiedDataSource(databaseName, groupName, storageUnitName)),
YamlEngine.marshal(new YamlQualifiedDataSourceStatusSwapper().swapToYamlConfiguration(status)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,5 @@
@Setter
public final class YamlQualifiedDataSourceStatus implements YamlConfiguration {

private String role;

private String status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.shardingsphere.infra.state.datasource.DataSourceState;
import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus;
import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus.Role;

/**
* YAML qualified data source status swapper.
Expand All @@ -30,13 +29,12 @@ public final class YamlQualifiedDataSourceStatusSwapper implements YamlConfigura
@Override
public YamlQualifiedDataSourceStatus swapToYamlConfiguration(final QualifiedDataSourceStatus data) {
YamlQualifiedDataSourceStatus result = new YamlQualifiedDataSourceStatus();
result.setRole(data.getRole().name());
result.setStatus(data.getStatus().name());
return result;
}

@Override
public QualifiedDataSourceStatus swapToObject(final YamlQualifiedDataSourceStatus yamlConfig) {
return new QualifiedDataSourceStatus(Role.valueOf(yamlConfig.getRole()), DataSourceState.valueOf(yamlConfig.getStatus()));
return new QualifiedDataSourceStatus(DataSourceState.valueOf(yamlConfig.getStatus()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ class QualifiedDataSourceNodeTest {

@Test
void assertGetRootPath() {
assertThat(QualifiedDataSourceNode.getRootPath(), is("/nodes/storage_nodes"));
assertThat(QualifiedDataSourceNode.getRootPath(), is("/nodes/qualified_data_sources"));
}

@Test
void assertGetQualifiedDataSourceNodePath() {
assertThat(QualifiedDataSourceNode.getQualifiedDataSourceNodePath(new QualifiedDataSource("replica_query_db.readwrite_ds.replica_ds_0")),
is("/nodes/storage_nodes/replica_query_db.readwrite_ds.replica_ds_0"));
is("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0"));
}

@Test
void assertExtractQualifiedDataSource() {
Optional<QualifiedDataSource> actual = QualifiedDataSourceNode.extractQualifiedDataSource("/nodes/storage_nodes/replica_query_db.readwrite_ds.replica_ds_0");
Optional<QualifiedDataSource> actual = QualifiedDataSourceNode.extractQualifiedDataSource("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0");
assertTrue(actual.isPresent());
assertThat(actual.get().getDatabaseName(), is("replica_query_db"));
assertThat(actual.get().getGroupName(), is("readwrite_ds"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.event.StorageNodeChangedEvent;
import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus.Role;
import org.junit.jupiter.api.Test;

import java.util.Optional;
Expand All @@ -37,33 +36,31 @@ class QualifiedDataSourceChangedWatcherTest {
@Test
void assertCreateEnabledQualifiedDataSourceChangedEvent() {
Optional<GovernanceEvent> actual = new QualifiedDataSourceChangedWatcher().createGovernanceEvent(
new DataChangedEvent("/nodes/storage_nodes/replica_query_db.readwrite_ds.replica_ds_0", "role: MEMBER\nstatus: ENABLED\n", Type.ADDED));
new DataChangedEvent("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0", "status: ENABLED\n", Type.ADDED));
assertTrue(actual.isPresent());
StorageNodeChangedEvent actualEvent = (StorageNodeChangedEvent) actual.get();
assertThat(actualEvent.getQualifiedDataSource().getDatabaseName(), is("replica_query_db"));
assertThat(actualEvent.getQualifiedDataSource().getGroupName(), is("readwrite_ds"));
assertThat(actualEvent.getQualifiedDataSource().getDataSourceName(), is("replica_ds_0"));
assertThat(actualEvent.getStatus().getRole(), is(Role.MEMBER));
assertThat(actualEvent.getStatus().getStatus(), is(DataSourceState.ENABLED));
}

@Test
void assertCreateDisabledQualifiedDataSourceChangedEvent() {
Optional<GovernanceEvent> actual = new QualifiedDataSourceChangedWatcher().createGovernanceEvent(
new DataChangedEvent("/nodes/storage_nodes/replica_query_db.readwrite_ds.replica_ds_0", "role: MEMBER\nstatus: DISABLED\n", Type.DELETED));
new DataChangedEvent("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0", "status: DISABLED\n", Type.DELETED));
assertTrue(actual.isPresent());
StorageNodeChangedEvent actualEvent = (StorageNodeChangedEvent) actual.get();
assertThat(actualEvent.getQualifiedDataSource().getDatabaseName(), is("replica_query_db"));
assertThat(actualEvent.getQualifiedDataSource().getGroupName(), is("readwrite_ds"));
assertThat(actualEvent.getQualifiedDataSource().getDataSourceName(), is("replica_ds_0"));
assertThat(actualEvent.getStatus().getRole(), is(Role.MEMBER));
assertThat(actualEvent.getStatus().getStatus(), is(DataSourceState.DISABLED));
}

@Test
void assertCreateEmptyEvent() {
Optional<GovernanceEvent> actual = new QualifiedDataSourceChangedWatcher().createGovernanceEvent(
new DataChangedEvent("/nodes/storage_nodes/replica_query_db.readwrite_ds.replica_ds_0", "", Type.ADDED));
new DataChangedEvent("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0", "", Type.ADDED));
assertFalse(actual.isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus;
import org.apache.shardingsphere.mode.storage.QualifiedDataSourceStatus.Role;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -114,7 +113,7 @@ private Map<String, ShardingSphereDatabase> createDatabases() {
void assertRenewForDisableStateChanged() {
StaticDataSourceRuleAttribute ruleAttribute = mock(StaticDataSourceRuleAttribute.class);
when(database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)).thenReturn(Collections.singleton(ruleAttribute));
StorageNodeChangedEvent event = new StorageNodeChangedEvent(new QualifiedDataSource("db.readwrite_ds.ds_0"), new QualifiedDataSourceStatus(Role.MEMBER, DataSourceState.DISABLED));
StorageNodeChangedEvent event = new StorageNodeChangedEvent(new QualifiedDataSource("db.readwrite_ds.ds_0"), new QualifiedDataSourceStatus(DataSourceState.DISABLED));
subscriber.renew(event);
verify(ruleAttribute).updateStatus(
argThat(qualifiedDataSource -> Objects.equals(event.getQualifiedDataSource(), qualifiedDataSource)),
Expand Down

0 comments on commit 12e9e74

Please sign in to comment.