Search in sources :

Example 1 with NodeGroupLink

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();
        }
    }
}
Also used : LoadFilterType(org.jumpmind.symmetric.model.LoadFilter.LoadFilterType) LoadFilter(org.jumpmind.symmetric.model.LoadFilter) ArrayList(java.util.ArrayList) List(java.util.List) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with NodeGroupLink

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;
}
Also used : Node(org.jumpmind.symmetric.web.rest.model.Node) NetworkedNode(org.jumpmind.symmetric.model.NetworkedNode) ArrayList(java.util.ArrayList) IConfigurationService(org.jumpmind.symmetric.service.IConfigurationService) Trigger(org.jumpmind.symmetric.model.Trigger) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) ArrayList(java.util.ArrayList) List(java.util.List) EngineList(org.jumpmind.symmetric.web.rest.model.EngineList) NodeList(org.jumpmind.symmetric.web.rest.model.NodeList) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) IDataService(org.jumpmind.symmetric.service.IDataService) TableName(org.jumpmind.symmetric.web.rest.model.TableName) INodeService(org.jumpmind.symmetric.service.INodeService) SendSchemaResponse(org.jumpmind.symmetric.web.rest.model.SendSchemaResponse)

Example 3 with NodeGroupLink

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;
    }
}
Also used : ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) IRegistrationService(org.jumpmind.symmetric.service.IRegistrationService) Node(org.jumpmind.symmetric.model.Node) ISymmetricEngine(org.jumpmind.symmetric.ISymmetricEngine) IConfigurationService(org.jumpmind.symmetric.service.IConfigurationService) IOException(java.io.IOException) TypedProperties(org.jumpmind.properties.TypedProperties) IOException(java.io.IOException) ISecurityService(org.jumpmind.security.ISecurityService) FileOutputStream(java.io.FileOutputStream) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) File(java.io.File) NodeGroup(org.jumpmind.symmetric.model.NodeGroup)

Example 4 with NodeGroupLink

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);
        }
    }
}
Also used : TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable) Table(org.jumpmind.db.model.Table) Node(org.jumpmind.symmetric.model.Node) ArrayList(java.util.ArrayList) Data(org.jumpmind.symmetric.model.Data) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) CsvData(org.jumpmind.symmetric.io.data.CsvData) DataProcessor(org.jumpmind.symmetric.io.data.DataProcessor) TransformPoint(org.jumpmind.symmetric.io.data.transform.TransformPoint) DataContext(org.jumpmind.symmetric.io.data.DataContext) Trigger(org.jumpmind.symmetric.model.Trigger) ProtocolDataWriter(org.jumpmind.symmetric.io.data.writer.ProtocolDataWriter) Batch(org.jumpmind.symmetric.io.data.Batch) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch) Column(org.jumpmind.db.model.Column) PlatformColumn(org.jumpmind.db.model.PlatformColumn) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) TransformTableNodeGroupLink(org.jumpmind.symmetric.service.impl.TransformService.TransformTableNodeGroupLink) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) ExtractDataReader(org.jumpmind.symmetric.io.data.reader.ExtractDataReader)

Example 5 with NodeGroupLink

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;
}
Also used : ArrayList(java.util.ArrayList) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink)

Aggregations

NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)22 Node (org.jumpmind.symmetric.model.Node)11 ArrayList (java.util.ArrayList)9 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)7 IConfigurationService (org.jumpmind.symmetric.service.IConfigurationService)5 TransformTableNodeGroupLink (org.jumpmind.symmetric.service.impl.TransformService.TransformTableNodeGroupLink)5 List (java.util.List)4 NetworkedNode (org.jumpmind.symmetric.model.NetworkedNode)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 TransformTable (org.jumpmind.symmetric.io.data.transform.TransformTable)3 Trigger (org.jumpmind.symmetric.model.Trigger)3 INodeService (org.jumpmind.symmetric.service.INodeService)3 ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)3 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)2 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)2 CsvData (org.jumpmind.symmetric.io.data.CsvData)2 TransformPoint (org.jumpmind.symmetric.io.data.transform.TransformPoint)2 TransformWriter (org.jumpmind.symmetric.io.data.writer.TransformWriter)2