From 0d57c39a78c2059bce71d0d12b9d9e14e36530fb Mon Sep 17 00:00:00 2001 From: miracle Date: Tue, 14 Jul 2026 23:16:10 +0800 Subject: [PATCH] perf(zookeeper): optimize connection initialization --- .../dbx/agent/zookeeper/ZooKeeperAgent.java | 108 +++++++++++++++++- .../agent/zookeeper/ZooKeeperAgentTest.java | 52 +++++++++ apps/desktop/src/App.vue | 22 +++- .../components/config/DriverStoreDialog.vue | 50 +++++++- .../connection/ConnectionDialog.vue | 13 ++- .../src/components/layout/AppDialogs.vue | 3 +- .../src/components/sidebar/TreeItem.vue | 23 ++-- .../__tests__/agentDriverInstallHint.spec.ts | 36 ++++++ .../lib/connection/agentDriverInstallHint.ts | 11 ++ .../app-tests/jdbcConnectionEntry.test.ts | 2 +- 10 files changed, 286 insertions(+), 34 deletions(-) create mode 100644 apps/desktop/src/lib/connection/__tests__/agentDriverInstallHint.spec.ts diff --git a/agents/drivers/zookeeper/src/main/java/com/dbx/agent/zookeeper/ZooKeeperAgent.java b/agents/drivers/zookeeper/src/main/java/com/dbx/agent/zookeeper/ZooKeeperAgent.java index 5cca0ccbd3..07a491afe7 100644 --- a/agents/drivers/zookeeper/src/main/java/com/dbx/agent/zookeeper/ZooKeeperAgent.java +++ b/agents/drivers/zookeeper/src/main/java/com/dbx/agent/zookeeper/ZooKeeperAgent.java @@ -11,6 +11,8 @@ import org.apache.zookeeper.data.Stat; import java.io.BufferedReader; import java.io.InputStreamReader; +import java.net.InetSocketAddress; +import java.net.Socket; import java.nio.ByteBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.CharsetDecoder; @@ -23,15 +25,18 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; public final class ZooKeeperAgent { private static final Gson GSON = new GsonBuilder().serializeNulls().create(); private static final int DEFAULT_LIMIT = 100; private static final int DEFAULT_SESSION_TIMEOUT_MS = 30000; - private static final int DEFAULT_CONNECTION_TIMEOUT_MS = 15000; - private static final int DEFAULT_BASE_SLEEP_TIME_MS = 1000; - private static final int DEFAULT_MAX_RETRIES = 3; + private static final int DEFAULT_CONNECTION_TIMEOUT_MS = 5000; + private static final int DEFAULT_BASE_SLEEP_TIME_MS = 250; + private static final int DEFAULT_MAX_RETRIES = 2; + private static final int DEFAULT_PORT = 2181; + private static final int DEFAULT_PROBE_TIMEOUT_MS = 2000; private static final String STAT_LOOKUP_CONCURRENCY_PROPERTY = "dbx.zookeeper.statLookupConcurrency"; private static final String STAT_LOOKUP_CONCURRENCY_ENV = "DBX_ZOOKEEPER_STAT_LOOKUP_CONCURRENCY"; private static final int DEFAULT_STAT_LOOKUP_CONCURRENCY = 16; @@ -88,10 +93,15 @@ public final class ZooKeeperAgent { int baseSleepTimeMs = intOrDefault(connection, "base_sleep_time_ms", DEFAULT_BASE_SLEEP_TIME_MS); int maxRetries = intOrDefault(connection, "max_retries", DEFAULT_MAX_RETRIES); + int connectionTimeoutMs = intOrDefault(connection, "connection_timeout_ms", DEFAULT_CONNECTION_TIMEOUT_MS); + String connectString = reachableConnectString( + connectString(connection), + Math.min(DEFAULT_PROBE_TIMEOUT_MS, connectionTimeoutMs) + ); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() - .connectString(connectString(connection)) + .connectString(connectString) .sessionTimeoutMs(intOrDefault(connection, "session_timeout_ms", DEFAULT_SESSION_TIMEOUT_MS)) - .connectionTimeoutMs(intOrDefault(connection, "connection_timeout_ms", DEFAULT_CONNECTION_TIMEOUT_MS)) + .connectionTimeoutMs(connectionTimeoutMs) .retryPolicy(new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries)); String namespace = trimSlashes(stringOrEmpty(connection, "namespace")); @@ -118,7 +128,93 @@ public final class ZooKeeperAgent { return configured; } return stringOrDefault(connection, "host", "127.0.0.1") + ":" - + intOrDefault(connection, "port", 2181); + + intOrDefault(connection, "port", DEFAULT_PORT); + } + + static String reachableConnectString(String connectString, int probeTimeoutMs) { + int slash = connectString.indexOf('/'); + String hostsPart = slash >= 0 ? connectString.substring(0, slash) : connectString; + String chroot = slash >= 0 ? connectString.substring(slash) : ""; + + List hosts = new ArrayList<>(); + for (String item : hostsPart.split(",")) { + String trimmed = item.trim(); + if (!trimmed.isEmpty()) { + hosts.add(trimmed); + } + } + + List reachable = probeReachableHosts(hosts, probeTimeoutMs); + if (reachable.isEmpty()) { + throw new IllegalStateException( + "No reachable ZooKeeper server within " + probeTimeoutMs + "ms: " + hostsPart + ); + } + // Curator needs the complete ensemble to reconnect when nodes recover or fail over later. + return String.join(",", hosts) + chroot; + } + + private static List probeReachableHosts(List hosts, int probeTimeoutMs) { + if (hosts.isEmpty()) { + return Collections.emptyList(); + } + ExecutorService executor = Executors.newFixedThreadPool( + Math.min(hosts.size(), 8), + daemonThreadFactory("dbx-zookeeper-probe-") + ); + try { + List> probes = new ArrayList<>(); + for (String host : hosts) { + probes.add(executor.submit(() -> isEndpointReachable(host, probeTimeoutMs))); + } + // Probes run in parallel; the deadline covers socket timeout plus DNS slack. + long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(probeTimeoutMs + 500L); + List reachable = new ArrayList<>(); + for (int i = 0; i < hosts.size(); i++) { + long remainingMs = Math.max(1, TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime())); + try { + if (probes.get(i).get(remainingMs, TimeUnit.MILLISECONDS)) { + reachable.add(hosts.get(i)); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("ZooKeeper reachability probe interrupted", e); + } catch (ExecutionException | TimeoutException e) { + // Treat probe failures as unreachable. + } + } + return reachable; + } finally { + executor.shutdownNow(); + } + } + + private static boolean isEndpointReachable(String endpoint, int probeTimeoutMs) { + try (Socket socket = new Socket()) { + socket.connect(parseEndpoint(endpoint), probeTimeoutMs); + return true; + } catch (Exception e) { + return false; + } + } + + static InetSocketAddress parseEndpoint(String endpoint) { + String host = endpoint; + int port = DEFAULT_PORT; + int bracketEnd = endpoint.indexOf(']'); + if (endpoint.startsWith("[") && bracketEnd > 0) { + host = endpoint.substring(1, bracketEnd); + if (bracketEnd + 1 < endpoint.length() && endpoint.charAt(bracketEnd + 1) == ':') { + port = Integer.parseInt(endpoint.substring(bracketEnd + 2)); + } + } else { + int colon = endpoint.lastIndexOf(':'); + if (colon >= 0 && endpoint.indexOf(':') == colon) { + host = endpoint.substring(0, colon); + port = Integer.parseInt(endpoint.substring(colon + 1)); + } + } + return new InetSocketAddress(host, port); } private static void startAndVerify(CuratorFramework active, JsonObject connection) throws Exception { diff --git a/agents/drivers/zookeeper/src/test/java/com/dbx/agent/zookeeper/ZooKeeperAgentTest.java b/agents/drivers/zookeeper/src/test/java/com/dbx/agent/zookeeper/ZooKeeperAgentTest.java index 0f11aa774f..479d7bba4f 100644 --- a/agents/drivers/zookeeper/src/test/java/com/dbx/agent/zookeeper/ZooKeeperAgentTest.java +++ b/agents/drivers/zookeeper/src/test/java/com/dbx/agent/zookeeper/ZooKeeperAgentTest.java @@ -137,6 +137,58 @@ final class ZooKeeperAgentTest { } } + @Test + void connectFailsFastWhenAllServersAreUnreachable() { + long startedAt = System.nanoTime(); + JsonObject error = error(request( + 1, + "connect", + "{\"connection\":{\"connect_string\":\"127.0.0.1:1\",\"connection_timeout_ms\":15000}}" + )); + long elapsedMs = (System.nanoTime() - startedAt) / 1_000_000; + + Assertions.assertTrue( + error.get("message").getAsString().contains("No reachable ZooKeeper server"), + "expected fast unreachable error, got: " + error.get("message").getAsString() + ); + Assertions.assertTrue(elapsedMs < 4000, "expected fast failure, took " + elapsedMs + "ms"); + } + + @Test + void connectUsesReachableServerWithoutDroppingEnsembleMembers() throws Exception { + try (TestingServer server = new TestingServer()) { + JsonObject connect = result(request( + 1, + "connect", + "{\"connection\":{\"connect_string\":\"127.0.0.1:1," + server.getConnectString() + "\"}}" + )); + Assertions.assertTrue(connect.get("ok").getAsBoolean()); + + JsonObject get = result(request(2, "kv_get", "{\"key\":\"/\"}")); + Assertions.assertTrue(get.get("found").getAsBoolean()); + } + } + + @Test + void reachableConnectStringPreservesFullEnsembleAndChroot() throws Exception { + try (TestingServer server = new TestingServer()) { + String alive = server.getConnectString(); + String ensemble = "127.0.0.1:1," + alive + "/app"; + + Assertions.assertEquals( + ensemble, + ZooKeeperAgent.reachableConnectString(ensemble, 1000) + ); + Assertions.assertEquals(alive, ZooKeeperAgent.reachableConnectString(alive, 1000)); + + IllegalStateException error = Assertions.assertThrows( + IllegalStateException.class, + () -> ZooKeeperAgent.reachableConnectString("127.0.0.1:1/app", 1000) + ); + Assertions.assertTrue(error.getMessage().contains("No reachable ZooKeeper server")); + } + } + @Test void testConnectionDoesNotReplaceActiveClient() throws Exception { try (TestingServer active = new TestingServer(); TestingServer probe = new TestingServer()) { diff --git a/apps/desktop/src/App.vue b/apps/desktop/src/App.vue index 82a2be0ca3..c9d7ef80b0 100644 --- a/apps/desktop/src/App.vue +++ b/apps/desktop/src/App.vue @@ -78,6 +78,7 @@ import { assessProductionSql } from "@/lib/database/productionSafety"; import { executeWithProductionSqlGuard } from "@/lib/database/productionExecutionGuard"; import { buildHistoryAiAnalysisPrompt } from "@/lib/history/historyAiAnalysis"; import { countAvailableAgentDriverUpdates, type AgentDriverUpdateBadgeState } from "@/lib/connection/agentDriverUpdateBadge"; +import type { DriverStoreFocus } from "@/lib/connection/agentDriverInstallHint"; import { safeLocalStorageGet, safeLocalStorageSet } from "@/lib/backend/safeStorage"; import { apiUrl, webPath } from "@/lib/common/webPath"; import { APP_FONT_SANS_CSS_VAR, DEFAULT_UI_FONT_FAMILY } from "@/lib/app/appFonts"; @@ -318,8 +319,18 @@ function closeSettingsPage() { driverStoreActive.value = false; } -function openDriverStorePage(tab?: "agent" | "jdbc" | "storage" | "runtime") { - if (tab) driverStoreActiveTab.value = tab; +const driverStoreFocus = ref(null); + +function openDriverStorePage(target?: "agent" | "jdbc" | "storage" | "runtime" | DriverStoreFocus | null) { + if (typeof target === "string") { + driverStoreActiveTab.value = target; + driverStoreFocus.value = null; + } else if (target && target.target === "tab") { + driverStoreActiveTab.value = target.tab; + driverStoreFocus.value = null; + } else { + driverStoreFocus.value = target ?? null; + } driverStoreTabOpen.value = true; driverStoreActive.value = true; settingsStore.settingsPageActive = false; @@ -329,6 +340,7 @@ function closeDriverStorePage() { driverStoreTabOpen.value = false; driverStoreActive.value = false; driverStoreActiveTab.value = "agent"; + driverStoreFocus.value = null; } const toolbarAgentDriverUpdateCount = computed(() => (updateNotificationsEnabled.value ? agentDriverUpdateCount.value : 0)); const toolbarHasUpdateAvailable = computed(() => updateNotificationsEnabled.value && hasUpdateAvailable.value); @@ -1739,8 +1751,8 @@ function handleContextMenu(e: MouseEvent) { e.preventDefault(); } -function openDriverStoreFromEvent() { - openDriverStorePage(); +function openDriverStoreFromEvent(event: Event) { + openDriverStorePage(((event as CustomEvent).detail as DriverStoreFocus | undefined) ?? null); } function runUpdateNotificationChecks() { @@ -1909,7 +1921,7 @@ onUnmounted(() => { @discard-all-tab-close="handleDiscardAllPendingTabClose" @cancel-tab-close="cancelPendingAppClose" /> - + -import { ref, onMounted, onUnmounted, computed, watch } from "vue"; +import { ref, onMounted, onUnmounted, computed, watch, nextTick } from "vue"; import { useI18n } from "vue-i18n"; import { Activity, ExternalLink, Cpu, FolderOpen, FolderSync, MemoryStick, Search, Square, Trash2, Download, RotateCcw, Loader2, RefreshCw, Check, Clock3, FileUp } from "@lucide/vue"; import { Button } from "@/components/ui/button"; @@ -20,6 +20,7 @@ import type { AgentDriverInfo, DriverRuntimeInfo, DriverRuntimeSummary, DriverSt import { formatRuntimeBytes, formatRuntimeCpu, formatRuntimeUptime, runtimeHealthClass, runtimeStatusClass, runtimeStatusDotClass } from "@/lib/connection/driverRuntimePresentation"; import { addDriverInstallQueue, driverInstallProgressPercent, isDriverInstallProgressTarget, removeDriverInstallQueue, takeNextDriverInstallQueue, type DriverInstallProgress } from "@/lib/connection/driverInstallProgressUi"; import { PRESTOSQL_DRIVER_DB_TYPE, prestoSqlBuiltinDriverRow, prestoSqlMavenBundle } from "@/lib/database/prestoSqlBuiltinDriver"; +import type { DriverStoreFocus } from "@/lib/connection/agentDriverInstallHint"; const { t } = useI18n(); const { toast } = useToast(); @@ -29,10 +30,12 @@ const props = withDefaults( defineProps<{ updateNotificationsEnabled?: boolean; activeTab?: "agent" | "jdbc" | "storage" | "runtime"; + focusTarget?: DriverStoreFocus | null; }>(), { updateNotificationsEnabled: true, activeTab: "agent", + focusTarget: null, }, ); @@ -664,6 +667,35 @@ const filteredAgentDrivers = computed(() => { return builtinDriverRows.value.filter((driver) => [driver.label, driver.db_type, driver.version, driver.installed_version, driverRequiresJavaRuntime(driver) ? driver.jre : ""].filter(Boolean).join(" ").toLowerCase().includes(query)); }); +const highlightedFocusKey = ref(null); +let focusHighlightTimer: ReturnType | undefined; + +function focusElementKey(focus: DriverStoreFocus): string { + return focus.target === "driver" ? `driver:${focus.driver ?? ""}` : "jre"; +} + +watch( + [() => props.focusTarget, builtinDriverRows], + async ([focus]) => { + if (!focus || focus.target === "tab") return; + driverStoreTab.value = "agent"; + if (focus.target === "driver") { + // Wait until the requested driver row is loaded before scrolling to it. + if (!focus.driver || !builtinDriverRows.value.some((driver) => driver.db_type === focus.driver)) return; + agentDriverSearch.value = ""; + } + const key = focusElementKey(focus); + highlightedFocusKey.value = key; + await nextTick(); + document.querySelector(`[data-driver-store-focus="${CSS.escape(key)}"]`)?.scrollIntoView({ block: "center", behavior: "smooth" }); + clearTimeout(focusHighlightTimer); + focusHighlightTimer = setTimeout(() => { + if (highlightedFocusKey.value === key) highlightedFocusKey.value = null; + }, 6000); + }, + { immediate: true }, +); + const jdbcDriverListItems = computed(() => { const localBundleItems = jdbcLocalBundles.value.map((bundle) => ({ kind: "local" as const, @@ -1099,7 +1131,7 @@ watch(driverStoreTab, (tab) => { -
+