@@ -145,69 +145,79 @@ public void start() {
145
145
}
146
146
147
147
protected void doStart (ClusterState clusterState ) {
148
- final Predicate <ClusterState > masterChangePredicate = MasterNodeChangePredicate .build (clusterState );
149
- final DiscoveryNodes nodes = clusterState .nodes ();
150
- if (nodes .isLocalNodeElectedMaster () || localExecute (request )) {
151
- // check for block, if blocked, retry, else, execute locally
152
- final ClusterBlockException blockException = checkBlock (request , clusterState );
153
- if (blockException != null ) {
154
- if (!blockException .retryable ()) {
155
- listener .onFailure (blockException );
156
- } else {
157
- logger .trace ("can't execute due to a cluster block, retrying" , blockException );
158
- retry (blockException , newState -> {
159
- ClusterBlockException newException = checkBlock (request , newState );
160
- return (newException == null || !newException .retryable ());
161
- });
162
- }
163
- } else {
164
- ActionListener <Response > delegate = new ActionListener <Response >() {
165
- @ Override
166
- public void onResponse (Response response ) {
167
- listener .onResponse (response );
148
+ try {
149
+ final Predicate <ClusterState > masterChangePredicate = MasterNodeChangePredicate .build (clusterState );
150
+ final DiscoveryNodes nodes = clusterState .nodes ();
151
+ if (nodes .isLocalNodeElectedMaster () || localExecute (request )) {
152
+ // check for block, if blocked, retry, else, execute locally
153
+ final ClusterBlockException blockException = checkBlock (request , clusterState );
154
+ if (blockException != null ) {
155
+ if (!blockException .retryable ()) {
156
+ listener .onFailure (blockException );
157
+ } else {
158
+ logger .trace ("can't execute due to a cluster block, retrying" , blockException );
159
+ retry (blockException , newState -> {
160
+ try {
161
+ ClusterBlockException newException = checkBlock (request , newState );
162
+ return (newException == null || !newException .retryable ());
163
+ } catch (Exception e ) {
164
+ // accept state as block will be rechecked by doStart() and listener.onFailure() then called
165
+ logger .trace ("exception occurred during cluster block checking, accepting state" , e );
166
+ return true ;
167
+ }
168
+ });
168
169
}
170
+ } else {
171
+ ActionListener <Response > delegate = new ActionListener <Response >() {
172
+ @ Override
173
+ public void onResponse (Response response ) {
174
+ listener .onResponse (response );
175
+ }
169
176
170
- @ Override
171
- public void onFailure (Exception t ) {
172
- if (t instanceof Discovery .FailedToCommitClusterStateException
177
+ @ Override
178
+ public void onFailure (Exception t ) {
179
+ if (t instanceof Discovery .FailedToCommitClusterStateException
173
180
|| (t instanceof NotMasterException )) {
174
- logger .debug (() -> new ParameterizedMessage ("master could not publish cluster state or stepped down before publishing action [{}], scheduling a retry" , actionName ), t );
175
- retry (t , masterChangePredicate );
176
- } else {
177
- listener .onFailure (t );
181
+ logger .debug (() -> new ParameterizedMessage ("master could not publish cluster state or stepped down before publishing action [{}], scheduling a retry" , actionName ), t );
182
+ retry (t , masterChangePredicate );
183
+ } else {
184
+ listener .onFailure (t );
185
+ }
178
186
}
179
- }
180
- };
181
- threadPool .executor (executor ).execute (new ActionRunnable (delegate ) {
182
- @ Override
183
- protected void doRun () throws Exception {
184
- masterOperation (task , request , clusterState , delegate );
185
- }
186
- });
187
- }
188
- } else {
189
- if (nodes .getMasterNode () == null ) {
190
- logger .debug ("no known master node, scheduling a retry" );
191
- retry (null , masterChangePredicate );
187
+ };
188
+ threadPool .executor (executor ).execute (new ActionRunnable (delegate ) {
189
+ @ Override
190
+ protected void doRun () throws Exception {
191
+ masterOperation (task , request , clusterState , delegate );
192
+ }
193
+ });
194
+ }
192
195
} else {
193
- DiscoveryNode masterNode = nodes .getMasterNode ();
194
- final String actionName = getMasterActionName (masterNode );
195
- transportService .sendRequest (masterNode , actionName , request , new ActionListenerResponseHandler <Response >(listener ,
196
- TransportMasterNodeAction .this ::newResponse ) {
197
- @ Override
198
- public void handleException (final TransportException exp ) {
199
- Throwable cause = exp .unwrapCause ();
200
- if (cause instanceof ConnectTransportException ) {
201
- // we want to retry here a bit to see if a new master is elected
202
- logger .debug ("connection exception while trying to forward request with action name [{}] to master node [{}], scheduling a retry. Error: [{}]" ,
196
+ if (nodes .getMasterNode () == null ) {
197
+ logger .debug ("no known master node, scheduling a retry" );
198
+ retry (null , masterChangePredicate );
199
+ } else {
200
+ DiscoveryNode masterNode = nodes .getMasterNode ();
201
+ final String actionName = getMasterActionName (masterNode );
202
+ transportService .sendRequest (masterNode , actionName , request , new ActionListenerResponseHandler <Response >(listener ,
203
+ TransportMasterNodeAction .this ::newResponse ) {
204
+ @ Override
205
+ public void handleException (final TransportException exp ) {
206
+ Throwable cause = exp .unwrapCause ();
207
+ if (cause instanceof ConnectTransportException ) {
208
+ // we want to retry here a bit to see if a new master is elected
209
+ logger .debug ("connection exception while trying to forward request with action name [{}] to master node [{}], scheduling a retry. Error: [{}]" ,
203
210
actionName , nodes .getMasterNode (), exp .getDetailedMessage ());
204
- retry (cause , masterChangePredicate );
205
- } else {
206
- listener .onFailure (exp );
211
+ retry (cause , masterChangePredicate );
212
+ } else {
213
+ listener .onFailure (exp );
214
+ }
207
215
}
208
- }
209
- });
216
+ });
217
+ }
210
218
}
219
+ } catch (Exception e ) {
220
+ listener .onFailure (e );
211
221
}
212
222
}
213
223
0 commit comments