Skip to content

Commit

Permalink
#1869 provided missing javadocs, fixed test
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jäckle <thomas.jaeckle@beyonnex.io>
  • Loading branch information
thjaeckle committed Jan 24, 2024
1 parent 0182f9b commit 76c0e67
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ public interface Cache<K, V> {
boolean invalidate(K key);

/**
* Invalidates the passed key from the cache if present. TODO TJ adjust docs
* Invalidates the passed key from the cache if present and the passed {@code valueCondition} evaluates to
* {@code true}.
*
* @param key the key to invalidate.
* @param valueCondition
* @param valueCondition the condition which has to pass in order to invalidate the key from the cache.
* @return {@code true} if the entry was cached and is now invalidated, {@code false} otherwise.
*/
boolean invalidateConditionally(K key, Predicate<V> valueCondition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static ThingWriteModel toWriteModel(final JsonObject thing,
.orElse(null);

final var metadata =
Metadata.of(thingId, thingRevision, thingPolicyTag, allReferencedPolicies,
Metadata.of(thingId, thingRevision, thingPolicyTag, null, allReferencedPolicies,
Optional.ofNullable(oldMetadata).flatMap(Metadata::getModified).orElse(null),
Optional.ofNullable(oldMetadata).map(Metadata::getEvents).orElse(List.of()),
Optional.ofNullable(oldMetadata).map(Metadata::getTimers).orElse(List.of()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ private Metadata(final ThingId thingId,
* @param thingId the Thing ID.
* @param thingRevision the Thing revision.
* @param thingPolicy the policy directly referenced by the thing.
* @param causingPolicyTag TODO TJ doc
* @param causingPolicyTag defines the policy which "caused" the update - this might e.g. be an "imported" policy
* when it differs to the provided {@code thingPolicy}.
* @param allReferencedPolicies the policy directly and indirectly (via policy import) referenced by the thing.
* @param timer an optional timer measuring the search updater's consistency lag.
* @return the new Metadata object.
Expand All @@ -118,7 +119,8 @@ public static Metadata of(final ThingId thingId,
* @param thingId the Thing ID.
* @param thingRevision the Thing revision.
* @param thingPolicy the policy directly referenced by the thing.
* @param causingPolicyTag TODO TJ doc
* @param causingPolicyTag defines the policy which "caused" the update - this might e.g. be an "imported" policy
* when it differs to the provided {@code thingPolicy}.
* @param allReferencedPolicies the policy directly and indirectly (via policy import) referenced by the thing.
* @param timer an optional timer measuring the search updater's consistency lag.
* @param ackRecipient the ackRecipient.
Expand All @@ -144,6 +146,8 @@ public static Metadata of(final ThingId thingId,
* @param thingId the Thing ID.
* @param thingRevision the Thing revision.
* @param thingPolicy the policy directly referenced by the thing.
* @param causingPolicyTag defines the policy which "caused" the update - this might e.g. be an "imported" policy
* when it differs to the provided {@code thingPolicy}.
* @param allReferencedPolicies the policy directly and indirectly (via policy import) referenced by the thing.
* @param modified the timestamp of the last change incorporated into the search index, or null if not known.
* @param events the events included in the metadata causing the search update.
Expand All @@ -155,14 +159,15 @@ public static Metadata of(final ThingId thingId,
public static Metadata of(final ThingId thingId,
final long thingRevision,
@Nullable final PolicyTag thingPolicy,
@Nullable final PolicyTag causingPolicyTag,
final Collection<PolicyTag> allReferencedPolicies,
@Nullable final Instant modified,
final List<ThingEvent<?>> events,
final Collection<StartedTimer> timers,
final Collection<ActorSelection> ackRecipient,
final Collection<UpdateReason> updateReasons) {

return new Metadata(thingId, thingRevision, thingPolicy, null/*TODO TJ */, allReferencedPolicies, modified, events, timers,
return new Metadata(thingId, thingRevision, thingPolicy, causingPolicyTag, allReferencedPolicies, modified, events, timers,
ackRecipient, false, false, updateReasons);
}

Expand All @@ -172,7 +177,8 @@ public static Metadata of(final ThingId thingId,
* @param thingId the Thing ID.
* @param thingRevision the Thing revision.
* @param thingPolicy the policy directly referenced by the thing.
* @param causingPolicyTag TODO TJ
* @param causingPolicyTag defines the policy which "caused" the update - this might e.g. be an "imported" policy
* when it differs to the provided {@code thingPolicy}.
* @param allReferencedPolicies the policy directly and indirectly (via policy import) referenced by the thing.
* @param modified the timestamp of the last change incorporated into the search index, or null if not known.
* @param timer an optional timer measuring the search updater's consistency lag.
Expand Down Expand Up @@ -285,8 +291,8 @@ public Optional<PolicyTag> getThingPolicyTag() {
}

/**
* TODO TJ doc
* @return
* @return the policy which "caused" the update - this might e.g. be an "imported" policy
* when it differs to the provided {@code getThingPolicyTag()}.
*/
public Optional<PolicyTag> getCausingPolicyTag() {
return Optional.ofNullable(causingPolicyTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.Predicate;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -339,13 +340,15 @@ private Source<Entry<Pair<Policy, Set<PolicyTag>>>, NotUsed> readCachedEnforcer(

// only invalidate causing policy tag once, e.g. when a massively imported policy is changed:
metadata.getCausingPolicyTag()
.filter(Predicate.not(tag -> policyId.equals(tag.getEntityId())))
.ifPresent(causingPolicyTag -> {
final boolean invalidated = policyEnforcerCache.invalidateConditionally(
causingPolicyTag.getEntityId(),
entry -> entry.exists() &&
entry.getRevision() < causingPolicyTag.getRevision()
);
log.debug("Causing policy tag was invalidated conditionally: <{}>", invalidated);
log.debug("Causing policy tag was invalidated conditionally: <{}>",
invalidated);
});

return readCachedEnforcer(metadata, policyId, iteration + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,6 @@ private FSM.State<State, Data> onPolicyReferenceTag(final PolicyReferenceTag pol
affectedOldPolicyTag);
}

// TODO TJ invalidate cache entry??

final var policyTag = policyReferenceTag.getPolicyTag();
if (affectedOldPolicyTag == null || affectedOldPolicyTag.getRevision() < policyTag.getRevision()) {
final PolicyTag thingPolicyTag = Optional.ofNullable(affectedOldPolicyTag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void appendsThingPolicyTagToAllReferencedPolicies() {
.getAllReferencedPolicyTags())
.containsExactlyInAnyOrderElementsOf(expectedReferencedPolicyTags);

assertThat(Metadata.of(ThingId.generateRandom(), 1337L, policyTag, Set.of(referencedPolicyTag), null, List.of(),
assertThat(Metadata.of(ThingId.generateRandom(), 1337L, policyTag, null, Set.of(referencedPolicyTag), null, List.of(),
List.of(), List.of(), List.of())
.getAllReferencedPolicyTags())
.containsExactlyInAnyOrderElementsOf(expectedReferencedPolicyTags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public void policyIdChangeTriggersSync() {
inletProbe.request(16);
final var data = inletProbe.expectNext();
assertThat(data.metadata().export()).isEqualTo(
Metadata.of(THING_ID, REVISION, null, null, Set.of(PolicyTag.of(policyId, 1L)), null));
Metadata.of(THING_ID, REVISION, null, policyTag.getPolicyTag(), Set.of(PolicyTag.of(policyId, 1L)), null));
assertThat(data.metadata().getUpdateReasons()).contains(UpdateReason.POLICY_UPDATE);
assertThat(data.metadata().getTimers()).isEmpty();
}};
Expand Down

0 comments on commit 76c0e67

Please sign in to comment.