Search in sources :

Example 6 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 7 with NodeGroupLink

use of org.jumpmind.symmetric.model.NodeGroupLink in project symmetric-ds by JumpMind.

the class RegistrationService method processRegistration.

protected Node processRegistration(Node nodePriorToRegistration, String remoteHost, String remoteAddress, boolean isRequestedRegistration, String deploymentType) throws IOException {
    Node processedNode = new Node();
    processedNode.setSyncEnabled(false);
    Node identity = nodeService.findIdentity();
    if (identity == null) {
        RegistrationRequest req = new RegistrationRequest(nodePriorToRegistration, RegistrationStatus.ER, remoteHost, remoteAddress);
        req.setErrorMessage("Cannot register a client node until this node is registered");
        saveRegistrationRequest(req);
        log.warn(req.getErrorMessage());
        return processedNode;
    }
    try {
        if (!nodeService.isRegistrationServer()) {
            /*
                 * registration is not allowed until this node has an identity
                 * and an initial load
                 */
            NodeSecurity security = nodeService.findNodeSecurity(identity.getNodeId());
            if (security == null || security.getInitialLoadTime() == null) {
                RegistrationRequest req = new RegistrationRequest(nodePriorToRegistration, RegistrationStatus.ER, remoteHost, remoteAddress);
                req.setErrorMessage("Cannot register a client node until this node has an initial load (ie. node_security.initial_load_time is a non null value)");
                saveRegistrationRequest(req);
                log.warn(req.getErrorMessage());
                return processedNode;
            }
        }
        String redirectUrl = getRedirectionUrlFor(nodePriorToRegistration.getExternalId());
        if (redirectUrl != null) {
            log.info("Redirecting {} to {} for registration.", nodePriorToRegistration.getExternalId(), redirectUrl);
            saveRegistrationRequest(new RegistrationRequest(nodePriorToRegistration, RegistrationStatus.RR, remoteHost, remoteAddress));
            throw new RegistrationRedirectException(redirectUrl);
        }
        /*
             * Check to see if there is a link that exists to service the node
             * that is requesting registration
             */
        NodeGroupLink link = configurationService.getNodeGroupLinkFor(identity.getNodeGroupId(), nodePriorToRegistration.getNodeGroupId(), false);
        if (link == null && parameterService.is(ParameterConstants.REGISTRATION_REQUIRE_NODE_GROUP_LINK, true)) {
            RegistrationRequest req = new RegistrationRequest(nodePriorToRegistration, RegistrationStatus.ER, remoteHost, remoteAddress);
            req.setErrorMessage(String.format("Cannot register a client node unless a node group link exists so the registering node can receive configuration updates.  Please add a group link where the source group id is %s and the target group id is %s", identity.getNodeGroupId(), nodePriorToRegistration.getNodeGroupId()));
            saveRegistrationRequest(req);
            log.warn(req.getErrorMessage());
            return processedNode;
        }
        String nodeId = StringUtils.isBlank(nodePriorToRegistration.getNodeId()) ? extensionService.getExtensionPoint(INodeIdCreator.class).selectNodeId(nodePriorToRegistration, remoteHost, remoteAddress) : nodePriorToRegistration.getNodeId();
        Node foundNode = nodeService.findNode(nodeId);
        NodeSecurity security = nodeService.findNodeSecurity(nodeId);
        if ((foundNode == null || security == null || !security.isRegistrationEnabled()) && parameterService.is(ParameterConstants.AUTO_REGISTER_ENABLED)) {
            openRegistration(nodePriorToRegistration, remoteHost, remoteAddress);
            nodeId = StringUtils.isBlank(nodePriorToRegistration.getNodeId()) ? extensionService.getExtensionPoint(INodeIdCreator.class).selectNodeId(nodePriorToRegistration, remoteHost, remoteAddress) : nodePriorToRegistration.getNodeId();
            security = nodeService.findNodeSecurity(nodeId);
            foundNode = nodeService.findNode(nodeId);
        } else if (foundNode == null || security == null || !security.isRegistrationEnabled()) {
            saveRegistrationRequest(new RegistrationRequest(nodePriorToRegistration, RegistrationStatus.RQ, remoteHost, remoteAddress));
            return processedNode;
        }
        foundNode.setSyncEnabled(true);
        if (Constants.DEPLOYMENT_TYPE_REST.equalsIgnoreCase(deploymentType)) {
            foundNode.setSymmetricVersion(null);
            foundNode.setDeploymentType(deploymentType);
        }
        foundNode.setSyncUrl(nodePriorToRegistration.getSyncUrl());
        foundNode.setDatabaseType(nodePriorToRegistration.getDatabaseType());
        foundNode.setDatabaseVersion(nodePriorToRegistration.getDatabaseVersion());
        foundNode.setSymmetricVersion(nodePriorToRegistration.getSymmetricVersion());
        nodeService.save(foundNode);
        /**
             * Only send automatic initial load once or if the client is really
             * re-registering
             */
        if ((security != null && security.getInitialLoadTime() == null) || isRequestedRegistration) {
            if (parameterService.is(ParameterConstants.AUTO_RELOAD_ENABLED)) {
                nodeService.setInitialLoadEnabled(nodeId, true, false, -1, "registration");
            }
            if (parameterService.is(ParameterConstants.AUTO_RELOAD_REVERSE_ENABLED)) {
                nodeService.setReverseInitialLoadEnabled(nodeId, true, false, -1, "registration");
            }
        }
        saveRegistrationRequest(new RegistrationRequest(foundNode, RegistrationStatus.OK, remoteHost, remoteAddress));
        statisticManager.incrementNodesRegistered(1);
        return foundNode;
    } catch (RegistrationNotOpenException ex) {
        if (StringUtils.isNotBlank(ex.getMessage())) {
            log.warn("Registration not allowed for {} because {}", nodePriorToRegistration.toString(), ex.getMessage());
        }
        return processedNode;
    }
}
Also used : RegistrationNotOpenException(org.jumpmind.symmetric.service.RegistrationNotOpenException) NodeSecurity(org.jumpmind.symmetric.model.NodeSecurity) INodeIdCreator(org.jumpmind.symmetric.config.INodeIdCreator) Node(org.jumpmind.symmetric.model.Node) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) RegistrationRequest(org.jumpmind.symmetric.model.RegistrationRequest) RegistrationRedirectException(org.jumpmind.symmetric.service.RegistrationRedirectException)

