sync/src/partition/partitionmodule.js

123 lines
3.9 KiB
JavaScript
Raw Normal View History

import { loadFromToml } from '../configuration/configloader';
2016-06-06 21:54:49 -07:00
import { PartitionConfig } from './partitionconfig';
import { PartitionDecider } from './partitiondecider';
import { PartitionClusterClient } from '../io/cluster/partitionclusterclient';
import RedisClientProvider from '../redis/redisclientprovider';
2016-06-06 21:54:49 -07:00
import path from 'path';
2016-07-03 21:28:43 -07:00
import { AnnouncementRefresher } from './announcementrefresher';
import { RedisPartitionMapReloader } from './redispartitionmapreloader';
2017-08-15 18:23:03 -07:00
import { RedisMessageBus } from '../pubsub/redis';
2016-06-06 21:54:49 -07:00
const PARTITION_CONFIG_PATH = path.resolve(__dirname, '..', '..', 'conf',
'partitions.toml');
2017-07-08 20:11:54 -07:00
const logger = require('@calzoneman/jsli')('PartitionModule');
2016-06-06 21:54:49 -07:00
class PartitionModule {
constructor() {
this.initConfig();
this.cliMode = false;
2016-06-06 21:54:49 -07:00
}
onReady() {
2016-07-03 21:28:43 -07:00
this.getAnnouncementRefresher();
2016-06-06 21:54:49 -07:00
}
initConfig() {
try {
this.partitionConfig = this.loadPartitionConfig();
2016-06-06 21:54:49 -07:00
} catch (error) {
process.exit(1);
}
}
loadPartitionConfig() {
2016-06-06 21:54:49 -07:00
try {
return loadFromToml(PartitionConfig, PARTITION_CONFIG_PATH);
} catch (error) {
if (typeof error.line !== 'undefined') {
logger.error(`Error in ${PARTITION_CONFIG_PATH}: ${error} ` +
`(line ${error.line})`);
} else {
logger.error(`Error loading ${PARTITION_CONFIG_PATH}: ` +
`${error.stack}`);
}
throw error;
}
}
getPartitionMapReloader() {
if (!this.partitionMapReloader) {
const redisProvider = this.getRedisClientProvider();
this.partitionMapReloader = new RedisPartitionMapReloader(
2016-10-18 23:13:25 -07:00
this.partitionConfig,
redisProvider.get(), // Client for GET partitionMap
redisProvider.get()); // Subscribe client
}
return this.partitionMapReloader;
}
2016-06-06 21:54:49 -07:00
getPartitionDecider() {
if (!this.partitionDecider) {
const reloader = this.getPartitionMapReloader();
this.partitionDecider = new PartitionDecider(this.partitionConfig,
reloader.getPartitionMap());
reloader.on('partitionMapChange', newMap => {
this.partitionDecider.setPartitionMap(newMap);
if (!this.cliMode) {
require('../server').getServer().handlePartitionMapChange();
}
});
2016-06-06 21:54:49 -07:00
}
return this.partitionDecider;
}
getClusterClient() {
if (!this.partitionClusterClient) {
this.partitionClusterClient = new PartitionClusterClient(
this.getPartitionDecider());
}
return this.partitionClusterClient;
}
2016-06-09 23:42:30 -07:00
getRedisClientProvider() {
if (!this.redisClientProvider) {
this.redisClientProvider = new RedisClientProvider(
this.partitionConfig.getRedisConfig()
);
}
return this.redisClientProvider;
}
2016-07-03 21:28:43 -07:00
getAnnouncementRefresher() {
if (!this.announcementRefresher) {
const provider = this.getRedisClientProvider();
this.announcementRefresher = new AnnouncementRefresher(
provider.get(),
provider.get(),
this.partitionConfig.getAnnouncementChannel()
2016-07-03 21:28:43 -07:00
);
}
return this.announcementRefresher;
}
2017-08-15 18:23:03 -07:00
getGlobalMessageBus() {
if (!this.globalMessageBus) {
const provider = this.getRedisClientProvider();
this.globalMessageBus = new RedisMessageBus(
provider.get(),
provider.get(),
this.partitionConfig.getGlobalMessageBusChannel()
);
}
return this.globalMessageBus;
}
2016-06-06 21:54:49 -07:00
}
export { PartitionModule };