maedhroz commented on code in PR #3955:
URL: https://github.com/apache/cassandra/pull/3955#discussion_r1989848542
##########
src/java/org/apache/cassandra/exceptions/RequestFailureReason.java:
##########
@@ -53,26 +59,31 @@ public enum RequestFailureReason
this.code = code;
}
- private static final RequestFailureReason[] codeToReasonMap;
+ private static final Map<Integer, RequestFailureReason> codeToReasonMap =
new HashMap<>();
+ private static final Map<Class<? extends Throwable>, RequestFailureReason>
exceptionToReasonMap = new HashMap<>();
static
{
RequestFailureReason[] reasons = values();
- int max = -1;
- for (RequestFailureReason r : reasons)
- max = max(r.code, max);
-
- RequestFailureReason[] codeMap = new RequestFailureReason[max + 1];
-
for (RequestFailureReason reason : reasons)
{
- if (codeMap[reason.code] != null)
+ if (codeToReasonMap.put(reason.code, reason) != null)
throw new RuntimeException("Two RequestFailureReason-s that
map to the same code: " + reason.code);
- codeMap[reason.code] = reason;
}
- codeToReasonMap = codeMap;
+ exceptionToReasonMap.put(TombstoneOverwhelmingException.class,
READ_TOO_MANY_TOMBSTONES);
+ exceptionToReasonMap.put(WriteTimeoutException.class, TIMEOUT);
+ exceptionToReasonMap.put(IncompatibleSchemaException.class,
INCOMPATIBLE_SCHEMA);
+ exceptionToReasonMap.put(ReadSizeAbortException.class, READ_SIZE);
+ exceptionToReasonMap.put(IndexNotAvailableException.class,
INDEX_NOT_AVAILABLE);
+ exceptionToReasonMap.put(NotCMSException.class, NOT_CMS);
+ exceptionToReasonMap.put(InvalidRoutingException.class,
INVALID_ROUTING);
+ exceptionToReasonMap.put(CoordinatorBehindException.class,
COORDINATOR_BEHIND);
+ exceptionToReasonMap.put(IndexBuildInProgressException.class,
INDEX_BUILD_IN_PROGRESS);
+
+ if (exceptionToReasonMap.size() != reasons.length-3)
+ throw new RuntimeException("A new RequestFailureReasons was
probably added and you may need to update the exceptionToReasonMap");
Review Comment:
What if we just add a nullable field to the enum `Class<? extends Throwable>
exception` and do...
```
if (reason.exception != null)
exceptionToReasonMap.put(reason.exception, reason);
```
...in the loop above? Seems like it helps us avoid the need for this size
check, which could get brittle.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]