Skip to content
Permalink
Browse files
Added validator and replaced use of booleans with enum
  • Loading branch information
nabarunnag committed Mar 3, 2020
1 parent 645afd5 commit f709b208ff2d005e8360eeb5adf521cf22c61446
Showing 6 changed files with 79 additions and 21 deletions.
@@ -86,6 +86,7 @@ protected static ConfigDef configurables() {
TASK_ID,
ConfigDef.Type.INT,
"0",
ConfigDef.Range.atLeast(0),
ConfigDef.Importance.MEDIUM,
TASK_ID_DOCUMENTATION,
GEODE_GROUP,
@@ -74,7 +74,7 @@ public void start(Map<String, String> props) {
void configure(GeodeSinkConnectorConfig geodeConnectorConfig) {
logger.debug("GeodeKafkaSourceTask id:" + geodeConnectorConfig.getTaskId() + " starting");
topicToRegions = geodeConnectorConfig.getTopicToRegions();
nullValuesMeansRemove = geodeConnectorConfig.getNullValuesMeanRemove();
nullValuesMeansRemove = geodeConnectorConfig.getNullValueBehavior();
}

// For tests only
@@ -14,11 +14,11 @@
*/
package org.apache.geode.kafka.sink;

import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.DEFAULT_NULL_VALUES_MEAN_REMOVE;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.DEFAULT_NULL_VALUES_BEHAVIOR;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.DEFAULT_TOPIC_TO_REGION_BINDING;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.NULL_VALUES_MEAN_REMOVE;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.NULL_VALUES_MEAN_REMOVE_DISPLAY_NAME;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.NULL_VALUES_MEAN_REMOVE_DOCUMENTATION;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.NULL_VALUES_BEHAVIOR;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.NULL_VALUES_BEHAVIOR_DISPLAY_NAME;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.NULL_VALUES_BEHAVIOR_DOCUMENTATION;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.SINK_GROUP;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.TOPIC_TO_REGION_BINDINGS;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.TOPIC_TO_REGION_BINDINGS_DISPLAY_NAME;
@@ -30,16 +30,19 @@
import org.apache.kafka.common.config.ConfigDef;

import org.apache.geode.kafka.GeodeConnectorConfig;
import org.apache.geode.kafka.utils.EnumValidator;
import org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants;

public class GeodeSinkConnectorConfig extends GeodeConnectorConfig {
public static final ConfigDef SINK_CONFIG_DEF = configurables();
private final Map<String, List<String>> topicToRegions;
private final boolean nullValuesMeanRemove;
private final GeodeSinkConfigurationConstants.NullValueBehavior nullValueBehavior;

public GeodeSinkConnectorConfig(Map<String, String> connectorProperties) {
super(SINK_CONFIG_DEF, connectorProperties);
topicToRegions = parseTopicToRegions(getString(TOPIC_TO_REGION_BINDINGS));
nullValuesMeanRemove = getBoolean(NULL_VALUES_MEAN_REMOVE);
nullValueBehavior = GeodeSinkConfigurationConstants.NullValueBehavior
.valueOf(getString(NULL_VALUES_BEHAVIOR).toUpperCase());
}

protected static ConfigDef configurables() {
@@ -56,24 +59,29 @@ protected static ConfigDef configurables() {
TOPIC_TO_REGION_BINDINGS_DISPLAY_NAME);

configDef.define(
NULL_VALUES_MEAN_REMOVE,
ConfigDef.Type.BOOLEAN,
DEFAULT_NULL_VALUES_MEAN_REMOVE,
NULL_VALUES_BEHAVIOR,
ConfigDef.Type.STRING,
DEFAULT_NULL_VALUES_BEHAVIOR,
EnumValidator.in(GeodeSinkConfigurationConstants.NullValueBehavior.values()),
ConfigDef.Importance.MEDIUM,
NULL_VALUES_MEAN_REMOVE_DOCUMENTATION,
NULL_VALUES_BEHAVIOR_DOCUMENTATION,
SINK_GROUP,
2,
ConfigDef.Width.MEDIUM,
NULL_VALUES_MEAN_REMOVE_DISPLAY_NAME);
NULL_VALUES_BEHAVIOR_DISPLAY_NAME);
return configDef;
}

public Map<String, List<String>> getTopicToRegions() {
return topicToRegions;
}

public boolean getNullValuesMeanRemove() {
return nullValuesMeanRemove;
public boolean getNullValueBehavior() {
if (nullValueBehavior.equals(GeodeSinkConfigurationConstants.NullValueBehavior.REMOVE)) {
return true;
} else {
return false;
}
}

}
@@ -0,0 +1,45 @@
/*
* 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.geode.kafka.utils;

import java.util.HashSet;
import java.util.Set;

import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigException;

public class EnumValidator implements ConfigDef.Validator {
private final Set<String> validValues;

public EnumValidator(Set<String> validValues) {
this.validValues = validValues;
}

public static <T> EnumValidator in(T[] enumerators) {
Set<String> validValues = new HashSet<String>(enumerators.length);
for (T e : enumerators) {
validValues.add(e.toString().toLowerCase());
}
return new EnumValidator(validValues);
}

@Override
public void ensureValid(String key, Object value) {
if (!validValues.contains(value.toString().toLowerCase())) {
throw new ConfigException(key, value, "Invalid enumeration value");
}

}
}
@@ -20,14 +20,18 @@ public class GeodeSinkConfigurationConstants {
*/
public static final String TOPIC_TO_REGION_BINDINGS = "topic-to-regions";
public static final String DEFAULT_TOPIC_TO_REGION_BINDING = "[gkcTopic:gkcRegion]";
public static final String NULL_VALUES_MEAN_REMOVE = "null-values-mean-remove";
public static final String DEFAULT_NULL_VALUES_MEAN_REMOVE = "true";
public static final String NULL_VALUES_MEAN_REMOVE_DOCUMENTATION =
"If set to true, when topics send a SinkRecord with a null value, we will convert to an operation similar to region.remove instead of putting a null value into the region";
public static final String NULL_VALUES_BEHAVIOR = "null-values-behavior";
public static final String DEFAULT_NULL_VALUES_BEHAVIOR = "remove";
public static final String NULL_VALUES_BEHAVIOR_DOCUMENTATION =
"If set to REMOVE, when topics send a SinkRecord with a null value, we will convert to an operation similar to region.remove instead of putting a null value into the region";
public static final String TOPIC_TO_REGION_BINDINGS_DOCUMENTATION =
"A comma separated list of \"one topic to many regions\" bindings. Each binding is surrounded by brackets. For example \"[topicName:regionName], [anotherTopic: regionName, anotherRegion]";
public static final String SINK_GROUP = "Sink-Configurations";
public final static String TOPIC_TO_REGION_BINDINGS_DISPLAY_NAME = "Topic to region mapping";
public final static String NULL_VALUES_MEAN_REMOVE_DISPLAY_NAME = "Null values behavior";
public final static String NULL_VALUES_BEHAVIOR_DISPLAY_NAME = "Null values behavior";

public enum NullValueBehavior {
REMOVE,
ADD
}
}
@@ -16,7 +16,7 @@

import static org.apache.geode.kafka.utils.GeodeConfigurationConstants.LOCATORS;
import static org.apache.geode.kafka.utils.GeodeConfigurationConstants.TASK_ID;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.NULL_VALUES_MEAN_REMOVE;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.NULL_VALUES_BEHAVIOR;
import static org.apache.geode.kafka.utils.GeodeSinkConfigurationConstants.TOPIC_TO_REGION_BINDINGS;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -40,7 +40,7 @@ private HashMap<String, String> createTestSinkProps() {
HashMap<String, String> props = new HashMap<>();
props.put(TOPIC_TO_REGION_BINDINGS, "[topic:region]");
props.put(TASK_ID, "0");
props.put(NULL_VALUES_MEAN_REMOVE, "true");
props.put(NULL_VALUES_BEHAVIOR, "REMOVE");
props.put(LOCATORS, "localhost[10334]");
return props;
}

0 comments on commit f709b20

Please sign in to comment.