Skip to content

Commit

Permalink
address review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
saxenakshitiz committed Aug 30, 2024
1 parent d6d3c34 commit 740ffd7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.hypertrace.label.application.rule.config.service;

import static java.util.function.Function.identity;

import com.google.protobuf.util.JsonFormat;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigObject;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.SneakyThrows;
import org.hypertrace.label.application.rule.config.service.v1.LabelApplicationRule;

public class LabelApplicationRuleConfig {
private static final String LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG =
"label.application.rule.config.service";
private static final String MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT =
"max.dynamic.label.application.rules.per.tenant";
private static final String SYSTEM_LABEL_APPLICATION_RULES = "system.label.application.rules";
private static final int DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT = 100;

@Getter private final int maxDynamicLabelApplicationRulesAllowed;
@Getter private final List<LabelApplicationRule> systemLabelApplicationRules;
@Getter private final Map<String, LabelApplicationRule> systemLabelApplicationRulesMap;

public LabelApplicationRuleConfig(Config config) {
Config labelApplicationRuleConfig =
config.hasPath(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG)
? config.getConfig(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG)
: ConfigFactory.empty();
this.maxDynamicLabelApplicationRulesAllowed =
labelApplicationRuleConfig.hasPath(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT)
? labelApplicationRuleConfig.getInt(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT)
: DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT;
if (labelApplicationRuleConfig.hasPath(SYSTEM_LABEL_APPLICATION_RULES)) {
final List<? extends ConfigObject> systemLabelApplicationRulesObjectList =
labelApplicationRuleConfig.getObjectList(SYSTEM_LABEL_APPLICATION_RULES);
this.systemLabelApplicationRules =
buildSystemLabelApplicationRuleList(systemLabelApplicationRulesObjectList);
this.systemLabelApplicationRulesMap =
this.systemLabelApplicationRules.stream()
.collect(Collectors.toUnmodifiableMap(LabelApplicationRule::getId, identity()));
} else {
this.systemLabelApplicationRules = Collections.emptyList();
this.systemLabelApplicationRulesMap = Collections.emptyMap();
}
}

private List<LabelApplicationRule> buildSystemLabelApplicationRuleList(
List<? extends com.typesafe.config.ConfigObject> configObjectList) {
return configObjectList.stream()
.map(LabelApplicationRuleConfig::buildLabelApplicationRuleFromConfig)
.collect(Collectors.toUnmodifiableList());
}

@SneakyThrows
private static LabelApplicationRule buildLabelApplicationRuleFromConfig(
com.typesafe.config.ConfigObject configObject) {
String jsonString = configObject.render();
LabelApplicationRule.Builder builder = LabelApplicationRule.newBuilder();
JsonFormat.parser().merge(jsonString, builder);
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package org.hypertrace.label.application.rule.config.service;

import static java.util.function.Function.identity;

import com.google.protobuf.util.JsonFormat;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import io.grpc.Channel;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import org.hypertrace.config.objectstore.ConfigObject;
import org.hypertrace.config.objectstore.IdentifiedObjectStore;
import org.hypertrace.config.service.change.event.api.ConfigChangeEventGenerator;
Expand All @@ -37,41 +30,14 @@

public class LabelApplicationRuleConfigServiceImpl
extends LabelApplicationRuleConfigServiceGrpc.LabelApplicationRuleConfigServiceImplBase {
private static final String LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG =
"label.application.rule.config.service";
private static final String MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT =
"max.dynamic.label.application.rules.per.tenant";
private static final String SYSTEM_LABEL_APPLICATION_RULES = "system.label.application.rules";
private static final int DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT = 100;

private final IdentifiedObjectStore<LabelApplicationRule> labelApplicationRuleStore;
private final LabelApplicationRuleValidator requestValidator;
private final int maxDynamicLabelApplicationRulesAllowed;
private final List<LabelApplicationRule> systemLabelApplicationRules;
private final Map<String, LabelApplicationRule> systemLabelApplicationRulesMap;
private final LabelApplicationRuleConfig labelApplicationRuleConfig;

public LabelApplicationRuleConfigServiceImpl(
Channel configChannel, Config config, ConfigChangeEventGenerator configChangeEventGenerator) {
Config labelApplicationRuleConfig =
config.hasPath(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG)
? config.getConfig(LABEL_APPLICATION_RULE_CONFIG_SERVICE_CONFIG)
: ConfigFactory.empty();
this.maxDynamicLabelApplicationRulesAllowed =
labelApplicationRuleConfig.hasPath(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT)
? labelApplicationRuleConfig.getInt(MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT)
: DEFAULT_MAX_DYNAMIC_LABEL_APPLICATION_RULES_PER_TENANT;
if (labelApplicationRuleConfig.hasPath(SYSTEM_LABEL_APPLICATION_RULES)) {
final List<? extends com.typesafe.config.ConfigObject> systemLabelApplicationRulesObjectList =
labelApplicationRuleConfig.getObjectList(SYSTEM_LABEL_APPLICATION_RULES);
this.systemLabelApplicationRules =
buildSystemLabelApplicationRuleList(systemLabelApplicationRulesObjectList);
this.systemLabelApplicationRulesMap =
this.systemLabelApplicationRules.stream()
.collect(Collectors.toUnmodifiableMap(LabelApplicationRule::getId, identity()));
} else {
this.systemLabelApplicationRules = Collections.emptyList();
this.systemLabelApplicationRulesMap = Collections.emptyMap();
}
this.labelApplicationRuleConfig = new LabelApplicationRuleConfig(config);

final ConfigServiceBlockingStub configServiceBlockingStub =
ConfigServiceGrpc.newBlockingStub(configChannel)
.withCallCredentials(
Expand Down Expand Up @@ -124,7 +90,7 @@ public void getLabelApplicationRules(
.map(LabelApplicationRule::getId)
.collect(Collectors.toUnmodifiableSet());
List<LabelApplicationRule> filteredSystemLabelApplicationRules =
systemLabelApplicationRules.stream()
this.labelApplicationRuleConfig.getSystemLabelApplicationRules().stream()
.filter(rule -> !labelApplicationRuleIds.contains(rule.getId()))
.collect(Collectors.toUnmodifiableList());
responseObserver.onNext(
Expand All @@ -148,7 +114,12 @@ public void updateLabelApplicationRule(
LabelApplicationRule existingRule =
this.labelApplicationRuleStore
.getData(requestContext, request.getId())
.or(() -> Optional.ofNullable(systemLabelApplicationRulesMap.get(request.getId())))
.or(
() ->
Optional.ofNullable(
this.labelApplicationRuleConfig
.getSystemLabelApplicationRulesMap()
.get(request.getId())))
.orElseThrow(Status.NOT_FOUND::asRuntimeException);
LabelApplicationRule updateLabelApplicationRule =
existingRule.toBuilder().setData(request.getData()).build();
Expand All @@ -174,7 +145,9 @@ public void deleteLabelApplicationRule(
RequestContext requestContext = RequestContext.CURRENT.get();
this.requestValidator.validateOrThrow(requestContext, request);
String labelApplicationRuleId = request.getId();
if (systemLabelApplicationRulesMap.containsKey(labelApplicationRuleId)) {
if (this.labelApplicationRuleConfig
.getSystemLabelApplicationRulesMap()
.containsKey(labelApplicationRuleId)) {
// Deleting a system label application rule is not allowed
responseObserver.onError(new StatusRuntimeException(Status.INVALID_ARGUMENT));
return;
Expand All @@ -200,25 +173,10 @@ private void checkRequestForDynamicLabelsLimit(
.filter(
action -> action.hasDynamicLabelExpression() || action.hasDynamicLabelKey())
.count();
if (dynamicLabelApplicationRules >= maxDynamicLabelApplicationRulesAllowed) {
if (dynamicLabelApplicationRules
>= this.labelApplicationRuleConfig.getMaxDynamicLabelApplicationRulesAllowed()) {
throw Status.RESOURCE_EXHAUSTED.asRuntimeException();
}
}
}

private List<LabelApplicationRule> buildSystemLabelApplicationRuleList(
List<? extends com.typesafe.config.ConfigObject> configObjectList) {
return configObjectList.stream()
.map(LabelApplicationRuleConfigServiceImpl::buildLabelApplicationRuleFromConfig)
.collect(Collectors.toUnmodifiableList());
}

@SneakyThrows
private static LabelApplicationRule buildLabelApplicationRuleFromConfig(
com.typesafe.config.ConfigObject configObject) {
String jsonString = configObject.render();
LabelApplicationRule.Builder builder = LabelApplicationRule.newBuilder();
JsonFormat.parser().merge(jsonString, builder);
return builder.build();
}
}

0 comments on commit 740ffd7

Please sign in to comment.