Search in sources :

Example 1 with MycatRouterConfig

use of io.mycat.config.MycatRouterConfig in project Mycat2 by MyCATApache.

the class MigrateUtil method read.

@SneakyThrows
public static Flowable<Object[]> read(MigrateUtil.MigrateJdbcInput migrateJdbcInput, Partition backend) {
    MycatRouterConfig routerConfig = MetaClusterCurrent.wrapper(MycatRouterConfig.class);
    ReplicaSelectorManager replicaSelectorRuntime = MetaClusterCurrent.wrapper(ReplicaSelectorManager.class);
    String targetName = backend.getTargetName();
    String tableName = backend.getTable();
    String schemaName = backend.getSchema();
    String datasourceName = replicaSelectorRuntime.getDatasourceNameByReplicaName(targetName, true, null);
    List<DatasourceConfig> datasources = routerConfig.getDatasources();
    DatasourceConfig datasourceConfig = datasources.stream().filter(i -> i.getName().equals(datasourceName)).findFirst().orElseThrow((Supplier<Throwable>) () -> {
        MycatException mycatException = new MycatException("can not found datasource " + datasourceName);
        LOGGER.error("", mycatException);
        return mycatException;
    });
    return read(migrateJdbcInput, tableName, schemaName, datasourceConfig.getUrl(), datasourceConfig.getUser(), datasourceConfig.getPassword());
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) DatasourceConfig(io.mycat.config.DatasourceConfig) MycatException(io.mycat.MycatException) MycatRouterConfig(io.mycat.config.MycatRouterConfig) ToString(groovy.transform.ToString) SneakyThrows(lombok.SneakyThrows)

Example 2 with MycatRouterConfig

use of io.mycat.config.MycatRouterConfig 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;
}
Also used : ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) ClusterConfig(io.mycat.config.ClusterConfig) java.util(java.util) MetaClusterCurrent(io.mycat.MetaClusterCurrent) MycatRouterConfig(io.mycat.config.MycatRouterConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EqualsAndHashCode(lombok.EqualsAndHashCode) Collectors(java.util.stream.Collectors) AtomicLong(java.util.concurrent.atomic.AtomicLong) ReplicaSelector(io.mycat.replica.ReplicaSelector) ImmutableList(com.google.common.collect.ImmutableList) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) Data(lombok.Data) ToString(lombok.ToString) PhysicsInstance(io.mycat.replica.PhysicsInstance) MycatRouterConfig(io.mycat.config.MycatRouterConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ToString(lombok.ToString) PhysicsInstance(io.mycat.replica.PhysicsInstance) ReplicaSelector(io.mycat.replica.ReplicaSelector) ImmutableList(com.google.common.collect.ImmutableList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterConfig(io.mycat.config.ClusterConfig)

Aggregations

MycatRouterConfig (io.mycat.config.MycatRouterConfig)2 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)2 ImmutableList (com.google.common.collect.ImmutableList)1 ToString (groovy.transform.ToString)1 MetaClusterCurrent (io.mycat.MetaClusterCurrent)1 MycatException (io.mycat.MycatException)1 ClusterConfig (io.mycat.config.ClusterConfig)1 DatasourceConfig (io.mycat.config.DatasourceConfig)1 PhysicsInstance (io.mycat.replica.PhysicsInstance)1 ReplicaSelector (io.mycat.replica.ReplicaSelector)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Collectors (java.util.stream.Collectors)1 Data (lombok.Data)1 EqualsAndHashCode (lombok.EqualsAndHashCode)1 SneakyThrows (lombok.SneakyThrows)1 ToString (lombok.ToString)1