Search in sources :

Example 91 with MetaException

use of org.apache.hadoop.hive.metastore.api.MetaException in project hive by apache.

the class ObjectStore method revokePrivileges.

@Override
public boolean revokePrivileges(PrivilegeBag privileges, boolean grantOption) throws InvalidObjectException, MetaException, NoSuchObjectException {
    boolean committed = false;
    try {
        openTransaction();
        List<Object> persistentObjs = new ArrayList<>();
        List<HiveObjectPrivilege> privilegeList = privileges.getPrivileges();
        if (CollectionUtils.isNotEmpty(privilegeList)) {
            Iterator<HiveObjectPrivilege> privIter = privilegeList.iterator();
            while (privIter.hasNext()) {
                HiveObjectPrivilege privDef = privIter.next();
                HiveObjectRef hiveObject = privDef.getHiveObject();
                String privilegeStr = privDef.getGrantInfo().getPrivilege();
                if (privilegeStr == null || privilegeStr.trim().equals("")) {
                    continue;
                }
                String[] privs = privilegeStr.split(",");
                String userName = privDef.getPrincipalName();
                PrincipalType principalType = privDef.getPrincipalType();
                if (hiveObject.getObjectType() == HiveObjectType.GLOBAL) {
                    List<MGlobalPrivilege> mSecUser = this.listPrincipalMGlobalGrants(userName, principalType);
                    boolean found = false;
                    if (mSecUser != null) {
                        for (String privilege : privs) {
                            for (MGlobalPrivilege userGrant : mSecUser) {
                                String userGrantPrivs = userGrant.getPrivilege();
                                if (privilege.equals(userGrantPrivs)) {
                                    found = true;
                                    if (grantOption) {
                                        if (userGrant.getGrantOption()) {
                                            userGrant.setGrantOption(false);
                                        } else {
                                            throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                        }
                                    }
                                    persistentObjs.add(userGrant);
                                    break;
                                }
                            }
                            if (!found) {
                                throw new InvalidObjectException("No user grant found for privileges " + privilege);
                            }
                        }
                    }
                } else if (hiveObject.getObjectType() == HiveObjectType.DATABASE) {
                    MDatabase dbObj = getMDatabase(hiveObject.getDbName());
                    if (dbObj != null) {
                        String db = hiveObject.getDbName();
                        boolean found = false;
                        List<MDBPrivilege> dbGrants = this.listPrincipalMDBGrants(userName, principalType, db);
                        for (String privilege : privs) {
                            for (MDBPrivilege dbGrant : dbGrants) {
                                String dbGrantPriv = dbGrant.getPrivilege();
                                if (privilege.equals(dbGrantPriv)) {
                                    found = true;
                                    if (grantOption) {
                                        if (dbGrant.getGrantOption()) {
                                            dbGrant.setGrantOption(false);
                                        } else {
                                            throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                        }
                                    }
                                    persistentObjs.add(dbGrant);
                                    break;
                                }
                            }
                            if (!found) {
                                throw new InvalidObjectException("No database grant found for privileges " + privilege + " on database " + db);
                            }
                        }
                    }
                } else if (hiveObject.getObjectType() == HiveObjectType.TABLE) {
                    boolean found = false;
                    List<MTablePrivilege> tableGrants = this.listAllMTableGrants(userName, principalType, hiveObject.getDbName(), hiveObject.getObjectName());
                    for (String privilege : privs) {
                        for (MTablePrivilege tabGrant : tableGrants) {
                            String tableGrantPriv = tabGrant.getPrivilege();
                            if (privilege.equalsIgnoreCase(tableGrantPriv)) {
                                found = true;
                                if (grantOption) {
                                    if (tabGrant.getGrantOption()) {
                                        tabGrant.setGrantOption(false);
                                    } else {
                                        throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                    }
                                }
                                persistentObjs.add(tabGrant);
                                break;
                            }
                        }
                        if (!found) {
                            throw new InvalidObjectException("No grant (" + privilege + ") found " + " on table " + hiveObject.getObjectName() + ", database is " + hiveObject.getDbName());
                        }
                    }
                } else if (hiveObject.getObjectType() == HiveObjectType.PARTITION) {
                    boolean found = false;
                    Table tabObj = this.getTable(hiveObject.getDbName(), hiveObject.getObjectName());
                    String partName = null;
                    if (hiveObject.getPartValues() != null) {
                        partName = Warehouse.makePartName(tabObj.getPartitionKeys(), hiveObject.getPartValues());
                    }
                    List<MPartitionPrivilege> partitionGrants = this.listPrincipalMPartitionGrants(userName, principalType, hiveObject.getDbName(), hiveObject.getObjectName(), partName);
                    for (String privilege : privs) {
                        for (MPartitionPrivilege partGrant : partitionGrants) {
                            String partPriv = partGrant.getPrivilege();
                            if (partPriv.equalsIgnoreCase(privilege)) {
                                found = true;
                                if (grantOption) {
                                    if (partGrant.getGrantOption()) {
                                        partGrant.setGrantOption(false);
                                    } else {
                                        throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                    }
                                }
                                persistentObjs.add(partGrant);
                                break;
                            }
                        }
                        if (!found) {
                            throw new InvalidObjectException("No grant (" + privilege + ") found " + " on table " + tabObj.getTableName() + ", partition is " + partName + ", database is " + tabObj.getDbName());
                        }
                    }
                } else if (hiveObject.getObjectType() == HiveObjectType.COLUMN) {
                    Table tabObj = this.getTable(hiveObject.getDbName(), hiveObject.getObjectName());
                    String partName = null;
                    if (hiveObject.getPartValues() != null) {
                        partName = Warehouse.makePartName(tabObj.getPartitionKeys(), hiveObject.getPartValues());
                    }
                    if (partName != null) {
                        List<MPartitionColumnPrivilege> mSecCol = listPrincipalMPartitionColumnGrants(userName, principalType, hiveObject.getDbName(), hiveObject.getObjectName(), partName, hiveObject.getColumnName());
                        boolean found = false;
                        if (mSecCol != null) {
                            for (String privilege : privs) {
                                for (MPartitionColumnPrivilege col : mSecCol) {
                                    String colPriv = col.getPrivilege();
                                    if (colPriv.equalsIgnoreCase(privilege)) {
                                        found = true;
                                        if (grantOption) {
                                            if (col.getGrantOption()) {
                                                col.setGrantOption(false);
                                            } else {
                                                throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                            }
                                        }
                                        persistentObjs.add(col);
                                        break;
                                    }
                                }
                                if (!found) {
                                    throw new InvalidObjectException("No grant (" + privilege + ") found " + " on table " + tabObj.getTableName() + ", partition is " + partName + ", column name = " + hiveObject.getColumnName() + ", database is " + tabObj.getDbName());
                                }
                            }
                        }
                    } else {
                        List<MTableColumnPrivilege> mSecCol = listPrincipalMTableColumnGrants(userName, principalType, hiveObject.getDbName(), hiveObject.getObjectName(), hiveObject.getColumnName());
                        boolean found = false;
                        if (mSecCol != null) {
                            for (String privilege : privs) {
                                for (MTableColumnPrivilege col : mSecCol) {
                                    String colPriv = col.getPrivilege();
                                    if (colPriv.equalsIgnoreCase(privilege)) {
                                        found = true;
                                        if (grantOption) {
                                            if (col.getGrantOption()) {
                                                col.setGrantOption(false);
                                            } else {
                                                throw new MetaException("User " + userName + " does not have grant option with privilege " + privilege);
                                            }
                                        }
                                        persistentObjs.add(col);
                                        break;
                                    }
                                }
                                if (!found) {
                                    throw new InvalidObjectException("No grant (" + privilege + ") found " + " on table " + tabObj.getTableName() + ", column name = " + hiveObject.getColumnName() + ", database is " + tabObj.getDbName());
                                }
                            }
                        }
                    }
                }
            }
        }
        if (CollectionUtils.isNotEmpty(persistentObjs)) {
            if (grantOption) {
            // If grant option specified, only update the privilege, don't remove it.
            // Grant option has already been removed from the privileges in the section above
            } else {
                pm.deletePersistentAll(persistentObjs);
            }
        }
        committed = commitTransaction();
    } finally {
        if (!committed) {
            rollbackTransaction();
        }
    }
    return committed;
}
Also used : ArrayList(java.util.ArrayList) MPartitionColumnPrivilege(org.apache.hadoop.hive.metastore.model.MPartitionColumnPrivilege) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) LinkedList(java.util.LinkedList) MStringList(org.apache.hadoop.hive.metastore.model.MStringList) ArrayList(java.util.ArrayList) List(java.util.List) MTableColumnPrivilege(org.apache.hadoop.hive.metastore.model.MTableColumnPrivilege) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) MVersionTable(org.apache.hadoop.hive.metastore.model.MVersionTable) Table(org.apache.hadoop.hive.metastore.api.Table) MTable(org.apache.hadoop.hive.metastore.model.MTable) HiveObjectRef(org.apache.hadoop.hive.metastore.api.HiveObjectRef) MDBPrivilege(org.apache.hadoop.hive.metastore.model.MDBPrivilege) MGlobalPrivilege(org.apache.hadoop.hive.metastore.model.MGlobalPrivilege) MDatabase(org.apache.hadoop.hive.metastore.model.MDatabase) HiveObjectPrivilege(org.apache.hadoop.hive.metastore.api.HiveObjectPrivilege) MPartitionPrivilege(org.apache.hadoop.hive.metastore.model.MPartitionPrivilege) PrincipalType(org.apache.hadoop.hive.metastore.api.PrincipalType) MTablePrivilege(org.apache.hadoop.hive.metastore.model.MTablePrivilege)

Example 92 with MetaException

use of org.apache.hadoop.hive.metastore.api.MetaException in project hive by apache.

the class ObjectStore method revokeRole.

@Override
public boolean revokeRole(Role role, String userName, PrincipalType principalType, boolean grantOption) throws MetaException, NoSuchObjectException {
    boolean success = false;
    try {
        openTransaction();
        MRoleMap roleMember = getMSecurityUserRoleMap(userName, principalType, role.getRoleName());
        if (grantOption) {
            // Revoke with grant option - only remove the grant option but keep the role.
            if (roleMember.getGrantOption()) {
                roleMember.setGrantOption(false);
            } else {
                throw new MetaException("User " + userName + " does not have grant option with role " + role.getRoleName());
            }
        } else {
            // No grant option in revoke, remove the whole role.
            pm.deletePersistent(roleMember);
        }
        success = commitTransaction();
    } finally {
        if (!success) {
            rollbackTransaction();
        }
    }
    return success;
}
Also used : MRoleMap(org.apache.hadoop.hive.metastore.model.MRoleMap) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 93 with MetaException

use of org.apache.hadoop.hive.metastore.api.MetaException in project hive by apache.

the class ObjectStore method addMasterKey.

@Override
public int addMasterKey(String key) throws MetaException {
    LOG.debug("Begin executing addMasterKey");
    boolean committed = false;
    MMasterKey masterKey = new MMasterKey(key);
    try {
        openTransaction();
        pm.makePersistent(masterKey);
        committed = commitTransaction();
    } finally {
        if (!committed) {
            rollbackTransaction();
        }
    }
    LOG.debug("Done executing addMasterKey with status : {}", committed);
    if (committed) {
        return ((IntIdentity) pm.getObjectId(masterKey)).getKey();
    } else {
        throw new MetaException("Failed to add master key.");
    }
}
Also used : MMasterKey(org.apache.hadoop.hive.metastore.model.MMasterKey) IntIdentity(javax.jdo.identity.IntIdentity) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 94 with MetaException

