/
ConfigurationChangedDataRouter.java
265 lines (227 loc) · 11.9 KB
/
ConfigurationChangedDataRouter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU Lesser General Public License (the
* "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.route;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.common.TableConstants;
import org.jumpmind.symmetric.model.DataMetaData;
import org.jumpmind.symmetric.model.NetworkedNode;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.NodeGroupLink;
import org.jumpmind.symmetric.service.IConfigurationService;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.symmetric.service.ITransformService;
import org.jumpmind.symmetric.service.ITriggerRouterService;
public class ConfigurationChangedDataRouter extends AbstractDataRouter implements IDataRouter {
final String CTX_KEY_RESYNC_NEEDED = "Resync."
+ ConfigurationChangedDataRouter.class.getSimpleName() + hashCode();
final String CTX_KEY_FLUSH_CHANNELS_NEEDED = "FlushChannels."
+ ConfigurationChangedDataRouter.class.getSimpleName() + hashCode();
final String CTX_KEY_FLUSH_TRANSFORMS_NEEDED = "FlushTransforms."
+ ConfigurationChangedDataRouter.class.getSimpleName() + hashCode();
public final static String KEY = "symconfig";
protected IConfigurationService configurationService;
protected INodeService nodeService;
protected ITriggerRouterService triggerRouterService;
protected IParameterService parameterService;
protected ITransformService transformService;
protected String tablePrefix;
public ConfigurationChangedDataRouter() {
}
public ConfigurationChangedDataRouter(IConfigurationService configurationService,
INodeService nodeService, ITriggerRouterService triggerRouterService,
IParameterService parameterService, ITransformService transformService) {
this.configurationService = configurationService;
this.nodeService = nodeService;
this.triggerRouterService = triggerRouterService;
this.parameterService = parameterService;
this.tablePrefix = parameterService.getTablePrefix();
this.transformService = transformService;
}
public Set<String> routeToNodes(SimpleRouterContext routingContext, DataMetaData dataMetaData,
Set<Node> possibleTargetNodes, boolean initialLoad) {
// the list of nodeIds that we will return
Set<String> nodeIds = null;
// the inbound data
Map<String, String> columnValues = getDataMap(dataMetaData);
Node me = findIdentity();
NetworkedNode rootNetworkedNode = getRootNetworkNodeFromContext(routingContext);
// if this is sym_node or sym_node_security determine which nodes it
// goes to.
if (tableMatches(dataMetaData, TableConstants.SYM_NODE)
|| tableMatches(dataMetaData, TableConstants.SYM_NODE_SECURITY)
|| tableMatches(dataMetaData, TableConstants.SYM_NODE_HOST)) {
String nodeIdInQuestion = columnValues.get("NODE_ID");
List<NodeGroupLink> nodeGroupLinks = getNodeGroupLinksFromContext(routingContext);
for (Node nodeThatMayBeRoutedTo : possibleTargetNodes) {
if (isLinked(nodeIdInQuestion, nodeThatMayBeRoutedTo, rootNetworkedNode, me,
nodeGroupLinks)
&& !isSameNumberOfLinksAwayFromRoot(nodeThatMayBeRoutedTo,
rootNetworkedNode, me)
|| (nodeThatMayBeRoutedTo.getNodeId().equals(me.getNodeId()) && initialLoad)) {
if (nodeIds == null) {
nodeIds = new HashSet<String>();
}
nodeIds.add(nodeThatMayBeRoutedTo.getNodeId());
}
}
// don't route node security to it's own node. that node will get
// node security
// via registration and it will be updated by initial load
if (!initialLoad && nodeIds != null
&& tableMatches(dataMetaData, TableConstants.SYM_NODE_SECURITY)) {
nodeIds.remove(nodeIdInQuestion);
}
} else {
for (Node nodeThatMayBeRoutedTo : possibleTargetNodes) {
if (!isSameNumberOfLinksAwayFromRoot(nodeThatMayBeRoutedTo, rootNetworkedNode, me)
|| (nodeThatMayBeRoutedTo.getNodeId().equals(me.getNodeId()) && initialLoad)) {
if (nodeIds == null) {
nodeIds = new HashSet<String>();
}
nodeIds.add(nodeThatMayBeRoutedTo.getNodeId());
}
}
if (StringUtils.isBlank(dataMetaData.getData().getSourceNodeId())
&& (tableMatches(dataMetaData, TableConstants.SYM_TRIGGER)
|| tableMatches(dataMetaData, TableConstants.SYM_TRIGGER_ROUTER)
|| tableMatches(dataMetaData, TableConstants.SYM_ROUTER) || tableMatches(
dataMetaData, TableConstants.SYM_NODE_GROUP_LINK))) {
routingContext.getContextCache().put(CTX_KEY_RESYNC_NEEDED, Boolean.TRUE);
}
if (tableMatches(dataMetaData, TableConstants.SYM_CHANNEL)) {
routingContext.getContextCache().put(CTX_KEY_FLUSH_CHANNELS_NEEDED, Boolean.TRUE);
}
if (tableMatches(dataMetaData, TableConstants.SYM_TRANSFORM_COLUMN)
|| tableMatches(dataMetaData, TableConstants.SYM_TRANSFORM_TABLE)) {
routingContext.getContextCache().put(CTX_KEY_FLUSH_TRANSFORMS_NEEDED, Boolean.TRUE);
}
}
return nodeIds;
}
protected Node findIdentity() {
return nodeService.findIdentity();
}
@SuppressWarnings("unchecked")
protected List<NodeGroupLink> getNodeGroupLinksFromContext(SimpleRouterContext routingContext) {
List<NodeGroupLink> list = (List<NodeGroupLink>) routingContext.getContextCache().get(
NodeGroupLink.class.getName());
if (list == null) {
list = configurationService.getNodeGroupLinks();
routingContext.getContextCache().put(NodeGroupLink.class.getName(), list);
}
return list;
}
protected NetworkedNode getRootNetworkNodeFromContext(SimpleRouterContext routingContext) {
NetworkedNode root = (NetworkedNode) routingContext.getContextCache().get(
NetworkedNode.class.getName());
if (root == null) {
root = nodeService.getRootNetworkedNode();
routingContext.getContextCache().put(NetworkedNode.class.getName(), root);
}
return root;
}
private boolean isSameNumberOfLinksAwayFromRoot(Node nodeThatCouldBeRoutedTo,
NetworkedNode root, Node me) {
return root.getNumberOfLinksAwayFromRoot(nodeThatCouldBeRoutedTo.getNodeId()) == root
.getNumberOfLinksAwayFromRoot(me.getNodeId());
}
private boolean isLinked(String nodeIdInQuestion, Node nodeThatCouldBeRoutedTo,
NetworkedNode root, Node me, List<NodeGroupLink> allLinks) {
if (nodeIdInQuestion != null && nodeThatCouldBeRoutedTo != null
&& !nodeIdInQuestion.equals(nodeThatCouldBeRoutedTo.getNodeId())) {
NetworkedNode networkedNodeInQuestion = root.findNetworkedNode(nodeIdInQuestion);
NetworkedNode networkedNodeThatCouldBeRoutedTo = root
.findNetworkedNode(nodeThatCouldBeRoutedTo.getNodeId());
if (networkedNodeInQuestion != null) {
if (networkedNodeInQuestion
.isInParentHierarchy(nodeThatCouldBeRoutedTo.getNodeId())) {
// always route changes to parent nodes
return true;
}
String createdAtNodeId = networkedNodeInQuestion.getNode().getCreatedAtNodeId();
if (createdAtNodeId != null && !createdAtNodeId.equals(me.getNodeId())
&& !networkedNodeInQuestion.getNode().getNodeId().equals(me.getNodeId())) {
if (createdAtNodeId.equals(nodeThatCouldBeRoutedTo.getNodeId())) {
return true;
} else {
// the node was created at some other node. lets attempt
// to get that update back to that node
return networkedNodeThatCouldBeRoutedTo.isInChildHierarchy(createdAtNodeId);
}
}
// if we haven't found a place to route by now, then we need to
// send the row to all nodes that have links to the node's group
String groupId = networkedNodeInQuestion.getNode().getNodeGroupId();
Set<String> groupsThatWillBeInterested = new HashSet<String>();
for (NodeGroupLink nodeGroupLink : allLinks) {
if (nodeGroupLink.getTargetNodeGroupId().equals(groupId)) {
groupsThatWillBeInterested.add(nodeGroupLink.getSourceNodeGroupId());
} else if (nodeGroupLink.getSourceNodeGroupId().equals(groupId)) {
groupsThatWillBeInterested.add(nodeGroupLink.getTargetNodeGroupId());
}
}
if (groupsThatWillBeInterested.contains(nodeThatCouldBeRoutedTo.getNodeGroupId())) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return true;
}
}
@Override
public void contextCommitted(SimpleRouterContext routingContext) {
if (routingContext.getContextCache().get(CTX_KEY_FLUSH_CHANNELS_NEEDED) != null) {
log.info("Channels flushed because new channels came through the data router");
configurationService.reloadChannels();
}
if (routingContext.getContextCache().get(CTX_KEY_RESYNC_NEEDED) != null
&& parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
log.info("About to syncTriggers because new configuration came through the data router");
triggerRouterService.syncTriggers();
}
if (routingContext.getContextCache().get(CTX_KEY_FLUSH_TRANSFORMS_NEEDED) != null
&& parameterService.is(ParameterConstants.AUTO_SYNC_CONFIGURATION)) {
log.info("About to refresh the cache of transformation because new configuration came through the data router");
transformService.resetCache();
}
}
private boolean tableMatches(DataMetaData dataMetaData, String tableName) {
boolean matches = false;
if (dataMetaData.getTable().getName()
.equalsIgnoreCase(TableConstants.getTableName(tablePrefix, tableName))) {
matches = true;
}
return matches;
}
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}
}