Skip to content

Commit

Permalink
Setup util class for getting UGI
Browse files Browse the repository at this point in the history
  • Loading branch information
Haizhou Zhao committed Jan 4, 2023
1 parent d8f9e98 commit 3e1d92b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package org.apache.iceberg.hive;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -35,7 +33,6 @@
import org.apache.hadoop.hive.metastore.api.PrincipalType;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.api.UnknownDBException;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.iceberg.BaseMetastoreCatalog;
import org.apache.iceberg.BaseMetastoreTableOperations;
import org.apache.iceberg.CatalogProperties;
Expand Down Expand Up @@ -570,13 +567,8 @@ Database convertToDatabase(Namespace namespace, Map<String, String> meta) {
});

if (database.getOwnerName() == null) {
try {
database.setOwnerName(UserGroupInformation.getCurrentUser().getUserName());
database.setOwnerType(PrincipalType.USER);
} catch (IOException e) {
throw new UncheckedIOException(
String.format("Fail to obtain default (UGI) user for database %s", database), e);
}
database.setOwnerName(HiveHadoopUtil.getCurrentUser());
database.setOwnerType(PrincipalType.USER);
}

database.setParameters(parameter);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.iceberg.hive;

import java.io.IOException;
import java.io.UncheckedIOException;
import org.apache.hadoop.security.UserGroupInformation;

public class HiveHadoopUtil {

private HiveHadoopUtil() {}

public static String getCurrentUser() {
try {
return UserGroupInformation.getCurrentUser().getUserName();
} catch (IOException e) {
throw new UncheckedIOException("Fail to obtain default (UGI) user", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
Expand Down Expand Up @@ -56,7 +54,6 @@
import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.iceberg.BaseMetastoreTableOperations;
import org.apache.iceberg.ClientPool;
import org.apache.iceberg.PartitionSpecParser;
Expand Down Expand Up @@ -425,23 +422,11 @@ private Table newHmsTable(TableMetadata metadata) {
Preconditions.checkNotNull(metadata, "'metadata' parameter can't be null");
final long currentTimeMillis = System.currentTimeMillis();

String owner = metadata.properties().get(HiveCatalog.HMS_TABLE_OWNER);
if (owner == null) {
try {
owner = UserGroupInformation.getCurrentUser().getUserName();
} catch (IOException e) {
throw new UncheckedIOException(
String.format(
"Fail to obtain default (UGI) user when creating table %s.%s", database, tableName),
e);
}
}

Table newTable =
new Table(
tableName,
database,
owner,
metadata.property(HiveCatalog.HMS_TABLE_OWNER, HiveHadoopUtil.getCurrentUser()),
(int) currentTimeMillis / 1000,
(int) currentTimeMillis / 1000,
Integer.MAX_VALUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public void testReplaceTxnBuilder() throws Exception {
}

@Test
public void testCreateTableWithOwner() throws IOException {
public void testCreateTableWithOwner() throws Exception {
createTableAndVerifyOwner(
DB_NAME,
"tbl_specified_owner",
Expand All @@ -262,19 +262,20 @@ public void testCreateTableWithOwner() throws IOException {
}

private void createTableAndVerifyOwner(
String db, String tbl, Map<String, String> prop, String expectedOwner) {
String db, String tbl, Map<String, String> prop, String expectedOwner)
throws IOException, TException {
TableIdentifier tableIdent = TableIdentifier.of(db, tbl);
try {
Schema schema = getTestSchema();
PartitionSpec spec = PartitionSpec.builderFor(schema).bucket("data", 16).build();
String location = temp.newFolder(tbl).toString();
catalog.createTable(TableIdentifier.of(db, tbl), schema, spec, location, prop);
catalog.createTable(tableIdent, schema, spec, location, prop);
org.apache.hadoop.hive.metastore.api.Table hmsTable = metastoreClient.getTable(db, tbl);
Assert.assertEquals(expectedOwner, hmsTable.getOwner());
Assert.assertFalse(hmsTable.getParameters().containsKey(HiveCatalog.HMS_TABLE_OWNER));
} catch (IOException | TException e) {
throw new RuntimeException("Unexpected exception", e);
Map<String, String> hmsTableParams = hmsTable.getParameters();
Assert.assertFalse(hmsTableParams.containsKey(HiveCatalog.HMS_TABLE_OWNER));
} finally {
catalog.dropTable(TableIdentifier.of(db, tbl));
catalog.dropTable(tableIdent);
}
}

Expand Down

0 comments on commit 3e1d92b

Please sign in to comment.