use of io.mycat.replica.ReplicaSelector 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.ReplicaSelector 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();
}
Aggregations