17
17
18
18
package org .apache .shardingsphere .mode .manager .standalone .persist .service ;
19
19
20
+ import lombok .extern .slf4j .Slf4j ;
20
21
import org .apache .shardingsphere .infra .config .rule .RuleConfiguration ;
22
+ import org .apache .shardingsphere .infra .database .core .type .DatabaseTypeRegistry ;
21
23
import org .apache .shardingsphere .infra .datasource .pool .props .domain .DataSourcePoolProperties ;
22
24
import org .apache .shardingsphere .infra .metadata .ShardingSphereMetaData ;
23
25
import org .apache .shardingsphere .infra .metadata .database .ShardingSphereDatabase ;
24
26
import org .apache .shardingsphere .infra .metadata .database .rule .RuleMetaData ;
27
+ import org .apache .shardingsphere .infra .metadata .database .schema .builder .GenericSchemaBuilder ;
28
+ import org .apache .shardingsphere .infra .metadata .database .schema .builder .GenericSchemaBuilderMaterial ;
29
+ import org .apache .shardingsphere .infra .metadata .database .schema .manager .GenericSchemaManager ;
25
30
import org .apache .shardingsphere .infra .metadata .database .schema .model .ShardingSphereSchema ;
26
31
import org .apache .shardingsphere .infra .metadata .database .schema .model .ShardingSphereTable ;
27
32
import org .apache .shardingsphere .infra .metadata .database .schema .model .ShardingSphereView ;
28
- import org .apache .shardingsphere .mode .node .path .version .MetaDataVersion ;
29
33
import org .apache .shardingsphere .infra .rule .scope .GlobalRule ;
30
34
import org .apache .shardingsphere .infra .rule .scope .GlobalRule .GlobalRuleChangedType ;
31
35
import org .apache .shardingsphere .infra .spi .type .ordered .cache .OrderedServicesCache ;
36
+ import org .apache .shardingsphere .mode .exception .LoadTableMetaDataFailedException ;
32
37
import org .apache .shardingsphere .mode .metadata .MetaDataContexts ;
33
38
import org .apache .shardingsphere .mode .metadata .changed .RuleItemChangedNodePathBuilder ;
34
39
import org .apache .shardingsphere .mode .metadata .manager .ActiveVersionChecker ;
37
42
import org .apache .shardingsphere .mode .metadata .persist .metadata .DatabaseMetaDataPersistFacade ;
38
43
import org .apache .shardingsphere .mode .metadata .refresher .metadata .util .TableRefreshUtils ;
39
44
import org .apache .shardingsphere .mode .node .path .type .database .metadata .rule .DatabaseRuleNodePath ;
45
+ import org .apache .shardingsphere .mode .node .path .version .MetaDataVersion ;
40
46
import org .apache .shardingsphere .mode .node .path .version .VersionNodePath ;
41
47
import org .apache .shardingsphere .mode .persist .service .MetaDataManagerPersistService ;
42
48
import org .apache .shardingsphere .single .config .SingleRuleConfiguration ;
46
52
import java .util .Collection ;
47
53
import java .util .Collections ;
48
54
import java .util .Map ;
55
+ import java .util .Map .Entry ;
49
56
import java .util .Optional ;
50
57
import java .util .Properties ;
51
58
import java .util .stream .Collectors ;
52
59
53
60
/**
54
61
* Standalone meta data manager persist service.
55
62
*/
63
+ @ Slf4j
56
64
public final class StandaloneMetaDataManagerPersistService implements MetaDataManagerPersistService {
57
65
58
66
private final MetaDataContextManager metaDataContextManager ;
@@ -217,13 +225,15 @@ public void alterRuleConfiguration(final ShardingSphereDatabase database, final
217
225
if (null == toBeAlteredRuleConfig ) {
218
226
return ;
219
227
}
228
+ Collection <String > needReloadTables = getNeedReloadTables (database , toBeAlteredRuleConfig );
220
229
for (MetaDataVersion each : metaDataPersistFacade .getDatabaseRuleService ().persist (database .getName (), Collections .singleton (toBeAlteredRuleConfig ))) {
221
230
Optional <DatabaseRuleNodePath > databaseRuleNodePath = ruleItemChangedNodePathBuilder .build (database .getName (), new VersionNodePath (each .getNodePath ()).getActiveVersionPath ());
222
231
if (databaseRuleNodePath .isPresent ()
223
232
&& new ActiveVersionChecker (metaDataPersistFacade .getRepository ()).checkSame (new VersionNodePath (databaseRuleNodePath .get ()), each .getActiveVersion ())) {
224
233
metaDataContextManager .getDatabaseRuleItemManager ().alter (databaseRuleNodePath .get ());
225
234
}
226
235
}
236
+ reloadAlteredTables (database .getName (), needReloadTables );
227
237
clearServiceCache ();
228
238
}
229
239
@@ -232,16 +242,45 @@ public void removeRuleConfigurationItem(final ShardingSphereDatabase database, f
232
242
if (null == toBeRemovedRuleConfig ) {
233
243
return ;
234
244
}
245
+ Collection <String > needReloadTables = getNeedReloadTables (database , toBeRemovedRuleConfig );
235
246
Collection <MetaDataVersion > metaDataVersions = metaDataPersistFacade .getDatabaseRuleService ().delete (database .getName (), Collections .singleton (toBeRemovedRuleConfig ));
236
247
for (MetaDataVersion each : metaDataVersions ) {
237
248
Optional <DatabaseRuleNodePath > databaseRuleNodePath = ruleItemChangedNodePathBuilder .build (database .getName (), new VersionNodePath (each .getNodePath ()).getActiveVersionPath ());
238
249
if (databaseRuleNodePath .isPresent ()) {
239
250
metaDataContextManager .getDatabaseRuleItemManager ().drop (databaseRuleNodePath .get ());
240
251
}
241
252
}
253
+ reloadAlteredTables (database .getName (), needReloadTables );
242
254
clearServiceCache ();
243
255
}
244
256
257
+ private void reloadAlteredTables (final String databaseName , final Collection <String > needReloadTables ) {
258
+ MetaDataContexts reloadMetaDataContexts = metaDataContextManager .getMetaDataContexts ();
259
+ ShardingSphereDatabase database = reloadMetaDataContexts .getMetaData ().getDatabase (databaseName );
260
+ GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial (database .getResourceMetaData ().getStorageUnits (),
261
+ database .getRuleMetaData ().getRules (), reloadMetaDataContexts .getMetaData ().getProps (),
262
+ new DatabaseTypeRegistry (database .getProtocolType ()).getDefaultSchemaName (databaseName ));
263
+ try {
264
+ Map <String , ShardingSphereSchema > schemas = GenericSchemaBuilder .build (needReloadTables , database .getProtocolType (), material );
265
+ for (Entry <String , ShardingSphereSchema > entry : schemas .entrySet ()) {
266
+ Collection <ShardingSphereTable > tables = GenericSchemaManager .getToBeAddedTables (entry .getValue (), database .getSchema (entry .getKey ()));
267
+ metaDataPersistFacade .getDatabaseMetaDataFacade ().getTable ().persist (databaseName , entry .getKey (), tables );
268
+ tables .forEach (each -> metaDataContextManager .getDatabaseMetaDataManager ().alterTable (databaseName , entry .getKey (), each ));
269
+ }
270
+ } catch (final SQLException ex ) {
271
+ log .error ("Load table meta failed, databaseName:{}, needReloadTables:{}" , databaseName , needReloadTables , ex );
272
+ throw new LoadTableMetaDataFailedException ();
273
+ }
274
+ }
275
+
276
+ private Collection <String > getNeedReloadTables (final ShardingSphereDatabase originalShardingDatabase , final RuleConfiguration toBeAlteredRuleConfig ) {
277
+ if (toBeAlteredRuleConfig instanceof SingleRuleConfiguration ) {
278
+ Collection <String > originalSingleTables = originalShardingDatabase .getRuleMetaData ().getSingleRule (SingleRule .class ).getConfiguration ().getLogicTableNames ();
279
+ return toBeAlteredRuleConfig .getLogicTableNames ().stream ().filter (each -> !originalSingleTables .contains (each )).collect (Collectors .toList ());
280
+ }
281
+ return toBeAlteredRuleConfig .getLogicTableNames ();
282
+ }
283
+
245
284
@ Override
246
285
public void removeRuleConfiguration (final ShardingSphereDatabase database , final String ruleType ) {
247
286
metaDataPersistFacade .getDatabaseRuleService ().delete (database .getName (), ruleType );
0 commit comments