|
| 1 | +package org.hypertrace.core.kafkastreams.framework.partitioner; |
| 2 | + |
| 3 | +import com.google.common.base.Splitter; |
| 4 | +import com.google.common.collect.Maps; |
| 5 | +import com.google.common.util.concurrent.AtomicDouble; |
| 6 | +import com.typesafe.config.Config; |
| 7 | +import com.typesafe.config.ConfigFactory; |
| 8 | +import lombok.Value; |
| 9 | + |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Map.Entry; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.function.Function; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +@Value |
| 19 | +class AvroFieldValuePartitionerConfig { |
| 20 | + private static final Splitter SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults(); |
| 21 | + |
| 22 | + static final String PARTITIONER_CONFIG_PREFIX = "avro.field.value.partitioner"; |
| 23 | + |
| 24 | + static final String TOPICS_CONFIG_PREFIX = "topics"; |
| 25 | + static final String FIELD_NAME = "field.name"; |
| 26 | + static final String EXCLUDED_PARTITIONS = "excluded.partitions"; |
| 27 | + static final String DEFAULT_GROUP_WEIGHT = "default.group.weight"; |
| 28 | + |
| 29 | + static final String GROUPS_CONFIG_PREFIX = "groups"; |
| 30 | + static final String GROUP_MEMBERS = "members"; |
| 31 | + static final String GROUP_WEIGHT = "weight"; |
| 32 | + |
| 33 | + Map<String, String> fieldNameByTopic; |
| 34 | + Map<String, Set<Integer>> excludedPartitionsByTopic; |
| 35 | + PartitionGroupConfig defaultGroupConfig; |
| 36 | + Map<String, PartitionGroupConfig> groupConfigByMember; |
| 37 | + |
| 38 | + @Value |
| 39 | + static class PartitionGroupConfig { |
| 40 | + // This represents some range between 0-1 (e.g. 0.6-0.8) that specifies this group's share |
| 41 | + double normalizedFractionalStart; |
| 42 | + double normalizedFractionalEnd; |
| 43 | + } |
| 44 | + |
| 45 | + public AvroFieldValuePartitionerConfig(Map<String, ?> streamConfigMap) { |
| 46 | + final Map<String, String> fieldNameByTopic = Maps.newHashMap(); |
| 47 | + final Map<String, Set<Integer>> excludedPartitionsByTopic = Maps.newHashMap(); |
| 48 | + |
| 49 | + Map<String, Object> partitionerConfigProps = |
| 50 | + streamConfigMap.entrySet().stream() |
| 51 | + .filter(entry -> entry.getKey().startsWith(PARTITIONER_CONFIG_PREFIX)) |
| 52 | + .collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue)); |
| 53 | + Config partitionerConfig = |
| 54 | + ConfigFactory.parseMap(partitionerConfigProps).getConfig(PARTITIONER_CONFIG_PREFIX); |
| 55 | + |
| 56 | + Config topicsConfig = partitionerConfig.getConfig(TOPICS_CONFIG_PREFIX); |
| 57 | + topicsConfig |
| 58 | + .root() |
| 59 | + .keySet() |
| 60 | + .forEach( |
| 61 | + topic -> { |
| 62 | + Config topicConfig = topicsConfig.getConfig(topic); |
| 63 | + fieldNameByTopic.put(topic, topicConfig.getString(FIELD_NAME)); |
| 64 | + String excludedPartitionsStr = |
| 65 | + topicConfig.hasPath(EXCLUDED_PARTITIONS) |
| 66 | + ? topicConfig.getString(EXCLUDED_PARTITIONS) |
| 67 | + : ""; |
| 68 | + Set<Integer> excludedPartitions = |
| 69 | + SPLITTER |
| 70 | + .splitToStream(excludedPartitionsStr) |
| 71 | + .map(Integer::valueOf) |
| 72 | + .collect(Collectors.toSet()); |
| 73 | + excludedPartitionsByTopic.put(topic, excludedPartitions); |
| 74 | + }); |
| 75 | + |
| 76 | + this.fieldNameByTopic = Map.copyOf(fieldNameByTopic); |
| 77 | + this.excludedPartitionsByTopic = Map.copyOf(excludedPartitionsByTopic); |
| 78 | + double defaultWeight = partitionerConfig.getDouble(DEFAULT_GROUP_WEIGHT); |
| 79 | + |
| 80 | + Config groupsConfig = |
| 81 | + partitionerConfig.hasPath(GROUPS_CONFIG_PREFIX) |
| 82 | + ? partitionerConfig.getConfig(GROUPS_CONFIG_PREFIX) |
| 83 | + : ConfigFactory.empty(); |
| 84 | + |
| 85 | + // Sort groups by name for consistent ordering |
| 86 | + List<Config> groupConfigs = |
| 87 | + groupsConfig.root().keySet().stream() |
| 88 | + .sorted() |
| 89 | + .map(groupsConfig::getConfig) |
| 90 | + .collect(Collectors.toUnmodifiableList()); |
| 91 | + |
| 92 | + double totalWeight = |
| 93 | + defaultWeight |
| 94 | + + groupConfigs.stream() |
| 95 | + .map(groupConfig -> groupConfig.getDouble(GROUP_WEIGHT)) |
| 96 | + .reduce(0d, Double::sum); |
| 97 | + AtomicDouble weightConsumedSoFar = new AtomicDouble(); |
| 98 | + this.defaultGroupConfig = |
| 99 | + new PartitionGroupConfig( |
| 100 | + weightConsumedSoFar.get(), weightConsumedSoFar.addAndGet(defaultWeight / totalWeight)); |
| 101 | + |
| 102 | + this.groupConfigByMember = |
| 103 | + groupConfigs.stream() |
| 104 | + .map( |
| 105 | + groupConfig -> |
| 106 | + buildKeyValueMapForConfig( |
| 107 | + groupConfig, |
| 108 | + new PartitionGroupConfig( |
| 109 | + weightConsumedSoFar.get(), |
| 110 | + weightConsumedSoFar.addAndGet( |
| 111 | + groupConfig.getDouble(GROUP_WEIGHT) / totalWeight)))) |
| 112 | + .map(Map::entrySet) |
| 113 | + .flatMap(Collection::stream) |
| 114 | + .collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue)); |
| 115 | + } |
| 116 | + |
| 117 | + private static Map<String, PartitionGroupConfig> buildKeyValueMapForConfig( |
| 118 | + Config groupConfig, PartitionGroupConfig partitionGroupConfig) { |
| 119 | + return SPLITTER.splitToList(groupConfig.getString(GROUP_MEMBERS)).stream() |
| 120 | + .collect(Collectors.toUnmodifiableMap(Function.identity(), unused -> partitionGroupConfig)); |
| 121 | + } |
| 122 | +} |
0 commit comments