use of io.mycat.replica.PhysicsInstance in project Mycat2 by MyCATApache.
the class RWEntry method snapshot.
public static RWEntryMap snapshot() {
if ((!MetaClusterCurrent.exist(ReplicaSelectorManager.class)) || (!MetaClusterCurrent.exist(MycatRouterConfig.class))) {
return new RWEntryMap();
}
ReplicaSelectorManager replicaSelectorManager = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
MycatRouterConfig routerConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
Map<String, ClusterConfig> clusterConfigMap = routerConfig.getClusters().stream().collect(Collectors.toMap(k -> k.getName(), v -> v));
Map<String, ReplicaSelector> replicaMap = replicaSelectorManager.getReplicaMap();
Map<String, RWEntry> rwEntryMap = new HashMap<>();
for (Map.Entry<String, Entry> entry : map.entrySet()) {
String name = entry.getKey();
Entry value = entry.getValue();
// //////////////////////////////////////////////////////////////
boolean status = false;
ReplicaSelector replicaSelector = replicaMap.get(name);
if (replicaSelector != null) {
ClusterConfig clusterConfig = clusterConfigMap.get(replicaSelector.getName());
List<String> dsNames = (List) ImmutableList.builder().addAll(clusterConfig.getMasters()).addAll(clusterConfig.getReplicas()).build().stream().distinct().collect(Collectors.toList());
int i = 0;
for (; i < dsNames.size(); i++) {
String dsName = dsNames.get(i);
PhysicsInstance physicsInstance = replicaSelector.getRawDataSourceMap().get(dsName);
if (physicsInstance == null) {
break;
} else {
if (!physicsInstance.isAlive()) {
break;
}
}
}
status = i == dsNames.size();
} else {
status = false;
}
rwEntryMap.put(name, new RWEntry(value.MASTER.get(), value.SLAVE.get(), status));
}
RWEntryMap res = new RWEntryMap();
res.rwMap = rwEntryMap;
return res;
}
use of io.mycat.replica.PhysicsInstance in project Mycat2 by MyCATApache.
the class InstanceCollector method collect.
@Override
public List<MetricFamilySamples> collect() {
List<String> columnList = ImmutableList.of("NAME");
GaugeMetricFamily gaugeMetricFamily = new GaugeMetricFamily("instance_active", "instance_active", columnList);
if (MetaClusterCurrent.exist(ReplicaSelectorManager.class)) {
ReplicaSelectorManager replicaSelectorManager = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
for (PhysicsInstance physicsInstance : replicaSelectorManager.getPhysicsInstances()) {
int value = physicsInstance.isAlive() ? 1 : 0;
gaugeMetricFamily.addMetric(columnList, value);
}
}
return ImmutableList.of(gaugeMetricFamily);
}
use of io.mycat.replica.PhysicsInstance in project Mycat2 by MyCATApache.
the class HintHandler method showInstances.
public static RowBaseIterator showInstances() {
MycatRouterConfig routerConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
ReplicaSelectorManager replicaSelectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
resultSetBuilder.addColumnInfo("NAME", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("ALIVE", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("READABLE", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("TYPE", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("SESSION_COUNT", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("WEIGHT", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("MASTER", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("LIMIT_SESSION_COUNT", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("REPLICA", JDBCType.VARCHAR);
Collection<PhysicsInstance> values = replicaSelectorRuntime.getPhysicsInstances();
Map<String, DatasourceConfig> dataSourceConfig = routerConfig.getDatasources().stream().collect(Collectors.toMap(k -> k.getName(), v -> v));
for (PhysicsInstance instance : values) {
String NAME = instance.getName();
String TYPE = instance.getType().name();
boolean READABLE = instance.asSelectRead();
int SESSION_COUNT = instance.getSessionCounter();
int WEIGHT = instance.getWeight();
boolean ALIVE = instance.isAlive();
boolean MASTER = instance.isMaster();
Optional<DatasourceConfig> e = Optional.ofNullable(dataSourceConfig.get(NAME));
String replicaDataSourceSelectorList = String.join(",", replicaSelectorRuntime.getReplicaNameListByInstanceName(NAME));
resultSetBuilder.addObjectRowPayload(Arrays.asList(NAME, ALIVE, READABLE, TYPE, SESSION_COUNT, WEIGHT, MASTER, e.map(i -> i.getMaxCon()).orElse(-1), replicaDataSourceSelectorList));
}
RowBaseIterator rowBaseIterator = resultSetBuilder.build();
return rowBaseIterator;
}
use of io.mycat.replica.PhysicsInstance in project Mycat2 by MyCATApache.
the class HintHandler method showClusters.
public static RowBaseIterator showClusters(String clusterName) {
MycatRouterConfig routerConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
resultSetBuilder.addColumnInfo("NAME", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("SWITCH_TYPE", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("MAX_REQUEST_COUNT", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("TYPE", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("WRITE_DS", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("READ_DS", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("WRITE_L", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("READ_L", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("AVAILABLE", JDBCType.VARCHAR);
Collection<ReplicaSelector> values = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class).getReplicaMap().values();
Map<String, ClusterConfig> clusterConfigMap = routerConfig.getClusters().stream().collect(Collectors.toMap(k -> k.getName(), v -> v));
for (ReplicaSelector value : values.stream().filter(v -> {
if (clusterName != null) {
return clusterName.equalsIgnoreCase(v.getName());
}
return true;
}).collect(Collectors.toList())) {
String NAME = value.getName();
Optional<ClusterConfig> e = Optional.ofNullable(clusterConfigMap.get(NAME));
ReplicaSwitchType SWITCH_TYPE = value.getSwitchType();
int MAX_REQUEST_COUNT = value.maxRequestCount();
String TYPE = value.getBalanceType().name();
String WRITE_DS = ((List<PhysicsInstance>) value.getWriteDataSourceByReplicaType()).stream().map(i -> i.getName()).collect(Collectors.joining(","));
String READ_DS = (value.getReadDataSourceByReplica()).stream().map(i -> i.getName()).collect(Collectors.joining(","));
String WL = Optional.ofNullable(value.getDefaultWriteLoadBalanceStrategy()).map(i -> i.getClass().getName()).orElse(null);
String RL = Optional.ofNullable(value.getDefaultReadLoadBalanceStrategy()).map(i -> i.getClass().getName()).orElse(null);
String AVAILABLE = Boolean.toString(((List<PhysicsInstance>) value.getWriteDataSourceByReplicaType()).stream().anyMatch(PhysicsInstance::isAlive));
resultSetBuilder.addObjectRowPayload(Arrays.asList(NAME, SWITCH_TYPE, MAX_REQUEST_COUNT, TYPE, WRITE_DS, READ_DS, WL, RL, AVAILABLE));
}
return resultSetBuilder.build();
}
use of io.mycat.replica.PhysicsInstance in project Mycat2 by MyCATApache.
the class HintHandler method showHeatbeatStat.
public static RowBaseIterator showHeatbeatStat() {
ResultSetBuilder resultSetBuilder = ResultSetBuilder.create();
resultSetBuilder.addColumnInfo("NAME", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("TYPE", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("READABLE", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("SESSION_COUNT", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("WEIGHT", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("ALIVE", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("MASTER", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("LIMIT_SESSION_COUNT", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("REPLICA", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("SLAVE_THRESHOLD", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("IS_HEARTBEAT_TIMEOUT", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("HB_ERROR_COUNT", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("HB_LAST_SWITCH_TIME", JDBCType.TIMESTAMP);
resultSetBuilder.addColumnInfo("HB_MAX_RETRY", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("IS_CHECKING", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("MIN_SWITCH_TIME_INTERVAL", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("HEARTBEAT_TIMEOUT", JDBCType.BIGINT);
resultSetBuilder.addColumnInfo("SYNC_DS_STATUS", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("HB_DS_STATUS", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("IS_SLAVE_BEHIND_MASTER", JDBCType.VARCHAR);
resultSetBuilder.addColumnInfo("LAST_SEND_QUERY_TIME", JDBCType.TIMESTAMP);
resultSetBuilder.addColumnInfo("LAST_RECEIVED_QUERY_TIME", JDBCType.TIMESTAMP);
if (MetaClusterCurrent.exist(MycatRouterConfig.class) && MetaClusterCurrent.exist(ReplicaSelectorManager.class)) {
MycatRouterConfig routerConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
ReplicaSelectorManager replicaSelectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
Map<String, DatasourceConfig> dataSourceConfig = routerConfig.getDatasources().stream().collect(Collectors.toMap(k -> k.getName(), v -> v));
for (HeartbeatFlow heartbeatFlow : replicaSelectorRuntime.getHeartbeatDetectorMap().values()) {
PhysicsInstance instance = heartbeatFlow.instance();
String NAME = instance.getName();
String TYPE = instance.getType().name();
boolean READABLE = instance.asSelectRead();
int SESSION_COUNT = instance.getSessionCounter();
int WEIGHT = instance.getWeight();
boolean ALIVE = instance.isAlive();
boolean MASTER = instance.isMaster();
double SLAVE_THRESHOLD = heartbeatFlow.getSlaveThreshold();
boolean IS_HEARTBEAT_TIMEOUT = heartbeatFlow.isHeartbeatTimeout();
final HeartBeatStatus HEART_BEAT_STATUS = heartbeatFlow.getHbStatus();
int HB_ERROR_COUNT = HEART_BEAT_STATUS.getErrorCount();
LocalDateTime HB_LAST_SWITCH_TIME = new Timestamp(HEART_BEAT_STATUS.getLastSwitchTime()).toLocalDateTime();
int HB_MAX_RETRY = HEART_BEAT_STATUS.getMaxRetry();
boolean IS_CHECKING = HEART_BEAT_STATUS.isChecking();
long MIN_SWITCH_TIME_INTERVAL = HEART_BEAT_STATUS.getMinSwitchTimeInterval();
final long HEARTBEAT_TIMEOUT = (heartbeatFlow.getHeartbeatTimeout());
DatasourceStatus DS_STATUS_OBJECT = heartbeatFlow.getDsStatus();
String SYNC_DS_STATUS = DS_STATUS_OBJECT.getDbSynStatus().name();
String HB_DS_STATUS = DS_STATUS_OBJECT.getStatus().name();
boolean IS_SLAVE_BEHIND_MASTER = DS_STATUS_OBJECT.isSlaveBehindMaster();
LocalDateTime LAST_SEND_QUERY_TIME = new Timestamp(heartbeatFlow.getLastSendQryTime()).toLocalDateTime();
LocalDateTime LAST_RECEIVED_QUERY_TIME = new Timestamp(heartbeatFlow.getLastReceivedQryTime()).toLocalDateTime();
Optional<DatasourceConfig> e = Optional.ofNullable(dataSourceConfig.get(NAME));
String replicaDataSourceSelectorList = String.join(",", replicaSelectorRuntime.getReplicaNameListByInstanceName(NAME));
resultSetBuilder.addObjectRowPayload(Arrays.asList(NAME, TYPE, READABLE, SESSION_COUNT, WEIGHT, ALIVE, MASTER, e.map(i -> i.getMaxCon()).orElse(-1), replicaDataSourceSelectorList, SLAVE_THRESHOLD, IS_HEARTBEAT_TIMEOUT, HB_ERROR_COUNT, HB_LAST_SWITCH_TIME, HB_MAX_RETRY, IS_CHECKING, MIN_SWITCH_TIME_INTERVAL, HEARTBEAT_TIMEOUT, SYNC_DS_STATUS, HB_DS_STATUS, IS_SLAVE_BEHIND_MASTER, LAST_SEND_QUERY_TIME, LAST_RECEIVED_QUERY_TIME));
}
}
RowBaseIterator rowBaseIterator = resultSetBuilder.build();
return rowBaseIterator;
}
Aggregations