8000 Remove the previous coordinator from JDBC_PING2 on view change by ahus1 · Pull Request #55248 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Draft
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 @@ -154,6 +154,9 @@ protected void insert(Connection connection, PingData data, String clustername)
@Override
protected void handleView(View new_view, View old_view, boolean coord_changed) {
super.handleView(new_view, old_view, coord_changed);
if (is_coord && old_view != null) {
removeLeftCoordinator(new_view, old_view);
}
if (coord_changed) {
try {
removeStaleEntries();
Expand All @@ -163,6 +166,23 @@ protected void handleView(View new_view, View old_view, boolean coord_changed) {
}
}

/**
* Remove the row of the previous coordinator if it left the view. Its row is allocated to itself, so it would
* otherwise be kept until stale, and could win the split-brain decision against this node.
* If that member is still alive, it will write its row again.
*/
private void removeLeftCoordinator(View new_view, View old_view) {
Address old_coord = old_view.getCoord();
if (old_coord == null || new_view.containsMember(old_coord)) {
return;
}
try (var conn = getConnection()) {
delete(conn, getClusterName(), old_coord);
Comment thread
ahus1 marked this conversation as resolved.
Resolved
} catch (Exception e) {
log.error(String.format("%s: failed removing the previous coordinator", local_addr), e);
}
}

protected void removeAllNotInCurrentView() {
View local_view = view;
if (local_view == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jgroups.Address;
import org.jgroups.JChannel;
import org.jgroups.PhysicalAddress;
import org.jgroups.View;
import org.jgroups.conf.ClassConfigurator;
import org.jgroups.logging.Log;
import org.jgroups.protocols.PingData;
Expand Down Expand Up @@ -247,6 +248,45 @@ public void testClearing() throws Exception {
}
}

@Test
public void testLeftCoordinatorRemovedOnViewChange() throws Exception {
JChannel channel = createChannel("0");
try (channel) {
channel.connect("ISPN");
KEYCLOAK_JDBC_PING2_FOR_TESTING jdbcPing = (KEYCLOAK_JDBC_PING2_FOR_TESTING) channel.getProtocolStack().getProtocols().stream().filter(protocol -> protocol instanceof KEYCLOAK_JDBC_PING2).findFirst().orElseThrow(() -> new RuntimeException("Didn't find JDBC_PING"));
try (Connection con = jdbcPing.getConnection()) {
// A previous coordinator that is coordinated by itself, and a node that never was in our view
UUID left = UUID.randomUUID();
UUID other = UUID.randomUUID();
for (UUID address : List.of(left, other)) {
try (PreparedStatement ps = con.prepareStatement(jdbcPing.getInsertSingleSql())) {
String addr = Util.addressToString(address);
ps.setString(1, addr);
ps.setString(2, "x");
ps.setString(3, jdbcPing.getClusterName());
ps.setString(4, "127.0.0.1:9999");
ps.setBoolean(5, true);
ps.setLong(6, Time.currentTime());
ps.setString(7, addr);
ps.executeUpdate();
}
}

View newView = channel.view();
View oldView = View.create(left, 1, left, channel.getAddress());
jdbcPing.handleView(newView, oldView, true);

List<PingData> remaining = jdbcPing.readFromDB(jdbcPing.getClusterName());
assertFalse(remaining.stream().anyMatch(p -> p.getAddress().equals(left)));
assertTrue(remaining.stream().anyMatch(p -> p.getAddress().equals(other)));
try (PreparedStatement ps = con.prepareStatement("DELETE FROM jgroups WHERE address = ?")) {
ps.setString(1, Util.addressToString(other));
ps.executeUpdate();
}
}
}
}

@SuppressWarnings("resource")
protected static JChannel createChannel(String name) throws Exception {
return new JChannel(JdbcPing2Test.PROTOCOL_STACK).name(name);
Expand Down
Loading
0