Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removing empty configs on delete #179

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.hypertrace.config.service;

import static org.hypertrace.config.service.ConfigServiceUtils.emptyValue;
import static org.hypertrace.config.service.ConfigServiceUtils.filterNull;
import static org.hypertrace.config.service.ConfigServiceUtils.merge;

Expand Down Expand Up @@ -134,7 +133,7 @@ public void deleteConfig(
}

// write an empty config for the specified config resource. This maintains the versioning.
configStore.writeConfig(configResourceContext, getUserId(), emptyValue());
configStore.deleteConfigs(List.of(configResourceContext));
responseObserver.onNext(
DeleteConfigResponse.newBuilder().setDeletedConfig(configToDelete).build());
responseObserver.onCompleted();
Expand All @@ -155,22 +154,19 @@ public void deleteConfigs(
.asException());
return;
}
Map<ConfigResourceContext, Value> valuesByContext =

List<ConfigResourceContext> valuesToDelete =
request.getConfigsList().stream()
.map(
requestedDelete ->
Map.entry(this.getConfigResourceContext(requestedDelete), emptyValue()))
.collect(ImmutableMap.toImmutableMap(Entry::getKey, Entry::getValue));
.map(this::getConfigResourceContext)
.collect(Collectors.toUnmodifiableList());

List<UpsertedConfig> deletedConfigs =
configStore.writeAllConfigs(valuesByContext, getUserId());
Map<ConfigResourceContext, ContextSpecificConfig> configsToDelete =
configStore.getContextConfigs(valuesToDelete);
configStore.deleteConfigs(configsToDelete.keySet());

responseObserver.onNext(
DeleteConfigsResponse.newBuilder()
.addAllDeletedConfigs(
deletedConfigs.stream()
.map(this::buildDeletedContextSpecificConfig)
.collect(Collectors.toUnmodifiableList()))
.addAllDeletedConfigs(configsToDelete.values())
.build());
responseObserver.onCompleted();
} catch (Exception e) {
Expand All @@ -179,15 +175,6 @@ public void deleteConfigs(
}
}

private ContextSpecificConfig buildDeletedContextSpecificConfig(UpsertedConfig deletedConfig) {
return ContextSpecificConfig.newBuilder()
.setContext(deletedConfig.getContext())
.setCreationTimestamp(deletedConfig.getCreationTimestamp())
.setUpdateTimestamp(deletedConfig.getUpdateTimestamp())
.setConfig(deletedConfig.getPrevConfig())
.build();
}

@Override
public void upsertAllConfigs(
UpsertAllConfigsRequest request, StreamObserver<UpsertAllConfigsResponse> responseObserver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.protobuf.Value;
import com.typesafe.config.Config;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.hypertrace.config.service.ConfigResource;
Expand Down Expand Up @@ -34,6 +35,8 @@ public interface ConfigStore {
UpsertedConfig writeConfig(
ConfigResourceContext configResourceContext, String userId, Value config) throws IOException;

void deleteConfigs(Collection<ConfigResourceContext> resourceContextCollection);

/**
* Get the config with the latest version for the specified resource.
*
Expand All @@ -52,6 +55,9 @@ UpsertedConfig writeConfig(
*/
List<ContextSpecificConfig> getAllConfigs(ConfigResource configResource) throws IOException;

Map<ConfigResourceContext, ContextSpecificConfig> getContextConfigs(
Collection<ConfigResourceContext> configResourceContexts) throws IOException;

/**
* Write each of the provided config value associated with the specified config resource to the
* store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.hypertrace.config.service.store.ConfigDocument.TENANT_ID_FIELD_NAME;
import static org.hypertrace.config.service.store.ConfigDocument.VERSION_FIELD_NAME;

import com.google.common.collect.Maps;
import com.google.protobuf.Value;
import com.typesafe.config.Config;
import io.grpc.Status;
Expand All @@ -20,6 +21,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -90,6 +92,22 @@ public UpsertedConfig writeConfig(
.orElseGet(() -> this.buildUpsertResult(latestConfigDocument));
}

@Override
public void deleteConfigs(java.util.Collection<ConfigResourceContext> resourceContextCollection) {
collection.delete(
resourceContextCollection.stream().map(ConfigDocumentKey::new).collect(Collectors.toSet()));
}

@Override
public Map<ConfigResourceContext, ContextSpecificConfig> getContextConfigs(
java.util.Collection<ConfigResourceContext> configResourceContexts) throws IOException {
return Maps.filterValues(
Maps.transformValues(
getLatestVersionConfigDocs(configResourceContexts),
doc -> doc.flatMap(this::convertToContextSpecificConfig).orElse(null)),
Objects::nonNull);
}

private List<UpsertedConfig> writeConfigs(
Map<ConfigResourceContext, Value> resourceContextValueMap, String userId) throws IOException {
Map<ConfigResourceContext, Optional<ConfigDocument>> previousConfigDocs =
Expand Down Expand Up @@ -212,7 +230,7 @@ private Optional<ConfigDocument> getLatestVersionConfigDoc(
}

private Map<ConfigResourceContext, Optional<ConfigDocument>> getLatestVersionConfigDocs(
Set<ConfigResourceContext> configResourceContexts) throws IOException {
java.util.Collection<ConfigResourceContext> configResourceContexts) throws IOException {
if (configResourceContexts.isEmpty()) {
return Collections.emptyMap();
}
Expand Down
Loading