Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,10 @@ static class CurrentOldFollowerInfos {
private final List<FollowerInfo> current;
private final List<FollowerInfo> old;

CurrentOldFollowerInfos(RaftConfigurationImpl conf, List<FollowerInfo> current, List<FollowerInfo> old) {
CurrentOldFollowerInfos(RaftConfigurationImpl conf,
List<FollowerInfo> current, List<FollowerInfo> old, RaftPeerId selfId) {
// set null when the sizes are not the same so that it will update next time.
this.conf = isSameSize(current, conf.getConf()) && isSameSize(old, conf.getOldConf())? conf: null;
this.conf = isSameSize(current, conf.getConf(), selfId) && isSameSize(old, conf.getOldConf(), selfId)? conf: null;
this.current = Collections.unmodifiableList(current);
this.old = old == null? null: Collections.unmodifiableList(old);
}
Expand All @@ -253,8 +254,12 @@ List<FollowerInfo> getOld() {
}
}

static boolean isSameSize(List<FollowerInfo> infos, PeerConfiguration conf) {
return conf == null? infos == null: conf.size() == infos.size();
static boolean isSameSize(List<FollowerInfo> infos, PeerConfiguration conf, RaftPeerId selfId) {
if (conf == null) {
return infos == null;
}
final int expectedFollowerCount = conf.size() - (conf.contains(selfId) ? 1 : 0);
return infos.size() == expectedFollowerCount;
}

/** Use == to compare if the confs are the same object. */
Expand All @@ -263,12 +268,19 @@ static boolean isSameConf(CurrentOldFollowerInfos cached, RaftConfigurationImpl
}

static class FollowerInfoMap {
private final RaftPeerId selfId;
private final Map<RaftPeerId, FollowerInfo> map = new ConcurrentHashMap<>();
@SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
private volatile CurrentOldFollowerInfos followerInfos;

void put(RaftPeerId id, FollowerInfo info) {
map.put(id, info);
public FollowerInfoMap(RaftPeerId selfId) {
this.selfId = selfId;
}

synchronized void put(RaftPeerId id, FollowerInfo info) {
if (map.put(id, info) != null) {
followerInfos = null;
}
}

CurrentOldFollowerInfos getFollowerInfos(RaftConfigurationImpl conf) {
Expand All @@ -283,7 +295,7 @@ CurrentOldFollowerInfos getFollowerInfos(RaftConfigurationImpl conf) {
synchronized CurrentOldFollowerInfos update(RaftConfigurationImpl conf) {
if (!isSameConf(followerInfos, conf)) { // compare again synchronized
followerInfos = new CurrentOldFollowerInfos(conf, getFollowerInfos(conf.getConf()),
Optional.ofNullable(conf.getOldConf()).map(this::getFollowerInfos).orElse(null));
Optional.ofNullable(conf.getOldConf()).map(this::getFollowerInfos).orElse(null), selfId);
}
return followerInfos;
}
Expand Down Expand Up @@ -331,7 +343,7 @@ boolean isApplied() {
@SuppressWarnings({"squid:S3077"}) // Suppress volatile for generic type
private volatile ConfigurationStagingState stagingState;

private final FollowerInfoMap followerInfoMap = new FollowerInfoMap();
private final FollowerInfoMap followerInfoMap;

/**
* The list of threads appending entries to followers.
Expand Down Expand Up @@ -369,6 +381,7 @@ boolean isApplied() {
LeaderStateImpl(RaftServerImpl server) {
this.name = ServerStringUtils.generateUnifiedName(server.getMemberId(), getClass());
this.server = server;
this.followerInfoMap = new FollowerInfoMap(server.getId());

final RaftProperties properties = server.getRaftServer().getProperties();
stagingCatchupGap = RaftServerConfigKeys.stagingCatchupGap(properties);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ratis.server.impl;

import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.server.impl.LeaderStateImpl.CurrentOldFollowerInfos;
import org.apache.ratis.server.impl.LeaderStateImpl.FollowerInfoMap;
import org.apache.ratis.server.leader.FollowerInfo;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

class TestFollowerInfoMap {
@Test
void testCacheReuseAndRefreshAfterFollowerInfoReplacement() {
final RaftPeer leader = peer("s0");
final RaftPeer follower1 = peer("s1");
final RaftPeer follower2 = peer("s2");

final RaftConfigurationImpl oldConf = RaftConfigurationImpl.newBuilder()
.setConf(Arrays.asList(leader, follower1, follower2))
.build();

final RaftConfigurationImpl jointConf = RaftConfigurationImpl.newBuilder()
.setConf(Arrays.asList(follower1, follower2))
.setOldConf(oldConf)
.build();

final FollowerInfoMap map = new FollowerInfoMap(leader.getId());
map.put(follower1.getId(), mock(FollowerInfo.class));
map.put(follower2.getId(), mock(FollowerInfo.class));

final CurrentOldFollowerInfos stable = map.getFollowerInfos(oldConf);
assertSame(stable, map.getFollowerInfos(oldConf), "Stable configuration cache miss");

final CurrentOldFollowerInfos joint = map.getFollowerInfos(jointConf);
assertEquals(2, joint.getCurrent().size());
assertEquals(2, joint.getOld().size());
assertSame(joint, map.getFollowerInfos(jointConf), "Joint configuration cache miss");

final FollowerInfo replacement = mock(FollowerInfo.class);
map.put(follower1.getId(), replacement);

final CurrentOldFollowerInfos refreshed = map.getFollowerInfos(jointConf);
assertNotSame(joint, refreshed);
assertTrue(refreshed.getCurrent().contains(replacement));
assertTrue(refreshed.getOld().contains(replacement));
}

private static RaftPeer peer(String id) {
return RaftPeer.newBuilder().setId(RaftPeerId.valueOf(id)).build();
}
}
Loading