2024-02-20 10:41:14 +00:00
|
|
|
package nu.marginalia.service;
|
2023-10-15 16:38:30 +00:00
|
|
|
|
|
|
|
import com.google.inject.Inject;
|
2024-02-20 10:41:14 +00:00
|
|
|
import com.zaxxer.hikari.HikariDataSource;
|
2023-10-15 16:38:30 +00:00
|
|
|
import lombok.SneakyThrows;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2024-02-20 10:41:14 +00:00
|
|
|
import java.sql.SQLException;
|
2023-10-15 16:38:30 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
public class NodeConfigurationWatcher {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(NodeConfigurationWatcher.class);
|
2024-02-20 10:41:14 +00:00
|
|
|
private final HikariDataSource dataSource;
|
2023-10-15 16:38:30 +00:00
|
|
|
|
|
|
|
private volatile List<Integer> queryNodes = new ArrayList<>();
|
|
|
|
|
|
|
|
@Inject
|
2024-02-20 10:41:14 +00:00
|
|
|
public NodeConfigurationWatcher(HikariDataSource dataSource) {
|
|
|
|
this.dataSource = dataSource;
|
2023-10-15 16:38:30 +00:00
|
|
|
|
|
|
|
var watcherThread = new Thread(this::pollConfiguration, "Node Configuration Watcher");
|
|
|
|
watcherThread.setDaemon(true);
|
|
|
|
watcherThread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
@SneakyThrows
|
|
|
|
private void pollConfiguration() {
|
|
|
|
for (;;) {
|
|
|
|
List<Integer> goodNodes = new ArrayList<>();
|
|
|
|
|
2024-02-20 10:41:14 +00:00
|
|
|
try (var conn = dataSource.getConnection()) {
|
|
|
|
var stmt = conn.prepareStatement("""
|
|
|
|
SELECT ID FROM NODE_CONFIGURATION
|
|
|
|
WHERE ACCEPT_QUERIES AND NOT DISABLED
|
|
|
|
""");
|
|
|
|
var rs = stmt.executeQuery();
|
|
|
|
while (rs.next()) {
|
|
|
|
goodNodes.add(rs.getInt(1));
|
2023-10-15 16:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-20 10:41:14 +00:00
|
|
|
catch (SQLException ex) {
|
|
|
|
logger.error("Error polling node configuration", ex);
|
|
|
|
}
|
|
|
|
|
2024-01-13 17:27:42 +00:00
|
|
|
queryNodes = goodNodes;
|
2023-10-15 16:38:30 +00:00
|
|
|
|
|
|
|
TimeUnit.SECONDS.sleep(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Integer> getQueryNodes() {
|
|
|
|
return queryNodes;
|
|
|
|
}
|
|
|
|
}
|