-
Notifications
You must be signed in to change notification settings - Fork 51
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
Add new method(overload) for CentralDogmaPropertySupplier#createPropertyFile/register #134
Changes from 16 commits
320cd69
3efe319
c17b3c7
df825c9
e8c311b
6b49b94
8d38efe
03ef312
6d9e69e
61cea29
382f706
f2ee11d
0f2db81
2e70198
f82bae3
e852943
ecf4284
e1952c4
ed02a7c
85f64d7
e6ae753
bea0861
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,14 @@ | |
|
||
package com.linecorp.decaton.centraldogma; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.stream.Collectors; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
@@ -119,7 +122,7 @@ public <T> Optional<Property<T>> getProperty(PropertyDefinition<T> definition) { | |
try { | ||
JsonNode node = child.initialValueFuture().join().value(); //doesn't fail since it's a child watcher | ||
setValue(prop, node); | ||
} catch (RuntimeException e) { | ||
} catch (RuntimeException e) { | ||
logger.warn("Failed to set initial value from CentralDogma for {}", definition.name(), e); | ||
} | ||
|
||
|
@@ -141,23 +144,51 @@ public void close() { | |
*/ | ||
public static CentralDogmaPropertySupplier register(CentralDogma centralDogma, String project, | ||
String repository, String filename) { | ||
createPropertyFile(centralDogma, project, repository, filename); | ||
createPropertyFile(centralDogma, project, repository, filename, ProcessorProperties.defaultProperties()); | ||
return new CentralDogmaPropertySupplier(centralDogma, project, repository, filename); | ||
} | ||
|
||
/** | ||
* Create a default property file if it doesn't exist on Central Dogma and | ||
* return a {@link CentralDogmaPropertySupplier}. | ||
* @param centralDogma a {@link CentralDogma} instance to use to access Central Dogma server. | ||
* @param project the project name where the properties are placed. | ||
* @param repository the repository name where the properties are placed. | ||
* @param filename the name of the file containing properties as top-level fields. | ||
* @param supplier a {@link PropertySupplier} which provides a set of properties with customized initial values. | ||
*/ | ||
public static CentralDogmaPropertySupplier register(CentralDogma centralDogma, String project, | ||
String repository, String filename, | ||
PropertySupplier supplier) { | ||
List<Property<?>> properties = ProcessorProperties.defaultProperties().stream().map(defaultProperty -> { | ||
Optional<? extends Property<?>> prop = supplier.getProperty(defaultProperty.definition()); | ||
if (prop.isPresent()) { | ||
return prop.get(); | ||
} else { | ||
return defaultProperty; | ||
} | ||
}).collect(Collectors.toList()); | ||
|
||
createPropertyFile(centralDogma, project, repository, filename, properties); | ||
return new CentralDogmaPropertySupplier(centralDogma, project, repository, filename); | ||
} | ||
|
||
private static void createPropertyFile(CentralDogma centralDogma, String project, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this overload no longer meaningful. Let's just use the below signature and pass default properties directly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I deleted this original method |
||
String repository, String fileName) { | ||
String repository, String fileName, | ||
List<Property<?>> properties) { | ||
Revision baseRevision = normalizeRevision(centralDogma, project, repository, Revision.HEAD); | ||
boolean fileExists = fileExists(centralDogma, project, repository, fileName, baseRevision); | ||
long startedTime = System.currentTimeMillis(); | ||
long remainingTime = remainingTime(PROPERTY_CREATION_TIMEOUT_MILLIS, startedTime); | ||
|
||
JsonNode jsonNodeProperties = convertPropertyListToJsonNode(properties); | ||
|
||
while (!fileExists && remainingTime > 0) { | ||
try { | ||
centralDogma.push(project, repository, baseRevision, | ||
String.format("[CentralDogmaPropertySupplier] Property file created: %s", | ||
fileName), | ||
Change.ofJsonUpsert(fileName, defaultProperties())) | ||
Change.ofJsonUpsert(fileName, jsonNodeProperties)) | ||
.get(remainingTime, TimeUnit.MILLISECONDS); | ||
logger.info("New property file registered on Central Dogma: {}/{}/{}", | ||
project, repository, fileName); | ||
|
@@ -225,13 +256,16 @@ private static long remainingTime(long totalTime, long startedTime) { | |
} | ||
|
||
// visible for testing | ||
static JsonNode defaultProperties() { | ||
final ObjectNode properties = objectMapper.createObjectNode(); | ||
ProcessorProperties.PROPERTY_DEFINITIONS | ||
.forEach(definition -> properties.set(definition.name(), | ||
objectMapper.valueToTree(definition.defaultValue())) | ||
); | ||
|
||
return properties; | ||
static JsonNode convertPropertyListToJsonNode(List<Property<?>> properties) { | ||
final ObjectNode propertiesObjectNode = objectMapper.createObjectNode(); | ||
properties.forEach( | ||
property -> { | ||
propertiesObjectNode.set( | ||
property.definition().name(), | ||
objectMapper.valueToTree(property.value()) | ||
); | ||
} | ||
); | ||
return propertiesObjectNode; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
package com.linecorp.decaton.processor.runtime; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
@@ -218,4 +219,13 @@ public static Builder<ProcessorProperties> builder() { | |
public ProcessorProperties(Map<PropertyDefinition<?>, Property<?>> properties) { | ||
super(properties); | ||
} | ||
|
||
/** | ||
* Returns a List of properties with default values. | ||
*/ | ||
public static List<Property<?>> defaultProperties() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please give it even a short javadoc since its also a public interface. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a short description for this method: f82bae3 |
||
List<Property<?>> properties = new ArrayList<>(); | ||
PROPERTY_DEFINITIONS.forEach(definition -> properties.add(Property.ofStatic(definition))); | ||
return properties; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As its a public interface, please give it a javadoc like another overload of
register
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a javadoc for the new added
register
method: e8c311b