OrchestrationFacade.java 7.5 KB
Newer Older
H
haocao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

T
terrymanu 已提交
18 19
package io.shardingjdbc.orchestration.internal;

20
import com.google.common.base.Preconditions;
T
terrymanu 已提交
21 22 23 24
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
T
terrymanu 已提交
25
import io.shardingjdbc.orchestration.api.config.OrchestrationConfiguration;
T
terrymanu 已提交
26
import io.shardingjdbc.orchestration.internal.config.ConfigurationService;
27
import io.shardingjdbc.orchestration.internal.listener.ListenerFactory;
28 29
import io.shardingjdbc.orchestration.internal.state.datasource.DataSourceService;
import io.shardingjdbc.orchestration.internal.state.instance.InstanceStateService;
30 31 32 33 34 35
import io.shardingjdbc.orchestration.reg.api.CoordinatorRegistryCenter;
import io.shardingjdbc.orchestration.reg.api.RegistryCenterConfiguration;
import io.shardingjdbc.orchestration.reg.etcd.EtcdConfiguration;
import io.shardingjdbc.orchestration.reg.etcd.EtcdRegistryCenter;
import io.shardingjdbc.orchestration.reg.zookeeper.ZookeeperConfiguration;
import io.shardingjdbc.orchestration.reg.zookeeper.ZookeeperRegistryCenter;
T
terrymanu 已提交
36 37

import javax.sql.DataSource;
38
import java.sql.SQLException;
T
terrymanu 已提交
39 40 41 42 43 44 45 46 47
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * Orchestration service facade.
 *
 * @author zhangliang
48
 * @author caohao
T
terrymanu 已提交
49 50 51 52 53 54 55
 */
public final class OrchestrationFacade {
    
    private final ConfigurationService configurationService;
    
    private final InstanceStateService instanceStateService;
    
56 57
    private final DataSourceService dataSourceService;
    
58 59
    private final ListenerFactory listenerManager;
    
T
terrymanu 已提交
60
    public OrchestrationFacade(final OrchestrationConfiguration config) {
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        CoordinatorRegistryCenter regCenter = createCoordinatorRegistryCenter(config.getRegCenterConfig());
        configurationService = new ConfigurationService(config, regCenter);
        instanceStateService = new InstanceStateService(config, regCenter);
        dataSourceService = new DataSourceService(config, regCenter);
        listenerManager = new ListenerFactory(config, regCenter);
    }
    
    private CoordinatorRegistryCenter createCoordinatorRegistryCenter(final RegistryCenterConfiguration regCenterConfig) {
        Preconditions.checkNotNull(regCenterConfig, "Registry center configuration cannot be null.");
        if (regCenterConfig instanceof ZookeeperConfiguration) {
            return new ZookeeperRegistryCenter((ZookeeperConfiguration) regCenterConfig);
        }
        if (regCenterConfig instanceof EtcdConfiguration) {
            return new EtcdRegistryCenter((EtcdConfiguration) regCenterConfig);
        }
        throw new UnsupportedOperationException(regCenterConfig.getClass().getName());
T
terrymanu 已提交
77 78 79 80
    }
    
    /**
     * Initial all orchestration actions for sharding data source.
T
terrymanu 已提交
81
     * 
T
terrymanu 已提交
82 83
     * @param dataSourceMap data source map
     * @param shardingRuleConfig sharding rule configuration
H
haocao 已提交
84
     * @param configMap config map
T
terrymanu 已提交
85 86
     * @param props sharding properties
     * @param shardingDataSource sharding datasource
87
     * @throws SQLException SQL exception
T
terrymanu 已提交
88
     */
T
terrymanu 已提交
89
    public void initShardingOrchestration(
H
haocao 已提交
90 91
            final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, final Map<String, Object> configMap, 
            final Properties props, final ShardingDataSource shardingDataSource) throws SQLException {
T
terrymanu 已提交
92 93
        if (shardingRuleConfig.getMasterSlaveRuleConfigs().isEmpty()) {
            reviseShardingRuleConfigurationForMasterSlave(dataSourceMap, shardingRuleConfig);
T
terrymanu 已提交
94
        }
H
haocao 已提交
95
        configurationService.persistShardingConfiguration(getActualDataSourceMapForMasterSlave(dataSourceMap), shardingRuleConfig, configMap, props);
96
        instanceStateService.persistShardingInstanceOnline();
97
        dataSourceService.persistDataSourcesNode();
98
        listenerManager.initShardingListeners(shardingDataSource);
99
        if (dataSourceService.hasDisabledDataSource()) {
H
haocao 已提交
100
            shardingDataSource.renew(dataSourceService.getAvailableShardingRule(), props);
101
        }
T
terrymanu 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    }
    
    private void reviseShardingRuleConfigurationForMasterSlave(final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig) {
        for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
            if (entry.getValue() instanceof MasterSlaveDataSource) {
                MasterSlaveDataSource masterSlaveDataSource = (MasterSlaveDataSource) entry.getValue();
                shardingRuleConfig.getMasterSlaveRuleConfigs().add(getMasterSlaveRuleConfiguration(masterSlaveDataSource));
            }
        }
    }
    
    private Map<String, DataSource> getActualDataSourceMapForMasterSlave(final Map<String, DataSource> dataSourceMap) {
        Map<String, DataSource> result = new HashMap<>();
        for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
            if (entry.getValue() instanceof MasterSlaveDataSource) {
                MasterSlaveDataSource masterSlaveDataSource = (MasterSlaveDataSource) entry.getValue();
                result.putAll(masterSlaveDataSource.getAllDataSources());
            } else {
                result.put(entry.getKey(), entry.getValue());
            }
        }
        return result;
    }
    
    private MasterSlaveRuleConfiguration getMasterSlaveRuleConfiguration(final MasterSlaveDataSource masterSlaveDataSource) {
        MasterSlaveRuleConfiguration result = new MasterSlaveRuleConfiguration();
        result.setName(masterSlaveDataSource.getMasterSlaveRule().getName());
        result.setMasterDataSourceName(masterSlaveDataSource.getMasterSlaveRule().getMasterDataSourceName());
        result.setSlaveDataSourceNames(masterSlaveDataSource.getMasterSlaveRule().getSlaveDataSourceMap().keySet());
        result.setLoadBalanceAlgorithmClassName(masterSlaveDataSource.getMasterSlaveRule().getStrategy().getClass().getName());
        return result;
    }
    
    /**
     * Initial all orchestration actions for master-slave data source.
T
terrymanu 已提交
137
     * 
T
terrymanu 已提交
138 139
     * @param dataSourceMap data source map
     * @param masterSlaveRuleConfig sharding rule configuration
T
terrymanu 已提交
140
     * @param masterSlaveDataSource master-slave datasource
H
haocao 已提交
141
     * @param configMap config map
T
terrymanu 已提交
142
     */
T
terrymanu 已提交
143
    public void initMasterSlaveOrchestration(
H
haocao 已提交
144 145
            final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, 
            final MasterSlaveDataSource masterSlaveDataSource, final Map<String, Object> configMap) {
H
haocao 已提交
146
        configurationService.persistMasterSlaveConfiguration(dataSourceMap, masterSlaveRuleConfig, configMap);
147 148
        instanceStateService.persistMasterSlaveInstanceOnline();
        dataSourceService.persistDataSourcesNode();
149
        listenerManager.initMasterSlaveListeners(masterSlaveDataSource);
150
        if (dataSourceService.hasDisabledDataSource()) {
H
haocao 已提交
151
            masterSlaveDataSource.renew(dataSourceService.getAvailableMasterSlaveRule());
152
        }
T
terrymanu 已提交
153 154
    }
}