Skip to content

Commit

Permalink
(incident 2024-05-05): enable redis testing with clustering mode (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored May 5, 2024
1 parent 6c2a278 commit 3f07e43
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 18 deletions.
2 changes: 2 additions & 0 deletions servers/fdr-deploy/bin/fdr-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async function main() {
desiredTaskCount: 2,
maxTaskCount: 4,
redis: true,
redisClusteringModeEnabled: false,
memory: 1024,
cpu: 512,
},
Expand All @@ -50,6 +51,7 @@ async function main() {
maxTaskCount: 1,
redis: false,
memory: 8192,
redisClusteringModeEnabled: false,
cpu: 4096,
},
{
Expand Down
2 changes: 2 additions & 0 deletions servers/fdr-deploy/scripts/fdr-deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface ElastiCacheProps {

interface FdrStackOptions {
redis: boolean;
redisClusteringModeEnabled: boolean;
maxTaskCount: number;
desiredTaskCount: number;
cpu: number;
Expand Down Expand Up @@ -145,6 +146,7 @@ export class FdrDeployStack extends Stack {
DOCS_CACHE_ENDPOINT: fernDocsCacheEndpoint,
ENABLE_CUSTOMER_NOTIFICATIONS: (environmentType === "PROD").toString(),
REDIS_ENABLED: options.redis.toString(),
REDIS_CLUSTERING_MODE_ENABLED: options.redisClusteringModeEnabled.toString(),
APPLICATION_ENVIRONMENT: getEnvironmentVariableOrThrow("APPLICATION_ENVIRONMENT"),
},
containerName: CONTAINER_NAME,
Expand Down
63 changes: 59 additions & 4 deletions servers/fdr/docker-compose.ete.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ services:
ALGOLIA_SEARCH_INDEX: dummy
ALGOLIA_SEARCH_API_KEY: dummy
SLACK_TOKEN: dummy
DOCS_CACHE_ENDPOINT: redis:6379
DOCS_CACHE_ENDPOINT: redis_0:6379
ENABLE_CUSTOMER_NOTIFICATIONS: "false"
REDIS_ENABLED: "true"
REDIS_CLUSTERING_ENABLED: "true"
APPLICATION_ENVIRONMENT: ete
depends_on:
- redis_0
- redis_1
- redis_2
- redis_3
- redis_4
- redis_5
postgres:
image: postgres:10.3
restart: always
Expand All @@ -35,8 +43,55 @@ services:
- 9191:9191
environment:
initialBuckets: fdr
redis:
image: redis

redis_0:
image: bitnami/redis-cluster:latest
restart: unless-stopped
ports:
- 6379:6379
command: ["redis-server"]
environment:
ALLOW_EMPTY_PASSWORD: "yes"
REDIS_CLUSTER_CREATOR: "yes"
REDIS_CLUSTER_REPLICAS: 1
REDIS_NODES: redis_0 redis_1 redis_2 redis_3 redis_4 redis_5
depends_on:
- redis_1
- redis_2
- redis_3
- redis_4
- redis_5

redis_1:
image: bitnami/redis-cluster:latest
restart: unless-stopped
environment:
ALLOW_EMPTY_PASSWORD: "yes"
REDIS_NODES: redis_0 redis_1 redis_2 redis_3 redis_4 redis_5

redis_2:
image: bitnami/redis-cluster:latest
restart: unless-stopped
environment:
ALLOW_EMPTY_PASSWORD: "yes"
REDIS_NODES: redis_0 redis_1 redis_2 redis_3 redis_4 redis_5

redis_3:
image: bitnami/redis-cluster:latest
restart: unless-stopped
environment:
ALLOW_EMPTY_PASSWORD: "yes"
REDIS_NODES: redis_0 redis_1 redis_2 redis_3 redis_4 redis_5

redis_4:
image: bitnami/redis-cluster:latest
restart: unless-stopped
environment:
ALLOW_EMPTY_PASSWORD: "yes"
REDIS_NODES: redis_0 redis_1 redis_2 redis_3 redis_4 redis_5

redis_5:
image: bitnami/redis-cluster:latest
restart: unless-stopped
environment:
ALLOW_EMPTY_PASSWORD: "yes"
REDIS_NODES: redis_0 redis_1 redis_2 redis_3 redis_4 redis_5
2 changes: 1 addition & 1 deletion servers/fdr/docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
environment:
initialBuckets: fdr
redis:
image: redis:latest
image: redis
ports:
- 6379:6379
command: ["redis-server"]
2 changes: 2 additions & 0 deletions servers/fdr/redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
port 6379
cluster-enabled yes
1 change: 1 addition & 0 deletions servers/fdr/src/__test__/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const baseMockFdrConfig: FdrConfig = {
enableCustomerNotifications: false,
applicationEnvironment: "mock",
redisEnabled: false,
redisClusteringEnabled: false,
};

export function getMockFdrConfig(overrides?: Partial<FdrConfig>): FdrConfig {
Expand Down
5 changes: 4 additions & 1 deletion servers/fdr/src/app/FdrApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export class FdrApplication {
this.dao = new FdrDao(prisma);

this.redisDatastore = config.redisEnabled
? new RedisDocsDefinitionStore(`redis://${this.config.docsCacheEndpoint}`)
? new RedisDocsDefinitionStore({
cacheEndpointUrl: `redis://${this.config.docsCacheEndpoint}`,
clusterModeEnabled: config.redisClusteringEnabled,
})
: undefined;

this.docsDefinitionCache = new DocsDefinitionCacheImpl(
Expand Down
3 changes: 3 additions & 0 deletions servers/fdr/src/app/FdrConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const LOG_LEVEL_ENV_VAR = "LOG_LEVEL";
const DOCS_CACHE_ENDPOINT_ENV_VAR = "DOCS_CACHE_ENDPOINT";
const ENABLE_CUSTOMER_NOTIFICATIONS_ENV_VAR = "ENABLE_CUSTOMER_NOTIFICATIONS";
const REDIS_ENABLED_ENV_VAR = "REDIS_ENABLED";
const REDIS_CLUSTERING_ENABLED_ENV_VAR = "REDIS_CLUSTERING_ENABLED";
const APPLICATION_ENVIRONMENT_ENV_VAR = "APPLICATION_ENVIRONMENT";

export interface FdrConfig {
Expand All @@ -33,6 +34,7 @@ export interface FdrConfig {
docsCacheEndpoint: string;
enableCustomerNotifications: boolean;
redisEnabled: boolean;
redisClusteringEnabled: boolean;
applicationEnvironment: string;
}

Expand All @@ -54,6 +56,7 @@ export function getConfig(): FdrConfig {
docsCacheEndpoint: getEnvironmentVariableOrThrow(DOCS_CACHE_ENDPOINT_ENV_VAR),
enableCustomerNotifications: getEnvironmentVariableOrThrow(ENABLE_CUSTOMER_NOTIFICATIONS_ENV_VAR) === "true",
redisEnabled: process.env[REDIS_ENABLED_ENV_VAR] === "true",
redisClusteringEnabled: process.env[REDIS_CLUSTERING_ENABLED_ENV_VAR] === "true",
applicationEnvironment: getEnvironmentVariableOrThrow(APPLICATION_ENVIRONMENT_ENV_VAR),
};
}
Expand Down
36 changes: 24 additions & 12 deletions servers/fdr/src/services/docs-cache/RedisDocsDefinitionStore.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import { createClient, RedisClientType } from "redis";
import { RedisClientType, RedisClusterType, createClient, createCluster } from "redis";
import { LOGGER } from "../../app/FdrApplication";
import { CachedDocsResponse } from "./DocsDefinitionCache";

export declare namespace RedisDocsDefinitionStore {
interface Args {
clusterModeEnabled: boolean;
cacheEndpointUrl: string;
}
}

export default class RedisDocsDefinitionStore {
private client: RedisClientType;
private client: RedisClusterType | RedisClientType;

public constructor(cacheEndpointUrl: string) {
this.client = createClient({ url: cacheEndpointUrl, pingInterval: 10000 });
public constructor({ cacheEndpointUrl, clusterModeEnabled }: RedisDocsDefinitionStore.Args) {
this.client = clusterModeEnabled
? createCluster({
rootNodes: [
{
url: cacheEndpointUrl,
},
],
defaults: {
pingInterval: 10000,
},
})
: createClient({ url: cacheEndpointUrl, pingInterval: 10000 });
this.client.on("error", (error) => LOGGER.error("Encountered error from redis", error));
}

public async initializeCache(): Promise<void> {
this.client.on("error", (err) => {
LOGGER.error(`Supressed Redis client error: ${err}`);
});
try {
await this.client.connect();
} catch (err) {
LOGGER.error(`Supressed Redis client error: ${err}`);
}
await this.client.connect();
}

public async get({ url }: { url: URL }): Promise<CachedDocsResponse | null> {
Expand Down

0 comments on commit 3f07e43

Please sign in to comment.