use of com.ctrip.platform.dal.dao.configure.SelectionContext in project dal by ctripcorp.
the class DatabaseSelectorTest method masterOnlyHaTest.
@Test
public void masterOnlyHaTest() throws DalException {
List<DataBase> ms = new ArrayList<DataBase>();
ms.add(new DataBase(M1, true, "", M1));
ms.add(new DataBase(M2, true, "", M2));
List<DataBase> ss = new ArrayList<DataBase>();
ss.add(new DataBase(S1, false, "", S1));
ss.add(new DataBase(S2, false, "", S2));
ss.add(new DataBase(S3, false, "", S3));
SelectionContext context;
// make sure no slave is qualified
autoMarkdown(S1);
autoMarkdown(S2);
autoMarkdown(S3);
context = getContext(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, true);
assertSelector(context, M2);
context = getContext(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, true);
assertSelector(context, M1);
context = getContext(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, false);
assertSelector(context, M2);
context = getContext(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, false);
assertSelector(context, M1);
context = getContext(new DalHints().setHA(new DalHA().addDB(M1).addDB(M2)), ms, ss, true, false);
assertSelector(context, ErrorCode.NoMoreConnectionToFailOver);
autoMarkdown(M1);
context = getContext(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, true);
assertSelector(context, M2);
context = getContext(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, true);
assertSelector(context, M2);
autoMarkup(M1);
autoMarkdown(M2);
context = getContext(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, true);
assertSelector(context, M1);
context = getContext(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, true);
assertSelector(context, M1);
autoMarkdown(M1);
context = getContext(new DalHints().setHA(new DalHA().addDB(M1)), ms, ss, true, true);
assertSelector(context, ErrorCode.NoMoreConnectionToFailOver);
context = getContext(new DalHints().setHA(new DalHA().addDB(M2)), ms, ss, true, true);
assertSelector(context, ErrorCode.NoMoreConnectionToFailOver);
context = getContext(new DalHints().setHA(new DalHA().addDB(M1).addDB(M2)), ms, ss, true, false);
assertSelector(context, ErrorCode.NoMoreConnectionToFailOver);
}
use of com.ctrip.platform.dal.dao.configure.SelectionContext in project dal by ctripcorp.
the class DatabaseSelectorTest method hasHAMixedMasterAndSlaveButOneSlaveMarkdownTest.
@Test
public void hasHAMixedMasterAndSlaveButOneSlaveMarkdownTest() throws DalException {
List<DataBase> ms = new ArrayList<DataBase>();
ms.add(new DataBase(M1, true, "", M1));
ms.add(new DataBase(M2, true, "", M2));
List<DataBase> ss = new ArrayList<DataBase>();
ss.add(new DataBase(S1, false, "", S1));
ss.add(new DataBase(S2, false, "", S2));
autoMarkdown(S1);
DalHA ha = new DalHA();
ha.addDB(M1);
SelectionContext context = getContext(new DalHints().setHA(ha), ms, ss, false, true);
String dbName = selector.select(context);
Assert.assertTrue(dbName.equals(S2));
// masterOnly
ha = new DalHA();
ha.addDB(M1);
context = getContext(new DalHints().setHA(ha), ms, ss, true, true);
Assert.assertEquals(M2, selector.select(context));
ha = new DalHA();
ha.addDB(M1);
context = getContext(new DalHints().setHA(ha), ms, ss, true, false);
Assert.assertEquals(M2, selector.select(context));
ha = new DalHA();
ha.addDB(M1);
context = getContext(new DalHints().setHA(ha), ms, ss, false, false);
Assert.assertEquals(M2, selector.select(context));
ha = new DalHA();
ha.addDB(M1);
context = getContext(new DalHints().setHA(ha).inDatabase(M1), ms, ss, false, false);
assertSelector(context, ErrorCode.NoMoreConnectionToFailOver);
ha = new DalHA();
ha.addDB(M1);
autoMarkdown(M2);
context = getContext(new DalHints().setHA(ha), ms, ss, false, false);
Assert.assertEquals(M1, selector.select(context));
}
use of com.ctrip.platform.dal.dao.configure.SelectionContext in project dal by ctripcorp.
the class DatabaseSelectorTest method notSelectTest.
@Test
public void notSelectTest() throws DalException {
List<DataBase> ms = new ArrayList<DataBase>();
ms.add(new DataBase(M1, true, "", M1));
ms.add(new DataBase(M2, true, "", M2));
List<DataBase> ss = new ArrayList<DataBase>();
ss.add(new DataBase(S1, false, "", S1));
ss.add(new DataBase(S2, false, "", S2));
SelectionContext context;
context = getContext(new DalHints(), ms, ss, false, false);
String dbName = selector.select(context);
Assert.assertTrue(dbName.equals(M1) || dbName.equals(M2));
context = getContext(new DalHints().inDatabase(M1), ms, ss, false, false);
Assert.assertTrue(selector.select(context).equals(M1));
context = getContext(new DalHints().inDatabase(M2), ms, ss, false, false);
Assert.assertTrue(selector.select(context).equals(M2));
context = getContext(new DalHints().inDatabase(S2), ms, ss, false, false);
assertSelector(context, ErrorCode.InvalidDatabaseKeyName);
// masterOnly
context = getContext(null, ms, ss, true, false);
dbName = selector.select(context);
Assert.assertTrue(dbName.equals(M1) || dbName.equals(M2));
context = getContext(new DalHints().inDatabase(M1), ms, ss, true, false);
Assert.assertTrue(selector.select(context).equals(M1));
context = getContext(new DalHints().inDatabase(M2), ms, ss, true, false);
Assert.assertTrue(selector.select(context).equals(M2));
context = getContext(new DalHints().inDatabase(S2), ms, ss, true, false);
assertSelector(context, ErrorCode.InvalidDatabaseKeyName);
}
use of com.ctrip.platform.dal.dao.configure.SelectionContext in project dal by ctripcorp.
the class DatabaseSelectorTest method onlyHaveMasterHaTest.
@Test
public void onlyHaveMasterHaTest() throws DalException {
List<DataBase> ms = new ArrayList<DataBase>();
ms.add(new DataBase(M1, true, "", M1));
ms.add(new DataBase(M2, true, "", M2));
List<DataBase> ss = null;
SelectionContext context;
Set<String> matched;
matched = newHashSet(M1, M2);
DalHints hints = new DalHints().setHA(new DalHA());
context = getContext(hints, ms, ss, false, true);
assertSelector(context, matched);
context = getContext(hints, ms, ss, false, true);
assertSelector(context, matched);
// reset
matched = newHashSet(M1, M2);
hints = new DalHints().setHA(new DalHA());
context = getContext(hints, ms, ss, false, false);
assertSelector(context, matched);
context = getContext(hints, ms, ss, false, false);
assertSelector(context, matched);
// Master only
matched = newHashSet(M1, M2);
hints = new DalHints().setHA(new DalHA());
context = getContext(hints, ms, ss, true, true);
assertSelector(context, M1, M2);
context = getContext(hints, ms, ss, true, true);
assertSelector(context, M1, M2);
// reset
matched = newHashSet(M1, M2);
hints = new DalHints().setHA(new DalHA());
context = getContext(hints, ms, ss, true, false);
assertSelector(context, M1, M2);
context = getContext(hints, ms, ss, true, false);
assertSelector(context, M1, M2);
}
use of com.ctrip.platform.dal.dao.configure.SelectionContext in project dal by ctripcorp.
the class DalConnectionManager method select.
private String select(String logicDbName, DatabaseSet dbSet, DalHints hints, String shard, boolean isMaster, boolean isSelect) throws DalException {
SelectionContext context = new SelectionContext(logicDbName, hints, shard, isMaster, isSelect);
if (shard == null) {
context.setMasters(dbSet.getMasterDbs());
context.setSlaves(dbSet.getSlaveDbs());
} else {
context.setMasters(dbSet.getMasterDbs(shard));
context.setSlaves(dbSet.getSlaveDbs(shard));
}
return config.getSelector().select(context);
}
Aggregations