mirror of https://github.com/calzoneman/sync.git
Work on auto reloading partition map from redis
This commit is contained in:
parent
3c11ac6cf5
commit
d2cce4f166
|
@ -44,6 +44,7 @@
|
||||||
"socket.io-redis": "^1.0.0",
|
"socket.io-redis": "^1.0.0",
|
||||||
"source-map-support": "^0.4.0",
|
"source-map-support": "^0.4.0",
|
||||||
"status-message-polyfill": "git://github.com/calzoneman/status-message-polyfill",
|
"status-message-polyfill": "git://github.com/calzoneman/status-message-polyfill",
|
||||||
|
"toml": "^2.3.0",
|
||||||
"uuid": "^2.0.1",
|
"uuid": "^2.0.1",
|
||||||
"yamljs": "^0.1.6"
|
"yamljs": "^0.1.6"
|
||||||
},
|
},
|
||||||
|
|
|
@ -3,18 +3,6 @@ class PartitionConfig {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPartitionMap() {
|
|
||||||
return this.config.partitions;
|
|
||||||
}
|
|
||||||
|
|
||||||
getOverrideMap() {
|
|
||||||
return this.config.overrides;
|
|
||||||
}
|
|
||||||
|
|
||||||
getPool() {
|
|
||||||
return this.config.pool;
|
|
||||||
}
|
|
||||||
|
|
||||||
getIdentity() {
|
getIdentity() {
|
||||||
return this.config.identity;
|
return this.config.identity;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,26 @@
|
||||||
import { murmurHash1 } from '../util/murmur';
|
import { murmurHash1 } from '../util/murmur';
|
||||||
|
|
||||||
class PartitionDecider {
|
class PartitionDecider {
|
||||||
constructor(config) {
|
constructor(config, partitionMap) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
this.partitionMap = partitionMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
getPartitionForChannel(channel) {
|
getPartitionForChannel(channel) {
|
||||||
const partitionMap = this.config.getPartitionMap();
|
return this.partitionMap.getPartitions()[this.getPartitionIdentityForChannel(channel)];
|
||||||
return partitionMap[this.getPartitionIdentityForChannel(channel)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getPartitionIdentityForChannel(channel) {
|
getPartitionIdentityForChannel(channel) {
|
||||||
channel = channel.toLowerCase();
|
channel = channel.toLowerCase();
|
||||||
const overrideMap = this.config.getOverrideMap();
|
const overrideMap = this.partitionMap.getOverrides();
|
||||||
if (overrideMap.hasOwnProperty(channel)) {
|
if (overrideMap.hasOwnProperty(channel)) {
|
||||||
return overrideMap[channel];
|
return overrideMap[channel];
|
||||||
} else {
|
} else if (this.partitionMap.getPool().length > 0) {
|
||||||
const pool = this.config.getPool();
|
const pool = this.partitionMap.getPool();
|
||||||
const i = murmurHash1(channel) % pool.length;
|
const i = murmurHash1(channel) % pool.length;
|
||||||
return pool[i];
|
return pool[i];
|
||||||
|
} else {
|
||||||
|
return { servers: [] };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,6 +28,10 @@ class PartitionDecider {
|
||||||
return this.getPartitionIdentityForChannel(channel) ===
|
return this.getPartitionIdentityForChannel(channel) ===
|
||||||
this.config.getIdentity();
|
this.config.getIdentity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setPartitionMap(newMap) {
|
||||||
|
this.partitionMap = newMap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { PartitionDecider };
|
export { PartitionDecider };
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
import crypto from 'crypto';
|
||||||
|
import fs from 'fs';
|
||||||
|
import toml from 'toml';
|
||||||
|
|
||||||
|
function sha256(input) {
|
||||||
|
var hash = crypto.createHash('sha256');
|
||||||
|
hash.update(input);
|
||||||
|
return hash.digest('base64');
|
||||||
|
}
|
||||||
|
|
||||||
|
class PartitionMap {
|
||||||
|
/**
|
||||||
|
* @param {Map<string, object>} partitions Map of node ids to io configs
|
||||||
|
* @param {Array<string>} pool List of available nodes
|
||||||
|
* @param {Map<string, string>} overrides Overrides for node assignment
|
||||||
|
*/
|
||||||
|
constructor(partitions, pool, overrides) {
|
||||||
|
this.partitions = partitions;
|
||||||
|
this.pool = pool;
|
||||||
|
this.overrides = overrides || {};
|
||||||
|
this._hash = sha256(JSON.stringify(this.partitions)
|
||||||
|
+ JSON.stringify(this.pool)
|
||||||
|
+ JSON.stringify(this.overrides));
|
||||||
|
}
|
||||||
|
|
||||||
|
getHash() {
|
||||||
|
return this._hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPartitions() {
|
||||||
|
return this.partitions;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPool() {
|
||||||
|
return this.pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
getOverrides() {
|
||||||
|
return this.overrides;
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return {
|
||||||
|
partitions: this.partitions,
|
||||||
|
pool: this.pool,
|
||||||
|
overrides: this.overrides,
|
||||||
|
hash: this._hash
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJSON(json) {
|
||||||
|
return new PartitionMap(json.partitions, json.pool, json.overrides);
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromFile(filename) {
|
||||||
|
const rawData = fs.readFileSync(filename).toString('utf8');
|
||||||
|
const parsed = toml.parse(rawData);
|
||||||
|
|
||||||
|
return PartitionMap.fromJSON(parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
static empty() {
|
||||||
|
return new PartitionMap({}, [], {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { PartitionMap };
|
|
@ -7,6 +7,7 @@ import logger from 'cytube-common/lib/logger';
|
||||||
import LegacyConfig from '../config';
|
import LegacyConfig from '../config';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { AnnouncementRefresher } from './announcementrefresher';
|
import { AnnouncementRefresher } from './announcementrefresher';
|
||||||
|
import { RedisPartitionMapReloader } from './redispartitionmapreloader';
|
||||||
|
|
||||||
const PARTITION_CONFIG_PATH = path.resolve(__dirname, '..', '..', 'conf',
|
const PARTITION_CONFIG_PATH = path.resolve(__dirname, '..', '..', 'conf',
|
||||||
'partitions.toml');
|
'partitions.toml');
|
||||||
|
@ -23,13 +24,13 @@ class PartitionModule {
|
||||||
initConfig() {
|
initConfig() {
|
||||||
logger.initialize(null, null, LegacyConfig.get('debug'));
|
logger.initialize(null, null, LegacyConfig.get('debug'));
|
||||||
try {
|
try {
|
||||||
this.partitionConfig = this.loadPartitionMap();
|
this.partitionConfig = this.loadPartitionConfig();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadPartitionMap() {
|
loadPartitionConfig() {
|
||||||
try {
|
try {
|
||||||
return loadFromToml(PartitionConfig, PARTITION_CONFIG_PATH);
|
return loadFromToml(PartitionConfig, PARTITION_CONFIG_PATH);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -44,9 +45,26 @@ class PartitionModule {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getPartitionMapReloader() {
|
||||||
|
if (!this.partitionMapReloader) {
|
||||||
|
const redisProvider = this.getRedisClientProvider();
|
||||||
|
this.partitionMapReloader = new RedisPartitionMapReloader(
|
||||||
|
redisProvider.get(), // Client for GET partitionMap
|
||||||
|
redisProvider.get()); // Subscribe client
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.partitionMapReloader;
|
||||||
|
}
|
||||||
|
|
||||||
getPartitionDecider() {
|
getPartitionDecider() {
|
||||||
if (!this.partitionDecider) {
|
if (!this.partitionDecider) {
|
||||||
this.partitionDecider = new PartitionDecider(this.partitionConfig);
|
const reloader = this.getPartitionMapReloader();
|
||||||
|
this.partitionDecider = new PartitionDecider(this.partitionConfig,
|
||||||
|
reloader.getPartitionMap());
|
||||||
|
reloader.on('partitionMapChange', newMap => {
|
||||||
|
this.partitionDecider.setPartitionMap(newMap);
|
||||||
|
require('../server').getServer().handlePartitionMapChange();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.partitionDecider;
|
return this.partitionDecider;
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
import { PartitionMap } from './partitionmap';
|
||||||
|
import logger from 'cytube-common/lib/logger';
|
||||||
|
import { EventEmitter } from 'events';
|
||||||
|
|
||||||
|
class RedisPartitionMapReloader extends EventEmitter {
|
||||||
|
constructor(redisClient, subClient) {
|
||||||
|
super();
|
||||||
|
this.redisClient = redisClient;
|
||||||
|
this.subClient = subClient;
|
||||||
|
this.partitionMap = PartitionMap.empty();
|
||||||
|
redisClient.once('ready', () => this.reload());
|
||||||
|
subClient.once('ready', () => this.subscribe());
|
||||||
|
}
|
||||||
|
|
||||||
|
subscribe() {
|
||||||
|
this.subClient.subscribe('partitionMap');
|
||||||
|
this.subClient.on('message', (channel, message) => {
|
||||||
|
if (channel !== 'partitionMap') {
|
||||||
|
logger.warn('RedisPartitionMapReloader received unexpected message '
|
||||||
|
+ `on redis channel ${channel}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.reload();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
reload() {
|
||||||
|
this.redisClient.getAsync('partitionMap').then(result => {
|
||||||
|
var newMap = null;
|
||||||
|
try {
|
||||||
|
newMap = PartitionMap.fromJSON(JSON.parse(result));
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`Failed to decode received partition map: ${error}`,
|
||||||
|
{ payload: result });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.partitionMap.getHash() !== newMap.getHash()) {
|
||||||
|
logger.info(`Partition map changed (hash=${newMap.getHash()})`);
|
||||||
|
this.partitionMap = newMap;
|
||||||
|
this.emit('partitionMapChange', newMap);
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
logger.error(`Failed to retrieve partition map from redis: ${error}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
getPartitionMap() {
|
||||||
|
return this.partitionMap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { RedisPartitionMapReloader };
|
|
@ -333,20 +333,7 @@ Server.prototype.shutdown = function () {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Server.prototype.reloadPartitionMap = function () {
|
Server.prototype.handlePartitionMapChange = function () {
|
||||||
if (!Config.get("enable-partition")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var config;
|
|
||||||
try {
|
|
||||||
config = this.initModule.loadPartitionMap();
|
|
||||||
} catch (error) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.initModule.partitionConfig.config = config.config;
|
|
||||||
|
|
||||||
const channels = Array.prototype.slice.call(this.channels);
|
const channels = Array.prototype.slice.call(this.channels);
|
||||||
Promise.map(channels, channel => {
|
Promise.map(channels, channel => {
|
||||||
if (channel.dead) {
|
if (channel.dead) {
|
||||||
|
@ -375,3 +362,11 @@ Server.prototype.reloadPartitionMap = function () {
|
||||||
Logger.syslog.log("Partition reload complete");
|
Logger.syslog.log("Partition reload complete");
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Server.prototype.reloadPartitionMap = function () {
|
||||||
|
if (!Config.get("enable-partitions")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.initModule.getPartitionMapReloader().reload();
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue