OrchestrationFacade.java 6.1 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 20 21 22 23
package io.shardingjdbc.orchestration.internal;

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 已提交
24
import io.shardingjdbc.orchestration.api.config.OrchestrationConfiguration;
T
terrymanu 已提交
25
import io.shardingjdbc.orchestration.internal.config.ConfigurationService;
26
import io.shardingjdbc.orchestration.internal.listener.ListenerManager;
27 28
import io.shardingjdbc.orchestration.internal.state.datasource.DataSourceService;
import io.shardingjdbc.orchestration.internal.state.instance.InstanceStateService;
T
terrymanu 已提交
29 30

import javax.sql.DataSource;
31
import java.sql.SQLException;
T
terrymanu 已提交
32 33 34 35 36 37 38 39 40
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * Orchestration service facade.
 *
 * @author zhangliang
41
 * @author caohao
T
terrymanu 已提交
42 43 44
 */
public final class OrchestrationFacade {
    
T
terrymanu 已提交
45 46
    private final OrchestrationConfiguration config;
    
T
terrymanu 已提交
47 48 49 50
    private final ConfigurationService configurationService;
    
    private final InstanceStateService instanceStateService;
    
51 52
    private final DataSourceService dataSourceService;
    
T
terrymanu 已提交
53 54 55 56
    public OrchestrationFacade(final OrchestrationConfiguration config) {
        this.config = config;
        configurationService = new ConfigurationService(config);
        instanceStateService = new InstanceStateService(config);
57
        dataSourceService = new DataSourceService(config);
T
terrymanu 已提交
58 59 60 61
    }
    
    /**
     * Initial all orchestration actions for sharding data source.
T
terrymanu 已提交
62
     * 
T
terrymanu 已提交
63 64
     * @param dataSourceMap data source map
     * @param shardingRuleConfig sharding rule configuration
T
terrymanu 已提交
65 66
     * @param props sharding properties
     * @param shardingDataSource sharding datasource
67
     * @throws SQLException SQL exception
T
terrymanu 已提交
68
     */
T
terrymanu 已提交
69
    public void initShardingOrchestration(
70
            final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, final Properties props, final ShardingDataSource shardingDataSource) throws SQLException {
T
terrymanu 已提交
71
        config.getRegistryCenter().init();
T
terrymanu 已提交
72 73
        if (shardingRuleConfig.getMasterSlaveRuleConfigs().isEmpty()) {
            reviseShardingRuleConfigurationForMasterSlave(dataSourceMap, shardingRuleConfig);
T
terrymanu 已提交
74
        }
75 76
        configurationService.persistShardingConfiguration(getActualDataSourceMapForMasterSlave(dataSourceMap), shardingRuleConfig, props);
        instanceStateService.persistShardingInstanceOnline();
77
        dataSourceService.persistDataSourcesNode();
78
        new ListenerManager(config).initShardingListeners(shardingDataSource);
79
        shardingDataSource.renew(dataSourceService.getAvailableShardingRule(), props);
T
terrymanu 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    }
    
    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 已提交
115
     * 
T
terrymanu 已提交
116 117
     * @param dataSourceMap data source map
     * @param masterSlaveRuleConfig sharding rule configuration
T
terrymanu 已提交
118 119
     * @param masterSlaveDataSource master-slave datasource
     */
T
terrymanu 已提交
120 121
    public void initMasterSlaveOrchestration(
            final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final MasterSlaveDataSource masterSlaveDataSource) {
T
terrymanu 已提交
122
        config.getRegistryCenter().init();
123 124 125 126 127
        configurationService.persistMasterSlaveConfiguration(dataSourceMap, masterSlaveRuleConfig);
        instanceStateService.persistMasterSlaveInstanceOnline();
        dataSourceService.persistDataSourcesNode();
        new ListenerManager(config).initMasterSlaveListeners(masterSlaveDataSource);
        masterSlaveDataSource.renew(dataSourceService.getAvailableMasterSlaveRule());
T
terrymanu 已提交
128 129
    }
}