ARTEMIS-1135: Prevent overflow in `DayCounter`

Use `long` array for hourly counters instead of `int` array.
Prevents overflow when the number of new messages (a `long`) is added.
Fixes one of the "Implicit narrowing conversion in compound assignment"
alerts on https://lgtm.com/projects/g/apache/activemq-artemis/alerts.
This commit is contained in:
Aditya Sharad 2017-04-29 15:52:08 +01:00 committed by Clebert Suconic
parent b998a8bdaf
commit 52a5b85557
3 changed files with 12 additions and 12 deletions

View File

@ -32,7 +32,7 @@ public final class DayCounterInfo {
private final String date; private final String date;
private final int[] counters; private final long[] counters;
// Static -------------------------------------------------------- // Static --------------------------------------------------------
@ -41,7 +41,7 @@ public final class DayCounterInfo {
JsonArrayBuilder counters = JsonLoader.createArrayBuilder(); JsonArrayBuilder counters = JsonLoader.createArrayBuilder();
for (DayCounterInfo info : infos) { for (DayCounterInfo info : infos) {
JsonArrayBuilder counter = JsonLoader.createArrayBuilder(); JsonArrayBuilder counter = JsonLoader.createArrayBuilder();
for (int c : info.getCounters()) { for (long c : info.getCounters()) {
counter.add(c); counter.add(c);
} }
JsonObjectBuilder dci = JsonLoader.createObjectBuilder().add("date", info.getDate()).add("counters", counter); JsonObjectBuilder dci = JsonLoader.createObjectBuilder().add("date", info.getDate()).add("counters", counter);
@ -63,7 +63,7 @@ public final class DayCounterInfo {
JsonObject counter = (JsonObject) dayCounters.get(i); JsonObject counter = (JsonObject) dayCounters.get(i);
JsonArray hour = counter.getJsonArray("counters"); JsonArray hour = counter.getJsonArray("counters");
int[] hourCounters = new int[24]; long[] hourCounters = new long[24];
for (int j = 0; j < 24; j++) { for (int j = 0; j < 24; j++) {
hourCounters[j] = hour.getInt(j); hourCounters[j] = hour.getInt(j);
} }
@ -75,7 +75,7 @@ public final class DayCounterInfo {
// Constructors -------------------------------------------------- // Constructors --------------------------------------------------
public DayCounterInfo(final String date, final int[] counters) { public DayCounterInfo(final String date, final long[] counters) {
this.date = date; this.date = date;
this.counters = counters; this.counters = counters;
} }
@ -93,7 +93,7 @@ public final class DayCounterInfo {
* Returns a 24-length array corresponding to the number of messages added to the queue * Returns a 24-length array corresponding to the number of messages added to the queue
* for the given hour of the day. * for the given hour of the day.
*/ */
public int[] getCounters() { public long[] getCounters() {
return counters; return counters;
} }
} }

View File

@ -395,7 +395,7 @@ public class MessageCounter {
GregorianCalendar date = null; GregorianCalendar date = null;
int[] counters = new int[DayCounter.HOURS]; long[] counters = new long[DayCounter.HOURS];
/** /**
* Constructor * Constructor
@ -415,17 +415,17 @@ public class MessageCounter {
for (int i = 0; i < DayCounter.HOURS; i++) { for (int i = 0; i < DayCounter.HOURS; i++) {
if (i < hour) { if (i < hour) {
if (isStartDay) { if (isStartDay) {
counters[i] = -1; counters[i] = -1L;
} else { } else {
counters[i] = 0; counters[i] = 0L;
} }
} else { } else {
counters[i] = -1; counters[i] = -1L;
} }
} }
// set the array element of the current hour to '0' // set the array element of the current hour to '0'
counters[hour] = 0; counters[hour] = 0L;
} }
/** /**
@ -437,7 +437,7 @@ public class MessageCounter {
return (GregorianCalendar) date.clone(); return (GregorianCalendar) date.clone();
} }
public int[] getCounters() { public long[] getCounters() {
return counters; return counters;
} }

View File

@ -38,7 +38,7 @@ public class MessageCounterHelper {
DayCounterInfo[] infos = new DayCounterInfo[history.size()]; DayCounterInfo[] infos = new DayCounterInfo[history.size()];
for (int i = 0; i < infos.length; i++) { for (int i = 0; i < infos.length; i++) {
DayCounter dayCounter = history.get(i); DayCounter dayCounter = history.get(i);
int[] counters = dayCounter.getCounters(); long[] counters = dayCounter.getCounters();
GregorianCalendar date = dayCounter.getDate(); GregorianCalendar date = dayCounter.getDate();
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);