1
2
3
4
5
6
7
8
9
10
11
12 package org.apache.hadoop.hbase.quotas;
13
14 import org.apache.hadoop.hbase.classification.InterfaceAudience;
15 import org.apache.hadoop.hbase.classification.InterfaceStability;
16 import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.Quotas;
17 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
18
19
20
21
22 @InterfaceAudience.Private
23 @InterfaceStability.Evolving
24 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="IS2_INCONSISTENT_SYNC",
25 justification="FindBugs seems confused; says globalLimiter and lastUpdate " +
26 "are mostly synchronized...but to me it looks like they are totally synchronized")
27 public class QuotaState {
28 private long lastUpdate = 0;
29 private long lastQuery = 0;
30 private QuotaLimiter globalLimiter = NoopQuotaLimiter.get();
31
32 public QuotaState() {
33 this(0);
34 }
35
36 public QuotaState(final long updateTs) {
37 lastUpdate = updateTs;
38 }
39
40 public synchronized long getLastUpdate() {
41 return lastUpdate;
42 }
43
44 public synchronized long getLastQuery() {
45 return lastQuery;
46 }
47
48 @Override
49 public synchronized String toString() {
50 StringBuilder builder = new StringBuilder();
51 builder.append("QuotaState(ts=" + getLastUpdate());
52 if (isBypass()) {
53 builder.append(" bypass");
54 } else {
55 if (globalLimiter != NoopQuotaLimiter.get()) {
56
57 builder.append(" " + globalLimiter);
58 }
59 }
60 builder.append(')');
61 return builder.toString();
62 }
63
64
65
66
67 public synchronized boolean isBypass() {
68 return globalLimiter == NoopQuotaLimiter.get();
69 }
70
71
72
73
74 public synchronized void setQuotas(final Quotas quotas) {
75 if (quotas.hasThrottle()) {
76 globalLimiter = QuotaLimiterFactory.fromThrottle(quotas.getThrottle());
77 } else {
78 globalLimiter = NoopQuotaLimiter.get();
79 }
80 }
81
82
83
84
85
86 public synchronized void update(final QuotaState other) {
87 if (globalLimiter == NoopQuotaLimiter.get()) {
88 globalLimiter = other.globalLimiter;
89 } else if (other.globalLimiter == NoopQuotaLimiter.get()) {
90 globalLimiter = NoopQuotaLimiter.get();
91 } else {
92 globalLimiter = QuotaLimiterFactory.update(globalLimiter, other.globalLimiter);
93 }
94 lastUpdate = other.lastUpdate;
95 }
96
97
98
99
100
101 public synchronized QuotaLimiter getGlobalLimiter() {
102 setLastQuery(EnvironmentEdgeManager.currentTime());
103 return globalLimiter;
104 }
105
106
107
108
109
110 synchronized QuotaLimiter getGlobalLimiterWithoutUpdatingLastQuery() {
111 return globalLimiter;
112 }
113
114 public synchronized void setLastQuery(long lastQuery) {
115 this.lastQuery = lastQuery;
116 }
117 }