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
;
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
;
52 * Could pass one in and have this class use it but this class wants to be standalone.
54 private Connection connection
;
58 * Should QutoaRetriever manage the state of the connection, or leave it be.
60 private boolean isManagedConnection
= false;
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
);
76 scanner
= table
.getScanner(scan
);
77 } catch (IOException e
) {
80 } catch (IOException ioe
) {
81 LOG
.warn("Failed getting scanner and then failed close on cleanup", e
);
88 public void close() throws IOException
{
89 if (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
108 && Bytes
.equals(result
.getRow(), QuotaTableUtil
.getExceedThrottleQuotaRowKey())) {
109 result
= scanner
.next();
111 if (result
== null) {
114 QuotaTableUtil
.parseResultToCollection(result
, cache
);
120 public Iterator
<QuotaSettings
> iterator() {
124 private class Iter
implements Iterator
<QuotaSettings
> {
129 cache
= QuotaRetriever
.this.next();
130 } catch (IOException e
) {
131 LOG
.warn(StringUtils
.stringifyException(e
));
136 public boolean hasNext() {
137 return cache
!= null;
141 public QuotaSettings
next() {
142 QuotaSettings result
= cache
;
144 cache
= QuotaRetriever
.this.next();
145 } catch (IOException e
) {
146 LOG
.warn(StringUtils
.stringifyException(e
));
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
)
176 Scan scan
= QuotaTableUtil
.makeScan(filter
);
177 QuotaRetriever scanner
= new QuotaRetriever();
178 scanner
.init(conf
, scan
);