Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -43,7 +44,7 @@ public TopicConfig() {
}

public TopicConfig(final Map<String, String> attributes) {
super(attributes);
super(TopicProperty.normalizeAttributes(attributes));
}

private static final Map<String, String> REALTIME_BATCH_MODE_CONFIG =
Expand Down Expand Up @@ -132,7 +133,7 @@ public Map<String, String> getAttributesWithSourcePrefix() {
final Map<String, String> attributesWithProcessorPrefix = new HashMap<>();
attributes.forEach(
(key, value) -> {
if (key.toLowerCase().startsWith("source")) {
if (key != null && key.trim().toLowerCase(Locale.ROOT).startsWith("source")) {
attributesWithProcessorPrefix.put(key, value);
}
});
Expand All @@ -145,7 +146,7 @@ public Map<String, String> getAttributesWithProcessorPrefix() {
final Map<String, String> attributesWithProcessorPrefix = new HashMap<>();
attributes.forEach(
(key, value) -> {
if (key.toLowerCase().startsWith("processor")) {
if (key != null && key.trim().toLowerCase(Locale.ROOT).startsWith("processor")) {
attributesWithProcessorPrefix.put(key, value);
}
});
Expand All @@ -171,7 +172,7 @@ public Map<String, String> getAttributesWithSinkPrefix() {
final Map<String, String> attributesWithProcessorPrefix = new HashMap<>();
attributes.forEach(
(key, value) -> {
if (key.toLowerCase().startsWith("sink")) {
if (key != null && key.trim().toLowerCase(Locale.ROOT).startsWith("sink")) {
attributesWithProcessorPrefix.put(key, value);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iotdb.rpc.subscription.config;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

public enum TopicProperty {
PATH(TopicConstant.PATH_KEY),
PATTERN(TopicConstant.PATTERN_KEY),
START_TIME(TopicConstant.START_TIME_KEY),
END_TIME(TopicConstant.END_TIME_KEY),
LOOSE_RANGE(TopicConstant.LOOSE_RANGE_KEY),
MODE(TopicConstant.MODE_KEY),
FORMAT(TopicConstant.FORMAT_KEY);

private static final Map<String, TopicProperty> PROPERTIES_BY_KEY;

static {
final Map<String, TopicProperty> properties = new HashMap<>();
for (final TopicProperty property : values()) {
properties.put(property.key, property);
}
PROPERTIES_BY_KEY = Collections.unmodifiableMap(properties);
}

private final String key;

TopicProperty(final String key) {
this.key = key;
}

public String getKey() {
return key;
}

public static TopicProperty fromKey(final String key) {
return key == null ? null : PROPERTIES_BY_KEY.get(key.trim().toLowerCase(Locale.ROOT));
}

public static Map<String, String> normalizeAttributes(final Map<String, String> attributes) {
final Map<String, String> normalizedAttributes = new HashMap<>();
if (attributes == null) {
return normalizedAttributes;
}
attributes.forEach(
(key, value) -> {
final TopicProperty property = fromKey(key);
normalizedAttributes.put(
property == null ? key : property.key, property == null ? value : trim(value));
});
return normalizedAttributes;
}

public static void replaceNowTimeValues(
final Map<String, String> attributes, final String currentTime) {
attributes.replaceAll(
(key, value) -> {
final TopicProperty property = fromKey(key);
return value != null
&& TopicConstant.NOW_TIME_VALUE.equalsIgnoreCase(value.trim())
&& (property == START_TIME || property == END_TIME)
? currentTime
: value;
});
}

public static boolean containsAttribute(
final Map<String, String> attributes, final String expectedKey) {
return attributes.keySet().stream()
.filter(key -> key != null)
.anyMatch(key -> expectedKey.equalsIgnoreCase(key.trim()));
}

public static String getAttributeValue(
final Map<String, String> attributes, final String expectedKey, final String defaultValue) {
return attributes.entrySet().stream()
.filter(entry -> entry.getKey() != null)
.filter(entry -> expectedKey.equalsIgnoreCase(entry.getKey().trim()))
.map(Map.Entry::getValue)
.filter(value -> value != null)
.map(String::trim)
.findFirst()
.orElse(defaultValue);
}

public static String getDuplicateAttributeKey(final Map<String, String> attributes) {
if (attributes == null) {
return null;
}
final Set<String> normalizedKeys = new HashSet<>();
for (final String key : attributes.keySet()) {
final TopicProperty property = fromKey(key);
if (property != null && !normalizedKeys.add(property.key)) {
return property.key;
}
}
return null;
}

public static String canonicalizeKey(final String key) {
final TopicProperty property = fromKey(key);
return property == null ? key : property.key;
}

private static String trim(final String value) {
return value == null ? null : value.trim();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iotdb.rpc.subscription.config;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class TopicConfigTest {

@Test
public void testNormalizeBuiltinAttributesAndPreserveExtensionAttributes() {
final Map<String, String> attributes = new HashMap<>();
attributes.put(" PATH ", " Root.SG.** ");
attributes.put(" START-TIME ", " 100 ");
attributes.put(" Extension.Option ", " Mixed Value ");

final TopicConfig topicConfig = new TopicConfig(attributes);

Assert.assertEquals("Root.SG.**", topicConfig.getAttribute().get(TopicConstant.PATH_KEY));
Assert.assertEquals("100", topicConfig.getAttribute().get(TopicConstant.START_TIME_KEY));
Assert.assertEquals(" Mixed Value ", topicConfig.getAttribute().get(" Extension.Option "));
Assert.assertFalse(topicConfig.getAttribute().containsKey(" PATH "));
}

@Test
public void testNowTimeValuesUseNormalizedBuiltinKeysAndValues() {
final Map<String, String> attributes = new HashMap<>();
attributes.put(" START-TIME ", " NoW ");
attributes.put("eNd-TiMe", "nOw");
attributes.put("extension.end-time", " now ");

TopicProperty.replaceNowTimeValues(attributes, "12345");

Assert.assertEquals("12345", attributes.get(" START-TIME "));
Assert.assertEquals("12345", attributes.get("eNd-TiMe"));
Assert.assertEquals(" now ", attributes.get("extension.end-time"));
}

@Test
public void testPrefixedAttributesAcceptCaseAndWhitespace() {
final Map<String, String> attributes = new HashMap<>();
attributes.put(" Source.Custom.Option ", " Mixed Value ");
attributes.put(" Processor.Custom.Option ", " Mixed Value ");
attributes.put(" Sink.Custom.Option ", " Mixed Value ");

final TopicConfig topicConfig = new TopicConfig(attributes);

Assert.assertTrue(
topicConfig.getAttributesWithSourcePrefix().containsKey(" Source.Custom.Option "));
Assert.assertTrue(
topicConfig.getAttributesWithProcessorPrefix().containsKey(" Processor.Custom.Option "));
Assert.assertTrue(
topicConfig.getAttributesWithSinkPrefix().containsKey(" Sink.Custom.Option "));
}

@Test
public void testDuplicateAttributeDetectionUsesTrimmedCaseInsensitiveKeys() {
final Map<String, String> attributes = new HashMap<>();
attributes.put("path", "root.sg.**");
attributes.put(" PATH ", "root.other.**");

Assert.assertEquals("path", TopicProperty.getDuplicateAttributeKey(attributes));
}

@Test
public void testDuplicateAttributeDetectionPreservesExtensionKeys() {
final Map<String, String> attributes = new HashMap<>();
attributes.put("Extension.Option", "first");
attributes.put(" extension.option ", "second");

Assert.assertNull(TopicProperty.getDuplicateAttributeKey(attributes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

public final class ConfigNodeMessages {

public static final String
EXCEPTION_DUPLICATE_TOPIC_ATTRIBUTE_KEY_AFTER_TRIMMING_AND_CASE_NORMALIZATION_ARG_4A81A799 =
"Duplicate topic attribute key after trimming and case normalization: %s.";

public static final String
MESSAGE_SUBSCRIPTION_IS_DISABLED_WHEN_DATA_REGION_CONSENSUS_PROTOCOL_IS_RATIS_194CE431 =
"Subscription is disabled when data region consensus protocol is Ratis.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

public final class ConfigNodeMessages {

public static final String
EXCEPTION_DUPLICATE_TOPIC_ATTRIBUTE_KEY_AFTER_TRIMMING_AND_CASE_NORMALIZATION_ARG_4A81A799 =
"Topic 属性键去除首尾空格并忽略大小写后必须唯一:%s。";

public static final String
MESSAGE_SUBSCRIPTION_IS_DISABLED_WHEN_DATA_REGION_CONSENSUS_PROTOCOL_IS_RATIS_194CE431 =
"当数据区域共识协议为 Ratis 时,Subscription 功能不可用。";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.iotdb.confignode.manager.pipe.agent.task;

import org.apache.iotdb.commons.exception.pipe.PipeRuntimeException;
import org.apache.iotdb.commons.pipe.agent.plugin.PipePluginAgent;
import org.apache.iotdb.commons.pipe.agent.plugin.builtin.BuiltinPipePlugin;
import org.apache.iotdb.commons.pipe.agent.task.meta.PipeTaskMeta;
import org.apache.iotdb.commons.pipe.agent.task.progress.PipeEventCommitManager;
Expand Down Expand Up @@ -115,7 +116,8 @@ private void initExtractor(final Map<String, String> extractorAttributes) throws
}

private void initProcessor(final Map<String, String> processorAttributes) {
final PipeParameters processorParameters = new PipeParameters(processorAttributes);
final PipeParameters processorParameters =
PipePluginAgent.normalizeProcessorParameters(new PipeParameters(processorAttributes));

final PipeTaskRuntimeConfiguration runtimeConfiguration =
new PipeTaskRuntimeConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.iotdb.commons.consensus.index.impl.MinimumProgressIndex;
import org.apache.iotdb.commons.exception.pipe.PipeRuntimeCriticalException;
import org.apache.iotdb.commons.exception.pipe.PipeRuntimeException;
import org.apache.iotdb.commons.pipe.agent.plugin.PipePluginAgent;
import org.apache.iotdb.commons.pipe.agent.plugin.builtin.BuiltinPipePlugin;
import org.apache.iotdb.commons.pipe.agent.task.PipeTaskAgent;
import org.apache.iotdb.commons.pipe.agent.task.meta.PipeMeta;
Expand Down Expand Up @@ -255,11 +256,15 @@ private boolean checkAndUpdateRequestBeforeAlterPipeInternal(final TAlterPipeReq
alterPipeRequest.setExtractorAttributes(
copiedPipeStaticMetaFromCoordinator.getExtractorParameters().getAttribute());
} else {
final PipeParameters existingSourceParameters =
copiedPipeStaticMetaFromCoordinator.getExtractorParameters();
alterPipeRequest.setExtractorAttributes(
copiedPipeStaticMetaFromCoordinator
.getExtractorParameters()
.addOrReplaceEquivalentAttributes(
new PipeParameters(alterPipeRequest.getExtractorAttributes()))
PipePluginAgent.normalizeSourceParametersForUpdate(
new PipeParameters(alterPipeRequest.getExtractorAttributes()),
existingSourceParameters))
.getAttribute());
}
}
Expand All @@ -269,11 +274,14 @@ private boolean checkAndUpdateRequestBeforeAlterPipeInternal(final TAlterPipeReq
alterPipeRequest.setProcessorAttributes(
copiedPipeStaticMetaFromCoordinator.getProcessorParameters().getAttribute());
} else {
final PipeParameters existingProcessorParameters =
copiedPipeStaticMetaFromCoordinator.getProcessorParameters();
alterPipeRequest.setProcessorAttributes(
copiedPipeStaticMetaFromCoordinator
.getProcessorParameters()
existingProcessorParameters
.addOrReplaceEquivalentAttributes(
new PipeParameters(alterPipeRequest.getProcessorAttributes()))
PipePluginAgent.normalizeProcessorParametersForUpdate(
new PipeParameters(alterPipeRequest.getProcessorAttributes()),
existingProcessorParameters))
.getAttribute());
}
}
Expand All @@ -283,11 +291,15 @@ private boolean checkAndUpdateRequestBeforeAlterPipeInternal(final TAlterPipeReq
alterPipeRequest.setConnectorAttributes(
copiedPipeStaticMetaFromCoordinator.getConnectorParameters().getAttribute());
} else {
final PipeParameters existingSinkParameters =
copiedPipeStaticMetaFromCoordinator.getConnectorParameters();
alterPipeRequest.setConnectorAttributes(
copiedPipeStaticMetaFromCoordinator
.getConnectorParameters()
.addOrReplaceEquivalentAttributes(
new PipeParameters(alterPipeRequest.getConnectorAttributes()))
PipePluginAgent.normalizeSinkParametersForUpdate(
new PipeParameters(alterPipeRequest.getConnectorAttributes()),
existingSinkParameters))
.getAttribute());
}
}
Expand Down
Loading
Loading