feat(etcd): add key TTL synchronization
This commit is contained in:
32 files changed
+1249
-65
No files matched your search
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from "vue";
|
||||
import { computed, onBeforeUnmount, ref, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import KvKeyBrowser from "@/components/kv/KvKeyBrowser.vue";
|
||||
import * as api from "@/lib/backend/api";
|
||||
@@ -8,11 +8,18 @@ const props = defineProps<{ connectionId: string }>();
|
||||
|
||||
const { t } = useI18n();
|
||||
const browserRef = ref<InstanceType<typeof KvKeyBrowser> | null>(null);
|
||||
const supportsTtl = ref(false);
|
||||
const ttlCapabilityKnown = ref(false);
|
||||
const ttlCapabilityRefreshIntervalMs = 5000;
|
||||
let ttlCapabilityRequest = 0;
|
||||
let ttlCapabilityInFlightConnection: string | null = null;
|
||||
let ttlCapabilityRefreshTimer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
const etcdApi = {
|
||||
listPrefix: api.etcdListPrefix,
|
||||
get: api.etcdGet,
|
||||
put: (connectionId: string, key: string, value: api.KvValue) => api.etcdPut(connectionId, key, value),
|
||||
getMetadata: (connectionId: string, key: string) => api.etcdGet(connectionId, key, { metadataOnly: true }),
|
||||
put: (connectionId: string, key: string, value: api.KvValue, options?: api.KvPutOptions | null) => api.etcdPut(connectionId, key, value, options),
|
||||
deleteKey: api.etcdDelete,
|
||||
};
|
||||
|
||||
@@ -31,11 +38,63 @@ const labels = computed(() => ({
|
||||
deleteTitle: t("etcd.deleteTitle"),
|
||||
keyPlaceholder: t("etcd.keyPlaceholder"),
|
||||
keyRequired: t("etcd.keyRequired"),
|
||||
ttl: t("etcd.ttl"),
|
||||
ttlPlaceholder: t("etcd.ttlPlaceholder"),
|
||||
ttlInvalid: t("etcd.ttlInvalid"),
|
||||
ttlUnavailable: t("etcd.ttlUnavailable"),
|
||||
saved: t("etcd.saved"),
|
||||
deleted: t("etcd.deleted"),
|
||||
base64Readonly: t("etcd.base64Readonly"),
|
||||
}));
|
||||
|
||||
async function refreshTtlCapability() {
|
||||
const connectionId = props.connectionId;
|
||||
if (ttlCapabilityInFlightConnection === connectionId) return;
|
||||
|
||||
const request = ++ttlCapabilityRequest;
|
||||
ttlCapabilityInFlightConnection = connectionId;
|
||||
try {
|
||||
const supported = await api.etcdSupportsTtl(connectionId);
|
||||
if (request !== ttlCapabilityRequest || props.connectionId !== connectionId) return;
|
||||
supportsTtl.value = supported;
|
||||
ttlCapabilityKnown.value = true;
|
||||
} catch {
|
||||
// A transient connection failure is not evidence that the installed Agent lacks TTL support.
|
||||
// Keep the last confirmed capability and let the interval or an explicit refresh retry.
|
||||
} finally {
|
||||
if (request === ttlCapabilityRequest) ttlCapabilityInFlightConnection = null;
|
||||
}
|
||||
}
|
||||
|
||||
function stopTtlCapabilityRefresh() {
|
||||
ttlCapabilityRequest++;
|
||||
ttlCapabilityInFlightConnection = null;
|
||||
if (ttlCapabilityRefreshTimer !== null) {
|
||||
clearInterval(ttlCapabilityRefreshTimer);
|
||||
ttlCapabilityRefreshTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
function startTtlCapabilityRefresh() {
|
||||
stopTtlCapabilityRefresh();
|
||||
void refreshTtlCapability();
|
||||
ttlCapabilityRefreshTimer = setInterval(() => {
|
||||
void refreshTtlCapability();
|
||||
}, ttlCapabilityRefreshIntervalMs);
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.connectionId,
|
||||
() => {
|
||||
supportsTtl.value = false;
|
||||
ttlCapabilityKnown.value = false;
|
||||
startTtlCapabilityRefresh();
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
onBeforeUnmount(stopTtlCapabilityRefresh);
|
||||
|
||||
function focusSearch(): boolean {
|
||||
return browserRef.value?.focusSearch() ?? false;
|
||||
}
|
||||
@@ -48,5 +107,5 @@ defineExpose({ focusSearch, refresh });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<KvKeyBrowser ref="browserRef" :connection-id="props.connectionId" :api="etcdApi" :labels="labels" />
|
||||
<KvKeyBrowser ref="browserRef" :connection-id="props.connectionId" :api="etcdApi" :labels="labels" :supports-ttl="supportsTtl" :ttl-capability-known="ttlCapabilityKnown" @refresh-requested="refreshTtlCapability" />
|
||||
</template>
|
||||
@@ -0,0 +1,131 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createApp, defineComponent, h, nextTick, type App } from "vue";
|
||||
|
||||
const backend = vi.hoisted(() => ({
|
||||
etcdSupportsTtl: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("vue-i18n", () => ({
|
||||
useI18n: () => ({ t: (key: string) => key }),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
etcdListPrefix: vi.fn(),
|
||||
etcdGet: vi.fn(),
|
||||
etcdPut: vi.fn(),
|
||||
etcdDelete: vi.fn(),
|
||||
etcdSupportsTtl: backend.etcdSupportsTtl,
|
||||
}));
|
||||
|
||||
vi.mock("@/components/kv/KvKeyBrowser.vue", () => ({
|
||||
default: defineComponent({
|
||||
name: "KvKeyBrowserStub",
|
||||
props: {
|
||||
supportsTtl: { type: Boolean, default: false },
|
||||
ttlCapabilityKnown: { type: Boolean, default: true },
|
||||
},
|
||||
emits: ["refreshRequested"],
|
||||
setup(props, { emit, expose }) {
|
||||
expose({
|
||||
focusSearch: () => true,
|
||||
refresh: () => {
|
||||
emit("refreshRequested");
|
||||
return true;
|
||||
},
|
||||
});
|
||||
return () =>
|
||||
h(
|
||||
"button",
|
||||
{
|
||||
id: "kv-browser-stub",
|
||||
"data-supports-ttl": String(props.supportsTtl),
|
||||
"data-capability-known": String(props.ttlCapabilityKnown),
|
||||
onClick: () => emit("refreshRequested"),
|
||||
},
|
||||
"refresh",
|
||||
);
|
||||
},
|
||||
}),
|
||||
}));
|
||||
|
||||
import EtcdKeyBrowser from "@/components/etcd/EtcdKeyBrowser.vue";
|
||||
|
||||
let app: App<Element> | null = null;
|
||||
let root: HTMLDivElement | null = null;
|
||||
|
||||
async function flushUi() {
|
||||
await Promise.resolve();
|
||||
await Promise.resolve();
|
||||
await nextTick();
|
||||
}
|
||||
|
||||
async function mountBrowser() {
|
||||
root = document.createElement("div");
|
||||
document.body.appendChild(root);
|
||||
app = createApp(EtcdKeyBrowser, { connectionId: "etcd-1" });
|
||||
app.mount(root);
|
||||
await flushUi();
|
||||
return root.querySelector<HTMLButtonElement>("#kv-browser-stub")!;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
backend.etcdSupportsTtl.mockReset();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
app?.unmount();
|
||||
app = null;
|
||||
root?.remove();
|
||||
root = null;
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe("EtcdKeyBrowser TTL capability recovery", () => {
|
||||
it("keeps a transient probe failure unknown and retries automatically", async () => {
|
||||
backend.etcdSupportsTtl.mockRejectedValueOnce(new Error("Agent is reconnecting")).mockResolvedValue(true);
|
||||
|
||||
const browser = await mountBrowser();
|
||||
expect(browser.dataset.supportsTtl).toBe("false");
|
||||
expect(browser.dataset.capabilityKnown).toBe("false");
|
||||
|
||||
await vi.advanceTimersByTimeAsync(5000);
|
||||
await flushUi();
|
||||
|
||||
expect(backend.etcdSupportsTtl).toHaveBeenCalledTimes(2);
|
||||
expect(browser.dataset.supportsTtl).toBe("true");
|
||||
expect(browser.dataset.capabilityKnown).toBe("true");
|
||||
});
|
||||
|
||||
it("rechecks a confirmed unsupported Agent when the user refreshes", async () => {
|
||||
backend.etcdSupportsTtl.mockResolvedValueOnce(false).mockResolvedValueOnce(true);
|
||||
|
||||
const browser = await mountBrowser();
|
||||
expect(browser.dataset.supportsTtl).toBe("false");
|
||||
expect(browser.dataset.capabilityKnown).toBe("true");
|
||||
|
||||
browser.click();
|
||||
await flushUi();
|
||||
|
||||
expect(backend.etcdSupportsTtl).toHaveBeenCalledTimes(2);
|
||||
expect(browser.dataset.supportsTtl).toBe("true");
|
||||
expect(browser.dataset.capabilityKnown).toBe("true");
|
||||
});
|
||||
|
||||
it("keeps the last confirmed capability through a transient later failure", async () => {
|
||||
backend.etcdSupportsTtl.mockResolvedValueOnce(true).mockRejectedValueOnce(new Error("temporary failure"));
|
||||
|
||||
const browser = await mountBrowser();
|
||||
expect(browser.dataset.supportsTtl).toBe("true");
|
||||
expect(browser.dataset.capabilityKnown).toBe("true");
|
||||
|
||||
await vi.advanceTimersByTimeAsync(5000);
|
||||
await flushUi();
|
||||
|
||||
expect(backend.etcdSupportsTtl).toHaveBeenCalledTimes(2);
|
||||
expect(browser.dataset.supportsTtl).toBe("true");
|
||||
expect(browser.dataset.capabilityKnown).toBe("true");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user