HBASE-26481 Consider rolling upgrading from old region replication framework (#3880)
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / quotas / QuotaRetriever.java
bloba48ce71d607a90e3ffa331d4ee80e26534780843
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.
19 package org.apache.hadoop.hbase.quotas;
21 import java.io.Closeable;
22 import java.io.IOException;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.Objects;
26 import java.util.Queue;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.apache.yetus.audience.InterfaceAudience;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.apache.hadoop.hbase.client.Connection;
34 import org.apache.hadoop.hbase.client.ConnectionFactory;
35 import org.apache.hadoop.hbase.client.Result;
36 import org.apache.hadoop.hbase.client.ResultScanner;
37 import org.apache.hadoop.hbase.client.Scan;
38 import org.apache.hadoop.hbase.client.Table;
39 import org.apache.hadoop.util.StringUtils;
41 /**
42 * Scanner to iterate over the quota settings.
44 @InterfaceAudience.Public
45 public class QuotaRetriever implements Closeable, Iterable<QuotaSettings> {
46 private static final Logger LOG = LoggerFactory.getLogger(QuotaRetriever.class);
48 private final Queue<QuotaSettings> cache = new LinkedList<>();
49 private ResultScanner scanner;
50 /**
51 * Connection to use.
52 * Could pass one in and have this class use it but this class wants to be standalone.
54 private Connection connection;
55 private Table table;
57 /**
58 * Should QutoaRetriever manage the state of the connection, or leave it be.
60 private boolean isManagedConnection = false;
62 QuotaRetriever() {
65 void init(final Configuration conf, final Scan scan) throws IOException {
66 // Set this before creating the connection and passing it down to make sure
67 // it's cleaned up if we fail to construct the Scanner.
68 this.isManagedConnection = true;
69 init(ConnectionFactory.createConnection(conf), scan);
72 void init(final Connection conn, final Scan scan) throws IOException {
73 this.connection = Objects.requireNonNull(conn);
74 this.table = this.connection.getTable(QuotaTableUtil.QUOTA_TABLE_NAME);
75 try {
76 scanner = table.getScanner(scan);
77 } catch (IOException e) {
78 try {
79 close();
80 } catch (IOException ioe) {
81 LOG.warn("Failed getting scanner and then failed close on cleanup", e);
83 throw e;
87 @Override
88 public void close() throws IOException {
89 if (this.table != null) {
90 this.table.close();
91 this.table = null;
93 // Null out the connection on close() even if we didn't explicitly close it
94 // to maintain typical semantics.
95 if (isManagedConnection) {
96 if (this.connection != null) {
97 this.connection.close();
100 this.connection = null;
103 public QuotaSettings next() throws IOException {
104 if (cache.isEmpty()) {
105 Result result = scanner.next();
106 // Skip exceedThrottleQuota row key because this is not a QuotaSettings
107 if (result != null
108 && Bytes.equals(result.getRow(), QuotaTableUtil.getExceedThrottleQuotaRowKey())) {
109 result = scanner.next();
111 if (result == null) {
112 return null;
114 QuotaTableUtil.parseResultToCollection(result, cache);
116 return cache.poll();
119 @Override
120 public Iterator<QuotaSettings> iterator() {
121 return new Iter();
124 private class Iter implements Iterator<QuotaSettings> {
125 QuotaSettings cache;
127 public Iter() {
128 try {
129 cache = QuotaRetriever.this.next();
130 } catch (IOException e) {
131 LOG.warn(StringUtils.stringifyException(e));
135 @Override
136 public boolean hasNext() {
137 return cache != null;
140 @Override
141 public QuotaSettings next() {
142 QuotaSettings result = cache;
143 try {
144 cache = QuotaRetriever.this.next();
145 } catch (IOException e) {
146 LOG.warn(StringUtils.stringifyException(e));
148 return result;
151 @Override
152 public void remove() {
153 throw new RuntimeException("remove() not supported");
158 * Open a QuotaRetriever with no filter, all the quota settings will be returned.
159 * @param conf Configuration object to use.
160 * @return the QuotaRetriever
161 * @throws IOException if a remote or network exception occurs
163 public static QuotaRetriever open(final Configuration conf) throws IOException {
164 return open(conf, null);
168 * Open a QuotaRetriever with the specified filter.
169 * @param conf Configuration object to use.
170 * @param filter the QuotaFilter
171 * @return the QuotaRetriever
172 * @throws IOException if a remote or network exception occurs
174 public static QuotaRetriever open(final Configuration conf, final QuotaFilter filter)
175 throws IOException {
176 Scan scan = QuotaTableUtil.makeScan(filter);
177 QuotaRetriever scanner = new QuotaRetriever();
178 scanner.init(conf, scan);
179 return scanner;