Search in sources :

Example 1 with DalMetadataException

use of com.ctrip.framework.dal.cluster.client.exception.DalMetadataException in project dal by ctripcorp.

the class ReadCurrentZoneSlavesOnlyStrategyTest method pickCurrentZoneSlaveOnly.

@Test
public void pickCurrentZoneSlaveOnly() {
    ReadCurrentZoneSlavesOnlyStrategy strategy = new ReadCurrentZoneSlavesOnlyStrategy();
    strategy.init(ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec(), null);
    strategy.currentZone = "shaoy";
    HostSpec hostSpec = strategy.pickRead(new DalHints());
    assertEquals(strategy.currentZone, hostSpec.getTrimLowerCaseZone());
    strategy.currentZone = "";
    try {
        hostSpec = strategy.pickRead(new DalHints());
        fail();
    } catch (Throwable t) {
        assertEquals(true, t instanceof DalMetadataException);
        assertEquals(" has no database in ", t.getMessage());
    }
    strategy.currentZone = "shaxy";
    try {
        hostSpec = strategy.pickRead(new DalHints());
        fail();
    } catch (Throwable t) {
        assertEquals(true, t instanceof DalMetadataException);
        assertEquals(" has no database in " + strategy.currentZone, t.getMessage());
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec(com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec) Test(org.junit.Test)

Example 2 with DalMetadataException

use of com.ctrip.framework.dal.cluster.client.exception.DalMetadataException in project dal by ctripcorp.

the class ReadMasterZoneSlavesOnlyStrategyTest method pickMasterZoneSlaveOnly.

@Test
public void pickMasterZoneSlaveOnly() {
    ReadMasterZoneSlavesOnlyStrategy strategy = new ReadMasterZoneSlavesOnlyStrategy();
    strategy.init(produceHostSpec(), null);
    try {
        strategy.pickMasterZoneSlaveOnly();
        fail();
    } catch (Throwable t) {
        assertTrue(t instanceof DalMetadataException);
    }
    ReadMasterZoneSlavesOnlyStrategy strategy1 = new ReadMasterZoneSlavesOnlyStrategy();
    strategy1.init(masterZoneHasSlave(), null);
    HostSpec hostSpec = strategy1.pickMasterZoneSlaveOnly();
    assertEquals(strategy1.masterZone, hostSpec.getTrimLowerCaseZone());
    assertFalse(hostSpec.isMaster());
}
Also used : DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) Test(org.junit.Test) ReadCurrentZoneSlavesFirstStrategyTest(com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest)

Example 3 with DalMetadataException

use of com.ctrip.framework.dal.cluster.client.exception.DalMetadataException in project dal by ctripcorp.

the class ReadCurrentZoneSlavesOnlyStrategy method init.

@Override
public void init(Set<HostSpec> hostSpecs, CaseInsensitiveProperties strategyProperties) {
    super.init(hostSpecs, strategyProperties);
    for (HostSpec hostSpec : hostSpecs) {
        if (!zoneToHost.containsKey(hostSpec.getTrimLowerCaseZone())) {
            List<HostSpec> hosts = new ArrayList<>();
            hosts.add(hostSpec);
            zoneToHost.put(hostSpec.getTrimLowerCaseZone(), hosts);
        } else {
            zoneToHost.get(hostSpec.getTrimLowerCaseZone()).add(hostSpec);
        }
    }
    if (StringUtils.isTrimmedEmpty(currentZone))
        throw new DalMetadataException(ZONE_MSG_LOST);
}
Also used : DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec)

Example 4 with DalMetadataException

use of com.ctrip.framework.dal.cluster.client.exception.DalMetadataException in project dal by ctripcorp.

the class DatabaseShardImpl method initReadStrategy.

public void initReadStrategy() {
    ClusterRouteStrategyConfig config = databaseShardConfig.getClusterConfig().getRouteStrategyConfig();
    if (config == null || config.multiMaster()) {
        return;
    }
    routeStrategy = (ReadStrategy) config.generate();
    List<Database> databases = new ArrayList<>();
    databases.addAll(masters);
    databases.addAll(slaves);
    Set<HostSpec> hostSpecs = new HashSet<>();
    databases.forEach(database -> {
        ConnectionString connString = database.getConnectionString();
        HostSpec host = HostSpec.of(connString.getPrimaryHost(), connString.getPrimaryPort(), database.getZone(), database.isMaster());
        hostToDataBase.putIfAbsent(host, database);
        hostSpecs.add(host);
    });
    try {
        routeStrategy.init(hostSpecs, new CaseInsensitiveProperties());
    } catch (DalMetadataException error) {
        throw new DalMetadataException(databaseShardConfig.getClusterConfig().getClusterName() + error.getMessage());
    }
}
Also used : CaseInsensitiveProperties(com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties) ClusterRouteStrategyConfig(com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig) DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) Database(com.ctrip.framework.dal.cluster.client.database.Database) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) ConnectionString(com.ctrip.framework.dal.cluster.client.database.ConnectionString)

Example 5 with DalMetadataException

use of com.ctrip.framework.dal.cluster.client.exception.DalMetadataException in project dal by ctripcorp.

the class ReadMasterZoneSlavesOnlyStrategy method init.

@Override
public void init(Set<HostSpec> hostSpecs, CaseInsensitiveProperties strategyProperties) {
    super.init(hostSpecs, strategyProperties);
    for (HostSpec hostSpec : hostSpecs) {
        if (hostSpec.isMaster()) {
            masterZone = hostSpec.getTrimLowerCaseZone();
            continue;
        }
        if (!zoneToHost.containsKey(hostSpec.getTrimLowerCaseZone())) {
            List<HostSpec> hosts = new ArrayList<>();
            hosts.add(hostSpec);
            zoneToHost.put(hostSpec.getTrimLowerCaseZone(), hosts);
        } else {
            zoneToHost.get(hostSpec.getTrimLowerCaseZone()).add(hostSpec);
        }
    }
    if (StringUtils.isTrimmedEmpty(masterZone))
        throw new DalMetadataException(ZONE_MSG_LOST);
}
Also used : DalMetadataException(com.ctrip.framework.dal.cluster.client.exception.DalMetadataException) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec)

Aggregations

HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)5 DalMetadataException (com.ctrip.framework.dal.cluster.client.exception.DalMetadataException)5 Test (org.junit.Test)2 ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)1 Database (com.ctrip.framework.dal.cluster.client.database.Database)1 ClusterRouteStrategyConfig (com.ctrip.framework.dal.cluster.client.multihost.ClusterRouteStrategyConfig)1 ReadCurrentZoneSlavesFirstStrategyTest (com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest)1 ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec (com.ctrip.framework.dal.cluster.client.shard.read.ReadCurrentZoneSlavesFirstStrategyTest.produceHostSpec)1 CaseInsensitiveProperties (com.ctrip.framework.dal.cluster.client.util.CaseInsensitiveProperties)1 DalHints (com.ctrip.platform.dal.dao.DalHints)1