Skip to content

Commit 1e32a2e

Browse files
author
gituser
committed
Merge branch '1.10_release_4.0.x' into 1.10_release_4.1.x
2 parents c2f57cd + 30e3058 commit 1e32a2e

File tree

7 files changed

+154
-124
lines changed

7 files changed

+154
-124
lines changed

redis5/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
<module>redis5-sink</module>
1616
<module>redis5-side</module>
1717
</modules>
18-
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.dtstack.flink</groupId>
21+
<artifactId>sql.core</artifactId>
22+
<version>1.0-SNAPSHOT</version>
23+
<scope>provided</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>redis.clients</groupId>
27+
<artifactId>jedis</artifactId>
28+
<version>2.9.0</version>
29+
</dependency>
30+
</dependencies>
1931

2032
</project>

redis5/redis5-side/pom.xml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@
1111
<modelVersion>4.0.0</modelVersion>
1212
<artifactId>sql.side.redis</artifactId>
1313
<name>redis-side</name>
14-
<dependencies>
15-
<dependency>
16-
<groupId>com.dtstack.flink</groupId>
17-
<artifactId>sql.core</artifactId>
18-
<version>1.0-SNAPSHOT</version>
19-
<scope>provided</scope>
20-
</dependency>
21-
<dependency>
22-
<groupId>redis.clients</groupId>
23-
<artifactId>jedis</artifactId>
24-
<version>2.8.0</version>
25-
</dependency>
26-
</dependencies>
27-
2814

2915
<modules>
3016
<module>redis-side-core</module>

