Skip to content

Commit

Permalink
optimize UpstreamAddrConverter (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Oct 23, 2023
1 parent 724f430 commit c88297d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,25 @@ public UpstreamAddrConverterResult convert(UpstreamAddrConverterContext context)
String udsPath = context.getUdsPath();
for (Config config : configList) {
if (host != null && config.getOriginalHost() != null) {
if (config.getOriginalHost().equals(CURRENT_HOST)) {
if (host.equals(currentHost)) {
return result(context, config);
if (config.getOriginalPort() <= 0) {
if (config.getOriginalHost().equals(CURRENT_HOST)) {
if (host.equals(currentHost)) {
return result(context, config);
}
} else {
if (host.equals(config.getOriginalHost())) {
return result(context, config);
}
}
} else {
if (host.equals(config.getOriginalHost())) {
return result(context, config);
if (config.getOriginalHost().equals(CURRENT_HOST) && config.getOriginalPort() == context.getPort()) {
if (host.equals(currentHost)) {
return result(context, config);
}
} else {
if (host.equals(config.getOriginalHost()) && config.getOriginalPort() == context.getPort()) {
return result(context, config);
}
}
}
}
Expand All @@ -79,7 +91,7 @@ public UpstreamAddrConverterResult convert(UpstreamAddrConverterContext context)
private UpstreamAddrConverterResult result(UpstreamAddrConverterContext context, Config config) {
if (config.getTargetHost() != null) {
if (SocketChannel.class.isAssignableFrom(context.getChannelClass())) {
return new UpstreamAddrConverterResult(config.getTargetHost(), null, context.getChannelClass(), ChannelType.tcp);
return new UpstreamAddrConverterResult(config.getTargetHost(), config.getTargetPort(), null, context.getChannelClass(), ChannelType.tcp);
}
Class<? extends Channel> socketChannel = null;
if (context.getEventLoop().parent() instanceof EpollEventLoopGroup) {
Expand All @@ -92,14 +104,14 @@ private UpstreamAddrConverterResult result(UpstreamAddrConverterContext context,
socketChannel = NioSocketChannel.class;
}
if (socketChannel != null) {
return new UpstreamAddrConverterResult(config.getTargetHost(), null, socketChannel, ChannelType.tcp);
return new UpstreamAddrConverterResult(config.getTargetHost(), config.getTargetPort(), null, socketChannel, ChannelType.tcp);
}
}
if (config.getTargetUdsPath() != null) {
if (context.getEventLoop().parent() instanceof EpollEventLoopGroup) {
return new UpstreamAddrConverterResult(null, config.getTargetUdsPath(), EpollDomainSocketChannel.class, ChannelType.uds);
return new UpstreamAddrConverterResult(null, -1, config.getTargetUdsPath(), EpollDomainSocketChannel.class, ChannelType.uds);
} else if (context.getEventLoop().parent() instanceof KQueueEventLoopGroup) {
return new UpstreamAddrConverterResult(null, config.getTargetUdsPath(), KQueueDomainSocketChannel.class, ChannelType.uds);
return new UpstreamAddrConverterResult(null, -1, config.getTargetUdsPath(), KQueueDomainSocketChannel.class, ChannelType.uds);
}
}
return null;
Expand Down Expand Up @@ -128,8 +140,10 @@ private void reloadConfig() {

private static class Config {
private String originalHost;
private int originalPort;
private String originalUdsPath;
private String targetHost;
private int targetPort;
private String targetUdsPath;

public String getOriginalHost() {
Expand All @@ -140,6 +154,14 @@ public void setOriginalHost(String originalHost) {
this.originalHost = originalHost;
}

public int getOriginalPort() {
return originalPort;
}

public void setOriginalPort(int originalPort) {
this.originalPort = originalPort;
}

public String getOriginalUdsPath() {
return originalUdsPath;
}
Expand All @@ -156,6 +178,14 @@ public void setTargetHost(String targetHost) {
this.targetHost = targetHost;
}

public int getTargetPort() {
return targetPort;
}

public void setTargetPort(int targetPort) {
this.targetPort = targetPort;
}

public String getTargetUdsPath() {
return targetUdsPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,26 @@ public void start() {
return;
}
String host = this.host;
int port = this.port;
String udsPath = this.udsPath;
UpstreamAddrConverter upstreamHostConverter = config.getUpstreamHostConverter();
if (upstreamHostConverter != null) {
UpstreamAddrConverter.UpstreamAddrConverterContext context = new UpstreamAddrConverter.UpstreamAddrConverterContext(host, udsPath, eventLoop, channelClass);
UpstreamAddrConverter.UpstreamAddrConverterContext context = new UpstreamAddrConverter.UpstreamAddrConverterContext(host, port, udsPath, eventLoop, channelClass);
UpstreamAddrConverter.UpstreamAddrConverterResult result = upstreamHostConverter.convert(context);
if (result != null) {
logger.info("upstream addr convert, old.host = {}, new.host = {}, old.udsPath = {}, new.udsPath = {}, old.socketChannel = {}, new.socketChannel = {}, old.channelType = {}, new.channelType = {}",
host, result.getHost(), udsPath, result.getUdsPath(), channelClass.getSimpleName(),
logger.info("upstream addr convert, old.host = {}, new.host = {}, old.port = {}, new.port = {}," +
" old.udsPath = {}, new.udsPath = {}, old.socketChannel = {}, new.socketChannel = {}, old.channelType = {}, new.channelType = {}",
host, result.getHost(), udsPath, result.getUdsPath(), port, result.getPort(), channelClass.getSimpleName(),
result.getChannelClass().getSimpleName(), channelType, result.getChannelType());
host = result.getHost();
udsPath = result.getUdsPath();
if (result.getHost() != null) {
host = result.getHost();
}
if (result.getPort() > 0) {
port = result.getPort();
}
if (result.getUdsPath() != null) {
udsPath = result.getUdsPath();
}
channelClass = result.getChannelClass();
channelType = result.getChannelType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ default UpstreamAddrConverterResult convert(UpstreamAddrConverterContext context

public static class UpstreamAddrConverterResult {
private final String host;
private final int port;
private final String udsPath;
private final Class<? extends Channel> channelClass;
private final ChannelType channelType;

public UpstreamAddrConverterResult(String host, String udsPath, Class<? extends Channel> socketChannel, ChannelType channelType) {
public UpstreamAddrConverterResult(String host, int port, String udsPath, Class<? extends Channel> socketChannel, ChannelType channelType) {
this.host = host;
this.port = port;
this.udsPath = udsPath;
this.channelClass = socketChannel;
this.channelType = channelType;
Expand All @@ -30,6 +32,10 @@ public String getHost() {
return host;
}

public int getPort() {
return port;
}

public String getUdsPath() {
return udsPath;
}
Expand All @@ -45,12 +51,14 @@ public ChannelType getChannelType() {

public static class UpstreamAddrConverterContext {
private final String host;
private final int port;
private final String udsPath;
private final EventLoop eventLoop;
private final Class<? extends Channel> channelClass;

public UpstreamAddrConverterContext(String host, String udsPath, EventLoop eventLoop, Class<? extends Channel> channelClass) {
public UpstreamAddrConverterContext(String host, int port, String udsPath, EventLoop eventLoop, Class<? extends Channel> channelClass) {
this.host = host;
this.port = port;
this.udsPath = udsPath;
this.eventLoop = eventLoop;
this.channelClass = channelClass;
Expand All @@ -60,6 +68,10 @@ public String getHost() {
return host;
}

public int getPort() {
return port;
}

public String getUdsPath() {
return udsPath;
}
Expand Down
32 changes: 31 additions & 1 deletion docs/redis-proxy/other/upstream-addr-converter.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,38 @@ camellia-redis-proxy:
```properties
upstream.addr.converter.enable=true
upstream.addr.converter.config=[{"originalHost": "@CurrentHost@", "targetUdsPath": "/Users/caojiajun/temp/redis.sock"}]
upstream.addr.converter.config=[{"originalHost": "@CurrentHost@", "originalPort": 6601, "targetUdsPath": "/Users/caojiajun/temp/redis.sock"}]
```
假设proxy部署在10.189.31.13这个节点上,则proxy访问本机的redis-server会走uds,而不是走10.189.31.13
#### 允许的配置字段
```java
private static class Config {
private String originalHost;
private int originalPort;
private String originalUdsPath;
private String targetHost;
private int targetPort;
private String targetUdsPath;
}
```

匹配:
* 如果originalPort大于0,先匹配originalHost+originalPort
* 如果originalPort缺失或者小于等于0,则只匹配originalHost
* 如果originalHost为空,则匹配originalUdsPath

备注:如果originalHost为特殊的@CurrentHost@字符串,则会检查originalHost会使用本机ip去匹配
本机ip默认自动获取,如果要手动指定,则可以配置:
```properties
current.proxy.host=10.189.31.13
```

返回:
* 如果targetHost不为空,targetPort大于等于0,则替换为targetHost+targetPort
* 如果targetHost不为空,targetPort缺失或者小于等于0,则替换为targetHost+originalPort
* 如果targetHost为空,则替换为targetUdsPath
* 否则,保持不变

0 comments on commit c88297d

Please sign in to comment.