Example 8 with NodeGroupLink

use of org.jumpmind.symmetric.model.NodeGroupLink in project symmetric-ds by JumpMind.

the class RouterService method findAvailableNodes.

protected Set<Node> findAvailableNodes(TriggerRouter triggerRouter, ChannelRouterContext context) {
    Set<Node> nodes = context.getAvailableNodes().get(triggerRouter);
    if (nodes == null) {
        nodes = new HashSet<Node>();
        Router router = triggerRouter.getRouter();
        NodeGroupLink link = engine.getConfigurationService().getNodeGroupLinkFor(router.getNodeGroupLink().getSourceNodeGroupId(), router.getNodeGroupLink().getTargetNodeGroupId(), false);
        if (link != null) {
            nodes.addAll(engine.getNodeService().findEnabledNodesFromNodeGroup(router.getNodeGroupLink().getTargetNodeGroupId()));
        } else {
            log.error("The router {} has no node group link configured from {} to {}", new Object[] { router.getRouterId(), router.getNodeGroupLink().getSourceNodeGroupId(), router.getNodeGroupLink().getTargetNodeGroupId() });
        }
        context.getAvailableNodes().put(triggerRouter, nodes);
    }
    return engine.getGroupletService().getTargetEnabled(triggerRouter, nodes);
}
Also used : Node(org.jumpmind.symmetric.model.Node) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) AuditTableDataRouter(org.jumpmind.symmetric.route.AuditTableDataRouter) LookupTableDataRouter(org.jumpmind.symmetric.route.LookupTableDataRouter) AbstractFileParsingRouter(org.jumpmind.symmetric.route.AbstractFileParsingRouter) ConfigurationChangedDataRouter(org.jumpmind.symmetric.route.ConfigurationChangedDataRouter) SubSelectDataRouter(org.jumpmind.symmetric.route.SubSelectDataRouter) FileSyncDataRouter(org.jumpmind.symmetric.route.FileSyncDataRouter) ColumnMatchDataRouter(org.jumpmind.symmetric.route.ColumnMatchDataRouter) Router(org.jumpmind.symmetric.model.Router) DefaultDataRouter(org.jumpmind.symmetric.route.DefaultDataRouter) BshDataRouter(org.jumpmind.symmetric.route.BshDataRouter) IDataRouter(org.jumpmind.symmetric.route.IDataRouter) DBFRouter(org.jumpmind.symmetric.route.DBFRouter) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink)

