FrankChen021 commented on code in PR #20183: URL: https://github.com/apache/druid/pull/20183#discussion_r4361101749
########## processing/src/main/java/org/apache/druid/query/SystemTableDataSource.java: ########## @@ -0,0 +1,110 @@ +/* + * 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.query; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.common.base.Preconditions; + +import java.util.Collections; +import java.util.Objects; +import java.util.Set; + +/** + * Identifies a Druid system table whose rows are supplied by a service rather than by segments. + */ +public class SystemTableDataSource extends LeafDataSource +{ + public static final String NODE_QUERY_ID_PREFIX = "native-system-node-"; + + private final String table; + + @JsonCreator + public SystemTableDataSource(@JsonProperty("table") final String table) + { + this.table = Preconditions.checkNotNull(table, "table"); + } + + @JsonProperty + public String getTable() + { + return table; + } + + @Override + public Set<String> getTableNames() + { + // QueryScheduler uses table names as cancellation authorization resources. Namespace the synthetic datasource + // name so a system table cannot collide with a regular Druid datasource that has the same unqualified name. Review Comment: The `sys.` name is no longer used for authorization. Reads and cancellation now use `SYSTEM_TABLE` resources named after the table. ########## server/src/main/java/org/apache/druid/server/system/table/ServerPropertiesTableDataProvider.java: ########## @@ -0,0 +1,218 @@ +/* + * 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.server.system.table; + +import com.google.common.collect.Maps; +import com.google.inject.Inject; +import org.apache.druid.client.DruidServerConfig; +import org.apache.druid.discovery.NodeRole; +import org.apache.druid.guice.annotations.Self; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.query.filter.DimFilter; +import org.apache.druid.query.filter.EqualityFilter; +import org.apache.druid.query.filter.SelectorDimFilter; +import org.apache.druid.server.DruidNode; +import org.apache.druid.server.security.Action; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.server.security.AuthorizationResult; +import org.apache.druid.server.security.AuthorizationUtils; +import org.apache.druid.server.security.AuthorizerMapper; +import org.apache.druid.server.security.ForbiddenException; +import org.apache.druid.server.security.Resource; +import org.apache.druid.server.security.ResourceAction; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.TreeMap; +import java.util.stream.Collectors; + +/** Native row supplier for {@code sys.server_properties}. */ +public class ServerPropertiesTableDataProvider implements SystemTableDataProvider +{ + private static final List<SystemTablePushdownFilter> PUSHDOWN_FILTERS = List.of( + new SystemTablePushdownFilter("server", null), + new SystemTablePushdownFilter("service_name", null) + ); + + private final DruidNode selfNode; + private final Set<NodeRole> selfNodeRoles; + private final AuthorizerMapper authorizerMapper; + private final Properties properties; + private final DruidServerConfig druidServerConfig; + + @Inject + public ServerPropertiesTableDataProvider( + @Self final DruidNode selfNode, + @Self final Set<NodeRole> selfNodeRoles, + final AuthorizerMapper authorizerMapper, + final Properties properties, + final DruidServerConfig druidServerConfig + ) + { + this.selfNode = selfNode; + this.selfNodeRoles = selfNodeRoles; + this.authorizerMapper = authorizerMapper; + this.properties = properties; + this.druidServerConfig = druidServerConfig; + } + + @Override + public List<SystemTablePushdownFilter> getPushdownFilters() + { + return PUSHDOWN_FILTERS; + } + + @Override + public Iterable<Object[]> getRows( + final List<DimFilter> filters, + final AuthenticationResult internalAuthenticationResult + ) + { + authorizeServerRead(internalAuthenticationResult); + + String serverFilter = null; + String serviceNameFilter = null; + for (final DimFilter filter : filters) { + if (isStringEqualityFilter(filter, "server")) { + serverFilter = getFilterValue(filter); + } else if (isStringEqualityFilter(filter, "service_name")) { + serviceNameFilter = getFilterValue(filter); + } + } + + final String server = selfNode.getHostAndPortToUse(); + if (serverFilter != null && !serverFilter.equals(server)) { + return Collections.emptyList(); + } + if (serviceNameFilter != null && !serviceNameFilter.equals(selfNode.getServiceName())) { + return Collections.emptyList(); + } + + final ServerProperties serverProperties = new ServerProperties(selfNode.getServiceName(), server); + selfNodeRoles.stream() + .map(NodeRole::getJsonName) + .sorted() + .forEach(serverProperties.nodeRoles::add); + + return buildRows(serverProperties); + } + + private void authorizeServerRead(final AuthenticationResult authenticationResult) Review Comment: Moved to `SystemTableAuthorization`. The descriptor's row authorizer is now the only place it's applied. ########## server/src/main/java/org/apache/druid/server/system/handler/SystemTableQueryHandler.java: ########## @@ -0,0 +1,167 @@ +/* + * 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.server.system.handler; + +import com.google.inject.Inject; +import org.apache.druid.client.DirectDruidClient; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.JodaUtils; +import org.apache.druid.java.util.common.guava.Sequence; +import org.apache.druid.query.BadQueryContextException; +import org.apache.druid.query.Druids; +import org.apache.druid.query.InlineDataSource; +import org.apache.druid.query.Query; +import org.apache.druid.query.QueryPlus; +import org.apache.druid.query.QueryRunner; +import org.apache.druid.query.SystemTableDataSource; +import org.apache.druid.query.context.ResponseContext; +import org.apache.druid.query.scan.ScanQuery; +import org.apache.druid.query.scan.ScanQueryEngine; +import org.apache.druid.segment.InlineSegmentWrangler; +import org.apache.druid.segment.Segment; +import org.apache.druid.server.DataSourceQueryHandler; +import org.apache.druid.server.security.AuthenticationResult; +import org.apache.druid.server.security.AuthorizerMapper; +import org.apache.druid.server.system.table.SystemTableDataProvider; +import org.apache.druid.server.system.table.SystemTableDescriptor; +import org.apache.druid.server.system.table.SystemTablePushdownFilter; + +import java.util.Map; + +/** Resolves one node-local system table and returns its rows through the standard native Scan stack. */ +public class SystemTableQueryHandler implements DataSourceQueryHandler +{ + private final Map<String, SystemTableDataProvider> dataSuppliers; + private final Map<String, SystemTableDescriptor> tableDescriptors; + private final ScanQueryEngine scanQueryEngine; + private final AuthorizerMapper authorizerMapper; + + @Inject + public SystemTableQueryHandler( + final Map<String, SystemTableDataProvider> dataSuppliers, + final Map<String, SystemTableDescriptor> tableDescriptors, + final ScanQueryEngine scanQueryEngine, + final AuthorizerMapper authorizerMapper + ) + { + this.dataSuppliers = dataSuppliers; + this.tableDescriptors = tableDescriptors; + this.scanQueryEngine = scanQueryEngine; + this.authorizerMapper = authorizerMapper; + } + + @Override + public <T> QueryRunner<T> createRunner( + final Query<T> query, + final AuthenticationResult requestAuthenticationResult, + final boolean executeLocally + ) + { + if (!(query instanceof ScanQuery)) { + throw new BadQueryContextException( + "Local system-table execution requires a Scan query with a SystemTableDataSource at the query root" + ); + } + if (!(query.getDataSource() instanceof SystemTableDataSource dataSource)) { + throw new BadQueryContextException( + "Local system-table execution requires a Scan query with a SystemTableDataSource at the query root" + ); + } + final SystemTableDataProvider dataSupplier = dataSuppliers.get(dataSource.getTable()); + if (dataSupplier == null) { + throw new ISE("System table[%s] is not served by this node", dataSource.getTable()); Review Comment: Converted, except `BadQueryContextException` in `readQuery` (only `QueryException` maps to 400 there) and `SystemTableNotLeaderException` (retry matches its class name). -- 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]
