-
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 1 commit
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; | ||
|
@@ -41,6 +44,7 @@ | |
import com.linecorp.decaton.processor.runtime.Property; | ||
import com.linecorp.decaton.processor.runtime.PropertyDefinition; | ||
import com.linecorp.decaton.processor.runtime.PropertySupplier; | ||
import com.linecorp.decaton.processor.runtime.StaticPropertySupplier; | ||
|
||
/** | ||
* A {@link PropertySupplier} implementation with Central Dogma backend. | ||
|
@@ -119,7 +123,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); | ||
} | ||
|
||
|
@@ -145,19 +149,43 @@ public static CentralDogmaPropertySupplier register(CentralDogma centralDogma, S | |
return new CentralDogmaPropertySupplier(centralDogma, project, repository, filename); | ||
} | ||
|
||
public static CentralDogmaPropertySupplier register(CentralDogma centralDogma, String project, | ||
String repository, String filename, | ||
StaticPropertySupplier supplier) { | ||
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 in most cases people would use 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. 3efe319 |
||
List<Property<?>> properties = defaultPropertiesAsList().stream().map(defaultProperty -> { | ||
if (supplier.getProperty(defaultProperty.definition()).isPresent()) { | ||
return supplier.getProperty(defaultProperty.definition()).get(); | ||
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. As a call of prop = supplier.getProperty(...);
if (prop.ifPresent()) {
return prop.get();
}
... 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 changed the code to use a temporary variable for keeping the value in external systems. 8d38efe |
||
} 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) { | ||
createPropertyFile(centralDogma, | ||
project, repository, fileName, defaultPropertiesAsList()); | ||
} | ||
|
||
private static void createPropertyFile(CentralDogma centralDogma, String project, | ||
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); | ||
|
@@ -224,6 +252,19 @@ private static long remainingTime(long totalTime, long startedTime) { | |
return totalTime - (System.currentTimeMillis() - startedTime); | ||
} | ||
|
||
private 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; | ||
} | ||
|
||
// visible for testing | ||
static JsonNode defaultProperties() { | ||
final ObjectNode properties = objectMapper.createObjectNode(); | ||
|
@@ -234,4 +275,13 @@ static JsonNode defaultProperties() { | |
|
||
return properties; | ||
} | ||
|
||
// visible for testing | ||
static List<Property<?>> defaultPropertiesAsList() { | ||
List<Property<?>> properties = new ArrayList<>(); | ||
ProcessorProperties.PROPERTY_DEFINITIONS | ||
.forEach(definition -> properties.add(new DynamicProperty(definition))); | ||
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. There's no point to use 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. |
||
|
||
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