kfaraz commented on code in PR #18692: URL: https://github.com/apache/druid/pull/18692#discussion_r2484251389
########## sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemPropertiesTable.java: ########## @@ -0,0 +1,191 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.calcite.schema; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Preconditions; +import org.apache.calcite.DataContext; +import org.apache.calcite.linq4j.Enumerable; +import org.apache.calcite.linq4j.Linq4j; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.schema.ScannableTable; +import org.apache.calcite.schema.Schema; +import org.apache.calcite.schema.impl.AbstractTable; +import org.apache.druid.discovery.DiscoveryDruidNode; +import org.apache.druid.discovery.DruidNodeDiscoveryProvider; +import org.apache.druid.error.DruidException; +import org.apache.druid.java.util.common.RE; +import org.apache.druid.java.util.http.client.HttpClient; +import org.apache.druid.java.util.http.client.Request; +import org.apache.druid.java.util.http.client.response.StringFullResponseHandler; +import org.apache.druid.java.util.http.client.response.StringFullResponseHolder; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.server.DruidNode; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.server.security.AuthorizerMapper; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.table.RowSignatures; +import org.jboss.netty.handler.codec.http.HttpMethod; + +import javax.servlet.http.HttpServletResponse; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * System schema table {@code sys.server_properties} that contains the properties of all Druid servers. + * Each row contains the value of a single property. If a server has multiple node roles, all the rows for + * that server would have multiple values in the column {@code node_roles} rather than duplicating all the + * rows. + */ +public class SystemPropertiesTable extends AbstractTable implements ScannableTable +{ + public static final String TABLE_NAME = "server_properties"; + + static final RowSignature ROW_SIGNATURE = RowSignature + .builder() + .add("service_name", ColumnType.STRING) + .add("server", ColumnType.STRING) + .add("node_roles", ColumnType.STRING) + .add("property", ColumnType.STRING) + .add("value", ColumnType.STRING) + .build(); + + private final DruidNodeDiscoveryProvider druidNodeDiscoveryProvider; + private final AuthorizerMapper authorizerMapper; + private final HttpClient httpClient; + private final ObjectMapper jsonMapper; + + public SystemPropertiesTable( + DruidNodeDiscoveryProvider druidNodeDiscoveryProvider, + AuthorizerMapper authorizerMapper, + HttpClient httpClient, + ObjectMapper jsonMapper + ) + { + this.druidNodeDiscoveryProvider = druidNodeDiscoveryProvider; + this.authorizerMapper = authorizerMapper; + this.httpClient = httpClient; + this.jsonMapper = jsonMapper; + } + + @Override + public RelDataType getRowType(RelDataTypeFactory typeFactory) + { + return RowSignatures.toRelDataType(ROW_SIGNATURE, typeFactory); + } + + @Override + public Schema.TableType getJdbcTableType() + { + return Schema.TableType.SYSTEM_TABLE; + } + + @Override + public Enumerable<Object[]> scan(DataContext root) + { + final AuthenticationResult authenticationResult = (AuthenticationResult) Preconditions.checkNotNull( + root.get(PlannerContext.DATA_CTX_AUTHENTICATION_RESULT), + "authenticationResult in dataContext" + ); + SystemSchema.checkStateReadAccessForServers(authenticationResult, authorizerMapper); + final Iterator<DiscoveryDruidNode> druidServers = SystemSchema.getDruidServers(druidNodeDiscoveryProvider); + + final Map<String, ServerProperties> serverToPropertiesMap = new HashMap<>(); + druidServers.forEachRemaining(discoveryDruidNode -> { + final DruidNode druidNode = discoveryDruidNode.getDruidNode(); + final Map<String, String> propertiesMap = getProperties(druidNode); + if (serverToPropertiesMap.containsKey(druidNode.getHostAndPortToUse())) { + ServerProperties serverProperties = serverToPropertiesMap.get(druidNode.getHostAndPortToUse()); + serverProperties.addNodeRole(discoveryDruidNode.getNodeRole().getJsonName()); + } else { + serverToPropertiesMap.put(druidNode.getHostAndPortToUse(), new ServerProperties(druidNode.getServiceName(), druidNode.getHostAndPortToUse(), new ArrayList<>(Arrays.asList(discoveryDruidNode.getNodeRole().getJsonName())), propertiesMap)); Review Comment: formatting: ```suggestion serverToPropertiesMap.put( druidNode.getHostAndPortToUse(), new ServerProperties( druidNode.getServiceName(), druidNode.getHostAndPortToUse(), new ArrayList<>(Arrays.asList(discoveryDruidNode.getNodeRole().getJsonName())), propertiesMap ) ); ``` ########## sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemPropertiesTable.java: ########## @@ -0,0 +1,191 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.calcite.schema; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Preconditions; +import org.apache.calcite.DataContext; +import org.apache.calcite.linq4j.Enumerable; +import org.apache.calcite.linq4j.Linq4j; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.schema.ScannableTable; +import org.apache.calcite.schema.Schema; +import org.apache.calcite.schema.impl.AbstractTable; +import org.apache.druid.discovery.DiscoveryDruidNode; +import org.apache.druid.discovery.DruidNodeDiscoveryProvider; +import org.apache.druid.error.DruidException; +import org.apache.druid.java.util.common.RE; +import org.apache.druid.java.util.http.client.HttpClient; +import org.apache.druid.java.util.http.client.Request; +import org.apache.druid.java.util.http.client.response.StringFullResponseHandler; +import org.apache.druid.java.util.http.client.response.StringFullResponseHolder; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.server.DruidNode; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.server.security.AuthorizerMapper; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.table.RowSignatures; +import org.jboss.netty.handler.codec.http.HttpMethod; + +import javax.servlet.http.HttpServletResponse; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * System schema table {@code sys.server_properties} that contains the properties of all Druid servers. + * Each row contains the value of a single property. If a server has multiple node roles, all the rows for + * that server would have multiple values in the column {@code node_roles} rather than duplicating all the + * rows. + */ +public class SystemPropertiesTable extends AbstractTable implements ScannableTable +{ + public static final String TABLE_NAME = "server_properties"; + + static final RowSignature ROW_SIGNATURE = RowSignature + .builder() + .add("service_name", ColumnType.STRING) + .add("server", ColumnType.STRING) + .add("node_roles", ColumnType.STRING) + .add("property", ColumnType.STRING) + .add("value", ColumnType.STRING) + .build(); + + private final DruidNodeDiscoveryProvider druidNodeDiscoveryProvider; + private final AuthorizerMapper authorizerMapper; + private final HttpClient httpClient; + private final ObjectMapper jsonMapper; + + public SystemPropertiesTable( + DruidNodeDiscoveryProvider druidNodeDiscoveryProvider, + AuthorizerMapper authorizerMapper, + HttpClient httpClient, + ObjectMapper jsonMapper + ) + { + this.druidNodeDiscoveryProvider = druidNodeDiscoveryProvider; + this.authorizerMapper = authorizerMapper; + this.httpClient = httpClient; + this.jsonMapper = jsonMapper; + } + + @Override + public RelDataType getRowType(RelDataTypeFactory typeFactory) + { + return RowSignatures.toRelDataType(ROW_SIGNATURE, typeFactory); + } + + @Override + public Schema.TableType getJdbcTableType() + { + return Schema.TableType.SYSTEM_TABLE; + } + + @Override + public Enumerable<Object[]> scan(DataContext root) + { + final AuthenticationResult authenticationResult = (AuthenticationResult) Preconditions.checkNotNull( + root.get(PlannerContext.DATA_CTX_AUTHENTICATION_RESULT), + "authenticationResult in dataContext" + ); + SystemSchema.checkStateReadAccessForServers(authenticationResult, authorizerMapper); + final Iterator<DiscoveryDruidNode> druidServers = SystemSchema.getDruidServers(druidNodeDiscoveryProvider); + + final Map<String, ServerProperties> serverToPropertiesMap = new HashMap<>(); + druidServers.forEachRemaining(discoveryDruidNode -> { + final DruidNode druidNode = discoveryDruidNode.getDruidNode(); + final Map<String, String> propertiesMap = getProperties(druidNode); + if (serverToPropertiesMap.containsKey(druidNode.getHostAndPortToUse())) { + ServerProperties serverProperties = serverToPropertiesMap.get(druidNode.getHostAndPortToUse()); + serverProperties.addNodeRole(discoveryDruidNode.getNodeRole().getJsonName()); + } else { + serverToPropertiesMap.put(druidNode.getHostAndPortToUse(), new ServerProperties(druidNode.getServiceName(), druidNode.getHostAndPortToUse(), new ArrayList<>(Arrays.asList(discoveryDruidNode.getNodeRole().getJsonName())), propertiesMap)); + } + }); + return Linq4j.asEnumerable(serverToPropertiesMap.values().stream().flatMap(ServerProperties::toRows).collect(Collectors.toList())); + } + + private Map<String, String> getProperties(DruidNode druidNode) + { + final String url = druidNode.getUriToUse().resolve("/status/properties").toString(); + try { + final Request request = new Request(HttpMethod.GET, new URL(url)); + final StringFullResponseHolder response; + response = httpClient + .go(request, new StringFullResponseHandler(StandardCharsets.UTF_8)) + .get(); + + if (response.getStatus().getCode() != HttpServletResponse.SC_OK) { + throw new RE( + "Failed to get properties from node[%s]. Error code[%d], description[%s].", + url, + response.getStatus().getCode(), + response.getStatus().getReasonPhrase() + ); + } + return jsonMapper.readValue( + response.getContent(), + new TypeReference<Map<String, String>>(){} + ); + } + catch (Exception e) { + throw DruidException.forPersona(DruidException.Persona.USER) + .ofCategory(DruidException.Category.UNCATEGORIZED) + .build(e, "HTTP request to[%s] failed", url); + } Review Comment: Maybe use `InternalServerError.exception()` instead. ########## sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemPropertiesTable.java: ########## @@ -0,0 +1,191 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.calcite.schema; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Preconditions; +import org.apache.calcite.DataContext; +import org.apache.calcite.linq4j.Enumerable; +import org.apache.calcite.linq4j.Linq4j; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.schema.ScannableTable; +import org.apache.calcite.schema.Schema; +import org.apache.calcite.schema.impl.AbstractTable; +import org.apache.druid.discovery.DiscoveryDruidNode; +import org.apache.druid.discovery.DruidNodeDiscoveryProvider; +import org.apache.druid.error.DruidException; +import org.apache.druid.java.util.common.RE; +import org.apache.druid.java.util.http.client.HttpClient; +import org.apache.druid.java.util.http.client.Request; +import org.apache.druid.java.util.http.client.response.StringFullResponseHandler; +import org.apache.druid.java.util.http.client.response.StringFullResponseHolder; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.server.DruidNode; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.server.security.AuthorizerMapper; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.table.RowSignatures; +import org.jboss.netty.handler.codec.http.HttpMethod; + +import javax.servlet.http.HttpServletResponse; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * System schema table {@code sys.server_properties} that contains the properties of all Druid servers. + * Each row contains the value of a single property. If a server has multiple node roles, all the rows for + * that server would have multiple values in the column {@code node_roles} rather than duplicating all the + * rows. + */ +public class SystemPropertiesTable extends AbstractTable implements ScannableTable +{ + public static final String TABLE_NAME = "server_properties"; + + static final RowSignature ROW_SIGNATURE = RowSignature + .builder() + .add("service_name", ColumnType.STRING) + .add("server", ColumnType.STRING) + .add("node_roles", ColumnType.STRING) + .add("property", ColumnType.STRING) + .add("value", ColumnType.STRING) + .build(); + + private final DruidNodeDiscoveryProvider druidNodeDiscoveryProvider; + private final AuthorizerMapper authorizerMapper; + private final HttpClient httpClient; + private final ObjectMapper jsonMapper; + + public SystemPropertiesTable( + DruidNodeDiscoveryProvider druidNodeDiscoveryProvider, + AuthorizerMapper authorizerMapper, + HttpClient httpClient, + ObjectMapper jsonMapper + ) + { + this.druidNodeDiscoveryProvider = druidNodeDiscoveryProvider; + this.authorizerMapper = authorizerMapper; + this.httpClient = httpClient; + this.jsonMapper = jsonMapper; + } + + @Override + public RelDataType getRowType(RelDataTypeFactory typeFactory) + { + return RowSignatures.toRelDataType(ROW_SIGNATURE, typeFactory); + } + + @Override + public Schema.TableType getJdbcTableType() + { + return Schema.TableType.SYSTEM_TABLE; + } + + @Override + public Enumerable<Object[]> scan(DataContext root) + { + final AuthenticationResult authenticationResult = (AuthenticationResult) Preconditions.checkNotNull( + root.get(PlannerContext.DATA_CTX_AUTHENTICATION_RESULT), + "authenticationResult in dataContext" + ); + SystemSchema.checkStateReadAccessForServers(authenticationResult, authorizerMapper); + final Iterator<DiscoveryDruidNode> druidServers = SystemSchema.getDruidServers(druidNodeDiscoveryProvider); + + final Map<String, ServerProperties> serverToPropertiesMap = new HashMap<>(); + druidServers.forEachRemaining(discoveryDruidNode -> { + final DruidNode druidNode = discoveryDruidNode.getDruidNode(); + final Map<String, String> propertiesMap = getProperties(druidNode); + if (serverToPropertiesMap.containsKey(druidNode.getHostAndPortToUse())) { + ServerProperties serverProperties = serverToPropertiesMap.get(druidNode.getHostAndPortToUse()); + serverProperties.addNodeRole(discoveryDruidNode.getNodeRole().getJsonName()); + } else { + serverToPropertiesMap.put(druidNode.getHostAndPortToUse(), new ServerProperties(druidNode.getServiceName(), druidNode.getHostAndPortToUse(), new ArrayList<>(Arrays.asList(discoveryDruidNode.getNodeRole().getJsonName())), propertiesMap)); + } + }); + return Linq4j.asEnumerable(serverToPropertiesMap.values().stream().flatMap(ServerProperties::toRows).collect(Collectors.toList())); + } + + private Map<String, String> getProperties(DruidNode druidNode) + { + final String url = druidNode.getUriToUse().resolve("/status/properties").toString(); + try { + final Request request = new Request(HttpMethod.GET, new URL(url)); + final StringFullResponseHolder response; + response = httpClient + .go(request, new StringFullResponseHandler(StandardCharsets.UTF_8)) + .get(); + + if (response.getStatus().getCode() != HttpServletResponse.SC_OK) { + throw new RE( + "Failed to get properties from node[%s]. Error code[%d], description[%s].", + url, + response.getStatus().getCode(), + response.getStatus().getReasonPhrase() + ); + } + return jsonMapper.readValue( + response.getContent(), + new TypeReference<Map<String, String>>(){} + ); + } + catch (Exception e) { + throw DruidException.forPersona(DruidException.Persona.USER) + .ofCategory(DruidException.Category.UNCATEGORIZED) + .build(e, "HTTP request to[%s] failed", url); + } + } + + private static class ServerProperties + { + final String serviceName; + final String server; + final List<String> nodeRoles; + final Map<String, String> properties; + + public ServerProperties(String serviceName, String server, List<String> nodeRoles, Map<String, String> properties) + { + this.serviceName = serviceName; + this.server = server; + this.nodeRoles = nodeRoles; + this.properties = properties; + } + + public void addNodeRole(String nodeRole) + { + nodeRoles.add(nodeRole); + } + + public Stream<Object[]> toRows() Review Comment: this method should return a `List<Object[]>` rows since we are going to always convert it to a list anyway. ########## sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemPropertiesTable.java: ########## @@ -0,0 +1,191 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.calcite.schema; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Preconditions; +import org.apache.calcite.DataContext; +import org.apache.calcite.linq4j.Enumerable; +import org.apache.calcite.linq4j.Linq4j; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.schema.ScannableTable; +import org.apache.calcite.schema.Schema; +import org.apache.calcite.schema.impl.AbstractTable; +import org.apache.druid.discovery.DiscoveryDruidNode; +import org.apache.druid.discovery.DruidNodeDiscoveryProvider; +import org.apache.druid.error.DruidException; +import org.apache.druid.java.util.common.RE; +import org.apache.druid.java.util.http.client.HttpClient; +import org.apache.druid.java.util.http.client.Request; +import org.apache.druid.java.util.http.client.response.StringFullResponseHandler; +import org.apache.druid.java.util.http.client.response.StringFullResponseHolder; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.server.DruidNode; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.server.security.AuthorizerMapper; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.table.RowSignatures; +import org.jboss.netty.handler.codec.http.HttpMethod; + +import javax.servlet.http.HttpServletResponse; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * System schema table {@code sys.server_properties} that contains the properties of all Druid servers. + * Each row contains the value of a single property. If a server has multiple node roles, all the rows for + * that server would have multiple values in the column {@code node_roles} rather than duplicating all the + * rows. + */ +public class SystemPropertiesTable extends AbstractTable implements ScannableTable +{ + public static final String TABLE_NAME = "server_properties"; + + static final RowSignature ROW_SIGNATURE = RowSignature + .builder() + .add("service_name", ColumnType.STRING) + .add("server", ColumnType.STRING) + .add("node_roles", ColumnType.STRING) + .add("property", ColumnType.STRING) + .add("value", ColumnType.STRING) + .build(); + + private final DruidNodeDiscoveryProvider druidNodeDiscoveryProvider; + private final AuthorizerMapper authorizerMapper; + private final HttpClient httpClient; + private final ObjectMapper jsonMapper; + + public SystemPropertiesTable( + DruidNodeDiscoveryProvider druidNodeDiscoveryProvider, + AuthorizerMapper authorizerMapper, + HttpClient httpClient, + ObjectMapper jsonMapper + ) + { + this.druidNodeDiscoveryProvider = druidNodeDiscoveryProvider; + this.authorizerMapper = authorizerMapper; + this.httpClient = httpClient; + this.jsonMapper = jsonMapper; + } + + @Override + public RelDataType getRowType(RelDataTypeFactory typeFactory) + { + return RowSignatures.toRelDataType(ROW_SIGNATURE, typeFactory); + } + + @Override + public Schema.TableType getJdbcTableType() + { + return Schema.TableType.SYSTEM_TABLE; + } + + @Override + public Enumerable<Object[]> scan(DataContext root) + { + final AuthenticationResult authenticationResult = (AuthenticationResult) Preconditions.checkNotNull( + root.get(PlannerContext.DATA_CTX_AUTHENTICATION_RESULT), + "authenticationResult in dataContext" + ); + SystemSchema.checkStateReadAccessForServers(authenticationResult, authorizerMapper); + final Iterator<DiscoveryDruidNode> druidServers = SystemSchema.getDruidServers(druidNodeDiscoveryProvider); + + final Map<String, ServerProperties> serverToPropertiesMap = new HashMap<>(); + druidServers.forEachRemaining(discoveryDruidNode -> { + final DruidNode druidNode = discoveryDruidNode.getDruidNode(); + final Map<String, String> propertiesMap = getProperties(druidNode); + if (serverToPropertiesMap.containsKey(druidNode.getHostAndPortToUse())) { + ServerProperties serverProperties = serverToPropertiesMap.get(druidNode.getHostAndPortToUse()); + serverProperties.addNodeRole(discoveryDruidNode.getNodeRole().getJsonName()); + } else { + serverToPropertiesMap.put(druidNode.getHostAndPortToUse(), new ServerProperties(druidNode.getServiceName(), druidNode.getHostAndPortToUse(), new ArrayList<>(Arrays.asList(discoveryDruidNode.getNodeRole().getJsonName())), propertiesMap)); + } + }); + return Linq4j.asEnumerable(serverToPropertiesMap.values().stream().flatMap(ServerProperties::toRows).collect(Collectors.toList())); + } + + private Map<String, String> getProperties(DruidNode druidNode) + { + final String url = druidNode.getUriToUse().resolve("/status/properties").toString(); + try { + final Request request = new Request(HttpMethod.GET, new URL(url)); + final StringFullResponseHolder response; + response = httpClient + .go(request, new StringFullResponseHandler(StandardCharsets.UTF_8)) + .get(); + + if (response.getStatus().getCode() != HttpServletResponse.SC_OK) { + throw new RE( + "Failed to get properties from node[%s]. Error code[%d], description[%s].", + url, + response.getStatus().getCode(), + response.getStatus().getReasonPhrase() + ); + } + return jsonMapper.readValue( + response.getContent(), + new TypeReference<Map<String, String>>(){} Review Comment: I think this can be omitted. ```suggestion new TypeReference<>(){} ``` ########## embedded-tests/src/test/java/org/apache/druid/testing/embedded/schema/SystemPropertiesTableTest.java: ########## @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.testing.embedded.schema; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.google.common.collect.ImmutableList; +import org.apache.druid.discovery.NodeRole; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.rpc.RequestBuilder; +import org.apache.druid.testing.embedded.EmbeddedBroker; +import org.apache.druid.testing.embedded.EmbeddedCoordinator; +import org.apache.druid.testing.embedded.EmbeddedDruidCluster; +import org.apache.druid.testing.embedded.EmbeddedOverlord; +import org.apache.druid.testing.embedded.junit5.EmbeddedClusterTestBase; +import org.jboss.netty.handler.codec.http.HttpMethod; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Map; + +public class SystemPropertiesTableTest extends EmbeddedClusterTestBase +{ + private static final String BROKER_PORT = "9082"; + private static final String BROKER_SERVICE = "test/broker"; + private static final String OVERLORD_PORT = "9090"; + private static final String OVERLORD_SERVICE = "test/overlord"; + private static final String COORDINATOR_PORT = "9081"; + private static final String COORDINATOR_SERVICE = "test/coordinator"; + + private final EmbeddedBroker broker = new EmbeddedBroker() + .addProperty("druid.service", BROKER_SERVICE) + .addProperty("druid.plaintextPort", BROKER_PORT) + .addProperty("test.onlyBroker", "brokerValue"); + + private final EmbeddedOverlord overlord = new EmbeddedOverlord() + .addProperty("druid.service", OVERLORD_SERVICE) + .addProperty("druid.plaintextPort", OVERLORD_PORT) + .addProperty("test.onlyOverlord", "overlordValue"); + + private final EmbeddedCoordinator coordinator = new EmbeddedCoordinator() + .addProperty("druid.service", COORDINATOR_SERVICE) + .addProperty("druid.plaintextPort", COORDINATOR_PORT) + .addProperty("test.onlyCoordinator", "coordinatorValue"); + + @Override + protected EmbeddedDruidCluster createCluster() + { + return EmbeddedDruidCluster + .withZookeeper() + .addServer(coordinator) + .addServer(overlord) + .addServer(broker) + .addCommonProperty("commonProperty", "commonValue"); + } + + @Test + public void test_serverPropertiesTable() + { + final Map<String, String> overlordProps = cluster.callApi().serviceClient().onLeaderOverlord( + mapper -> new RequestBuilder(HttpMethod.GET, "/status/properties"), + new TypeReference<>(){} + ); + verifyPropertiesForServer(overlordProps, OVERLORD_SERVICE, StringUtils.format("localhost:%s", OVERLORD_PORT), NodeRole.OVERLORD_JSON_NAME); + + final Map<String, String> brokerProps = cluster.callApi().serviceClient().onAnyBroker( + mapper -> new RequestBuilder(HttpMethod.GET, "/status/properties"), + new TypeReference<>(){} + ); + verifyPropertiesForServer(brokerProps, BROKER_SERVICE, StringUtils.format("localhost:%s", BROKER_PORT), NodeRole.BROKER_JSON_NAME); + + final Map<String, String> coordinatorProps = cluster.callApi().serviceClient().onLeaderCoordinator( + mapper -> new RequestBuilder(HttpMethod.GET, "/status/properties"), + new TypeReference<>(){} + ); + verifyPropertiesForServer(coordinatorProps, COORDINATOR_SERVICE, StringUtils.format("localhost:%s", COORDINATOR_PORT), NodeRole.COORDINATOR_JSON_NAME); + } + + private void verifyPropertiesForServer(Map<String, String> properties, String serivceName, String hostAndPort, String nodeRole) + { + String[] expectedRows = properties.entrySet().stream().map(entry -> String.join( + ",", + escapeCsvField(serivceName), + escapeCsvField(hostAndPort), + escapeCsvField(ImmutableList.of(nodeRole).toString()), + escapeCsvField(entry.getKey()), + escapeCsvField(entry.getValue()) + )).toArray(String[]::new); + Arrays.sort(expectedRows, String::compareTo); + String[] actualRows = Arrays.stream(cluster.runSql("SELECT * FROM sys.server_properties WHERE server='%s'", hostAndPort).split("\n")).map(entry -> StringUtils.replace(entry, "...", "\"\"")).toArray(String[]::new); Review Comment: Nit: break up the line for readability Also, is the removal of ellipsis (`...`) required or was it copied over from some other test? ```suggestion final String result = cluster.runSql("SELECT * FROM sys.server_properties WHERE server='%s'", hostAndPort); String[] actualRows = result.split("\n"); ``` ########## embedded-tests/src/test/java/org/apache/druid/testing/embedded/schema/SystemPropertiesTableTest.java: ########## @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.testing.embedded.schema; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.google.common.collect.ImmutableList; +import org.apache.druid.discovery.NodeRole; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.rpc.RequestBuilder; +import org.apache.druid.testing.embedded.EmbeddedBroker; +import org.apache.druid.testing.embedded.EmbeddedCoordinator; +import org.apache.druid.testing.embedded.EmbeddedDruidCluster; +import org.apache.druid.testing.embedded.EmbeddedOverlord; +import org.apache.druid.testing.embedded.junit5.EmbeddedClusterTestBase; +import org.jboss.netty.handler.codec.http.HttpMethod; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Map; + +public class SystemPropertiesTableTest extends EmbeddedClusterTestBase +{ + private static final String BROKER_PORT = "9082"; + private static final String BROKER_SERVICE = "test/broker"; + private static final String OVERLORD_PORT = "9090"; + private static final String OVERLORD_SERVICE = "test/overlord"; + private static final String COORDINATOR_PORT = "9081"; + private static final String COORDINATOR_SERVICE = "test/coordinator"; + + private final EmbeddedBroker broker = new EmbeddedBroker() + .addProperty("druid.service", BROKER_SERVICE) + .addProperty("druid.plaintextPort", BROKER_PORT) + .addProperty("test.onlyBroker", "brokerValue"); + + private final EmbeddedOverlord overlord = new EmbeddedOverlord() + .addProperty("druid.service", OVERLORD_SERVICE) + .addProperty("druid.plaintextPort", OVERLORD_PORT) + .addProperty("test.onlyOverlord", "overlordValue"); + + private final EmbeddedCoordinator coordinator = new EmbeddedCoordinator() + .addProperty("druid.service", COORDINATOR_SERVICE) + .addProperty("druid.plaintextPort", COORDINATOR_PORT) + .addProperty("test.onlyCoordinator", "coordinatorValue"); + + @Override + protected EmbeddedDruidCluster createCluster() + { + return EmbeddedDruidCluster + .withZookeeper() + .addServer(coordinator) + .addServer(overlord) + .addServer(broker) + .addCommonProperty("commonProperty", "commonValue"); + } + + @Test + public void test_serverPropertiesTable() Review Comment: - Please add a separate test method for each service. - Please add another test method which verifies the values of specific properties using some WHERE clauses. e.g. ```sql 1. SELECT value FROM sys.server_properties WHERE server = xyz AND property = abc; 2. SELECT value FROM sys.server_properties WHERE service_name = xyz AND property = abc; 3. SELECT * FROM sys.server_properties WHERE service_name = xyz AND property = abc; ``` Example shorthand snippet: ```java Assertions.assertEquals( "expectedValue", cluster.runSql("SELECT value FROM sys.server_properties WHERE server = xyz AND property = abc") ); Assertions.assertEquals( "localhost:1234,test/overlord,overlord,abc,propertyValue", cluster.runSql("SELECT * FROM sys.server_properties WHERE server = xyz AND property = abc") ); ``` ########## sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemPropertiesTable.java: ########## @@ -0,0 +1,191 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.calcite.schema; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Preconditions; +import org.apache.calcite.DataContext; +import org.apache.calcite.linq4j.Enumerable; +import org.apache.calcite.linq4j.Linq4j; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.schema.ScannableTable; +import org.apache.calcite.schema.Schema; +import org.apache.calcite.schema.impl.AbstractTable; +import org.apache.druid.discovery.DiscoveryDruidNode; +import org.apache.druid.discovery.DruidNodeDiscoveryProvider; +import org.apache.druid.error.DruidException; +import org.apache.druid.java.util.common.RE; +import org.apache.druid.java.util.http.client.HttpClient; +import org.apache.druid.java.util.http.client.Request; +import org.apache.druid.java.util.http.client.response.StringFullResponseHandler; +import org.apache.druid.java.util.http.client.response.StringFullResponseHolder; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.segment.column.RowSignature; +import org.apache.druid.server.DruidNode; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.server.security.AuthorizerMapper; +import org.apache.druid.sql.calcite.planner.PlannerContext; +import org.apache.druid.sql.calcite.table.RowSignatures; +import org.jboss.netty.handler.codec.http.HttpMethod; + +import javax.servlet.http.HttpServletResponse; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * System schema table {@code sys.server_properties} that contains the properties of all Druid servers. + * Each row contains the value of a single property. If a server has multiple node roles, all the rows for + * that server would have multiple values in the column {@code node_roles} rather than duplicating all the + * rows. + */ +public class SystemPropertiesTable extends AbstractTable implements ScannableTable +{ + public static final String TABLE_NAME = "server_properties"; + + static final RowSignature ROW_SIGNATURE = RowSignature + .builder() + .add("service_name", ColumnType.STRING) + .add("server", ColumnType.STRING) Review Comment: Nit: Please make `server` the first column since it is unique and serves as the "id" in this table. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
