Stop reindexing indexes on new version

This commit is contained in:
Martin Kanis 2022-10-18 20:35:54 +02:00 committed by Michal Hajas
parent 4b6b607fe9
commit 8478b01758
2 changed files with 24 additions and 9 deletions

View file

@ -35,6 +35,7 @@ import org.keycloak.models.map.storage.hotRod.common.HotRodVersionUtils;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@ -118,7 +119,6 @@ public class DefaultHotRodConnectionProviderFactory implements HotRodConnectionP
LOG.debugf("Uploading proto schema to Infinispan server.");
registerSchemata();
String reindexCaches = config.get("reindexCaches", null);
RemoteCacheManagerAdmin administration = remoteCacheManager.administration();
if (reindexCaches != null && reindexCaches.equals("all")) {
@ -141,23 +141,34 @@ public class DefaultHotRodConnectionProviderFactory implements HotRodConnectionP
private void registerSchemata() {
final RemoteCache<String, String> protoMetadataCache = remoteCacheManager.getCache(ProtobufMetadataManagerConstants.PROTOBUF_METADATA_CACHE_NAME);
Set<String> cachesForIndexUpdate = new HashSet<>();
// First add Common classes definitions
GeneratedSchema commonSchema = CommonPrimitivesProtoSchemaInitializer.INSTANCE;
if (isUpdateNeeded(commonSchema.getProtoFileName(),
CommonPrimitivesProtoSchemaInitializer.COMMON_PRIMITIVES_VERSION,
protoMetadataCache.get(commonSchema.getProtoFileName()))) {
String currentProtoFile = protoMetadataCache.get(commonSchema.getProtoFileName());
// there is no proto file deployed on the server
if (currentProtoFile == null) {
protoMetadataCache.put(commonSchema.getProtoFileName(), commonSchema.getProtoFile());
}
else if (isUpdateNeeded(commonSchema.getProtoFileName(), CommonPrimitivesProtoSchemaInitializer.COMMON_PRIMITIVES_VERSION, currentProtoFile)) {
protoMetadataCache.put(commonSchema.getProtoFileName(), commonSchema.getProtoFile());
// if there is a change in common primitives, update all caches as we don't track in what areas are these common primitives used
cachesForIndexUpdate = ENTITY_DESCRIPTOR_MAP.values().stream().map(HotRodEntityDescriptor::getCacheName).collect(Collectors.toSet());
}
// Add schema for each entity descriptor
for (HotRodEntityDescriptor<?,?> descriptor : ENTITY_DESCRIPTOR_MAP.values()) {
GeneratedSchema schema = descriptor.getProtoSchema();
if (isUpdateNeeded(schema.getProtoFileName(),
descriptor.getCurrentVersion(),
protoMetadataCache.get(schema.getProtoFileName()))) {
currentProtoFile = protoMetadataCache.get(schema.getProtoFileName());
// there is no proto file deployed on the server
if (currentProtoFile == null) {
protoMetadataCache.put(schema.getProtoFileName(), schema.getProtoFile());
}
else if (isUpdateNeeded(schema.getProtoFileName(), descriptor.getCurrentVersion(), currentProtoFile)) {
protoMetadataCache.put(schema.getProtoFileName(), schema.getProtoFile());
cachesForIndexUpdate.add(descriptor.getCacheName());
}
}
String errors = protoMetadataCache.get(ProtobufMetadataManagerConstants.ERRORS_KEY_SUFFIX);
@ -173,6 +184,10 @@ public class DefaultHotRodConnectionProviderFactory implements HotRodConnectionP
throw new IllegalStateException("Some Protobuf schema files contain errors: " + errors);
}
// update index schema for caches, where a proto schema was updated
RemoteCacheManagerAdmin administration = remoteCacheManager.administration();
cachesForIndexUpdate.forEach(administration::updateIndexSchema);
}
/**

View file

@ -318,9 +318,9 @@ public class StorageOptions {
public static final Option<String> STORAGE_HOTROD_CACHE_REINDEX = new OptionBuilder<>("storage-hotrod-cache-reindex", String.class)
.category(OptionCategory.STORAGE)
.defaultValue("all")
.defaultValue(Optional.empty())
.expectedValues(StorageOptions::getExpectedCacheNames)
.description("List of cache names that should be indexed on Keycloak startup. Defaulting to `all` which means all caches are reindexed.")
.description("List of cache names that should be indexed on Keycloak startup. When set to `all`, all caches are reindexed. By default no caches are reindexed.")
.hidden()
.build();