|
| 1 | +/* |
| 2 | + * Copyright 2014-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.session.data.redis; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.UUID; |
| 21 | + |
| 22 | +import com.redis.testcontainers.RedisContainer; |
| 23 | +import io.lettuce.core.ReadFrom; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.testcontainers.containers.Container; |
| 26 | +import org.testcontainers.containers.Network; |
| 27 | + |
| 28 | +import org.springframework.data.redis.connection.RedisStaticMasterReplicaConfiguration; |
| 29 | +import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; |
| 30 | +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
| 31 | +import org.springframework.data.redis.core.RedisTemplate; |
| 32 | +import org.springframework.data.redis.serializer.RedisSerializer; |
| 33 | +import org.springframework.session.FindByIndexNameSessionRepository; |
| 34 | +import org.springframework.session.data.redis.RedisIndexedSessionRepository.RedisSession; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.awaitility.Awaitility.await; |
| 38 | + |
| 39 | +/** |
| 40 | + * Integration tests for {@link RedisIndexedSessionRepository} using |
| 41 | + * {@link RedisStaticMasterReplicaConfiguration}. |
| 42 | + */ |
| 43 | +class RedisIndexedSessionRepositoryStaticMasterReplicaITests { |
| 44 | + |
| 45 | + private static final String INDEX_NAME = FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME; |
| 46 | + |
| 47 | + @Test // gh-1715 |
| 48 | + void cleanUpExpiredSessionsWhenStaticMasterReplicaExpirationEventUnavailableThenRemovesPrincipalIndex() |
| 49 | + throws Exception { |
| 50 | + Network network = Network.newNetwork(); |
| 51 | + RedisContainer master = redisContainer(); |
| 52 | + master.withNetwork(network); |
| 53 | + master.withNetworkAliases("redis-master"); |
| 54 | + RedisContainer replica = redisContainer(); |
| 55 | + replica.withNetwork(network); |
| 56 | + replica.withCommand("redis-server", "--replicaof", "redis-master", "6379"); |
| 57 | + try { |
| 58 | + master.start(); |
| 59 | + replica.start(); |
| 60 | + awaitReplica(replica); |
| 61 | + LettuceConnectionFactory connectionFactory = createStaticMasterReplicaConnectionFactory(master, replica); |
| 62 | + try { |
| 63 | + connectionFactory.afterPropertiesSet(); |
| 64 | + RedisTemplate<String, Object> redis = createRedisTemplate(connectionFactory); |
| 65 | + RedisIndexedSessionRepository repository = new RedisIndexedSessionRepository(redis); |
| 66 | + String namespace = "RedisIndexedSessionRepositoryStaticMasterReplicaITests" + UUID.randomUUID(); |
| 67 | + repository.setRedisKeyNamespace(namespace); |
| 68 | + repository.setDefaultMaxInactiveInterval(Duration.ofSeconds(1)); |
| 69 | + |
| 70 | + String principalName = "findByPrincipalNameStaticMasterReplica" + UUID.randomUUID(); |
| 71 | + RedisSession session = repository.createSession(); |
| 72 | + session.setAttribute(INDEX_NAME, principalName); |
| 73 | + |
| 74 | + repository.save(session); |
| 75 | + |
| 76 | + String sessionKey = namespace + ":sessions:" + session.getId(); |
| 77 | + String expiresKey = namespace + ":sessions:expires:" + session.getId(); |
| 78 | + String principalIndexKey = namespace + ":index:" + INDEX_NAME + ":" + principalName; |
| 79 | + awaitReplicaContainsSessionAndIndex(replica, sessionKey, principalIndexKey); |
| 80 | + assertThat(repository.findByIndexNameAndIndexValue(INDEX_NAME, principalName)).hasSize(1); |
| 81 | + |
| 82 | + await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { |
| 83 | + assertThat(redis.hasKey(expiresKey)).isFalse(); |
| 84 | + assertThat(repository.findById(session.getId())).isNull(); |
| 85 | + }); |
| 86 | + |
| 87 | + assertThat(redis.boundSetOps(principalIndexKey).members()).contains(session.getId()); |
| 88 | + assertThat(repository.findByIndexNameAndIndexValue(INDEX_NAME, principalName)).isEmpty(); |
| 89 | + |
| 90 | + await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> { |
| 91 | + String expirationKey = namespace + ":expirations:" + roundDownMinute(System.currentTimeMillis()); |
| 92 | + redis.boundSetOps(expirationKey).add("expires:" + session.getId()); |
| 93 | + repository.cleanUpExpiredSessions(); |
| 94 | + assertThat(redis.boundSetOps(principalIndexKey).members()).doesNotContain(session.getId()); |
| 95 | + }); |
| 96 | + } |
| 97 | + finally { |
| 98 | + connectionFactory.destroy(); |
| 99 | + } |
| 100 | + } |
| 101 | + finally { |
| 102 | + replica.stop(); |
| 103 | + master.stop(); |
| 104 | + network.close(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static RedisContainer redisContainer() { |
| 109 | + return new RedisContainer(RedisContainer.DEFAULT_IMAGE_NAME.withTag(RedisContainer.DEFAULT_TAG)); |
| 110 | + } |
| 111 | + |
| 112 | + private static long roundDownMinute(long timeInMillis) { |
| 113 | + long minuteInMillis = Duration.ofMinutes(1).toMillis(); |
| 114 | + return timeInMillis - (timeInMillis % minuteInMillis); |
| 115 | + } |
| 116 | + |
| 117 | + private static LettuceConnectionFactory createStaticMasterReplicaConnectionFactory(RedisContainer master, |
| 118 | + RedisContainer replica) { |
| 119 | + RedisStaticMasterReplicaConfiguration configuration = new RedisStaticMasterReplicaConfiguration( |
| 120 | + master.getHost(), master.getFirstMappedPort()); |
| 121 | + configuration.node(replica.getHost(), replica.getFirstMappedPort()); |
| 122 | + LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder() |
| 123 | + .readFrom(ReadFrom.REPLICA_PREFERRED) |
| 124 | + .commandTimeout(Duration.ofSeconds(5)) |
| 125 | + .build(); |
| 126 | + return new LettuceConnectionFactory(configuration, clientConfiguration); |
| 127 | + } |
| 128 | + |
| 129 | + private static RedisTemplate<String, Object> createRedisTemplate(LettuceConnectionFactory connectionFactory) { |
| 130 | + RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); |
| 131 | + redisTemplate.setKeySerializer(RedisSerializer.string()); |
| 132 | + redisTemplate.setHashKeySerializer(RedisSerializer.string()); |
| 133 | + redisTemplate.setConnectionFactory(connectionFactory); |
| 134 | + redisTemplate.afterPropertiesSet(); |
| 135 | + return redisTemplate; |
| 136 | + } |
| 137 | + |
| 138 | + private static void awaitReplica(RedisContainer replica) { |
| 139 | + await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> { |
| 140 | + Container.ExecResult result = replica.execInContainer("redis-cli", "INFO", "replication"); |
| 141 | + String output = result.getStdout(); |
| 142 | + assertThat(output).contains("master_link_status:up"); |
| 143 | + assertThat(output.contains("role:slave") || output.contains("role:replica")).isTrue(); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + private static void awaitReplicaContainsSessionAndIndex(RedisContainer replica, String sessionKey, |
| 148 | + String principalIndexKey) { |
| 149 | + await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> { |
| 150 | + Container.ExecResult sessionResult = replica.execInContainer("redis-cli", "EXISTS", sessionKey); |
| 151 | + Container.ExecResult indexResult = replica.execInContainer("redis-cli", "SCARD", principalIndexKey); |
| 152 | + assertThat(sessionResult.getStdout().trim()).isEqualTo("1"); |
| 153 | + assertThat(indexResult.getStdout().trim()).isEqualTo("1"); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments