Skip to content

Commit 9c69a98

Browse files
author
Alex Huang
committed
session info within sequence numbers
1 parent fd27127 commit 9c69a98

6 files changed

Lines changed: 168 additions & 160 deletions

File tree

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void stop(final String reason, final String detail) {
224224
final ShutdownCommand cmd = new ShutdownCommand(reason, detail);
225225
try {
226226
if (_link != null) {
227-
Request req = new Request(0, (_id != null ? _id : -1), -1, cmd, false);
227+
Request req = new Request((_id != null ? _id : -1), -1, cmd, false);
228228
_link.send(req.toBytes());
229229
}
230230
} catch (final ClosedChannelException e) {
@@ -289,7 +289,8 @@ public void sendStartup(Link link) {
289289
commands[i] = startup[i];
290290
}
291291

292-
final Request request = new Request(getNextSequence(), _id != null ? _id : -1, -1, commands, false, false);
292+
final Request request = new Request(_id != null ? _id : -1, -1, commands, false, false);
293+
request.setSequence(getNextSequence());
293294

294295
if (s_logger.isDebugEnabled()) {
295296
s_logger.debug("Sending Startup: " + request.toString());
@@ -544,7 +545,8 @@ public void processOtherTask(Task task) {
544545
}
545546

546547
final PingCommand ping = _resource.getCurrentStatus(getId());
547-
final Request request = new Request(getNextSequence(), _id, -1, ping, false);
548+
final Request request = new Request(_id, -1, ping, false);
549+
request.setSequence(getNextSequence());
548550
if (s_logger.isDebugEnabled()) {
549551
s_logger.debug("Sending ping: " + request.toString());
550552
}
@@ -624,7 +626,9 @@ public void unregisterControlListener(IAgentControlListener listener) {
624626

625627
@Override
626628
public AgentControlAnswer sendRequest(AgentControlCommand cmd, int timeoutInMilliseconds) throws AgentControlChannelException {
627-
Request request = new Request(this.getNextSequence(), this.getId(), -1, new Command[] { cmd }, true, false);
629+
Request request = new Request(this.getId(), -1, new Command[] { cmd }, true, false);
630+
request.setSequence(getNextSequence());
631+
628632
AgentControlListener listener = new AgentControlListener(request);
629633

630634
registerControlListener(listener);
@@ -646,7 +650,8 @@ public AgentControlAnswer sendRequest(AgentControlCommand cmd, int timeoutInMill
646650

647651
@Override
648652
public void postRequest(AgentControlCommand cmd) throws AgentControlChannelException {
649-
Request request = new Request(this.getNextSequence(), this.getId(), -1, new Command[] { cmd }, true, false);
653+
Request request = new Request(this.getId(), -1, new Command[] { cmd }, true, false);
654+
request.setSequence(getNextSequence());
650655
postRequest(request);
651656
}
652657

‎core/src/com/cloud/agent/transport/Request.java‎

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,20 @@ protected Request(Version ver, long seq, long agentId, long mgmtId, long via, sh
147147
_content = content;
148148
}
149149

150-
public Request(long seq, long agentId, long mgmtId, final Command command, boolean fromServer) {
151-
this(seq, agentId, mgmtId, new Command[] { command }, true, fromServer);
150+
public Request(long agentId, long mgmtId, Command command, boolean fromServer) {
151+
this(agentId, mgmtId, new Command[] { command }, true, fromServer);
152152
}
153153

154-
public Request(long seq, long agentId, long mgmtId, Command[] cmds, boolean stopOnError, boolean fromServer) {
155-
this(Version.v3, seq, agentId, mgmtId, (short) 0, cmds);
154+
public Request(long agentId, long mgmtId, Command[] cmds, boolean stopOnError, boolean fromServer) {
155+
this(Version.v1, -1l, agentId, mgmtId, (short)0, cmds);
156156
setStopOnError(stopOnError);
157157
setFromServer(fromServer);
158158
}
159159

160+
public void setSequence(long seq) {
161+
_seq = seq;
162+
}
163+
160164
protected void setInSequence(Command[] cmds) {
161165
if (cmds == null) {
162166
return;
@@ -316,31 +320,55 @@ protected short getFlags() {
316320
return (short) (((this instanceof Response) ? FLAG_RESPONSE : FLAG_REQUEST) | _flags);
317321
}
318322

319-
public void log(long agentId, String msg) {
323+
public void logD(String msg) {
324+
logD(msg, true);
325+
}
326+
327+
public void logD(String msg, boolean logContent) {
328+
if (s_logger.isDebugEnabled()) {
329+
s_logger.debug(log(msg, logContent));
330+
}
331+
}
332+
333+
public void logT(String msg, boolean logD) {
334+
if (s_logger.isTraceEnabled()) {
335+
s_logger.trace(log(msg, true));
336+
} else if (logD && s_logger.isDebugEnabled()) {
337+
s_logger.debug(log(msg, false));
338+
}
339+
}
340+
341+
public String log(String msg, boolean logContent) {
320342
StringBuilder buf = new StringBuilder("Seq ");
321343
buf.append(_agentId).append("-").append(_seq).append(": ");
322344

323345
buf.append(msg);
324346
buf.append("{ ").append(getType());
325-
buf.append(", Ver: ").append(_ver.toString());
326347
buf.append(", MgmtId: ").append(_mgmtId).append(", via: ").append(_via);
327-
buf.append(", Flags: ").append(Integer.toBinaryString(getFlags())).append(", ");
328-
if (_cmds != null) {
329-
try {
330-
s_gogger.toJson(_cmds, buf);
331-
} catch (Throwable e) {
332-
s_logger.error("Gson serialization error on Request.toString() " + getClass().getCanonicalName(), e);
333-
return;
348+
if (logContent) {
349+
buf.append(", Ver: ").append(_ver.toString());
350+
buf.append(", Flags: ").append(Integer.toBinaryString(getFlags())).append(", ");
351+
if (_cmds != null) {
352+
try {
353+
s_gogger.toJson(_cmds, buf);
354+
} catch (Throwable e) {
355+
StringBuilder buff = new StringBuilder();
356+
for (Command cmd : _cmds) {
357+
buff.append(cmd.getClass().getName()).append("/");
358+
}
359+
s_logger.error("Gson serialization error " + buff.toString(), e);
360+
assert false : "More gson errors on " + buff.toString();
361+
return "";
362+
}
363+
} else if (_content != null) {
364+
buf.append(_content.subSequence(0, 32));
365+
} else {
366+
buf.append("I've got nada here!");
367+
assert false : "How can both commands and content be null? What are we sending here?";
334368
}
335-
} else if (_content != null) {
336-
buf.append(_content.subSequence(0, 32));
337-
} else {
338-
buf.append("I've got nada here!");
339-
assert false : "How can both commands and content be null? What are we sending here?";
340369
}
341370
buf.append(" }");
342-
343-
s_logger.debug(buf.toString());
371+
return buf.toString();
344372
}
345373

346374
/**
@@ -358,7 +386,7 @@ public static Request parse(final byte[] bytes) throws ClassNotFoundException, U
358386
final ByteBuffer buff = ByteBuffer.wrap(bytes);
359387
final byte ver = buff.get();
360388
final Version version = Version.get(ver);
361-
if (version.ordinal() < Version.v3.ordinal()) {
389+
if (version.ordinal() != Version.v1.ordinal()) {
362390
throw new UnsupportedVersionException("This version is no longer supported: " + version.toString(), UnsupportedVersionException.IncompatibleVersion);
363391
}
364392
final byte reserved = buff.get(); // tossed away for now.

0 commit comments

Comments
 (0)