|
| 1 | +/* |
| 2 | + * Copyright 2022-2023 Google LLC |
| 3 | + * Copyright 2013-2021 CompilerWorks |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.google.edwmigration.dumper.application.dumper.connector.teradata; |
| 18 | + |
| 19 | +import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_QUERY_LOG_ALTERNATES; |
| 20 | +import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_QUERY_LOG_ALTERNATES_DEPRECATION_MESSAGE; |
| 21 | +import static com.google.edwmigration.dumper.application.dumper.connector.teradata.AbstractTeradataConnector.DEF_LOG_TABLE; |
| 22 | +import static com.google.edwmigration.dumper.application.dumper.connector.teradata.TeradataLogsConnector.ASSESSMENT_DEF_LOG_TABLE; |
| 23 | +import static com.google.edwmigration.dumper.application.dumper.connector.teradata.TeradataLogsConnector.TeradataLogsConnectorProperty.QUERY_LOGS_TABLE; |
| 24 | +import static com.google.edwmigration.dumper.application.dumper.connector.teradata.TeradataLogsConnector.TeradataLogsConnectorProperty.SQL_LOGS_TABLE; |
| 25 | +import static com.google.edwmigration.dumper.application.dumper.connector.teradata.TeradataUtils.nonEmpty; |
| 26 | + |
| 27 | +import com.google.common.base.Preconditions; |
| 28 | +import com.google.common.collect.ImmutableList; |
| 29 | +import com.google.edwmigration.dumper.application.dumper.ConnectorArguments; |
| 30 | +import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException; |
| 31 | +import java.util.List; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +/** |
| 36 | + * Resolver that retrieves the names of query log tables from the command-line options. |
| 37 | + * |
| 38 | + * <p>Teradata saves query logs in two tables: {@code DBC.DBQLogTbl} and {@code DBC.DBQLSqlTbl}. |
| 39 | + * These two tables are called query log table and SQL log table for the purposes of the query logs |
| 40 | + * extraction. |
| 41 | + * |
| 42 | + * <p>Depending on the flags provided on the command-line, the {@code teradata-logs} connector uses |
| 43 | + * different pair of source tables for extraction: |
| 44 | + * |
| 45 | + * <ul> |
| 46 | + * <li>If the {@code --assessment} flag is not used, then the logs are extracted from tables |
| 47 | + * {@code DBC.DBQLogTbl} and {@code DBC.DBQLSqlTbl} |
| 48 | + * <li>If the {@code --assessment} flag is used, then the logs are extracted from tables {@code |
| 49 | + * DBC.QryLogV} (a view that selects the logs from {@code DBC.DBQLogTbl} table) and {@code |
| 50 | + * DBC.DBQLSqlTbl} |
| 51 | + * </ul> |
| 52 | + * |
| 53 | + * <p>Additionally, the user can specify an alternative table for each of the two tables: |
| 54 | + * |
| 55 | + * <ul> |
| 56 | + * <li>For the query log table, the command-line flag is: {@code -Dteradata-logs.query-log-table} |
| 57 | + * <li>For the SQL log table, the command-line flag is: {@code -Dteradata-logs.sql-log-table} |
| 58 | + * </ul> |
| 59 | + * |
| 60 | + * <p>Deprecated: There is also a second way of specifying both alternative tables by using the |
| 61 | + * {@code --query-log-alternates} command-line flag that accepts these two log tables as a parameter |
| 62 | + * (separated by a comma). |
| 63 | + */ |
| 64 | +class QueryLogTableNamesResolver { |
| 65 | + private static final Logger LOG = LoggerFactory.getLogger(QueryLogTableNamesResolver.class); |
| 66 | + |
| 67 | + private static final String TERADATA_LOGS_CONNECTOR_NAME = "teradata-logs"; |
| 68 | + |
| 69 | + static QueryLogTableNames resolve(ConnectorArguments arguments) { |
| 70 | + Preconditions.checkArgument( |
| 71 | + arguments.getConnectorName().equalsIgnoreCase(TERADATA_LOGS_CONNECTOR_NAME), |
| 72 | + "Resolver requires the connector to be '%s'.", |
| 73 | + TERADATA_LOGS_CONNECTOR_NAME); |
| 74 | + if (arguments.getQueryLogAlternates().isEmpty()) { |
| 75 | + return resolveFromDefinitionOptions(arguments); |
| 76 | + } |
| 77 | + return resolveFromLegacyOption(arguments); |
| 78 | + } |
| 79 | + |
| 80 | + private static QueryLogTableNames resolveFromDefinitionOptions(ConnectorArguments arguments) { |
| 81 | + boolean alternateQueryLogTableSpecified = arguments.isDefinitionSpecified(QUERY_LOGS_TABLE); |
| 82 | + boolean alternateSqlLogTableSpecified = arguments.isDefinitionSpecified(SQL_LOGS_TABLE); |
| 83 | + String queryLogTable = |
| 84 | + nonEmpty(arguments.getDefinition(QUERY_LOGS_TABLE)) |
| 85 | + .orElse(arguments.isAssessment() ? ASSESSMENT_DEF_LOG_TABLE : DEF_LOG_TABLE); |
| 86 | + String sqlLogTable = arguments.getDefinitionOrDefault(SQL_LOGS_TABLE); |
| 87 | + if (alternateQueryLogTableSpecified && !alternateSqlLogTableSpecified) { |
| 88 | + LOG.warn( |
| 89 | + "The alternate query log table was provided using the '-D{}' flag, but no" |
| 90 | + + " alternate SQL table was provided using the '-D{}' flag. The following tables" |
| 91 | + + " will be joined to extract the query logs: '{}' and '{}'.", |
| 92 | + QUERY_LOGS_TABLE.getName(), |
| 93 | + SQL_LOGS_TABLE.getName(), |
| 94 | + queryLogTable, |
| 95 | + sqlLogTable); |
| 96 | + } else if (!alternateQueryLogTableSpecified && alternateSqlLogTableSpecified) { |
| 97 | + LOG.warn( |
| 98 | + "The alternate SQL log table was provided using the '-D{}' flag, but no alternate" |
| 99 | + + " query table was provided using the '-D{}' flag. The following tables will be" |
| 100 | + + " joined to extract the query logs: '{}' and '{}'.", |
| 101 | + SQL_LOGS_TABLE.getName(), |
| 102 | + QUERY_LOGS_TABLE.getName(), |
| 103 | + queryLogTable, |
| 104 | + sqlLogTable); |
| 105 | + } |
| 106 | + return QueryLogTableNames.create( |
| 107 | + queryLogTable, |
| 108 | + sqlLogTable, |
| 109 | + alternateQueryLogTableSpecified || alternateSqlLogTableSpecified); |
| 110 | + } |
| 111 | + |
| 112 | + private static final QueryLogTableNames resolveFromLegacyOption(ConnectorArguments arguments) { |
| 113 | + List<String> legacyAlternates = arguments.getQueryLogAlternates(); |
| 114 | + boolean alternateQueryLogTableSpecified = arguments.isDefinitionSpecified(QUERY_LOGS_TABLE); |
| 115 | + boolean alternateSqlLogTableSpecified = arguments.isDefinitionSpecified(SQL_LOGS_TABLE); |
| 116 | + if (alternateQueryLogTableSpecified || alternateSqlLogTableSpecified) { |
| 117 | + ImmutableList.Builder<String> flagsUsed = ImmutableList.builder(); |
| 118 | + flagsUsed.add(OPT_QUERY_LOG_ALTERNATES); |
| 119 | + if (alternateQueryLogTableSpecified) { |
| 120 | + flagsUsed.add(QUERY_LOGS_TABLE.getName()); |
| 121 | + } |
| 122 | + if (alternateSqlLogTableSpecified) { |
| 123 | + flagsUsed.add(SQL_LOGS_TABLE.getName()); |
| 124 | + } |
| 125 | + throw new MetadataDumperUsageException( |
| 126 | + String.format( |
| 127 | + "Mixed alternate query log table configuration detected in flags '%s'. (", |
| 128 | + flagsUsed.build()) |
| 129 | + + OPT_QUERY_LOG_ALTERNATES_DEPRECATION_MESSAGE |
| 130 | + + ")"); |
| 131 | + } |
| 132 | + if (legacyAlternates.size() != 2) { |
| 133 | + throw new MetadataDumperUsageException( |
| 134 | + "Alternate query log tables must be given as a pair separated by a comma;" |
| 135 | + + " you specified: '" |
| 136 | + + legacyAlternates |
| 137 | + + "'. (" |
| 138 | + + OPT_QUERY_LOG_ALTERNATES_DEPRECATION_MESSAGE |
| 139 | + + ")"); |
| 140 | + } |
| 141 | + return QueryLogTableNames.create( |
| 142 | + /* queryLogsTableName= */ legacyAlternates.get(0), |
| 143 | + /* sqlLogsTableName= */ legacyAlternates.get(1), |
| 144 | + /* usingAtLeastOneAlternate= */ true); |
| 145 | + } |
| 146 | +} |
0 commit comments