Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.network.protocol;
0019 
0020 import java.util.Objects;
0021 
0022 import io.netty.buffer.ByteBuf;
0023 import org.apache.commons.lang3.builder.ToStringBuilder;
0024 import org.apache.commons.lang3.builder.ToStringStyle;
0025 
0026 import org.apache.spark.network.buffer.ManagedBuffer;
0027 import org.apache.spark.network.buffer.NettyManagedBuffer;
0028 
0029 /**
0030  * A generic RPC which is handled by a remote {@link org.apache.spark.network.server.RpcHandler}.
0031  * This will correspond to a single
0032  * {@link org.apache.spark.network.protocol.ResponseMessage} (either success or failure).
0033  */
0034 public final class RpcRequest extends AbstractMessage implements RequestMessage {
0035   /** Used to link an RPC request with its response. */
0036   public final long requestId;
0037 
0038   public RpcRequest(long requestId, ManagedBuffer message) {
0039     super(message, true);
0040     this.requestId = requestId;
0041   }
0042 
0043   @Override
0044   public Message.Type type() { return Type.RpcRequest; }
0045 
0046   @Override
0047   public int encodedLength() {
0048     // The integer (a.k.a. the body size) is not really used, since that information is already
0049     // encoded in the frame length. But this maintains backwards compatibility with versions of
0050     // RpcRequest that use Encoders.ByteArrays.
0051     return 8 + 4;
0052   }
0053 
0054   @Override
0055   public void encode(ByteBuf buf) {
0056     buf.writeLong(requestId);
0057     // See comment in encodedLength().
0058     buf.writeInt((int) body().size());
0059   }
0060 
0061   public static RpcRequest decode(ByteBuf buf) {
0062     long requestId = buf.readLong();
0063     // See comment in encodedLength().
0064     buf.readInt();
0065     return new RpcRequest(requestId, new NettyManagedBuffer(buf.retain()));
0066   }
0067 
0068   @Override
0069   public int hashCode() {
0070     return Objects.hash(requestId, body());
0071   }
0072 
0073   @Override
0074   public boolean equals(Object other) {
0075     if (other instanceof RpcRequest) {
0076       RpcRequest o = (RpcRequest) other;
0077       return requestId == o.requestId && super.equals(o);
0078     }
0079     return false;
0080   }
0081 
0082   @Override
0083   public String toString() {
0084     return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
0085       .append("requestId", requestId)
0086       .append("body", body())
0087       .toString();
0088   }
0089 }