Skip to content

Commit

Permalink
Merge pull request #33437 from zhaojinchao95/dev7
Browse files Browse the repository at this point in the history
Add database name for JDBC when execute SHOW COMPUTE NODES
  • Loading branch information
iamhucong authored Oct 28, 2024
2 parents ba7ba6f + 429f12c commit cd84d9f
Show file tree
Hide file tree
Showing 29 changed files with 116 additions and 79 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
1. Add arguments not null check when creating RouteUnit - [#33382](https://github.com/apache/shardingsphere/pull/33382)
1. Add index columns not empty judgement for IndexColumnTokenGenerator - [#33384](https://github.com/apache/shardingsphere/pull/33384)
1. SQL Parser: Support parsing Doris STRRIGHT - [#33393](https://github.com/apache/shardingsphere/pull/33393)
1. JDBC: Add show database name for JDBC when execute SHOW COMPUTE NODES - [#33437](https://github.com/apache/shardingsphere/pull/33437)

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,28 @@ ShowComputeNodes ::=

### 返回值说明

|| 说明 |
|---------------|-----------|
| instance_id | 实例 id |
| instance_type | 实例类型 |
| host | 主机地址 |
| port | 端口号 |
| status | 实例状态 |
| mode_type | 模式类型 |
| worker_id | worker id |
| labels | 标签 |
| version | 版本 |
|| 说明 |
|---------------|---------------------|
| instance_id | 实例 id |
| instance_type | 实例类型 |
| host | 主机地址 |
| port | 端口号 |
| status | 实例状态 |
| mode_type | 模式类型 |
| worker_id | worker id |
| labels | 标签 |
| version | 版本 |
| database_name | 当前连接逻辑库(仅对 JDBC 生效) |

### 示例

```sql
mysql> SHOW COMPUTE NODES;
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| instance_id | instance_type | host | port | status | mode_type | worker_id | labels | version |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| 3e84d33e-cb97-42f2-b6ce-f78fea0ded89 | PROXY | 127.0.0.1 | 3307 | OK | Cluster | -1 | | 5.4.2 |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+--------------+
| instance_id | instance_type | host | port | status | mode_type | worker_id | labels | version | database_name |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+--------------+
| 3e84d33e-cb97-42f2-b6ce-f78fea0ded89 | PROXY | 127.0.0.1 | 3307 | OK | Cluster | -1 | | 5.4.2 | logic_db |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+--------------+
1 row in set (0.01 sec)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,28 @@ ShowComputeNodes ::=

### Return Value Description

| Columns | Description |
|---------------|---------------|
| instance_id | instance id |
| instance_type | instance type |
| host | host |
| port | port |
| status | status |
| mode_type | mode type |
| worker_id | worker id |
| labels | labels |
| version | version |
| Columns | Description |
|---------------|------------------|
| instance_id | instance id |
| instance_type | instance type |
| host | host |
| port | port |
| status | status |
| mode_type | mode type |
| worker_id | worker id |
| labels | labels |
| version | version |
| database_name | database name (Only valid for JDBC) |

### Example

```sql
mysql> SHOW COMPUTE NODES;
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| instance_id | instance_type | host | port | status | mode_type | worker_id | labels | version |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| 3e84d33e-cb97-42f2-b6ce-f78fea0ded89 | PROXY | 127.0.0.1 | 3307 | OK | Cluster | -1 | | 5.4.2 |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+--------------+
| instance_id | instance_type | host | port | status | mode_type | worker_id | labels | version | database_name |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+--------------+
| 3e84d33e-cb97-42f2-b6ce-f78fea0ded89 | PROXY | 127.0.0.1 | 3307 | OK | Cluster | -1 | | 5.4.2 | logic_db |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+--------------+
1 row in set (0.01 sec)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ShowComputeNodeInfo ::=
| mode_type | proxy 实例模式 |
| worker_id | worker id |
| labels | 标签 |
| version | 版本 |

### 示例

Expand All @@ -43,11 +44,12 @@ SHOW COMPUTE NODE INFO;

```sql
mysql> SHOW COMPUTE NODE INFO;
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+
| instance_id | host | port | status | mode_type | worker_id | labels |
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+
| 734bb036-b15d-4af0-be87-2372d8b6a0cd | 192.168.5.163 | 3307 | OK | Cluster | -1 | |
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+
mysql> SHOW COMPUTE NODES;
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| instance_id | instance_type | host | port | status | mode_type | worker_id | labels | version |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| 3e84d33e-cb97-42f2-b6ce-f78fea0ded89 | PROXY | 127.0.0.1 | 3307 | OK | Cluster | -1 | | 5.4.2 |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
1 row in set (0.01 sec)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ShowComputeNodeInfo ::=
| mode_type | proxy instance mode |
| worker_id | worker id |
| labels | labels |
| version | version |

### Example

Expand All @@ -41,12 +42,12 @@ SHOW COMPUTE NODE INFO;
```

```sql
mysql> SHOW COMPUTE NODE INFO;
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+
| instance_id | host | port | status | mode_type | worker_id | labels |
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+
| 734bb036-b15d-4af0-be87-2372d8b6a0cd | 192.168.5.163 | 3307 | OK | Cluster | -1 | |
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+
mysql> SHOW COMPUTE NODES;
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| instance_id | instance_type | host | port | status | mode_type | worker_id | labels | version |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| 3e84d33e-cb97-42f2-b6ce-f78fea0ded89 | PROXY | 127.0.0.1 | 3307 | OK | Cluster | -1 | | 5.4.2 |
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
1 row in set (0.01 sec)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public final class ComputeNodeData {
private final String attribute;

private final String version;

private final String databaseName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ public interface InstanceMetaData {
* @return version
*/
String getVersion();

/**
* Get database name.
*
* @return database name
*/
default String getDatabaseName() {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public interface InstanceMetaDataBuilder extends TypedSPI {
* Build instance meta data.
*
* @param port port
* @param databaseName database name
* @return built instance meta data
*/
InstanceMetaData build(int port);
InstanceMetaData build(int port, String databaseName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public final class InstanceMetaDataFactory {
* @param instanceType instance type
* @param attributes attributes
* @param version version
* @param databaseName database name
* @return created instance meta data
*/
public static InstanceMetaData create(final String instanceId, final InstanceType instanceType, final String attributes, final String version) {
return InstanceType.JDBC == instanceType ? new JDBCInstanceMetaData(instanceId, attributes, version) : new ProxyInstanceMetaData(instanceId, attributes, version);
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ public final class JDBCInstanceMetaData implements InstanceMetaData {

private final String version;

public JDBCInstanceMetaData(final String id) {
private final String databaseName;

public JDBCInstanceMetaData(final String id, final String databaseName) {
this.id = id;
ip = IpUtils.getIp();
version = ShardingSphereVersion.VERSION;
this.databaseName = databaseName;
}

public JDBCInstanceMetaData(final String id, final String attributes, final String version) {
public JDBCInstanceMetaData(final String id, final String attributes, final String version, final String databaseName) {
this.id = id;
ip = attributes;
this.version = version;
this.databaseName = databaseName;
}

@Override
Expand All @@ -56,4 +60,10 @@ public InstanceType getType() {
public String getAttributes() {
return ip;
}

@Override
public String getDatabaseName() {
return databaseName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
public final class JDBCInstanceMetaDataBuilder implements InstanceMetaDataBuilder {

@Override
public InstanceMetaData build(final int port) {
return new JDBCInstanceMetaData(UUID.randomUUID().toString());
public InstanceMetaData build(final int port, final String databaseName) {
return new JDBCInstanceMetaData(UUID.randomUUID().toString(), databaseName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public final class ProxyInstanceMetaDataBuilder implements InstanceMetaDataBuilder {

@Override
public InstanceMetaData build(final int port) {
public InstanceMetaData build(final int port, final String databaseName) {
return new ProxyInstanceMetaData(UUID.randomUUID().toString(), port);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public final class YamlComputeNodeData implements YamlConfiguration {
private String attribute;

private String version;

private String databaseName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ public YamlComputeNodeData swapToYamlConfiguration(final ComputeNodeData data) {
YamlComputeNodeData result = new YamlComputeNodeData();
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());
return new ComputeNodeData(yamlConfig.getAttribute(), yamlConfig.getVersion(), yamlConfig.getDatabaseName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class InstanceMetaDataFactoryTest {

@Test
void assertCreateJDBCInstanceMetaDataWithInstanceId() {
InstanceMetaData actual = InstanceMetaDataFactory.create("foo_id", InstanceType.JDBC, "127.0.0.1", "foo_version");
InstanceMetaData actual = InstanceMetaDataFactory.create("foo_id", InstanceType.JDBC, "127.0.0.1", "foo_version", "foo_db");
assertThat(actual.getId(), is("foo_id"));
assertNotNull(actual.getIp());
assertThat(actual.getAttributes(), is("127.0.0.1"));
Expand All @@ -38,7 +38,7 @@ void assertCreateJDBCInstanceMetaDataWithInstanceId() {

@Test
void assertCreateProxyInstanceMetaDataWithInstanceId() {
ProxyInstanceMetaData actual = (ProxyInstanceMetaData) InstanceMetaDataFactory.create("foo_id", InstanceType.PROXY, "127.0.0.1@3307", "foo_version");
ProxyInstanceMetaData actual = (ProxyInstanceMetaData) InstanceMetaDataFactory.create("foo_id", InstanceType.PROXY, "127.0.0.1@3307", "foo_version", "foo_db");
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 @@ -31,6 +31,6 @@ class JDBCInstanceMetaDataBuilderTest {

@Test
void assertBuild() {
assertThat(instanceMetaDataBuilder.build(3306).getType(), is(InstanceType.JDBC));
assertThat(instanceMetaDataBuilder.build(3306, "foo_db").getType(), is(InstanceType.JDBC));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class ProxyInstanceMetaDataBuilderTest {

@Test
void assertBuild() {
assertThat(instanceMetaDataBuilder.build(3306).getType(), is(InstanceType.PROXY));
assertThat(instanceMetaDataBuilder.build(3306, "").getType(), is(InstanceType.PROXY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ShardingSphereDataSource(final String databaseName, final ModeConfigurati

private ContextManager createContextManager(final ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap,
final Collection<RuleConfiguration> ruleConfigs, final Properties props) throws SQLException {
InstanceMetaData instanceMetaData = TypedSPILoader.getService(InstanceMetaDataBuilder.class, "JDBC").build(-1);
InstanceMetaData instanceMetaData = TypedSPILoader.getService(InstanceMetaDataBuilder.class, "JDBC").build(-1, databaseName);
Collection<RuleConfiguration> globalRuleConfigs = ruleConfigs.stream().filter(GlobalRuleConfiguration.class::isInstance).collect(Collectors.toList());
Collection<RuleConfiguration> databaseRuleConfigs = new LinkedList<>(ruleConfigs);
databaseRuleConfigs.removeAll(globalRuleConfigs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public final class ComputeNodePersistService {
public void registerOnline(final ComputeNodeInstance computeNodeInstance) {
String instanceId = computeNodeInstance.getMetaData().getId();
repository.persistEphemeral(ComputeNode.getOnlineInstanceNodePath(instanceId, computeNodeInstance.getMetaData().getType()), YamlEngine.marshal(
new YamlComputeNodeDataSwapper().swapToYamlConfiguration(new ComputeNodeData(computeNodeInstance.getMetaData().getAttributes(), computeNodeInstance.getMetaData().getVersion()))));
new YamlComputeNodeDataSwapper().swapToYamlConfiguration(new ComputeNodeData(computeNodeInstance.getMetaData().getAttributes(),
computeNodeInstance.getMetaData().getVersion(), computeNodeInstance.getMetaData().getDatabaseName()))));
repository.persistEphemeral(ComputeNode.getComputeNodeStateNodePath(instanceId), computeNodeInstance.getState().getCurrentState().name());
persistInstanceLabels(instanceId, computeNodeInstance.getLabels());
}
Expand Down Expand Up @@ -136,7 +137,7 @@ private Collection<ComputeNodeInstance> loadComputeNodeInstances(final InstanceT
continue;
}
ComputeNodeData computeNodeData = new YamlComputeNodeDataSwapper().swapToObject(YamlEngine.unmarshal(value, YamlComputeNodeData.class));
result.add(loadComputeNodeInstance(InstanceMetaDataFactory.create(each, instanceType, computeNodeData.getAttribute(), computeNodeData.getVersion())));
result.add(loadComputeNodeInstance(InstanceMetaDataFactory.create(each, instanceType, computeNodeData.getAttribute(), computeNodeData.getVersion(), computeNodeData.getDatabaseName())));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void assertReloadTableWithDataSourceName() {

@Test
void assertGetPreSelectedDatabaseNameWithJDBC() {
when(computeNodeInstanceContext.getInstance()).thenReturn(new ComputeNodeInstance(new JDBCInstanceMetaData("foo_id"), Collections.emptyList()));
when(computeNodeInstanceContext.getInstance()).thenReturn(new ComputeNodeInstance(new JDBCInstanceMetaData("foo_id", "foo_db"), Collections.emptyList()));
when(metaDataContexts.getMetaData().getDatabases()).thenReturn(Collections.singletonMap("foo_db", mock(ShardingSphereDatabase.class)));
assertThat(contextManager.getPreSelectedDatabaseName(), is("foo_db"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private Optional<DispatchEvent> createInstanceEvent(final DataChangedEvent event
}
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());
matcher.group(2), InstanceType.valueOf(matcher.group(1).toUpperCase()), computeNodeData.getAttribute(), computeNodeData.getVersion(), computeNodeData.getDatabaseName());
if (Type.ADDED == event.getType()) {
return Optional.of(new InstanceOnlineEvent(instanceMetaData));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void assertBuildWithNullRepositoryConfiguration() {

@Test
void assertBuildForJDBC() throws SQLException {
assertBuild(new JDBCInstanceMetaData("foo"));
assertBuild(new JDBCInstanceMetaData("foo", "foo_db"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void assertBuildWithDefault() throws SQLException {

@SuppressWarnings("resource")
void assertBuild(final ContextManagerBuilder builder) throws SQLException {
InstanceMetaData instanceMetaData = new JDBCInstanceMetaData("foo");
InstanceMetaData instanceMetaData = new JDBCInstanceMetaData("foo", "foo_db");
ContextManager actual = builder.build(new ContextManagerBuilderParameter(createModeConfiguration(),
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyList(), new Properties(), Collections.emptyList(), instanceMetaData, false), mock(EventBusContext.class));
assertThat(actual.getComputeNodeInstanceContext().getInstance().getMetaData(), is(instanceMetaData));
Expand Down
Loading

0 comments on commit cd84d9f

Please sign in to comment.