/
ManagedServer.java
917 lines (813 loc) · 34 KB
/
ManagedServer.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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.host.controller;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HOST;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RUNNING_SERVER;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.TIMEOUT;
import static org.jboss.as.host.controller.logging.HostControllerLogger.ROOT_LOGGER;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.jboss.as.controller.CurrentOperationIdHolder;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.ProxyOperationAddressTranslator;
import org.jboss.as.controller.TransformingProxyController;
import org.jboss.as.controller.client.helpers.domain.ServerStatus;
import org.jboss.as.controller.remote.TransactionalProtocolClient;
import org.jboss.as.controller.remote.TransactionalProtocolHandlers;
import org.jboss.as.controller.transform.TransformationTarget;
import org.jboss.as.controller.transform.Transformers;
import org.jboss.as.network.NetworkUtils;
import org.jboss.as.process.ProcessControllerClient;
import org.jboss.as.protocol.mgmt.ManagementChannelHandler;
import org.jboss.as.server.DomainServerCommunicationServices;
import org.jboss.as.server.ServerStartTask;
import org.jboss.dmr.ModelNode;
import org.jboss.logging.Logger;
import org.jboss.marshalling.Marshaller;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;
import org.jboss.marshalling.SimpleClassResolver;
import org.jboss.msc.service.ServiceActivator;
/**
* Represents a managed server.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
* @author <a href="mailto:kabir.khan@jboss.com">Kabir Khan</a>
* @author Brian Stansberry
* @author Emanuel Muckenhuber
*/
class ManagedServer {
private static final Logger.Level DEBUG_LEVEL = Logger.Level.TRACE;
private static final MarshallerFactory MARSHALLER_FACTORY;
private static final MarshallingConfiguration CONFIG;
static {
MARSHALLER_FACTORY = Marshalling.getMarshallerFactory("river", Marshalling.class.getClassLoader());
final ClassLoader cl = ManagedServer.class.getClassLoader();
final MarshallingConfiguration config = new MarshallingConfiguration();
config.setVersion(2);
config.setClassResolver(new SimpleClassResolver(cl));
CONFIG = config;
}
/**
* Prefix applied to a server's name to create it's process name.
*/
private static final String SERVER_PROCESS_NAME_PREFIX = "Server:";
static String getServerProcessName(String serverName) {
return SERVER_PROCESS_NAME_PREFIX + serverName;
}
static boolean isServerProcess(String serverProcessName) {
return serverProcessName.startsWith(SERVER_PROCESS_NAME_PREFIX);
}
static String getServerName(String serverProcessName) {
return serverProcessName.substring(SERVER_PROCESS_NAME_PREFIX.length());
}
private final byte[] authKey;
private final String serverName;
private final String serverProcessName;
private final String hostControllerName;
private final InetSocketAddress managementSocket;
private final ProcessControllerClient processControllerClient;
private final ManagedServerProxy protocolClient;
private final TransformingProxyController proxyController;
private volatile boolean requiresReload;
private volatile InternalState requiredState = InternalState.STOPPED;
private volatile InternalState internalState = InternalState.STOPPED;
private volatile int operationID = CurrentOperationIdHolder.getCurrentOperationID();
private volatile ManagedServerBootConfiguration bootConfiguration;
ManagedServer(final String hostControllerName, final String serverName, final byte[] authKey,
final ProcessControllerClient processControllerClient, final InetSocketAddress managementSocket,
final TransformationTarget transformationTarget) {
assert hostControllerName != null : "hostControllerName is null";
assert serverName != null : "serverName is null";
assert processControllerClient != null : "processControllerSlave is null";
assert managementSocket != null : "managementSocket is null";
this.hostControllerName = hostControllerName;
this.serverName = serverName;
this.serverProcessName = getServerProcessName(serverName);
this.processControllerClient = processControllerClient;
this.managementSocket = managementSocket;
this.authKey = authKey;
// Setup the proxy controller
final PathElement serverPath = PathElement.pathElement(RUNNING_SERVER, serverName);
final PathAddress address = PathAddress.EMPTY_ADDRESS.append(PathElement.pathElement(HOST, hostControllerName), serverPath);
this.protocolClient = new ManagedServerProxy(this);
this.proxyController = TransformingProxyController.Factory.create(protocolClient,
Transformers.Factory.create(transformationTarget), address, ProxyOperationAddressTranslator.SERVER, true);
}
/**
* Get the process auth key.
*
* @return the auth key
*/
byte[] getAuthKey() {
return authKey;
}
/**
* Get the server name.
*
* @return the server name
*/
String getServerName() {
return serverName;
}
/**
* Get the transforming proxy controller instance.
*
* @return the proxy controller
*/
TransformingProxyController getProxyController() {
return proxyController;
}
/**
* Determine the current state the server is in.
*
* @return the server status
*/
ServerStatus getState() {
final InternalState requiredState = this.requiredState;
final InternalState state = internalState;
if(requiredState == InternalState.FAILED) {
return ServerStatus.FAILED;
}
switch (state) {
case STOPPED:
return ServerStatus.STOPPED;
case SERVER_STARTED:
return ServerStatus.STARTED;
default: {
if(requiredState == InternalState.SERVER_STARTED) {
return ServerStatus.STARTING;
} else {
return ServerStatus.STOPPING;
}
}
}
}
boolean isRequiresReload() {
return requiresReload;
}
/**
* Require a reload on the the next reconnect.
*/
void requireReload() {
requiresReload = true;
}
/**
* Reload a managed server.
*
* @param permit the controller permit
* @return whether the state was changed successfully or not
*/
synchronized boolean reload(int permit) {
return internalSetState(new ReloadTask(permit), InternalState.SERVER_STARTED, InternalState.RELOADING);
}
/**
* Start a managed server.
*
* @param factory the boot command factory
*/
synchronized void start(final ManagedServerBootCmdFactory factory) {
final InternalState required = this.requiredState;
// Ignore if the server is already started
if(required == InternalState.SERVER_STARTED) {
return;
}
// In case the server failed to start, try to start it again
if(required != InternalState.FAILED) {
final InternalState current = this.internalState;
if(current != required) {
// TODO this perhaps should wait?
throw new IllegalStateException();
}
}
operationID = CurrentOperationIdHolder.getCurrentOperationID();
bootConfiguration = factory.createConfiguration();
requiredState = InternalState.SERVER_STARTED;
ROOT_LOGGER.startingServer(serverName);
transition();
}
/**
* Stop a managed server.
*/
synchronized void stop(int operationID, int timeout) {
final InternalState required = this.requiredState;
if(required != InternalState.STOPPED) {
this.requiredState = InternalState.STOPPED;
ROOT_LOGGER.stoppingServer(serverName);
internalSetState(new ServerStopTask(operationID, timeout), internalState, InternalState.PROCESS_STOPPING);
}
}
synchronized void destroy(int permit) {
final InternalState required = this.requiredState;
if(required == InternalState.STOPPED) {
if(internalState != InternalState.STOPPED) {
try {
processControllerClient.destroyProcess(serverProcessName);
} catch (IOException e) {
ROOT_LOGGER.logf(DEBUG_LEVEL, e, "failed to send destroy_process message to %s", serverName);
}
}
} else {
stop(permit, 0);
}
}
synchronized void kill(int permit) {
final InternalState required = this.requiredState;
if(required == InternalState.STOPPED) {
if(internalState != InternalState.STOPPED) {
try {
processControllerClient.killProcess(serverProcessName);
} catch (IOException e) {
ROOT_LOGGER.logf(DEBUG_LEVEL, e, "failed to send kill_process message to %s", serverName);
}
}
} else {
stop(permit, 0);
}
}
/**
* Try to reconnect to a started server.
*/
synchronized void reconnectServerProcess(final ManagedServerBootCmdFactory factory) {
if(this.requiredState != InternalState.SERVER_STARTED) {
this.bootConfiguration = factory;
this.requiredState = InternalState.SERVER_STARTED;
ROOT_LOGGER.reconnectingServer(serverName);
internalSetState(new ReconnectTask(), InternalState.STOPPED, InternalState.SEND_STDIN);
}
}
/**
* On host controller reload, remove a not running server registered in the process controller declared as down.
*/
synchronized void removeServerProcess() {
this.requiredState = InternalState.STOPPED;
internalSetState(new ProcessRemoveTask(), InternalState.STOPPED, InternalState.PROCESS_REMOVING);
}
/**
* On host controller reload, remove a not running server registered in the process controller declared as stopping.
*/
synchronized void setServerProcessStopping() {
this.requiredState = InternalState.STOPPED;
internalSetState(null, InternalState.STOPPED, InternalState.PROCESS_STOPPING);
}
/**
* Await a state.
*
* @param expected the expected state
* @return {@code true} if the state was reached, {@code false} otherwise
*/
boolean awaitState(final InternalState expected) {
synchronized (this) {
final InternalState initialRequired = this.requiredState;
for(;;) {
final InternalState required = this.requiredState;
// Stop in case the server failed to reach the state
if(required == InternalState.FAILED) {
return false;
// Stop in case the required state changed
} else if (initialRequired != required) {
return false;
}
final InternalState current = this.internalState;
if(expected == current) {
return true;
}
try {
wait();
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
return false;
}
}
}
}
/**
* Notification that the process was added
*/
void processAdded() {
finishTransition(InternalState.PROCESS_ADDING, InternalState.PROCESS_ADDED);
}
/**
* Notification that the process was started.
*/
void processStarted() {
finishTransition(InternalState.PROCESS_STARTING, InternalState.PROCESS_STARTED);
}
synchronized TransactionalProtocolClient channelRegistered(final ManagementChannelHandler channelAssociation) {
final InternalState current = this.internalState;
// Create the remote controller client
channelAssociation.getAttachments().attach(TransactionalProtocolClient.SEND_SUBJECT, Boolean.TRUE);
final TransactionalProtocolClient remoteClient = TransactionalProtocolHandlers.createClient(channelAssociation);
if (current == InternalState.RELOADING) {
internalSetState(new TransitionTask() {
@Override
public boolean execute(ManagedServer server) throws Exception {
// Update the current remote connection
protocolClient.connected(remoteClient);
// clear reload required state
requiresReload = false;
return true;
}
}, InternalState.RELOADING, InternalState.SERVER_STARTING);
} else {
internalSetState(new TransitionTask() {
@Override
public boolean execute(final ManagedServer server) throws Exception {
// Update the current remote connection
protocolClient.connected(remoteClient);
return true;
}
// TODO we just check that we are in the correct state, perhaps introduce a new state
}, InternalState.SEND_STDIN, InternalState.SERVER_STARTING);
}
return remoteClient;
}
synchronized void serverStarted(final TransitionTask task) {
internalSetState(task, InternalState.SERVER_STARTING, InternalState.SERVER_STARTED);
}
synchronized void serverStartFailed() {
internalSetState(null, InternalState.SERVER_STARTING, InternalState.FAILED);
}
/**
* Unregister the mgmt channel.
*
* @param old the proxy controller to unregister
* @param shuttingDown whether the server inventory is shutting down
* @return whether the registration can be removed from the domain-controller
*/
synchronized boolean callbackUnregistered(final TransactionalProtocolClient old, final boolean shuttingDown) {
// Disconnect the remote connection
protocolClient.disconnected(old);
// If the connection dropped without us stopping the process ask for reconnection
if(! shuttingDown && requiredState == InternalState.SERVER_STARTED) {
final InternalState state = internalState;
if(state == InternalState.PROCESS_STOPPED
|| state == InternalState.PROCESS_STOPPING
|| state == InternalState.STOPPED) {
// In case it stopped we don't reconnect
return true;
}
// In case we are reloading, it will reconnect automatically
if (state == InternalState.RELOADING) {
return true;
}
try {
ROOT_LOGGER.logf(DEBUG_LEVEL, "trying to reconnect to %s current-state (%s) required-state (%s)", serverName, state, requiredState);
internalSetState(new ReconnectTask(), state, InternalState.SEND_STDIN);
} catch (Exception e) {
ROOT_LOGGER.logf(DEBUG_LEVEL, e, "failed to send reconnect task");
}
return false;
} else {
return true;
}
}
/**
* Notification that the server process finished.
*/
synchronized void processFinished() {
final InternalState required = this.requiredState;
final InternalState state = this.internalState;
// If the server was not stopped
if(required == InternalState.STOPPED && state == InternalState.PROCESS_STOPPING) {
finishTransition(InternalState.PROCESS_STOPPING, InternalState.PROCESS_STOPPED);
} else {
this.requiredState = InternalState.FAILED;
internalSetState(null, state, InternalState.PROCESS_STOPPED);
}
}
/**
* Notification that the process got removed from the process controller.
*/
void processRemoved() {
finishTransition(InternalState.PROCESS_REMOVING, InternalState.STOPPED);
}
private void transition() {
transition(true);
}
private synchronized void transition(boolean checkAsync) {
final InternalState required = this.requiredState;
final InternalState current = this.internalState;
// Check if we are waiting for a notification from the server
if(checkAsync && current.isAsync()) {
return;
}
final InternalState next = nextState(current, required);
if(next != null) {
final TransitionTask task = getTransitionTask(next);
internalSetState(task, current, next);
}
}
/**
* Notification that a state transition failed.
*
* @param state the failed transition
*/
synchronized void transitionFailed(final InternalState state) {
final InternalState current = this.internalState;
if(state == current) {
// Revert transition and mark as failed
switch (current) {
case PROCESS_ADDING:
this.internalState = InternalState.PROCESS_STOPPED;
break;
case PROCESS_STARTED:
internalSetState(getTransitionTask(InternalState.PROCESS_STOPPING), InternalState.PROCESS_STARTED, InternalState.PROCESS_ADDED);
break;
case PROCESS_STARTING:
this.internalState = InternalState.PROCESS_ADDED;
break;
case SEND_STDIN:
case SERVER_STARTING:
this.internalState = InternalState.PROCESS_STARTED;
break;
}
this.requiredState = InternalState.FAILED;
notifyAll();
}
}
/**
* Finish a state transition from a notification.
*
* @param current
* @param next
*/
private synchronized void finishTransition(final InternalState current, final InternalState next) {
internalSetState(getTransitionTask(next), current, next);
transition();
}
private boolean internalSetState(final TransitionTask task, final InternalState current, final InternalState next) {
assert Thread.holdsLock(this); // Call under lock
final InternalState internalState = this.internalState;
ROOT_LOGGER.logf(DEBUG_LEVEL, "changing server state (%s) from %s to %s", serverName, current, next);
if(internalState == current) {
try {
if(task != null) {
if(! task.execute(this)) {
return true; // Not a failure condition
}
}
this.internalState = next;
return true;
} catch (final Exception e) {
ROOT_LOGGER.logf(DEBUG_LEVEL, e, "transition (%s > %s) failed for server \"%s\"", current, next, serverName);
transitionFailed(current);
} finally {
notifyAll();
}
}
return false;
}
private TransitionTask getTransitionTask(final InternalState next) {
switch (next) {
case PROCESS_ADDING: {
return new ProcessAddTask();
} case PROCESS_STARTING: {
return new ProcessStartTask();
} case SEND_STDIN: {
return new SendStdInTask();
} case SERVER_STARTED: {
return new ServerStartedTask();
} case PROCESS_STOPPING: {
return new ServerStopTask(-1, 0);
} case PROCESS_REMOVING: {
return new ProcessRemoveTask();
} default: {
return null;
}
}
}
private static InternalState nextState(final InternalState state, final InternalState required) {
switch (state) {
case STOPPED: {
if(required == InternalState.SERVER_STARTED) {
return InternalState.PROCESS_ADDING;
}
break;
} case PROCESS_ADDING: {
if(required == InternalState.SERVER_STARTED) {
return InternalState.PROCESS_ADDED;
}
break;
} case PROCESS_ADDED: {
if(required == InternalState.SERVER_STARTED) {
return InternalState.PROCESS_STARTING;
} else if( required == InternalState.STOPPED) {
return InternalState.PROCESS_REMOVING;
}
break;
} case PROCESS_STARTING: {
if(required == InternalState.SERVER_STARTED) {
return InternalState.PROCESS_STARTED;
}
break;
} case PROCESS_STARTED: {
if(required == InternalState.SERVER_STARTED) {
return InternalState.SEND_STDIN;
} else if( required == InternalState.STOPPED) {
return InternalState.PROCESS_STOPPING;
}
break;
} case SEND_STDIN: {
if(required == InternalState.SERVER_STARTED) {
return InternalState.SERVER_STARTING;
} else if( required == InternalState.STOPPED) {
return InternalState.PROCESS_STOPPING;
}
break;
} case SERVER_STARTING: {
if(required == InternalState.SERVER_STARTED) {
return InternalState.SERVER_STARTED;
} else if( required == InternalState.STOPPED) {
return InternalState.PROCESS_STOPPING;
}
break;
} case SERVER_STARTED: {
if(required == InternalState.STOPPED) {
return InternalState.PROCESS_STOPPING;
}
break;
} case RELOADING: {
if (required == InternalState.SERVER_STARTED) {
return InternalState.SERVER_STARTED;
} else if (required == InternalState.STOPPED) {
return InternalState.PROCESS_STOPPING;
}
break;
} case PROCESS_STOPPING: {
if(required == InternalState.STOPPED) {
return InternalState.PROCESS_STOPPED;
}
break;
} case PROCESS_STOPPED: {
if(required == InternalState.SERVER_STARTED) {
return InternalState.PROCESS_STARTING;
} else if( required == InternalState.STOPPED) {
return InternalState.PROCESS_REMOVING;
}
break;
} case PROCESS_REMOVING: {
if(required == InternalState.STOPPED) {
return InternalState.STOPPED;
}
break;
} default: {
return null;
}
}
return null;
}
boolean suspend() {
final ModelNode operation = new ModelNode();
operation.get(OP).set("suspend");
operation.get(OP_ADDR).setEmptyList();
try {
final TransactionalProtocolClient.PreparedOperation<?> prepared = TransactionalProtocolHandlers.executeBlocking(operation, protocolClient);
if (prepared.isFailed()) {
return false;
}
prepared.commit();
prepared.getFinalResult().get();
} catch (Exception ignore) {
return false;
}
return true;
}
boolean resume() {
final ModelNode operation = new ModelNode();
operation.get(OP).set("resume");
operation.get(OP_ADDR).setEmptyList();
try {
final TransactionalProtocolClient.PreparedOperation<?> prepared = TransactionalProtocolHandlers.executeBlocking(operation, protocolClient);
if (prepared.isFailed()) {
return false;
}
prepared.commit();
prepared.getFinalResult().get();
} catch (Exception ignore) {
return false;
}
return true;
}
void awaitSuspended(long timeout) {
//we just re-suspend, but this time give a timeout
final ModelNode operation = new ModelNode();
operation.get(OP).set("suspend");
operation.get(OP_ADDR).setEmptyList();
operation.get(TIMEOUT).set(timeout);
try {
final TransactionalProtocolClient.PreparedOperation<?> prepared = TransactionalProtocolHandlers.executeBlocking(operation, protocolClient);
if (prepared.isFailed()) {
return;
}
prepared.commit();
prepared.getFinalResult().get();
} catch (Exception ignore) {
return;
}
return;
}
static enum InternalState {
STOPPED,
PROCESS_ADDING(true),
PROCESS_ADDED,
PROCESS_STARTING(true),
PROCESS_STARTED,
SEND_STDIN(true),
SERVER_STARTING(true),
SERVER_STARTED,
RELOADING(true),
PROCESS_STOPPING(true),
PROCESS_STOPPED,
PROCESS_REMOVING(true),
SUSPENDING(true),
FAILED,
;
/** State transition creates an async task. */
private final boolean async;
InternalState() {
this(false);
}
InternalState(boolean async) {
this.async = async;
}
boolean isAsync() {
return async;
}
}
interface TransitionTask {
boolean execute(ManagedServer server) throws Exception;
}
private class ProcessAddTask implements TransitionTask {
@Override
public boolean execute(ManagedServer server) throws Exception {
assert Thread.holdsLock(ManagedServer.this); // Call under lock
final List<String> command = bootConfiguration.getServerLaunchCommand();
final Map<String, String> env = bootConfiguration.getServerLaunchEnvironment();
final HostControllerEnvironment environment = bootConfiguration.getHostControllerEnvironment();
// Add the process to the process controller
processControllerClient.addProcess(serverProcessName, authKey, command.toArray(new String[command.size()]), environment.getHomeDir().getAbsolutePath(), env);
return true;
}
}
private class ProcessRemoveTask implements TransitionTask {
@Override
public boolean execute(ManagedServer server) throws Exception {
assert Thread.holdsLock(ManagedServer.this); // Call under lock
// Remove process
processControllerClient.removeProcess(serverProcessName);
return true;
}
}
private class ProcessStartTask implements TransitionTask {
@Override
public boolean execute(ManagedServer server) throws Exception {
assert Thread.holdsLock(ManagedServer.this); // Call under lock
// Start the process
processControllerClient.startProcess(serverProcessName);
return true;
}
}
private class SendStdInTask implements TransitionTask {
@Override
public boolean execute(ManagedServer server) throws Exception {
assert Thread.holdsLock(ManagedServer.this); // Call under lock
// Get the standalone boot updates
final List<ModelNode> bootUpdates = Collections.emptyList(); // bootConfiguration.getBootUpdates();
final Map<String, String> launchProperties = parseLaunchProperties(bootConfiguration.getServerLaunchCommand());
final boolean useSubsystemEndpoint = bootConfiguration.isManagementSubsystemEndpoint();
final ModelNode endpointConfig = bootConfiguration.getSubsystemEndpointConfiguration();
// Send std.in
final ServiceActivator hostControllerCommActivator = DomainServerCommunicationServices.create(endpointConfig, managementSocket, serverName, serverProcessName, authKey, useSubsystemEndpoint);
final ServerStartTask startTask = new ServerStartTask(hostControllerName, serverName, 0, operationID, Collections.<ServiceActivator>singletonList(hostControllerCommActivator), bootUpdates, launchProperties);
final Marshaller marshaller = MARSHALLER_FACTORY.createMarshaller(CONFIG);
final OutputStream os = processControllerClient.sendStdin(serverProcessName);
marshaller.start(Marshalling.createByteOutput(os));
marshaller.writeObject(startTask);
marshaller.finish();
marshaller.close();
os.close();
return true;
}
}
private class ServerStartedTask implements TransitionTask {
@Override
public boolean execute(ManagedServer server) throws Exception {
return true;
}
}
private class ServerStopTask implements TransitionTask {
private final int permit;
private final int timeout;
private ServerStopTask(int permit, int timeout) {
this.permit = permit;
this.timeout = timeout;
}
@Override
public boolean execute(ManagedServer server) throws Exception {
assert Thread.holdsLock(ManagedServer.this); // Call under lock
// Stop process
try {
//graceful shutdown
//this just suspends the server, it does not actually shut it down
if (operationID != -1) {
final ModelNode operation = new ModelNode();
operation.get(OP).set("shutdown");
operation.get(OP_ADDR).setEmptyList();
operation.get("operation-id").set(permit);
operation.get("timeout").set(timeout);
final TransactionalProtocolClient.PreparedOperation<?> prepared = TransactionalProtocolHandlers.executeBlocking(operation, protocolClient);
if (prepared.isFailed()) {
return true;
}
//we stop the server via an operation
prepared.commit();
prepared.getFinalResult().get();
}
} catch (Exception ignore) {
} finally {
try {
processControllerClient.stopProcess(serverProcessName);
} catch (IOException ignore) {
}
}
return true;
}
}
private class ReconnectTask implements TransitionTask {
@Override
public boolean execute(ManagedServer server) throws Exception {
assert Thread.holdsLock(ManagedServer.this); // Call under lock
// Reconnect
final String hostName = managementSocket.getHostString();
final int port = managementSocket.getPort();
processControllerClient.reconnectProcess(serverProcessName, NetworkUtils.formatPossibleIpv6Address(hostName), port, bootConfiguration.isManagementSubsystemEndpoint(), authKey);
return true;
}
}
private class ReloadTask implements TransitionTask {
private final int permit;
private ReloadTask(int permit) {
this.permit = permit;
}
@Override
public boolean execute(ManagedServer server) throws Exception {
final ModelNode operation = new ModelNode();
operation.get(OP).set("reload");
operation.get(OP_ADDR).setEmptyList();
operation.get("operation-id").set(permit);
try {
final TransactionalProtocolClient.PreparedOperation<?> prepared = TransactionalProtocolHandlers.executeBlocking(operation, protocolClient);
if (prepared.isFailed()) {
return false;
}
prepared.commit(); // Just commit and discard the result
} catch (IOException ignore) {
//
}
return true;
}
}
private static Map<String, String> parseLaunchProperties(final List<String> commands) {
final Map<String, String> result = new LinkedHashMap<String, String>();
for (String cmd : commands) {
if (cmd.startsWith("-D")) {
final String[] parts = cmd.substring(2).split("=");
if (parts.length == 2) {
result.put(parts[0], parts[1]);
} else if (parts.length == 1) {
result.put(parts[0], "true");
}
}
}
return result;
}
}