|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.phoenix.mapreduce.index; |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.sql.Connection; |
| 24 | + |
| 25 | +import java.sql.SQLException; |
| 26 | +import java.util.UUID; |
| 27 | + |
| 28 | + |
| 29 | +import org.apache.hadoop.hbase.NamespaceDescriptor; |
| 30 | +import org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting; |
| 31 | +import org.apache.hadoop.hbase.TableExistsException; |
| 32 | +import org.apache.hadoop.hbase.client.*; |
| 33 | +import org.apache.phoenix.coprocessorclient.MetaDataProtocol; |
| 34 | +import org.apache.phoenix.query.QueryConstants; |
| 35 | + |
| 36 | +import org.apache.hadoop.conf.Configured; |
| 37 | +import org.apache.hadoop.hbase.TableName; |
| 38 | + |
| 39 | +import org.apache.phoenix.jdbc.PhoenixConnection; |
| 40 | + |
| 41 | +import org.apache.phoenix.query.ConnectionQueryServices; |
| 42 | + |
| 43 | +import org.apache.phoenix.schema.PTableType; |
| 44 | +import org.apache.phoenix.util.ClientUtil; |
| 45 | +import org.apache.phoenix.util.SchemaUtil; |
| 46 | +import org.slf4j.Logger; |
| 47 | +import org.slf4j.LoggerFactory; |
| 48 | + |
| 49 | +import static org.apache.phoenix.query.QueryConstants.SYSTEM_SCHEMA_NAME; |
| 50 | + |
| 51 | +/** |
| 52 | + * Utility class to create index tables and/or migrate them. |
| 53 | + * |
| 54 | + */ |
| 55 | +public class IndexToolTableUtil extends Configured { |
| 56 | + private static final Logger LOGGER = LoggerFactory.getLogger(IndexToolTableUtil.class); |
| 57 | + |
| 58 | + public final static String OUTPUT_TABLE_NAME = "PHOENIX_INDEX_TOOL"; |
| 59 | + public static String SYSTEM_OUTPUT_TABLE_NAME = SchemaUtil.getTableName(SYSTEM_SCHEMA_NAME, |
| 60 | + OUTPUT_TABLE_NAME); |
| 61 | + |
| 62 | + public final static String RESULT_TABLE_NAME = "PHOENIX_INDEX_TOOL_RESULT"; |
| 63 | + public static String SYSTEM_RESULT_TABLE_NAME = SchemaUtil.getTableName(SYSTEM_SCHEMA_NAME, |
| 64 | + RESULT_TABLE_NAME); |
| 65 | + |
| 66 | + public static void setIndexToolTableName(Connection connection) throws Exception { |
| 67 | + ConnectionQueryServices queryServices = connection.unwrap(PhoenixConnection.class).getQueryServices(); |
| 68 | + if (SchemaUtil.isNamespaceMappingEnabled(PTableType.SYSTEM, queryServices.getConfiguration())) { |
| 69 | + SYSTEM_OUTPUT_TABLE_NAME = SchemaUtil.getTableName(SYSTEM_SCHEMA_NAME, OUTPUT_TABLE_NAME).replace( |
| 70 | + QueryConstants.NAME_SEPARATOR, |
| 71 | + QueryConstants.NAMESPACE_SEPARATOR); |
| 72 | + SYSTEM_RESULT_TABLE_NAME = SchemaUtil.getTableName(SYSTEM_SCHEMA_NAME, RESULT_TABLE_NAME).replace( |
| 73 | + QueryConstants.NAME_SEPARATOR, |
| 74 | + QueryConstants.NAMESPACE_SEPARATOR); |
| 75 | + } else { |
| 76 | + SYSTEM_OUTPUT_TABLE_NAME = SchemaUtil.getTableName(SYSTEM_SCHEMA_NAME, OUTPUT_TABLE_NAME); |
| 77 | + SYSTEM_RESULT_TABLE_NAME = SchemaUtil.getTableName(SYSTEM_SCHEMA_NAME, RESULT_TABLE_NAME); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static Table createResultTable(Connection connection) throws IOException, SQLException { |
| 82 | + ConnectionQueryServices queryServices = connection.unwrap(PhoenixConnection.class).getQueryServices(); |
| 83 | + try (Admin admin = queryServices.getAdmin()) { |
| 84 | + TableName resultTableName = TableName.valueOf(SYSTEM_RESULT_TABLE_NAME); |
| 85 | + if (SYSTEM_RESULT_TABLE_NAME.contains(QueryConstants.NAMESPACE_SEPARATOR)) { |
| 86 | + createSystemNamespaceTable(connection); |
| 87 | + } |
| 88 | + return createTable(admin, resultTableName); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static Table createOutputTable(Connection connection) throws IOException, SQLException { |
| 93 | + ConnectionQueryServices queryServices = connection.unwrap(PhoenixConnection.class).getQueryServices(); |
| 94 | + try (Admin admin = queryServices.getAdmin()) { |
| 95 | + TableName outputTableName = TableName.valueOf(SYSTEM_OUTPUT_TABLE_NAME); |
| 96 | + if (SYSTEM_OUTPUT_TABLE_NAME.contains(QueryConstants.NAMESPACE_SEPARATOR)) { |
| 97 | + createSystemNamespaceTable(connection); |
| 98 | + } |
| 99 | + return createTable(admin, outputTableName); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static void createSystemNamespaceTable(Connection connection) throws IOException, SQLException { |
| 104 | + ConnectionQueryServices queryServices = connection.unwrap(PhoenixConnection.class).getQueryServices(); |
| 105 | + if (SchemaUtil.isNamespaceMappingEnabled(PTableType.SYSTEM, queryServices.getConfiguration())) { |
| 106 | + try (Admin admin = queryServices.getAdmin()) { |
| 107 | + if (!ClientUtil.isHBaseNamespaceAvailable(admin, SYSTEM_SCHEMA_NAME)) { |
| 108 | + NamespaceDescriptor namespaceDescriptor = |
| 109 | + NamespaceDescriptor.create(SYSTEM_SCHEMA_NAME).build(); |
| 110 | + admin.createNamespace(namespaceDescriptor); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @VisibleForTesting |
| 117 | + private static Table createTable(Admin admin, TableName tableName) throws IOException { |
| 118 | + if (!admin.tableExists(tableName)) { |
| 119 | + ColumnFamilyDescriptor columnDescriptor = |
| 120 | + ColumnFamilyDescriptorBuilder |
| 121 | + .newBuilder(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES) |
| 122 | + .setTimeToLive(MetaDataProtocol.DEFAULT_LOG_TTL) |
| 123 | + .build(); |
| 124 | + TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) |
| 125 | + .setColumnFamily(columnDescriptor).build(); |
| 126 | + try { |
| 127 | + admin.createTable(tableDescriptor); |
| 128 | + } catch (TableExistsException e) { |
| 129 | + LOGGER.warn("Table exists, ignoring", e); |
| 130 | + } |
| 131 | + } |
| 132 | + return admin.getConnection().getTable(tableName); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + public static void createNewIndexToolTables(Connection connection) throws Exception { |
| 137 | + setIndexToolTableName(connection); |
| 138 | + |
| 139 | + migrateTable(connection, OUTPUT_TABLE_NAME); |
| 140 | + migrateTable(connection, RESULT_TABLE_NAME); |
| 141 | + } |
| 142 | + |
| 143 | + private static void migrateTable(Connection connection, String tableName) throws Exception { |
| 144 | + if (!tableName.equals(OUTPUT_TABLE_NAME) && !tableName.equals(RESULT_TABLE_NAME)) { |
| 145 | + LOGGER.info("Only migrating PHOENIX_INDEX_TOOL tables!"); |
| 146 | + } else { |
| 147 | + ConnectionQueryServices queryServices = connection.unwrap(PhoenixConnection.class).getQueryServices(); |
| 148 | + try (Admin admin = queryServices.getAdmin()) { |
| 149 | + TableName oldTableName = TableName.valueOf(tableName); |
| 150 | + String newTableNameString = tableName.equals(OUTPUT_TABLE_NAME) ? |
| 151 | + SYSTEM_OUTPUT_TABLE_NAME : SYSTEM_RESULT_TABLE_NAME; |
| 152 | + |
| 153 | + TableName newTableName = TableName.valueOf(newTableNameString); |
| 154 | + |
| 155 | + if (admin.tableExists(oldTableName)) { |
| 156 | + String snapshotName = tableName + "_" + UUID.randomUUID(); |
| 157 | + admin.disableTable(oldTableName); |
| 158 | + admin.snapshot(snapshotName, oldTableName); |
| 159 | + admin.cloneSnapshot(snapshotName, newTableName); |
| 160 | + admin.deleteSnapshot(snapshotName); |
| 161 | + admin.deleteTable(oldTableName); |
| 162 | + } else { |
| 163 | + createTable(admin, newTableName); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments