Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -301,6 +301,20 @@ private PrefabContextSetReadable resolveContext(
.getContext()
.filter(Predicate.not(PrefabContextSetReadable::isEmpty));

if (LOG.isTraceEnabled()) {
LOG.trace(
"context store contains {}, after filtering {}",
getContextStore().getContext().map(Object::toString).orElse("n/a"),
existingContext.map(Object::toString).orElse("n/a")
);

LOG.trace(
"Merging passed context {} with context store {}",
prefabContextSetReadable,
getContextStore().getContext().map(Object::toString).orElse("n/a")
);
}

if (newContext.isEmpty()) {
return existingContext.orElse(PrefabContextSetReadable.EMPTY);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ void start() {
@VisibleForTesting
void doUpload() {
if (shouldUpload()) {
LOG.debug("uploading context shapes");
prefabHttpClient.reportContextShape(buildProtoShapesFromShapeState());
lastUploadTime = clock.millis();
dirtyFlag.set(false);
boolean previousDirtyFlagValue = dirtyFlag.getAndSet(false);
Prefab.ContextShapes shapesToUpload = buildProtoShapesFromShapeState();
LOG.debug("uploading context shapes {}", shapesToUpload);
if (prefabHttpClient.reportContextShape(shapesToUpload)) {
lastUploadTime = clock.millis();
} else {
dirtyFlag.compareAndSet(false, previousDirtyFlagValue);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void start() {
try {
doUpload();
} catch (Exception e) {
LOG.debug("error uploading context shapes", e);
LOG.debug("error uploading evaluated keys", e);
}
},
1,
Expand All @@ -76,9 +76,12 @@ void doUpload() {
.newBuilder()
.addAllKeys(evaluatedKeys);
namespace.ifPresent(builder::setNamespace);
prefabHttpClient.reportEvaluatedKeys(builder.build());
lastUploadTime = clock.millis();
dirtyFlag.set(false);
Prefab.EvaluatedKeys keysToUpload = builder.build();
LOG.debug("uploading evaluated keys {}", keysToUpload);
if (prefabHttpClient.reportEvaluatedKeys(keysToUpload)) {
lastUploadTime = clock.millis();
dirtyFlag.set(false);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void reportLoggers(Prefab.Loggers loggers) {
}
}

void reportContextShape(Prefab.ContextShapes contextShapes) {
boolean reportContextShape(Prefab.ContextShapes contextShapes) {
HttpRequest request = getClientBuilderWithStandardHeaders()
.header("Content-Type", PROTO_MEDIA_TYPE)
.header("Accept", PROTO_MEDIA_TYPE)
Expand All @@ -82,23 +82,25 @@ void reportContextShape(Prefab.ContextShapes contextShapes) {
request,
HttpResponse.BodyHandlers.ofString()
);

if (!isSuccess(response.statusCode())) {
LOG.info(
"Uploading context shapes returned unsuccessful code {} with body {}",
response.statusCode(),
response.body()
);
if (isSuccess(response.statusCode())) {
return true;
}

LOG.info(
"Uploading context shapes returned unsuccessful code {} with body {}",
response.statusCode(),
response.body()
);
} catch (IOException e) {
LOG.warn("Error uploading context shapes via http {}", e.getMessage());
} catch (InterruptedException e) {
LOG.warn("Interrupted while uploading context shapes");
Thread.currentThread().interrupt();
}
return false;
}

void reportEvaluatedKeys(Prefab.EvaluatedKeys evaluatedKeys) {
boolean reportEvaluatedKeys(Prefab.EvaluatedKeys evaluatedKeys) {
HttpRequest request = getClientBuilderWithStandardHeaders()
.header("Content-Type", PROTO_MEDIA_TYPE)
.header("Accept", PROTO_MEDIA_TYPE)
Expand All @@ -112,19 +114,21 @@ void reportEvaluatedKeys(Prefab.EvaluatedKeys evaluatedKeys) {
HttpResponse.BodyHandlers.ofString()
);

if (!isSuccess(response.statusCode())) {
LOG.info(
"Uploading evaluated keys returned unsuccessful code {} with body {}",
response.statusCode(),
response.body()
);
if (isSuccess(response.statusCode())) {
return true;
}
LOG.info(
"Uploading evaluated keys returned unsuccessful code {} with body {}",
response.statusCode(),
response.body()
);
} catch (IOException e) {
LOG.warn("Error uploading evaluated keys via http {}", e.getMessage());
} catch (InterruptedException e) {
LOG.warn("Interrupted while uploading evaluated keys");
Thread.currentThread().interrupt();
}
return false;
}

CompletableFuture<HttpResponse<Void>> requestConfigSSE(
Expand Down
19 changes: 14 additions & 5 deletions client/src/main/java/cloud/prefab/context/PrefabContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public boolean equals(Object o) {
);
}

@Override
public int hashCode() {
return Objects.hash(name, properties);
}

@Override
public String toString() {
return com.google.common.base.MoreObjects
.toStringHelper(this)
.add("name", name)
.add("properties", properties)
.toString();
}

public Prefab.ContextShape getShape() {
Prefab.ContextShape.Builder shapeBuilder = Prefab.ContextShape
.newBuilder()
Expand All @@ -69,11 +83,6 @@ public Prefab.ContextShape getShape() {
return shapeBuilder.build();
}

@Override
public int hashCode() {
return Objects.hash(name, properties);
}

public static PrefabContext unnamedFromMap(
Map<String, Prefab.ConfigValue> configValueMap
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(contextByNameMap);
}

@Override
public String toString() {
return com.google.common.base.MoreObjects
.toStringHelper(this)
.add("contextByNameMap", contextByNameMap)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public Iterable<PrefabContext> getContexts() {
public boolean isEmpty() {
return true;
}

@Override
public String toString() {
return com.google.common.base.MoreObjects
.toStringHelper(this)
.add("contextByNameMap", "EMPTY")
.toString();
}
};

static PrefabContextSetReadable readOnlyContextSetView(PrefabContextSet contextSet) {
Expand All @@ -42,6 +50,11 @@ public Iterable<PrefabContext> getContexts() {
public boolean isEmpty() {
return contextSet.isEmpty();
}

@Override
public String toString() {
return contextSet.toString();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -39,6 +40,7 @@ class ContextShapeAggregatorTest {

@BeforeEach
void beforeEach() {
lenient().when(prefabHttpClient.reportContextShape(any())).thenReturn(true);
aggregator =
new ContextShapeAggregator(
new Options().setNamespace("the-namespace"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -38,6 +39,7 @@ class EvaluatedKeysAggregatorTest {

@BeforeEach
void beforeEach() {
lenient().when(prefabHttpClient.reportEvaluatedKeys(any())).thenReturn(true);
aggregator =
new EvaluatedKeysAggregator(
new Options().setNamespace("the-namespace"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ private Optional<PrefabContextSet> getPrefabContextSet() {
objectHttpRequest.getAttribute(ATTRIBUTE_NAME, PrefabContextSet.class)
);
}

@Override
public boolean isAvailable() {
return ServerRequestContext.currentRequest().isPresent();
}
}