Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(renterd): move host scan API #820

Merged
merged 1 commit into from
Nov 22, 2024
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
7 changes: 7 additions & 0 deletions .changeset/brown-rings-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/renterd-js': minor
'@siafoundation/renterd-react': minor
'@siafoundation/renterd-types': minor
---

The host scanning API was moved from the worker to the bus. Closes https://github.com/SiaFoundation/renterd/issues/1644
9 changes: 5 additions & 4 deletions apps/renterd/components/Hosts/HostContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
useHostResetLostSectorCount,
useHostsAllowlist,
useHostsBlocklist,
useRhpScan,
useHostScan,
} from '@siafoundation/renterd-react'
import { routes } from '../../config/routes'
import { useRouter } from 'next/router'
Expand Down Expand Up @@ -86,7 +86,7 @@ export function HostContextMenuContent({
const allowlist = useHostsAllowlist()
const blocklistUpdate = useBlocklistUpdate()
const allowlistUpdate = useAllowlistUpdate()
const rescan = useRhpScan()
const rescan = useHostScan()
const resetLostSectors = useHostResetLostSectorCount()
return (
<>
Expand Down Expand Up @@ -164,9 +164,10 @@ export function HostContextMenuContent({
return
}
rescan.post({
params: {
hostkey: publicKey,
},
payload: {
hostKey: publicKey,
hostIP: address,
timeout: secondsInMilliseconds(30),
},
})
Expand Down
11 changes: 5 additions & 6 deletions apps/renterd/contexts/hosts/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { format, formatDistance, formatRelative } from 'date-fns'
import { HostContextMenu } from '../../components/Hosts/HostContextMenu'
import { useWorkflows } from '@siafoundation/react-core'
import {
RhpScanPayload,
workerRhpScanRoute,
HostScanPayload,
busHostHostKeyScanRoute,
} from '@siafoundation/renterd-types'
import { HostPriceTable, HostSettings } from '@siafoundation/types'
import { useHostsAllowlist } from '@siafoundation/renterd-react'
Expand Down Expand Up @@ -218,10 +218,9 @@ export const columns: HostsTableColumn[] = (
render: function LastScan({ data }) {
const { workflows } = useWorkflows()
const isPending = workflows.find((w) => {
const rhpw = w as { route?: string; payload?: RhpScanPayload }
return (
rhpw.route?.startsWith(workerRhpScanRoute) &&
rhpw.payload?.hostKey === data.publicKey
const rhpw = w as { route?: string; payload?: HostScanPayload }
return rhpw.route?.startsWith(
busHostHostKeyScanRoute.replace(':hostkey', data.publicKey)
)
})
if (isPending) {
Expand Down
13 changes: 7 additions & 6 deletions libs/react-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,19 @@ The lifecycle of imperative methods are tracked by a workflow ID. Workflows allo

```ts
// Hook
export function useRhpScan(
args?: HookArgsCallback<RhpScanParams, RhpScanPayload, RhpScanResponse>
export function useHostScan(
args?: HookArgsCallback<HostScanParams, HostScanPayload, HostScanResponse>
) {
return usePostFunc({ ...args, route: workerRhpScanRoute })
return usePostFunc({ ...args, route: busHostScanRoute })
}

// Usage
const rescan = useRhpScan()
const rescan = useHostScan()
await rescan.post({
params: {
hostkey: publicKey,
},
payload: {
hostKey: publicKey,
hostIP: address,
timeout: secondsInMilliseconds(30),
},
})
Expand Down
9 changes: 9 additions & 0 deletions libs/renterd-js/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ import {
ConsensusNetworkPayload,
ConsensusNetworkResponse,
busConsensusNetworkRoute,
HostScanParams,
HostScanPayload,
HostScanResponse,
busHostHostKeyScanRoute,
} from '@siafoundation/renterd-types'
import { buildRequestHandler, initAxios } from '@siafoundation/request'

Expand Down Expand Up @@ -393,6 +397,11 @@ export function Bus({ api, password }: { api: string; password?: string }) {
HostResetLostSectorCountPayload,
HostResetLostSectorCountResponse
>(axios, 'post', busHostPublicKeyResetlostsectorsRoute),
hostScan: buildRequestHandler<
HostScanParams,
HostScanPayload,
HostScanResponse
>(axios, 'post', busHostHostKeyScanRoute),
contracts: buildRequestHandler<
ContractsParams,
ContractsPayload,
Expand Down
9 changes: 0 additions & 9 deletions libs/renterd-js/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ import {
ObjectUploadParams,
ObjectUploadPayload,
ObjectUploadResponse,
RhpScanParams,
RhpScanPayload,
RhpScanResponse,
WorkerStateParams,
WorkerStatePayload,
WorkerStateResponse,
workerAccountIdResetdriftRoute,
workerMultipartKeyRoute,
workerObjectKeyRoute,
workerRhpScanRoute,
workerStateRoute,
} from '@siafoundation/renterd-types'
import { buildRequestHandler, initAxios } from '@siafoundation/request'
Expand Down Expand Up @@ -65,11 +61,6 @@ export function Worker({ api, password }: { api: string; password?: string }) {
},
},
}),
rhpScan: buildRequestHandler<
RhpScanParams,
RhpScanPayload,
RhpScanResponse
>(axios, 'post', workerRhpScanRoute),
accountResetDrift: buildRequestHandler<
AccountResetDriftParams,
AccountResetDriftPayload,
Expand Down
55 changes: 55 additions & 0 deletions libs/renterd-react/src/bus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import useSWR from 'swr'
import { debounce } from '@technically/lodash'
import {
useDeleteFunc,
useGetSwr,
Expand Down Expand Up @@ -244,6 +245,11 @@ import {
ConsensusNetworkParams,
ConsensusNetworkResponse,
busConsensusNetworkRoute,
HostScanParams,
HostScanPayload,
HostScanResponse,
busHostHostKeyScanRoute,
Host,
} from '@siafoundation/renterd-types'

// state
Expand Down Expand Up @@ -464,6 +470,8 @@ export function useWalletPrepareRenew(
return usePostFunc({ ...args, route: busWalletPrepareRenewRoute })
}

// host

export function useHosts(
args: HookArgsWithPayloadSwr<HostsParams, HostsPayload, HostsResponse>
) {
Expand Down Expand Up @@ -559,6 +567,53 @@ export function useHostResetLostSectorCount(
})
}

const debouncedListRevalidate = debounce((func: () => void) => func(), 5_000)

export function useHostScan(
args?: HookArgsCallback<HostScanParams, HostScanPayload, HostScanResponse>
) {
return usePostFunc(
{ ...args, route: busHostHostKeyScanRoute },
async (mutate, { params: { hostkey } }, response) => {
// Fetching immediately after the response returns stale data so
// we optimistically update without triggering revalidation,
// and then revalidate after a 5s delay. The list revalidation
// is debounced so if the user rescans multiple hosts in quick
// succession the list is optimistically updated n times followed
// by a single network revalidate.
mutate<Host[]>(
(key) => key.startsWith(busHostsRoute),
(data) =>
data?.map((h) => {
if (h.publicKey === hostkey) {
return {
...h,
host: {
...h,
interactions: {
...h.interactions,
lastScan: new Date().toISOString(),
lastScanSuccess: !response.data.scanError,
},
settings: response.data.settings,
},
}
}
return h
}),
false
)
debouncedListRevalidate(() => {
mutate(
(key) => key.startsWith(busHostsRoute),
(d) => d,
true
)
})
}
)
}

// contracts

export function useContracts(
Expand Down
54 changes: 0 additions & 54 deletions libs/renterd-react/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { debounce } from '@technically/lodash'
import {
useGetDownloadFunc,
usePutFunc,
Expand All @@ -20,19 +19,13 @@ import {
ObjectUploadParams,
ObjectUploadPayload,
ObjectUploadResponse,
RhpScanParams,
RhpScanPayload,
RhpScanResponse,
WorkerStateParams,
WorkerStateResponse,
busObjectsRoute,
busHostsRoute,
workerAccountIdResetdriftRoute,
workerMultipartKeyRoute,
workerObjectKeyRoute,
workerRhpScanRoute,
workerStateRoute,
Host,
} from '@siafoundation/renterd-types'

// state
Expand Down Expand Up @@ -105,53 +98,6 @@ export function useMultipartUploadPart(
})
}

const debouncedListRevalidate = debounce((func: () => void) => func(), 5_000)

export function useRhpScan(
args?: HookArgsCallback<RhpScanParams, RhpScanPayload, RhpScanResponse>
) {
return usePostFunc(
{ ...args, route: workerRhpScanRoute },
async (mutate, { payload: { hostKey } }, response) => {
// Fetching immediately after the response returns stale data so
// we optimistically update without triggering revalidation,
// and then revalidate after a 5s delay. The list revalidation
// is debounced so if the user rescans multiple hosts in quick
// succession the list is optimistically updated n times followed
// by a single network revalidate.
mutate<Host[]>(
(key) => key.startsWith(busHostsRoute),
(data) =>
data?.map((h) => {
if (h.publicKey === hostKey) {
return {
...h,
host: {
...h,
interactions: {
...h.interactions,
lastScan: new Date().toISOString(),
lastScanSuccess: !response.data.scanError,
},
settings: response.data.settings,
},
}
}
return h
}),
false
)
debouncedListRevalidate(() => {
mutate(
(key) => key.startsWith(busHostsRoute),
(d) => d,
true
)
})
}
)
}

// accounts

export function useAccountResetDrift(
Expand Down
15 changes: 15 additions & 0 deletions libs/renterd-types/src/bus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Block,
Currency,
HostPriceTable,
HostSettings,
FileContractID,
FileContractRevision,
Expand Down Expand Up @@ -44,6 +45,7 @@ export const busWalletPrepareFormRoute = '/bus/wallet/prepare/form'
export const busWalletPrepareRenewRoute = '/bus/wallet/prepare/renew'
export const busHostsRoute = '/bus/hosts'
export const busHostHostKeyRoute = '/bus/host/:hostkey'
export const busHostHostKeyScanRoute = '/bus/host/:hostkey/scan'
export const busHostsHostKeyRoute = '/bus/hosts/:hostkey'
export const busHostsBlocklistRoute = '/bus/hosts/blocklist'
export const busHostsAllowlistRoute = '/bus/hosts/allowlist'
Expand Down Expand Up @@ -282,6 +284,19 @@ export type HostResetLostSectorCountParams = {
export type HostResetLostSectorCountPayload = void
export type HostResetLostSectorCountResponse = void

export type HostScanParams = {
hostkey: string
}
export type HostScanPayload = {
timeout: number
}
export type HostScanResponse = {
ping: string
scanError?: string
settings?: HostSettings
priceTable?: HostPriceTable
}

// contracts

export type ContractsParams = void
Expand Down
16 changes: 0 additions & 16 deletions libs/renterd-types/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { HostSettings } from '@siafoundation/types'
import { BusStateResponse } from './bus'

export const workerStateRoute = '/worker/state'
export const workerObjectKeyRoute = '/worker/object/:key'
export const workerMultipartKeyRoute = '/worker/multipart/:key'
export const workerRhpScanRoute = '/worker/rhp/scan'
export const workerAccountIdResetdriftRoute = '/worker/account/:id/resetdrift'

// state
Expand Down Expand Up @@ -45,20 +43,6 @@ export type MultipartUploadPartParams = {
export type MultipartUploadPartPayload = Blob | Buffer | ArrayBuffer | string
export type MultipartUploadPartResponse = void

// rhp

export type RhpScanParams = void
export type RhpScanPayload = {
hostKey: string
hostIP: string
timeout: number
}
export type RhpScanResponse = {
ping: string
scanError?: string
settings?: HostSettings
}

// accounts

export type AccountResetDriftParams = { id: string }
Expand Down
Loading