use of org.jumpmind.symmetric.model.NodeGroupLink in project symmetric-ds by JumpMind.
the class LoadFilterService method refreshCache.
protected void refreshCache() {
// get the cache timeout
long cacheTimeoutInMs = parameterService.getLong(ParameterConstants.CACHE_TIMEOUT_LOAD_FILTER_IN_MS);
synchronized (this) {
if (System.currentTimeMillis() - lastCacheTimeInMs >= cacheTimeoutInMs || loadFilterCacheByNodeGroupLink == null) {
loadFilterCacheByNodeGroupLink = new HashMap<NodeGroupLink, Map<LoadFilterType, Map<String, List<LoadFilter>>>>();
List<LoadFilterNodeGroupLink> loadFilters = getLoadFiltersFromDB();
boolean ignoreCase = this.parameterService.is(ParameterConstants.DB_METADATA_IGNORE_CASE);
for (LoadFilterNodeGroupLink loadFilter : loadFilters) {
NodeGroupLink nodeGroupLink = loadFilter.getNodeGroupLink();
if (nodeGroupLink != null) {
Map<LoadFilterType, Map<String, List<LoadFilter>>> loadFiltersByType = loadFilterCacheByNodeGroupLink.get(nodeGroupLink);
if (loadFiltersByType == null) {
loadFiltersByType = new HashMap<LoadFilterType, Map<String, List<LoadFilter>>>();
loadFilterCacheByNodeGroupLink.put(nodeGroupLink, loadFiltersByType);
}
Map<String, List<LoadFilter>> loadFiltersByTable = loadFiltersByType.get(loadFilter.getLoadFilterType());
if (loadFiltersByTable == null) {
loadFiltersByTable = new HashMap<String, List<LoadFilter>>();
loadFiltersByType.put(loadFilter.getLoadFilterType(), loadFiltersByTable);
}
String tableName = loadFilter.getTargetTableName();
if (StringUtils.isBlank(tableName)) {
tableName = FormatUtils.WILDCARD;
} else if (ignoreCase) {
tableName = tableName.toUpperCase();
}
String schemaName = loadFilter.getTargetSchemaName();
if (StringUtils.isBlank(schemaName)) {
schemaName = FormatUtils.WILDCARD;
} else if (ignoreCase) {
schemaName = schemaName.toUpperCase();
}
String catalogName = loadFilter.getTargetCatalogName();
if (StringUtils.isBlank(catalogName)) {
catalogName = FormatUtils.WILDCARD;
} else if (ignoreCase) {
catalogName = catalogName.toUpperCase();
}
String qualifiedName = Table.getFullyQualifiedTableName(catalogName, schemaName, tableName);
List<LoadFilter> loadFiltersForTable = loadFiltersByTable.get(qualifiedName);
if (loadFiltersForTable == null) {
loadFiltersForTable = new ArrayList<LoadFilter>();
loadFiltersByTable.put(qualifiedName, loadFiltersForTable);
}
loadFiltersForTable.add(loadFilter);
}
}
lastCacheTimeInMs = System.currentTimeMillis();
}
}
}
use of org.jumpmind.symmetric.model.NodeGroupLink in project symmetric-ds by JumpMind.
the class RestService method sendSchemaImpl.
private SendSchemaResponse sendSchemaImpl(ISymmetricEngine engine, SendSchemaRequest request) {
IConfigurationService configurationService = engine.getConfigurationService();
INodeService nodeService = engine.getNodeService();
ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
IDataService dataService = engine.getDataService();
SendSchemaResponse response = new SendSchemaResponse();
org.jumpmind.symmetric.model.Node identity = nodeService.findIdentity();
if (identity != null) {
List<org.jumpmind.symmetric.model.Node> nodesToSendTo = new ArrayList<org.jumpmind.symmetric.model.Node>();
List<String> nodeIds = request.getNodeIdsToSendTo();
if (nodeIds == null || nodeIds.size() == 0) {
nodeIds = new ArrayList<String>();
String nodeGroupIdToSendTo = request.getNodeGroupIdToSendTo();
if (isNotBlank(nodeGroupIdToSendTo)) {
NodeGroupLink link = configurationService.getNodeGroupLinkFor(identity.getNodeGroupId(), nodeGroupIdToSendTo, false);
if (link != null) {
Collection<org.jumpmind.symmetric.model.Node> nodes = nodeService.findEnabledNodesFromNodeGroup(nodeGroupIdToSendTo);
nodesToSendTo.addAll(nodes);
} else {
log.warn("Could not send schema to all nodes in the '" + nodeGroupIdToSendTo + "' node group. No node group link exists");
}
} else {
log.warn("Could not send schema to nodes. There are none that were provided and the nodeGroupIdToSendTo was also not provided");
}
} else {
for (String nodeIdToValidate : nodeIds) {
org.jumpmind.symmetric.model.Node node = nodeService.findNode(nodeIdToValidate);
if (node != null) {
NodeGroupLink link = configurationService.getNodeGroupLinkFor(identity.getNodeGroupId(), node.getNodeGroupId(), false);
if (link != null) {
nodesToSendTo.add(node);
} else {
log.warn("Could not send schema to node '" + nodeIdToValidate + "'. No node group link exists");
}
} else {
log.warn("Could not send schema to node '" + nodeIdToValidate + "'. It was not present in the database");
}
}
}
Map<String, List<TableName>> results = response.getNodeIdsSentTo();
List<String> nodeIdsToSendTo = toNodeIds(nodesToSendTo);
for (String nodeId : nodeIdsToSendTo) {
results.put(nodeId, new ArrayList<TableName>());
}
if (nodesToSendTo.size() > 0) {
List<TableName> tablesToSend = request.getTablesToSend();
List<TriggerRouter> triggerRouters = triggerRouterService.getTriggerRouters(false);
for (TriggerRouter triggerRouter : triggerRouters) {
Trigger trigger = triggerRouter.getTrigger();
NodeGroupLink link = triggerRouter.getRouter().getNodeGroupLink();
if (link.getSourceNodeGroupId().equals(identity.getNodeGroupId())) {
for (org.jumpmind.symmetric.model.Node node : nodesToSendTo) {
if (link.getTargetNodeGroupId().equals(node.getNodeGroupId())) {
if (tablesToSend == null || tablesToSend.size() == 0 || contains(trigger, tablesToSend)) {
dataService.sendSchema(node.getNodeId(), trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName(), false);
results.get(node.getNodeId()).add(new TableName(trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName()));
}
}
}
}
}
}
}
return response;
}
use of org.jumpmind.symmetric.model.NodeGroupLink in project symmetric-ds by JumpMind.
the class SymmetricEngineHolder method install.
public ISymmetricEngine install(Properties passedInProperties) throws Exception {
TypedProperties properties = new TypedProperties(passedInProperties);
String password = properties.getProperty(BasicDataSourcePropertyConstants.DB_POOL_PASSWORD);
if (StringUtils.isNotBlank(password) && !password.startsWith(SecurityConstants.PREFIX_ENC)) {
try {
ISecurityService service = SecurityServiceFactory.create(SecurityServiceType.CLIENT, properties);
properties.setProperty(BasicDataSourcePropertyConstants.DB_POOL_PASSWORD, SecurityConstants.PREFIX_ENC + service.encrypt(password));
} catch (Exception ex) {
log.warn("Could not encrypt password", ex);
}
}
String engineName = validateRequiredProperties(properties);
passedInProperties.setProperty(ParameterConstants.ENGINE_NAME, engineName);
if (engines.get(engineName) != null) {
try {
engines.get(engineName).stop();
} catch (Exception e) {
log.error("", e);
}
engines.remove(engineName);
}
File enginesDir = new File(AbstractCommandLauncher.getEnginesDir());
File symmetricProperties = new File(enginesDir, engineName + ".properties");
FileOutputStream fileOs = null;
try {
fileOs = new FileOutputStream(symmetricProperties);
properties.store(fileOs, "Updated by SymmetricDS Pro");
} catch (IOException ex) {
throw new RuntimeException("Failed to write symmetric.properties to engine directory", ex);
} finally {
IOUtils.closeQuietly(fileOs);
}
ISymmetricEngine engine = null;
try {
String registrationUrl = properties.getProperty(ParameterConstants.REGISTRATION_URL);
if (StringUtils.isNotBlank(registrationUrl)) {
Collection<ServerSymmetricEngine> all = getEngines().values();
for (ISymmetricEngine currentEngine : all) {
if (currentEngine.getParameterService().getSyncUrl().equals(registrationUrl)) {
String serverNodeGroupId = currentEngine.getParameterService().getNodeGroupId();
String clientNodeGroupId = properties.getProperty(ParameterConstants.NODE_GROUP_ID);
String externalId = properties.getProperty(ParameterConstants.EXTERNAL_ID);
IConfigurationService configurationService = currentEngine.getConfigurationService();
ITriggerRouterService triggerRouterService = currentEngine.getTriggerRouterService();
List<NodeGroup> groups = configurationService.getNodeGroups();
boolean foundGroup = false;
for (NodeGroup nodeGroup : groups) {
if (nodeGroup.getNodeGroupId().equals(clientNodeGroupId)) {
foundGroup = true;
}
}
if (!foundGroup) {
configurationService.saveNodeGroup(new NodeGroup(clientNodeGroupId));
}
boolean foundLink = false;
List<NodeGroupLink> links = configurationService.getNodeGroupLinksFor(serverNodeGroupId, false);
for (NodeGroupLink nodeGroupLink : links) {
if (nodeGroupLink.getTargetNodeGroupId().equals(clientNodeGroupId)) {
foundLink = true;
}
}
if (!foundLink) {
configurationService.saveNodeGroupLink(new NodeGroupLink(serverNodeGroupId, clientNodeGroupId, NodeGroupLinkAction.W));
triggerRouterService.syncTriggers();
}
IRegistrationService registrationService = currentEngine.getRegistrationService();
if (!registrationService.isAutoRegistration() && !registrationService.isRegistrationOpen(clientNodeGroupId, externalId)) {
Node node = new Node(properties);
registrationService.openRegistration(node);
}
}
}
}
engine = create(symmetricProperties.getAbsolutePath());
if (engine != null) {
engineCount++;
engine.start();
} else {
FileUtils.deleteQuietly(symmetricProperties);
log.warn("The engine could not be created. It will not be started");
}
return engine;
} catch (RuntimeException ex) {
if (engine != null) {
engine.destroy();
}
FileUtils.deleteQuietly(symmetricProperties);
throw ex;
}
}
use of org.jumpmind.symmetric.model.NodeGroupLink in project symmetric-ds by JumpMind.
the class DataExtractorService method extractConfigurationStandalone.
/**
* Extract the SymmetricDS configuration for the passed in {@link Node}.
*/
public void extractConfigurationStandalone(Node targetNode, Writer writer, String... tablesToExclude) {
Node sourceNode = nodeService.findIdentity();
if (targetNode != null && sourceNode != null) {
Batch batch = new Batch(BatchType.EXTRACT, Constants.VIRTUAL_BATCH_FOR_REGISTRATION, Constants.CHANNEL_CONFIG, symmetricDialect.getBinaryEncoding(), sourceNode.getNodeId(), targetNode.getNodeId(), false);
NodeGroupLink nodeGroupLink = new NodeGroupLink(parameterService.getNodeGroupId(), targetNode.getNodeGroupId());
List<TriggerRouter> triggerRouters = triggerRouterService.buildTriggerRoutersForSymmetricTables(StringUtils.isBlank(targetNode.getSymmetricVersion()) ? Version.version() : targetNode.getSymmetricVersion(), nodeGroupLink, tablesToExclude);
List<SelectFromTableEvent> initialLoadEvents = new ArrayList<SelectFromTableEvent>(triggerRouters.size() * 2);
for (int i = triggerRouters.size() - 1; i >= 0; i--) {
TriggerRouter triggerRouter = triggerRouters.get(i);
String channelId = triggerRouter.getTrigger().getChannelId();
if (Constants.CHANNEL_CONFIG.equals(channelId) || Constants.CHANNEL_HEARTBEAT.equals(channelId)) {
if (filter(targetNode, triggerRouter.getTrigger().getSourceTableName())) {
TriggerHistory triggerHistory = triggerRouterService.getNewestTriggerHistoryForTrigger(triggerRouter.getTrigger().getTriggerId(), null, null, triggerRouter.getTrigger().getSourceTableName());
if (triggerHistory == null) {
Trigger trigger = triggerRouter.getTrigger();
Table table = symmetricDialect.getPlatform().getTableFromCache(trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName(), false);
if (table == null) {
throw new IllegalStateException("Could not find a required table: " + triggerRouter.getTrigger().getSourceTableName());
}
triggerHistory = new TriggerHistory(table, triggerRouter.getTrigger(), symmetricDialect.getTriggerTemplate());
triggerHistory.setTriggerHistoryId(Integer.MAX_VALUE - i);
}
StringBuilder sql = new StringBuilder(symmetricDialect.createPurgeSqlFor(targetNode, triggerRouter, triggerHistory));
addPurgeCriteriaToConfigurationTables(triggerRouter.getTrigger().getSourceTableName(), sql);
String sourceTable = triggerHistory.getSourceTableName();
Data data = new Data(1, null, sql.toString(), DataEventType.SQL, sourceTable, null, triggerHistory, triggerRouter.getTrigger().getChannelId(), null, null);
data.putAttribute(Data.ATTRIBUTE_ROUTER_ID, triggerRouter.getRouter().getRouterId());
initialLoadEvents.add(new SelectFromTableEvent(data));
}
}
}
for (int i = 0; i < triggerRouters.size(); i++) {
TriggerRouter triggerRouter = triggerRouters.get(i);
String channelId = triggerRouter.getTrigger().getChannelId();
if (Constants.CHANNEL_CONFIG.equals(channelId) || Constants.CHANNEL_HEARTBEAT.equals(channelId)) {
if (filter(targetNode, triggerRouter.getTrigger().getSourceTableName())) {
TriggerHistory triggerHistory = triggerRouterService.getNewestTriggerHistoryForTrigger(triggerRouter.getTrigger().getTriggerId(), null, null, null);
if (triggerHistory == null) {
Trigger trigger = triggerRouter.getTrigger();
triggerHistory = new TriggerHistory(symmetricDialect.getPlatform().getTableFromCache(trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName(), false), trigger, symmetricDialect.getTriggerTemplate());
triggerHistory.setTriggerHistoryId(Integer.MAX_VALUE - i);
}
Table table = symmetricDialect.getPlatform().getTableFromCache(triggerHistory.getSourceCatalogName(), triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(), false);
String initialLoadSql = "1=1 order by ";
String quote = symmetricDialect.getPlatform().getDdlBuilder().getDatabaseInfo().getDelimiterToken();
Column[] pkColumns = table.getPrimaryKeyColumns();
for (int j = 0; j < pkColumns.length; j++) {
if (j > 0) {
initialLoadSql += ", ";
}
initialLoadSql += quote + pkColumns[j].getName() + quote;
}
if (!triggerRouter.getTrigger().getSourceTableName().endsWith(TableConstants.SYM_NODE_IDENTITY)) {
initialLoadEvents.add(new SelectFromTableEvent(targetNode, triggerRouter, triggerHistory, initialLoadSql));
} else {
Data data = new Data(1, null, targetNode.getNodeId(), DataEventType.INSERT, triggerHistory.getSourceTableName(), null, triggerHistory, triggerRouter.getTrigger().getChannelId(), null, null);
initialLoadEvents.add(new SelectFromTableEvent(data));
}
}
}
}
SelectFromTableSource source = new SelectFromTableSource(batch, initialLoadEvents);
ExtractDataReader dataReader = new ExtractDataReader(this.symmetricDialect.getPlatform(), source);
ProtocolDataWriter dataWriter = new ProtocolDataWriter(nodeService.findIdentityNodeId(), writer, targetNode.requires13Compatiblity());
DataProcessor processor = new DataProcessor(dataReader, dataWriter, "configuration extract");
DataContext ctx = new DataContext();
ctx.put(Constants.DATA_CONTEXT_TARGET_NODE, targetNode);
ctx.put(Constants.DATA_CONTEXT_SOURCE_NODE, sourceNode);
processor.process(ctx);
if (triggerRouters.size() == 0) {
log.error("{} attempted registration, but was sent an empty configuration", targetNode);
}
}
}
use of org.jumpmind.symmetric.model.NodeGroupLink in project symmetric-ds by JumpMind.
the class ConfigurationService method getNodeGroupLinksFor.
public List<NodeGroupLink> getNodeGroupLinksFor(String sourceNodeGroupId, boolean refreshCache) {
List<NodeGroupLink> links = getNodeGroupLinks(refreshCache);
List<NodeGroupLink> target = new ArrayList<NodeGroupLink>(links.size());
for (NodeGroupLink nodeGroupLink : links) {
if (nodeGroupLink.getSourceNodeGroupId().equals(sourceNodeGroupId)) {
target.add(nodeGroupLink);
}
}
return target;
}
Aggregations