Example 9 with NodeGroupLink

use of org.jumpmind.symmetric.model.NodeGroupLink in project symmetric-ds by JumpMind.

the class RouterService method insertInitialLoadEvents.

/**
     * If a load has been queued up by setting the initial load enabled or
     * reverse initial load enabled flags, then the router service will insert
     * the reload events. This process will not run at the same time sync
     * triggers is running.
     */
protected void insertInitialLoadEvents() {
    ProcessInfo processInfo = engine.getStatisticManager().newProcessInfo(new ProcessInfoKey(engine.getNodeService().findIdentityNodeId(), null, ProcessType.INSERT_LOAD_EVENTS));
    processInfo.setStatus(ProcessInfo.Status.PROCESSING);
    try {
        INodeService nodeService = engine.getNodeService();
        Node identity = nodeService.findIdentity();
        if (identity != null) {
            boolean isClusteringEnabled = parameterService.is(ParameterConstants.CLUSTER_LOCKING_ENABLED);
            NodeSecurity identitySecurity = nodeService.findNodeSecurity(identity.getNodeId(), !isClusteringEnabled);
            if (engine.getParameterService().isRegistrationServer() || (identitySecurity != null && !identitySecurity.isRegistrationEnabled() && identitySecurity.getRegistrationTime() != null)) {
                List<NodeSecurity> nodeSecurities = findNodesThatAreReadyForInitialLoad();
                if (nodeSecurities != null && nodeSecurities.size() > 0) {
                    gapDetector.setFullGapAnalysis(true);
                    boolean reverseLoadFirst = parameterService.is(ParameterConstants.INITIAL_LOAD_REVERSE_FIRST);
                    boolean isInitialLoadQueued = false;
                    for (NodeSecurity security : nodeSecurities) {
                        if (engine.getTriggerRouterService().getActiveTriggerHistories().size() > 0) {
                            boolean thisMySecurityRecord = security.getNodeId().equals(identity.getNodeId());
                            boolean reverseLoadQueued = security.isRevInitialLoadEnabled();
                            boolean initialLoadQueued = security.isInitialLoadEnabled();
                            boolean registered = security.getRegistrationTime() != null;
                            if (thisMySecurityRecord && reverseLoadQueued && (reverseLoadFirst || !initialLoadQueued)) {
                                sendReverseInitialLoad(processInfo);
                            } else if (!thisMySecurityRecord && registered && initialLoadQueued && (!reverseLoadFirst || !reverseLoadQueued)) {
                                long ts = System.currentTimeMillis();
                                engine.getDataService().insertReloadEvents(engine.getNodeService().findNode(security.getNodeId()), false, processInfo);
                                isInitialLoadQueued = true;
                                ts = System.currentTimeMillis() - ts;
                                if (ts > Constants.LONG_OPERATION_THRESHOLD) {
                                    log.warn("Inserted reload events for node {} took longer than expected.  It took {} ms", security.getNodeId(), ts);
                                } else {
                                    log.info("Inserted reload events for node {} in {} ms", security.getNodeId(), ts);
                                }
                            }
                        } else {
                            List<NodeGroupLink> links = engine.getConfigurationService().getNodeGroupLinksFor(parameterService.getNodeGroupId(), false);
                            if (links == null || links.size() == 0) {
                                log.warn("Could not queue up a load for {} because a node group link is NOT configured over which a load could be delivered", security.getNodeId());
                            } else {
                                log.warn("Could not queue up a load for {} because sync triggers has not yet run", security.getNodeId());
                                if (!syncTriggersBeforeInitialLoadAttempted) {
                                    syncTriggersBeforeInitialLoadAttempted = true;
                                    engine.getTriggerRouterService().syncTriggers();
                                }
                            }
                        }
                    }
                    if (isInitialLoadQueued) {
                        gapDetector.setFullGapAnalysis(true);
                    }
                }
                processTableRequestLoads(identity, processInfo);
            }
        }
        processInfo.setStatus(ProcessInfo.Status.OK);
    } catch (Exception ex) {
        processInfo.setStatus(ProcessInfo.Status.ERROR);
        log.error("", ex);
    }
}
Also used : NodeSecurity(org.jumpmind.symmetric.model.NodeSecurity) INodeService(org.jumpmind.symmetric.service.INodeService) Node(org.jumpmind.symmetric.model.Node) ProcessInfoKey(org.jumpmind.symmetric.model.ProcessInfoKey) ProcessInfo(org.jumpmind.symmetric.model.ProcessInfo) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink) SyntaxParsingException(org.jumpmind.symmetric.SyntaxParsingException) SymmetricException(org.jumpmind.symmetric.SymmetricException) DelayRoutingException(org.jumpmind.symmetric.route.DelayRoutingException)

