Search in sources :

Example 1 with CacheService

use of com.actiontech.dble.cache.CacheService in project dble by actiontech.

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(false);
    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 = "company";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(com.actiontech.dble.config.model.TableConfig) CacheService(com.actiontech.dble.cache.CacheService) Test(org.junit.Test)

Example 2 with CacheService

use of com.actiontech.dble.cache.CacheService in project dble by actiontech.

the class DDLRouteTest method testDDLDefaultNode.

@Test
public void testDDLDefaultNode() throws Exception {
    SchemaConfig schema = schemaMap.get("solo1");
    CacheService cacheService = new CacheService(false);
    RouteService routerService = new RouteService(cacheService);
    // create table/view/function/..
    String sql = " create table company(idd int)";
    sql = RouterUtil.getFixedSql(sql);
    String upsql = sql.toUpperCase();
    String tablename = "COMPANY";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, 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 = "COMPANY";
    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(schema, sqlType, sql, null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(com.actiontech.dble.config.model.TableConfig) CacheService(com.actiontech.dble.cache.CacheService) Test(org.junit.Test)

Example 3 with CacheService

use of com.actiontech.dble.cache.CacheService in project dble by actiontech.

the class DDLRouteTest method testSpecialCharDDL.

@Test
public void testSpecialCharDDL() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    CacheService cacheService = new CacheService(false);
    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 = "COMPANY";
    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(schema, sqlType, sql, null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(com.actiontech.dble.config.model.TableConfig) CacheService(com.actiontech.dble.cache.CacheService) Test(org.junit.Test)

Example 4 with CacheService

use of com.actiontech.dble.cache.CacheService in project dble by actiontech.

the class HintDBTypeTest method testHint.

/**
 * testHint
 *
 * @throws Exception
 */
@Test
public void testHint() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    // (new hint,/*!dble*/),runOnSlave=false force master
    String sql = "/*!dble:db_type=master*/select * from employee where sharding_id=1";
    CacheService cacheService = new CacheService(false);
    RouteService routerService = new RouteService(cacheService);
    RouteResultset rrs = routerService.route(schema, ServerParse.SELECT, sql, null);
    Assert.assertTrue(!rrs.getRunOnSlave());
    // (new hint,/*#dble*/),runOnSlave=false force master
    sql = "/*#dble:db_type=master*/select * from employee where sharding_id=1";
    rrs = routerService.route(schema, ServerParse.SELECT, sql, null);
    Assert.assertTrue(!rrs.getRunOnSlave());
    // (new hint,/*dble*/),runOnSlave=false force master
    sql = "/*dble:db_type=master*/select * from employee where sharding_id=1";
    rrs = routerService.route(schema, ServerParse.SELECT, sql, null);
    Assert.assertTrue(!rrs.getRunOnSlave());
    // no hint ,runOnSlave=null
    sql = "select * from employee where sharding_id=1";
    rrs = routerService.route(schema, ServerParse.SELECT, sql, null);
    Assert.assertTrue(rrs.getRunOnSlave() == null);
}
Also used : SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) CacheService(com.actiontech.dble.cache.CacheService) Test(org.junit.Test)

Example 5 with CacheService

use of com.actiontech.dble.cache.CacheService in project dble by actiontech.

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.getPacketId();
    CacheService cacheService = DbleServer.getInstance().getCacheService();
    for (Map.Entry<String, CachePool> entry : cacheService.getAllCachePools().entrySet()) {
        String cacheName = entry.getKey();
        CachePool cachePool = entry.getValue();
        if (cachePool != null) {
            if (cachePool instanceof LayerCachePool) {
                for (Map.Entry<String, CacheStatic> staticsEntry : ((LayerCachePool) cachePool).getAllCacheStatic().entrySet()) {
                    RowDataPacket row = getRow(cacheName + '.' + staticsEntry.getKey(), staticsEntry.getValue(), c.getCharset().getResults());
                    row.setPacketId(++packetId);
                    buffer = row.write(buffer, c, true);
                }
            } else {
                RowDataPacket row = getRow(cacheName, cachePool.getCacheStatic(), c.getCharset().getResults());
                row.setPacketId(++packetId);
                buffer = row.write(buffer, c, true);
            }
        }
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.setPacketId(++packetId);
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(com.actiontech.dble.net.mysql.RowDataPacket) EOFPacket(com.actiontech.dble.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) CachePool(com.actiontech.dble.cache.CachePool) LayerCachePool(com.actiontech.dble.cache.LayerCachePool) CacheStatic(com.actiontech.dble.cache.CacheStatic) LayerCachePool(com.actiontech.dble.cache.LayerCachePool) FieldPacket(com.actiontech.dble.net.mysql.FieldPacket) Map(java.util.Map) CacheService(com.actiontech.dble.cache.CacheService)

Aggregations

CacheService (com.actiontech.dble.cache.CacheService)6 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)4 Test (org.junit.Test)4 TableConfig (com.actiontech.dble.config.model.TableConfig)3 ArrayList (java.util.ArrayList)3 DirectByteBufferPool (com.actiontech.dble.buffer.DirectByteBufferPool)1 CachePool (com.actiontech.dble.cache.CachePool)1 CacheStatic (com.actiontech.dble.cache.CacheStatic)1 LayerCachePool (com.actiontech.dble.cache.LayerCachePool)1 SystemConfig (com.actiontech.dble.config.model.SystemConfig)1 TxnLogProcessor (com.actiontech.dble.log.transaction.TxnLogProcessor)1 ManagerConnectionFactory (com.actiontech.dble.manager.ManagerConnectionFactory)1 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)1 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)1 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)1 RouteService (com.actiontech.dble.route.RouteService)1 ServerConnectionFactory (com.actiontech.dble.server.ServerConnectionFactory)1 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1