Skip to content

Commit 79a3f8c

Browse files
CLOUDSTACK-8822 - Replacing Runnable by Callable in the Taks and NioConnection classes
- All the sub-classes were also updated according to the changes in the super-classes - There were also code formatting changes
1 parent 2d90f18 commit 79a3f8c

11 files changed

Lines changed: 582 additions & 428 deletions

File tree

agent/src/com/cloud/agent/Agent.java

Lines changed: 69 additions & 56 deletions
Large diffs are not rendered by default.

engine/orchestration/src/com/cloud/agent/manager/AgentManagerImpl.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
import com.cloud.utils.db.TransactionLegacy;
104104
import com.cloud.utils.exception.CloudRuntimeException;
105105
import com.cloud.utils.exception.HypervisorVersionChangedException;
106+
import com.cloud.utils.exception.NioConnectionException;
107+
import com.cloud.utils.exception.TaskExecutionException;
106108
import com.cloud.utils.fsm.NoTransitionException;
107109
import com.cloud.utils.fsm.StateMachine2;
108110
import com.cloud.utils.nio.HandlerFactory;
@@ -593,7 +595,11 @@ public boolean start() {
593595
startDirectlyConnectedHosts();
594596

595597
if (_connection != null) {
596-
_connection.start();
598+
try {
599+
_connection.start();
600+
} catch (final NioConnectionException e) {
601+
s_logger.error("Error when connecting to the NioServer!", e);
602+
}
597603
}
598604

599605
_monitorExecutor.scheduleWithFixedDelay(new MonitorTask(), PingInterval.value(), PingInterval.value(), TimeUnit.SECONDS);
@@ -827,7 +833,7 @@ protected boolean handleDisconnectWithInvestigation(final AgentAttache attache,
827833
Status determinedState = investigate(attache);
828834
// if state cannot be determined do nothing and bail out
829835
if (determinedState == null) {
830-
if (((System.currentTimeMillis() >> 10) - host.getLastPinged()) > AlertWait.value()) {
836+
if ((System.currentTimeMillis() >> 10) - host.getLastPinged() > AlertWait.value()) {
831837
s_logger.warn("Agent " + hostId + " state cannot be determined for more than " + AlertWait + "(" + AlertWait.value() + ") seconds, will go to Alert state");
832838
determinedState = Status.Alert;
833839
} else {
@@ -840,7 +846,7 @@ protected boolean handleDisconnectWithInvestigation(final AgentAttache attache,
840846
s_logger.info("The agent " + hostId + " state determined is " + determinedState);
841847

842848
if (determinedState == Status.Down) {
843-
String message = "Host is down: " + host.getId() + "-" + host.getName() + ". Starting HA on the VMs";
849+
final String message = "Host is down: " + host.getId() + "-" + host.getName() + ". Starting HA on the VMs";
844850
s_logger.error(message);
845851
if (host.getType() != Host.Type.SecondaryStorage && host.getType() != Host.Type.ConsoleProxy) {
846852
_alertMgr.sendAlert(AlertManager.AlertType.ALERT_TYPE_HOST, host.getDataCenterId(), host.getPodId(), "Host down, " + host.getId(), message);
@@ -1299,7 +1305,7 @@ protected void processResponse(final Link link, final Response response) {
12991305
}
13001306

13011307
@Override
1302-
protected void doTask(final Task task) throws Exception {
1308+
protected void doTask(final Task task) throws TaskExecutionException {
13031309
final TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
13041310
try {
13051311
final Type type = task.getType();
@@ -1315,6 +1321,10 @@ protected void doTask(final Task task) throws Exception {
13151321
} catch (final UnsupportedVersionException e) {
13161322
s_logger.warn(e.getMessage());
13171323
// upgradeAgent(task.getLink(), data, e.getReason());
1324+
} catch (final ClassNotFoundException e) {
1325+
final String message = String.format("Exception occured when executing taks! Error '%s'", e.getMessage());
1326+
s_logger.error(message);
1327+
throw new TaskExecutionException(message, e);
13181328
}
13191329
} else if (type == Task.Type.CONNECT) {
13201330
} else if (type == Task.Type.DISCONNECT) {

engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java

Lines changed: 185 additions & 178 deletions
Large diffs are not rendered by default.

utils/src/main/java/com/cloud/utils/SerialVersionUID.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ public interface SerialVersionUID {
6666
public static final long UnableDeleteHostException = Base | 0x29;
6767
public static final long AffinityConflictException = Base | 0x2a;
6868
public static final long JobCancellationException = Base | 0x2b;
69+
public static final long NioConnectionException = Base | 0x2c;
70+
public static final long TaskExecutionException = Base | 0x2d;
6971
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// 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 com.cloud.utils.exception;
21+
22+
import com.cloud.utils.SerialVersionUID;
23+
24+
/**
25+
* Used by the NioConnection class to wrap-up its exceptions.
26+
*/
27+
public class NioConnectionException extends Exception {
28+
private static final long serialVersionUID = SerialVersionUID.NioConnectionException;
29+
30+
protected int csErrorCode;
31+
32+
public NioConnectionException(final String msg, final Throwable cause) {
33+
super(msg, cause);
34+
setCSErrorCode(CSExceptionErrorCode.getCSErrCode(this.getClass().getName()));
35+
}
36+
37+
public NioConnectionException(final String msg) {
38+
super(msg);
39+
}
40+
41+
public void setCSErrorCode(final int cserrcode) {
42+
csErrorCode = cserrcode;
43+
}
44+
45+
public int getCSErrorCode() {
46+
return csErrorCode;
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// 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 com.cloud.utils.exception;
21+
22+
import com.cloud.utils.SerialVersionUID;
23+
24+
/**
25+
* Used by the Task class to wrap-up its exceptions.
26+
*/
27+
public class TaskExecutionException extends Exception {
28+
private static final long serialVersionUID = SerialVersionUID.NioConnectionException;
29+
30+
protected int csErrorCode;
31+
32+
public TaskExecutionException(final String msg, final Throwable cause) {
33+
super(msg, cause);
34+
setCSErrorCode(CSExceptionErrorCode.getCSErrCode(this.getClass().getName()));
35+
}
36+
37+
public TaskExecutionException(final String msg) {
38+
super(msg);
39+
}
40+
41+
public void setCSErrorCode(final int cserrcode) {
42+
csErrorCode = cserrcode;
43+
}
44+
45+
public int getCSErrorCode() {
46+
return csErrorCode;
47+
}
48+
}

utils/src/main/java/com/cloud/utils/nio/NioClient.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@
2929
import javax.net.ssl.SSLContext;
3030
import javax.net.ssl.SSLEngine;
3131

32-
import org.apache.log4j.Logger;
33-
3432
import org.apache.cloudstack.utils.security.SSLUtils;
33+
import org.apache.log4j.Logger;
3534

3635
public class NioClient extends NioConnection {
3736
private static final Logger s_logger = Logger.getLogger(NioClient.class);
@@ -40,12 +39,12 @@ public class NioClient extends NioConnection {
4039
protected String _bindAddress;
4140
protected SocketChannel _clientConnection;
4241

43-
public NioClient(String name, String host, int port, int workers, HandlerFactory factory) {
42+
public NioClient(final String name, final String host, final int port, final int workers, final HandlerFactory factory) {
4443
super(name, port, workers, factory);
4544
_host = host;
4645
}
4746

48-
public void setBindAddress(String ipAddress) {
47+
public void setBindAddress(final String ipAddress) {
4948
_bindAddress = ipAddress;
5049
}
5150

@@ -62,18 +61,18 @@ protected void init() throws IOException {
6261
if (_bindAddress != null) {
6362
s_logger.info("Binding outbound interface at " + _bindAddress);
6463

65-
InetSocketAddress bindAddr = new InetSocketAddress(_bindAddress, 0);
64+
final InetSocketAddress bindAddr = new InetSocketAddress(_bindAddress, 0);
6665
_clientConnection.socket().bind(bindAddr);
6766
}
6867

69-
InetSocketAddress peerAddr = new InetSocketAddress(_host, _port);
68+
final InetSocketAddress peerAddr = new InetSocketAddress(_host, _port);
7069
_clientConnection.connect(peerAddr);
7170

7271
SSLEngine sslEngine = null;
7372
// Begin SSL handshake in BLOCKING mode
7473
_clientConnection.configureBlocking(true);
7574

76-
SSLContext sslContext = Link.initSSLContext(true);
75+
final SSLContext sslContext = Link.initSSLContext(true);
7776
sslEngine = sslContext.createSSLEngine(_host, _port);
7877
sslEngine.setUseClientMode(true);
7978
sslEngine.setEnabledProtocols(SSLUtils.getSupportedProtocols(sslEngine.getEnabledProtocols()));
@@ -83,32 +82,31 @@ protected void init() throws IOException {
8382
s_logger.info("Connected to " + _host + ":" + _port);
8483

8584
_clientConnection.configureBlocking(false);
86-
Link link = new Link(peerAddr, this);
85+
final Link link = new Link(peerAddr, this);
8786
link.setSSLEngine(sslEngine);
88-
SelectionKey key = _clientConnection.register(_selector, SelectionKey.OP_READ);
87+
final SelectionKey key = _clientConnection.register(_selector, SelectionKey.OP_READ);
8988
link.setKey(key);
9089
key.attach(link);
9190
// Notice we've already connected due to the handshake, so let's get the
9291
// remaining task done
9392
task = _factory.create(Task.Type.CONNECT, link, null);
94-
} catch (GeneralSecurityException e) {
93+
} catch (final GeneralSecurityException e) {
9594
_selector.close();
9695
throw new IOException("Failed to initialise security", e);
97-
} catch (IOException e) {
96+
} catch (final IOException e) {
9897
_selector.close();
9998
throw e;
10099
}
101-
102-
_executor.execute(task);
100+
_executor.submit(task);
103101
}
104102

105103
@Override
106-
protected void registerLink(InetSocketAddress saddr, Link link) {
104+
protected void registerLink(final InetSocketAddress saddr, final Link link) {
107105
// don't do anything.
108106
}
109107

110108
@Override
111-
protected void unregisterLink(InetSocketAddress saddr) {
109+
protected void unregisterLink(final InetSocketAddress saddr) {
112110
// don't do anything.
113111
}
114112

@@ -119,7 +117,5 @@ public void cleanUp() throws IOException {
119117
_clientConnection.close();
120118
}
121119
s_logger.info("NioClient connection closed");
122-
123120
}
124-
125-
}
121+
}

0 commit comments

Comments
 (0)