redis5/redis5-side/redis-all-side/src/main/java/com/dtstack/flink/sql/side/redis/RedisAllReqRow.java

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.google.common.collect.Maps;
3131
import org.apache.calcite.sql.JoinType;
3232
import org.apache.commons.collections.CollectionUtils;
33-
import org.apache.commons.lang3.StringUtils;
3433
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
3534
import org.apache.flink.api.java.typeutils.RowTypeInfo;
3635
import org.apache.flink.table.dataformat.BaseRow;
@@ -48,20 +47,27 @@
4847
import java.io.Closeable;
4948
import java.io.IOException;
5049
import java.sql.SQLException;
50+
import java.util.Arrays;
5151
import java.util.Calendar;
5252
import java.util.HashSet;
5353
import java.util.List;
5454
import java.util.Map;
55+
import java.util.Objects;
5556
import java.util.Set;
5657
import java.util.TreeSet;
5758
import java.util.concurrent.atomic.AtomicReference;
59+
import java.util.regex.Matcher;
60+
import java.util.regex.Pattern;
61+
5862
/**
5963
* @author yanxi
6064
*/
6165
public class RedisAllReqRow extends BaseAllReqRow {
6266

6367
private static final long serialVersionUID = 7578879189085344807L;
6468

69+
private static final Pattern HOST_PORT_PATTERN = Pattern.compile("(?<host>(.*)):(?<port>\\d+)*");
70+
6571
private static final Logger LOG = LoggerFactory.getLogger(RedisAllReqRow.class);
6672

6773
private static final int CONN_RETRY_NUM = 3;
@@ -72,9 +78,9 @@ public class RedisAllReqRow extends BaseAllReqRow {
7278

7379
private RedisSideTableInfo tableInfo;
7480

75-
private AtomicReference<Map<String, Map<String, String>>> cacheRef = new AtomicReference<>();
81+
private final AtomicReference<Map<String, Map<String, String>>> cacheRef = new AtomicReference<>();
7682

77-
private RedisSideReqRow redisSideReqRow;
83+
private final RedisSideReqRow redisSideReqRow;
7884

7985
public RedisAllReqRow(RowTypeInfo rowTypeInfo, JoinInfo joinInfo, List<FieldInfo> outFieldInfoList, AbstractSideTableInfo sideTableInfo) {
8086
super(new RedisAllSideInfo(rowTypeInfo, joinInfo, outFieldInfoList, sideTableInfo));
@@ -112,9 +118,9 @@ protected void reloadCache() {
112118
public void flatMap(BaseRow input, Collector<BaseRow> out) throws Exception {
113119
GenericRow genericRow = (GenericRow) input;
114120
Map<String, Object> inputParams = Maps.newHashMap();
115-
for(Integer conValIndex : sideInfo.getEqualValIndex()){
121+
for (Integer conValIndex : sideInfo.getEqualValIndex()) {
116122
Object equalObj = genericRow.getField(conValIndex);
117-
if(equalObj == null){
123+
if (equalObj == null) {
118124
if (sideInfo.getJoinType() == JoinType.LEFT) {
119125
BaseRow data = fillData(input, null);
120126
RowDataComplete.collectBaseRow(out, data);
@@ -128,11 +134,11 @@ public void flatMap(BaseRow input, Collector<BaseRow> out) throws Exception {
128134

129135
Map<String, String> cacheMap = cacheRef.get().get(key);
130136

131-
if (cacheMap == null){
132-
if(sideInfo.getJoinType() == JoinType.LEFT){
137+
if (cacheMap == null) {
138+
if (sideInfo.getJoinType() == JoinType.LEFT) {
133139
BaseRow data = fillData(input, null);
134140
RowDataComplete.collectBaseRow(out, data);
135-
}else{
141+
} else {
136142
return;
137143
}
138144

@@ -147,7 +153,7 @@ private void loadData(Map<String, Map<String, String>> tmpCache) throws SQLExcep
147153
JedisCommands jedis = null;
148154
try {
149155
StringBuilder keyPattern = new StringBuilder(tableInfo.getTableName());
150-
for (String key : tableInfo.getPrimaryKeys()) {
156+
for (int i = 0; i < tableInfo.getPrimaryKeys().size(); i++) {
151157
keyPattern.append("_").append("*");
152158
}
153159
jedis = getJedisWithRetry(CONN_RETRY_NUM);
@@ -182,41 +188,54 @@ private JedisCommands getJedis(RedisSideTableInfo tableInfo) {
182188
String url = tableInfo.getUrl();
183189
String password = tableInfo.getPassword();
184190
String database = tableInfo.getDatabase() == null ? "0" : tableInfo.getDatabase();
191+
String masterName = tableInfo.getMasterName();
185192
int timeout = tableInfo.getTimeout();
186-
if (timeout == 0){
187-
timeout = 1000;
193+
if (timeout == 0) {
194+
timeout = 10000;
188195
}
189196

190-
String[] nodes = StringUtils.split(url, ",");
191-
String[] firstIpPort = StringUtils.split(nodes[0], ":");
192-
String firstIp = firstIpPort[0];
193-
String firstPort = firstIpPort[1];
194-
Set<HostAndPort> addresses = new HashSet<>();
195-
Set<String> ipPorts = new HashSet<>();
196-
for (String ipPort : nodes) {
197-
ipPorts.add(ipPort);
198-
String[] ipPortPair = ipPort.split(":");
199-
addresses.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
200-
}
201-
if (timeout == 0){
202-
timeout = 1000;
203-
}
197+
String[] nodes = url.split(",");
204198
JedisCommands jedis = null;
205199
GenericObjectPoolConfig poolConfig = setPoolConfig(tableInfo.getMaxTotal(), tableInfo.getMaxIdle(), tableInfo.getMinIdle());
206-
switch (RedisType.parse(tableInfo.getRedisType())){
207-
//单机
200+
switch (RedisType.parse(tableInfo.getRedisType())) {
208201
case STANDALONE:
209-
pool = new JedisPool(poolConfig, firstIp, Integer.parseInt(firstPort), timeout, password, Integer.parseInt(database));
210-
jedis = pool.getResource();
202+
String firstIp = null;
203+
String firstPort = null;
204+
Matcher standalone = HOST_PORT_PATTERN.matcher(nodes[0]);
205+
if (standalone.find()) {
206+
firstIp = standalone.group("host").trim();
207+
firstPort = standalone.group("port").trim();
208+
}
209+
if (Objects.nonNull(firstIp)) {
210+
pool = new JedisPool(poolConfig, firstIp, Integer.parseInt(firstPort), timeout, password, Integer.parseInt(database));
211+
jedis = pool.getResource();
212+
} else {
213+
throw new IllegalArgumentException(
214+
String.format("redis url error. current url [%s]", nodes[0]));
215+
}
211216
break;
212-
//哨兵
213217
case SENTINEL:
214-
jedisSentinelPool = new JedisSentinelPool(tableInfo.getMasterName(), ipPorts, poolConfig, timeout, password, Integer.parseInt(database));
218+
Set<String> ipPorts = new HashSet<>(Arrays.asList(nodes));
219+
jedisSentinelPool = new JedisSentinelPool(masterName, ipPorts, poolConfig, timeout, password, Integer.parseInt(database));
215220
jedis = jedisSentinelPool.getResource();
216221
break;
217-
//集群
218222
case CLUSTER:
219-
jedis = new JedisCluster(addresses, timeout, timeout,1, poolConfig);
223+
Set<HostAndPort> addresses = new HashSet<>();
224+
// 对ipv6 支持
225+
for (String node : nodes) {
226+
Matcher matcher = HOST_PORT_PATTERN.matcher(node);
227+
if (matcher.find()) {
228+
String host = matcher.group("host").trim();
229+
String portStr = matcher.group("port").trim();
230+
if (org.apache.commons.lang3.StringUtils.isNotBlank(host) && org.apache.commons.lang3.StringUtils.isNotBlank(portStr)) {
231+
// 转化为int格式的端口
232+
int port = Integer.parseInt(portStr);
233+
addresses.add(new HostAndPort(host, port));
234+
}
235+
}
236+
}
237+
jedis = new JedisCluster(addresses, timeout, timeout, 10, password, poolConfig);
238+
break;
220239
default:
221240
break;
222241
}
@@ -244,35 +263,32 @@ private JedisCommands getJedisWithRetry(int retryNum) {
244263
return null;
245264
}
246265

247-
private Set<String> getRedisKeys(RedisType redisType, JedisCommands jedis, String keyPattern){
248-
if(!redisType.equals(RedisType.CLUSTER)){
266+
private Set<String> getRedisKeys(RedisType redisType, JedisCommands jedis, String keyPattern) {
267+
if (!redisType.equals(RedisType.CLUSTER)) {
249268
return ((Jedis) jedis).keys(keyPattern);
250269
}
251270
Set<String> keys = new TreeSet<>();
252-
Map<String, JedisPool> clusterNodes = ((JedisCluster)jedis).getClusterNodes();
253-
for(String k : clusterNodes.keySet()){
271+
Map<String, JedisPool> clusterNodes = ((JedisCluster) jedis).getClusterNodes();
272+
for (String k : clusterNodes.keySet()) {
254273
JedisPool jp = clusterNodes.get(k);
255-
Jedis connection = jp.getResource();
256-
try {
274+
try (Jedis connection = jp.getResource()) {
257275
keys.addAll(connection.keys(keyPattern));
258-
} catch (Exception e){
259-
LOG.error("Getting keys error: {}", e);
260-
} finally {
261-
connection.close();
276+
} catch (Exception e) {
277+
LOG.error("Getting keys error", e);
262278
}
263279
}
264280
return keys;
265281
}
266282

267-
private GenericObjectPoolConfig setPoolConfig(String maxTotal, String maxIdle, String minIdle){
283+
private GenericObjectPoolConfig setPoolConfig(String maxTotal, String maxIdle, String minIdle) {
268284
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
269-
if (maxTotal != null){
285+
if (maxTotal != null) {
270286
config.setMaxTotal(Integer.parseInt(maxTotal));
271287
}
272-
if (maxIdle != null){
288+
if (maxIdle != null) {
273289
config.setMaxIdle(Integer.parseInt(maxIdle));
274290
}
275-
if (minIdle != null){
291+
if (minIdle != null) {
276292
config.setMinIdle(Integer.parseInt(minIdle));
277293
}
278294
return config;

redis5/redis5-side/redis-async-side/src/main/java/com/dtstack/flink/sql/side/redis/RedisAsyncReqRow.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class RedisAsyncReqRow extends BaseAsyncReqRow {
6969

7070
private RedisSideTableInfo redisSideTableInfo;
7171

72-
private RedisSideReqRow redisSideReqRow;
72+
private final RedisSideReqRow redisSideReqRow;
7373

7474
public RedisAsyncReqRow(RowTypeInfo rowTypeInfo, JoinInfo joinInfo, List<FieldInfo> outFieldInfoList, AbstractSideTableInfo sideTableInfo) {
7575
super(new RedisAsyncSideInfo(rowTypeInfo, joinInfo, outFieldInfoList, sideTableInfo));
@@ -95,7 +95,7 @@ private void buildRedisClient(RedisSideTableInfo tableInfo){
9595
case STANDALONE:
9696
RedisURI redisURI = RedisURI.create("redis://" + url);
9797
redisURI.setPassword(password);
98-
redisURI.setDatabase(Integer.valueOf(database));
98+
redisURI.setDatabase(Integer.parseInt(database));
9999
redisClient = RedisClient.create(redisURI);
100100
connection = redisClient.connect();
101101
async = connection.async();
@@ -167,21 +167,18 @@ public void handleAsyncInvoke(Map<String, Object> inputParams, BaseRow input, Re
167167
return;
168168
}
169169
RedisFuture<Map<String, String>> future = ((RedisHashAsyncCommands) async).hgetall(key);
170-
future.thenAccept(new Consumer<Map<String, String>>() {
171-
@Override
172-
public void accept(Map<String, String> values) {
173-
if (MapUtils.isNotEmpty(values)) {
174-
try {
175-
BaseRow row = fillData(input, values);
176-
dealCacheData(key,CacheObj.buildCacheObj(ECacheContentType.SingleLine, row));
177-
RowDataComplete.completeBaseRow(resultFuture, row);
178-
} catch (Exception e) {
179-
dealFillDataError(input, resultFuture, e);
180-
}
181-
} else {
182-
dealMissKey(input, resultFuture);
183-
dealCacheData(key, CacheMissVal.getMissKeyObj());
170+
future.thenAccept(values -> {
171+
if (MapUtils.isNotEmpty(values)) {
172+
try {
173+
BaseRow row = fillData(input, values);
174+
dealCacheData(key,CacheObj.buildCacheObj(ECacheContentType.SingleLine, row));
175+
RowDataComplete.completeBaseRow(resultFuture, row);
176+
} catch (Exception e) {
177+
dealFillDataError(input, resultFuture, e);
184178
}
179+
} else {
180+
dealMissKey(input, resultFuture);
181+
dealCacheData(key, CacheMissVal.getMissKeyObj());
185182
}
186183
});
187184
}

redis5/redis5-side/redis-side-core/pom.xml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@
1212
</parent>
1313

1414
<artifactId>sql.side.redis.core</artifactId>
15-
<dependencies>
16-
<dependency>
17-
<groupId>com.dtstack.flink</groupId>
18-
<artifactId>sql.core</artifactId>
19-
<version>1.0-SNAPSHOT</version>
20-
<scope>provided</scope>
21-
</dependency>
22-
</dependencies>
15+
2316
<packaging>jar</packaging>
2417

2518

redis5/redis5-sink/pom.xml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616
<name>redis-sink</name>
1717
<url>http://maven.apache.org</url>
1818

19-
<dependencies>
20-
<dependency>
21-
<groupId>com.dtstack.flink</groupId>
22-
<artifactId>sql.core</artifactId>
23-
<version>1.0-SNAPSHOT</version>
24-
<scope>provided</scope>
25-
</dependency>
26-
<dependency>
27-
<groupId>redis.clients</groupId>
28-
<artifactId>jedis</artifactId>
29-
<version>2.9.0</version>
30-
</dependency>
31-
</dependencies>
32-
3319
<build>
3420
<plugins>
3521
<plugin>

0 commit comments

Comments
 (0)