Search in sources :

Example 16 with ClusterConfig

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

the class StatisticCenterTest 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(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);
    statisticCenter.init();
}
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) JdbcConnectionManager(io.mycat.datasource.jdbc.datasource.JdbcConnectionManager) ClusterConfig(io.mycat.config.ClusterConfig)

Example 17 with ClusterConfig

use of io.mycat.config.ClusterConfig 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 18 with ClusterConfig

use of io.mycat.config.ClusterConfig 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)

Example 19 with ClusterConfig

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

the class CreateTableSQLHandler method createTable.

public synchronized void createTable(Map hint, String schemaName, String tableName, MySqlCreateTableStatement createTableSql) throws Exception {
    if (createTableSql == null && hint != null) {
        Object sql = hint.get("createTableSql");
        if (sql instanceof MySqlCreateTableStatement) {
            createTableSql = (MySqlCreateTableStatement) sql;
        } else {
            createTableSql = (MySqlCreateTableStatement) SQLUtils.parseSingleMysqlStatement(Objects.toString(sql));
        }
    }
    Objects.requireNonNull(createTableSql);
    try (MycatRouterConfigOps ops = ConfigUpdater.getOps()) {
        if (schemaName == null || tableName == null) {
            MySqlCreateTableStatement ast = (MySqlCreateTableStatement) createTableSql;
            schemaName = SQLUtils.normalize(ast.getSchema());
            tableName = SQLUtils.normalize(ast.getTableName());
        }
        if (hint == null || (hint != null && hint.isEmpty())) {
            if (createTableSql.isBroadCast()) {
                ops.putGlobalTable(schemaName, tableName, createTableSql);
            } else if (createTableSql.getDbPartitionBy() == null && createTableSql.getTablePartitionBy() == null) {
                ops.putNormalTable(schemaName, tableName, createTableSql);
            } else {
                MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
                int defaultStoreNodeNum = metadataManager.getDefaultStoreNodeNum();
                if (defaultStoreNodeNum == 0) {
                    ops.getOriginal().getClusters().stream().filter(i -> "prototype".equals(i.getName())).findFirst().ifPresent(clusterConfig -> {
                        ClusterConfig config = JsonUtil.from(JsonUtil.toJson(clusterConfig), ClusterConfig.class);
                        ops.putReplica(config);
                    });
                }
                ops.putHashTable(schemaName, tableName, createTableSql, getAutoHashProperties(createTableSql));
            }
        } else {
            Map<String, Object> infos = hint;
            switch(Objects.requireNonNull(infos.get("type")).toString()) {
                case "normal":
                    {
                        String targetName = (String) infos.get("targetName");
                        ops.putNormalTable(schemaName, tableName, createTableSql, targetName);
                        break;
                    }
                case "global":
                    {
                        ops.putGlobalTable(schemaName, tableName, createTableSql);
                        break;
                    }
                case "range":
                    {
                        ops.putRangeTable(schemaName, tableName, createTableSql, infos);
                        break;
                    }
                case "hash":
                    {
                        ops.putHashTable(schemaName, tableName, createTableSql, infos);
                        break;
                    }
            }
        }
        ops.commit();
        MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
        TableHandler table = metadataManager.getTable(schemaName, tableName);
        if (table == null) {
            throw new MycatException("create table fail:" + schemaName + "." + tableName);
        }
    }
}
Also used : ClusterConfig(io.mycat.config.ClusterConfig) SQLUtils(com.alibaba.druid.sql.SQLUtils) Logger(org.slf4j.Logger) io.mycat(io.mycat) SQLRequest(io.mycat.sqlhandler.SQLRequest) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) Vertx(io.vertx.core.Vertx) LoggerFactory(org.slf4j.LoggerFactory) AbstractSQLHandler(io.mycat.sqlhandler.AbstractSQLHandler) MycatRouterConfigOps.getAutoHashProperties(io.mycat.config.MycatRouterConfigOps.getAutoHashProperties) ConfigUpdater(io.mycat.sqlhandler.ConfigUpdater) Future(io.vertx.core.Future) Objects(java.util.Objects) Lock(io.vertx.core.shareddata.Lock) MycatRouterConfigOps(io.mycat.config.MycatRouterConfigOps) Map(java.util.Map) Optional(java.util.Optional) JsonUtil(io.mycat.util.JsonUtil) MycatRouterConfigOps(io.mycat.config.MycatRouterConfigOps) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement) ClusterConfig(io.mycat.config.ClusterConfig)

Example 20 with ClusterConfig

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

the class StdStorageManagerImpl method getPrototypeDatasourceConfig.

@Nullable
public static Optional<DatasourceConfig> getPrototypeDatasourceConfig(StorageManager fileStorageManager) {
    KV<ClusterConfig> clusterConfigKV = fileStorageManager.get(ClusterConfig.class);
    KV<DatasourceConfig> datasourceConfigKV = fileStorageManager.get(DatasourceConfig.class);
    Optional<ClusterConfig> prototypeOptional = clusterConfigKV.get("prototype");
    Optional<DatasourceConfig> datasourceConfigOptional = prototypeOptional.flatMap(clusterConfig -> {
        List<String> masters = Optional.ofNullable(clusterConfig.getMasters()).orElse(Collections.emptyList());
        List<String> replicas = Optional.ofNullable(clusterConfig.getReplicas()).orElse(Collections.emptyList());
        List<String> strings = new ArrayList<>();
        strings.addAll(masters);
        strings.addAll(replicas);
        return strings.stream().map(i -> datasourceConfigKV.get(i)).filter(i -> i != null).findFirst();
    }).orElse(Optional.ofNullable(datasourceConfigKV.get("prototype")).orElse(datasourceConfigKV.get("prototypeDs")));
    DatasourceConfig configPrototypeDs = datasourceConfigOptional.orElse(null);
    if (configPrototypeDs == null) {
        List<DatasourceConfig> values = datasourceConfigKV.values();
        if (values.isEmpty()) {
        // 不开启db
        } else {
            configPrototypeDs = values.get(0);
        }
    }
    return Optional.ofNullable(configPrototypeDs);
}
Also used : ClusterConfig(io.mycat.config.ClusterConfig) java.util(java.util) Logger(org.slf4j.Logger) MycatException(io.mycat.MycatException) Predicate(java.util.function.Predicate) SQLCallStatementHandler(io.mycat.sqlhandler.ddl.SQLCallStatementHandler) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) KVObject(io.mycat.config.KVObject) DatasourceConfig(io.mycat.config.DatasourceConfig) NotNull(org.jetbrains.annotations.NotNull) DatasourceConfig(io.mycat.config.DatasourceConfig) ClusterConfig(io.mycat.config.ClusterConfig) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ClusterConfig (io.mycat.config.ClusterConfig)31 DatasourceConfig (io.mycat.config.DatasourceConfig)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)7 CreateClusterHint (io.mycat.hint.CreateClusterHint)6 CreateDataSourceHint (io.mycat.hint.CreateDataSourceHint)6 HeartBeatStrategy (io.mycat.replica.heartbeat.HeartBeatStrategy)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ReplicaSelectorManager (io.mycat.replica.ReplicaSelectorManager)3 SchemaHandler (io.mycat.calcite.table.SchemaHandler)2 ServerConfig (io.mycat.config.ServerConfig)2 DruidDatasourceProvider (io.mycat.datasource.jdbc.DruidDatasourceProvider)2 JdbcConnectionManager (io.mycat.datasource.jdbc.datasource.JdbcConnectionManager)2 NameMap (io.mycat.util.NameMap)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2