Search in sources :

Example 11 with NodeSecurity

use of org.jumpmind.symmetric.model.NodeSecurity 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 12 with NodeSecurity

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

the class PushService method pushData.

public synchronized RemoteNodeStatuses pushData(boolean force) {
    RemoteNodeStatuses statuses = new RemoteNodeStatuses(configurationService.getChannels(false));
    Node identity = nodeService.findIdentity();
    if (identity != null && identity.isSyncEnabled()) {
        long minimumPeriodMs = parameterService.getLong(ParameterConstants.PUSH_MINIMUM_PERIOD_MS, -1);
        if (force || !clusterService.isInfiniteLocked(ClusterConstants.PUSH)) {
            List<NodeCommunication> nodes = nodeCommunicationService.list(CommunicationType.PUSH);
            if (nodes.size() > 0) {
                NodeSecurity identitySecurity = nodeService.findNodeSecurity(identity.getNodeId(), true);
                if (identitySecurity != null) {
                    int availableThreads = nodeCommunicationService.getAvailableThreads(CommunicationType.PUSH);
                    for (NodeCommunication nodeCommunication : nodes) {
                        boolean meetsMinimumTime = true;
                        if (minimumPeriodMs > 0 && nodeCommunication.getLastLockTime() != null && (System.currentTimeMillis() - nodeCommunication.getLastLockTime().getTime()) < minimumPeriodMs) {
                            meetsMinimumTime = false;
                        }
                        if (availableThreads > 0 && meetsMinimumTime) {
                            if (nodeCommunicationService.execute(nodeCommunication, statuses, this)) {
                                availableThreads--;
                            }
                        }
                    }
                } else {
                    log.error("Could not find a node security row for '{}'.  A node needs a matching security row in both the local and remote nodes if it is going to authenticate to push data", identity.getNodeId());
                }
            }
        } else {
            log.debug("Did not run the push process because it has been stopped");
        }
    }
    return statuses;
}
Also used : RemoteNodeStatuses(org.jumpmind.symmetric.model.RemoteNodeStatuses) NodeCommunication(org.jumpmind.symmetric.model.NodeCommunication) NodeSecurity(org.jumpmind.symmetric.model.NodeSecurity) Node(org.jumpmind.symmetric.model.Node)

Example 13 with NodeSecurity

use of org.jumpmind.symmetric.model.NodeSecurity 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 14 with NodeSecurity

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

the class RouterService method processTableRequestLoads.

public void processTableRequestLoads(Node source, ProcessInfo processInfo) {
    List<TableReloadRequest> loadsToProcess = engine.getDataService().getTableReloadRequestToProcess(source.getNodeId());
    if (loadsToProcess.size() > 0) {
        processInfo.setStatus(ProcessInfo.Status.CREATING);
        log.info("Found " + loadsToProcess.size() + " table reload requests to process.");
        gapDetector.setFullGapAnalysis(true);
        Map<String, List<TableReloadRequest>> requestsSplitByLoad = new HashMap<String, List<TableReloadRequest>>();
        for (TableReloadRequest load : loadsToProcess) {
            if (load.isFullLoadRequest() && isValidLoadTarget(load.getTargetNodeId())) {
                List<TableReloadRequest> fullLoad = new ArrayList<TableReloadRequest>();
                fullLoad.add(load);
                engine.getDataService().insertReloadEvents(engine.getNodeService().findNode(load.getTargetNodeId()), false, fullLoad, processInfo);
            } else {
                NodeSecurity targetNodeSecurity = engine.getNodeService().findNodeSecurity(load.getTargetNodeId());
                boolean registered = targetNodeSecurity != null && (targetNodeSecurity.getRegistrationTime() != null || targetNodeSecurity.getNodeId().equals(targetNodeSecurity.getCreatedAtNodeId()));
                if (registered) {
                    // Make loads unique to the target and create time
                    String key = load.getTargetNodeId() + "::" + load.getCreateTime().toString();
                    if (!requestsSplitByLoad.containsKey(key)) {
                        requestsSplitByLoad.put(key, new ArrayList<TableReloadRequest>());
                    }
                    requestsSplitByLoad.get(key).add(load);
                } else {
                    log.warn("There was a load queued up for '{}', but the node is not registered.  It is being ignored", load.getTargetNodeId());
                }
            }
        }
        for (Map.Entry<String, List<TableReloadRequest>> entry : requestsSplitByLoad.entrySet()) {
            engine.getDataService().insertReloadEvents(engine.getNodeService().findNode(entry.getKey().split("::")[0]), false, entry.getValue(), processInfo);
        }
    }
}
Also used : NodeSecurity(org.jumpmind.symmetric.model.NodeSecurity) HashMap(java.util.HashMap) TableReloadRequest(org.jumpmind.symmetric.model.TableReloadRequest) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 15 with NodeSecurity

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

the class RouterService method isValidLoadTarget.

public boolean isValidLoadTarget(String targetNodeId) {
    boolean result = false;
    NodeSecurity targetNodeSecurity = engine.getNodeService().findNodeSecurity(targetNodeId);
    boolean reverseLoadFirst = parameterService.is(ParameterConstants.INITIAL_LOAD_REVERSE_FIRST);
    boolean registered = targetNodeSecurity.getRegistrationTime() != null;
    boolean reverseLoadQueued = targetNodeSecurity.isRevInitialLoadEnabled();
    if (registered && (!reverseLoadFirst || !reverseLoadQueued)) {
        result = true;
    } else {
        log.info("Unable to process load for target node id " + targetNodeId + " [registered: " + registered + ", reverse load first: " + reverseLoadFirst + ", reverse load queued: " + reverseLoadQueued + "]");
    }
    return result;
}
Also used : NodeSecurity(org.jumpmind.symmetric.model.NodeSecurity)

Aggregations

NodeSecurity (org.jumpmind.symmetric.model.NodeSecurity)31 Node (org.jumpmind.symmetric.model.Node)13 INodeService (org.jumpmind.symmetric.service.INodeService)12 Date (java.util.Date)7 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)7 ProcessInfoKey (org.jumpmind.symmetric.model.ProcessInfoKey)7 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)4 IoException (org.jumpmind.exception.IoException)4 SymmetricException (org.jumpmind.symmetric.SymmetricException)4 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)4 NodeHost (org.jumpmind.symmetric.model.NodeHost)4 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 UniqueKeyException (org.jumpmind.db.sql.UniqueKeyException)3 ChannelMap (org.jumpmind.symmetric.model.ChannelMap)2 NetworkedNode (org.jumpmind.symmetric.model.NetworkedNode)2 NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)2 OutgoingBatch (org.jumpmind.symmetric.model.OutgoingBatch)2