use of com.alibaba.cobar.mysql.MySQLDataNode in project cobar by alibaba.
the class MultiNodeQueryHandler method execute.
public void execute() throws Exception {
final ReentrantLock lock = this.lock;
lock.lock();
try {
this.reset(route.length);
this.fieldsReturned = false;
this.affectedRows = 0L;
this.insertId = 0L;
this.buffer = session.getSource().allocate();
} finally {
lock.unlock();
}
if (session.closed()) {
decrementCountToZero();
recycleResources();
return;
}
session.setConnectionRunning(route);
ThreadPoolExecutor executor = session.getSource().getProcessor().getExecutor();
for (final RouteResultsetNode node : route) {
final MySQLConnection conn = session.getTarget(node);
if (conn != null) {
conn.setAttachment(node);
executor.execute(new Runnable() {
@Override
public void run() {
_execute(conn, node);
}
});
} else {
CobarConfig conf = CobarServer.getInstance().getConfig();
MySQLDataNode dn = conf.getDataNodes().get(node.getName());
dn.getConnection(this, node);
}
}
}
use of com.alibaba.cobar.mysql.MySQLDataNode in project cobar by alibaba.
the class ShowHeartbeat method getRows.
private static List<RowDataPacket> getRows() {
List<RowDataPacket> list = new LinkedList<RowDataPacket>();
CobarConfig conf = CobarServer.getInstance().getConfig();
// cobar nodes
Map<String, CobarNode> cobarNodes = conf.getCluster().getNodes();
List<String> cobarNodeKeys = new ArrayList<String>(cobarNodes.size());
cobarNodeKeys.addAll(cobarNodes.keySet());
Collections.sort(cobarNodeKeys);
for (String key : cobarNodeKeys) {
CobarNode node = cobarNodes.get(key);
if (node != null) {
CobarHeartbeat hb = node.getHeartbeat();
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(node.getName().getBytes());
row.add("COBAR".getBytes());
row.add(node.getConfig().getHost().getBytes());
row.add(IntegerUtil.toBytes(node.getConfig().getPort()));
row.add(IntegerUtil.toBytes(hb.getStatus()));
row.add(IntegerUtil.toBytes(hb.getErrorCount()));
row.add(hb.isChecking() ? "checking".getBytes() : "idle".getBytes());
row.add(LongUtil.toBytes(hb.getTimeout()));
row.add(hb.getRecorder().get().getBytes());
String at = hb.lastActiveTime();
row.add(at == null ? null : at.getBytes());
row.add(hb.isStop() ? "true".getBytes() : "false".getBytes());
list.add(row);
}
}
// data nodes
Map<String, MySQLDataNode> dataNodes = conf.getDataNodes();
List<String> dataNodeKeys = new ArrayList<String>(dataNodes.size());
dataNodeKeys.addAll(dataNodes.keySet());
Collections.sort(dataNodeKeys, new Comparators<String>());
for (String key : dataNodeKeys) {
MySQLDataNode node = dataNodes.get(key);
if (node != null) {
MySQLHeartbeat hb = node.getHeartbeat();
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(node.getName().getBytes());
row.add("MYSQL".getBytes());
if (hb != null) {
row.add(hb.getSource().getConfig().getHost().getBytes());
row.add(IntegerUtil.toBytes(hb.getSource().getConfig().getPort()));
row.add(IntegerUtil.toBytes(hb.getStatus()));
row.add(IntegerUtil.toBytes(hb.getErrorCount()));
row.add(hb.isChecking() ? "checking".getBytes() : "idle".getBytes());
row.add(LongUtil.toBytes(hb.getTimeout()));
row.add(hb.getRecorder().get().getBytes());
String lat = hb.getLastActiveTime();
row.add(lat == null ? null : lat.getBytes());
row.add(hb.isStop() ? "true".getBytes() : "false".getBytes());
} else {
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
}
list.add(row);
}
}
return list;
}
use of com.alibaba.cobar.mysql.MySQLDataNode in project cobar by alibaba.
the class CobarConfig method apply.
private void apply(Map<String, UserConfig> users, Map<String, SchemaConfig> schemas, Map<String, MySQLDataNode> dataNodes, Map<String, DataSourceConfig> dataSources, CobarCluster cluster, QuarantineConfig quarantine) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
// stop mysql heartbeat
Map<String, MySQLDataNode> oldDataNodes = this.dataNodes;
if (oldDataNodes != null) {
for (MySQLDataNode n : oldDataNodes.values()) {
if (n != null) {
n.stopHeartbeat();
}
}
}
// stop cobar heartbeat
CobarCluster oldCluster = this.cluster;
if (oldCluster != null) {
Map<String, CobarNode> nodes = oldCluster.getNodes();
for (CobarNode n : nodes.values()) {
if (n != null) {
n.stopHeartbeat();
}
}
}
this._users = this.users;
this._schemas = this.schemas;
this._dataNodes = this.dataNodes;
this._dataSources = this.dataSources;
this._cluster = this.cluster;
this._quarantine = this.quarantine;
// start mysql heartbeat
if (dataNodes != null) {
for (MySQLDataNode n : dataNodes.values()) {
if (n != null) {
n.startHeartbeat();
}
}
}
// start cobar heartbeat
if (cluster != null) {
Map<String, CobarNode> nodes = cluster.getNodes();
for (CobarNode n : nodes.values()) {
if (n != null) {
n.startHeartbeat();
}
}
}
this.users = users;
this.schemas = schemas;
this.dataNodes = dataNodes;
this.dataSources = dataSources;
this.cluster = cluster;
this.quarantine = quarantine;
} finally {
lock.unlock();
}
}
use of com.alibaba.cobar.mysql.MySQLDataNode in project cobar by alibaba.
the class CobarServer method startup.
public void startup() throws IOException {
// server startup
LOGGER.info("===============================================");
LOGGER.info(NAME + " is ready to startup ...");
SystemConfig system = config.getSystem();
timer.schedule(updateTime(), 0L, TIME_UPDATE_PERIOD);
// startup processors
LOGGER.info("Startup processors ...");
int handler = system.getProcessorHandler();
int executor = system.getProcessorExecutor();
int committer = system.getProcessorCommitter();
processors = new NIOProcessor[system.getProcessors()];
for (int i = 0; i < processors.length; i++) {
processors[i] = new NIOProcessor("Processor" + i, handler, executor, committer);
processors[i].startup();
}
timer.schedule(processorCheck(), 0L, system.getProcessorCheckPeriod());
// startup connector
LOGGER.info("Startup connector ...");
connector = new NIOConnector(NAME + "Connector");
connector.setProcessors(processors);
connector.start();
// init dataNodes
Map<String, MySQLDataNode> dataNodes = config.getDataNodes();
LOGGER.info("Initialize dataNodes ...");
for (MySQLDataNode node : dataNodes.values()) {
node.init(1, 0);
}
timer.schedule(dataNodeIdleCheck(), 0L, system.getDataNodeIdleCheckPeriod());
timer.schedule(dataNodeHeartbeat(), 0L, system.getDataNodeHeartbeatPeriod());
// startup manager
ManagerConnectionFactory mf = new ManagerConnectionFactory();
mf.setCharset(system.getCharset());
mf.setIdleTimeout(system.getIdleTimeout());
manager = new NIOAcceptor(NAME + "Manager", system.getManagerPort(), mf);
manager.setProcessors(processors);
manager.start();
LOGGER.info(manager.getName() + " is started and listening on " + manager.getPort());
// startup server
ServerConnectionFactory sf = new ServerConnectionFactory();
sf.setCharset(system.getCharset());
sf.setIdleTimeout(system.getIdleTimeout());
server = new NIOAcceptor(NAME + "Server", system.getServerPort(), sf);
server.setProcessors(processors);
server.start();
timer.schedule(clusterHeartbeat(), 0L, system.getClusterHeartbeatPeriod());
// server started
LOGGER.info(server.getName() + " is started and listening on " + server.getPort());
LOGGER.info("===============================================");
}
use of com.alibaba.cobar.mysql.MySQLDataNode in project cobar by alibaba.
the class ConfigInitializer method initDataNodes.
private Map<String, MySQLDataNode> initDataNodes(ConfigLoader configLoader) {
Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes();
Map<String, MySQLDataNode> nodes = new HashMap<String, MySQLDataNode>(nodeConfs.size());
for (DataNodeConfig conf : nodeConfs.values()) {
MySQLDataNode dataNode = getDataNode(conf, configLoader);
if (nodes.containsKey(dataNode.getName())) {
throw new ConfigException("dataNode " + dataNode.getName() + " duplicated!");
}
nodes.put(dataNode.getName(), dataNode);
}
return nodes;
}
Aggregations