forked from treasure-data/digdag
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request treasure-data#294 from treasure-data/attempt-limit
Limit number of active attempts
- Loading branch information
Showing
12 changed files
with
235 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
digdag-core/src/main/java/io/digdag/core/workflow/AttemptLimitExceededException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.digdag.core.workflow; | ||
|
||
/** | ||
* An exception thrown when adding more attempts to a site than limit. | ||
* | ||
* Actual number of attempts is not guaranteed because limitation of number of | ||
* attempts is based on a slight old transaction. | ||
*/ | ||
public class AttemptLimitExceededException | ||
extends LimitExceededException | ||
{ | ||
// TODO (sada): should this be a checked exception? | ||
|
||
public AttemptLimitExceededException(String message) | ||
{ | ||
super(message); | ||
} | ||
|
||
public AttemptLimitExceededException(Throwable cause) | ||
{ | ||
super(cause); | ||
} | ||
|
||
public AttemptLimitExceededException(String message, Throwable cause) | ||
{ | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
digdag-tests/src/test/java/acceptance/AttemptLimitIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package acceptance; | ||
|
||
import com.google.common.base.Optional; | ||
import io.digdag.client.DigdagClient; | ||
import io.digdag.client.api.RestSchedule; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import utils.CommandStatus; | ||
import utils.TemporaryDigdagServer; | ||
|
||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.List; | ||
|
||
import static utils.TestUtils.copyResource; | ||
import static utils.TestUtils.main; | ||
import static utils.TestUtils.expect; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assume.assumeThat; | ||
import static java.time.temporal.ChronoUnit.HOURS; | ||
|
||
public class AttemptLimitIT | ||
{ | ||
@Rule | ||
public TemporaryFolder folder = new TemporaryFolder(); | ||
|
||
@Rule | ||
public TemporaryDigdagServer server = TemporaryDigdagServer.builder() | ||
.inProcess(false) // setting system property doesn't work with in-process mode | ||
.systemProperty("io.digdag.limits.maxAttempts", "3") | ||
.build(); | ||
|
||
private Path config; | ||
private Path projectDir; | ||
private DigdagClient client; | ||
|
||
@Before | ||
public void setUp() | ||
throws Exception | ||
{ | ||
projectDir = folder.getRoot().toPath().resolve("foobar"); | ||
config = folder.newFile().toPath(); | ||
|
||
client = DigdagClient.builder() | ||
.host(server.host()) | ||
.port(server.port()) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void startFailsWithTooManyAttempts() | ||
throws Exception | ||
{ | ||
// Create new project | ||
CommandStatus initStatus = main("init", | ||
"-c", config.toString(), | ||
projectDir.toString()); | ||
assertThat(initStatus.code(), is(0)); | ||
|
||
copyResource("acceptance/attempt_limit/hourly_sleep.dig", projectDir.resolve("hourly_sleep.dig")); | ||
|
||
{ | ||
CommandStatus pushStatus = main("push", | ||
"--project", projectDir.toString(), | ||
"foobar", | ||
"-c", config.toString(), | ||
"-e", server.endpoint()); | ||
assertThat(pushStatus.errUtf8(), pushStatus.code(), is(0)); | ||
} | ||
|
||
// Start 3 sessions | ||
for (int i = 0; i < 3; i++) { | ||
CommandStatus startStatus = main("start", | ||
"-c", config.toString(), | ||
"-e", server.endpoint(), | ||
"foobar", "hourly_sleep", | ||
"--session", "2016-01-0" + (i + 1)); | ||
assertThat(startStatus.errUtf8(), startStatus.code(), is(0)); | ||
} | ||
|
||
// Next attempt fails with 400 Bad Request | ||
{ | ||
CommandStatus startStatus = main("start", | ||
"-c", config.toString(), | ||
"-e", server.endpoint(), | ||
"foobar", "hourly_sleep", | ||
"--session", "2016-01-04"); | ||
assertThat(startStatus.errUtf8(), startStatus.code(), is(1)); | ||
assertThat(startStatus.errUtf8(), containsString("Too many attempts running")); | ||
assertThat(startStatus.errUtf8(), containsString("\"status\":400")); | ||
} | ||
} | ||
|
||
@Test | ||
public void scheduleWithAttemptLimit() | ||
throws Exception | ||
{ | ||
// Create a new project | ||
CommandStatus initStatus = main("init", | ||
"-c", config.toString(), | ||
projectDir.toString()); | ||
assertThat(initStatus.code(), is(0)); | ||
|
||
// Push hourly schedule from now minus 10 hours | ||
Instant startTime = Instant.now().minus(Duration.ofHours(10)).truncatedTo(HOURS); | ||
|
||
copyResource("acceptance/attempt_limit/hourly_sleep.dig", projectDir.resolve("hourly_sleep.dig")); | ||
{ | ||
CommandStatus pushStatus = main("push", | ||
"--project", projectDir.toString(), | ||
"foobar", | ||
"-c", config.toString(), | ||
"-e", server.endpoint(), | ||
"--schedule-from", Long.toString(startTime.getEpochSecond())); | ||
assertThat(pushStatus.errUtf8(), pushStatus.code(), is(0)); | ||
} | ||
|
||
// Wait until next run time becomes later than now | ||
expect(Duration.ofMinutes(5), () -> { | ||
List<RestSchedule> scheds = client.getSchedules(); | ||
return scheds.size() > 0 && scheds.get(0).getNextRunTime().isAfter(Instant.now()); | ||
}); | ||
|
||
// Number of actually submitted sessions should be 3 = maxAttempts | ||
assertThat(client.getSessionAttempts(Optional.absent()).size(), is(3)); | ||
|
||
// Although next run time > now, next schedule time is 3-attempt later than start time | ||
assertThat(client.getSchedules().get(0).getNextScheduleTime().toInstant(), is(startTime.plus(Duration.ofHours(3)))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
digdag-tests/src/test/resources/acceptance/attempt_limit/hourly_sleep.dig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
timezone: UTC | ||
|
||
schedule: | ||
hourly>: 00:00 | ||
|
||
+foo: | ||
sh>: sleep 20 | ||
|