Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,15 @@ void computeAndCreateTopicPartitions() throws ExecutionException, InterruptedExc
}
}

private void createNewTopics(Set<String> newSourceTopics, Map<String, Long> sourceTopicToPartitionCounts)
// visible for testing
void createNewTopics(Set<String> newSourceTopics, Map<String, Long> sourceTopicToPartitionCounts)
throws ExecutionException, InterruptedException {
Map<String, Config> sourceTopicToConfig = describeTopicConfigs(newSourceTopics);
Map<String, NewTopic> newTopics = newSourceTopics.stream()
.map(sourceTopic -> {
String remoteTopic = formatRemoteTopic(sourceTopic);
int partitionCount = sourceTopicToPartitionCounts.get(sourceTopic).intValue();
Map<String, String> configs = configToMap(sourceTopicToConfig.get(sourceTopic));
Map<String, String> configs = configToMap(targetConfig(sourceTopicToConfig.get(sourceTopic)));
return new NewTopic(remoteTopic, partitionCount, (short) replicationFactor)
.configs(configs);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
Expand Down Expand Up @@ -152,6 +155,48 @@ public void testConfigPropertyFiltering() {
.anyMatch(x -> x.name().equals("min.insync.replicas")), "should not replicate excluded properties");
}

@Test
public void testNewTopicConfigs() throws Exception {
Map<String, Object> filterConfig = new HashMap<>();
filterConfig.put(DefaultConfigPropertyFilter.CONFIG_PROPERTIES_EXCLUDE_CONFIG, "follower\\.replication\\.throttled\\.replicas, "
+ "leader\\.replication\\.throttled\\.replicas, "
+ "message\\.timestamp\\.difference\\.max\\.ms, "
+ "message\\.timestamp\\.type, "
+ "unclean\\.leader\\.election\\.enable, "
+ "min\\.insync\\.replicas,"
+ "exclude_param.*");
DefaultConfigPropertyFilter filter = new DefaultConfigPropertyFilter();
filter.configure(filterConfig);

MirrorSourceConnector connector = spy(new MirrorSourceConnector(new SourceAndTarget("source", "target"),
new DefaultReplicationPolicy(), x -> true, filter));

final String topic = "testtopic";
List<ConfigEntry> entries = new ArrayList<>();
entries.add(new ConfigEntry("name-1", "value-1"));
entries.add(new ConfigEntry("exclude_param.param1", "value-param1"));
entries.add(new ConfigEntry("min.insync.replicas", "2"));
Config config = new Config(entries);
doReturn(Collections.singletonMap(topic, config)).when(connector).describeTopicConfigs(any());
doAnswer(invocation -> {
Map<String, NewTopic> newTopics = invocation.getArgument(0);
assertNotNull(newTopics.get("source." + topic));
Map<String, String> targetConfig = newTopics.get("source." + topic).configs();

// property 'name-1' isn't defined in the exclude filter -> should be replicated
assertNotNull(targetConfig.get("name-1"), "should replicate properties");

// this property is in default list, just double check it:
String prop1 = "min.insync.replicas";
assertNull(targetConfig.get(prop1), "should not replicate excluded properties " + prop1);
// this property is only in exclude filter custom parameter, also tests regex on the way:
String prop2 = "exclude_param.param1";
assertNull(targetConfig.get(prop2), "should not replicate excluded properties " + prop2);
return null;
}).when(connector).createNewTopics(any());
connector.createNewTopics(Collections.singleton(topic), Collections.singletonMap(topic, 1L));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that calling verify on targetConfig() is not the best. But I think we should call verify on createNewTopics(any()) to ensure our mocked method, and the assertions, actually run.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. with a small correction of createNewTopics(any(), any()). since the targetConfig() is being called from overloaded method with two params.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, the code we changed is actually in createNewTopics(any(), any()). As we explicitly call that method ourselves, I don't think the verify call adds a lot of value. But I think it's fine, it shouldn't hurt


@Test
public void testMirrorSourceConnectorTaskConfig() {
List<TopicPartition> knownSourceTopicPartitions = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.kafka.common.utils.Exit;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.connect.connector.Connector;
import org.apache.kafka.connect.mirror.DefaultConfigPropertyFilter;
import org.apache.kafka.connect.mirror.MirrorClient;
import org.apache.kafka.connect.mirror.MirrorHeartbeatConnector;
import org.apache.kafka.connect.mirror.MirrorMakerConfig;
Expand Down Expand Up @@ -60,6 +61,7 @@
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -519,6 +521,47 @@ public void testOffsetSyncsTopicsOnTarget() throws Exception {
assertFalse(primaryTopics.contains("mm2-offset-syncs." + BACKUP_CLUSTER_ALIAS + ".internal"));
}

@Test

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of an integration test, what about adding a test like this to MirrorSourceConnectorTest:

@Test
public void testNewTopicConfigs() throws Exception {
    Map<String, Object> mmConfig = new HashMap<>();
    mmConfig.put(DefaultConfigPropertyFilter.CONFIG_PROPERTIES_EXCLUDE_CONFIG, "follower\\.replication\\.throttled\\.replicas, "
            + "leader\\.replication\\.throttled\\.replicas, "
            + "message\\.timestamp\\.difference\\.max\\.ms, "
            + "message\\.timestamp\\.type, "
            + "unclean\\.leader\\.election\\.enable, "
            + "min\\.insync\\.replicas,"
            + "exclude_param.*");
    DefaultConfigPropertyFilter filter = new DefaultConfigPropertyFilter();
    filter.configure(mmConfig);

    MirrorSourceConnector connector = spy(new MirrorSourceConnector(new SourceAndTarget("source", "target"),
            new DefaultReplicationPolicy(), x -> true, filter));

    String topic = "testtopic";
    List<ConfigEntry> entries = new ArrayList<>();
    entries.add(new ConfigEntry("name-1", "value-1"));
    entries.add(new ConfigEntry("exclude_param.param1", "value-param1"));
    entries.add(new ConfigEntry("min.insync.replicas", "2"));
    Config config = new Config(entries);
    doReturn(Collections.singletonMap(topic, config)).when(connector).describeTopicConfigs(any());
    doAnswer(invocation -> {
        Map<String, NewTopic> newTopics = invocation.getArgument(0);
        assertNotNull(newTopics.get("source." + topic));
        Map<String, String> targetConfig = newTopics.get("source." + topic).configs();

        // property 'name-1' isn't defined in the exclude filter -> should be replicated
        assertNotNull(targetConfig.get("name-1"), "should replicate properties");

        // this property is in default list, just double check it:
        String prop1 = "min.insync.replicas";
        assertNull(targetConfig.get(prop1), "should not replicate excluded properties " + prop1);
        // this property is only in exclude filter custom parameter, also tests regex on the way:
        String prop2 = "exclude_param.param1";
        assertNull(targetConfig.get(prop2), "should not replicate excluded properties " + prop2);
        return null;
    }).when(connector).createNewTopics(any());
    connector.createNewTopics(Collections.singleton(topic), Collections.singletonMap(topic, 1L));
    verify(connector).targetConfig(config);
}

What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I've had to check Mockito usage before.

This makes sense, really appreciate the help. Except for last line. Not sure why we have to add hard dependency on targetConfig invocation, if assertions in doAnswer provide full test coverage. I'll submit without the verify, let me know if disagree, then I'll add verify line.

Also adding a change to the connector class itself, as it needs a visibility change for this test. And still gonna keep integration test to follow the existing approach.

Fully retested with ./gradlew connect:mirror:test

public void testTopicConfigPropertyFilteringExclude() throws Exception {
// create exclude filter configuration and start MM2:
mm2Props.put(BACKUP_CLUSTER_ALIAS + "->" + PRIMARY_CLUSTER_ALIAS + ".enabled", "false");
mm2Props.put(DefaultConfigPropertyFilter.CONFIG_PROPERTIES_EXCLUDE_CONFIG, "follower\\.replication\\.throttled\\.replicas, "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep the test as simple as needed, what about simply using:

mm2Props.put(DefaultConfigPropertyFilter.CONFIG_PROPERTIES_EXCLUDE_CONFIG, "delete.retention.ms");

+ "leader\\.replication\\.throttled\\.replicas, "
+ "message\\.timestamp\\.difference\\.max\\.ms, "
+ "message\\.timestamp\\.type, "
+ "unclean\\.leader\\.election\\.enable, "
+ "min\\.insync\\.replicas,"
+ "delete\\.retention\\..*"); // this is in addition to the default exclude list

mm2Config = new MirrorMakerConfig(mm2Props);
waitUntilMirrorMakerIsRunning(backup, CONNECTOR_LIST, mm2Config, PRIMARY_CLUSTER_ALIAS, BACKUP_CLUSTER_ALIAS);

// create topic with configuration to test:
final Map<String, String> topicConfig = new HashMap<>();
topicConfig.put("delete.retention.ms", "1000"); // should be excluded (default value is 86400000)
topicConfig.put("retention.bytes", "1000"); // should be included, default value is -1


final String topic = "test-topic-with-config";
primary.kafka().createTopic(topic, NUM_PARTITIONS, 1, topicConfig);
waitForTopicCreated(backup, PRIMARY_CLUSTER_ALIAS + "." + topic);

String primaryConfig, backupConfig;

primaryConfig = getTopicConfig(primary.kafka(), topic, "delete.retention.ms");
backupConfig = getTopicConfig(backup.kafka(), PRIMARY_CLUSTER_ALIAS + "." + topic, "delete.retention.ms");
assertNotEquals(primaryConfig, backupConfig,
"`delete.retention.ms` should be different, because it's in exclude filter! ");

// regression test for the config that are still supposed to be replicated
primaryConfig = getTopicConfig(primary.kafka(), topic, "retention.bytes");
backupConfig = getTopicConfig(backup.kafka(), PRIMARY_CLUSTER_ALIAS + "." + topic, "retention.bytes");
assertEquals(primaryConfig, backupConfig,
"`retention.bytes` should be the same, because it isn't in exclude filter! ");
assertEquals(backupConfig, "1000",
"`retention.bytes` should be the same, because it's explicitly defined! ");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be the same -> should be 1000
Also let's swap the arguments as the expected value should come first:

assertEquals("1000", backupConfig, ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated per suggestions

}

/*
* launch the connectors on kafka connect cluster and check if they are running
*/
Expand Down