Skip to content

Commit

Permalink
allow configuration of default value for remoteDatacenterFallbackEnab…
Browse files Browse the repository at this point in the history
…led (#1875)

* allow configuration of default value for remoteDatacenterFallbackEnabled

* remove redundant test
  • Loading branch information
moscicky authored Jul 2, 2024
1 parent 2673b98 commit 1e634b4
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 19 deletions.
12 changes: 8 additions & 4 deletions hermes-api/src/main/java/pl/allegro/tech/hermes/api/Topic.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class Topic {
private boolean jsonToAvroDryRunEnabled = false;
@NotNull
private Ack ack;
private boolean fallbackToRemoteDatacenterEnabled;
public static final String DEFAULT_FALLBACK_TO_REMOTE_DATACENTER_KEY = "defaultFallbackToRemoteDatacenterEnabled";
private final boolean fallbackToRemoteDatacenterEnabled;
private PublishingChaosPolicy chaos;
@NotNull
private ContentType contentType;
Expand All @@ -58,8 +59,10 @@ public class Topic {
private Instant modifiedAt;

public Topic(TopicName name, String description, OwnerId owner, RetentionTime retentionTime,
boolean migratedFromJsonType, Ack ack, boolean fallbackToRemoteDatacenterEnabled, PublishingChaosPolicy chaos,
boolean trackingEnabled, ContentType contentType, boolean jsonToAvroDryRunEnabled,
boolean migratedFromJsonType, Ack ack,
@JacksonInject(value = DEFAULT_FALLBACK_TO_REMOTE_DATACENTER_KEY, useInput = OptBoolean.TRUE)
Boolean fallbackToRemoteDatacenterEnabled,
PublishingChaosPolicy chaos, boolean trackingEnabled, ContentType contentType, boolean jsonToAvroDryRunEnabled,
@JacksonInject(value = DEFAULT_SCHEMA_ID_SERIALIZATION_ENABLED_KEY, useInput = OptBoolean.TRUE)
Boolean schemaIdAwareSerializationEnabled,
int maxMessageSize, PublishingAuth publishingAuth, boolean subscribingRestricted,
Expand Down Expand Up @@ -93,7 +96,8 @@ public Topic(
@JsonProperty("retentionTime") RetentionTime retentionTime,
@JsonProperty("jsonToAvroDryRun") boolean jsonToAvroDryRunEnabled,
@JsonProperty("ack") Ack ack,
@JsonProperty("fallbackToRemoteDatacenterEnabled") boolean fallbackToRemoteDatacenterEnabled,
@JacksonInject(value = DEFAULT_FALLBACK_TO_REMOTE_DATACENTER_KEY, useInput = OptBoolean.TRUE)
@JsonProperty("fallbackToRemoteDatacenterEnabled") Boolean fallbackToRemoteDatacenterEnabled,
@JsonProperty("chaos") PublishingChaosPolicy chaos,
@JsonProperty("trackingEnabled") boolean trackingEnabled,
@JsonProperty("migratedFromJsonType") boolean migratedFromJsonType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public TopicWithSchema(@JsonProperty("schema") String schema,
@JsonProperty("retentionTime") RetentionTime retentionTime,
@JsonProperty("jsonToAvroDryRun") boolean jsonToAvroDryRunEnabled,
@JsonProperty("ack") Ack ack,
@JsonProperty("fallbackToRemoteDatacenterEnabled") boolean fallbackToRemoteDatacenterEnabled,
@JsonProperty("fallbackToRemoteDatacenterEnabled")
@JacksonInject(value = DEFAULT_FALLBACK_TO_REMOTE_DATACENTER_KEY, useInput = OptBoolean.TRUE)
boolean fallbackToRemoteDatacenterEnabled,
@JsonProperty("chaos") PublishingChaosPolicy chaos,
@JsonProperty("trackingEnabled") boolean trackingEnabled,
@JsonProperty("migratedFromJsonType") boolean migratedFromJsonType,
Expand Down
37 changes: 34 additions & 3 deletions hermes-api/src/test/java/pl/allegro/tech/hermes/api/TopicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class TopicTest {

private final ObjectMapper objectMapper = createObjectMapper();
private final ObjectMapper objectMapper = createObjectMapper(false);

@Test
public void shouldDeserializeTopicWithDefaults() throws Exception {
Expand Down Expand Up @@ -65,11 +65,42 @@ public void shouldSkippedDeserializedOldSchemaVersionId() throws Exception {
assertThat(topic.getName().getName()).isEqualTo("bar");
}

private ObjectMapper createObjectMapper() {
@Test
public void shouldDeserializeFallbackToRemoteDatacenterWithDefaults() throws Exception {
// given
String json = "{\"name\":\"foo.bar\", \"description\": \"description\"}";

// when
Topic topic = objectMapper.readValue(json, Topic.class);

// then
assertThat(topic.isFallbackToRemoteDatacenterEnabled()).isEqualTo(false);

// and when
Topic topic2 = createObjectMapper(true).readValue(json, Topic.class);

// then
assertThat(topic2.isFallbackToRemoteDatacenterEnabled()).isEqualTo(true);
}

@Test
public void shouldDeserializeFallbackToRemoteDatacenter() throws Exception {
// given
String json = "{\"name\":\"foo.bar\", \"description\": \"description\", \"fallbackToRemoteDatacenterEnabled\": true}";

// when
Topic topic = objectMapper.readValue(json, Topic.class);

// then
assertThat(topic.isFallbackToRemoteDatacenterEnabled()).isEqualTo(true);
}

private ObjectMapper createObjectMapper(boolean fallbackToRemoteDatacenterEnabled) {
ObjectMapper mapper = new ObjectMapper();

final InjectableValues defaultSchemaIdAwareSerializationEnabled = new InjectableValues
.Std().addValue(Topic.DEFAULT_SCHEMA_ID_SERIALIZATION_ENABLED_KEY, true);
.Std().addValue(Topic.DEFAULT_SCHEMA_ID_SERIALIZATION_ENABLED_KEY, true)
.addValue(Topic.DEFAULT_FALLBACK_TO_REMOTE_DATACENTER_KEY, fallbackToRemoteDatacenterEnabled);

mapper.setInjectableValues(defaultSchemaIdAwareSerializationEnabled);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
public class ObjectMapperFactory {

private final boolean schemaIdSerializationEnabled;
private final boolean fallbackToRemoteDatacenterEnabled;

public ObjectMapperFactory(boolean schemaIdSerializationEnabled) {
public ObjectMapperFactory(boolean schemaIdSerializationEnabled, boolean fallbackToRemoteDatacenterEnabled) {
this.schemaIdSerializationEnabled = schemaIdSerializationEnabled;
this.fallbackToRemoteDatacenterEnabled = fallbackToRemoteDatacenterEnabled;
}

public ObjectMapper provide() {
Expand All @@ -23,8 +25,9 @@ public ObjectMapper provide() {
objectMapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
objectMapper.registerModule(new JavaTimeModule());

final InjectableValues defaultSchemaIdAwareSerializationEnabled = new InjectableValues
.Std().addValue(Topic.DEFAULT_SCHEMA_ID_SERIALIZATION_ENABLED_KEY, schemaIdSerializationEnabled);
final InjectableValues defaultSchemaIdAwareSerializationEnabled = new InjectableValues.Std()
.addValue(Topic.DEFAULT_SCHEMA_ID_SERIALIZATION_ENABLED_KEY, schemaIdSerializationEnabled)
.addValue(Topic.DEFAULT_FALLBACK_TO_REMOTE_DATACENTER_KEY, fallbackToRemoteDatacenterEnabled);
objectMapper.setInjectableValues(defaultSchemaIdAwareSerializationEnabled);

return objectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class IntegrationTest extends Specification {

protected RepositoryWaiter wait = new RepositoryWaiter(zookeeperResource.curator(), paths)

protected ObjectMapper mapper = new ObjectMapperFactory(true).provide()
protected ObjectMapper mapper = new ObjectMapperFactory(true, false).provide()

protected ZookeeperGroupRepository groupRepository = new ZookeeperGroupRepository(zookeeper(), mapper, paths)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ObjectMapperFactoryTest {

@Before
public void init() {
ObjectMapperFactory factory = new ObjectMapperFactory(false);
ObjectMapperFactory factory = new ObjectMapperFactory(false, false);
mapper = factory.provide();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ public ZookeeperAdminCache zookeeperAdminCache(ZookeeperPaths zookeeperPaths,

@Bean
public ObjectMapper objectMapper(SchemaProperties schemaProperties) {
return new ObjectMapperFactory(schemaProperties.isIdSerializationEnabled()).provide();
return new ObjectMapperFactory(
schemaProperties.isIdSerializationEnabled(),
/* fallbackToRemoteDatacenter is frontend specific property, we so don't expose consumer side property for it */
false
).provide();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
ZookeeperClustersProperties.class,
KafkaClustersProperties.class,
ContentRootProperties.class,
DatacenterNameProperties.class
DatacenterNameProperties.class,
TopicDefaultsProperties.class
})
public class CommonConfiguration {

Expand Down Expand Up @@ -183,8 +184,8 @@ public ZookeeperAdminCache zookeeperAdminCache(ZookeeperPaths zookeeperPaths,
}

@Bean
public ObjectMapper objectMapper(SchemaProperties schemaProperties) {
return new ObjectMapperFactory(schemaProperties.isIdSerializationEnabled()).provide();
public ObjectMapper objectMapper(SchemaProperties schemaProperties, TopicDefaultsProperties topicDefaults) {
return new ObjectMapperFactory(schemaProperties.isIdSerializationEnabled(), topicDefaults.isFallbackToRemoteDatacenterEnabled()).provide();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pl.allegro.tech.hermes.frontend.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "frontend.topic.defaults")
public class TopicDefaultsProperties {
private boolean fallbackToRemoteDatacenterEnabled = false;

public boolean isFallbackToRemoteDatacenterEnabled() {
return fallbackToRemoteDatacenterEnabled;
}

public void setFallbackToRemoteDatacenterEnabled(boolean fallbackToRemoteDatacenterEnabled) {
this.fallbackToRemoteDatacenterEnabled = fallbackToRemoteDatacenterEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public ObjectMapper objectMapper() {

final InjectableValues defaultSchemaIdAwareSerializationEnabled = new InjectableValues.Std().addValue(
Topic.DEFAULT_SCHEMA_ID_SERIALIZATION_ENABLED_KEY,
topicProperties.isDefaultSchemaIdAwareSerializationEnabled());
topicProperties.isDefaultSchemaIdAwareSerializationEnabled())
.addValue(Topic.DEFAULT_FALLBACK_TO_REMOTE_DATACENTER_KEY, topicProperties.isDefaultFallbackToRemoteDatacenterEnabled());

mapper.setInjectableValues(defaultSchemaIdAwareSerializationEnabled);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class TopicProperties {

private boolean defaultSchemaIdAwareSerializationEnabled = false;

private boolean defaultFallbackToRemoteDatacenterEnabled = false;

private boolean avroContentTypeMetadataRequired = true;

/**
Expand Down Expand Up @@ -155,6 +157,14 @@ public void setDefaultSchemaIdAwareSerializationEnabled(boolean defaultSchemaIdA
this.defaultSchemaIdAwareSerializationEnabled = defaultSchemaIdAwareSerializationEnabled;
}

public boolean isDefaultFallbackToRemoteDatacenterEnabled() {
return defaultFallbackToRemoteDatacenterEnabled;
}

public void setDefaultFallbackToRemoteDatacenterEnabled(boolean defaultFallbackToRemoteDatacenterEnabled) {
this.defaultFallbackToRemoteDatacenterEnabled = defaultFallbackToRemoteDatacenterEnabled;
}

public boolean isAvroContentTypeMetadataRequired() {
return avroContentTypeMetadataRequired;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pl.allegro.tech.hermes.api.ContentType;
import pl.allegro.tech.hermes.api.MessageTextPreview;
import pl.allegro.tech.hermes.api.OwnerId;
import pl.allegro.tech.hermes.api.PatchData;
Expand Down

0 comments on commit 1e634b4

Please sign in to comment.