-
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 0402180
Showing
14 changed files
with
658 additions
and
100 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
67 changes: 54 additions & 13 deletions
67
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,62 @@ | ||
package com.exadel.frs.core.trainservice.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CacheActionDto { | ||
@JsonIgnoreProperties(ignoreUnknown = true) // here and below "ignoreUnknown = true" for backward compatibility | ||
public record CacheActionDto<T>( | ||
CacheAction cacheAction, | ||
String apiKey, | ||
@JsonProperty("uuid") | ||
UUID serverUUID, | ||
T payload | ||
) { | ||
public <S> CacheActionDto<S> withPayload(S payload) { | ||
return new CacheActionDto<>( | ||
cacheAction, | ||
apiKey, | ||
serverUUID, | ||
payload | ||
); | ||
} | ||
|
||
@JsonProperty("cacheAction") | ||
private String cacheAction; | ||
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("apiKey") | ||
private String apiKey; | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record RemoveEmbeddings( | ||
Map<String, List<UUID>> embeddings | ||
) { | ||
} | ||
|
||
@JsonProperty("uuid") | ||
private String serverUUID; | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record RemoveSubjects( | ||
List<String> subjects | ||
) { | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record AddEmbeddings( | ||
List<UUID> embeddings | ||
) { | ||
} | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record RenameSubjects( | ||
Map<String, String> subjectsNamesMapping | ||
) { | ||
} | ||
} |
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
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.payload().embeddings() | ||
.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.apiKey(), | ||
c -> c.removeEmbedding(em) | ||
) | ||
); | ||
} | ||
|
||
public void removeSubjects(CacheActionDto<RemoveSubjects> action) { | ||
action.payload().subjects() | ||
.stream() | ||
.filter(StringUtils::isNotBlank) | ||
.forEach( | ||
s -> cacheProvider.expose( | ||
action.apiKey(), | ||
c -> c.removeEmbeddingsBySubjectName(s) | ||
) | ||
); | ||
} | ||
|
||
|
||
public void addEmbeddings(CacheActionDto<AddEmbeddings> action) { | ||
var filtered = action.payload().embeddings() | ||
.stream() | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
subjectService.loadEmbeddingsById(filtered) | ||
.forEach( | ||
em -> cacheProvider.expose( | ||
action.apiKey(), | ||
c -> c.addEmbedding(em) | ||
) | ||
); | ||
} | ||
|
||
public void renameSubjects(CacheActionDto<RenameSubjects> action) { | ||
action.payload().subjectsNamesMapping() | ||
.entrySet() | ||
.stream() | ||
.filter(e -> StringUtils.isNotBlank(e.getKey())) | ||
.filter(e -> StringUtils.isNotBlank(e.getValue())) | ||
.forEach( | ||
e -> cacheProvider.expose( | ||
action.apiKey(), | ||
c -> c.updateSubjectName(e.getKey(), e.getValue()) | ||
) | ||
); | ||
} | ||
|
||
public <T> void invalidate(CacheActionDto<T> action) { | ||
cacheProvider.expose( | ||
action.apiKey(), | ||
e -> cacheProvider.receiveInvalidateCache(action.apiKey()) | ||
); | ||
} | ||
|
||
/** | ||
* @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.apiKey()); | ||
} | ||
|
||
/** | ||
* @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.apiKey()); | ||
} | ||
} |
Oops, something went wrong.