Skip to content

Commit

Permalink
rpc: drop RpcMessage class
Browse files Browse the repository at this point in the history
Motivation:
The RpcMessage class used only the encode message xid and type and
always right at the beginning of the xdr stream. As the use case is
very limited, the direct encoding of xid ant type can be applyed to
avoid additional object allocation.

Modification:
Drop RpcMessage. Replace RpcMessage#xdrEncode with direct encoding of
xid and type.

Result:
less object alocation

Acked-by: Alber Rossi
Target: master
  • Loading branch information
kofemann committed Jul 26, 2022
1 parent 18c432c commit f7cf70a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 87 deletions.
14 changes: 7 additions & 7 deletions oncrpc4j-core/src/main/java/org/dcache/oncrpc4j/rpc/RpcCall.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 - 2021 Deutsches Elektronen-Synchroton,
* Copyright (c) 2009 - 2022 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -295,9 +295,9 @@ public void startTLS() throws IOException {
public void reject(int status, XdrAble reason) {
XdrEncodingStream xdr = _xdr;
try {
RpcMessage replyMessage = new RpcMessage(_xid, RpcMessageType.REPLY);
xdr.beginEncoding();
replyMessage.xdrEncode(_xdr);
xdr.xdrEncodeInt(_xid);
xdr.xdrEncodeInt(RpcMessageType.REPLY);
xdr.xdrEncodeInt(RpcReplyStatus.MSG_DENIED);
xdr.xdrEncodeInt(status);
reason.xdrEncode(_xdr);
Expand All @@ -324,9 +324,9 @@ public void acceptedReply(int state, XdrAble reply) {

XdrEncodingStream xdr = _xdr;
try {
RpcMessage replyMessage = new RpcMessage(_xid, RpcMessageType.REPLY);
xdr.beginEncoding();
replyMessage.xdrEncode(_xdr);
xdr.xdrEncodeInt(_xid);
xdr.xdrEncodeInt(RpcMessageType.REPLY);
xdr.xdrEncodeInt(RpcReplyStatus.MSG_ACCEPTED);
_cred.getVerifier().xdrEncode(xdr);
xdr.xdrEncodeInt(state);
Expand Down Expand Up @@ -459,8 +459,8 @@ private int callInternal(int procedure, XdrAble args, CompletionHandler<RpcReply

Xdr xdr = new Xdr(Xdr.INITIAL_XDR_SIZE);
xdr.beginEncoding();
RpcMessage rpcMessage = new RpcMessage(xid, RpcMessageType.CALL);
rpcMessage.xdrEncode(xdr);
xdr.xdrEncodeInt(xid);
xdr.xdrEncodeInt(RpcMessageType.CALL);
xdr.xdrEncodeInt(RPCVERS);
xdr.xdrEncodeInt(_prog);
xdr.xdrEncodeInt(_version);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 - 2018 Deutsches Elektronen-Synchroton,
* Copyright (c) 2009 - 2022 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -50,17 +50,19 @@ public NextAction handleRead(FilterChainContext ctx) throws IOException {

xdr.beginDecoding();

RpcMessage message = new RpcMessage(xdr);
final int xid = xdr.xdrDecodeInt();
final int type = xdr.xdrDecodeInt();

/**
* In case of UDP grizzly does not populates connection with correct destination address.
* We have to get peer address from the request context, which will contain SocketAddress where from
* request was coming.
*/
RpcTransport transport = new GrizzlyRpcTransport(ctx.getConnection(), (InetSocketAddress)ctx.getAddress(), _replyQueue);

switch (message.type()) {
switch (type) {
case RpcMessageType.CALL:
RpcCall call = new RpcCall(message.xid(), xdr, transport);
RpcCall call = new RpcCall(xid, xdr, transport);
try {
call.accept();
ctx.setMessage(call);
Expand All @@ -76,8 +78,8 @@ public NextAction handleRead(FilterChainContext ctx) throws IOException {
return ctx.getInvokeAction();
case RpcMessageType.REPLY:
try {
RpcReply reply = new RpcReply(message.xid(), xdr, transport);
CompletionHandler<RpcReply, RpcTransport> callback = _replyQueue.get(message.xid());
RpcReply reply = new RpcReply(xid, xdr, transport);
CompletionHandler<RpcReply, RpcTransport> callback = _replyQueue.get(xid);
if (callback != null) {
if (!reply.isAccepted()) {
callback.failed(new OncRpcRejectedException(reply.getRejectStatus()), transport);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 - 2018 Deutsches Elektronen-Synchroton,
* Copyright (c) 2009 - 2022 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
Expand All @@ -19,13 +19,6 @@
*/
package org.dcache.oncrpc4j.rpc;

import org.dcache.oncrpc4j.rpc.RpcAuth;
import org.dcache.oncrpc4j.rpc.RpcMessageParserTCP;
import org.dcache.oncrpc4j.rpc.RpcMessage;
import org.dcache.oncrpc4j.rpc.ReplyQueue;
import org.dcache.oncrpc4j.rpc.RpcMessageType;
import org.dcache.oncrpc4j.rpc.RpcAuthTypeNone;
import org.dcache.oncrpc4j.rpc.RpcProtocolFilter;
import org.dcache.oncrpc4j.xdr.XdrVoid;
import org.dcache.oncrpc4j.xdr.XdrAble;
import org.dcache.oncrpc4j.xdr.XdrString;
Expand Down Expand Up @@ -147,8 +140,8 @@ public Xdr build() throws OncRpcException, IOException {
Xdr xdr = new Xdr(Xdr.MAX_XDR_SIZE);
xdr.beginEncoding();

RpcMessage rpcMessage = new RpcMessage(xid, RpcMessageType.CALL);
rpcMessage.xdrEncode(xdr);
xdr.xdrEncodeInt(xid);
xdr.xdrEncodeInt(RpcMessageType.CALL);
xdr.xdrEncodeInt(rpcvers);
xdr.xdrEncodeInt(prog);
xdr.xdrEncodeInt(vers);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009 - 2018 Deutsches Elektronen-Synchroton,
* Copyright (c) 2009 - 2022 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
Expand All @@ -19,9 +19,6 @@
*/
package org.dcache.oncrpc4j.rpc;

import org.dcache.oncrpc4j.rpc.RpcMessage;
import org.dcache.oncrpc4j.rpc.ReplyQueue;
import org.dcache.oncrpc4j.rpc.RpcProtocolFilter;
import org.dcache.oncrpc4j.xdr.Xdr;
import java.io.IOException;
import org.glassfish.grizzly.Connection;
Expand Down Expand Up @@ -58,8 +55,8 @@ public void testSomeMethod() throws IOException {
private Xdr createBadXdr() {
Xdr xdr = new Xdr(32);
xdr.beginEncoding();
RpcMessage rpcMessage = new RpcMessage(1, 2); // xdr, type 0 = call, 1 = reply, 2 = not allowed
rpcMessage.xdrEncode(xdr);
xdr.xdrEncodeInt(1); // xid
xdr.xdrEncodeInt(2); // type: 0 = call, 1 = reply, x = invalid
return xdr;
}
}

0 comments on commit f7cf70a

Please sign in to comment.