feat(redshift-serverless): add scheduled action actions (#5852)

Add CreateScheduledAction, GetScheduledAction, ListScheduledActions,
UpdateScheduledAction and DeleteScheduledAction. An action is recorded and
echoed, never run, and a cron schedule reports no upcoming invocations. It is
tied to the namespace by the namespace's generated id, so a namespace recreated
under the same name does not inherit it.

Required members, name patterns, the snapshot name prefix length and the tag
shape come from the AWS model through RedshiftServerlessRequestShapes. Only what
the model cannot express stays in the service: the schedule union, the time
window, the retention range and the namespaces that must exist.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-17 00:04:32 -04:00
1 parent a611e9ea6f
commit fa2a49e771
10 files changed
+739 -7

No files matched your search

@@ -9,12 +9,16 @@ import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.services.redshiftserverless.model.CustomDomainAssociation;
import io.github.hectorvent.floci.services.redshiftserverless.model.EndpointAccess;
import io.github.hectorvent.floci.services.redshiftserverless.model.ResourcePolicy;
import io.github.hectorvent.floci.services.redshiftserverless.model.ScheduledAction;
import io.github.hectorvent.floci.services.redshiftserverless.model.SnapshotCopyConfiguration;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
/**
@@ -33,18 +37,21 @@ public class RedshiftServerlessAdminJsonHandler {
private final RedshiftServerlessEndpointAccessService endpointAccess;
private final RedshiftServerlessResourcePolicyService resourcePolicies;
private final RedshiftServerlessSnapshotCopyService snapshotCopies;
private final RedshiftServerlessScheduledActionService scheduledActions;
@Inject
public RedshiftServerlessAdminJsonHandler(ObjectMapper objectMapper,
RedshiftServerlessResourcePolicyService resourcePolicies,
RedshiftServerlessEndpointAccessService endpointAccess,
RedshiftServerlessCustomDomainService customDomains,
RedshiftServerlessSnapshotCopyService snapshotCopies) {
RedshiftServerlessSnapshotCopyService snapshotCopies,
RedshiftServerlessScheduledActionService scheduledActions) {
this.objectMapper = objectMapper;
this.resourcePolicies = resourcePolicies;
this.endpointAccess = endpointAccess;
this.customDomains = customDomains;
this.snapshotCopies = snapshotCopies;
this.scheduledActions = scheduledActions;
}
Supplier<Response> operation(String action, JsonNode request, String region) {
@@ -112,6 +119,15 @@ public class RedshiftServerlessAdminJsonHandler {
case "DeleteSnapshotCopyConfiguration" -> () -> snapshotCopyResponse(
snapshotCopies.deleteSnapshotCopyConfiguration(
ServerlessSupport.text(request, "snapshotCopyConfigurationId"), region));
case "CreateScheduledAction" -> () -> scheduledActionResponse(
scheduledActions.createScheduledAction(parseScheduledAction(request), region));
case "GetScheduledAction" -> () -> scheduledActionResponse(
scheduledActions.getScheduledAction(ServerlessSupport.text(request, "scheduledActionName"), region));
case "ListScheduledActions" -> () -> listScheduledActions(request, region);
case "UpdateScheduledAction" -> () -> scheduledActionResponse(
scheduledActions.updateScheduledAction(parseScheduledAction(request), region));
case "DeleteScheduledAction" -> () -> scheduledActionResponse(
scheduledActions.deleteScheduledAction(ServerlessSupport.text(request, "scheduledActionName"), region));
default -> null;
};
}
@@ -253,6 +269,131 @@ public class RedshiftServerlessAdminJsonHandler {
}
return node;
}
// ── Scheduled action ───────────────────────────────────────────────────
private ScheduledAction parseScheduledAction(JsonNode request) {
ScheduledAction action = new ScheduledAction();
action.setScheduledActionName(ServerlessSupport.text(request, "scheduledActionName"));
action.setNamespaceName(ServerlessSupport.text(request, "namespaceName"));
action.setRoleArn(ServerlessSupport.text(request, "roleArn"));
action.setDescription(ServerlessSupport.text(request, "scheduledActionDescription"));
action.setEnabled(ServerlessSupport.bool(request, "enabled"));
action.setStartTime(ServerlessSupport.epochSeconds(request, "startTime"));
action.setEndTime(ServerlessSupport.epochSeconds(request, "endTime"));
JsonNode schedule = ServerlessSupport.object(request, "schedule");
if (schedule != null) {
action.setScheduleSpecified(true);
action.setScheduleAt(ServerlessSupport.epochSeconds(schedule, "at"));
action.setScheduleCron(ServerlessSupport.text(schedule, "cron"));
}
JsonNode target = ServerlessSupport.object(request, "targetAction");
if (target != null) {
JsonNode snapshot = ServerlessSupport.object(target, "createSnapshot");
if (snapshot == null) {
throw ServerlessSupport.validation("targetAction must set createSnapshot.");
}
action.setTargetSpecified(true);
action.setTargetNamespaceName(ServerlessSupport.text(snapshot, "namespaceName"));
action.setSnapshotNamePrefix(ServerlessSupport.text(snapshot, "snapshotNamePrefix"));
action.setRetentionPeriod(ServerlessSupport.integer(snapshot, "retentionPeriod"));
action.setSnapshotTags(parseTags(snapshot.path("tags")));
}
return action;
}
private static Map<String, String> parseTags(JsonNode tags) {
if (tags.isMissingNode() || tags.isNull()) {
return null;
}
if (!tags.isArray()) {
throw ServerlessSupport.validation("tags must be an array of tags.");
}
Map<String, String> parsed = new LinkedHashMap<>();
for (JsonNode tag : tags) {
if (!tag.isObject()) {
throw ServerlessSupport.validation("tags must contain only tag objects.");
}
if (!tag.path("key").isTextual() || !tag.path("value").isTextual()) {
throw ServerlessSupport.validation("every tag needs a string key and a string value.");
}
parsed.put(tag.path("key").textValue(), tag.path("value").textValue());
}
return parsed;
}
private Response listScheduledActions(JsonNode request, String region) {
PaginatedResult<ScheduledAction> page = scheduledActions.listScheduledActions(
ServerlessSupport.text(request, "namespaceName"), region, ServerlessSupport.maxResults(request),
ServerlessSupport.text(request, "nextToken"));
ObjectNode response = objectMapper.createObjectNode();
ArrayNode items = response.putArray("scheduledActions");
for (ScheduledAction action : page.items()) {
ObjectNode item = items.addObject();
item.put("namespaceName", action.getNamespaceName());
item.put("scheduledActionName", action.getScheduledActionName());
}
putNextToken(response, page);
return Response.ok(response).build();
}
private Response scheduledActionResponse(ScheduledAction action) {
ObjectNode response = objectMapper.createObjectNode();
response.set("scheduledAction", scheduledActionNode(action));
return Response.ok(response).build();
}
/** A one-off schedule reports its own time while it is ahead and within its active window on an enabled action; a cron schedule reports none, as Floci never computes them. */
private ObjectNode scheduledActionNode(ScheduledAction action) {
ObjectNode node = objectMapper.createObjectNode();
if (action.getEndTime() != null) {
node.put("endTime", epoch(action.getEndTime()));
}
node.put("namespaceName", action.getNamespaceName());
ArrayNode invocations = node.putArray("nextInvocations");
if (action.getScheduleAt() != null
&& !Boolean.FALSE.equals(action.getEnabled())
&& action.getScheduleAt().isAfter(Instant.now())
&& (action.getStartTime() == null || !action.getScheduleAt().isBefore(action.getStartTime()))
&& (action.getEndTime() == null || !action.getScheduleAt().isAfter(action.getEndTime()))) {
invocations.add(epoch(action.getScheduleAt()));
}
node.put("roleArn", action.getRoleArn());
ObjectNode schedule = node.putObject("schedule");
if (action.getScheduleAt() != null) {
schedule.put("at", epoch(action.getScheduleAt()));
} else {
schedule.put("cron", action.getScheduleCron());
}
if (action.getDescription() != null) {
node.put("scheduledActionDescription", action.getDescription());
}
node.put("scheduledActionName", action.getScheduledActionName());
node.put("scheduledActionUuid", action.getScheduledActionUuid());
if (action.getStartTime() != null) {
node.put("startTime", epoch(action.getStartTime()));
}
node.put("state", Boolean.FALSE.equals(action.getEnabled()) ? "DISABLED" : "ACTIVE");
ObjectNode createSnapshot = node.putObject("targetAction").putObject("createSnapshot");
createSnapshot.put("namespaceName", action.getTargetNamespaceName());
if (action.getRetentionPeriod() != null) {
createSnapshot.put("retentionPeriod", action.getRetentionPeriod());
}
createSnapshot.put("snapshotNamePrefix", action.getSnapshotNamePrefix());
if (action.getSnapshotTags() != null) {
ArrayNode tags = createSnapshot.putArray("tags");
action.getSnapshotTags().forEach((key, value) -> {
ObjectNode tag = tags.addObject();
tag.put("key", key);
tag.put("value", value);
});
}
return node;
}
private static double epoch(Instant instant) {
return instant.toEpochMilli() / 1000.0;
}
// ── Tracks, managed workgroups and reservations ────────────────────────
/**
@@ -0,0 +1,207 @@
package io.github.hectorvent.floci.services.redshiftserverless;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
import io.github.hectorvent.floci.services.redshiftserverless.model.ScheduledAction;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
/**
* Redshift Serverless scheduled actions. They are stored and echoed, not run: Floci never takes
* the snapshot on the schedule, and a cron schedule reports no upcoming invocations.
*
* <p>Required members, name patterns and lengths come from the AWS model through
* {@link RedshiftServerlessRequestShapes}; what the model cannot express (the schedule union, the time
* window, the retention range and the namespaces that must exist) is checked here.
*
* <p>An action belongs to a namespace but lives in its own store, so deleting the namespace needs
* no hook here. Each read checks that the namespace is still there and drops the action otherwise.
*/
@ApplicationScoped
public class RedshiftServerlessScheduledActionService implements Resettable {
private static final int MAX_RETENTION_DAYS = 3653;
private final AccountAwareStorageBackend<ScheduledAction> actions;
private final RedshiftServerlessService serverless;
@Inject
public RedshiftServerlessScheduledActionService(StorageFactory storageFactory,
RedshiftServerlessService serverless) {
this.actions = storageFactory.create("redshiftserverless", "redshiftserverless-scheduledactions.json",
new TypeReference<Map<String, ScheduledAction>>() {});
this.serverless = serverless;
}
/**
* Creates the action described by {@code request}. {@code enabled} defaults to true. The target
* namespace of the snapshot and the owning namespace must both exist.
*/
public synchronized ScheduledAction createScheduledAction(ScheduledAction request, String region) {
requireNamespace(request.getNamespaceName(), region);
validateSchedule(request);
validateTarget(request, region);
validateWindow(request);
if (live(region).stream().anyMatch(existing ->
existing.getScheduledActionName().equals(request.getScheduledActionName()))) {
throw ServerlessSupport.conflict("The scheduled action " + request.getScheduledActionName()
+ " already exists.");
}
ScheduledAction action = new ScheduledAction(request);
action.setOwnerId(namespaceIdOf(request.getNamespaceName(), region));
action.setScheduledActionUuid(UUID.randomUUID().toString());
action.setEnabled(request.getEnabled() == null || request.getEnabled());
actions.put(storageKey(region, action.getScheduledActionName()), action);
return new ScheduledAction(action);
}
public synchronized ScheduledAction getScheduledAction(String scheduledActionName, String region) {
return new ScheduledAction(require(scheduledActionName, region));
}
public synchronized PaginatedResult<ScheduledAction> listScheduledActions(String namespaceName, String region,
Integer maxResults, String nextToken) {
int limit = ServerlessSupport.pageSize(maxResults);
List<ScheduledAction> matching = live(region).stream()
.filter(action -> namespaceName == null || namespaceName.equals(action.getNamespaceName()))
.sorted(Comparator.comparing(ScheduledAction::getScheduledActionName))
.map(ScheduledAction::new)
.toList();
return Pagination.paginate(matching, ScheduledAction::getScheduledActionName,
limit, nextToken, "scheduled-action",
token -> ServerlessSupport.invalidPagination("Invalid nextToken."));
}
/**
* Applies the members {@code changes} carries. The schedule and the target replace the stored
* ones as a whole, because each is a union of which exactly one member may be set.
*/
public synchronized ScheduledAction updateScheduledAction(ScheduledAction changes, String region) {
ScheduledAction current = require(changes.getScheduledActionName(), region);
ScheduledAction updated = new ScheduledAction(current);
if (changes.getRoleArn() != null) {
updated.setRoleArn(changes.getRoleArn());
}
if (changes.getDescription() != null) {
updated.setDescription(changes.getDescription());
}
if (changes.getEnabled() != null) {
updated.setEnabled(changes.getEnabled());
}
if (changes.getStartTime() != null) {
updated.setStartTime(changes.getStartTime());
}
if (changes.getEndTime() != null) {
updated.setEndTime(changes.getEndTime());
}
if (changes.isScheduleSpecified()) {
validateSchedule(changes);
updated.setScheduleAt(changes.getScheduleAt());
updated.setScheduleCron(changes.getScheduleCron());
}
boolean changesTarget = changes.isTargetSpecified()
|| changes.getTargetNamespaceName() != null || changes.getSnapshotNamePrefix() != null
|| changes.getRetentionPeriod() != null || changes.getSnapshotTags() != null;
if (changesTarget) {
validateTarget(changes, region);
updated.setTargetNamespaceName(changes.getTargetNamespaceName());
updated.setSnapshotNamePrefix(changes.getSnapshotNamePrefix());
updated.setRetentionPeriod(changes.getRetentionPeriod());
updated.setSnapshotTags(changes.getSnapshotTags());
}
validateWindow(updated);
actions.put(storageKey(region, updated.getScheduledActionName()), updated);
return new ScheduledAction(updated);
}
public synchronized ScheduledAction deleteScheduledAction(String scheduledActionName, String region) {
ScheduledAction existing = new ScheduledAction(require(scheduledActionName, region));
actions.delete(storageKey(region, scheduledActionName));
return existing;
}
@Override
public void clear() {
actions.clear();
}
private ScheduledAction require(String scheduledActionName, String region) {
ScheduledAction action = actions.get(storageKey(region, scheduledActionName)).orElse(null);
if (action != null && !isOnSameNamespace(action, region)) {
actions.delete(storageKey(region, scheduledActionName));
action = null;
}
if (action == null) {
throw ServerlessSupport.notFound("The scheduled action " + scheduledActionName + " was not found.");
}
return action;
}
/** The region's actions whose namespace is still there; any other is deleted as it is found. */
private List<ScheduledAction> live(String region) {
return ServerlessSupport.live(actions, region,
action -> isOnSameNamespace(action, region),
action -> storageKey(region, action.getScheduledActionName()));
}
/** A namespace recreated under the same name has a new id, so it never inherits its predecessor's actions. */
private boolean isOnSameNamespace(ScheduledAction action, String region) {
return serverless.findNamespace(action.getNamespaceName(), region)
.map(namespace -> Objects.equals(namespace.getNamespaceId(), action.getOwnerId()))
.orElse(false);
}
private String namespaceIdOf(String namespaceName, String region) {
return serverless.findNamespace(namespaceName, region).map(Namespace::getNamespaceId).orElse(null);
}
private void requireNamespace(String namespaceName, String region) {
if (!serverless.namespaceExists(namespaceName, region)) {
throw ServerlessSupport.notFound("The namespace " + namespaceName + " was not found.");
}
}
/** Exactly one of {@code at} and {@code cron} must be set: the schedule is a union. */
private static void validateSchedule(ScheduledAction action) {
boolean hasAt = action.getScheduleAt() != null;
boolean hasCron = action.getScheduleCron() != null && !action.getScheduleCron().isBlank();
if (hasAt == hasCron) {
throw ServerlessSupport.validation("schedule must set exactly one of at and cron.");
}
}
private void validateTarget(ScheduledAction action, String region) {
if (action.getTargetNamespaceName() == null) {
throw ServerlessSupport.validation("targetAction must set createSnapshot.");
}
requireNamespace(action.getTargetNamespaceName(), region);
Integer retention = action.getRetentionPeriod();
if (retention != null && retention != -1 && (retention < 1 || retention > MAX_RETENTION_DAYS)) {
throw ServerlessSupport.validation("targetAction.createSnapshot.retentionPeriod must be -1 "
+ "(indefinite) or between 1 and " + MAX_RETENTION_DAYS + ".");
}
}
private static void validateWindow(ScheduledAction action) {
if (action.getStartTime() != null && action.getEndTime() != null
&& !action.getEndTime().isAfter(action.getStartTime())) {
throw ServerlessSupport.validation("endTime must be after startTime.");
}
}
private static String storageKey(String region, String name) {
return region + "::" + name;
}
}
@@ -475,6 +475,11 @@ public class RedshiftServerlessService implements Resettable {
return snapshotArn.substring(prefix.length());
}
/** Whether {@code namespaceName} names a namespace in {@code region}; never throws for an unknown name. */
public boolean namespaceExists(String namespaceName, String region) {
return namespaceName != null && namespaces.get(storageKey(region, namespaceName)).isPresent();
}
/** The workgroup called {@code workgroupName} in {@code region}, empty when there is none. */
public Optional<Workgroup> findWorkgroup(String workgroupName, String region) {
return workgroupName == null ? Optional.empty() : workgroups.get(storageKey(region, workgroupName));
@@ -0,0 +1,195 @@
package io.github.hectorvent.floci.services.redshiftserverless.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* A Redshift Serverless scheduled action. Floci stores and echoes it but never runs it, so no
* snapshot is taken on the schedule. The same type carries an update request, in which a null member
* means the caller left it out.
*/
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class ScheduledAction {
private String scheduledActionName;
private String scheduledActionUuid;
private String namespaceName;
private String roleArn;
private String description;
private Boolean enabled;
private Instant startTime;
private Instant endTime;
private Instant scheduleAt;
private String scheduleCron;
private String targetNamespaceName;
private String snapshotNamePrefix;
private Integer retentionPeriod;
private Map<String, String> snapshotTags;
private String ownerId;
private boolean scheduleSpecified;
private boolean targetSpecified;
public ScheduledAction() {
}
public ScheduledAction(ScheduledAction other) {
this.ownerId = other.ownerId;
this.scheduleSpecified = other.scheduleSpecified;
this.targetSpecified = other.targetSpecified;
this.scheduledActionName = other.scheduledActionName;
this.scheduledActionUuid = other.scheduledActionUuid;
this.namespaceName = other.namespaceName;
this.roleArn = other.roleArn;
this.description = other.description;
this.enabled = other.enabled;
this.startTime = other.startTime;
this.endTime = other.endTime;
this.scheduleAt = other.scheduleAt;
this.scheduleCron = other.scheduleCron;
this.targetNamespaceName = other.targetNamespaceName;
this.snapshotNamePrefix = other.snapshotNamePrefix;
this.retentionPeriod = other.retentionPeriod;
this.snapshotTags = other.snapshotTags == null ? null : new LinkedHashMap<>(other.snapshotTags);
}
public String getScheduledActionName() {
return scheduledActionName;
}
public void setScheduledActionName(String scheduledActionName) {
this.scheduledActionName = scheduledActionName;
}
public String getScheduledActionUuid() {
return scheduledActionUuid;
}
public void setScheduledActionUuid(String scheduledActionUuid) {
this.scheduledActionUuid = scheduledActionUuid;
}
public String getNamespaceName() {
return namespaceName;
}
public void setNamespaceName(String namespaceName) {
this.namespaceName = namespaceName;
}
public String getRoleArn() {
return roleArn;
}
public void setRoleArn(String roleArn) {
this.roleArn = roleArn;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public Instant getStartTime() {
return startTime;
}
public void setStartTime(Instant startTime) {
this.startTime = startTime;
}
public Instant getEndTime() {
return endTime;
}
public void setEndTime(Instant endTime) {
this.endTime = endTime;
}
public Instant getScheduleAt() {
return scheduleAt;
}
public void setScheduleAt(Instant scheduleAt) {
this.scheduleAt = scheduleAt;
}
public String getScheduleCron() {
return scheduleCron;
}
public void setScheduleCron(String scheduleCron) {
this.scheduleCron = scheduleCron;
}
public String getTargetNamespaceName() {
return targetNamespaceName;
}
public void setTargetNamespaceName(String targetNamespaceName) {
this.targetNamespaceName = targetNamespaceName;
}
public String getSnapshotNamePrefix() {
return snapshotNamePrefix;
}
public void setSnapshotNamePrefix(String snapshotNamePrefix) {
this.snapshotNamePrefix = snapshotNamePrefix;
}
public Integer getRetentionPeriod() {
return retentionPeriod;
}
public void setRetentionPeriod(Integer retentionPeriod) {
this.retentionPeriod = retentionPeriod;
}
public Map<String, String> getSnapshotTags() {
return snapshotTags;
}
public void setSnapshotTags(Map<String, String> snapshotTags) {
this.snapshotTags = snapshotTags;
}
public String getOwnerId() {
return ownerId;
}
public void setOwnerId(String ownerId) {
this.ownerId = ownerId;
}
public boolean isScheduleSpecified() {
return scheduleSpecified;
}
public void setScheduleSpecified(boolean scheduleSpecified) {
this.scheduleSpecified = scheduleSpecified;
}
public boolean isTargetSpecified() {
return targetSpecified;
}
public void setTargetSpecified(boolean targetSpecified) {
this.targetSpecified = targetSpecified;
}
}