Search in sources :

Example 11 with DalQueryDao

use of com.ctrip.platform.dal.dao.DalQueryDao in project dal by ctripcorp.

the class DalWatcherTest method testQueryObjectWhenStarup.

@Test
public void testQueryObjectWhenStarup() throws Exception {
    final AtomicReference<Boolean> done = new AtomicReference<>();
    final AtomicReference<Boolean> p = new AtomicReference<>();
    done.set(false);
    p.set(false);
    final DalQueryDao client = new DalQueryDao(DATABASE_NAME);
    Runnable query = new Runnable() {

        public void run() {
            while (!done.get() && true) {
                String sql = "SELECT quantity FROM " + TABLE_NAME;
                StatementParameters param = new StatementParameters();
                DalHints hints = new DalHints();
                try {
                    client.queryFrom(sql, param, hints, Integer.class, 0, 10);
                } catch (SQLException e) {
                    if (!p.get() && !e.getMessage().contains("Invalid object name 'dal_client_test'.")) {
                        e.printStackTrace();
                        p.set(true);
                    }
                    break;
                }
                if (done.get())
                    break;
            }
        }
    };
    DalClientFactory.shutdownFactory();
    System.out.println("Factory is down");
    int j = 3000;
    while (j-- > 0) {
        new Thread(query).start();
    }
    System.out.println("start threads...");
    int i = 3;
    while (i-- > 0) {
        System.out.println("tic");
        Thread.sleep(1 * 10000);
    }
    System.out.println("done main");
    DalClientFactory.shutdownFactory();
    done.set(true);
    System.out.println("cool down");
    Thread.sleep(2 * 1000);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) AtomicReference(java.util.concurrent.atomic.AtomicReference) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao) Test(org.junit.Test)

Example 12 with DalQueryDao

use of com.ctrip.platform.dal.dao.DalQueryDao in project dal by ctripcorp.

the class SlaveFreshnessScannerMysqlTest method setUpBeforeClass.

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    freshnessMap.put("shard_0", 3);
    freshnessMap.put("shard_1", 5);
    freshnessMap.put("dal_shard_0", 7);
    freshnessMap.put("dal_shard_1", 9);
    Map<String, Integer> shardMap = new HashMap<>();
    shardMap.put("shard_0", 3);
    shardMap.put("shard_1", 5);
    freshnessShardMap.put(0, shardMap);
    shardMap = new HashMap<>();
    shardMap.put("dal_shard_0", 7);
    shardMap.put("dal_shard_1", 9);
    freshnessShardMap.put(1, shardMap);
    DalQueryDao dao = new DalQueryDao(DATABASE_NAME);
    String id = dao.queryForObject(GET_DB_NAME, new StatementParameters(), new DalHints(), String.class);
    // make sure warmup is done.
    Thread.sleep(1 * 1000);
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) HashMap(java.util.HashMap) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao) BeforeClass(org.junit.BeforeClass)

Example 13 with DalQueryDao

use of com.ctrip.platform.dal.dao.DalQueryDao in project dal by ctripcorp.

the class SlaveFreshnessScannerMysqlTest method testShardBelow.

private void testShardBelow(int shardId, int freshness) throws SQLException {
    DalQueryDao dao = new DalQueryDao(SHARD_DATABASE_NAME);
    for (int i = 0; i < 100; i++) {
        String id = dao.queryForObject(GET_DB_NAME, new StatementParameters(), new DalHints().freshness(freshness).inShard(shardId), String.class);
        Assert.assertTrue(freshnessShardMap.get(shardId).get(id) <= freshness);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao)

Example 14 with DalQueryDao

use of com.ctrip.platform.dal.dao.DalQueryDao in project dal by ctripcorp.

the class SlaveFreshnessScannerMysqlTest method testShardMaster.

private void testShardMaster(int freshness) throws SQLException {
    DalQueryDao dao = new DalQueryDao(SHARD_DATABASE_NAME);
    int shardId = 0;
    while (shardId < 2) {
        for (int i = 0; i < 100; i++) {
            String id = dao.queryForObject(GET_DB_NAME, new StatementParameters(), new DalHints().freshness(freshness).inShard(shardId), String.class);
            Assert.assertEquals(masterShard[shardId], id);
        }
        shardId++;
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao)

Example 15 with DalQueryDao

use of com.ctrip.platform.dal.dao.DalQueryDao in project dal by ctripcorp.

the class SlaveFreshnessScannerMysqlTest method testMaster.

private void testMaster(int freshness) throws SQLException {
    DalQueryDao dao = new DalQueryDao(DATABASE_NAME);
    for (int i = 0; i < 100; i++) {
        String id = dao.queryForObject(GET_DB_NAME, new StatementParameters(), new DalHints().freshness(freshness), String.class);
        Assert.assertEquals(master, id);
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao)

Aggregations

DalQueryDao (com.ctrip.platform.dal.dao.DalQueryDao)20 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)19 DalHints (com.ctrip.platform.dal.dao.DalHints)13 Test (org.junit.Test)9 SQLException (java.sql.SQLException)7 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)6 DalDefaultJpaMapper (com.ctrip.platform.dal.dao.helper.DalDefaultJpaMapper)4 DalColumnMapRowMapper (com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper)3 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 DalClient (com.ctrip.platform.dal.dao.DalClient)1 HashMap (java.util.HashMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BeforeClass (org.junit.BeforeClass)1