Skip to content

Commit

Permalink
feat(proxy): unify className config key (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Sep 18, 2024
1 parent 64e5e5b commit c3ea93d
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.netease.nim.camellia.redis.proxy.reply.StatusReply;
import com.netease.nim.camellia.redis.proxy.upstream.connection.RedisConnection;
import com.netease.nim.camellia.redis.proxy.upstream.connection.RedisConnectionHub;
import com.netease.nim.camellia.redis.proxy.util.BeanInitUtils;
import com.netease.nim.camellia.redis.proxy.util.ConcurrentHashSet;
import com.netease.nim.camellia.redis.proxy.util.ConfigInitUtil;
import com.netease.nim.camellia.redis.proxy.util.Utils;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void init() {
initNodes(false);
current();
//init leader selector
String className = ProxyDynamicConf.getString("cluster.mode.consensus.leader.selector.class.name", RedisConsensusLeaderSelector.class.getName());
String className = BeanInitUtils.getClassName("cluster.mode.consensus.leader.selector", RedisConsensusLeaderSelector.class.getName());
leaderSelector = ConfigInitUtil.initConsensusLeaderSelector(className);
leaderSelector.init(current(), new ArrayList<>(initNodes(false)));
//get leader
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.netease.nim.camellia.redis.proxy.plugin.bigkey;

import com.netease.nim.camellia.redis.proxy.command.Command;
import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;
import com.netease.nim.camellia.redis.proxy.monitor.ProxyMonitorCollector;
import com.netease.nim.camellia.redis.proxy.plugin.*;
import com.netease.nim.camellia.redis.proxy.util.BeanInitUtils;
Expand All @@ -15,7 +14,7 @@ public class BigKeyProxyPlugin implements ProxyPlugin {

@Override
public void init(ProxyBeanFactory factory) {
String callbackClassName = ProxyDynamicConf.getString("big.key.monitor.callback.className", DummyBigKeyMonitorCallback.class.getName());
String callbackClassName = BeanInitUtils.getClassName("big.key.monitor.callback", DummyBigKeyMonitorCallback.class.getName());
Class<?> clazz = BeanInitUtils.parseClass(callbackClassName);
BigKeyMonitorCallback callback = (BigKeyMonitorCallback) factory.getBean(clazz);
bigKeyHunter = new BigKeyHunter(callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.netease.nim.camellia.redis.proxy.command.Command;
import com.netease.nim.camellia.redis.proxy.command.CommandContext;
import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;
import com.netease.nim.camellia.redis.proxy.enums.RedisCommand;
import com.netease.nim.camellia.redis.proxy.plugin.*;
import com.netease.nim.camellia.redis.proxy.upstream.utils.PubSubUtils;
Expand All @@ -18,32 +17,32 @@ public class ConverterProxyPlugin implements ProxyPlugin {
@Override
public void init(ProxyBeanFactory factory) {
ConverterConfig converterConfig = new ConverterConfig();
String keyConverterClassName = ProxyDynamicConf.getString("converter.key.className", null);
String keyConverterClassName = BeanInitUtils.getClassName("converter.key", null);
if (keyConverterClassName != null) {
KeyConverter keyConverter = (KeyConverter)factory.getBean(BeanInitUtils.parseClass(keyConverterClassName));
converterConfig.setKeyConverter(keyConverter);
}
String stringConverterClassName = ProxyDynamicConf.getString("converter.string.className", null);
String stringConverterClassName = BeanInitUtils.getClassName("converter.string", null);
if (stringConverterClassName != null) {
StringConverter stringConverter = (StringConverter)factory.getBean(BeanInitUtils.parseClass(stringConverterClassName));
converterConfig.setStringConverter(stringConverter);
}
String hashConverterClassName = ProxyDynamicConf.getString("converter.hash.className", null);
String hashConverterClassName = BeanInitUtils.getClassName("converter.hash", null);
if (hashConverterClassName != null) {
HashConverter hashConverter = (HashConverter)factory.getBean(BeanInitUtils.parseClass(hashConverterClassName));
converterConfig.setHashConverter(hashConverter);
}
String setConverterClassName = ProxyDynamicConf.getString("converter.set.className", null);
String setConverterClassName = BeanInitUtils.getClassName("converter.set", null);
if (setConverterClassName != null) {
SetConverter setConverter = (SetConverter)factory.getBean(BeanInitUtils.parseClass(setConverterClassName));
converterConfig.setSetConverter(setConverter);
}
String zsetConverterClassName = ProxyDynamicConf.getString("converter.zset.className", null);
String zsetConverterClassName = BeanInitUtils.getClassName("converter.zset", null);
if (zsetConverterClassName != null) {
ZSetConverter zsetConverter = (ZSetConverter)factory.getBean(BeanInitUtils.parseClass(zsetConverterClassName));
converterConfig.setzSetConverter(zsetConverter);
}
String listConverterClassName = ProxyDynamicConf.getString("converter.list.className", null);
String listConverterClassName = BeanInitUtils.getClassName("converter.list", null);
if (listConverterClassName != null) {
ListConverter listConverter = (ListConverter)factory.getBean(BeanInitUtils.parseClass(listConverterClassName));
converterConfig.setListConverter(listConverter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;
import com.netease.nim.camellia.redis.proxy.monitor.ProxyMonitorCollector;
import com.netease.nim.camellia.redis.proxy.plugin.*;
import com.netease.nim.camellia.redis.proxy.plugin.bigkey.DummyBigKeyMonitorCallback;
import com.netease.nim.camellia.redis.proxy.util.BeanInitUtils;

import java.util.List;
Expand All @@ -18,7 +19,7 @@ public class HotKeyProxyPlugin implements ProxyPlugin {

@Override
public void init(ProxyBeanFactory factory) {
String callbackClassName = ProxyDynamicConf.getString("hot.key.monitor.callback.className", DummyHotKeyMonitorCallback.class.getName());
String callbackClassName = BeanInitUtils.getClassName("hot.key.monitor.callback", DummyHotKeyMonitorCallback.class.getName());
Class<?> clazz = BeanInitUtils.parseClass(callbackClassName);
HotKeyMonitorCallback callback = (HotKeyMonitorCallback) factory.getBean(clazz);
manager = new HotKeyHunterManager(callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class HotKeyRouteRewriteProxyPlugin implements ProxyPlugin {

@Override
public void init(ProxyBeanFactory factory) {
String rewriteCheckerClassName = ProxyDynamicConf.getString("hot.key.route.rewriter.className", DefaultRouteRewriter.class.getName());
String rewriteCheckerClassName = BeanInitUtils.getClassName("hot.key.route.rewriter", DefaultRouteRewriter.class.getName());
RouteRewriter routeRewriter = (RouteRewriter) factory.getBean(BeanInitUtils.parseClass(rewriteCheckerClassName));

String callbackClassName = ProxyDynamicConf.getString("hot.key.monitor.callback.className", DummyHotKeyMonitorCallback.class.getName());
String callbackClassName = BeanInitUtils.getClassName("hot.key.monitor.callback", DummyHotKeyMonitorCallback.class.getName());
HotKeyMonitorCallback monitorCallback = (HotKeyMonitorCallback) factory.getBean(BeanInitUtils.parseClass(callbackClassName));

this.rewriter = new HotKeyRouteRewriter(monitorCallback, routeRewriter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.netease.nim.camellia.redis.proxy.command.Command;
import com.netease.nim.camellia.redis.proxy.command.CommandContext;
import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;
import com.netease.nim.camellia.redis.proxy.enums.RedisCommand;
import com.netease.nim.camellia.redis.proxy.plugin.*;
import com.netease.nim.camellia.redis.proxy.reply.BulkReply;
Expand All @@ -21,11 +20,11 @@ public void init(ProxyBeanFactory factory) {
HotKeyCacheConfig hotKeyCacheConfig = new HotKeyCacheConfig();

// Set hotKeyChecker and the default is PrefixMatchHotKeyCacheKeyChecker. Also, you can implement HotKeyCacheKeyChecker to customize a checker.
String hotKeyCacheKeyCheckerClassName = ProxyDynamicConf.getString("hot.key.cache.key.checker.className", PrefixMatchHotKeyCacheKeyChecker.class.getName());
String hotKeyCacheKeyCheckerClassName = BeanInitUtils.getClassName("hot.key.cache.key.checker", PrefixMatchHotKeyCacheKeyChecker.class.getName());
HotKeyCacheKeyChecker hotKeyCacheKeyChecker = (HotKeyCacheKeyChecker) factory.getBean(BeanInitUtils.parseClass(hotKeyCacheKeyCheckerClassName));
hotKeyCacheConfig.setHotKeyCacheKeyChecker(hotKeyCacheKeyChecker);

String hotKeyCacheStatsCallbackClassName = ProxyDynamicConf.getString("hot.key.cache.stats.callback.className", DummyHotKeyCacheStatsCallback.class.getName());
String hotKeyCacheStatsCallbackClassName = BeanInitUtils.getClassName("hot.key.cache.stats.callback", DummyHotKeyCacheStatsCallback.class.getName());
HotKeyCacheStatsCallback hotKeyCacheStatsCallback = (HotKeyCacheStatsCallback) factory.getBean(BeanInitUtils.parseClass(hotKeyCacheStatsCallbackClassName));
hotKeyCacheConfig.setHotKeyCacheStatsCallback(hotKeyCacheStatsCallback);
manager = new HotKeyCacheManager(hotKeyCacheConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class MultiWriteProxyPlugin implements ProxyPlugin {

@Override
public void init(ProxyBeanFactory factory) {
String multiWriteFuncClassName = ProxyDynamicConf.getString("multi.write.func.class.name", KeyPrefixMultiWriteFunc.class.getName());
String multiWriteFuncClassName = BeanInitUtils.getClassName("multi.write.func", KeyPrefixMultiWriteFunc.class.getName());
this.multiWriteFunc = (MultiWriteFunc) factory.getBean(BeanInitUtils.parseClass(multiWriteFuncClassName));
this.skipDb = ProxyDynamicConf.getBoolean("multi.write.plugin.skip.db.enable", false);
int poolSize = ProxyDynamicConf.getInt("multi.write.executor.pool.size", Runtime.getRuntime().availableProcessors());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void init(ProxyBeanFactory factory) {
reloadConf();
ProxyDynamicConf.registerCallback(this::reloadConf);

String slowCommandMonitorCallbackClassName = ProxyDynamicConf.getString("slow.command.monitor.callback.className", DummySlowCommandMonitorCallback.class.getName());
String slowCommandMonitorCallbackClassName = BeanInitUtils.getClassName("slow.command.monitor.callback", DummySlowCommandMonitorCallback.class.getName());
slowCommandMonitorCallback = (SlowCommandMonitorCallback) factory.getBean(BeanInitUtils.parseClass(slowCommandMonitorCallbackClassName));
CALLBACK_NAME = slowCommandMonitorCallback.getClass().getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.netease.nim.camellia.redis.proxy.upstream.connection.RedisConnection;
import com.netease.nim.camellia.redis.proxy.upstream.connection.RedisConnectionHub;
import com.netease.nim.camellia.redis.proxy.upstream.sentinel.RedisSentinelUtils;
import com.netease.nim.camellia.redis.proxy.util.BeanInitUtils;
import com.netease.nim.camellia.redis.proxy.util.ConfigInitUtil;
import com.netease.nim.camellia.redis.proxy.util.ErrorLogCollector;
import com.netease.nim.camellia.redis.proxy.util.Utils;
Expand Down Expand Up @@ -77,7 +78,7 @@ private void init() {
this.currentNode = proxyNode;
this.sentinelUserName = ProxyDynamicConf.getString("proxy.sentinel.mode.sentinel.username", null);
this.sentinelPassword = ProxyDynamicConf.getString("proxy.sentinel.mode.sentinel.password", null);
String className = ProxyDynamicConf.getString("proxy.sentinel.mode.nodes.provider.class.name", DefaultProxySentinelModeNodesProvider.class.getName());
String className = BeanInitUtils.getClassName("proxy.sentinel.mode.nodes.provider", DefaultProxySentinelModeNodesProvider.class.getName());
this.provider = ConfigInitUtil.initSentinelModeNodesProvider(className);
this.provider.init(currentNode);
//online nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private void sendMultiKeyCommand(RedisCommand redisCommand, Command command, Com
}

private KVClient initKVClient() {
String className = RedisKvConf.getString(namespace, "kv.client.class.name", null);
String className = RedisKvConf.getClassName(namespace, "kv.client", null);
return (KVClient) GlobalRedisProxyEnv.getProxyBeanFactory().getBean(BeanInitUtils.parseClass(className));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.netease.nim.camellia.redis.proxy.upstream.kv.conf;

import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;
import com.netease.nim.camellia.redis.proxy.util.BeanInitUtils;

/**
* Created by caojiajun on 2024/4/17
Expand All @@ -22,4 +23,15 @@ public static String getString(String namespace, String key, String defaultValue
public static boolean getBoolean(String namespace, String key, boolean defaultValue) {
return ProxyDynamicConf.getBoolean(namespace + "." + key, ProxyDynamicConf.getBoolean(key, defaultValue));
}

public static String getClassName(String namespace, String key, String defaultClassName) {
String className = BeanInitUtils.getClassName(namespace + "." + key, null);
if (className == null) {
className = BeanInitUtils.getClassName(key, null);
}
if (className == null) {
return defaultClassName;
}
return className;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.netease.nim.camellia.redis.proxy.util;

import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;

/**
* Created by caojiajun on 2022/9/16
*/
Expand All @@ -19,4 +21,15 @@ public static Class<?> parseClass(String className) {
}
}

public static String getClassName(String configPrefix, String defaultClassName) {
String className = ProxyDynamicConf.getString(configPrefix + ".className", null);
if (className == null) {
className = ProxyDynamicConf.getString(configPrefix + ".class.name", null);
}
if (className == null) {
return defaultClassName;
}
return className;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static ConsensusLeaderSelector initConsensusLeaderSelector(String classNa

public static ProxyNodesDiscovery initProxyNodesDiscovery(CamelliaServerProperties serverProperties,
ProxyClusterModeProcessor proxyClusterModeProcessor, ProxySentinelModeProcessor proxySentinelModeProcessor) {
String className = ProxyDynamicConf.getString("proxy.nodes.discovery.className", DefaultProxyNodesDiscovery.class.getName());
String className = BeanInitUtils.getClassName("proxy.nodes.discovery", DefaultProxyNodesDiscovery.class.getName());
if (className.equals(DefaultProxyNodesDiscovery.class.getName())) {
return new DefaultProxyNodesDiscovery(proxyClusterModeProcessor, proxySentinelModeProcessor);
} else if (className.equals(RedisProxyNodesDiscovery.class.getName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.netease.nim.camellia.redis.proxy.command.Command;
import com.netease.nim.camellia.redis.proxy.command.CommandContext;
import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;
import com.netease.nim.camellia.redis.proxy.enums.RedisCommand;
import com.netease.nim.camellia.redis.proxy.hotkey.common.ProxyHotKeyServerDiscoveryFactory;
import com.netease.nim.camellia.redis.proxy.hotkey.common.ProxyLocalHotKeyServerDiscoveryFactory;
Expand All @@ -24,12 +23,12 @@ public class HotKeyCachePlugin implements ProxyPlugin {
@Override
public void init(ProxyBeanFactory factory) {
// 默认使用本地sever
String hotKeyCacheDiscoveryClassName = ProxyDynamicConf.getString("hot.key.server.discovery.className", ProxyLocalHotKeyServerDiscoveryFactory.class.getName());
String hotKeyCacheDiscoveryClassName = BeanInitUtils.getClassName("hot.key.server.discovery", ProxyLocalHotKeyServerDiscoveryFactory.class.getName());
ProxyHotKeyServerDiscoveryFactory discoveryFactory = (ProxyHotKeyServerDiscoveryFactory) factory.getBean(BeanInitUtils.parseClass(hotKeyCacheDiscoveryClassName));
HotKeyCacheConfig hotKeyCacheConfig = new HotKeyCacheConfig();
hotKeyCacheConfig.setDiscovery(discoveryFactory.getDiscovery());

String hotKeyCacheKeyCheckerClassName = ProxyDynamicConf.getString("hot.key.cache.key.checker.className", PrefixMatchHotKeyCacheKeyChecker.class.getName());
String hotKeyCacheKeyCheckerClassName = BeanInitUtils.getClassName("hot.key.cache.key.checker", PrefixMatchHotKeyCacheKeyChecker.class.getName());
HotKeyCacheKeyChecker hotKeyCacheKeyChecker = (HotKeyCacheKeyChecker) factory.getBean(BeanInitUtils.parseClass(hotKeyCacheKeyCheckerClassName));
hotKeyCacheConfig.setHotKeyCacheKeyChecker(hotKeyCacheKeyChecker);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.netease.nim.camellia.redis.proxy.command.Command;
import com.netease.nim.camellia.redis.proxy.command.CommandContext;
import com.netease.nim.camellia.redis.proxy.conf.ProxyDynamicConf;
import com.netease.nim.camellia.redis.proxy.hotkey.common.ProxyHotKeyServerDiscoveryFactory;
import com.netease.nim.camellia.redis.proxy.hotkey.common.ProxyLocalHotKeyServerDiscoveryFactory;
import com.netease.nim.camellia.redis.proxy.hotkey.common.ProxyHotKeyUtils;
Expand All @@ -19,7 +18,7 @@ public class HotKeyMonitorPlugin implements ProxyPlugin {
@Override
public void init(ProxyBeanFactory factory) {
// 默认使用本地sever
String hotKeyDiscoveryClassName = ProxyDynamicConf.getString("hot.key.server.discovery.className", ProxyLocalHotKeyServerDiscoveryFactory.class.getName());
String hotKeyDiscoveryClassName = BeanInitUtils.getClassName("hot.key.server.discovery", ProxyLocalHotKeyServerDiscoveryFactory.class.getName());
ProxyHotKeyServerDiscoveryFactory discoveryFactory = (ProxyHotKeyServerDiscoveryFactory) factory.getBean(BeanInitUtils.parseClass(hotKeyDiscoveryClassName));
HotKeyMonitorConfig hotKeyMonitorConfig = new HotKeyMonitorConfig();
hotKeyMonitorConfig.setDiscovery(discoveryFactory.getDiscovery());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MqMultiWriteProducerProxyPlugin implements ProxyPlugin {

@Override
public void init(ProxyBeanFactory factory) {
String className = ProxyDynamicConf.getString("mq.multi.write.sender.className", null);
String className = BeanInitUtils.getClassName("mq.multi.write.sender", null);
this.mqSender = (MqPackSender) factory.getBean(BeanInitUtils.parseClass(className));
logger.info("mqSender init success, className = {}", className);

Expand Down

0 comments on commit c3ea93d

Please sign in to comment.