diff --git metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java index dc14084..8d72da2 100644 --- metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java +++ metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java @@ -87,22 +87,28 @@ import org.apache.hadoop.hive.metastore.api.UnknownPartitionException; import org.apache.hadoop.hive.metastore.api.UnknownTableException; import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; +import org.apache.hadoop.hive.metastore.events.AddIndexEvent; +import org.apache.hadoop.hive.metastore.events.AlterIndexEvent; import org.apache.hadoop.hive.metastore.events.AddPartitionEvent; import org.apache.hadoop.hive.metastore.events.AlterPartitionEvent; import org.apache.hadoop.hive.metastore.events.AlterTableEvent; import org.apache.hadoop.hive.metastore.events.CreateDatabaseEvent; import org.apache.hadoop.hive.metastore.events.CreateTableEvent; import org.apache.hadoop.hive.metastore.events.DropDatabaseEvent; +import org.apache.hadoop.hive.metastore.events.DropIndexEvent; import org.apache.hadoop.hive.metastore.events.DropPartitionEvent; import org.apache.hadoop.hive.metastore.events.DropTableEvent; import org.apache.hadoop.hive.metastore.events.EventCleanerTask; import org.apache.hadoop.hive.metastore.events.LoadPartitionDoneEvent; +import org.apache.hadoop.hive.metastore.events.PreAddIndexEvent; import org.apache.hadoop.hive.metastore.events.PreAddPartitionEvent; +import org.apache.hadoop.hive.metastore.events.PreAlterIndexEvent; import org.apache.hadoop.hive.metastore.events.PreAlterPartitionEvent; import org.apache.hadoop.hive.metastore.events.PreAlterTableEvent; import org.apache.hadoop.hive.metastore.events.PreCreateDatabaseEvent; import org.apache.hadoop.hive.metastore.events.PreCreateTableEvent; import org.apache.hadoop.hive.metastore.events.PreDropDatabaseEvent; +import org.apache.hadoop.hive.metastore.events.PreDropIndexEvent; import org.apache.hadoop.hive.metastore.events.PreDropPartitionEvent; import org.apache.hadoop.hive.metastore.events.PreDropTableEvent; import org.apache.hadoop.hive.metastore.events.PreEventContext; @@ -1128,19 +1134,31 @@ private boolean is_table_exists(RawStore ms, String dbname, String name) return (ms.getTable(dbname, name) != null); } - private void drop_table_core(final RawStore ms, final String dbname, final String name, - final boolean deleteData, final EnvironmentContext envContext) - throws NoSuchObjectException, MetaException, IOException, - InvalidObjectException, InvalidInputException { + private boolean drop_table_core(final RawStore ms, final String dbname, final String name, + final boolean deleteData, final EnvironmentContext envContext, + final String indexName) throws NoSuchObjectException, + MetaException, IOException, InvalidObjectException, InvalidInputException { + boolean success = false; + ms.openTransaction(); + try { + if (success = drop_table_core_notxn(ms, dbname, name, indexName, deleteData, envContext)) { + success = ms.commitTransaction(); + } + } finally { + if (!success) { + ms.rollbackTransaction(); + } + } + return success; + } + + private boolean drop_table_core_notxn(RawStore ms, String dbname, String name, + String indexName, boolean deleteData, EnvironmentContext envContext) + throws NoSuchObjectException, MetaException, + IOException, InvalidObjectException, InvalidInputException { boolean success = false; - boolean isExternal = false; - Path tblPath = null; - List partPaths = null; Table tbl = null; - isExternal = false; - boolean isIndexTable = false; try { - ms.openTransaction(); // drop any partitions tbl = get_table(dbname, name); if (tbl == null) { @@ -1152,8 +1170,8 @@ private void drop_table_core(final RawStore ms, final String dbname, final Strin firePreEvent(new PreDropTableEvent(tbl, deleteData, this)); - isIndexTable = isIndexTable(tbl); - if (isIndexTable) { + boolean isIndexTable = isIndexTable(tbl); + if (indexName == null && isIndexTable) { throw new RuntimeException( "The table " + name + " is an index table. Please do drop index instead."); } @@ -1171,40 +1189,43 @@ private void drop_table_core(final RawStore ms, final String dbname, final Strin throw new MetaException(e.getMessage()); } } - isExternal = isExternal(tbl); + Path tblPath = null; + boolean isExternal = isExternal(tbl); if (tbl.getSd().getLocation() != null) { tblPath = new Path(tbl.getSd().getLocation()); if (!wh.isWritable(tblPath.getParent())) { - throw new MetaException("Table metadata not deleted since " + + String target = indexName == null ? "Table" : "Index table"; + throw new MetaException(target + " metadata not deleted since " + tblPath.getParent() + " is not writable by " + hiveConf.getUser()); } } // Drop the partitions and get a list of locations which need to be deleted - partPaths = dropPartitionsAndGetLocations(ms, dbname, name, tblPath, + List partPaths = dropPartitionsAndGetLocations(ms, dbname, name, tblPath, tbl.getPartitionKeys(), deleteData && !isExternal); if (!ms.dropTable(dbname, name)) { - throw new MetaException("Unable to drop table"); + String tableName = dbname + "." + name; + throw new MetaException(indexName == null ? "Unable to drop table " + tableName: + "Unable to drop index table " + tableName + " for index " + indexName); } - success = ms.commitTransaction(); - } finally { - if (!success) { - ms.rollbackTransaction(); - } else if (deleteData && !isExternal) { + if (deleteData && !isExternal) { // Delete the data in the partitions which have other locations deletePartitionData(partPaths); // Delete the data in the table deleteTableData(tblPath); // ok even if the data is not deleted } + success = true; + } finally { for (MetaStoreEventListener listener : listeners) { DropTableEvent dropTableEvent = new DropTableEvent(tbl, success, deleteData, this); dropTableEvent.setEnvironmentContext(envContext); listener.onDropTable(dropTableEvent); } } + return success; } /** @@ -1321,8 +1342,7 @@ public void drop_table_with_environment_context(final String dbname, final Strin boolean success = false; Exception ex = null; try { - drop_table_core(getMS(), dbname, name, deleteData, envContext); - success = true; + success = drop_table_core(getMS(), dbname, name, deleteData, envContext, null); } catch (IOException e) { ex = e; throw new MetaException(e.getMessage()); @@ -2356,7 +2376,12 @@ public void alter_index(final String dbname, final String base_table_name, boolean success = false; Exception ex = null; + Index oldIndex = null; try { + oldIndex = get_index_by_name(dbname, base_table_name, index_name); + + firePreEvent(new PreAlterIndexEvent(oldIndex, newIndex, this)); + getMS().alterIndex(dbname, base_table_name, index_name, newIndex); success = true; } catch (InvalidObjectException e) { @@ -2375,6 +2400,10 @@ public void alter_index(final String dbname, final String base_table_name, } } finally { endFunction("alter_index", success, ex, base_table_name); + for (MetaStoreEventListener listener : listeners) { + AlterIndexEvent alterIndexEvent = new AlterIndexEvent(oldIndex, newIndex, success, this); + listener.onAlterIndex(alterIndexEvent); + } } return; } @@ -2969,6 +2998,8 @@ private Index add_index_core(final RawStore ms, final Index index, final Table i try { ms.openTransaction(); + firePreEvent(new PreAddIndexEvent(index, this)); + Index old_index = null; try { old_index = get_index_by_name(index.getDbName(), index @@ -3016,6 +3047,10 @@ private Index add_index_core(final RawStore ms, final Index index, final Table i } ms.rollbackTransaction(); } + for (MetaStoreEventListener listener : listeners) { + AddIndexEvent addIndexEvent = new AddIndexEvent(index, success, this); + listener.onAddIndex(addIndexEvent); + } } } @@ -3060,56 +3095,34 @@ private boolean drop_index_by_name_core(final RawStore ms, MetaException, TException, IOException, InvalidObjectException, InvalidInputException { boolean success = false; - Path tblPath = null; - List partPaths = null; + Index index = null; try { ms.openTransaction(); // drop the underlying index table - Index index = get_index_by_name(dbName, tblName, indexName); - if (index == null) { - throw new NoSuchObjectException(indexName + " doesn't exist"); - } + index = get_index_by_name(dbName, tblName, indexName); // throws exception if not exists + + firePreEvent(new PreDropIndexEvent(index, this)); + ms.dropIndex(dbName, tblName, indexName); String idxTblName = index.getIndexTableName(); if (idxTblName != null) { - Table tbl = null; - tbl = this.get_table(dbName, idxTblName); - if (tbl.getSd() == null) { - throw new MetaException("Table metadata is corrupted"); - } - - if (tbl.getSd().getLocation() != null) { - tblPath = new Path(tbl.getSd().getLocation()); - if (!wh.isWritable(tblPath.getParent())) { - throw new MetaException("Index table metadata not deleted since " + - tblPath.getParent() + " is not writable by " + - hiveConf.getUser()); - } - } - - // Drop the partitions and get a list of partition locations which need to be deleted - partPaths = dropPartitionsAndGetLocations(ms, dbName, idxTblName, tblPath, - tbl.getPartitionKeys(), deleteData); - - if (!ms.dropTable(dbName, idxTblName)) { - throw new MetaException("Unable to drop underlying data table " - + idxTblName + " for index " + idxTblName); - } + success = drop_table_core_notxn(ms, dbName, idxTblName, indexName, deleteData, null); + } + if (success) { + success = ms.commitTransaction(); } - success = ms.commitTransaction(); } finally { if (!success) { ms.rollbackTransaction(); - return false; - } else if (deleteData && tblPath != null) { - deletePartitionData(partPaths); - deleteTableData(tblPath); - // ok even if the data is not deleted + } + for (MetaStoreEventListener listener : listeners) { + DropIndexEvent dropIndexEvent = new DropIndexEvent(index, success, this); + listener.onDropIndex(dropIndexEvent); } } - return true; + return success; } @Override @@ -3138,7 +3151,7 @@ public Index get_index_by_name(final String dbName, final String tblName, throw me; } } finally { - endFunction("drop_index_by_name", ret != null, ex, tblName); + endFunction("get_index_by_name", ret != null, ex, tblName); } return ret; } diff --git metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreEventListener.java metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreEventListener.java index c28c46a..dca8ec3 100644 --- metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreEventListener.java +++ metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreEventListener.java @@ -21,12 +21,15 @@ import org.apache.hadoop.conf.Configurable; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.hadoop.hive.metastore.events.AddIndexEvent; +import org.apache.hadoop.hive.metastore.events.AlterIndexEvent; import org.apache.hadoop.hive.metastore.events.AddPartitionEvent; import org.apache.hadoop.hive.metastore.events.AlterPartitionEvent; import org.apache.hadoop.hive.metastore.events.AlterTableEvent; import org.apache.hadoop.hive.metastore.events.CreateDatabaseEvent; import org.apache.hadoop.hive.metastore.events.CreateTableEvent; import org.apache.hadoop.hive.metastore.events.DropDatabaseEvent; +import org.apache.hadoop.hive.metastore.events.DropIndexEvent; import org.apache.hadoop.hive.metastore.events.DropPartitionEvent; import org.apache.hadoop.hive.metastore.events.DropTableEvent; import org.apache.hadoop.hive.metastore.events.LoadPartitionDoneEvent; @@ -109,7 +112,27 @@ public void onDropDatabase (DropDatabaseEvent dbEvent) throws MetaException { * @throws MetaException */ public void onLoadPartitionDone(LoadPartitionDoneEvent partSetDoneEvent) throws MetaException { + } + + /** + * @param indexEvent index event + * @throws MetaException + */ + public void onAddIndex(AddIndexEvent indexEvent) throws MetaException { + } + + /** + * @param indexEvent index event + * @throws MetaException + */ + public void onDropIndex(DropIndexEvent indexEvent) throws MetaException { + } + /** + * @param indexEvent index event + * @throws MetaException + */ + public void onAlterIndex(AlterIndexEvent indexEvent) throws MetaException { } @Override diff --git metastore/src/java/org/apache/hadoop/hive/metastore/events/AddIndexEvent.java metastore/src/java/org/apache/hadoop/hive/metastore/events/AddIndexEvent.java new file mode 100644 index 0000000..43ac0aa --- /dev/null +++ metastore/src/java/org/apache/hadoop/hive/metastore/events/AddIndexEvent.java @@ -0,0 +1,36 @@ +/** + * 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.hadoop.hive.metastore.events; + +import org.apache.hadoop.hive.metastore.HiveMetaStore; +import org.apache.hadoop.hive.metastore.api.Index; + +public class AddIndexEvent extends ListenerEvent { + + private final Index index; + + public AddIndexEvent(Index index, boolean status, HiveMetaStore.HMSHandler handler) { + super(status, handler); + this.index = index; + } + + public Index getIndex() { + return index; + } +} diff --git metastore/src/java/org/apache/hadoop/hive/metastore/events/AlterIndexEvent.java metastore/src/java/org/apache/hadoop/hive/metastore/events/AlterIndexEvent.java new file mode 100644 index 0000000..4a49700 --- /dev/null +++ metastore/src/java/org/apache/hadoop/hive/metastore/events/AlterIndexEvent.java @@ -0,0 +1,43 @@ +/** + * 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.hadoop.hive.metastore.events; + +import org.apache.hadoop.hive.metastore.HiveMetaStore; +import org.apache.hadoop.hive.metastore.api.Index; + +public class AlterIndexEvent extends ListenerEvent { + + private final Index newIndex; + private final Index oldIndex; + + public AlterIndexEvent(Index oldIndex, Index newIndex, boolean status, + HiveMetaStore.HMSHandler handler) { + super(status, handler); + this.oldIndex = oldIndex; + this.newIndex = newIndex; + } + + public Index getOldIndex() { + return oldIndex; + } + + public Index getNewIndex() { + return newIndex; + } +} diff --git metastore/src/java/org/apache/hadoop/hive/metastore/events/DropIndexEvent.java metastore/src/java/org/apache/hadoop/hive/metastore/events/DropIndexEvent.java new file mode 100644 index 0000000..06f2302 --- /dev/null +++ metastore/src/java/org/apache/hadoop/hive/metastore/events/DropIndexEvent.java @@ -0,0 +1,36 @@ +/** + * 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.hadoop.hive.metastore.events; + +import org.apache.hadoop.hive.metastore.HiveMetaStore; +import org.apache.hadoop.hive.metastore.api.Index; + +public class DropIndexEvent extends ListenerEvent { + + private final Index index; + + public DropIndexEvent(Index index, boolean status, HiveMetaStore.HMSHandler handler) { + super(status, handler); + this.index = index; + } + + public Index getIndex() { + return index; + } +} diff --git metastore/src/java/org/apache/hadoop/hive/metastore/events/PreAddIndexEvent.java metastore/src/java/org/apache/hadoop/hive/metastore/events/PreAddIndexEvent.java new file mode 100644 index 0000000..baa04a5 --- /dev/null +++ metastore/src/java/org/apache/hadoop/hive/metastore/events/PreAddIndexEvent.java @@ -0,0 +1,37 @@ +/** + * 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.hadoop.hive.metastore.events; + +import org.apache.hadoop.hive.metastore.HiveMetaStore; +import org.apache.hadoop.hive.metastore.api.Index; + +public class PreAddIndexEvent extends PreEventContext { + +private final Index table; + + public PreAddIndexEvent(Index table, HiveMetaStore.HMSHandler handler) { + super(PreEventType.ADD_INDEX, handler); + this.table = table; + } + + public Index getIndex() { + return table; + } +} + diff --git metastore/src/java/org/apache/hadoop/hive/metastore/events/PreAlterIndexEvent.java metastore/src/java/org/apache/hadoop/hive/metastore/events/PreAlterIndexEvent.java new file mode 100644 index 0000000..8ab5af8 --- /dev/null +++ metastore/src/java/org/apache/hadoop/hive/metastore/events/PreAlterIndexEvent.java @@ -0,0 +1,43 @@ +/** + * 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.hadoop.hive.metastore.events; + +import org.apache.hadoop.hive.metastore.HiveMetaStore; +import org.apache.hadoop.hive.metastore.api.Index; + +public class PreAlterIndexEvent extends PreEventContext { + +private final Index newIndex; + private final Index oldIndex; + + public PreAlterIndexEvent(Index oldIndex, Index newIndex, HiveMetaStore.HMSHandler handler) { + super(PreEventType.ALTER_INDEX, handler); + this.oldIndex = oldIndex; + this.newIndex = newIndex; + } + + public Index getOldIndex() { + return oldIndex; + } + + public Index getNewIndex() { + return newIndex; + } +} + diff --git metastore/src/java/org/apache/hadoop/hive/metastore/events/PreDropIndexEvent.java metastore/src/java/org/apache/hadoop/hive/metastore/events/PreDropIndexEvent.java new file mode 100644 index 0000000..437e5c1 --- /dev/null +++ metastore/src/java/org/apache/hadoop/hive/metastore/events/PreDropIndexEvent.java @@ -0,0 +1,36 @@ +/** + * 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.hadoop.hive.metastore.events; + +import org.apache.hadoop.hive.metastore.HiveMetaStore; +import org.apache.hadoop.hive.metastore.api.Index; + +public class PreDropIndexEvent extends PreEventContext { + + private final Index index; + + public PreDropIndexEvent(Index index, HiveMetaStore.HMSHandler handler) { + super(PreEventType.DROP_INDEX, handler); + this.index = index; + } + + public Index getIndex() { + return index; + } +} diff --git metastore/src/java/org/apache/hadoop/hive/metastore/events/PreEventContext.java metastore/src/java/org/apache/hadoop/hive/metastore/events/PreEventContext.java index 5021a73..79c5ab2 100644 --- metastore/src/java/org/apache/hadoop/hive/metastore/events/PreEventContext.java +++ metastore/src/java/org/apache/hadoop/hive/metastore/events/PreEventContext.java @@ -36,7 +36,10 @@ ALTER_PARTITION, CREATE_DATABASE, DROP_DATABASE, - LOAD_PARTITION_DONE + LOAD_PARTITION_DONE, + ADD_INDEX, + ALTER_INDEX, + DROP_INDEX } private final PreEventType eventType; diff --git metastore/src/test/org/apache/hadoop/hive/metastore/DummyListener.java metastore/src/test/org/apache/hadoop/hive/metastore/DummyListener.java index 92f2388..6805da8 100644 --- metastore/src/test/org/apache/hadoop/hive/metastore/DummyListener.java +++ metastore/src/test/org/apache/hadoop/hive/metastore/DummyListener.java @@ -23,12 +23,15 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.hadoop.hive.metastore.events.AddIndexEvent; import org.apache.hadoop.hive.metastore.events.AddPartitionEvent; +import org.apache.hadoop.hive.metastore.events.AlterIndexEvent; import org.apache.hadoop.hive.metastore.events.AlterPartitionEvent; import org.apache.hadoop.hive.metastore.events.AlterTableEvent; import org.apache.hadoop.hive.metastore.events.CreateDatabaseEvent; import org.apache.hadoop.hive.metastore.events.CreateTableEvent; import org.apache.hadoop.hive.metastore.events.DropDatabaseEvent; +import org.apache.hadoop.hive.metastore.events.DropIndexEvent; import org.apache.hadoop.hive.metastore.events.DropPartitionEvent; import org.apache.hadoop.hive.metastore.events.DropTableEvent; import org.apache.hadoop.hive.metastore.events.ListenerEvent; @@ -102,4 +105,18 @@ public void onLoadPartitionDone(LoadPartitionDoneEvent partEvent) throws MetaExc notifyList.add(partEvent); } + @Override + public void onAddIndex(AddIndexEvent indexEvent) throws MetaException { + notifyList.add(indexEvent); + } + + @Override + public void onDropIndex(DropIndexEvent indexEvent) throws MetaException { + notifyList.add(indexEvent); + } + + @Override + public void onAlterIndex(AlterIndexEvent indexEvent) throws MetaException { + notifyList.add(indexEvent); + } } diff --git metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java index 4951c94..9c786fb 100644 --- metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java @@ -28,25 +28,32 @@ import org.apache.hadoop.hive.cli.CliSessionState; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.api.Database; +import org.apache.hadoop.hive.metastore.api.Index; import org.apache.hadoop.hive.metastore.api.Partition; import org.apache.hadoop.hive.metastore.api.PartitionEventType; import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.events.AddIndexEvent; import org.apache.hadoop.hive.metastore.events.AddPartitionEvent; +import org.apache.hadoop.hive.metastore.events.AlterIndexEvent; import org.apache.hadoop.hive.metastore.events.AlterPartitionEvent; import org.apache.hadoop.hive.metastore.events.AlterTableEvent; import org.apache.hadoop.hive.metastore.events.CreateDatabaseEvent; import org.apache.hadoop.hive.metastore.events.CreateTableEvent; import org.apache.hadoop.hive.metastore.events.DropDatabaseEvent; +import org.apache.hadoop.hive.metastore.events.DropIndexEvent; import org.apache.hadoop.hive.metastore.events.DropPartitionEvent; import org.apache.hadoop.hive.metastore.events.DropTableEvent; import org.apache.hadoop.hive.metastore.events.ListenerEvent; import org.apache.hadoop.hive.metastore.events.LoadPartitionDoneEvent; +import org.apache.hadoop.hive.metastore.events.PreAddIndexEvent; import org.apache.hadoop.hive.metastore.events.PreAddPartitionEvent; +import org.apache.hadoop.hive.metastore.events.PreAlterIndexEvent; import org.apache.hadoop.hive.metastore.events.PreAlterPartitionEvent; import org.apache.hadoop.hive.metastore.events.PreAlterTableEvent; import org.apache.hadoop.hive.metastore.events.PreCreateDatabaseEvent; import org.apache.hadoop.hive.metastore.events.PreCreateTableEvent; import org.apache.hadoop.hive.metastore.events.PreDropDatabaseEvent; +import org.apache.hadoop.hive.metastore.events.PreDropIndexEvent; import org.apache.hadoop.hive.metastore.events.PreDropPartitionEvent; import org.apache.hadoop.hive.metastore.events.PreDropTableEvent; import org.apache.hadoop.hive.metastore.events.PreEventContext; @@ -180,6 +187,29 @@ private void validateDropDb(Database expectedDb, Database actualDb) { assertEquals(expectedDb, actualDb); } + private void validateIndex(Index expectedIndex, Index actualIndex) { + assertEquals(expectedIndex.getDbName(), actualIndex.getDbName()); + assertEquals(expectedIndex.getIndexName(), actualIndex.getIndexName()); + assertEquals(expectedIndex.getIndexHandlerClass(), actualIndex.getIndexHandlerClass()); + assertEquals(expectedIndex.getOrigTableName(), actualIndex.getOrigTableName()); + assertEquals(expectedIndex.getIndexTableName(), actualIndex.getIndexTableName()); + assertEquals(expectedIndex.getSd().getLocation(), actualIndex.getSd().getLocation()); + } + + private void validateAddIndex(Index expectedIndex, Index actualIndex) { + validateIndex(expectedIndex, actualIndex); + } + + private void validateAlterIndex(Index expectedOldIndex, Index actualOldIndex, + Index expectedNewIndex, Index actualNewIndex) { + validateIndex(expectedOldIndex, actualOldIndex); + validateIndex(expectedNewIndex, actualNewIndex); + } + + private void validateDropIndex(Index expectedIndex, Index actualIndex) { + validateIndex(expectedIndex, actualIndex); + } + public void testListener() throws Exception { int listSize = 0; @@ -215,6 +245,50 @@ public void testListener() throws Exception { PreCreateTableEvent preTblEvent = (PreCreateTableEvent)(preNotifyList.get(listSize - 1)); validateCreateTable(tbl, preTblEvent.getTable()); + driver.run("create index tmptbl_i on table tmptbl(a) as 'compact' " + + "WITH DEFERRED REBUILD IDXPROPERTIES ('prop1'='val1', 'prop2'='val2')"); + listSize += 2; // creates index table internally + assertEquals(notifyList.size(), listSize); + assertEquals(preNotifyList.size(), listSize); + + Index oldIndex = msc.getIndex(dbName, "tmptbl", "tmptbl_i"); + + AddIndexEvent addIndexEvent = (AddIndexEvent)notifyList.get(listSize - 1); + assert addIndexEvent.getStatus(); + validateAddIndex(oldIndex, addIndexEvent.getIndex()); + + PreAddIndexEvent preAddIndexEvent = (PreAddIndexEvent)(preNotifyList.get(listSize - 2)); + validateAddIndex(oldIndex, preAddIndexEvent.getIndex()); + + driver.run("alter index tmptbl_i on tmptbl set IDXPROPERTIES " + + "('prop1'='val1_new', 'prop3'='val3')"); + listSize++; + assertEquals(notifyList.size(), listSize); + assertEquals(preNotifyList.size(), listSize); + + Index newIndex = msc.getIndex(dbName, "tmptbl", "tmptbl_i"); + + AlterIndexEvent alterIndexEvent = (AlterIndexEvent) notifyList.get(listSize - 1); + assert alterIndexEvent.getStatus(); + validateAlterIndex(oldIndex, alterIndexEvent.getOldIndex(), + newIndex, alterIndexEvent.getNewIndex()); + + PreAlterIndexEvent preAlterIndexEvent = (PreAlterIndexEvent) (preNotifyList.get(listSize - 1)); + validateAlterIndex(oldIndex, preAlterIndexEvent.getOldIndex(), + newIndex, preAlterIndexEvent.getNewIndex()); + + driver.run("drop index tmptbl_i on tmptbl"); + listSize += 2; + assertEquals(notifyList.size(), listSize); + assertEquals(preNotifyList.size(), listSize); + + DropIndexEvent dropIndexEvent = (DropIndexEvent) notifyList.get(listSize - 1); + assert dropIndexEvent.getStatus(); + validateDropIndex(newIndex, dropIndexEvent.getIndex()); + + PreDropIndexEvent preDropIndexEvent = (PreDropIndexEvent) (preNotifyList.get(listSize - 2)); + validateDropIndex(newIndex, preDropIndexEvent.getIndex()); + driver.run("alter table tmptbl add partition (b='2011')"); listSize++; Partition part = msc.getPartition("hive2038", "tmptbl", "b=2011"); diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java index 939defc..0ed315a 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/DDLTask.java @@ -887,8 +887,7 @@ private int alterDatabase(AlterDatabaseDesc alterDbDesc) throws HiveException { } private int dropIndex(Hive db, DropIndexDesc dropIdx) throws HiveException { - db.dropIndex(db.getCurrentDatabase(), dropIdx.getTableName(), - dropIdx.getIndexName(), true); + db.dropIndex(db.getCurrentDatabase(), dropIdx.getTableName(), dropIdx.getIndexName()); return 0; } diff --git ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java index 17daaa1..50a7996 100644 --- ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java +++ ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java @@ -811,11 +811,19 @@ public Index getIndex(String dbName, String baseTableName, } } - public boolean dropIndex(String db_name, String tbl_name, String index_name, boolean deleteData) throws HiveException { + public void dropIndex(String db_name, String tbl_name, String index_name) + throws HiveException { + dropIndex(db_name, tbl_name, index_name, true, true); + } + + public void dropIndex(String db_name, String tbl_name, String index_name, + boolean deleteData, boolean ignoreUnknownIdx) throws HiveException { try { - return getMSC().dropIndex(db_name, tbl_name, index_name, deleteData); + getMSC().dropIndex(db_name, tbl_name, index_name, deleteData); } catch (NoSuchObjectException e) { - throw new HiveException("Partition or table doesn't exist.", e); + if (!ignoreUnknownIdx) { + throw new HiveException("Index doesn't exist.", e); + } } catch (Exception e) { throw new HiveException("Unknown error. Please check logs.", e); } diff --git ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java index d7abdfd..57ba0de 100644 --- ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java +++ ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java @@ -481,7 +481,7 @@ public void clearTestSideEffects () throws Exception { List indexes = db.getIndexes(dbName, tblName, (short)-1); if (indexes != null && indexes.size() > 0) { for (Index index : indexes) { - db.dropIndex(dbName, tblName, index.getIndexName(), true); + db.dropIndex(dbName, tblName, index.getIndexName()); } } } diff --git ql/src/test/org/apache/hadoop/hive/ql/metadata/TestHive.java ql/src/test/org/apache/hadoop/hive/ql/metadata/TestHive.java index 4ee5267..e91d7fb 100755 --- ql/src/test/org/apache/hadoop/hive/ql/metadata/TestHive.java +++ ql/src/test/org/apache/hadoop/hive/ql/metadata/TestHive.java @@ -455,7 +455,7 @@ public void testIndex() throws Throwable { // Drop index try { - hm.dropIndex(MetaStoreUtils.DEFAULT_DATABASE_NAME, tableName, indexName, true); + hm.dropIndex(MetaStoreUtils.DEFAULT_DATABASE_NAME, tableName, indexName); } catch (HiveException e) { System.err.println(StringUtils.stringifyException(e)); assertTrue("Unable to drop index: " + indexName, false);