Unverified Commit 1d929f94 authored by Licho's avatar Licho Committed by GitHub

feat: improve WatchTableListener logic for failed initial, and fix no… (#1937)

* feat: improve WatchTableListener logic for failed initial, and fix not-all default selected. * Spotless Apply * feat: watch failed break Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> --------- Signed-off-by: 's avatarLicho <lecho.sun@gmail.com> Co-authored-by: 's avatarleechor <leechor@users.noreply.github.com>
parent 58a2aca4
...@@ -24,7 +24,7 @@ import org.dinky.service.WatchTableService; ...@@ -24,7 +24,7 @@ import org.dinky.service.WatchTableService;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.SocketException; import java.net.SocketException;
import java.util.Arrays; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
...@@ -47,7 +47,7 @@ public class WatchTableServiceImpl implements WatchTableService { ...@@ -47,7 +47,7 @@ public class WatchTableServiceImpl implements WatchTableService {
private final Map<String, Set<String>> registerTableMap = new ConcurrentHashMap<>(); private final Map<String, Set<String>> registerTableMap = new ConcurrentHashMap<>();
private static final Pattern FULL_TABLE_NAME_PATTERN = private static final Pattern FULL_TABLE_NAME_PATTERN =
Pattern.compile("^`([^\\.]+)`\\.`([^\\.]+)`\\.`([^\\.]+)`$"); Pattern.compile("^`(\\w+)`\\.`(\\w+)`\\.`(\\w+)`$");
public WatchTableServiceImpl(SimpMessagingTemplate messagingTemplate) { public WatchTableServiceImpl(SimpMessagingTemplate messagingTemplate) {
this.messagingTemplate = messagingTemplate; this.messagingTemplate = messagingTemplate;
...@@ -73,7 +73,7 @@ public class WatchTableServiceImpl implements WatchTableService { ...@@ -73,7 +73,7 @@ public class WatchTableServiceImpl implements WatchTableService {
String destination = getDestinationByFullName(userId, fullName); String destination = getDestinationByFullName(userId, fullName);
Set<String> destinations = registerTableMap.get(fullName); Set<String> destinations = registerTableMap.get(fullName);
if (destinations == null) { if (destinations == null) {
registerTableMap.put(fullName, new HashSet<>(Arrays.asList(destination))); registerTableMap.put(fullName, new HashSet<>(Collections.singletonList(destination)));
} else { } else {
destinations.add(destination); destinations.add(destination);
} }
...@@ -120,20 +120,33 @@ public class WatchTableServiceImpl implements WatchTableService { ...@@ -120,20 +120,33 @@ public class WatchTableServiceImpl implements WatchTableService {
public static final int PORT = 7125; public static final int PORT = 7125;
private DatagramSocket socket; private DatagramSocket socket;
private boolean running; private boolean running;
private byte[] buf = new byte[4096]; private final byte[] buf = new byte[4096];
public WatchTableListener(Consumer<String> consumer) { public WatchTableListener(Consumer<String> consumer) {
this.consumer = consumer; this.consumer = consumer;
this.socket = getDatagramSocket(PORT);
}
private static DatagramSocket getDatagramSocket(int port) {
try { try {
this.socket = new DatagramSocket(PORT); return new DatagramSocket(port);
} catch (SocketException e) { } catch (SocketException e) {
log.error(e.getMessage()); log.error(
"WatchTableListener:DatagramSocket init failed, port {}: {}",
PORT,
e.getMessage());
} }
return null;
} }
public void run() { public void run() {
running = true; running = true;
while (running) { while (running) {
if (socket == null) {
log.warn("WatchTableListener:socket is null, try to initial it");
socket = getDatagramSocket(PORT);
if (socket == null) break;
}
DatagramPacket packet = new DatagramPacket(buf, buf.length); DatagramPacket packet = new DatagramPacket(buf, buf.length);
try { try {
...@@ -145,7 +158,9 @@ public class WatchTableServiceImpl implements WatchTableService { ...@@ -145,7 +158,9 @@ public class WatchTableServiceImpl implements WatchTableService {
} }
} }
socket.close(); if (socket != null) {
socket.close();
}
} }
public void stopThread() { public void stopThread() {
......
...@@ -877,7 +877,7 @@ ...@@ -877,7 +877,7 @@
<activation> <activation>
<activeByDefault>true</activeByDefault> <activeByDefault>true</activeByDefault>
<property> <property>
<name>-flink-all</name> <name>!flink-all</name>
</property> </property>
</activation> </activation>
</profile> </profile>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment