Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions common/src/main/java/org/apache/rocketmq/common/UtilAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,29 @@ public static long computeElapsedTimeMilliseconds(final long beginTime) {
}

public static boolean isItTimeToDo(final String when) {
if (StringUtils.isBlank(when)) {
return false;
}

String[] whiles = when.split(";");
if (whiles.length > 0) {
Calendar now = Calendar.getInstance();
int nowHour = now.get(Calendar.HOUR_OF_DAY);
for (String w : whiles) {
int nowHour = Integer.parseInt(w);
if (nowHour == now.get(Calendar.HOUR_OF_DAY)) {
return true;
if (StringUtils.isBlank(w)) {
continue;
}
String trimmed = w.trim();
try {
int hour = Integer.parseInt(trimmed);
if (hour < 0 || hour > 23) {
continue;
}
if (hour == nowHour) {
return true;
}
} catch (NumberFormatException ignored) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we avoid silently ignoring invalid when tokens here, or at least log a warning when a token cannot be parsed or is outside the 0-23 range? This helper is used by scheduled maintenance paths such as commitlog cleanup and topic queue mapping cleanup. If deleteWhen or a similar schedule is misconfigured, returning false forever would silently disable the task and make the operator miss the configuration problem. The previous behavior failed loudly; changing that to silent ignore seems risky without some visibility.

// Ignore invalid hour tokens to avoid breaking scheduled tasks.
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions common/src/test/java/org/apache/rocketmq/common/UtilAllTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
Expand All @@ -34,6 +35,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class UtilAllTest {
Expand Down Expand Up @@ -151,6 +153,20 @@ public void testSplit() {
assertEquals(Collections.EMPTY_LIST, UtilAll.split("", comma));
}

@Test
public void testIsItTimeToDo() {
int currentHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
assertTrue(UtilAll.isItTimeToDo(String.valueOf(currentHour)));
assertTrue(UtilAll.isItTimeToDo("foo; " + currentHour + " ; 25"));
assertTrue(UtilAll.isItTimeToDo(" " + currentHour + " "));

assertFalse(UtilAll.isItTimeToDo(null));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test depends on the hour read here matching the hour read inside UtilAll.isItTimeToDo. It can theoretically become flaky if the test crosses an hour boundary between the two calls. Could we make the positive case independent of wall-clock timing, for example by passing all valid hours (0;1;...;23) and asserting it returns true, while keeping the invalid-token cases separate?

assertFalse(UtilAll.isItTimeToDo(""));
assertFalse(UtilAll.isItTimeToDo(" ; "));
assertFalse(UtilAll.isItTimeToDo("not_a_number"));
assertFalse(UtilAll.isItTimeToDo("99"));
}

static class DemoConfig {
private int demoWidth = 0;
private int demoLength = 0;
Expand Down
Loading