Skip to content

Commit

Permalink
Support JDBC in show compute nodes. (#28732)
Browse files Browse the repository at this point in the history
* Fixes #28730, support JDBC in for show compute nodes.

* Update E2E xml for show compute nodes
  • Loading branch information
RaigorJiang authored Oct 12, 2023
1 parent 0a53ca3 commit 19eb941
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ weight = 3

### 描述

`SHOW COMPUTE NODES` 语法用于查询 proxy 实例信息
`SHOW COMPUTE NODES` 语法用于查询计算节点信息

### 语法

Expand All @@ -23,32 +23,27 @@ ShowComputeNodes ::=

### 返回值说明

|| 说明 |
|-------------|------------|
| instance_id | proxy 实例编号 |
| host | 主机地址 |
| port | 端口号 |
| status | proxy 实例状态 |
| mode_type | proxy 实例模式 |
| worker_id | worker id |
| labels | 标签 |
| version | 版本 |
|| 说明 |
|---------------|-----------|
| instance_id | 实例 id |
| instance_type | 实例类型 |
| host | 主机地址 |
| port | 端口号 |
| status | 实例状态 |
| mode_type | 模式类型 |
| worker_id | worker id |
| labels | 标签 |
| version | 版本 |

### 示例

- 查询 proxy 实例信息

```sql
SHOW COMPUTE NODES;
```

```sql
mysql> SHOW COMPUTE NODES;
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+---------+
| instance_id | host | port | status | mode_type | worker_id | labels | version |
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+---------+
| 734bb036-b15d-4af0-be87-2372d8b6a0cd | 192.168.5.163 | 3307 | OK | Cluster | -1 | | 5.3.0 |
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+---------+
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| 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 @@ -5,7 +5,7 @@ weight = 3

### Description

The `SHOW COMPUTE NODES` syntax is used to query proxy instance information.
The `SHOW COMPUTE NODES` syntax is used to query compute nodes information.
### Syntax

{{< tabs >}}
Expand All @@ -22,32 +22,27 @@ ShowComputeNodes ::=

### Return Value Description

| Columns | Description |
|----------------|-----------------------|
| instance_id | proxy instance id |
| host | host address |
| port | port number |
| status | proxy instance status |
| mode_type | proxy instance mode |
| 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 |

### Example

- Query proxy instance information

```sql
SHOW COMPUTE NODES;
```

```sql
mysql> SHOW COMPUTE NODES;
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+---------+
| instance_id | host | port | status | mode_type | worker_id | labels | version |
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+---------+
| 734bb036-b15d-4af0-be87-2372d8b6a0cd | 192.168.5.163 | 3307 | OK | Cluster | -1 | | 5.3.0 |
+--------------------------------------+---------------+------+--------+-----------+-----------+--------+---------+
+--------------------------------------+---------------+------------+------+--------+------------+-----------+--------+----------+
| 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 @@ -22,14 +22,12 @@
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand All @@ -39,7 +37,7 @@ public final class ShowComputeNodesExecutor implements InstanceContextRequiredQu

@Override
public Collection<String> getColumnNames() {
return Arrays.asList("instance_id", "host", "port", "status", "mode_type", "worker_id", "labels", "version");
return Arrays.asList("instance_id", "instance_type", "host", "port", "status", "mode_type", "worker_id", "labels", "version");
}

@Override
Expand All @@ -48,15 +46,14 @@ public Collection<LocalDataQueryResultRow> getRows(final InstanceContext instanc
if ("Standalone".equals(modeType)) {
return Collections.singleton(buildRow(instanceContext.getInstance(), modeType));
}
Collection<ComputeNodeInstance> instances = instanceContext.getAllClusterInstances().stream()
.filter(each -> InstanceType.PROXY == each.getMetaData().getType()).collect(Collectors.toList());
return instances.isEmpty() ? Collections.emptyList() : instances.stream().filter(Objects::nonNull).map(each -> buildRow(each, modeType)).collect(Collectors.toList());
Collection<ComputeNodeInstance> instances = instanceContext.getAllClusterInstances();
return instances.isEmpty() ? Collections.emptyList() : instances.stream().map(each -> buildRow(each, modeType)).collect(Collectors.toList());
}

private LocalDataQueryResultRow buildRow(final ComputeNodeInstance instance, final String modeType) {
String labels = String.join(",", instance.getLabels());
InstanceMetaData instanceMetaData = instance.getMetaData();
return new LocalDataQueryResultRow(instanceMetaData.getId(), instanceMetaData.getIp(),
return new LocalDataQueryResultRow(instanceMetaData.getId(), instanceMetaData.getType().name(), instanceMetaData.getIp(),
instanceMetaData instanceof ProxyInstanceMetaData ? ((ProxyInstanceMetaData) instanceMetaData).getPort() : -1,
instance.getState().getCurrentState().name(), modeType, instance.getWorkerId(), labels, instanceMetaData.getVersion());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.queryable;

import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowComputeNodesStatement;
import org.apache.shardingsphere.distsql.statement.ral.queryable.ShowComputeNodeInfoStatement;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
Expand Down Expand Up @@ -56,11 +56,11 @@ void assertGetColumns() {

@Test
void assertExecute() {
ShowComputeNodesExecutor executor = new ShowComputeNodesExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(createInstanceContext(), mock(ShowComputeNodesStatement.class));
ShowComputeNodeInfoExecutor executor = new ShowComputeNodeInfoExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(createInstanceContext(), mock(ShowComputeNodeInfoStatement.class));
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
assertThat(row.getCell(1), is("127.0.0.1@3309"));
assertThat(row.getCell(1), is("foo"));
assertThat(row.getCell(2), is("127.0.0.1"));
assertThat(row.getCell(3), is(3309));
assertThat(row.getCell(4), is("OK"));
Expand All @@ -72,7 +72,7 @@ void assertExecute() {

private InstanceContext createInstanceContext() {
InstanceContext result = mock(InstanceContext.class, RETURNS_DEEP_STUBS);
when(result.getInstance().getMetaData()).thenReturn(new ProxyInstanceMetaData("127.0.0.1@3309", "127.0.0.1@3309", "foo_version"));
when(result.getInstance().getMetaData()).thenReturn(new ProxyInstanceMetaData("foo", "127.0.0.1@3309", "foo_version"));
when(result.getInstance().getState()).thenReturn(new InstanceStateContext());
when(result.getModeConfiguration()).thenReturn(new ModeConfiguration("Standalone", new StandalonePersistRepositoryConfiguration("H2", new Properties())));
when(result.getInstance().getWorkerId()).thenReturn(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ class ShowComputeNodesExecutorTest {
void assertGetColumns() {
ShowComputeNodesExecutor executor = new ShowComputeNodesExecutor();
Collection<String> columns = executor.getColumnNames();
assertThat(columns.size(), is(8));
assertThat(columns.size(), is(9));
Iterator<String> iterator = columns.iterator();
assertThat(iterator.next(), is("instance_id"));
assertThat(iterator.next(), is("instance_type"));
assertThat(iterator.next(), is("host"));
assertThat(iterator.next(), is("port"));
assertThat(iterator.next(), is("status"));
Expand All @@ -63,14 +64,15 @@ void assertExecuteWithStandaloneMode() {
Collection<LocalDataQueryResultRow> actual = executor.getRows(createStandaloneInstanceContext(), mock(ShowComputeNodesStatement.class));
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
assertThat(row.getCell(1), is("127.0.0.1@3308"));
assertThat(row.getCell(2), is("127.0.0.1"));
assertThat(row.getCell(3), is(3308));
assertThat(row.getCell(4), is("OK"));
assertThat(row.getCell(5), is("Standalone"));
assertThat(row.getCell(6), is(0));
assertThat(row.getCell(7), is(""));
assertThat(row.getCell(8), is("foo_version"));
assertThat(row.getCell(1), is("foo"));
assertThat(row.getCell(2), is("PROXY"));
assertThat(row.getCell(3), is("127.0.0.1"));
assertThat(row.getCell(4), is(3308));
assertThat(row.getCell(5), is("OK"));
assertThat(row.getCell(6), is("Standalone"));
assertThat(row.getCell(7), is(0));
assertThat(row.getCell(8), is(""));
assertThat(row.getCell(9), is("foo_version"));
}

@Test
Expand All @@ -79,19 +81,20 @@ void assertExecuteWithClusterMode() {
Collection<LocalDataQueryResultRow> actual = executor.getRows(createClusterInstanceContext(), mock(ShowComputeNodesStatement.class));
assertThat(actual.size(), is(1));
LocalDataQueryResultRow row = actual.iterator().next();
assertThat(row.getCell(1), is("127.0.0.1@3309"));
assertThat(row.getCell(2), is("127.0.0.1"));
assertThat(row.getCell(3), is(3309));
assertThat(row.getCell(4), is("OK"));
assertThat(row.getCell(5), is("Cluster"));
assertThat(row.getCell(6), is(1));
assertThat(row.getCell(7), is(""));
assertThat(row.getCell(8), is("foo_version"));
assertThat(row.getCell(1), is("foo"));
assertThat(row.getCell(2), is("PROXY"));
assertThat(row.getCell(3), is("127.0.0.1"));
assertThat(row.getCell(4), is(3309));
assertThat(row.getCell(5), is("OK"));
assertThat(row.getCell(6), is("Cluster"));
assertThat(row.getCell(7), is(1));
assertThat(row.getCell(8), is(""));
assertThat(row.getCell(9), is("foo_version"));
}

private InstanceContext createStandaloneInstanceContext() {
InstanceContext result = mock(InstanceContext.class, RETURNS_DEEP_STUBS);
when(result.getInstance().getMetaData()).thenReturn(new ProxyInstanceMetaData("127.0.0.1@3308", "127.0.0.1@3308", "foo_version"));
when(result.getInstance().getMetaData()).thenReturn(new ProxyInstanceMetaData("foo", "127.0.0.1@3308", "foo_version"));
when(result.getInstance().getState()).thenReturn(new InstanceStateContext());
when(result.getModeConfiguration()).thenReturn(new ModeConfiguration("Standalone", new StandalonePersistRepositoryConfiguration("H2", new Properties())));
when(result.getInstance().getWorkerId()).thenReturn(0);
Expand All @@ -102,7 +105,7 @@ private InstanceContext createClusterInstanceContext() {
InstanceContext result = mock(InstanceContext.class, RETURNS_DEEP_STUBS);
when(result.getModeConfiguration()).thenReturn(new ModeConfiguration("Cluster", mock(PersistRepositoryConfiguration.class)));
ComputeNodeInstance computeNodeInstance = mock(ComputeNodeInstance.class, RETURNS_DEEP_STUBS);
when(computeNodeInstance.getMetaData()).thenReturn(new ProxyInstanceMetaData("127.0.0.1@3309", "127.0.0.1@3309", "foo_version"));
when(computeNodeInstance.getMetaData()).thenReturn(new ProxyInstanceMetaData("foo", "127.0.0.1@3309", "foo_version"));
when(computeNodeInstance.getState()).thenReturn(new InstanceStateContext());
when(computeNodeInstance.getWorkerId()).thenReturn(1);
when(result.getAllClusterInstances()).thenReturn(Collections.singleton(computeNodeInstance));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<dataset>
<metadata>
<column name="instance_id" assertion="false" />
<column name="instance_type" />
<column name="host" assertion="false" />
<column name="port" />
<column name="status" />
Expand All @@ -26,5 +27,5 @@
<column name="labels" />
<column name="version" assertion="false"/>
</metadata>
<row values=" | | 3307| OK| Cluster| 0| |" />
<row values=" | PROXY| | 3307| OK| Cluster| 0| |" />
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<dataset>
<metadata>
<column name="instance_id" assertion="false" />
<column name="instance_type" />
<column name="host" assertion="false" />
<column name="port" />
<column name="status" />
Expand All @@ -26,5 +27,5 @@
<column name="labels" />
<column name="version" assertion="false"/>
</metadata>
<row values=" | | 3307| OK| Standalone| 1| |" />
<row values=" | PROXY| | 3307| OK| Standalone| 1| |" />
</dataset>

0 comments on commit 19eb941

Please sign in to comment.