use of org.jumpmind.symmetric.service.INodeService in project symmetric-ds by JumpMind.
the class DataService method insertReloadEvents.
public void insertReloadEvents(Node targetNode, boolean reverse, List<TableReloadRequest> reloadRequests, ProcessInfo processInfo) {
if (engine.getClusterService().lock(ClusterConstants.SYNCTRIGGERS)) {
try {
synchronized (engine.getTriggerRouterService()) {
engine.getClusterService().lock(ClusterConstants.SYNCTRIGGERS);
boolean isFullLoad = reloadRequests == null || (reloadRequests.size() == 1 && reloadRequests.get(0).isFullLoadRequest());
if (!reverse) {
log.info("Queueing up " + (isFullLoad ? "an initial" : "a") + " load to node " + targetNode.getNodeId());
} else {
log.info("Queueing up a reverse " + (isFullLoad ? "initial" : "") + " load to node " + targetNode.getNodeId());
}
/*
* Outgoing data events are pointless because we are
* reloading all data
*/
if (isFullLoad) {
engine.getOutgoingBatchService().markAllAsSentForNode(targetNode.getNodeId(), false);
}
INodeService nodeService = engine.getNodeService();
ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
Node sourceNode = nodeService.findIdentity();
boolean transactional = parameterService.is(ParameterConstants.DATA_RELOAD_IS_BATCH_INSERT_TRANSACTIONAL);
String nodeIdRecord = reverse ? nodeService.findIdentityNodeId() : targetNode.getNodeId();
NodeSecurity nodeSecurity = nodeService.findNodeSecurity(nodeIdRecord);
ISqlTransaction transaction = null;
try {
transaction = platform.getSqlTemplate().startSqlTransaction();
long loadId = engine.getSequenceService().nextVal(transaction, Constants.SEQUENCE_OUTGOING_BATCH_LOAD_ID);
processInfo.setCurrentLoadId(loadId);
String createBy = reverse ? nodeSecurity.getRevInitialLoadCreateBy() : nodeSecurity.getInitialLoadCreateBy();
List<TriggerHistory> triggerHistories = new ArrayList<TriggerHistory>();
if (isFullLoad) {
triggerHistories = triggerRouterService.getActiveTriggerHistories();
} else {
for (TableReloadRequest reloadRequest : reloadRequests) {
triggerHistories.addAll(engine.getTriggerRouterService().getActiveTriggerHistories(new Trigger(reloadRequest.getTriggerId(), null)));
}
}
processInfo.setDataCount(triggerHistories.size());
Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId = triggerRouterService.fillTriggerRoutersByHistIdAndSortHist(sourceNode.getNodeGroupId(), targetNode.getNodeGroupId(), triggerHistories);
if (isFullLoad) {
callReloadListeners(true, targetNode, transactional, transaction, loadId);
insertCreateSchemaScriptPriorToReload(targetNode, nodeIdRecord, loadId, createBy, transactional, transaction);
}
Map<String, TableReloadRequest> mapReloadRequests = convertReloadListToMap(reloadRequests);
String symNodeSecurityReloadChannel = null;
try {
symNodeSecurityReloadChannel = triggerRoutersByHistoryId.get(triggerHistories.get(0).getTriggerHistoryId()).get(0).getTrigger().getReloadChannelId();
} catch (Exception e) {
}
if (isFullLoad || (reloadRequests != null && reloadRequests.size() > 0)) {
insertSqlEventsPriorToReload(targetNode, nodeIdRecord, loadId, createBy, transactional, transaction, reverse, triggerHistories, triggerRoutersByHistoryId, mapReloadRequests, isFullLoad, symNodeSecurityReloadChannel);
}
insertCreateBatchesForReload(targetNode, loadId, createBy, triggerHistories, triggerRoutersByHistoryId, transactional, transaction, mapReloadRequests);
insertDeleteBatchesForReload(targetNode, loadId, createBy, triggerHistories, triggerRoutersByHistoryId, transactional, transaction, mapReloadRequests);
insertSQLBatchesForReload(targetNode, loadId, createBy, triggerHistories, triggerRoutersByHistoryId, transactional, transaction, mapReloadRequests);
insertLoadBatchesForReload(targetNode, loadId, createBy, triggerHistories, triggerRoutersByHistoryId, transactional, transaction, mapReloadRequests, processInfo);
if (isFullLoad) {
String afterSql = parameterService.getString(reverse ? ParameterConstants.INITIAL_LOAD_REVERSE_AFTER_SQL : ParameterConstants.INITIAL_LOAD_AFTER_SQL);
if (isNotBlank(afterSql)) {
insertSqlEvent(transaction, targetNode, afterSql, true, loadId, createBy);
}
}
insertFileSyncBatchForReload(targetNode, loadId, createBy, transactional, transaction, processInfo);
if (isFullLoad) {
callReloadListeners(false, targetNode, transactional, transaction, loadId);
if (!reverse) {
nodeService.setInitialLoadEnabled(transaction, nodeIdRecord, false, false, loadId, createBy);
} else {
nodeService.setReverseInitialLoadEnabled(transaction, nodeIdRecord, false, false, loadId, createBy);
}
}
if (!Constants.DEPLOYMENT_TYPE_REST.equals(targetNode.getDeploymentType())) {
insertNodeSecurityUpdate(transaction, nodeIdRecord, targetNode.getNodeId(), true, loadId, createBy, symNodeSecurityReloadChannel);
}
engine.getStatisticManager().incrementNodesLoaded(1);
if (reloadRequests != null && reloadRequests.size() > 0) {
for (TableReloadRequest request : reloadRequests) {
transaction.prepareAndExecute(getSql("updateProcessedTableReloadRequest"), loadId, new Date(), request.getTargetNodeId(), request.getSourceNodeId(), request.getTriggerId(), request.getRouterId(), request.getCreateTime());
}
log.info("Table reload request(s) for load id " + loadId + " have been processed.");
}
transaction.commit();
} catch (Error ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} catch (RuntimeException ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} finally {
close(transaction);
}
if (!reverse) {
/*
* Remove all incoming events for the node that we are
* starting a reload for
*/
engine.getPurgeService().purgeAllIncomingEventsForNode(targetNode.getNodeId());
}
}
} finally {
engine.getClusterService().unlock(ClusterConstants.SYNCTRIGGERS);
}
} else {
log.info("Not attempting to insert reload events because sync trigger is currently running");
}
}
use of org.jumpmind.symmetric.service.INodeService in project symmetric-ds by JumpMind.
the class DataService method reloadNode.
public String reloadNode(String nodeId, boolean reverseLoad, String createBy) {
INodeService nodeService = engine.getNodeService();
Node targetNode = engine.getNodeService().findNode(nodeId);
if (targetNode == null) {
return String.format("Unknown node %s", nodeId);
} else if (reverseLoad && nodeService.setReverseInitialLoadEnabled(nodeId, true, true, -1, createBy)) {
return String.format("Successfully enabled reverse initial load for node %s", nodeId);
} else if (nodeService.setInitialLoadEnabled(nodeId, true, true, -1, createBy)) {
return String.format("Successfully enabled initial load for node %s", nodeId);
} else {
return String.format("Could not enable initial load for %s", nodeId);
}
}
use of org.jumpmind.symmetric.service.INodeService in project symmetric-ds by JumpMind.
the class DataService method insertReloadEvent.
public boolean insertReloadEvent(TableReloadRequest request, boolean deleteAtClient) {
boolean successful = false;
if (request != null) {
ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
INodeService nodeService = engine.getNodeService();
Node targetNode = nodeService.findNode(request.getTargetNodeId());
if (targetNode != null) {
TriggerRouter triggerRouter = triggerRouterService.getTriggerRouterForCurrentNode(request.getTriggerId(), request.getRouterId(), false);
if (triggerRouter != null) {
Trigger trigger = triggerRouter.getTrigger();
Router router = triggerRouter.getRouter();
NodeGroupLink link = router.getNodeGroupLink();
Node me = nodeService.findIdentity();
if (link.getSourceNodeGroupId().equals(me.getNodeGroupId())) {
if (link.getTargetNodeGroupId().equals(targetNode.getNodeGroupId())) {
TriggerHistory triggerHistory = lookupTriggerHistory(trigger);
ISqlTransaction transaction = null;
try {
transaction = sqlTemplate.startSqlTransaction();
if (parameterService.is(ParameterConstants.INITIAL_LOAD_DELETE_BEFORE_RELOAD)) {
String overrideDeleteStatement = StringUtils.isNotBlank(request.getBeforeCustomSql()) ? request.getBeforeCustomSql() : null;
insertPurgeEvent(transaction, targetNode, triggerRouter, triggerHistory, false, overrideDeleteStatement, -1, null);
}
insertReloadEvent(transaction, targetNode, triggerRouter, triggerHistory, request.getReloadSelect(), false, -1, null, Status.NE);
if (!targetNode.requires13Compatiblity() && deleteAtClient) {
insertSqlEvent(transaction, triggerHistory, trigger.getChannelId(), targetNode, String.format("delete from %s where target_node_id='%s' and source_node_id='%s' and trigger_id='%s' and router_id='%s'", TableConstants.getTableName(tablePrefix, TableConstants.SYM_TABLE_RELOAD_REQUEST), request.getTargetNodeId(), request.getSourceNodeId(), request.getTriggerId(), request.getRouterId()), false, -1, null);
}
deleteTableReloadRequest(transaction, request);
transaction.commit();
} catch (Error ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} catch (RuntimeException ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} finally {
close(transaction);
}
} else {
log.error("Could not reload table {} for node {} because the router {} target node group id {} did not match", new Object[] { trigger.getSourceTableName(), request.getTargetNodeId(), request.getRouterId(), link.getTargetNodeGroupId() });
}
} else {
log.error("Could not reload table {} for node {} because the router {} source node group id {} did not match", new Object[] { trigger.getSourceTableName(), request.getTargetNodeId(), request.getRouterId(), link.getSourceNodeGroupId() });
}
} else {
log.error("Could not reload table for node {} because the trigger router ({}, {}) could not be found", new Object[] { request.getTargetNodeId(), request.getTriggerId(), request.getRouterId() });
}
} else {
log.error("Could not reload table for node {} because the target node could not be found", request.getTargetNodeId());
}
}
return successful;
}
use of org.jumpmind.symmetric.service.INodeService in project symmetric-ds by JumpMind.
the class RouterService method sendReverseInitialLoad.
protected void sendReverseInitialLoad(ProcessInfo processInfo) {
INodeService nodeService = engine.getNodeService();
boolean queuedLoad = false;
List<Node> nodes = new ArrayList<Node>();
nodes.addAll(nodeService.findTargetNodesFor(NodeGroupLinkAction.P));
nodes.addAll(nodeService.findTargetNodesFor(NodeGroupLinkAction.W));
for (Node node : nodes) {
engine.getDataService().insertReloadEvents(node, true, processInfo);
queuedLoad = true;
}
if (!queuedLoad) {
log.info("{} was enabled but no nodes were linked to load", ParameterConstants.AUTO_RELOAD_REVERSE_ENABLED);
}
}
use of org.jumpmind.symmetric.service.INodeService 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);
}
}
Aggregations