mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(aps): honor workspace idempotency tokens (#6552)
This commit is contained in:
1 parent
d4aee01679
commit
2bab3b5cbe
8 files changed
+505
-9
No files matched your search
@@ -60,10 +60,10 @@ public class ApsController {
|
||||
String alias = asString(body.get("alias"), "alias");
|
||||
String kmsKeyArn = asString(body.get("kmsKeyArn"), "kmsKeyArn");
|
||||
Map<String, String> tags = asStringMap(body.get("tags"), "tags");
|
||||
// clientToken is accepted and ignored: creates are not deduplicated.
|
||||
String clientToken = asString(body.get("clientToken"), "clientToken");
|
||||
|
||||
PrometheusWorkspace workspace =
|
||||
service.createWorkspace(regionResolver.resolveRegion(headers), alias, tags, kmsKeyArn);
|
||||
service.createWorkspace(regionResolver.resolveRegion(headers), alias, tags, kmsKeyArn, clientToken);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("workspaceId", workspace.getWorkspaceId());
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.jboss.logging.Logger;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -31,6 +32,7 @@ public class ApsService implements TagHandler {
|
||||
private static final int MAX_PAGE = 1000;
|
||||
private static final int MAX_NAMESPACE_NAME_LENGTH = 128;
|
||||
private static final Pattern NAMESPACE_NAME = Pattern.compile(".*[0-9A-Za-z][-.0-9A-Z_a-z]*.*");
|
||||
private static final Pattern CLIENT_TOKEN = Pattern.compile("[!-~]{1,64}");
|
||||
|
||||
private final StorageBackend<String, PrometheusWorkspace> storage;
|
||||
private final StorageBackend<String, RuleGroupsNamespace> namespaceStorage;
|
||||
@@ -47,6 +49,29 @@ public class ApsService implements TagHandler {
|
||||
|
||||
public PrometheusWorkspace createWorkspace(String region, String alias, Map<String, String> tags,
|
||||
String kmsKeyArn) {
|
||||
return createWorkspace(region, alias, tags, kmsKeyArn, null);
|
||||
}
|
||||
|
||||
// ponytail: serialize token claims; use per-token locks if workspace-create throughput matters.
|
||||
public synchronized PrometheusWorkspace createWorkspace(String region, String alias, Map<String, String> tags,
|
||||
String kmsKeyArn, String clientToken) {
|
||||
if (clientToken != null && !CLIENT_TOKEN.matcher(clientToken).matches()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"clientToken must contain 1 to 64 printable ASCII characters without spaces.", 400);
|
||||
}
|
||||
Map<String, String> requestedTags = tags != null ? tags : Map.of();
|
||||
if (clientToken != null) {
|
||||
PrometheusWorkspace receipt = storage.get(workspaceRequestKey(region, clientToken)).orElse(null);
|
||||
if (receipt != null) {
|
||||
if (!Objects.equals(stripAlias(alias), receipt.getAlias())
|
||||
|| !Objects.equals(kmsKeyArn, receipt.getKmsKeyArn())
|
||||
|| !requestedTags.equals(receipt.getTags())) {
|
||||
throw new AwsException("ConflictException",
|
||||
"clientToken has already been used with different workspace parameters.", 409);
|
||||
}
|
||||
return copyWorkspace(receipt);
|
||||
}
|
||||
}
|
||||
String workspaceId = "ws-" + UUID.randomUUID();
|
||||
String arn = regionResolver.buildArn("aps", region, "workspace/" + workspaceId);
|
||||
|
||||
@@ -66,9 +91,29 @@ public class ApsService implements TagHandler {
|
||||
workspace.getTags().putAll(tags);
|
||||
}
|
||||
|
||||
storage.put(key(region, workspaceId), workspace);
|
||||
PrometheusWorkspace response = clientToken != null ? copyWorkspace(workspace) : workspace;
|
||||
if (clientToken != null) {
|
||||
// One commit keeps the receipt and resource together in snapshots and WAL replay.
|
||||
storage.putAllAtomically(Map.of(key(region, workspaceId), workspace,
|
||||
workspaceRequestKey(region, clientToken), response));
|
||||
} else {
|
||||
storage.put(key(region, workspaceId), workspace);
|
||||
}
|
||||
LOG.infov("Created AMP workspace: {0} in {1}", workspaceId, region);
|
||||
return workspace;
|
||||
return clientToken != null ? copyWorkspace(response) : workspace;
|
||||
}
|
||||
|
||||
private static PrometheusWorkspace copyWorkspace(PrometheusWorkspace source) {
|
||||
PrometheusWorkspace copy = new PrometheusWorkspace();
|
||||
copy.setWorkspaceId(source.getWorkspaceId());
|
||||
copy.setAlias(source.getAlias());
|
||||
copy.setArn(source.getArn());
|
||||
copy.setStatus(source.getStatus());
|
||||
copy.setPrometheusEndpoint(source.getPrometheusEndpoint());
|
||||
copy.setCreatedAt(source.getCreatedAt());
|
||||
copy.setKmsKeyArn(source.getKmsKeyArn());
|
||||
copy.setTags(source.getTags());
|
||||
return copy;
|
||||
}
|
||||
|
||||
public PrometheusWorkspace describeWorkspace(String region, String workspaceId) {
|
||||
@@ -263,6 +308,11 @@ public class ApsService implements TagHandler {
|
||||
return region + "::";
|
||||
}
|
||||
|
||||
private static String workspaceRequestKey(String region, String clientToken) {
|
||||
// Request regions cannot contain ':', keeping receipts outside every workspace prefix.
|
||||
return ":CreateWorkspace::" + region + "::" + clientToken;
|
||||
}
|
||||
|
||||
private static String namespaceKey(String region, String workspaceId, String name) {
|
||||
return namespaceKeyPrefix(region, workspaceId) + name;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user