Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache CD dynamic properties #222

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -18,6 +18,8 @@

import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand All @@ -44,6 +46,9 @@
import com.linecorp.decaton.processor.runtime.PropertyDefinition;
import com.linecorp.decaton.processor.runtime.PropertySupplier;

import lombok.AccessLevel;
import lombok.Getter;

/**
* A {@link PropertySupplier} implementation with Central Dogma backend.
*
Expand Down Expand Up @@ -72,6 +77,9 @@ public class CentralDogmaPropertySupplier implements PropertySupplier, AutoClose

private final Watcher<JsonNode> rootWatcher;

@Getter(AccessLevel.PACKAGE)// visible for testing
private final ConcurrentMap<String, DynamicProperty<?>> cachedProperties = new ConcurrentHashMap<>();

/**
* Creates a new {@link CentralDogmaPropertySupplier}.
* @param centralDogma a {@link CentralDogma} instance to use to access Central Dogma server.
Expand Down Expand Up @@ -118,32 +126,43 @@ public <T> Optional<Property<T>> getProperty(PropertyDefinition<T> definition) {
return Optional.empty();
}

DynamicProperty<T> prop = new DynamicProperty<>(definition);
Watcher<JsonNode> child = rootWatcher.newChild(jsonNode -> jsonNode.path(definition.name()));
child.watch(node -> {
// note: cache DynamicProperties to avoid using too many child watchers if getProperty is called repeatedly.
// for most use cases though, this cache is only filled/read once.
final DynamicProperty<?> cachedProp = cachedProperties.computeIfAbsent(definition.name(), name -> {
DynamicProperty<T> prop = new DynamicProperty<>(definition);
Watcher<JsonNode> child = rootWatcher.newChild(jsonNode -> jsonNode.path(definition.name()));
child.watch(node -> {
try {
setValue(prop, node);
} catch (Exception e) {
// Catching Exception instead of RuntimeException, since
// Kotlin-implemented DynamicProperty would throw checked exceptions
logger.warn("Failed to set value updated from CentralDogma for {}", definition.name(), e);
}
});
try {
JsonNode node = child.initialValueFuture().join().value(); //doesn't fail since it's a child watcher
setValue(prop, node);
} catch (Exception e) {
// Catching Exception instead of RuntimeException, since
// Kotlin-implemented DynamicProperty would throw checked exceptions
logger.warn("Failed to set value updated from CentralDogma for {}", definition.name(), e);
logger.warn("Failed to set initial value from CentralDogma for {}", definition.name(), e);
}

return prop;
});
try {
JsonNode node = child.initialValueFuture().join().value(); //doesn't fail since it's a child watcher
setValue(prop, node);
} catch (Exception e) {
// Catching Exception instead of RuntimeException, since
// Kotlin-implemented DynamicProperty would throw checked exceptions
logger.warn("Failed to set initial value from CentralDogma for {}", definition.name(), e);
}

return Optional.of(prop);
if (cachedProp.definition().runtimeType() != definition.runtimeType()) {
throw new IllegalStateException("Several different properties have the same name: " + definition.name());
}
//noinspection unchecked
return Optional.of((Property<T>) cachedProp);
}

@Override
public void close() {
rootWatcher.close();
cachedProperties.clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -119,6 +120,11 @@ public void testCDIntegration() throws InterruptedException {

latch.await();
assertEquals(20, prop.value().intValue());

IntStream.range(0, 10000)
.mapToObj(i -> CONFIG_PARTITION_CONCURRENCY)
.map(supplier::getProperty);
assertEquals(1, supplier.getCachedProperties().size());
Copy link
Contributor

Choose a reason for hiding this comment

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

To check cache is working properly, I think it's better all property returned from supplier.getProperty are same reference, rather than checking the internal hashmap's content

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough. Fixed

}

@Test
Expand Down
Loading