1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.spark.network.protocol;
19 
20 import com.google.common.base.Objects;
21 import io.netty.buffer.ByteBuf;
22 
23 /**
24  * Response to {@link ChunkFetchRequest} when there is an error fetching the chunk.
25  */
26 public final class ChunkFetchFailure extends AbstractMessage implements ResponseMessage {
27   public final StreamChunkId streamChunkId;
28   public final String errorString;
29 
ChunkFetchFailure(StreamChunkId streamChunkId, String errorString)30   public ChunkFetchFailure(StreamChunkId streamChunkId, String errorString) {
31     this.streamChunkId = streamChunkId;
32     this.errorString = errorString;
33   }
34 
35   @Override
type()36   public Type type() { return Type.ChunkFetchFailure; }
37 
38   @Override
encodedLength()39   public int encodedLength() {
40     return streamChunkId.encodedLength() + Encoders.Strings.encodedLength(errorString);
41   }
42 
43   @Override
encode(ByteBuf buf)44   public void encode(ByteBuf buf) {
45     streamChunkId.encode(buf);
46     Encoders.Strings.encode(buf, errorString);
47   }
48 
decode(ByteBuf buf)49   public static ChunkFetchFailure decode(ByteBuf buf) {
50     StreamChunkId streamChunkId = StreamChunkId.decode(buf);
51     String errorString = Encoders.Strings.decode(buf);
52     return new ChunkFetchFailure(streamChunkId, errorString);
53   }
54 
55   @Override
hashCode()56   public int hashCode() {
57     return Objects.hashCode(streamChunkId, errorString);
58   }
59 
60   @Override
equals(Object other)61   public boolean equals(Object other) {
62     if (other instanceof ChunkFetchFailure) {
63       ChunkFetchFailure o = (ChunkFetchFailure) other;
64       return streamChunkId.equals(o.streamChunkId) && errorString.equals(o.errorString);
65     }
66     return false;
67   }
68 
69   @Override
toString()70   public String toString() {
71     return Objects.toStringHelper(this)
72       .add("streamChunkId", streamChunkId)
73       .add("errorString", errorString)
74       .toString();
75   }
76 }
77