Skip to content

Commit

Permalink
Add latest cycle records data cache
Browse files Browse the repository at this point in the history
  • Loading branch information
tanuj-shardeum authored and jairajdev committed May 8, 2024
1 parent bff4eba commit b1ce45b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export function registerRoutes(server: FastifyInstance<Server, IncomingMessage,
return
}
if (count > MAX_CYCLES_PER_REQUEST) count = MAX_CYCLES_PER_REQUEST
const cycleInfo = await CycleDB.queryLatestCycleRecords(count)
const cycleInfo = await Cycles.getLatestCycleRecords(count)
const res = Crypto.sign({
cycleInfo,
})
Expand Down
6 changes: 6 additions & 0 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export interface Config {
MAX_CYCLES_PER_REQUEST: number
MAX_BETWEEN_CYCLES_PER_REQUEST: number
}
cycleRecordsCache: {
enabled: boolean
}
}

let config: Config = {
Expand Down Expand Up @@ -101,6 +104,9 @@ let config: Config = {
MAX_CYCLES_PER_REQUEST: 100,
MAX_BETWEEN_CYCLES_PER_REQUEST: 100,
},
cycleRecordsCache: {
enabled: false,
}
}
// Override default config params from config file, env vars, and cli args
export async function overrideDefaultConfig(file: string): Promise<void> {
Expand Down
3 changes: 2 additions & 1 deletion src/Data/Collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as Logger from '../Logger'
import { nestedCountersInstance } from '../profiler/nestedCounters'
import { profilerInstance } from '../profiler/profiler'
import { getCurrentCycleCounter, shardValuesByCycle, computeCycleMarker } from './Cycles'
import { bulkInsertCycles, Cycle as DbCycle, queryCycleByMarker, updateCycle } from '../dbstore/cycles'
import { bulkInsertCycles, queryCycleByMarker, updateCycle } from '../dbstore/cycles'
import * as State from '../State'
import * as Utils from '../Utils'
import { DataType, GossipData, adjacentArchivers, sendDataToAdjacentArchivers, TxData } from './GossipData'
Expand All @@ -28,6 +28,7 @@ import ShardFunction from '../ShardFunctions'
import { ConsensusNodeInfo } from '../NodeList'
import { verifyAccountHash } from '../shardeum/calculateAccountHash'
import { verifyAppReceiptData } from '../shardeum/verifyAppReceiptData'
import { Cycle as DbCycle } from '../dbstore/types'

export let storingAccountData = false
const processedReceiptsMap: Map<string, number> = new Map()
Expand Down
13 changes: 13 additions & 0 deletions src/Data/Cycles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { P2P as P2PTypes, StateManager } from '@shardus/types'
import { getJson, postJson } from '../P2P'
import { profilerInstance } from '../profiler/profiler'
import { nestedCountersInstance } from '../profiler/nestedCounters'
import * as cycleDataCache from '../cache/cycleRecordsCache'

import {
clearDataSenders,
dataSenders,
Expand All @@ -25,6 +27,8 @@ import { handleLostArchivers } from '../LostArchivers'
import ShardFunctions from '../ShardFunctions'
import { RequestDataType, queryFromArchivers } from '../API'
import { stringifyReduce } from '../profiler/StringifyReduce'
import { addCyclesToCache } from '../cache/cycleRecordsCache'
import { queryLatestCycleRecords } from '../dbstore/cycles'

interface ArchiverCycleResponse {
cycleInfo: P2PTypes.CycleCreatorTypes.CycleData[]
Expand Down Expand Up @@ -92,6 +96,7 @@ export async function processCycles(cycles: P2PTypes.CycleCreatorTypes.CycleData
cleanOldOriginalTxsMap(cleanupTimestamp)
cleanOldReceiptsMap(cleanupTimestamp)
}
await addCyclesToCache(cycles)
} finally {
if (profilerInstance) profilerInstance.profileSectionEnd('process_cycle', false)
}
Expand Down Expand Up @@ -534,3 +539,11 @@ function updateShardValues(cycle: P2PTypes.CycleCreatorTypes.CycleData): void {
shardValuesByCycle.delete(shardValuesByCycle.keys().next().value)
}
}

export async function getLatestCycleRecords(count: number): Promise<P2PTypes.CycleCreatorTypes.CycleData[]> {
if (config.cycleRecordsCache.enabled) {
return await cycleDataCache.getLatestCycleRecords(count)
}

return await queryLatestCycleRecords(count)
}
50 changes: 50 additions & 0 deletions src/cache/cycleRecordsCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { P2P } from '@shardus/types'
import { config } from '../Config'
import { queryLatestCycleRecords } from '../dbstore/cycles'

let cachedCycleRecords: P2P.CycleCreatorTypes.CycleData[] = []
let lastCacheUpdateFromDBRunning = false

async function updateCacheFromDB(): Promise<void> {
if (lastCacheUpdateFromDBRunning) {
return
}

lastCacheUpdateFromDBRunning = true

try {
cachedCycleRecords = await queryLatestCycleRecords(config.REQUEST_LIMIT.MAX_CYCLES_PER_REQUEST)
} catch (error) {
console.log('Error updating latest cache: ', error)
} finally {
lastCacheUpdateFromDBRunning = false
}
}

export async function addCyclesToCache(cycles: P2P.CycleCreatorTypes.CycleData[]): Promise<void> {
cycles.sort((a, b) => a.counter - b.counter);

if (cachedCycleRecords.length === 0) {
await updateCacheFromDB()
}

for (const cycle of cycles) {
if(cycle.counter < cachedCycleRecords[0].counter) {
continue
}

cachedCycleRecords.unshift(cycle)
}

if (cachedCycleRecords.length > config.REQUEST_LIMIT.MAX_CYCLES_PER_REQUEST) {
cachedCycleRecords.splice(config.REQUEST_LIMIT.MAX_CYCLES_PER_REQUEST)
}
}

export async function getLatestCycleRecords(count: number): Promise<P2P.CycleCreatorTypes.CycleData[]> {
if (cachedCycleRecords.length === 0) {
await updateCacheFromDB()
}

return cachedCycleRecords.slice(0, count)
}
13 changes: 2 additions & 11 deletions src/dbstore/cycles.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import * as db from './sqlite3storage'
import { extractValues, extractValuesFromArray } from './sqlite3storage'
import { P2P, StateManager } from '@shardus/types'
import { P2P } from '@shardus/types'
import * as Logger from '../Logger'
import { config } from '../Config'
import { DeSerializeFromJsonString, SerializeToJsonString } from '../utils/serialization'

export interface Cycle {
counter: P2P.CycleCreatorTypes.CycleData['counter']
cycleRecord: P2P.CycleCreatorTypes.CycleData
cycleMarker: StateManager.StateMetaDataTypes.CycleMarker
}

type DbCycle = Cycle & {
cycleRecord: string
}
import { Cycle, DbCycle } from './types'

export async function insertCycle(cycle: Cycle): Promise<void> {
try {
Expand Down
11 changes: 11 additions & 0 deletions src/dbstore/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { P2P, StateManager } from "@shardus/types"

export interface Cycle {
counter: P2P.CycleCreatorTypes.CycleData['counter']
cycleRecord: P2P.CycleCreatorTypes.CycleData
cycleMarker: StateManager.StateMetaDataTypes.CycleMarker
}

export type DbCycle = Cycle & {
cycleRecord: string
}

0 comments on commit b1ce45b

Please sign in to comment.