/
RegistrationService.java
797 lines (704 loc) · 37.9 KB
/
RegistrationService.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/**
* 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 General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) 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.service.impl;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.sql.Types;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.jumpmind.db.sql.ISqlRowMapper;
import org.jumpmind.db.sql.ISqlTransaction;
import org.jumpmind.db.sql.Row;
import org.jumpmind.db.sql.mapper.StringMapper;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.config.INodeIdCreator;
import org.jumpmind.symmetric.ext.INodeRegistrationListener;
import org.jumpmind.symmetric.ext.IRegistrationRedirect;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.NodeGroupLink;
import org.jumpmind.symmetric.model.NodeHost;
import org.jumpmind.symmetric.model.NodeSecurity;
import org.jumpmind.symmetric.model.RegistrationRequest;
import org.jumpmind.symmetric.model.RegistrationRequest.RegistrationStatus;
import org.jumpmind.symmetric.model.RemoteNodeStatus.Status;
import org.jumpmind.symmetric.security.INodePasswordFilter;
import org.jumpmind.symmetric.service.IConfigurationService;
import org.jumpmind.symmetric.service.IDataExtractorService;
import org.jumpmind.symmetric.service.IDataLoaderService;
import org.jumpmind.symmetric.service.IDataService;
import org.jumpmind.symmetric.service.IExtensionService;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.IOutgoingBatchService;
import org.jumpmind.symmetric.service.IRegistrationService;
import org.jumpmind.symmetric.service.RegistrationFailedException;
import org.jumpmind.symmetric.service.RegistrationNotOpenException;
import org.jumpmind.symmetric.service.RegistrationRedirectException;
import org.jumpmind.symmetric.statistic.IStatisticManager;
import org.jumpmind.symmetric.transport.ConnectionRejectedException;
import org.jumpmind.symmetric.transport.ITransportManager;
import org.jumpmind.symmetric.transport.ServiceUnavailableException;
import org.jumpmind.symmetric.web.WebConstants;
import org.jumpmind.util.AppUtils;
import org.jumpmind.util.RandomTimeSlot;
/**
* @see IRegistrationService
*/
public class RegistrationService extends AbstractService implements IRegistrationService {
private INodeService nodeService;
private IDataExtractorService dataExtractorService;
private IDataService dataService;
private IDataLoaderService dataLoaderService;
private ITransportManager transportManager;
private IOutgoingBatchService outgoingBatchService;
private RandomTimeSlot randomTimeSlot;
private IStatisticManager statisticManager;
private IConfigurationService configurationService;
private IExtensionService extensionService;
private ISymmetricEngine engine;
private boolean allowClientRegistration = true;
public RegistrationService(ISymmetricEngine engine) {
super(engine.getParameterService(), engine.getSymmetricDialect());
this.engine = engine;
this.nodeService = engine.getNodeService();
this.dataExtractorService = engine.getDataExtractorService();
this.dataService = engine.getDataService();
this.dataLoaderService = engine.getDataLoaderService();
this.transportManager = engine.getTransportManager();
this.statisticManager = engine.getStatisticManager();
this.configurationService = engine.getConfigurationService();
this.outgoingBatchService = engine.getOutgoingBatchService();
this.extensionService = engine.getExtensionService();
this.randomTimeSlot = new RandomTimeSlot(parameterService.getExternalId(), 30);
setSqlMap(new RegistrationServiceSqlMap(symmetricDialect.getPlatform(),
createSqlReplacementTokens()));
}
public Node registerPullOnlyNode(String externalId, String nodeGroupId,
String databaseType, String databaseVersion)
throws IOException {
Node node = new Node();
node.setExternalId(externalId);
node.setNodeGroupId(nodeGroupId);
node.setDatabaseType(databaseType);
node.setDatabaseVersion(databaseVersion);
node.setDeploymentType(Constants.DEPLOYMENT_TYPE_REST);
node = processRegistration(node, null, null, true);
if (node.isSyncEnabled()) {
//set the node as registered as we have no
//virtual batch for registration to be ok'd
markNodeAsRegistered(node.getNodeId());
}
return node;
}
public boolean registerNode(Node preRegisteredNode, OutputStream out, boolean isRequestedRegistration)
throws IOException {
return registerNode(preRegisteredNode, null, null, out, isRequestedRegistration);
}
protected void extractConfiguration(OutputStream out, Node registeredNode) {
dataExtractorService.extractConfigurationStandalone(registeredNode, out);
}
protected Node processRegistration(Node nodePriorToRegistration, String remoteHost,
String remoteAddress, boolean isRequestedRegistration)
throws IOException {
Node processedNode = new Node();
processedNode.setSyncEnabled(false);
if (!allowClientRegistration) {
log.warn("Cannot register a client node until this node has synced triggers");
return processedNode;
}
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
*/
boolean requiresInitialLoad = parameterService.is(ParameterConstants.REGISTRATION_REQUIRE_INITIAL_LOAD, true);
NodeSecurity security = nodeService.findNodeSecurity(identity.getNodeId());
if (requiresInitialLoad && (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 = null;
IRegistrationRedirect registrationRedirect = extensionService.getExtensionPoint(IRegistrationRedirect.class);
if (registrationRedirect != null) {
redirectUrl = registrationRedirect.getRedirectionUrlFor(nodePriorToRegistration.getExternalId(),
nodePriorToRegistration.getNodeGroupId());
} else {
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);
foundNode.setSyncUrl(nodePriorToRegistration.getSyncUrl());
foundNode.setDatabaseType(nodePriorToRegistration.getDatabaseType());
foundNode.setDatabaseVersion(nodePriorToRegistration.getDatabaseVersion());
foundNode.setSymmetricVersion(nodePriorToRegistration.getSymmetricVersion());
foundNode.setDeploymentType(nodePriorToRegistration.getDeploymentType());
nodeService.save(foundNode);
log.info("Completed registration of node " + foundNode.toString());
/**
* 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));
markNodeAsRegistrationPending(nodeId);
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;
}
}
/**
* @see IRegistrationService#registerNode(Node, OutputStream, boolean)
*/
// Called when node connects using pull registration URL
public boolean registerNode(Node nodePriorToRegistration, String remoteHost,
String remoteAddress, OutputStream out, boolean isRequestedRegistration)
throws IOException {
Node processedNode = processRegistration(nodePriorToRegistration, remoteHost,
remoteAddress, isRequestedRegistration);
if (processedNode.isSyncEnabled()) {
/*
* Mark all configuration batches as processed because we are about to reload
* the configuration for the node
*/
outgoingBatchService.markAllConfigAsSentForNode(processedNode.getNodeId());
extractConfiguration(out, processedNode);
}
return processedNode.isSyncEnabled();
}
public List<RegistrationRequest> getRegistrationRequests(
boolean includeNodesWithOpenRegistrations) {
List<RegistrationRequest> requests = sqlTemplate.query(
getSql("selectRegistrationRequestSql"), new RegistrationRequestMapper());
if (!includeNodesWithOpenRegistrations) {
Collection<Node> nodes = nodeService.findNodesWithOpenRegistration();
Iterator<RegistrationRequest> i = requests.iterator();
while (i.hasNext()) {
RegistrationRequest registrationRequest = (RegistrationRequest) i.next();
for (Node node : nodes) {
if (node.getNodeGroupId().equals(registrationRequest.getNodeGroupId())
&& node.getExternalId().equals(registrationRequest.getExternalId())) {
i.remove();
}
}
}
}
return requests;
}
public boolean deleteRegistrationRequest(RegistrationRequest request) {
String externalId = request.getExternalId() == null ? "" : request.getExternalId();
String nodeGroupId = request.getNodeGroupId() == null ? "" : request.getNodeGroupId();
return 0 < sqlTemplate.update(getSql("deleteRegistrationRequestSql"), new Object[] {
nodeGroupId, externalId, request.getHostName(), request.getStatus().name() });
}
public void saveRegistrationRequest(RegistrationRequest request) {
/**
* Lookup existing registration requests to update the attempt count. We previously
* did this in SQL on the update, but as400 v5 didn't like that
*/
boolean foundOne = false;
List<RegistrationRequest> requests = getRegistrationRequests(true);
for (RegistrationRequest registrationRequest : requests) {
if (registrationRequest.getNodeGroupId().equals(request.getNodeGroupId()) && registrationRequest.getExternalId().equals(request.getExternalId())) {
request.setAttemptCount(registrationRequest.getAttemptCount()+1);
foundOne = true;
break;
}
}
String externalId = request.getExternalId() == null ? "" : request.getExternalId();
String nodeGroupId = request.getNodeGroupId() == null ? "" : request.getNodeGroupId();
int count = 0;
if (foundOne) {
count = sqlTemplate.update(
getSql("updateRegistrationRequestSql"),
new Object[] { request.getAttemptCount(), request.getLastUpdateBy(),
request.getLastUpdateTime(), request.getRegisteredNodeId(),
request.getStatus().name(), request.getErrorMessage(), nodeGroupId,
externalId, request.getHostName() }, new int[] {
Types.NUMERIC, Types.VARCHAR, Types.TIMESTAMP, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
}
if (count == 0) {
sqlTemplate.update(
getSql("insertRegistrationRequestSql"),
new Object[] { request.getLastUpdateBy(), request.getLastUpdateTime(),
request.getRegisteredNodeId(), request.getStatus().name(), nodeGroupId,
externalId, request.getIpAddress(), request.getHostName(),
request.getErrorMessage() }, new int[] { Types.VARCHAR, Types.TIMESTAMP,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
}
}
public String getRedirectionUrlFor(String externalId) {
List<String> list = sqlTemplate.query(getSql("getRegistrationRedirectUrlSql"),
new StringMapper(), new Object[] { externalId }, new int[] { Types.VARCHAR });
if (list.size() > 0) {
return transportManager.resolveURL(list.get(0), parameterService.getRegistrationUrl());
} else {
return null;
}
}
public void saveRegistrationRedirect(String externalIdToRedirect, String nodeIdToRedirectTo) {
int count = sqlTemplate.update(getSql("updateRegistrationRedirectUrlSql"), new Object[] {
nodeIdToRedirectTo, externalIdToRedirect }, new int[] { Types.VARCHAR,
Types.VARCHAR });
if (count == 0) {
sqlTemplate.update(getSql("insertRegistrationRedirectUrlSql"), new Object[] {
nodeIdToRedirectTo, externalIdToRedirect }, new int[] { Types.VARCHAR,
Types.VARCHAR });
}
}
/**
* @see IRegistrationService#markNodeAsRegistered(Node)
*/
public void markNodeAsRegistered(String nodeId) {
ISqlTransaction transaction = null;
try {
transaction = sqlTemplate.startSqlTransaction();
// symmetricDialect.disableSyncTriggers(transaction, nodeId);
transaction.prepareAndExecute(getSql("registerNodeSecuritySql"), nodeId);
transaction.commit();
nodeService.flushNodeAuthorizedCache();
} catch (Error ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} catch (RuntimeException ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} finally {
List<INodeRegistrationListener> registrationListeners = extensionService.getExtensionPointList(INodeRegistrationListener.class);
for (INodeRegistrationListener l : registrationListeners) {
l.registrationSyncTriggers();
}
// symmetricDialect.enableSyncTriggers(transaction);
close(transaction);
}
}
protected void markNodeAsRegistrationPending(String nodeId) {
ISqlTransaction transaction = null;
try {
transaction = sqlTemplate.startSqlTransaction();
symmetricDialect.disableSyncTriggers(transaction, nodeId);
transaction.prepareAndExecute(getSql("registrationPendingSql"), nodeId);
transaction.commit();
nodeService.flushNodeAuthorizedCache();
} catch (Error ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} catch (RuntimeException ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} finally {
symmetricDialect.enableSyncTriggers(transaction);
close(transaction);
}
}
private void sleepBeforeRegistrationRetry() {
long sleepTimeInMs = DateUtils.MILLIS_PER_SECOND
* randomTimeSlot.getRandomValueSeededByExternalId();
log.info("Could not register. Sleeping for {}ms before attempting again.", sleepTimeInMs);
List<INodeRegistrationListener> registrationListeners = extensionService.getExtensionPointList(INodeRegistrationListener.class);
for (INodeRegistrationListener l : registrationListeners) {
l.registrationNextAttemptUpdated((int)(sleepTimeInMs/1000));
}
AppUtils.sleep(sleepTimeInMs);
}
public boolean isRegisteredWithServer() {
return nodeService.findIdentity() != null;
}
/**
* @see IRegistrationService#registerWithServer()
*/
public void registerWithServer() {
boolean registered = isRegisteredWithServer();
int maxNumberOfAttempts = parameterService
.getInt(ParameterConstants.REGISTRATION_NUMBER_OF_ATTEMPTS);
while (!registered && (maxNumberOfAttempts < 0 || maxNumberOfAttempts > 0) && engine.isStarted()) {
maxNumberOfAttempts--;
registered = attemptToRegisterWithServer(maxNumberOfAttempts);
if (!registered && maxNumberOfAttempts != 0) {
sleepBeforeRegistrationRetry();
}
}
if (!registered) {
throw new RegistrationFailedException(String.format(
"Failed after trying to register %s times.",
parameterService.getString(ParameterConstants.REGISTRATION_NUMBER_OF_ATTEMPTS)));
}
}
public synchronized boolean attemptToRegisterWithServer(int maxNumberOfAttempts) {
List<INodeRegistrationListener> registrationListeners = extensionService.getExtensionPointList(INodeRegistrationListener.class);
boolean registered = isRegisteredWithServer();
if (!registered) {
try {
for (INodeRegistrationListener l : registrationListeners) {
l.registrationStarting();
}
log.info("This node is unregistered. It will attempt to register using the registration.url");
registered = dataLoaderService.loadDataFromPull(null, (String)null).getStatus() == Status.DATA_PROCESSED;
} catch (ConnectException e) {
log.warn(
"The request to register failed because the client failed to connect to the server. The connection error message was: {}",
e.getMessage());
for (INodeRegistrationListener l : registrationListeners) {
l.registrationFailed(
"The request to register failed because the client failed to connect to the server. The connection error message was: "
+ e.getMessage());
}
} catch (UnknownHostException e) {
log.warn("The request to register failed because the host was unknown. The unknown host exception was: {}", e.getMessage());
for (INodeRegistrationListener l : registrationListeners) {
l.registrationFailed("The request to register failed because the host was unknown. The unknown host exception was: "
+ e.getMessage());
}
} catch (ConnectionRejectedException e) {
log.warn(
"The request to register was rejected by the server. Either the server node is not started, the server is not configured properly or the registration url is incorrect");
for (INodeRegistrationListener l : registrationListeners) {
l.registrationFailed(
"The request to register was rejected by the server. Either the server node is not started, the server is not configured properly or the registration url is incorrect");
}
} catch (RegistrationNotOpenException e) {
log.warn("Waiting for registration to be accepted by the server. Registration is not open.");
for (INodeRegistrationListener l : registrationListeners) {
l.registrationFailed("Waiting for registration to be accepted by the server. Registration is not open.");
}
} catch (ServiceUnavailableException e) {
log.warn("Unable to register with server because the service is not available. It may be starting up.");
for (INodeRegistrationListener l : registrationListeners) {
l.registrationFailed("Unable to register with server because the service is not available. It may be starting up.");
}
} catch (Exception e) {
log.error("Unexpected error during registration: "
+ (StringUtils.isNotBlank(e.getMessage()) ? e.getMessage() : e.getClass().getName()), e);
for (INodeRegistrationListener l : registrationListeners) {
l.registrationFailed("Unexpected error during registration: "
+ (StringUtils.isNotBlank(e.getMessage()) ? e.getMessage() : e.getClass().getName()));
}
}
registered = checkRegistrationSuccessful(registered, maxNumberOfAttempts);
}
return registered;
}
protected boolean checkRegistrationSuccessful(boolean registered, int maxNumberOfAttempts) {
if (!registered && (maxNumberOfAttempts < 0 || maxNumberOfAttempts > 0)) {
registered = isRegisteredWithServer();
if (registered) {
log.info("We registered, but were not able to acknowledge our registration. Sending a sql event to the node where we registered to indicate that we are alive and registered");
Node identity = nodeService.findIdentity();
Node parentNode = nodeService.findNode(identity.getCreatedAtNodeId());
dataService
.insertSqlEvent(
parentNode,
"update "
+ tablePrefix
+ "_node_security set registration_enabled=1, registration_time=current_timestamp where node_id='"
+ identity.getNodeId() + "'", false, -1, null);
}
}
if (registered) {
List<INodeRegistrationListener> registrationListeners = extensionService.getExtensionPointList(INodeRegistrationListener.class);
Node node = nodeService.findIdentity();
if (node != null) {
log.info("Successfully registered node [id={}]", node.getNodeId());
extensionService.refresh();
dataService.heartbeat(true);
for (INodeRegistrationListener l : registrationListeners) {
l.registrationSuccessful();
}
} else {
log.error("Node identity is missing after registration. The registration server may be misconfigured or have an error");
for (INodeRegistrationListener l : registrationListeners) {
l.registrationFailed("Node identity is missing after registration. The registration server may be misconfigured or have an error");
}
registered = false;
}
}
return registered;
}
/**
* @see IRegistrationService#reOpenRegistration(String)
*/
public synchronized void reOpenRegistration(String nodeId) {
reOpenRegistration(nodeId, null, null);
}
protected synchronized void reOpenRegistration(String nodeId, String remoteHost, String remoteAddress) {
Node node = nodeService.findNode(nodeId);
NodeSecurity security = nodeService.findNodeSecurity(nodeId);
String password = null;
if (security != null && parameterService.is(ParameterConstants.REGISTRATION_REOPEN_USE_SAME_PASSWORD, true)) {
password = security.getNodePassword();
} else {
password = extensionService.getExtensionPoint(INodeIdCreator.class).generatePassword(node);
password = filterPasswordOnSaveIfNeeded(password);
}
if (node != null) {
int updateCount = sqlTemplate.update(getSql("reopenRegistrationSql"), new Object[] {
password, nodeId });
if (updateCount == 0 && nodeService.findNodeSecurity(nodeId) == null) {
// if the update count was 0, then we probably have a row in the
// node table, but not in node security.
// lets go ahead and try to insert into node security.
sqlTemplate.update(getSql("openRegistrationNodeSecuritySql"), new Object[] {
nodeId, password, nodeService.findIdentityNodeId() });
log.info("Registration was opened for {}", nodeId);
} else if (updateCount == 0) {
log.warn("Registration was already enabled for {}. No need to reenable it", nodeId);
} else {
log.info("Registration was reopened for {}", nodeId);
}
if (isNotBlank(remoteHost)) {
NodeHost nodeHost = new NodeHost(node.getNodeId(), null);
nodeHost.setHeartbeatTime(new Date());
nodeHost.setIpAddress(remoteAddress);
nodeHost.setHostName(remoteHost);
nodeService.updateNodeHost(nodeHost);
}
nodeService.flushNodeAuthorizedCache();
} else {
log.warn("There was no row with a node id of {} to 'reopen' registration for", nodeId);
}
}
/**
* @see IRegistrationService#openRegistration(String, String)
* @return The nodeId of the registered node
*/
public synchronized String openRegistration(String nodeGroup, String externalId) {
Node node = new Node();
node.setExternalId(externalId);
node.setNodeGroupId(nodeGroup);
return openRegistration(node);
}
public synchronized String openRegistration(String nodeGroup, String externalId, String remoteHost, String remoteAddress) {
Node node = new Node();
node.setExternalId(externalId);
node.setNodeGroupId(nodeGroup);
return openRegistration(node, remoteHost, remoteAddress);
}
public synchronized String openRegistration(Node node) {
return openRegistration(node, null, null);
}
protected String openRegistration(Node node, String remoteHost, String remoteAddress) {
Node me = nodeService.findIdentity();
if (me != null) {
String nodeId = extensionService.getExtensionPoint(INodeIdCreator.class).generateNodeId(node, remoteHost, remoteAddress);
Node existingNode = nodeService.findNode(nodeId);
if (existingNode == null) {
node.setNodeId(nodeId);
node.setSyncEnabled(false);
boolean masterToMasterOnly = configurationService.containsMasterToMaster();
node.setCreatedAtNodeId(masterToMasterOnly ? null: me.getNodeId());
nodeService.save(node);
// make sure there isn't a node security row lying around w/out
// a node row
nodeService.deleteNodeSecurity(nodeId);
String password = extensionService.getExtensionPoint(INodeIdCreator.class).generatePassword(node);
password = filterPasswordOnSaveIfNeeded(password);
sqlTemplate.update(getSql("openRegistrationNodeSecuritySql"), new Object[] {
nodeId, password, me.getNodeId() });
if (isNotBlank(remoteHost)) {
NodeHost nodeHost = new NodeHost(node.getNodeId(), null);
nodeHost.setHeartbeatTime(new Date());
nodeHost.setIpAddress(remoteAddress);
nodeHost.setHostName(remoteHost);
nodeService.updateNodeHost(nodeHost);
}
nodeService.flushNodeAuthorizedCache();
nodeService.flushNodeCache();
nodeService.insertNodeGroup(node.getNodeGroupId(), null);
nodeService.flushNodeGroupCache();
log.info(
"Just opened registration for external id of {} and a node group of {} and a node id of {}",
new Object[] { node.getExternalId(), node.getNodeGroupId(), nodeId });
} else {
reOpenRegistration(nodeId, remoteHost, remoteAddress);
}
return nodeId;
} else {
throw new IllegalStateException(
"This node has not been configured. Could not find a row in the identity table");
}
}
public boolean isAutoRegistration() {
return parameterService.is(ParameterConstants.AUTO_REGISTER_ENABLED);
}
private String filterPasswordOnSaveIfNeeded(String password) {
String s = password;
INodePasswordFilter nodePasswordFilter = extensionService.getExtensionPoint(INodePasswordFilter.class);
if (nodePasswordFilter != null) {
s = nodePasswordFilter.onNodeSecuritySave(password);
}
return s;
}
public boolean isRegistrationOpen(String nodeGroupId, String externalId) {
Node node = nodeService.findNodeByExternalId(nodeGroupId, externalId);
if (node != null) {
NodeSecurity security = nodeService.findNodeSecurity(node.getNodeId());
return security != null && security.isRegistrationEnabled();
}
return false;
}
public void requestNodeCopy() {
Node copyFrom = nodeService.findIdentity();
if (copyFrom == null) {
throw new IllegalStateException("No identity found. Can only copy if the node has an identity");
}
boolean copied = false;
int maxNumberOfAttempts = parameterService
.getInt(ParameterConstants.REGISTRATION_NUMBER_OF_ATTEMPTS);
while (!copied && (maxNumberOfAttempts < 0 || maxNumberOfAttempts > 0)) {
try {
log.info("Detected that node '{}' should be copied to a new node id. Attempting to contact server to accomplish this", copyFrom.getNodeId());
copied = transportManager.sendCopyRequest(copyFrom) == WebConstants.SC_OK;
if (copied) {
nodeService.deleteIdentity();
}
} catch (ConnectException e) {
log.warn("The request to copy failed because the client failed to connect to the server");
} catch (UnknownHostException e) {
log.warn("The request to copy failed because the host was unknown");
} catch (ConnectionRejectedException ex) {
log.warn("The request to copy was rejected by the server. Either the server node is not started, the server is not configured properly or the registration url is incorrect");
} catch (Exception e) {
log.error("", e);
}
maxNumberOfAttempts--;
if (!copied) {
long sleepTimeInMs = DateUtils.MILLIS_PER_SECOND
* randomTimeSlot.getRandomValueSeededByExternalId();
log.warn("Copy failed. Sleeping before attempting again.", sleepTimeInMs);
log.info("Sleeping for {}ms", sleepTimeInMs);
AppUtils.sleep(sleepTimeInMs);
}
}
if (!copied) {
throw new RegistrationFailedException(String.format(
"Failed after trying to copy %s times.",
parameterService.getString(ParameterConstants.REGISTRATION_NUMBER_OF_ATTEMPTS)));
}
}
public void setAllowClientRegistration(boolean enabled) {
this.allowClientRegistration = enabled;
}
static class RegistrationRequestMapper implements ISqlRowMapper<RegistrationRequest> {
public RegistrationRequest mapRow(Row rs) {
RegistrationRequest request = new RegistrationRequest();
request.setNodeGroupId(rs.getString("node_group_id"));
request.setExternalId(rs.getString("external_id"));
request.setStatus(RegistrationStatus.valueOf(RegistrationStatus.class,
rs.getString("status")));
request.setHostName(rs.getString("host_name"));
request.setIpAddress(rs.getString("ip_address"));
request.setAttemptCount(rs.getLong("attempt_count"));
request.setRegisteredNodeId(rs.getString("registered_node_id"));
request.setCreateTime(rs.getDateTime("create_time"));
request.setLastUpdateBy(rs.getString("last_update_by"));
request.setLastUpdateTime(rs.getDateTime("last_update_time"));
request.setErrorMessage(rs.getString("error_message"));
return request;
}
}
}