OrchestrationFacade.java 6.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 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.ListenerFactory;
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;
    
53 54
    private final ListenerFactory listenerManager;
    
T
terrymanu 已提交
55 56 57 58
    public OrchestrationFacade(final OrchestrationConfiguration config) {
        this.config = config;
        configurationService = new ConfigurationService(config);
        instanceStateService = new InstanceStateService(config);
59
        dataSourceService = new DataSourceService(config);
60
        listenerManager = new ListenerFactory(config);
T
terrymanu 已提交
61 62 63 64
    }
    
    /**
     * Initial all orchestration actions for sharding data source.
T
terrymanu 已提交
65
     * 
T
terrymanu 已提交
66 67
     * @param dataSourceMap data source map
     * @param shardingRuleConfig sharding rule configuration
H
haocao 已提交
68
     * @param configMap config map
T
terrymanu 已提交
69 70
     * @param props sharding properties
     * @param shardingDataSource sharding datasource
71
     * @throws SQLException SQL exception
T
terrymanu 已提交
72
     */
T
terrymanu 已提交
73
    public void initShardingOrchestration(
H
haocao 已提交
74 75
            final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, final Map<String, Object> configMap, 
            final Properties props, final ShardingDataSource shardingDataSource) throws SQLException {
T
terrymanu 已提交
76
        config.getRegistryCenter().init();
T
terrymanu 已提交
77 78
        if (shardingRuleConfig.getMasterSlaveRuleConfigs().isEmpty()) {
            reviseShardingRuleConfigurationForMasterSlave(dataSourceMap, shardingRuleConfig);
T
terrymanu 已提交
79
        }
80 81
        configurationService.persistShardingConfiguration(getActualDataSourceMapForMasterSlave(dataSourceMap), shardingRuleConfig, props);
        instanceStateService.persistShardingInstanceOnline();
82
        dataSourceService.persistDataSourcesNode();
83
        listenerManager.initShardingListeners(shardingDataSource);
84
        if (dataSourceService.hasDisabledDataSource()) {
H
haocao 已提交
85
            shardingDataSource.renew(dataSourceService.getAvailableShardingRule(), configMap, props);
86
        }
T
terrymanu 已提交
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 115 116 117 118 119 120 121
    }
    
    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 已提交
122
     * 
T
terrymanu 已提交
123 124
     * @param dataSourceMap data source map
     * @param masterSlaveRuleConfig sharding rule configuration
T
terrymanu 已提交
125
     * @param masterSlaveDataSource master-slave datasource
H
haocao 已提交
126
     * @param configMap config map
T
terrymanu 已提交
127
     */
T
terrymanu 已提交
128
    public void initMasterSlaveOrchestration(
H
haocao 已提交
129 130
            final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, 
            final MasterSlaveDataSource masterSlaveDataSource, final Map<String, Object> configMap) {
T
terrymanu 已提交
131
        config.getRegistryCenter().init();
132 133 134
        configurationService.persistMasterSlaveConfiguration(dataSourceMap, masterSlaveRuleConfig);
        instanceStateService.persistMasterSlaveInstanceOnline();
        dataSourceService.persistDataSourcesNode();
135
        listenerManager.initMasterSlaveListeners(masterSlaveDataSource);
136
        if (dataSourceService.hasDisabledDataSource()) {
H
haocao 已提交
137
            masterSlaveDataSource.renew(dataSourceService.getAvailableMasterSlaveRule(), configMap);
138
        }
T
terrymanu 已提交
139 140
    }
}