-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Redis cache to the REST API (#8187)
* Add a Redis cache to `/api/v1/transactions` * Add Redis configuration to REST chart * Remove temporary snyk Gradle plugin workaround --------- Signed-off-by: Steven Sheehy <steven.sheehy@swirldslabs.com>
- Loading branch information
1 parent
f70bc44
commit 12f1570
Showing
12 changed files
with
445 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ hedera: | |
rest: | ||
metrics: | ||
enabled: false | ||
redis: | ||
enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import config from '../config'; | ||
import {Cache} from '../cache'; | ||
import {RedisContainer} from '@testcontainers/redis'; | ||
import {defaultBeforeAllTimeoutMillis} from './integrationUtils.js'; | ||
|
||
let cache; | ||
let redisContainer; | ||
|
||
beforeAll(async () => { | ||
config.redis.enabled = true; | ||
redisContainer = await new RedisContainer().withStartupTimeout(20000).start(); | ||
logger.info('Started Redis container'); | ||
}, defaultBeforeAllTimeoutMillis); | ||
|
||
afterAll(async () => { | ||
await redisContainer.stop({signal: 'SIGKILL', t: 5}); | ||
logger.info('Stopped Redis container'); | ||
}); | ||
|
||
beforeEach(async () => { | ||
config.redis.uri = `0.0.0.0:${redisContainer.getMappedPort(6379)}`; | ||
cache = new Cache(); | ||
await cache.clear(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await cache.stop(); | ||
}); | ||
|
||
const loader = (keys) => keys.map((key) => `v${key}`); | ||
const keyMapper = (key) => `k${key}`; | ||
|
||
describe('get', () => { | ||
test('All keys from database', async () => { | ||
const values = await cache.get(['1', '2', '3'], loader, keyMapper); | ||
expect(values).toEqual(['v1', 'v2', 'v3']); | ||
}); | ||
|
||
test('Some keys from database', async () => { | ||
const values = await cache.get(['1', '2', '3'], loader, keyMapper); | ||
expect(values).toEqual(['v1', 'v2', 'v3']); | ||
|
||
const newValues = await cache.get(['2', '3', '4'], loader, keyMapper); | ||
expect(newValues).toEqual(['v2', 'v3', 'v4']); | ||
}); | ||
|
||
test('No keys from database', async () => { | ||
const values = await cache.get(['1', '2', '3'], loader, keyMapper); | ||
expect(values).toEqual(['v1', 'v2', 'v3']); | ||
|
||
const newValues = await cache.get(['1', '2', '3'], (k) => [], keyMapper); | ||
expect(newValues).toEqual(['v1', 'v2', 'v3']); | ||
}); | ||
|
||
test('Disabled', async () => { | ||
config.redis.enabled = false; | ||
const values = await cache.get(['1', '2', '3'], loader, keyMapper); | ||
expect(values).toEqual(['v1', 'v2', 'v3']); | ||
}); | ||
|
||
test('Unable to connect', async () => { | ||
config.redis.uri = 'redis://invalid:6379'; | ||
cache = new Cache(); | ||
const values = await cache.get(['1', '2', '3'], loader, keyMapper); | ||
expect(values).toEqual(['v1', 'v2', 'v3']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Redis from 'ioredis'; | ||
import config from './config'; | ||
import _ from 'lodash'; | ||
import {JSONParse, JSONStringify} from './utils.js'; | ||
|
||
export class Cache { | ||
constructor() { | ||
const enabled = config?.redis?.enabled; | ||
const uri = config?.redis?.uri; | ||
const uriSanitized = uri.replaceAll(RegExp('(?<=//).*:.+@', 'g'), '***:***@'); | ||
this.ready = false; | ||
|
||
this.redis = new Redis(uri, { | ||
commandTimeout: config?.redis?.commandTimeout, | ||
connectTimeout: config?.redis?.connectTimeout, | ||
enableAutoPipelining: true, | ||
enableOfflineQueue: true, | ||
enableReadyCheck: true, | ||
keepAlive: 30000, | ||
lazyConnect: !enabled, | ||
maxRetriesPerRequest: config?.redis?.maxRetriesPerRequest, | ||
retryStrategy: (attempt) => { | ||
this.ready = false; | ||
|
||
if (!enabled) { | ||
return null; | ||
} | ||
|
||
return Math.min(attempt * 2000, config?.redis?.maxBackoff); | ||
}, | ||
}); | ||
|
||
this.redis.on('connect', () => logger.info(`Connected to ${uriSanitized}`)); | ||
this.redis.on('error', (err) => logger.error(`Error connecting to ${uriSanitized}: ${err.message}`)); | ||
this.redis.on('ready', () => { | ||
this.#setConfig('maxmemory', config?.redis?.maxMemory); | ||
this.#setConfig('maxmemory-policy', config?.redis?.maxMemoryPolicy); | ||
this.ready = true; | ||
}); | ||
} | ||
|
||
#setConfig(key, value) { | ||
this.redis | ||
.config('SET', key, value) | ||
.catch((e) => logger.warn(`Unable to set Redis ${key} to ${value}: ${e.message}`)); | ||
} | ||
|
||
async clear() { | ||
return this.redis.flushall(); | ||
} | ||
|
||
async get(keys, loader, keyMapper = (k) => (k ? k.toString() : k)) { | ||
if (!this.ready) { | ||
return loader(keys); | ||
} | ||
|
||
const buffers = | ||
(await this.redis | ||
.mgetBuffer(_.map(keys, keyMapper)) | ||
.catch((err) => logger.warn(`Redis error during mget: ${err.message}`))) || new Array(keys.length); | ||
const values = buffers.map((t) => JSONParse(t)); | ||
|
||
let i = 0; | ||
const missingKeys = keys.filter(() => _.isNil(values[i++])); | ||
|
||
if (missingKeys.length > 0) { | ||
const missing = await loader(missingKeys); | ||
const newValues = []; | ||
let j = 0; | ||
|
||
missing.forEach((value) => { | ||
// Update missing values in Redis array | ||
for (; j < values.length; j++) { | ||
if (_.isNil(values[j])) { | ||
values[j] = value; | ||
newValues.push(keyMapper(keys[j])); | ||
newValues.push(JSONStringify(value)); | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
this.redis.mset(newValues).catch((err) => logger.warn(`Redis error during mset: ${err.message}`)); | ||
} | ||
|
||
if (logger.isDebugEnabled()) { | ||
const count = keys.length - missingKeys.length; | ||
logger.debug(`Redis returned ${count} of ${keys.length} keys`); | ||
} | ||
|
||
return values; | ||
} | ||
|
||
async stop() { | ||
return this.redis.quit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.