Example 10 with NodeGroupLink

use of org.jumpmind.symmetric.model.NodeGroupLink in project symmetric-ds by JumpMind.

the class ConfigurationChangedDataRouter method routeNodeTables.

protected void routeNodeTables(Set<String> nodeIds, Map<String, String> columnValues, NetworkedNode rootNetworkedNode, Node me, SimpleRouterContext routingContext, DataMetaData dataMetaData, Set<Node> possibleTargetNodes, boolean initialLoad) {
    String nodeIdForRecordBeingRouted = columnValues.get("NODE_ID");
    if (dataMetaData.getData().getDataEventType() == DataEventType.DELETE) {
        String createAtNodeId = columnValues.get("CREATED_AT_NODE_ID");
        for (Node nodeThatMayBeRoutedTo : possibleTargetNodes) {
            if (!Constants.DEPLOYMENT_TYPE_REST.equals(nodeThatMayBeRoutedTo.getDeploymentType()) && !nodeIdForRecordBeingRouted.equals(nodeThatMayBeRoutedTo.getNodeId()) && !nodeThatMayBeRoutedTo.getNodeId().equals(createAtNodeId) && (nodeThatMayBeRoutedTo.getCreatedAtNodeId() == null || !nodeThatMayBeRoutedTo.getCreatedAtNodeId().equals(nodeIdForRecordBeingRouted))) {
                nodeIds.add(nodeThatMayBeRoutedTo.getNodeId());
            }
        }
    } else {
        IConfigurationService configurationService = engine.getConfigurationService();
        List<NodeGroupLink> nodeGroupLinks = getNodeGroupLinksFromContext(routingContext);
        for (Node nodeThatMayBeRoutedTo : possibleTargetNodes) {
            if (!Constants.DEPLOYMENT_TYPE_REST.equals(nodeThatMayBeRoutedTo.getDeploymentType()) && !nodeThatMayBeRoutedTo.requires13Compatiblity() && isLinked(nodeIdForRecordBeingRouted, nodeThatMayBeRoutedTo, rootNetworkedNode, me, nodeGroupLinks) && (!isSameNumberOfLinksAwayFromRoot(nodeThatMayBeRoutedTo, rootNetworkedNode, me) || configurationService.isMasterToMaster()) || (nodeThatMayBeRoutedTo.getNodeId().equals(me.getNodeId()) && initialLoad)) {
                nodeIds.add(nodeThatMayBeRoutedTo.getNodeId());
            }
        }
        if (!initialLoad && nodeIds != null) {
            if (tableMatches(dataMetaData, TableConstants.SYM_NODE_SECURITY)) {
                routeSymNodeSecurity(me, nodeIdForRecordBeingRouted, dataMetaData, nodeIds, columnValues);
            }
            /*
                 * Don't route insert events for a node to itself. They will be
                 * loaded during registration. If we route them, then an old
                 * state can override the correct state
                 * 
                 * Don't send deletes to a node. A node should be responsible
                 * for deleting itself.
                 */
            if (dataMetaData.getData().getDataEventType() == DataEventType.INSERT) {
                nodeIds.remove(nodeIdForRecordBeingRouted);
            }
        }
    }
}
Also used : Node(org.jumpmind.symmetric.model.Node) NetworkedNode(org.jumpmind.symmetric.model.NetworkedNode) IConfigurationService(org.jumpmind.symmetric.service.IConfigurationService) 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