Search in sources :

Example 26 with DatasourceConfig

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

the class TestMySQLManagerImpl method getWriteableConnectionMap.

@Override
@SneakyThrows
public Map<String, java.sql.Connection> getWriteableConnectionMap() {
    ConcurrentHashMap.KeySetView<String, DruidDataSource> strings = nameMap.keySet();
    JdbcConnectionManager jdbcConnectionManager = MetaClusterCurrent.wrapper(JdbcConnectionManager.class);
    Map<String, JdbcDataSource> datasourceInfo = jdbcConnectionManager.getDatasourceInfo();
    HashMap<String, Connection> map = new HashMap<>();
    for (String string : strings) {
        JdbcDataSource jdbcDataSource = datasourceInfo.get(string);
        DatasourceConfig config = jdbcDataSource.getConfig();
        if (jdbcDataSource.isMySQLType()) {
            if (InstanceType.valueOf(Optional.ofNullable(config.getInstanceType()).orElse("READ_WRITE").toUpperCase()).isWriteType()) {
                Connection connection = jdbcDataSource.getDataSource().getConnection();
                map.put(string, connection);
            }
        }
    }
    return map;
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JdbcDataSource(io.mycat.datasource.jdbc.datasource.JdbcDataSource) Connection(java.sql.Connection) NewMycatConnection(io.mycat.newquery.NewMycatConnection) SqlConnection(io.vertx.sqlclient.SqlConnection) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) SneakyThrows(lombok.SneakyThrows)

Example 27 with DatasourceConfig

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

the class SpmTest method init.

@BeforeClass
public static void init() throws Exception {
    HashMap<Class, Object> context = new HashMap<>();
    context.put(Vertx.class, Vertx.vertx());
    context.put(ServerConfig.class, new ServerConfig());
    context.put(IOExecutor.class, IOExecutor.DEFAULT);
    context.put(DrdsSqlCompiler.class, new DrdsSqlCompiler(new DrdsConst() {

        @Override
        public NameMap<SchemaHandler> schemas() {
            return new NameMap<>();
        }
    }));
    MetaClusterCurrent.register(context);
    String customerDatasourceProvider = DruidDatasourceProvider.class.getName();
    DatasourceConfig datasourceConfig = new DatasourceConfig();
    datasourceConfig.setDbType("mysql");
    datasourceConfig.setUser("root");
    datasourceConfig.setPassword("123456");
    datasourceConfig.setName("prototypeDs");
    datasourceConfig.setUrl("jdbc:mysql://localhost:3306/mysql");
    Map<String, DatasourceConfig> datasources = Maps.of("prototypeDs", datasourceConfig);
    ClusterConfig clusterConfig = new ClusterConfig();
    clusterConfig.setName("prototype");
    clusterConfig.setMasters(Arrays.asList("prototypeDs"));
    Map<String, ClusterConfig> clusterConfigs = Maps.of("prototype", clusterConfig);
    LinkedList<Runnable> runnables = new LinkedList<>();
    ReplicaSelectorManager manager = ReplicaSelectorRuntime.create(new ArrayList<>(clusterConfigs.values()), datasources, new LoadBalanceManager(), name -> 0, (command, initialDelay, period, unit) -> {
        runnables.add(command);
        return () -> {
        };
    });
    context.put(ReplicaSelectorManager.class, manager);
    context.put(JdbcConnectionManager.class, jdbcManager = new JdbcConnectionManager(DruidDatasourceProvider.class.getName(), datasources));
    MetaClusterCurrent.register(context);
}
Also used : SchemaHandler(io.mycat.calcite.table.SchemaHandler) ReplicaSelectorManager(io.mycat.replica.ReplicaSelectorManager) NameMap(io.mycat.util.NameMap) LoadBalanceManager(io.mycat.plug.loadBalance.LoadBalanceManager) DruidDatasourceProvider(io.mycat.datasource.jdbc.DruidDatasourceProvider) ServerConfig(io.mycat.config.ServerConfig) DatasourceConfig(io.mycat.config.DatasourceConfig) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ClusterConfig(io.mycat.config.ClusterConfig) BeforeClass(org.junit.BeforeClass)

Example 28 with DatasourceConfig

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

the class JdbcConnectionManager method getConfigAsMap.

@Override
public Map<String, DatasourceConfig> getConfigAsMap() {
    HashMap<String, DatasourceConfig> res = new HashMap<>();
    for (Map.Entry<String, JdbcDataSource> entry : dataSourceMap.entrySet()) {
        DatasourceConfig config = entry.getValue().getConfig();
        String key = config.getName();
        res.put(key, config);
    }
    return res;
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 29 with DatasourceConfig

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

the class DbKVImpl method values.

@Override
public List<T> values() {
    Config config = DbStorageManagerImpl.readConfig(datasourceConfig);
    Map<String, String> stringStringMap = config.config.getOrDefault(this.path, Collections.emptyMap());
    return (List) stringStringMap.values().stream().map(i -> JsonUtil.from(i, aClass)).collect(Collectors.toList());
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig)

Example 30 with DatasourceConfig

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

the class DbKVImpl method removeKey.

@Override
public void removeKey(String key) {
    Config config = DbStorageManagerImpl.readConfig(datasourceConfig);
    Map<String, String> stringStringMap = new HashMap<>(config.config.getOrDefault(this.path, Collections.emptyMap()));
    stringStringMap.remove(key);
    config.config.put(this.path, stringStringMap);
    DbStorageManagerImpl.removeBy(datasourceConfig, config.version);
    DbStorageManagerImpl.writeString(datasourceConfig, config.config);
}
Also used : DatasourceConfig(io.mycat.config.DatasourceConfig)

Aggregations

DatasourceConfig (io.mycat.config.DatasourceConfig)44 ClusterConfig (io.mycat.config.ClusterConfig)26 LoadBalanceManager (io.mycat.plug.loadBalance.LoadBalanceManager)24 TimerConfig (io.mycat.config.TimerConfig)22 Test (org.junit.Test)22 ConfigReporter (io.mycat.ConfigReporter)16 Consumer (java.util.function.Consumer)8 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)7 CreateClusterHint (io.mycat.hint.CreateClusterHint)6 CreateDataSourceHint (io.mycat.hint.CreateDataSourceHint)6 Connection (java.sql.Connection)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 SneakyThrows (lombok.SneakyThrows)6 HeartBeatStrategy (io.mycat.replica.heartbeat.HeartBeatStrategy)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 ConnectionUrlParser (com.mysql.cj.conf.ConnectionUrlParser)3 HostInfo (com.mysql.cj.conf.HostInfo)3 MycatException (io.mycat.MycatException)3 SchemaHandler (io.mycat.calcite.table.SchemaHandler)3 DruidDatasourceProvider (io.mycat.datasource.jdbc.DruidDatasourceProvider)3