use of org.apache.hadoop.hive.metastore.api.MetaException in project hive by apache.

the class ObjectStore method createDbGuidAndPersist.

private String createDbGuidAndPersist() throws MetaException {
    boolean success = false;
    Query query = null;
    try {
        openTransaction();
        MMetastoreDBProperties prop = new MMetastoreDBProperties();
        prop.setPropertykey("guid");
        final String guid = UUID.randomUUID().toString();
        LOG.debug("Attempting to add a guid {} for the metastore db", guid);
        prop.setPropertyValue(guid);
        prop.setDescription("Metastore DB GUID generated on " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));
        pm.makePersistent(prop);
        success = commitTransaction();
        if (success) {
            LOG.info("Metastore db guid {} created successfully", guid);
            return guid;
        }
    } catch (Exception e) {
        LOG.warn("Metastore db guid creation failed", e);
    } finally {
        rollbackAndCleanup(success, query);
    }
    // it possible that some other HMS instance could have created the guid
    // at the same time due which this instance could not create a guid above
    // in such case return the guid already generated
    final String guid = getGuidFromDB();
    if (guid == null) {
        throw new MetaException("Unable to create or fetch the metastore database uuid");
    }
    return guid;
}
Also used : Query(javax.jdo.Query) MMetastoreDBProperties(org.apache.hadoop.hive.metastore.model.MMetastoreDBProperties) AlreadyExistsException(org.apache.hadoop.hive.metastore.api.AlreadyExistsException) InvalidInputException(org.apache.hadoop.hive.metastore.api.InvalidInputException) InvalidOperationException(org.apache.hadoop.hive.metastore.api.InvalidOperationException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) IOException(java.io.IOException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) JDOCanRetryException(javax.jdo.JDOCanRetryException) InvalidPartitionException(org.apache.hadoop.hive.metastore.api.InvalidPartitionException) UnknownPartitionException(org.apache.hadoop.hive.metastore.api.UnknownPartitionException) InvalidObjectException(org.apache.hadoop.hive.metastore.api.InvalidObjectException) JDOException(javax.jdo.JDOException) MissingTableException(org.datanucleus.store.rdbms.exceptions.MissingTableException) SQLException(java.sql.SQLException) UnknownDBException(org.apache.hadoop.hive.metastore.api.UnknownDBException) TException(org.apache.thrift.TException) JDODataStoreException(javax.jdo.JDODataStoreException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) UnknownTableException(org.apache.hadoop.hive.metastore.api.UnknownTableException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 95 with MetaException

use of org.apache.hadoop.hive.metastore.api.MetaException in project hive by apache.

the class CompactionTxnHandler method cleanEmptyAbortedTxns.

/**
 * Clean up aborted transactions from txns that have no components in txn_components.  The reason such
 * txns exist can be that now work was done in this txn (e.g. Streaming opened TransactionBatch and
 * abandoned it w/o doing any work) or due to {@link #markCleaned(CompactionInfo)} being called.
 */
@Override
@RetrySemantics.SafeToRetry
public void cleanEmptyAbortedTxns() throws MetaException {
    try {
        Connection dbConn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // Aborted is a terminal state, so nothing about the txn can change
            // after that, so READ COMMITTED is sufficient.
            dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED);
            stmt = dbConn.createStatement();
            String s = "select txn_id from TXNS where " + "txn_id not in (select tc_txnid from TXN_COMPONENTS) and " + "txn_state = '" + TXN_ABORTED + "'";
            LOG.debug("Going to execute query <" + s + ">");
            rs = stmt.executeQuery(s);
            List<Long> txnids = new ArrayList<>();
            while (rs.next()) txnids.add(rs.getLong(1));
            close(rs);
            if (txnids.size() <= 0) {
                return;
            }
            // easier to read logs
            Collections.sort(txnids);
            List<String> queries = new ArrayList<>();
            StringBuilder prefix = new StringBuilder();
            StringBuilder suffix = new StringBuilder();
            prefix.append("delete from TXNS where ");
            suffix.append("");
            TxnUtils.buildQueryWithINClause(conf, queries, prefix, suffix, txnids, "txn_id", false, false);
            for (String query : queries) {
                LOG.debug("Going to execute update <" + query + ">");
                int rc = stmt.executeUpdate(query);
                LOG.info("Removed " + rc + "  empty Aborted transactions from TXNS");
            }
            LOG.info("Aborted transactions removed from TXNS: " + txnids);
            LOG.debug("Going to commit");
            dbConn.commit();
        } catch (SQLException e) {
            LOG.error("Unable to delete from txns table " + e.getMessage());
            LOG.debug("Going to rollback");
            rollbackDBConn(dbConn);
            checkRetryable(dbConn, e, "cleanEmptyAbortedTxns");
            throw new MetaException("Unable to connect to transaction database " + StringUtils.stringifyException(e));
        } finally {
            close(rs, stmt, dbConn);
        }
    } catch (RetryException e) {
        cleanEmptyAbortedTxns();
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Aggregations

MetaException (org.apache.hadoop.hive.metastore.api.MetaException)318 IOException (java.io.IOException)123 ArrayList (java.util.ArrayList)95 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)74 TException (org.apache.thrift.TException)67 Table (org.apache.hadoop.hive.metastore.api.Table)59 Partition (org.apache.hadoop.hive.metastore.api.Partition)57 SQLException (java.sql.SQLException)55 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)53 Path (org.apache.hadoop.fs.Path)45 Connection (java.sql.Connection)36 InvalidOperationException (org.apache.hadoop.hive.metastore.api.InvalidOperationException)34 AlreadyExistsException (org.apache.hadoop.hive.metastore.api.AlreadyExistsException)32 Statement (java.sql.Statement)31 Test (org.junit.Test)30 List (java.util.List)25 Database (org.apache.hadoop.hive.metastore.api.Database)25 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)25 ResultSet (java.sql.ResultSet)22 UnknownDBException (org.apache.hadoop.hive.metastore.api.UnknownDBException)22