Skip to content

Commit 6b50832

Browse files
committed
feat: kv-client support degradation (#373)
1 parent 79f162a commit 6b50832

File tree

1 file changed

+60
-0
lines changed
  • camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/kv

1 file changed

+60
-0
lines changed

camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/upstream/kv/kv/DecoratorKVClient.java

+60
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.netease.nim.camellia.redis.proxy.upstream.kv.kv;
22

3+
import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;
34
import com.netease.nim.camellia.redis.proxy.monitor.KvStorageMonitor;
45
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;
59

610
import java.util.Collections;
711
import java.util.List;
@@ -11,14 +15,28 @@
1115
*/
1216
public class DecoratorKVClient implements KVClient {
1317

18+
private static final Logger logger = LoggerFactory.getLogger(DecoratorKVClient.class);
19+
1420
private final String namespace;
1521
private final KVClient kvClient;
1622
private final String name;
1723

24+
private boolean degradation = false;
25+
1826
public DecoratorKVClient(String namespace, KVClient kvClient) {
1927
this.namespace = namespace;
2028
this.kvClient = kvClient;
2129
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+
}
2240
}
2341

2442
@Override
@@ -32,6 +50,9 @@ public boolean supportTTL() {
3250

3351
@Override
3452
public void put(int slot, byte[] key, byte[] value, long ttl) {
53+
if (degradation) {
54+
return;
55+
}
3556
if (ProxyMonitorCollector.isMonitorEnable()) {
3657
long start = System.nanoTime();
3758
try {
@@ -47,6 +68,9 @@ public void put(int slot, byte[] key, byte[] value, long ttl) {
4768

4869
@Override
4970
public void put(int slot, byte[] key, byte[] value) {
71+
if (degradation) {
72+
return;
73+
}
5074
if (ProxyMonitorCollector.isMonitorEnable()) {
5175
long start = System.nanoTime();
5276
try {
@@ -62,6 +86,9 @@ public void put(int slot, byte[] key, byte[] value) {
6286

6387
@Override
6488
public void batchPut(int slot, List<KeyValue> list) {
89+
if (degradation) {
90+
return;
91+
}
6592
if (list.isEmpty()) {
6693
return;
6794
}
@@ -81,6 +108,9 @@ public void batchPut(int slot, List<KeyValue> list) {
81108

82109
@Override
83110
public KeyValue get(int slot, byte[] key) {
111+
if (degradation) {
112+
return null;
113+
}
84114
if (ProxyMonitorCollector.isMonitorEnable()) {
85115
long start = System.nanoTime();
86116
try {
@@ -96,6 +126,9 @@ public KeyValue get(int slot, byte[] key) {
96126

97127
@Override
98128
public boolean exists(int slot, byte[] key) {
129+
if (degradation) {
130+
return false;
131+
}
99132
if (ProxyMonitorCollector.isMonitorEnable()) {
100133
long start = System.nanoTime();
101134
try {
@@ -111,6 +144,9 @@ public boolean exists(int slot, byte[] key) {
111144

112145
@Override
113146
public boolean[] exists(int slot, byte[]... keys) {
147+
if (degradation) {
148+
return new boolean[keys.length];
149+
}
114150
if (keys.length == 0) {
115151
return new boolean[0];
116152
}
@@ -130,6 +166,9 @@ public boolean[] exists(int slot, byte[]... keys) {
130166

131167
@Override
132168
public List<KeyValue> batchGet(int slot, byte[]... keys) {
169+
if (degradation) {
170+
return Collections.emptyList();
171+
}
133172
if (keys.length == 0) {
134173
return Collections.emptyList();
135174
}
@@ -149,6 +188,9 @@ public List<KeyValue> batchGet(int slot, byte[]... keys) {
149188

150189
@Override
151190
public void delete(int slot, byte[] key) {
191+
if (degradation) {
192+
return;
193+
}
152194
if (ProxyMonitorCollector.isMonitorEnable()) {
153195
long start = System.nanoTime();
154196
try {
@@ -164,6 +206,9 @@ public void delete(int slot, byte[] key) {
164206

165207
@Override
166208
public void batchDelete(int slot, byte[]... keys) {
209+
if (degradation) {
210+
return;
211+
}
167212
if (keys.length == 0) {
168213
return;
169214
}
@@ -188,6 +233,9 @@ public boolean supportCheckAndDelete() {
188233

189234
@Override
190235
public void checkAndDelete(int slot, byte[] key, byte[] value) {
236+
if (degradation) {
237+
return;
238+
}
191239
if (ProxyMonitorCollector.isMonitorEnable()) {
192240
long start = System.nanoTime();
193241
try {
@@ -208,6 +256,9 @@ public boolean supportReverseScan() {
208256

209257
@Override
210258
public List<KeyValue> scanByPrefix(int slot, byte[] startKey, byte[] prefix, int limit, Sort sort, boolean includeStartKey) {
259+
if (degradation) {
260+
return Collections.emptyList();
261+
}
211262
if (ProxyMonitorCollector.isMonitorEnable()) {
212263
long start = System.nanoTime();
213264
try {
@@ -223,6 +274,9 @@ public List<KeyValue> scanByPrefix(int slot, byte[] startKey, byte[] prefix, int
223274

224275
@Override
225276
public long countByPrefix(int slot, byte[] startKey, byte[] prefix, boolean includeStartKey) {
277+
if (degradation) {
278+
return 0;
279+
}
226280
if (ProxyMonitorCollector.isMonitorEnable()) {
227281
long start = System.nanoTime();
228282
try {
@@ -238,6 +292,9 @@ public long countByPrefix(int slot, byte[] startKey, byte[] prefix, boolean incl
238292

239293
@Override
240294
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+
}
241298
if (ProxyMonitorCollector.isMonitorEnable()) {
242299
long start = System.nanoTime();
243300
try {
@@ -253,6 +310,9 @@ public List<KeyValue> scanByStartEnd(int slot, byte[] startKey, byte[] endKey, b
253310

254311
@Override
255312
public long countByStartEnd(int slot, byte[] startKey, byte[] endKey, byte[] prefix, boolean includeStartKey) {
313+
if (degradation) {
314+
return 0;
315+
}
256316
if (ProxyMonitorCollector.isMonitorEnable()) {
257317
long start = System.nanoTime();
258318
try {

0 commit comments

Comments
 (0)