|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.security.support; |
| 9 | + |
| 10 | +import org.elasticsearch.cluster.metadata.ProjectId; |
| 11 | +import org.elasticsearch.cluster.project.ProjectResolver; |
| 12 | +import org.elasticsearch.common.cache.Cache; |
| 13 | +import org.elasticsearch.common.cache.CacheBuilder; |
| 14 | +import org.elasticsearch.common.cache.CacheLoader; |
| 15 | +import org.elasticsearch.common.cache.RemovalListener; |
| 16 | +import org.elasticsearch.common.cache.RemovalNotification; |
| 17 | +import org.elasticsearch.common.util.concurrent.ReleasableLock; |
| 18 | +import org.elasticsearch.core.TimeValue; |
| 19 | +import org.elasticsearch.xpack.core.security.support.CacheIteratorHelper; |
| 20 | + |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.concurrent.ExecutionException; |
| 23 | +import java.util.function.ToLongBiFunction; |
| 24 | + |
| 25 | +/** |
| 26 | + * Wrapper around a {@link Cache} instance where a composite key of the original key and the current project id, resolved through a |
| 27 | + * {@link ProjectResolver}, is used to write to and read from the cache. |
| 28 | + * <p> |
| 29 | + * During invalidation the cache is protected through locking in the {@link CacheIteratorHelper} because the result of iteration under any |
| 30 | + * mutation other than Cache.CacheIterator#remove() is undefined. Concurrent writes are allowed as long as there is no active |
| 31 | + * invalidation, the cache is protected against that by acquiring a read lock (blocking if invalidation in progress) before writing. |
| 32 | + * |
| 33 | + * @param <K> key type |
| 34 | + * @param <V> value type |
| 35 | + */ |
| 36 | +public class ProjectScopedCache<K, V> { |
| 37 | + private final Cache<ProjectScoped<K>, V> cache; |
| 38 | + private final ProjectResolver projectResolver; |
| 39 | + private final CacheIteratorHelper<ProjectScoped<K>, V> cacheIteratorHelper; |
| 40 | + |
| 41 | + public ProjectScopedCache(ProjectResolver projectResolver, Cache<ProjectScoped<K>, V> cache) { |
| 42 | + this.projectResolver = projectResolver; |
| 43 | + this.cache = cache; |
| 44 | + cacheIteratorHelper = new CacheIteratorHelper<>(cache); |
| 45 | + } |
| 46 | + |
| 47 | + public void invalidateProject() { |
| 48 | + if (projectResolver.supportsMultipleProjects()) { |
| 49 | + invalidateProject(projectResolver.getProjectId()); |
| 50 | + } else { |
| 51 | + cache.invalidateAll(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public void invalidateProject(ProjectId projectId) { |
| 56 | + cacheIteratorHelper.removeKeysIf(key -> key.projectId().equals(projectId)); |
| 57 | + } |
| 58 | + |
| 59 | + public void invalidate(K key) { |
| 60 | + invalidate(projectResolver.getProjectId(), key); |
| 61 | + } |
| 62 | + |
| 63 | + public void invalidate(ProjectId projectId, K key) { |
| 64 | + try (ReleasableLock ignored = cacheIteratorHelper.acquireUpdateLock()) { |
| 65 | + cache.invalidate(new ProjectScoped<>(projectId, key)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void invalidate(K key, V value) { |
| 70 | + try (ReleasableLock ignored = cacheIteratorHelper.acquireUpdateLock()) { |
| 71 | + cache.invalidate(new ProjectScoped<>(projectResolver.getProjectId(), key), value); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public void invalidateAll() { |
| 76 | + try (ReleasableLock ignored = cacheIteratorHelper.acquireUpdateLock()) { |
| 77 | + cache.invalidateAll(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public V computeIfAbsent(K key, CacheLoader<K, V> loader) throws ExecutionException { |
| 82 | + try (var ignored = cacheIteratorHelper.acquireUpdateLock()) { |
| 83 | + return cache.computeIfAbsent(new ProjectScoped<>(projectResolver.getProjectId(), key), k -> loader.load(k.value)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public int count() { |
| 88 | + return cache.count(); |
| 89 | + } |
| 90 | + |
| 91 | + public static class Builder<K, V> { |
| 92 | + private long maximumWeight; |
| 93 | + private TimeValue expireAfterAccessNanos; |
| 94 | + private TimeValue expireAfterWrite; |
| 95 | + private ToLongBiFunction<K, V> weigher; |
| 96 | + private RemovalListener<K, V> removalListener; |
| 97 | + |
| 98 | + public static <K, V> Builder<K, V> builder() { |
| 99 | + return new Builder<>(); |
| 100 | + } |
| 101 | + |
| 102 | + private Builder() {} |
| 103 | + |
| 104 | + public Builder<K, V> setMaximumWeight(long maximumWeight) { |
| 105 | + if (maximumWeight < 0) { |
| 106 | + throw new IllegalArgumentException("maximumWeight < 0"); |
| 107 | + } |
| 108 | + this.maximumWeight = maximumWeight; |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + public Builder<K, V> setExpireAfterAccess(TimeValue expireAfterAccess) { |
| 113 | + Objects.requireNonNull(expireAfterAccess); |
| 114 | + final long expireAfterAccessNanos = expireAfterAccess.getNanos(); |
| 115 | + if (expireAfterAccessNanos <= 0) { |
| 116 | + throw new IllegalArgumentException("expireAfterAccess <= 0"); |
| 117 | + } |
| 118 | + this.expireAfterAccessNanos = expireAfterAccess; |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + public Builder<K, V> setExpireAfterWrite(TimeValue expireAfterWrite) { |
| 123 | + Objects.requireNonNull(expireAfterWrite); |
| 124 | + final long expireAfterWriteNanos = expireAfterWrite.getNanos(); |
| 125 | + if (expireAfterWriteNanos <= 0) { |
| 126 | + throw new IllegalArgumentException("expireAfterWrite <= 0"); |
| 127 | + } |
| 128 | + this.expireAfterWrite = expireAfterWrite; |
| 129 | + return this; |
| 130 | + } |
| 131 | + |
| 132 | + public Builder<K, V> weigher(ToLongBiFunction<K, V> weigher) { |
| 133 | + Objects.requireNonNull(weigher); |
| 134 | + this.weigher = weigher; |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + public Builder<K, V> removalListener(RemovalListener<K, V> removalListener) { |
| 139 | + Objects.requireNonNull(removalListener); |
| 140 | + this.removalListener = removalListener; |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public ProjectScopedCache<K, V> build(ProjectResolver projectResolver) { |
| 145 | + CacheBuilder<ProjectScoped<K>, V> cacheBuilder = CacheBuilder.builder(); |
| 146 | + |
| 147 | + if (maximumWeight != -1) { |
| 148 | + cacheBuilder.setMaximumWeight(maximumWeight); |
| 149 | + } |
| 150 | + if (expireAfterAccessNanos != null) { |
| 151 | + cacheBuilder.setExpireAfterAccess(expireAfterAccessNanos); |
| 152 | + } |
| 153 | + if (expireAfterWrite != null) { |
| 154 | + cacheBuilder.setExpireAfterWrite(expireAfterWrite); |
| 155 | + } |
| 156 | + if (weigher != null) { |
| 157 | + cacheBuilder.weigher((key, value) -> weigher.applyAsLong(key.value, value)); |
| 158 | + } |
| 159 | + if (removalListener != null) { |
| 160 | + cacheBuilder.removalListener((notification) -> { |
| 161 | + removalListener.onRemoval( |
| 162 | + new RemovalNotification<>(notification.getKey().value, notification.getValue(), notification.getRemovalReason()) |
| 163 | + ); |
| 164 | + }); |
| 165 | + } |
| 166 | + return new ProjectScopedCache<>(projectResolver, cacheBuilder.build()); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private record ProjectScoped<T>(ProjectId projectId, T value) { |
| 171 | + private ProjectScoped(ProjectId projectId, T value) { |
| 172 | + this.projectId = Objects.requireNonNull(projectId); |
| 173 | + this.value = value; |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments