2727import org .slf4j .LoggerFactory ;
2828
2929import org .apache .cassandra .db .TypeSizes ;
30+ import org .apache .cassandra .exceptions .ExceptionCode ;
3031import org .apache .cassandra .io .IVersionedSerializer ;
3132import org .apache .cassandra .io .util .DataInputPlus ;
3233import org .apache .cassandra .io .util .DataOutputPlus ;
@@ -144,13 +145,15 @@ public boolean isFailure()
144145
145146 final class Failure implements Result
146147 {
148+ public final ExceptionCode code ;
147149 public final String message ;
148150 // Rejection means that we were able to linearize the operation,
149151 // but it was rejected by the internal logic of the transformation.
150152 public final boolean rejected ;
151153
152- public Failure (String message , boolean rejected )
154+ public Failure (ExceptionCode code , String message , boolean rejected )
153155 {
156+ this .code = code ;
154157 this .message = message ;
155158 this .rejected = rejected ;
156159 }
@@ -159,6 +162,7 @@ public Failure(String message, boolean rejected)
159162 public String toString ()
160163 {
161164 return "Failure{" +
165+ "code='" + code + '\'' +
162166 "message='" + message + '\'' +
163167 "rejected=" + rejected +
164168 '}' ;
@@ -177,13 +181,15 @@ public boolean isFailure()
177181
178182 class Serializer implements IVersionedSerializer <Result >
179183 {
180-
184+ private static final byte SUCCESS = 1 ;
185+ private static final byte REJECTED = 2 ;
186+ private static final byte FAILED = 3 ;
181187 @ Override
182188 public void serialize (Result t , DataOutputPlus out , int version ) throws IOException
183189 {
184190 if (t instanceof Success )
185191 {
186- out .writeByte (1 );
192+ out .writeByte (SUCCESS );
187193 Version metadataVersion = t .success ().metadataVersion ;
188194 out .writeUnsignedVInt32 (metadataVersion .asInt ());
189195 Replication .serializer .serialize (t .success ().replication , out , metadataVersion );
@@ -193,7 +199,8 @@ public void serialize(Result t, DataOutputPlus out, int version) throws IOExcept
193199 {
194200 assert t instanceof Failure ;
195201 Failure failure = (Failure ) t ;
196- out .writeByte (failure .rejected ? 2 : 3 );
202+ out .writeByte (failure .rejected ? REJECTED : FAILED );
203+ out .writeUnsignedVInt32 (failure .code .value );
197204 out .writeUTF (failure .message );
198205 }
199206 }
@@ -202,7 +209,7 @@ public void serialize(Result t, DataOutputPlus out, int version) throws IOExcept
202209 public Result deserialize (DataInputPlus in , int version ) throws IOException
203210 {
204211 int b = in .readByte ();
205- if (b == 1 )
212+ if (b == SUCCESS )
206213 {
207214 Version metadataVersion = Version .fromInt (in .readUnsignedVInt32 ());
208215 Replication delta = Replication .serializer .deserialize (in , metadataVersion );
@@ -211,7 +218,9 @@ public Result deserialize(DataInputPlus in, int version) throws IOException
211218 }
212219 else
213220 {
214- return new Failure (in .readUTF (), b == 2 );
221+ return new Failure (ExceptionCode .fromValue (in .readUnsignedVInt32 ()),
222+ in .readUTF (),
223+ b == REJECTED );
215224 }
216225 }
217226
@@ -222,13 +231,14 @@ public long serializedSize(Result t, int version)
222231 if (t instanceof Success )
223232 {
224233 Version metadataVersion = t .success ().metadataVersion ;
225- size += VIntCoding .computeVIntSize (metadataVersion .asInt ());
234+ size += VIntCoding .computeUnsignedVIntSize (metadataVersion .asInt ());
226235 size += Replication .serializer .serializedSize (t .success ().replication , metadataVersion );
227236 size += Epoch .serializer .serializedSize (t .success ().epoch , metadataVersion );
228237 }
229238 else
230239 {
231240 assert t instanceof Failure ;
241+ size += VIntCoding .computeUnsignedVIntSize (((Failure ) t ).code .value );
232242 size += TypeSizes .sizeof (((Failure )t ).message );
233243 }
234244 return size ;
0 commit comments