From b69bd82a72c051a5685a4338cbf295d2eea1ab09 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sat, 23 Apr 2016 19:53:18 -0700 Subject: [PATCH] Add DualClusterClient for live testing phase of backend/frontend split --- index.js | 12 ++++++++++++ src/backend/backendmodule.js | 19 ++++++++++++++++++- src/io/cluster/dualclusterclient.js | 27 +++++++++++++++++++++++++++ src/io/cluster/redisclusterclient.js | 7 ++++++- src/server.js | 5 +++++ src/switches.js | 11 +++++++++++ 6 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 src/io/cluster/dualclusterclient.js create mode 100644 src/switches.js diff --git a/index.js b/index.js index 05d3aeeb..fb8e3cc0 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,7 @@ try { } var Config = require("./lib/config"); var Logger = require("./lib/logger"); +const Switches = require("./lib/switches"); require("source-map-support").install(); Config.load("config.yaml"); @@ -61,5 +62,16 @@ function handleLine(line) { Logger.syslog.log("Deleted old channel tables"); } }); + } else if (line.indexOf("/switch") === 0) { + var args = line.split(" "); + args.shift(); + if (args.length === 1) { + Logger.syslog.log("Switch " + args[0] + " is " + + (Switches.isActive(args[0]) ? "ON" : "OFF")); + } else if (args.length === 2) { + Switches.setActive(args[0], args[1].toLowerCase() === "on" ? true : false); + Logger.syslog.log("Switch " + args[0] + " is now " + + (Switches.isActive(args[0]) ? "ON" : "OFF")); + } } } diff --git a/src/backend/backendmodule.js b/src/backend/backendmodule.js index dcca65ea..2fcb7ee7 100644 --- a/src/backend/backendmodule.js +++ b/src/backend/backendmodule.js @@ -1,4 +1,6 @@ import { RedisClusterClient } from '../io/cluster/redisclusterclient'; +import { DualClusterClient } from '../io/cluster/dualclusterclient'; +import NullClusterClient from '../io/cluster/nullclusterclient'; import { FrontendPool } from 'cytube-common/lib/redis/frontendpool'; import RedisClientProvider from 'cytube-common/lib/redis/redisclientprovider'; import { loadFromToml } from 'cytube-common/lib/configuration/configloader'; @@ -6,6 +8,9 @@ import path from 'path'; import { BackendConfiguration } from './backendconfiguration'; import logger from 'cytube-common/lib/logger'; import redisAdapter from 'socket.io-redis'; +import LegacyConfig from '../config'; +import IOConfiguration from '../configuration/ioconfig'; +import * as Switches from '../switches'; const BACKEND_CONFIG_PATH = path.resolve(__dirname, '..', '..', 'backend.toml'); @@ -67,7 +72,19 @@ class BackendModule { this.redisClusterClient = new RedisClusterClient(this.getFrontendPool()); } - return this.redisClusterClient; + if (Switches.isActive(Switches.DUAL_BACKEND) && !this.nullClusterClient) { + this.nullClusterClient = new NullClusterClient( + IOConfiguration.fromOldConfig(LegacyConfig)); + } + + if (Switches.isActive(Switches.DUAL_BACKEND)) { + this.clusterClient = new DualClusterClient(this.nullClusterClient, + this.redisClusterClient); + } else { + this.clusterClient = this.redisClusterClient; + } + + return this.clusterClient; } } diff --git a/src/io/cluster/dualclusterclient.js b/src/io/cluster/dualclusterclient.js new file mode 100644 index 00000000..e36c31c9 --- /dev/null +++ b/src/io/cluster/dualclusterclient.js @@ -0,0 +1,27 @@ +import logger from 'cytube-common/lib/logger'; +import * as Switches from '../../switches'; + +class DualClusterClient { + constructor(authoritativeClient, altClient) { + this.authoritativeClient = authoritativeClient; + this.altClient = altClient; + } + + getSocketConfig(channel) { + return this.authoritativeClient.getSocketConfig(channel).then(result => { + if (!Switches.isActive(Switches.DUAL_BACKEND)) { + return result; + } + + return this.altClient.getSocketConfig(channel).then(altResult => { + result.alt = altResult.servers; + return result; + }).catch(error => { + logger.warn(`Error loading alt servers: ${error}`); + return result; + }); + }) + } +} + +export { DualClusterClient }; diff --git a/src/io/cluster/redisclusterclient.js b/src/io/cluster/redisclusterclient.js index b4c7eb83..0725b0d6 100644 --- a/src/io/cluster/redisclusterclient.js +++ b/src/io/cluster/redisclusterclient.js @@ -1,3 +1,8 @@ +import Promise from 'bluebird'; + +const ONE_SECOND = 1000; +const ERR_TIMEOUT = 'Timed out when retrieving server information'; + class RedisClusterClient { constructor(frontendPool) { this.frontendPool = frontendPool; @@ -10,7 +15,7 @@ class RedisClusterClient { } return { servers: result }; - }); + }).timeout(ONE_SECOND, ERR_TIMEOUT); } } diff --git a/src/server.js b/src/server.js index fbd23186..9b90d931 100644 --- a/src/server.js +++ b/src/server.js @@ -48,6 +48,8 @@ import WebConfiguration from './configuration/webconfig'; import NullClusterClient from './io/cluster/nullclusterclient'; import session from './session'; import { LegacyModule } from './legacymodule'; +import * as Switches from './switches'; +console.log(Switches); var Server = function () { var self = this; @@ -62,6 +64,9 @@ var Server = function () { // backend init var initModule; if (Config.get("new-backend")) { + if (Config.get("dual-backend")) { + Switches.setActive(Switches.DUAL_BACKEND, true); + } const BackendModule = require('./backend/backendmodule').BackendModule; initModule = new BackendModule(); } else { diff --git a/src/switches.js b/src/switches.js new file mode 100644 index 00000000..53b57fb1 --- /dev/null +++ b/src/switches.js @@ -0,0 +1,11 @@ +const switches = {}; + +export function isActive(switchName) { + return switches.hasOwnProperty(switchName) && switches[switchName] === true; +} + +export function setActive(switchName, active) { + switches[switchName] = active; +} + +export const DUAL_BACKEND = 'DUAL_BACKEND';