Skip to content

Commit

Permalink
add system rules support
Browse files Browse the repository at this point in the history
  • Loading branch information
sanket-mundra committed Mar 12, 2024
1 parent 59747da commit bb31d19
Showing 1 changed file with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package org.hypertrace.label.application.rule.config.service;

import com.google.protobuf.util.JsonFormat;
import com.typesafe.config.Config;
import io.grpc.Channel;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
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 @@ -31,10 +39,13 @@ public class LabelApplicationRuleConfigServiceImpl
"label.application.rule.config.service";
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 =
"label.application.rule.config.service.system.label.application.rules";
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 Map<String, LabelApplicationRule> systemLabelApplicationRuleIdToRuleMap;

public LabelApplicationRuleConfigServiceImpl(
Channel configChannel, Config config, ConfigChangeEventGenerator configChangeEventGenerator) {
Expand All @@ -56,6 +67,20 @@ public LabelApplicationRuleConfigServiceImpl(
this.labelApplicationRuleStore =
new LabelApplicationRuleStore(configServiceBlockingStub, configChangeEventGenerator);
this.requestValidator = new LabelApplicationRuleValidatorImpl();

buildSystemLabelApplicationRuleConfigs(config);
}

private void buildSystemLabelApplicationRuleConfigs(Config config) {
if (!config.hasPath(SYSTEM_LABEL_APPLICATION_RULES)) {
systemLabelApplicationRuleIdToRuleMap = Collections.emptyMap();
return;
}

List<? extends com.typesafe.config.ConfigObject> systemLabelApplicationRuleObjects =
config.getObjectList(SYSTEM_LABEL_APPLICATION_RULES);
systemLabelApplicationRuleIdToRuleMap =
buildSystemLabelApplicationRuleIdRuleMap(systemLabelApplicationRuleObjects);

Check warning on line 83 in label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java#L80-L83

Added lines #L80 - L83 were not covered by tests
}

@Override
Expand Down Expand Up @@ -92,10 +117,16 @@ public void getLabelApplicationRules(
try {
RequestContext requestContext = RequestContext.CURRENT.get();
this.requestValidator.validateOrThrow(requestContext, request);
List<LabelApplicationRule> labelApplicationRules =
List<LabelApplicationRule> userLabelApplicationRules =
this.labelApplicationRuleStore.getAllObjects(requestContext).stream()
.map(ConfigObject::getData)
.collect(Collectors.toUnmodifiableList());

// reorder user label application rules and system label application rules as per expected
// priority
List<LabelApplicationRule> labelApplicationRules =
reorderLabelApplicationRules(userLabelApplicationRules);

responseObserver.onNext(
GetLabelApplicationRulesResponse.newBuilder()
.addAllLabelApplicationRules(labelApplicationRules)
Expand Down Expand Up @@ -140,6 +171,12 @@ public void deleteLabelApplicationRule(
try {
RequestContext requestContext = RequestContext.CURRENT.get();
this.requestValidator.validateOrThrow(requestContext, request);

// do not allow deleting system label application rules
if (systemLabelApplicationRuleIdToRuleMap.containsKey(request.getId())) {
throw Status.INVALID_ARGUMENT.asRuntimeException();

Check warning on line 177 in label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java#L177

Added line #L177 was not covered by tests
}

this.labelApplicationRuleStore
.deleteObject(requestContext, request.getId())
.orElseThrow(Status.NOT_FOUND::asRuntimeException);
Expand All @@ -166,4 +203,56 @@ private void checkRequestForDynamicLabelsLimit(
}
}
}

private Map<String, LabelApplicationRule> buildSystemLabelApplicationRuleIdRuleMap(
List<? extends com.typesafe.config.ConfigObject> configObjects) {
return configObjects.stream()
.map(this::buildLabelApplicationRuleFromConfig)
.collect(Collectors.toUnmodifiableMap(LabelApplicationRule::getId, Function.identity()));

Check warning on line 211 in label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java#L209-L211

Added lines #L209 - L211 were not covered by tests
}

@SneakyThrows

Check warning on line 214 in label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java#L214

Added line #L214 was not covered by tests
private LabelApplicationRule buildLabelApplicationRuleFromConfig(
com.typesafe.config.ConfigObject configObject) {
String jsonString = configObject.render();
LabelApplicationRule.Builder builder = LabelApplicationRule.newBuilder();
JsonFormat.parser().merge(jsonString, builder);
return builder.build();

Check warning on line 220 in label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java#L217-L220

Added lines #L217 - L220 were not covered by tests
}

private List<LabelApplicationRule> reorderLabelApplicationRules(
List<LabelApplicationRule> userLabelApplicationRules) {
// This method sets the priority of rules. User label application rules -> user overridden
// system
// label application rules -> non overridden system label application rules
List<LabelApplicationRule> labelApplicationRules = new ArrayList<>();
List<LabelApplicationRule> overriddenSystemLabelApplicationRules = new ArrayList<>();
Set<String> overriddenSystemLabelApplicationRuleIds = new HashSet<>();

for (LabelApplicationRule userLabelApplicationRule : userLabelApplicationRules) {
String id = userLabelApplicationRule.getId();
if (systemLabelApplicationRuleIdToRuleMap.containsKey(id)) {
// user overridden system label application rules
overriddenSystemLabelApplicationRules.add(userLabelApplicationRule);
overriddenSystemLabelApplicationRuleIds.add(id);

Check warning on line 237 in label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java#L236-L237

Added lines #L236 - L237 were not covered by tests
} else {
// user label application rules
labelApplicationRules.add(userLabelApplicationRule);
}
}

// non overridden system label application rules
List<LabelApplicationRule> nonOverriddenSystemLabelApplicationRules =
systemLabelApplicationRuleIdToRuleMap.entrySet().stream()
.filter(
labelApplicationRuleEntry ->
!overriddenSystemLabelApplicationRuleIds.contains(
labelApplicationRuleEntry.getKey()))

Check warning on line 250 in label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

label-application-rule-config-service-impl/src/main/java/org/hypertrace/label/application/rule/config/service/LabelApplicationRuleConfigServiceImpl.java#L250

Added line #L250 was not covered by tests
.map(Map.Entry::getValue)
.collect(Collectors.toUnmodifiableList());

labelApplicationRules.addAll(overriddenSystemLabelApplicationRules);
labelApplicationRules.addAll(nonOverriddenSystemLabelApplicationRules);
return Collections.unmodifiableList(labelApplicationRules);
}
}

0 comments on commit bb31d19

Please sign in to comment.