HBASE-21779 Reimplement BulkLoadHFilesTool to use AsyncClusterConnection
[hbase.git] / hbase-mapreduce / src / main / java / org / apache / hadoop / hbase / mapreduce / DefaultVisibilityExpressionResolver.java
blob07f05dd79804b3b4ca0894abd84940f6aa9bb1ac
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.mapreduce;
20 import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_FAMILY;
21 import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABELS_TABLE_NAME;
22 import static org.apache.hadoop.hbase.security.visibility.VisibilityConstants.LABEL_QUALIFIER;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.TableNotFoundException;
31 import org.apache.hadoop.hbase.Tag;
32 import org.apache.yetus.audience.InterfaceAudience;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.apache.hadoop.hbase.client.Connection;
36 import org.apache.hadoop.hbase.client.ConnectionFactory;
37 import org.apache.hadoop.hbase.client.Result;
38 import org.apache.hadoop.hbase.client.ResultScanner;
39 import org.apache.hadoop.hbase.client.Scan;
40 import org.apache.hadoop.hbase.client.Table;
41 import org.apache.hadoop.hbase.security.visibility.Authorizations;
42 import org.apache.hadoop.hbase.security.visibility.VisibilityConstants;
43 import org.apache.hadoop.hbase.security.visibility.VisibilityLabelOrdinalProvider;
44 import org.apache.hadoop.hbase.security.visibility.VisibilityUtils;
45 import org.apache.hadoop.hbase.util.Bytes;
47 /**
48 * This implementation creates tags by expanding expression using label ordinal. Labels will be
49 * serialized in sorted order of it's ordinal.
51 @InterfaceAudience.Private
52 public class DefaultVisibilityExpressionResolver implements VisibilityExpressionResolver {
53 private static final Logger LOG =
54 LoggerFactory.getLogger(DefaultVisibilityExpressionResolver.class);
56 private Configuration conf;
57 private final Map<String, Integer> labels = new HashMap<>();
59 @Override
60 public Configuration getConf() {
61 return this.conf;
64 @Override
65 public void setConf(Configuration conf) {
66 this.conf = conf;
69 @Override
70 public void init() {
71 // Reading all the labels and ordinal.
72 // This scan should be done by user with global_admin privileges.. Ensure that it works
73 Table labelsTable = null;
74 Connection connection = null;
75 try {
76 connection = ConnectionFactory.createConnection(conf);
77 try {
78 labelsTable = connection.getTable(LABELS_TABLE_NAME);
79 } catch (IOException e) {
80 LOG.error("Error opening 'labels' table", e);
81 return;
83 Scan scan = new Scan();
84 scan.setAuthorizations(new Authorizations(VisibilityUtils.SYSTEM_LABEL));
85 scan.addColumn(LABELS_TABLE_FAMILY, LABEL_QUALIFIER);
86 ResultScanner scanner = null;
87 try {
88 scanner = labelsTable.getScanner(scan);
89 Result next = null;
90 while ((next = scanner.next()) != null) {
91 byte[] row = next.getRow();
92 byte[] value = next.getValue(LABELS_TABLE_FAMILY, LABEL_QUALIFIER);
93 labels.put(Bytes.toString(value), Bytes.toInt(row));
95 } catch (TableNotFoundException e) {
96 // Table not found. So just return
97 return;
98 } catch (IOException e) {
99 LOG.error("Error scanning 'labels' table", e);
100 } finally {
101 if (scanner != null) scanner.close();
103 } catch (IOException ioe) {
104 LOG.error("Failed reading 'labels' tags", ioe);
105 return;
106 } finally {
107 if (labelsTable != null) {
108 try {
109 labelsTable.close();
110 } catch (IOException ioe) {
111 LOG.warn("Error closing 'labels' table", ioe);
114 if (connection != null)
115 try {
116 connection.close();
117 } catch (IOException ioe) {
118 LOG.warn("Failed close of temporary connection", ioe);
123 @Override
124 public List<Tag> createVisibilityExpTags(String visExpression) throws IOException {
125 VisibilityLabelOrdinalProvider provider = new VisibilityLabelOrdinalProvider() {
126 @Override
127 public int getLabelOrdinal(String label) {
128 Integer ordinal = null;
129 ordinal = labels.get(label);
130 if (ordinal != null) {
131 return ordinal.intValue();
133 return VisibilityConstants.NON_EXIST_LABEL_ORDINAL;
136 @Override
137 public String getLabel(int ordinal) {
138 // Unused
139 throw new UnsupportedOperationException(
140 "getLabel should not be used in VisibilityExpressionResolver");
143 return VisibilityUtils.createVisibilityExpTags(visExpression, true, false, null, provider);