Skip to content
Merged
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
10 changes: 9 additions & 1 deletion src/main/java/ai/chalk/client/ChalkClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ public UploadFeaturesResult uploadFeatures(UploadFeaturesParams params) throws C

@Override
public DeleteFeaturesResult deleteFeatures(DeleteFeaturesParams params) throws ChalkException {
if (Boolean.FALSE.equals(params.getRetainMaterializedAggregates())
&& (params.getFeatures() != null || params.getTags() != null)) {
throw new ClientException(
"retainMaterializedAggregates=false deletes every materialized aggregation of the primary keys "
+ "and cannot be combined with features or tags."
);
}
String branch = params.getBranch() != null ? params.getBranch() : this.branch;
if (branch != null) {
throw new ClientException(
Expand All @@ -284,7 +291,8 @@ public DeleteFeaturesResult deleteFeatures(DeleteFeaturesParams params) throws C
params.getTags(),
params.getPrimaryKeys(),
params.isRetainOffline(),
params.isRetainOnline()
params.isRetainOnline(),
params.getRetainMaterializedAggregates()
))
.method("DELETE")
.environmentOverride(params.getEnvironmentId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public record FeatureObservationDeletionRequest(
List<String> tags,
List<String> primaryKeys,
boolean retainOffline,
boolean retainOnline
boolean retainOnline,
Boolean retainMaterializedAggregates
) {
}
44 changes: 43 additions & 1 deletion src/main/java/ai/chalk/models/DeleteFeaturesParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ public class DeleteFeaturesParams {
* If true, the given observations will not be dropped from the online store.
*/
private final boolean retainOnline;
/**
* If false, also delete the keys' materialized aggregation buckets (not with features or tags).
* Null leaves the default to the API server, which keeps the buckets.
*/
@Nullable
private final Boolean retainMaterializedAggregates;

/** Equivalent to the full constructor with {@code retainMaterializedAggregates = null}. */
public DeleteFeaturesParams(
String namespace,
@Nullable List<String> features,
@Nullable List<String> tags,
List<String> primaryKeys,
@Nullable String environmentId,
@Nullable String branch,
boolean retainOffline,
boolean retainOnline
) {
this(namespace, features, tags, primaryKeys, environmentId, branch, retainOffline, retainOnline, null);
}

@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -66,6 +86,21 @@ public static class Builder {
protected String branch;
protected boolean retainOffline = false;
protected boolean retainOnline = false;
protected Boolean retainMaterializedAggregates = null;

/** Equivalent to the full constructor with {@code retainMaterializedAggregates = null}. */
public Builder(
String namespace,
List<String> features,
List<String> tags,
List<String> primaryKeys,
String environmentId,
String branch,
boolean retainOffline,
boolean retainOnline
) {
this(namespace, features, tags, primaryKeys, environmentId, branch, retainOffline, retainOnline, null);
}

public DeleteFeaturesParams build() {
return new DeleteFeaturesParams(
Expand All @@ -76,7 +111,8 @@ public DeleteFeaturesParams build() {
this.environmentId,
this.branch,
this.retainOffline,
this.retainOnline);
this.retainOnline,
this.retainMaterializedAggregates);
}

public Builder withNamespace(String namespace) {
Expand Down Expand Up @@ -120,6 +156,12 @@ public Builder withRetainOnline(boolean retainOnline) {
this.retainOnline = retainOnline;
return this;
}

/** Pass false to also delete the keys' materialized aggregation buckets. Defaults to {@code true}. */
public Builder withRetainMaterializedAggregates(boolean retainMaterializedAggregates) {
this.retainMaterializedAggregates = retainMaterializedAggregates;
return this;
}
}

public static Builder builder() {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ai/chalk/models/DeleteFeaturesResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.annotation.Nullable;
import java.util.List;

/**
Expand All @@ -17,4 +18,11 @@
@AllArgsConstructor
public class DeleteFeaturesResult {
private List<ServerError> errors;
/** Online store entries removed when {@code retainMaterializedAggregates} is false, else null. */
@Nullable
private Long materializedAggregateKeysDeleted;

public DeleteFeaturesResult(List<ServerError> errors) {
this(errors, null);
}
}
87 changes: 86 additions & 1 deletion src/test/java/ai/chalk/client/TestDeleteFeatures.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -66,13 +67,97 @@ public void testHttpDeleteFeatures() throws Exception {
assertEquals("DELETE", method.get());
assertEquals(
"{\"namespace\":\"user\",\"features\":[\"name\",\"email\"],\"tags\":null,"
+ "\"primary_keys\":[\"1\",\"2\"],\"retain_offline\":false,\"retain_online\":false}",
+ "\"primary_keys\":[\"1\",\"2\"],\"retain_offline\":false,\"retain_online\":false,"
+ "\"retain_materialized_aggregates\":null}",
body.get()
);
assertEquals("Bearer test-token", authorization.get());
assertEquals(ENVIRONMENT_ID, environmentHeader.get());
assertEquals(1, result.getErrors().size());
assertEquals("partial failure", result.getErrors().get(0).getMessage());
assertNull(result.getMaterializedAggregateKeysDeleted());
} finally {
server.stop(0);
}
}

@Test
public void testHttpDeleteFeaturesDeletingMaterializedAggregates() throws Exception {
AtomicReference<String> body = new AtomicReference<>();
HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
server.createContext("/v1/oauth/token", exchange -> respondJson(
exchange,
"""
{
"access_token": "test-token",
"expires_in": 3600,
"primary_environment": "%s",
"engines": {}
}
""".formatted(ENVIRONMENT_ID)
));
server.createContext("/v1/features/rows", exchange -> {
body.set(new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8));
respondJson(exchange, "{\"errors\": [], \"materialized_aggregate_keys_deleted\": 7}");
});
server.start();

String serverUrl = "http://localhost:" + server.getAddress().getPort();
try (ChalkClient client = ChalkClient.builder()
.withApiServer(serverUrl)
.withClientId("test-client")
.withClientSecret("test-secret")
.withEnvironmentId(ENVIRONMENT_ID)
.build()) {
DeleteFeaturesResult result = client.deleteFeatures(DeleteFeaturesParams.builder()
.withNamespace("user")
.withPrimaryKeys(List.of("1"))
.withRetainMaterializedAggregates(false)
.build());

assertEquals(
"{\"namespace\":\"user\",\"features\":null,\"tags\":null,\"primary_keys\":[\"1\"],"
+ "\"retain_offline\":false,\"retain_online\":false,"
+ "\"retain_materialized_aggregates\":false}",
body.get()
);
assertTrue(result.getErrors().isEmpty());
assertEquals(7L, result.getMaterializedAggregateKeysDeleted());
} finally {
server.stop(0);
}
}

@Test
public void testNotRetainingMaterializedAggregatesRejectsFeatureSelection() throws Exception {
AtomicReference<Boolean> deleteCalled = new AtomicReference<>(false);
HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 0), 0);
server.createContext("/v1/features/rows", exchange -> {
deleteCalled.set(true);
respondJson(exchange, "{\"errors\": []}");
});
server.start();

String serverUrl = "http://localhost:" + server.getAddress().getPort();
try (ChalkClient client = ChalkClient.builder()
.withApiServer(serverUrl)
.withClientId("test-client")
.withClientSecret("test-secret")
.withEnvironmentId(ENVIRONMENT_ID)
.build()) {
for (DeleteFeaturesParams.Builder builder : List.of(
DeleteFeaturesParams.builder().withFeatures(List.of("name")),
DeleteFeaturesParams.builder().withTags(List.of("pii"))
)) {
ClientException e = assertThrows(ClientException.class, () -> client.deleteFeatures(
builder.withNamespace("user")
.withPrimaryKeys(List.of("1"))
.withRetainMaterializedAggregates(false)
.build()
));
assertTrue(e.getMessage().contains("cannot be combined"));
}
assertFalse(deleteCalled.get());
} finally {
server.stop(0);
}
Expand Down
Loading