1
+ /*
2
+ * Licensed to Elastic Search and Shay Banon under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. Elastic Search licenses this
6
+ * file to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ package org .elasticsearch .action .admin .cluster .node .restart ;
21
+
22
+ import org .elasticsearch .ElasticSearchException ;
23
+ import org .elasticsearch .ElasticSearchIllegalStateException ;
24
+ import org .elasticsearch .action .TransportActions ;
25
+ import org .elasticsearch .action .support .nodes .NodeOperationRequest ;
26
+ import org .elasticsearch .action .support .nodes .TransportNodesOperationAction ;
27
+ import org .elasticsearch .cluster .ClusterName ;
28
+ import org .elasticsearch .cluster .ClusterService ;
29
+ import org .elasticsearch .node .Node ;
30
+ import org .elasticsearch .threadpool .ThreadPool ;
31
+ import org .elasticsearch .transport .TransportService ;
32
+ import org .elasticsearch .util .TimeValue ;
33
+ import org .elasticsearch .util .guice .inject .Inject ;
34
+ import org .elasticsearch .util .io .stream .StreamInput ;
35
+ import org .elasticsearch .util .io .stream .StreamOutput ;
36
+ import org .elasticsearch .util .settings .Settings ;
37
+
38
+ import java .io .IOException ;
39
+ import java .util .List ;
40
+ import java .util .concurrent .TimeUnit ;
41
+ import java .util .concurrent .atomic .AtomicReferenceArray ;
42
+
43
+ import static org .elasticsearch .util .TimeValue .*;
44
+ import static org .elasticsearch .util .gcommon .collect .Lists .*;
45
+
46
+ /**
47
+ * @author kimchy (shay.banon)
48
+ */
49
+ public class TransportNodesRestartAction extends TransportNodesOperationAction <NodesRestartRequest , NodesRestartResponse , TransportNodesRestartAction .NodeRestartRequest , NodesRestartResponse .NodeRestartResponse > {
50
+
51
+ private final Node node ;
52
+
53
+ private final boolean disabled ;
54
+
55
+ @ Inject public TransportNodesRestartAction (Settings settings , ClusterName clusterName , ThreadPool threadPool ,
56
+ ClusterService clusterService , TransportService transportService ,
57
+ Node node ) {
58
+ super (settings , clusterName , threadPool , clusterService , transportService );
59
+ this .node = node ;
60
+ disabled = componentSettings .getAsBoolean ("disabled" , false );
61
+ }
62
+
63
+ @ Override protected String transportAction () {
64
+ return TransportActions .Admin .Cluster .Node .RESTART ;
65
+ }
66
+
67
+ @ Override protected String transportNodeAction () {
68
+ return "/cluster/nodes/restart/node" ;
69
+ }
70
+
71
+ @ Override protected NodesRestartResponse newResponse (NodesRestartRequest nodesShutdownRequest , AtomicReferenceArray responses ) {
72
+ final List <NodesRestartResponse .NodeRestartResponse > nodeRestartResponses = newArrayList ();
73
+ for (int i = 0 ; i < responses .length (); i ++) {
74
+ Object resp = responses .get (i );
75
+ if (resp instanceof NodesRestartResponse .NodeRestartResponse ) {
76
+ nodeRestartResponses .add ((NodesRestartResponse .NodeRestartResponse ) resp );
77
+ }
78
+ }
79
+ return new NodesRestartResponse (clusterName , nodeRestartResponses .toArray (new NodesRestartResponse .NodeRestartResponse [nodeRestartResponses .size ()]));
80
+ }
81
+
82
+ @ Override protected NodesRestartRequest newRequest () {
83
+ return new NodesRestartRequest ();
84
+ }
85
+
86
+ @ Override protected NodeRestartRequest newNodeRequest () {
87
+ return new NodeRestartRequest ();
88
+ }
89
+
90
+ @ Override protected NodeRestartRequest newNodeRequest (String nodeId , NodesRestartRequest request ) {
91
+ return new NodeRestartRequest (nodeId , request .delay );
92
+ }
93
+
94
+ @ Override protected NodesRestartResponse .NodeRestartResponse newNodeResponse () {
95
+ return new NodesRestartResponse .NodeRestartResponse ();
96
+ }
97
+
98
+ @ Override protected NodesRestartResponse .NodeRestartResponse nodeOperation (NodeRestartRequest request ) throws ElasticSearchException {
99
+ if (disabled ) {
100
+ throw new ElasticSearchIllegalStateException ("Restart is disabled" );
101
+ }
102
+ logger .info ("Restarting in [{}]" , request .delay );
103
+ threadPool .schedule (new Runnable () {
104
+ @ Override public void run () {
105
+ boolean restartWithWrapper = false ;
106
+ if (System .getProperty ("elasticsearch-service" ) != null ) {
107
+ try {
108
+ Class wrapperManager = settings .getClassLoader ().loadClass ("org.tanukisoftware.wrapper.WrapperManager" );
109
+ logger .info ("Initiating requested restart (using service)" );
110
+ wrapperManager .getMethod ("restartAndReturn" ).invoke (null );
111
+ restartWithWrapper = true ;
112
+ } catch (Throwable e ) {
113
+ e .printStackTrace ();
114
+ }
115
+ }
116
+ if (!restartWithWrapper ) {
117
+ logger .info ("Initiating requested restart" );
118
+ try {
119
+ node .stop ();
120
+ node .start ();
121
+ } catch (Exception e ) {
122
+ logger .warn ("Failed to restart" , e );
123
+ }
124
+ }
125
+ }
126
+ }, request .delay .millis (), TimeUnit .MILLISECONDS );
127
+ return new NodesRestartResponse .NodeRestartResponse (clusterService .state ().nodes ().localNode ());
128
+ }
129
+
130
+ @ Override protected boolean accumulateExceptions () {
131
+ return false ;
132
+ }
133
+
134
+ protected static class NodeRestartRequest extends NodeOperationRequest {
135
+
136
+ TimeValue delay ;
137
+
138
+ private NodeRestartRequest () {
139
+ }
140
+
141
+ private NodeRestartRequest (String nodeId , TimeValue delay ) {
142
+ super (nodeId );
143
+ this .delay = delay ;
144
+ }
145
+
146
+ @ Override public void readFrom (StreamInput in ) throws IOException {
147
+ super .readFrom (in );
148
+ delay = readTimeValue (in );
149
+ }
150
+
151
+ @ Override public void writeTo (StreamOutput out ) throws IOException {
152
+ super .writeTo (out );
153
+ delay .writeTo (out );
154
+ }
155
+ }
156
+ }
0 commit comments