Skip to content

Commit

Permalink
add event time to configChangeEventValue (#74)
Browse files Browse the repository at this point in the history
* add event time to configChangeEventValue

* make event time optional

* revert making event time field optional
  • Loading branch information
SrikarMannepalli authored Oct 20, 2021
1 parent 32a08ce commit 91b1b34
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ message ConfigChangeEventValue {
}
optional string user_id = 4;
optional string user_name = 5;
int64 event_time_millis = 6;
}

message ConfigCreateEvent {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.hypertrace.config.service.change.event.impl;

import com.typesafe.config.Config;
import java.time.Clock;
import org.hypertrace.config.service.change.event.api.ConfigChangeEventGenerator;

public class ConfigChangeEventGeneratorFactory {
Expand All @@ -17,9 +18,10 @@ public static ConfigChangeEventGeneratorFactory getInstance() {
return instance;
}

public ConfigChangeEventGenerator createConfigChangeEventGenerator(Config appConfig) {
public ConfigChangeEventGenerator createConfigChangeEventGenerator(
Config appConfig, Clock clock) {
if (appConfig.getBoolean(GENERIC_CONFIG_SERVICE_PUBLISH_CHANGE_EVENTS)) {
return new ConfigChangeEventGeneratorImpl(appConfig);
return new ConfigChangeEventGeneratorImpl(appConfig, clock);
} else {
return new NoopConfigChangeEventGenerator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.Value;
import com.typesafe.config.Config;
import java.time.Clock;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.hypertrace.config.change.event.v1.ConfigChangeEventKey;
Expand Down Expand Up @@ -31,9 +32,11 @@ public class ConfigChangeEventGeneratorImpl implements ConfigChangeEventGenerato

private final EventProducer<ConfigChangeEventKey, ConfigChangeEventValue>
configChangeEventProducer;
private Clock clock;

ConfigChangeEventGeneratorImpl(Config appConfig) {
ConfigChangeEventGeneratorImpl(Config appConfig, Clock clock) {
Config config = appConfig.getConfig(EVENT_STORE);
this.clock = clock;
String storeType = config.getString(EVENT_STORE_TYPE_CONFIG);
EventStore eventStore = EventStoreProvider.getEventStore(storeType, config);
configChangeEventProducer =
Expand All @@ -45,7 +48,9 @@ public class ConfigChangeEventGeneratorImpl implements ConfigChangeEventGenerato

@VisibleForTesting
ConfigChangeEventGeneratorImpl(
EventProducer<ConfigChangeEventKey, ConfigChangeEventValue> configChangeEventProducer) {
EventProducer<ConfigChangeEventKey, ConfigChangeEventValue> configChangeEventProducer,
Clock clock) {
this.clock = clock;
this.configChangeEventProducer = configChangeEventProducer;
}

Expand Down Expand Up @@ -103,6 +108,7 @@ private void produceCreateNotification(
ConfigCreateEvent.newBuilder()
.setCreatedConfigJson(ConfigProtoConverter.convertToJsonString(config))
.build());
builder.setEventTimeMillis(clock.millis());
populateUserDetails(requestContext, builder);
configChangeEventProducer.send(
KeyUtil.getKey(tenantId, configType, contextOptional), builder.build());
Expand Down Expand Up @@ -130,6 +136,7 @@ private void produceUpdateNotification(
.setPreviousConfigJson(ConfigProtoConverter.convertToJsonString(prevConfig))
.setLatestConfigJson(ConfigProtoConverter.convertToJsonString(latestConfig))
.build());
builder.setEventTimeMillis(clock.millis());
populateUserDetails(requestContext, builder);
configChangeEventProducer.send(
KeyUtil.getKey(tenantId, configType, contextOptional), builder.build());
Expand All @@ -155,6 +162,7 @@ private void produceDeleteNotification(
ConfigDeleteEvent.newBuilder()
.setDeletedConfigJson(ConfigProtoConverter.convertToJsonString(config))
.build());
builder.setEventTimeMillis(clock.millis());
populateUserDetails(requestContext, builder);
configChangeEventProducer.send(
KeyUtil.getKey(tenantId, configType, contextOptional), builder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.time.Clock;
import java.util.Map;
import org.hypertrace.config.service.change.event.api.ConfigChangeEventGenerator;
import org.junit.jupiter.api.Test;
Expand All @@ -16,15 +17,17 @@ void createNoopConfigChangeEventGenerator() {
Config config =
ConfigFactory.parseMap(Map.of(GENERIC_CONFIG_SERVICE_PUBLISH_CHANGE_EVENTS, "false"));
ConfigChangeEventGenerator configChangeEventGenerator =
ConfigChangeEventGeneratorFactory.getInstance().createConfigChangeEventGenerator(config);
ConfigChangeEventGeneratorFactory.getInstance()
.createConfigChangeEventGenerator(config, Clock.systemUTC());
assertTrue(configChangeEventGenerator instanceof NoopConfigChangeEventGenerator);
}

@Test
void createConfigChangeEventGeneratorImpl() {
Config config = getEventStoreConfig();
ConfigChangeEventGenerator configChangeEventGenerator =
ConfigChangeEventGeneratorFactory.getInstance().createConfigChangeEventGenerator(config);
ConfigChangeEventGeneratorFactory.getInstance()
.createConfigChangeEventGenerator(config, Clock.systemUTC());
assertTrue(configChangeEventGenerator instanceof ConfigChangeEventGeneratorImpl);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.hypertrace.config.service.change.event.impl;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Value;
import java.time.Clock;
import java.util.Optional;
import org.hypertrace.config.change.event.v1.ConfigChangeEventKey;
import org.hypertrace.config.change.event.v1.ConfigChangeEventValue;
Expand All @@ -29,15 +32,19 @@ class ConfigChangeEventGeneratorImplTest {
private static final String TEST_CONTEXT = "test-context";
private static final String TEST_VALUE = "test-value";
private static final String TEST_NEW_VALUE = "test-new-value";
private static final long CURRENT_TIME_MILLIS = 1000;

@Mock EventProducer<ConfigChangeEventKey, ConfigChangeEventValue> eventProducer;

ConfigChangeEventGeneratorImpl changeEventGenerator;
RequestContext requestContext;
private Clock mockClock;

@BeforeEach
void setup() {
changeEventGenerator = new ConfigChangeEventGeneratorImpl(eventProducer);
mockClock = mock(Clock.class);
when(mockClock.millis()).thenReturn(CURRENT_TIME_MILLIS);
changeEventGenerator = new ConfigChangeEventGeneratorImpl(eventProducer, mockClock);
requestContext = RequestContext.forTenantId(TEST_TENANT_ID_1);
}

Expand All @@ -50,6 +57,7 @@ void sendCreateNotification() throws InvalidProtocolBufferException {
.send(
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.of(TEST_CONTEXT)),
ConfigChangeEventValue.newBuilder()
.setEventTimeMillis(CURRENT_TIME_MILLIS)
.setCreateEvent(
ConfigCreateEvent.newBuilder()
.setCreatedConfigJson(ConfigProtoConverter.convertToJsonString(config))
Expand All @@ -65,6 +73,7 @@ void sendCreateNotificationWithNoContext() throws InvalidProtocolBufferException
.send(
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.empty()),
ConfigChangeEventValue.newBuilder()
.setEventTimeMillis(CURRENT_TIME_MILLIS)
.setCreateEvent(
ConfigCreateEvent.newBuilder()
.setCreatedConfigJson(ConfigProtoConverter.convertToJsonString(config))
Expand All @@ -81,6 +90,7 @@ void sendDeleteNotification() throws InvalidProtocolBufferException {
.send(
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.of(TEST_CONTEXT)),
ConfigChangeEventValue.newBuilder()
.setEventTimeMillis(CURRENT_TIME_MILLIS)
.setDeleteEvent(
ConfigDeleteEvent.newBuilder()
.setDeletedConfigJson(ConfigProtoConverter.convertToJsonString(config))
Expand All @@ -96,6 +106,7 @@ void sendDeleteNotificationWithNoContext() throws InvalidProtocolBufferException
.send(
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.empty()),
ConfigChangeEventValue.newBuilder()
.setEventTimeMillis(CURRENT_TIME_MILLIS)
.setDeleteEvent(
ConfigDeleteEvent.newBuilder()
.setDeletedConfigJson(ConfigProtoConverter.convertToJsonString(config))
Expand All @@ -114,6 +125,7 @@ void sendChangeNotification() throws InvalidProtocolBufferException {
.send(
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.of(TEST_CONTEXT)),
ConfigChangeEventValue.newBuilder()
.setEventTimeMillis(CURRENT_TIME_MILLIS)
.setUpdateEvent(
ConfigUpdateEvent.newBuilder()
.setPreviousConfigJson(ConfigProtoConverter.convertToJsonString(prevConfig))
Expand All @@ -133,6 +145,7 @@ void sendChangeNotificationWithNoContext() throws InvalidProtocolBufferException
.send(
KeyUtil.getKey(TEST_TENANT_ID_1, TEST_CONFIG_TYPE, Optional.empty()),
ConfigChangeEventValue.newBuilder()
.setEventTimeMillis(CURRENT_TIME_MILLIS)
.setUpdateEvent(
ConfigUpdateEvent.newBuilder()
.setPreviousConfigJson(ConfigProtoConverter.convertToJsonString(prevConfig))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.grpc.BindableService;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.time.Clock;
import java.util.List;
import org.hypertrace.alerting.config.service.EventConditionConfigServiceImpl;
import org.hypertrace.config.service.change.event.api.ConfigChangeEventGenerator;
Expand Down Expand Up @@ -33,7 +34,8 @@ public static List<BindableService> buildAllConfigServices(
lifecycle.shutdownComplete().thenRun(configChannel::shutdown);

ConfigChangeEventGenerator configChangeEventGenerator =
ConfigChangeEventGeneratorFactory.getInstance().createConfigChangeEventGenerator(config);
ConfigChangeEventGeneratorFactory.getInstance()
.createConfigChangeEventGenerator(config, Clock.systemUTC());

return List.of(
new ConfigServiceGrpcImpl(configStore),
Expand Down

0 comments on commit 91b1b34

Please sign in to comment.