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
3 changes: 2 additions & 1 deletion servers/cu/src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ export const createApis = async (ctx) => {
logger: dryRunLogger
}),
DRY_RUN_DEFAULT_MAX_PROCESS_AGE: ctx.DRY_RUN_DEFAULT_MAX_PROCESS_AGE,
DRY_RUN_PROCESS_CACHE_TTL: ctx.DRY_RUN_PROCESS_CACHE_TTL
DRY_RUN_PROCESS_CACHE_TTL: ctx.DRY_RUN_PROCESS_CACHE_TTL,
DRY_RUN_RESULT_MAX_AGE: ctx.DRY_RUN_RESULT_MAX_AGE
})

/**
Expand Down
2 changes: 2 additions & 0 deletions servers/cu/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ const CONFIG_ENVS = {
HYDRATION_MODE_NONCE_LIMIT: process.env.HYDRATION_MODE_NONCE_LIMIT || 200,
DRY_RUN_DEFAULT_MAX_PROCESS_AGE: process.env.DRY_RUN_DEFAULT_MAX_PROCESS_AGE || 100,
DRY_RUN_PROCESS_CACHE_TTL: process.env.DRY_RUN_PROCESS_CACHE_TTL || 2000,
DRY_RUN_RESULT_MAX_AGE: process.env.DRY_RUN_RESULT_MAX_AGE || 60000,
LOAD_MESSAGES_PAGE_SIZE: process.env.LOAD_MESSAGES_PAGE_SIZE || 1000
},
production: {
Expand Down Expand Up @@ -244,6 +245,7 @@ const CONFIG_ENVS = {
HYDRATION_MODE_NONCE_LIMIT: process.env.HYDRATION_MODE_NONCE_LIMIT || 200,
DRY_RUN_DEFAULT_MAX_PROCESS_AGE: process.env.DRY_RUN_DEFAULT_MAX_PROCESS_AGE || 100,
DRY_RUN_PROCESS_CACHE_TTL: process.env.DRY_RUN_PROCESS_CACHE_TTL || 2000,
DRY_RUN_RESULT_MAX_AGE: process.env.DRY_RUN_RESULT_MAX_AGE || 60000,
LOAD_MESSAGES_PAGE_SIZE: process.env.LOAD_MESSAGES_PAGE_SIZE || 1000
}
}
Expand Down
33 changes: 32 additions & 1 deletion servers/cu/src/domain/api/dryRun.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ const TtlCache = ({ setTimeout, clearTimeout }) => {
return cache
}

const cyrb53 = (str, seed = 0) => {
let h1 = 0xdeadbeef ^ seed; let h2 = 0x41c6ce57 ^ seed
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i)
h1 = Math.imul(h1 ^ ch, 2654435761)
h2 = Math.imul(h2 ^ ch, 1597334677)
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507)
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909)
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507)
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909)
return 4294967296 * (2097151 & h2) + (h1 >>> 0)
}

/**
* @typedef Env
*
Expand All @@ -58,6 +72,7 @@ const TtlCache = ({ setTimeout, clearTimeout }) => {
*/
export function dryRunWith (env) {
const DRY_RUN_DEFAULT_MAX_PROCESS_AGE = env.DRY_RUN_DEFAULT_MAX_PROCESS_AGE
const DRY_RUN_RESULT_MAX_AGE = env.DRY_RUN_RESULT_MAX_AGE
const DRY_RUN_PROCESS_CACHE_TTL = env.DRY_RUN_PROCESS_CACHE_TTL
const logger = env.logger
const loadMessageMeta = loadMessageMetaWith(env)
Expand All @@ -73,6 +88,7 @@ export function dryRunWith (env) {
})

const readStateCache = TtlCache(env)
const dryRunResultCache = TtlCache(env)

function loadMessageCtx ({ messageTxId, processId }) {
/**
Expand Down Expand Up @@ -159,6 +175,16 @@ export function dryRunWith (env) {
}

return ({ processId, messageTxId, maxProcessAge = DRY_RUN_DEFAULT_MAX_PROCESS_AGE, dryRun }) => {
const dryRunHash = cyrb53(JSON.stringify(dryRun))
const cached = dryRunResultCache.get(dryRunHash)
if (cached && new Date().getTime() - cached.age <= DRY_RUN_RESULT_MAX_AGE) {
logger.debug(
'Using recently cached dry-run result for dry-run to process "%s"',
processId
)
return Resolved(cached.ctx)
}

return of({ processId, messageTxId })
.chain(loadMessageCtx)
.chain(ensureProcessLoaded({ maxProcessAge }))
Expand Down Expand Up @@ -222,6 +248,11 @@ export function dryRunWith (env) {
*/
return evaluate({ ...ctx, dryRun: true, messages: Readable.from(dryRunMessage()) })
})
.map((res) => omit(['Memory'], res.output))
.map((res) => {
const omitted = omit(['Memory'], res.output)
const cached = { age: new Date().getTime(), ctx: omitted }
dryRunResultCache.set(dryRunHash, cached, DRY_RUN_RESULT_MAX_AGE)
return omitted
})
}
}
4 changes: 4 additions & 0 deletions servers/cu/src/domain/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ export const domainConfigSchema = z.object({
* The TTL of the dry run process cache in milliseconds.
*/
DRY_RUN_PROCESS_CACHE_TTL: positiveIntSchema,
/*
* Max time to cache a dry run result
*/
DRY_RUN_RESULT_MAX_AGE: positiveIntSchema,
/**
* The size of the page to load when fetching messages from the AO SU.
*/
Expand Down