Search in sources :

Example 1 with CacheService

use of io.mycat.cache.CacheService in project Mycat-Server by MyCATApache.

the class ShowCache method execute.

public static void execute(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    CacheService cacheService = MycatServer.getInstance().getCacheService();
    for (Map.Entry<String, CachePool> entry : cacheService.getAllCachePools().entrySet()) {
        String cacheName = entry.getKey();
        CachePool cachePool = entry.getValue();
        if (cachePool instanceof LayerCachePool) {
            for (Map.Entry<String, CacheStatic> staticsEntry : ((LayerCachePool) cachePool).getAllCacheStatic().entrySet()) {
                RowDataPacket row = getRow(cacheName + '.' + staticsEntry.getKey(), staticsEntry.getValue(), c.getCharset());
                row.packetId = ++packetId;
                buffer = row.write(buffer, c, true);
            }
        } else {
            RowDataPacket row = getRow(cacheName, cachePool.getCacheStatic(), c.getCharset());
            row.packetId = ++packetId;
            buffer = row.write(buffer, c, true);
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) LayerCachePool(io.mycat.cache.LayerCachePool) CachePool(io.mycat.cache.CachePool) CacheStatic(io.mycat.cache.CacheStatic) LayerCachePool(io.mycat.cache.LayerCachePool) FieldPacket(io.mycat.net.mysql.FieldPacket) Map(java.util.Map) CacheService(io.mycat.cache.CacheService)

Example 2 with CacheService

use of io.mycat.cache.CacheService in project Mycat-Server by MyCATApache.

the class DDLRouteTest method testDDLDefaultNode.

@Test
public void testDDLDefaultNode() throws Exception {
    SchemaConfig schema = schemaMap.get("solo1");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // create table/view/function/..
    String sql = " create table company(idd int)";
    sql = RouterUtil.getFixedSql(sql);
    String upsql = sql.toUpperCase();
    //TODO:modify by zhuam 小写表名,转为大写比较
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getCreateTablePos(upsql, 0));
    tablename = tablename.toUpperCase();
    List<String> dataNodes = new ArrayList<>();
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // drop table test
    sql = " drop table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // drop table test
    sql = " drop table if exists COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    //alter table
    sql = "   alter table COMPANY add COLUMN name int ;";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    //truncate table;
    sql = " truncate table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getTruncateTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    if (nodeSize == 0 && schema.getDataNode() != null) {
        nodeSize = 1;
    }
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 3 with CacheService

use of io.mycat.cache.CacheService in project Mycat-Server by MyCATApache.

the class DDLRouteTest method testSpecialCharDDL.

@Test
public void testSpecialCharDDL() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // alter table test
    String sql = " ALTER TABLE COMPANY\r\nADD COLUMN TEST  VARCHAR(255) NULL AFTER CREATE_DATE,\r\n CHARACTER SET = UTF8";
    sql = RouterUtil.getFixedSql(sql);
    List<String> dataNodes = new ArrayList<>();
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(sql, 0));
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 4 with CacheService

use of io.mycat.cache.CacheService in project Mycat-Server by MyCATApache.

the class HintDBTypeTest method testHint.

/**
     * 测试注解
     *
     * @throws Exception
     */
@Test
public void testHint() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    //使用注解(新注解,/*!mycat*/),runOnSlave=false 强制走主节点
    String sql = "/*!mycat:db_type=master*/select * from employee where sharding_id=1";
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
    Assert.assertTrue(!rrs.getRunOnSlave());
    //使用注解(新注解,/*#mycat*/),runOnSlave=false 强制走主节点
    sql = "/*#mycat:db_type=master*/select * from employee where sharding_id=1";
    rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
    Assert.assertTrue(!rrs.getRunOnSlave());
    //使用注解(新注解,/*mycat*/),runOnSlave=false 强制走主节点
    sql = "/*mycat:db_type=master*/select * from employee where sharding_id=1";
    rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
    Assert.assertTrue(!rrs.getRunOnSlave());
    //不使用注解,runOnSlave=null, 根据读写分离策略走主从库
    sql = "select * from employee where sharding_id=1";
    rrs = routerService.route(new SystemConfig(), schema, ServerParse.SELECT, sql, "UTF-8", null);
    Assert.assertTrue(rrs.getRunOnSlave() == null);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) CacheService(io.mycat.cache.CacheService) Test(org.junit.Test)

Example 5 with CacheService

use of io.mycat.cache.CacheService in project Mycat-Server by MyCATApache.

the class DDLRouteTest method testDDL.

/**
     * ddl deal test
     *
     * @throws Exception
     */
@Test
public void testDDL() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // create table/view/function/..
    String sql = " create table company(idd int)";
    sql = RouterUtil.getFixedSql(sql);
    String upsql = sql.toUpperCase();
    //TODO : modify by zhuam
    // 小写表名,需要额外转为 大写 做比较
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getCreateTablePos(upsql, 0));
    tablename = tablename.toUpperCase();
    List<String> dataNodes = new ArrayList<>();
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // drop table test
    sql = " drop table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    //alter table
    sql = "   alter table COMPANY add COLUMN name int ;";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    //truncate table;
    sql = " truncate table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getTruncateTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Aggregations

CacheService (io.mycat.cache.CacheService)6 SchemaConfig (io.mycat.config.model.SchemaConfig)5 SystemConfig (io.mycat.config.model.SystemConfig)5 Test (org.junit.Test)5 RouteResultset (io.mycat.route.RouteResultset)4 RouteService (io.mycat.route.RouteService)4 TableConfig (io.mycat.config.model.TableConfig)3 ArrayList (java.util.ArrayList)3 CachePool (io.mycat.cache.CachePool)1 CacheStatic (io.mycat.cache.CacheStatic)1 LayerCachePool (io.mycat.cache.LayerCachePool)1 EOFPacket (io.mycat.net.mysql.EOFPacket)1 FieldPacket (io.mycat.net.mysql.FieldPacket)1 RowDataPacket (io.mycat.net.mysql.RowDataPacket)1 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1