Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/specs/homes.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ LAYERFS_HOME (default ~/.layerfs) LayerFS's
bucket-<name>.key
pg-<store>--<random>.pw pg-<store>--<machine>--<random>.pw
job/ (0700) storage jobs about this home (§5)
storage-jobs.json push, pull, move, backup and restore jobs
storage-jobs.json push, pull, move, backup, restore and compact jobs
doctor-jobs.json home check reports
stat/ (0700) storage statistics about this home (§5)
storage-observations/ Store statistics journals
Expand Down
2 changes: 1 addition & 1 deletion src/client/storage/HomeOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function HomeOverview(props: HomeOverviewProps): ReactNode {
if (!j.error && j.outcome !== 'failed' && j.outcome !== 'interrupted' && j.outcome !== 'unknown') continue
needs.push({
key: `job:${j.id}`, tone: 'danger', rank: 0,
title: <StoreLink name={j.store} onOpen={props.onOpen}>{endedWords(j, t)}</StoreLink>,
title: <StoreLink name={j.store} onOpen={props.onOpen}>{endedWords(j, t, bytes)}</StoreLink>,
detail: j.error ? <>{j.error.message} · {ago(j.ended, now, t as Translate)}</> : ago(j.ended, now, t as Translate),
action: { label: t('jobRetry'), run: () => props.onRetry(j) },
extra: <Button variant="ghost" size="sm" onClick={() => props.onDismiss(j)}>{t('dismiss')}</Button>,
Expand Down
4 changes: 2 additions & 2 deletions src/client/storage/JobProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function JobPanel({ job: j, t, onRetry, onDismiss }: { job: StorageJob, t
<div className={css.job} data-state={running ? 'running' : failed ? 'failed' : 'done'} role="status">
<div className={css.jobLine}>
<StateDot state={running ? 'ongoing' : failed ? 'error' : 'done'} size={running ? 13 : 10} />
<span className={css.jobTitle}>{running ? doingWords(j, t) : endedWords(j, t)}</span>
<span className={css.jobTitle}>{running ? doingWords(j, t) : endedWords(j, t, bytes)}</span>
{running ? <span className={css.jobMeta}>{t('jobStep', { at, of: steps.length, step: stepWords(j.phase, t) })}</span> : null}
{running && j.total ? <span className={css.jobMeta}>{t('jobBytes', { done: bytes(j.done ?? 0), total: bytes(j.total) })}</span> : null}
<span className={css.jobEnd}>
Expand Down Expand Up @@ -52,7 +52,7 @@ export function JobList({ jobs, t, now }: { jobs: readonly StorageJob[], t: Stor
<li key={j.id} className={css.row}>
<StateDot state={!j.ended ? 'ongoing' : j.error ? 'error' : 'done'} size={!j.ended ? 13 : 10} />
<span className={css.rowMain}>
<span className={css.rowTitle}>{j.ended ? endedWords(j, t) : doingWords(j, t)}</span>
<span className={css.rowTitle}>{j.ended ? endedWords(j, t, bytes) : doingWords(j, t)}</span>
<span className={css.rowSub}>{kindWords(j.kind, t)} {j.target} · {ago(j.ended ?? j.started, now, t as Translate)}</span>
</span>
</li>
Expand Down
9 changes: 9 additions & 0 deletions src/client/storage/StoragePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ function StorageIde({ api, doctorT, t, openRef, map, more }: { api: LayerfsApi,
if (j.kind === 'pull') return api.call('POST', `/api/stores/${enc(j.store)}/pull`)
if (j.kind === 'move') return api.call('POST', `/api/stores/${enc(j.store)}/move`, { machine: j.target })
if (j.kind === 'backup') return api.call('POST', `/api/stores/${enc(j.store)}/backup`, { bucket: j.target, ...(j.file ? { file: j.file } : {}) })
// a new run: it settles what the failed one left before it marks again
if (j.kind === 'compact') return api.call('POST', `/api/stores/${enc(j.store)}/compact`)
return api.call('POST', `/api/buckets/${enc(j.target)}/restore`, { store: j.store, ...(j.file ? { file: j.file } : {}) })
}
const openMachine = (name: string) => open(`machine:${name}`)
Expand All @@ -162,6 +164,13 @@ function StorageIde({ api, doctorT, t, openRef, map, more }: { api: LayerfsApi,
backup: () => setDialog(view && view.buckets.length === 0 ? { kind: 'bucket', then: { kind: 'backup', store: s, known: [] } } : { kind: 'backup', store: s }),
backupTo: (l) => act(() => api.call('POST', `/api/stores/${enc(s.name)}/backup`, { bucket: l.bucket, file: l.file })),
restore: (bucket, file) => setDialog({ kind: 'restore', bucket, file, store: s.name }),
// only a runtime that has store.compact gets the menu row
...(view?.compact ? {
compact: () => setDialog({
kind: 'confirm', title: t('confirmTitleCompact', { name: s.name }), body: t('confirmCompactBody'), action: t('compactAction'), plain: true,
run: () => api.call('POST', `/api/stores/${enc(s.name)}/compact`).then((job) => { if (job && typeof job === 'object' && 'id' in job && 'kind' in job) take([job as StorageJob]) }),
}),
} : {}),
remove: () => setDialog({
kind: 'confirm', title: t('confirmTitleRemove', { name: s.name }), body: t('confirmRemoveBody', { n: s.projects.length }), action: t('removeAction'), name: s.name,
run: () => api.call('DELETE', `/api/stores/${enc(s.name)}?confirm=${enc(s.name)}`).then(() => closeStore(s.name)),
Expand Down
8 changes: 7 additions & 1 deletion src/client/storage/StoreCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ago, bytes } from '../files/format.ts'
import type { Translate } from '../files/types.ts'
import { LinkButton, MoreMenu } from '../ui/index.tsx'
import type { StorageT } from './locale.ts'
import { inHome, latestBackup } from './model.ts'
import { compactBlock, inHome, latestBackup } from './model.ts'
import type { StorageJob, StoreBackupLink, StoreRow } from './types.ts'
import css from './Storage.module.css'

Expand All @@ -18,6 +18,8 @@ export interface StoreActions {
move: () => void
backup: () => void
backupTo: (link: StoreBackupLink) => void
/** Confirm, then compact the Store; absent when the selected LayerFS runtime has no store.compact. */
compact?: (() => void) | undefined
restore: (bucket: string, file: string) => void
remove: () => void
disconnect: () => void
Expand Down Expand Up @@ -52,14 +54,17 @@ export function Location({ store: s, t, onMachine }: { store: StoreRow, t: Stora
*/
export function StoreMenu({ store: s, on, t, machine }: { store: StoreRow, on: StoreActions, t: StorageT, machine?: string | undefined }): ReactNode {
const pg = s.kind === 'postgresql'
const blocked = compactBlock(s, t)
return (
<MoreMenu label={t('storeActions', { name: s.name })}>
{(close) => {
const run = (fn: () => void) => () => { close(); fn() }
const compact = on.compact ? <MenuItemButton disabled={blocked !== null} disabledReason={blocked ?? undefined} onSelect={run(on.compact)}>{t('compactStore')}</MenuItemButton> : null
return pg ? (
<>
<MenuItemButton onSelect={run(() => on.editConnection())}>{t('msEditConnection')}</MenuItemButton>
<MenuItemButton onSelect={run(() => on.check(machine))}>{t('pgCheckFrom')}</MenuItemButton>
{compact}
<MenuItemButton danger separatorBefore onSelect={run(on.disconnect)}>{t('disconnect')}</MenuItemButton>
</>
) : (
Expand All @@ -78,6 +83,7 @@ export function StoreMenu({ store: s, on, t, machine }: { store: StoreRow, on: S
{`${t('backupNow')} · ${l.bucket}/${l.file}`}
</MenuItemButton>
))}
{compact}
<MenuItemButton danger separatorBefore onSelect={run(on.remove)}>{t('remove')}</MenuItemButton>
</>
)
Expand Down
11 changes: 6 additions & 5 deletions src/client/storage/dialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export type StorageDialog =
| { kind: 'restore', bucket: string, file: string, store?: string | null }
/** `then`: the dialog Add a bucket returns to, closed or saved. */
| { kind: 'bucket', then?: StorageDialog }
| { kind: 'confirm', title: string, body: string, action: string, name?: string, force?: ConfirmForce, run: (force: boolean) => Promise<unknown> }
/** `plain`: the action removes nothing, so its button is not the danger one (Compact). */
| { kind: 'confirm', title: string, body: string, action: string, name?: string, force?: ConfirmForce, plain?: boolean, run: (force: boolean) => Promise<unknown> }

/** An optional Force checkbox on a confirm: checked, the action runs with force and reads `action`. */
export interface ConfirmForce { label: string, action: string }
Expand Down Expand Up @@ -84,7 +85,7 @@ export function StorageDialogs({ api, t, dialog, view, onClose, done, onDialog }
case 'backups': return <BackupsDialog api={api} t={t} store={dialog.store} view={view} onClose={onClose} done={done} onDialog={onDialog} />
case 'restore': return <RestoreDialog api={api} t={t} dialog={dialog} view={view} onClose={onClose} done={done} />
case 'bucket': return <BucketDialog api={api} t={t} onClose={dialog.then && onDialog ? () => onDialog(dialog.then!) : onClose} done={done} />
case 'confirm': return <ConfirmDialog t={t} title={dialog.title} body={dialog.body} action={dialog.action} name={dialog.name} force={dialog.force} onClose={onClose} run={(force) => after(dialog.run(force))} />
case 'confirm': return <ConfirmDialog t={t} title={dialog.title} body={dialog.body} action={dialog.action} name={dialog.name} force={dialog.force} plain={dialog.plain} onClose={onClose} run={(force) => after(dialog.run(force))} />
}
}

Expand Down Expand Up @@ -545,14 +546,14 @@ function BucketDialog({ api, t, onClose, done }: { api: LayerfsApi, t: StorageT,
// ---------- Confirm ----------

/** A destructive confirmation; with `name`, the name is typed to confirm. */
function ConfirmDialog({ t, title, body, action, name, force, onClose, run }: {
t: StorageT, title: string, body: string, action: string, name?: string | undefined, force?: ConfirmForce | undefined, onClose: () => void, run: (force: boolean) => Promise<unknown>
function ConfirmDialog({ t, title, body, action, name, force, plain, onClose, run }: {
t: StorageT, title: string, body: string, action: string, name?: string | undefined, force?: ConfirmForce | undefined, plain?: boolean | undefined, onClose: () => void, run: (force: boolean) => Promise<unknown>
}): ReactNode {
const [typed, setTyped] = useState('')
const [forced, setForced] = useState(false)
const on = !!force && forced
return (
<FormModal open onClose={onClose} t={t} title={title} description={body} action={on ? force.action : action} danger valid={!name || typed === name} submit={() => run(on)}>
<FormModal open onClose={onClose} t={t} title={title} description={body} action={on ? force.action : action} danger={!plain} valid={!name || typed === name} submit={() => run(on)}>
{name ? <TextField label={t('typeToConfirm', { name })} value={typed} onChange={setTyped} mono autoFocus placeholder={name} /> : null}
{force ? <Checkbox checked={forced} onChange={setForced} label={force.label} /> : null}
{!name && !force ? <span className={clsx(css.hidden)} /> : null}
Expand Down
30 changes: 30 additions & 0 deletions src/client/storage/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,25 @@ const en = {
stepUpload: 'uploading',
stepDownload: 'downloading',
stepHash: 'hashing',
stepMark: 'marking',
stepBuild: 'building',
stepSwap: 'swapping',
stepCleanup: 'cleaning up',
stepStarting: 'starting',
jobPushing: 'Pushing to {place}',
jobPulling: 'Pulling from {place}',
jobMoving: 'Moving to {place}',
jobBackingUp: 'Backing up to {place}',
jobRestoring: 'Restoring from {place}',
jobCompacting: 'Compacting {place}',
jobPushed: 'Pushed to {place}',
jobPulled: 'Pulled from {place}',
jobMoved: 'Moved to {place}',
jobBackedUp: 'Backed up to {place}',
jobRestored: 'Restored from {place}',
jobCompacted: 'Compacted {place}',
jobCompactedFreed: 'Compacted {place} · freed {freed}',
jobCompactedNone: 'Compacted {place} · nothing to reclaim',
jobFailed: '{what} {place} failed in {phase}',
jobUnknown: 'Outcome unknown · inspect the Store',
jobInterrupted: 'Interrupted · final result unavailable',
Expand All @@ -169,6 +177,7 @@ const en = {
kindMove: 'Move to',
kindBackup: 'Backup to',
kindRestore: 'Restore from',
kindCompact: 'Compaction of',
jobRetry: 'Retry',
recentOperations: 'Recent operations',
recentOperationsNone: 'No push, pull, move, backup or restore in this session.',
Expand Down Expand Up @@ -492,6 +501,12 @@ const en = {
connectBucket: 'Connect bucket',
confirmTitleRemove: 'Remove {name}?',
confirmRemoveBody: 'The Store and every project in it ({n}) are deleted with all their history. Backups in buckets stay.',
confirmTitleCompact: 'Compact {name}?',
confirmCompactBody: 'Reclaims the space of removed projects, Branches and files by rebuilding the Store. The Store refuses other work until it ends.',
compactAction: 'Compact',
compactStore: 'Compact…',
compactNeedsHome: 'Pull the Store to LayerFS Home first',
compactNeedsIdle: 'The Store is not idle',
confirmTitlePg: 'Disconnect the PostgreSQL Store {name}?',
confirmPgBody: 'This home forgets the Store and its projects. The database and its data stay; connect it again to use them.',
confirmTitleBucket: 'Disconnect the bucket {name}?',
Expand Down Expand Up @@ -1073,17 +1088,25 @@ const zh: typeof en = {
stepUpload: '上传中',
stepDownload: '下载中',
stepHash: '计算哈希',
stepMark: '标记中',
stepBuild: '重建中',
stepSwap: '切换中',
stepCleanup: '清理中',
stepStarting: '启动中',
jobPushing: '正在推送到 {place}',
jobPulling: '正在从 {place} 拉回',
jobMoving: '正在迁移到 {place}',
jobBackingUp: '正在备份到 {place}',
jobRestoring: '正在从 {place} 恢复',
jobCompacting: '正在压缩 {place}',
jobPushed: '已推送到 {place}',
jobPulled: '已从 {place} 拉回',
jobMoved: '已迁移到 {place}',
jobBackedUp: '已备份到 {place}',
jobRestored: '已从 {place} 恢复',
jobCompacted: '已压缩 {place}',
jobCompactedFreed: '已压缩 {place} · 释放 {freed}',
jobCompactedNone: '已压缩 {place} · 没有可回收的空间',
jobFailed: '{what} {place} 在 {phase} 阶段失败',
jobUnknown: '结果未知 · 请检查 Store',
jobInterrupted: '已中断 · 最终结果不可用',
Expand All @@ -1094,6 +1117,7 @@ const zh: typeof en = {
kindMove: '迁移到',
kindBackup: '备份到',
kindRestore: '恢复自',
kindCompact: '压缩',
jobRetry: '重试',
recentOperations: '最近的操作',
recentOperationsNone: '本次会话中没有推送、拉回、迁移、备份或恢复。',
Expand Down Expand Up @@ -1410,6 +1434,12 @@ const zh: typeof en = {
connectBucket: '连接存储桶',
confirmTitleRemove: '移除 {name}?',
confirmRemoveBody: 'Store 及其中所有项目({n} 个)将连同全部历史一起删除。存储桶中的备份保留。',
confirmTitleCompact: '压缩 {name}?',
confirmCompactBody: '通过重建 Store 回收已移除的项目、分支和文件所占的空间。结束前 Store 会拒绝其他操作。',
compactAction: '压缩',
compactStore: '压缩…',
compactNeedsHome: '请先将 Store 拉回 LayerFS Home',
compactNeedsIdle: 'Store 当前不空闲',
confirmTitlePg: '断开 PostgreSQL Store {name}?',
confirmPgBody: '此 Home 将忘记该 Store 及其项目。数据库及数据保留;重新连接即可再次使用。',
confirmTitleBucket: '断开存储桶 {name}?',
Expand Down
35 changes: 31 additions & 4 deletions src/client/storage/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import type { StorageT } from './locale.ts'
/** A sqlite Store saved under the LayerFS home (`native`). */
export const inHome = (s: StoreRow): boolean => s.kind === 'sqlite' && s.machine === 'native'

/**
* Why store.compact cannot start on a Store now, or null. LayerFS compacts a
* sqlite Store only where its file is, and the home is the one place this app
* runs it; a PostgreSQL Store is compacted on its server.
*/
export function compactBlock(s: StoreRow, t: StorageT): string | null {
if (s.kind === 'sqlite' && !inHome(s)) return t('compactNeedsHome')
if (s.state !== 'idle') return t('compactNeedsIdle')
return null
}

/** project.init goes into a Store on any machine, not while it is down or busy. */
export const canAddProject = (s: StoreRow): boolean => s.kind === 'postgresql' || (s.state !== 'down' && s.state !== 'busy')

Expand Down Expand Up @@ -159,14 +170,18 @@ export const STEPS: Record<JobKind, readonly string[]> = {
move: ['check', 'snapshot', 'send', 'verify', 'install', 'record', 'retire'],
backup: ['check', 'snapshot', 'send', 'check', 'hash', 'upload'],
restore: ['check', 'download', 'verify', 'install'],
// a sqlite Store's; the host names a PostgreSQL Store's own (`steps`)
compact: ['mark', 'build', 'verify', 'install'],
}

export type StepState = 'done' | 'now' | 'failed' | 'todo'

/** One run of a call's steps with each step's state. */
export function stepsOf(j: StorageJob): { step: string, state: StepState }[] {
const seen = j.phases?.filter((p) => p.half === (j.half ?? null)).map((p) => p.phase)
const steps = seen?.length ? seen : [...STEPS[j.kind], ...(!STEPS[j.kind].includes(j.phase ?? '') && j.phase ? [j.phase] : [])]
// a job that names its steps keeps them in that order from the start, plus any phase it did not name
const named = j.steps?.length ? [...j.steps, ...(seen ?? []).filter((p) => !j.steps!.includes(p))] : null
const steps = named ?? (seen?.length ? seen : [...STEPS[j.kind], ...(!STEPS[j.kind].includes(j.phase ?? '') && j.phase ? [j.phase] : [])])
const ok = j.ended !== null && j.error === null
const at = j.error ? steps.lastIndexOf(j.error.phase ?? '') : j.phase ? steps.lastIndexOf(j.phase) : j.ended ? -1 : 0
return steps.map((step, i) => ({ step, state: ok || i < at ? 'done' : i === at ? (j.error ? 'failed' : 'now') : 'todo' }))
Expand Down Expand Up @@ -196,6 +211,10 @@ export function stepWords(step: string | null, t: StorageT): string {
case 'upload': return t('stepUpload')
case 'download': return t('stepDownload')
case 'hash': return t('stepHash')
case 'mark': return t('stepMark')
case 'build': return t('stepBuild')
case 'swap': return t('stepSwap')
case 'cleanup': return t('stepCleanup')
case null: return t('stepStarting')
default: return step
}
Expand All @@ -209,22 +228,29 @@ export function doingWords(j: StorageJob, t: StorageT): string {
case 'move': return t('jobMoving', { place: placeWords(j.target, t) })
case 'backup': return t('jobBackingUp', { place: j.target })
case 'restore': return t('jobRestoring', { place: j.file ? `${j.target}/${j.file}` : j.target })
case 'compact': return t('jobCompacting', { place: j.store })
}
}

/** How a job ended, in words. */
export function endedWords(j: StorageJob, t: StorageT): string {
/** How a job ended, in words. `size` writes a byte count (a compaction's freed space); without it the count is left out. */
export function endedWords(j: StorageJob, t: StorageT, size?: (n: number) => string): string {
if (j.summary) return j.summary
if (j.outcome === 'unknown') return t('jobUnknown')
if (j.outcome === 'interrupted') return t('jobInterrupted')
const place = j.kind === 'pull' ? placeWords(j.from, t) : j.kind === 'backup' || j.kind === 'restore' ? j.target : placeWords(j.target, t)
const place = j.kind === 'pull' ? placeWords(j.from, t) : j.kind === 'backup' || j.kind === 'restore' ? j.target : j.kind === 'compact' ? j.store : placeWords(j.target, t)
if (j.error) return t('jobFailed', { what: kindWords(j.kind, t), place, phase: j.error.phase ?? '—' })
switch (j.kind) {
case 'push': return t('jobPushed', { place })
case 'pull': return t('jobPulled', { place })
case 'move': return t('jobMoved', { place })
case 'backup': return t('jobBackedUp', { place })
case 'restore': return t('jobRestored', { place })
case 'compact': {
// the report's own words: nothing was worth rebuilding, or what the Store shrank by
if (j.result?.path === 'none') return t('jobCompactedNone', { place })
const freed = j.result?.physical_shrink
return size && typeof freed === 'number' && freed > 0 ? t('jobCompactedFreed', { place, freed: size(freed) }) : t('jobCompacted', { place })
}
}
}

Expand All @@ -236,6 +262,7 @@ export function kindWords(kind: JobKind, t: StorageT): string {
case 'move': return t('kindMove')
case 'backup': return t('kindBackup')
case 'restore': return t('kindRestore')
case 'compact': return t('kindCompact')
}
}

Expand Down
Loading