1
1
package com .netease .nim .camellia .redis .proxy .upstream .kv .kv ;
2
2
3
+ import com .netease .nim .camellia .redis .proxy .conf .ProxyDynamicConf ;
3
4
import com .netease .nim .camellia .redis .proxy .monitor .KvStorageMonitor ;
4
5
import com .netease .nim .camellia .redis .proxy .monitor .ProxyMonitorCollector ;
6
+ import com .netease .nim .camellia .redis .proxy .upstream .kv .conf .RedisKvConf ;
7
+ import org .slf4j .Logger ;
8
+ import org .slf4j .LoggerFactory ;
5
9
6
10
import java .util .Collections ;
7
11
import java .util .List ;
11
15
*/
12
16
public class DecoratorKVClient implements KVClient {
13
17
18
+ private static final Logger logger = LoggerFactory .getLogger (DecoratorKVClient .class );
19
+
14
20
private final String namespace ;
15
21
private final KVClient kvClient ;
16
22
private final String name ;
17
23
24
+ private boolean degradation = false ;
25
+
18
26
public DecoratorKVClient (String namespace , KVClient kvClient ) {
19
27
this .namespace = namespace ;
20
28
this .kvClient = kvClient ;
21
29
this .name = kvClient .getClass ().getSimpleName ();
30
+ updateConf ();
31
+ ProxyDynamicConf .registerCallback (this ::updateConf );
32
+ }
33
+
34
+ private void updateConf () {
35
+ boolean degradation = RedisKvConf .getBoolean (namespace , "kv.client.degradation.enable" , this .degradation );
36
+ if (degradation != this .degradation ) {
37
+ this .degradation = degradation ;
38
+ logger .info ("kv client degradation = {}, namespace = {}" , degradation , namespace );
39
+ }
22
40
}
23
41
24
42
@ Override
@@ -32,6 +50,9 @@ public boolean supportTTL() {
32
50
33
51
@ Override
34
52
public void put (int slot , byte [] key , byte [] value , long ttl ) {
53
+ if (degradation ) {
54
+ return ;
55
+ }
35
56
if (ProxyMonitorCollector .isMonitorEnable ()) {
36
57
long start = System .nanoTime ();
37
58
try {
@@ -47,6 +68,9 @@ public void put(int slot, byte[] key, byte[] value, long ttl) {
47
68
48
69
@ Override
49
70
public void put (int slot , byte [] key , byte [] value ) {
71
+ if (degradation ) {
72
+ return ;
73
+ }
50
74
if (ProxyMonitorCollector .isMonitorEnable ()) {
51
75
long start = System .nanoTime ();
52
76
try {
@@ -62,6 +86,9 @@ public void put(int slot, byte[] key, byte[] value) {
62
86
63
87
@ Override
64
88
public void batchPut (int slot , List <KeyValue > list ) {
89
+ if (degradation ) {
90
+ return ;
91
+ }
65
92
if (list .isEmpty ()) {
66
93
return ;
67
94
}
@@ -81,6 +108,9 @@ public void batchPut(int slot, List<KeyValue> list) {
81
108
82
109
@ Override
83
110
public KeyValue get (int slot , byte [] key ) {
111
+ if (degradation ) {
112
+ return null ;
113
+ }
84
114
if (ProxyMonitorCollector .isMonitorEnable ()) {
85
115
long start = System .nanoTime ();
86
116
try {
@@ -96,6 +126,9 @@ public KeyValue get(int slot, byte[] key) {
96
126
97
127
@ Override
98
128
public boolean exists (int slot , byte [] key ) {
129
+ if (degradation ) {
130
+ return false ;
131
+ }
99
132
if (ProxyMonitorCollector .isMonitorEnable ()) {
100
133
long start = System .nanoTime ();
101
134
try {
@@ -111,6 +144,9 @@ public boolean exists(int slot, byte[] key) {
111
144
112
145
@ Override
113
146
public boolean [] exists (int slot , byte []... keys ) {
147
+ if (degradation ) {
148
+ return new boolean [keys .length ];
149
+ }
114
150
if (keys .length == 0 ) {
115
151
return new boolean [0 ];
116
152
}
@@ -130,6 +166,9 @@ public boolean[] exists(int slot, byte[]... keys) {
130
166
131
167
@ Override
132
168
public List <KeyValue > batchGet (int slot , byte []... keys ) {
169
+ if (degradation ) {
170
+ return Collections .emptyList ();
171
+ }
133
172
if (keys .length == 0 ) {
134
173
return Collections .emptyList ();
135
174
}
@@ -149,6 +188,9 @@ public List<KeyValue> batchGet(int slot, byte[]... keys) {
149
188
150
189
@ Override
151
190
public void delete (int slot , byte [] key ) {
191
+ if (degradation ) {
192
+ return ;
193
+ }
152
194
if (ProxyMonitorCollector .isMonitorEnable ()) {
153
195
long start = System .nanoTime ();
154
196
try {
@@ -164,6 +206,9 @@ public void delete(int slot, byte[] key) {
164
206
165
207
@ Override
166
208
public void batchDelete (int slot , byte []... keys ) {
209
+ if (degradation ) {
210
+ return ;
211
+ }
167
212
if (keys .length == 0 ) {
168
213
return ;
169
214
}
@@ -188,6 +233,9 @@ public boolean supportCheckAndDelete() {
188
233
189
234
@ Override
190
235
public void checkAndDelete (int slot , byte [] key , byte [] value ) {
236
+ if (degradation ) {
237
+ return ;
238
+ }
191
239
if (ProxyMonitorCollector .isMonitorEnable ()) {
192
240
long start = System .nanoTime ();
193
241
try {
@@ -208,6 +256,9 @@ public boolean supportReverseScan() {
208
256
209
257
@ Override
210
258
public List <KeyValue > scanByPrefix (int slot , byte [] startKey , byte [] prefix , int limit , Sort sort , boolean includeStartKey ) {
259
+ if (degradation ) {
260
+ return Collections .emptyList ();
261
+ }
211
262
if (ProxyMonitorCollector .isMonitorEnable ()) {
212
263
long start = System .nanoTime ();
213
264
try {
@@ -223,6 +274,9 @@ public List<KeyValue> scanByPrefix(int slot, byte[] startKey, byte[] prefix, int
223
274
224
275
@ Override
225
276
public long countByPrefix (int slot , byte [] startKey , byte [] prefix , boolean includeStartKey ) {
277
+ if (degradation ) {
278
+ return 0 ;
279
+ }
226
280
if (ProxyMonitorCollector .isMonitorEnable ()) {
227
281
long start = System .nanoTime ();
228
282
try {
@@ -238,6 +292,9 @@ public long countByPrefix(int slot, byte[] startKey, byte[] prefix, boolean incl
238
292
239
293
@ Override
240
294
public List <KeyValue > scanByStartEnd (int slot , byte [] startKey , byte [] endKey , byte [] prefix , int limit , Sort sort , boolean includeStartKey ) {
295
+ if (degradation ) {
296
+ return Collections .emptyList ();
297
+ }
241
298
if (ProxyMonitorCollector .isMonitorEnable ()) {
242
299
long start = System .nanoTime ();
243
300
try {
@@ -253,6 +310,9 @@ public List<KeyValue> scanByStartEnd(int slot, byte[] startKey, byte[] endKey, b
253
310
254
311
@ Override
255
312
public long countByStartEnd (int slot , byte [] startKey , byte [] endKey , byte [] prefix , boolean includeStartKey ) {
313
+ if (degradation ) {
314
+ return 0 ;
315
+ }
256
316
if (ProxyMonitorCollector .isMonitorEnable ()) {
257
317
long start = System .nanoTime ();
258
318
try {
0 commit comments