-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide more granular way to manage embedding cache
- Loading branch information
1 parent
557c19c
commit f205da2
Showing
14 changed files
with
689 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 62 additions & 7 deletions
69
java/api/src/main/java/com/exadel/frs/core/trainservice/dto/CacheActionDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,76 @@ | ||
package com.exadel.frs.core.trainservice.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CacheActionDto { | ||
@JsonIgnoreProperties(ignoreUnknown = true) // here and below "ignoreUnknown = true" for backward compatibility | ||
public class CacheActionDto<T> { | ||
private CacheAction cacheAction; | ||
private String apiKey; | ||
@JsonProperty("uuid") | ||
private UUID serverUUID; | ||
private T payload; | ||
|
||
@JsonProperty("cacheAction") | ||
private String cacheAction; | ||
public <S> CacheActionDto<S> withPayload(S newPayload) { | ||
return new CacheActionDto<>( | ||
cacheAction, | ||
apiKey, | ||
serverUUID, | ||
newPayload | ||
); | ||
} | ||
|
||
@JsonProperty("apiKey") | ||
private String apiKey; | ||
public enum CacheAction { | ||
// UPDATE and DELETE stays here to support rolling update | ||
@Deprecated | ||
UPDATE, | ||
@Deprecated | ||
DELETE, | ||
REMOVE_EMBEDDINGS, | ||
REMOVE_SUBJECTS, | ||
ADD_EMBEDDINGS, | ||
RENAME_SUBJECTS, | ||
INVALIDATE | ||
} | ||
|
||
@JsonProperty("uuid") | ||
private String serverUUID; | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class RemoveEmbeddings { | ||
private Map<String, List<UUID>> embeddings; | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class RemoveSubjects { | ||
private List<String> subjects; | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class AddEmbeddings { | ||
private List<UUID> embeddings; | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static class RenameSubjects { | ||
private Map<String, String> subjectsNamesMapping; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
java/api/src/main/java/com/exadel/frs/core/trainservice/service/NotificationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.exadel.frs.core.trainservice.service; | ||
|
||
import com.exadel.frs.commonservice.projection.EmbeddingProjection; | ||
import com.exadel.frs.core.trainservice.cache.EmbeddingCacheProvider; | ||
import com.exadel.frs.core.trainservice.dto.CacheActionDto; | ||
import com.exadel.frs.core.trainservice.dto.CacheActionDto.AddEmbeddings; | ||
import com.exadel.frs.core.trainservice.dto.CacheActionDto.RemoveEmbeddings; | ||
import com.exadel.frs.core.trainservice.dto.CacheActionDto.RemoveSubjects; | ||
import com.exadel.frs.core.trainservice.dto.CacheActionDto.RenameSubjects; | ||
import java.util.Objects; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class NotificationHandler { | ||
private final EmbeddingCacheProvider cacheProvider; | ||
private final SubjectService subjectService; | ||
|
||
public void removeEmbeddings(CacheActionDto<RemoveEmbeddings> action) { | ||
action.getPayload().getEmbeddings() | ||
.entrySet() | ||
.stream() | ||
.filter(e -> StringUtils.isNotBlank(e.getKey())) | ||
.filter(e -> Objects.nonNull(e.getValue())) | ||
.filter(e -> !e.getValue().isEmpty()) | ||
.flatMap(e -> e.getValue().stream().filter(Objects::nonNull).map(id -> new EmbeddingProjection(id, e.getKey()))) | ||
.forEach( | ||
em -> cacheProvider.expose( | ||
action.getApiKey(), | ||
c -> c.removeEmbedding(em) | ||
) | ||
); | ||
} | ||
|
||
public void removeSubjects(CacheActionDto<RemoveSubjects> action) { | ||
action.getPayload().getSubjects() | ||
.stream() | ||
.filter(StringUtils::isNotBlank) | ||
.forEach( | ||
s -> cacheProvider.expose( | ||
action.getApiKey(), | ||
c -> c.removeEmbeddingsBySubjectName(s) | ||
) | ||
); | ||
} | ||
|
||
|
||
public void addEmbeddings(CacheActionDto<AddEmbeddings> action) { | ||
var filtered = action.getPayload().getEmbeddings() | ||
.stream() | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
subjectService.loadEmbeddingsById(filtered) | ||
.forEach( | ||
em -> cacheProvider.expose( | ||
action.getApiKey(), | ||
c -> c.addEmbedding(em) | ||
) | ||
); | ||
} | ||
|
||
public void renameSubjects(CacheActionDto<RenameSubjects> action) { | ||
action.getPayload().getSubjectsNamesMapping() | ||
.entrySet() | ||
.stream() | ||
.filter(e -> StringUtils.isNotBlank(e.getKey())) | ||
.filter(e -> StringUtils.isNotBlank(e.getValue())) | ||
.forEach( | ||
e -> cacheProvider.expose( | ||
action.getApiKey(), | ||
c -> c.updateSubjectName(e.getKey(), e.getValue()) | ||
) | ||
); | ||
} | ||
|
||
public <T> void invalidate(CacheActionDto<T> action) { | ||
cacheProvider.expose( | ||
action.getApiKey(), | ||
e -> cacheProvider.receiveInvalidateCache(action.getApiKey()) | ||
); | ||
} | ||
|
||
/** | ||
* @param action cacheAction | ||
* @deprecated in favour more granular cache managing. | ||
* See {@link CacheActionDto}. | ||
* Stays here to support rolling update | ||
*/ | ||
@Deprecated(forRemoval = true) | ||
public <T> void handleDelete(CacheActionDto<T> action) { | ||
cacheProvider.receiveInvalidateCache(action.getApiKey()); | ||
} | ||
|
||
/** | ||
* @param action cacheAction | ||
* @deprecated in favour more granular cache managing. | ||
* See {@link CacheActionDto}. | ||
* Stays here to support rolling update | ||
*/ | ||
@Deprecated(forRemoval = true) | ||
public <T> void handleUpdate(CacheActionDto<T> action) { | ||
cacheProvider.receivePutOnCache(action.getApiKey()); | ||
} | ||
} |
Oops, something went wrong.