forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopologyInfo.java
207 lines (162 loc) · 7.92 KB
/
TopologyInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright 2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed 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.keycloak.connections.infinispan;
import java.net.InetSocketAddress;
import java.security.SecureRandom;
import java.util.Objects;
import org.infinispan.Cache;
import org.infinispan.distribution.DistributionManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.remoting.transport.Address;
import org.infinispan.remoting.transport.LocalModeAddress;
import org.infinispan.remoting.transport.Transport;
import org.infinispan.remoting.transport.jgroups.JGroupsAddress;
import org.infinispan.remoting.transport.jgroups.JGroupsTransport;
import org.jboss.logging.Logger;
import org.jgroups.Event;
import org.jgroups.JChannel;
import org.jgroups.stack.IpAddress;
import org.jgroups.util.NameCache;
import org.keycloak.Config;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class TopologyInfo {
private static final Logger logger = Logger.getLogger(TopologyInfo.class);
// Node name used in clustered environment. This typically points to "jboss.node.name" . If "jboss.node.name" is not set, it is randomly generated
// name
private final String myNodeName;
// Used just if "site" is configured (typically in cross-dc environment). Otherwise null
private final String mySiteName;
private final boolean isGeneratedNodeName;
public TopologyInfo(EmbeddedCacheManager cacheManager, Config.Scope config, boolean embedded, String providerId) {
String siteName;
String nodeName;
boolean isGeneratedNodeName = false;
if (System.getProperty(InfinispanConnectionProvider.JBOSS_SITE_NAME) != null) {
throw new IllegalArgumentException(
String.format("System property %s is in use. Use --spi-connections-infinispan-%s-site-name config option instead",
InfinispanConnectionProvider.JBOSS_SITE_NAME, providerId));
}
if (!embedded) {
Transport transport = cacheManager.getTransport();
if (transport != null) {
nodeName = transport.getAddress().toString();
siteName = cacheManager.getCacheManagerConfiguration().transport().siteId();
if (siteName == null) {
siteName = config.get("siteName");
}
} else {
nodeName = System.getProperty(InfinispanConnectionProvider.JBOSS_NODE_NAME);
siteName = config.get("siteName");
}
if (nodeName == null || nodeName.equals("localhost")) {
isGeneratedNodeName = true;
nodeName = generateNodeName();
}
} else {
boolean clustered = config.getBoolean("clustered", false);
nodeName = config.get("nodeName", System.getProperty(InfinispanConnectionProvider.JBOSS_NODE_NAME));
if (nodeName != null && nodeName.isEmpty()) {
nodeName = null;
}
siteName = config.get("siteName");
if (siteName != null && siteName.isEmpty()) {
siteName = null;
}
if (nodeName == null) {
if (!clustered) {
isGeneratedNodeName = true;
nodeName = generateNodeName();
} else {
throw new IllegalStateException("You must set jboss.node.name if you use clustered mode for InfinispanConnectionProvider");
}
}
}
this.myNodeName = nodeName;
this.mySiteName = siteName;
this.isGeneratedNodeName = isGeneratedNodeName;
}
private String generateNodeName() {
return InfinispanConnectionProvider.NODE_PREFIX + new SecureRandom().nextInt(1000000);
}
public String getMyNodeName() {
return myNodeName;
}
public String getMySiteName() {
return mySiteName;
}
@Override
public String toString() {
return String.format("Node name: %s, Site name: %s", myNodeName, mySiteName);
}
/**
* True if I am primary owner of the key in case of distributed caches. In case of local caches, always return true
*/
public boolean amIOwner(Cache cache, Object key) {
Address myAddress = cache.getCacheManager().getAddress();
Address objectOwnerAddress = getOwnerAddress(cache, key);
// NOTE: For scattered caches, this will always return true, which may not be correct. Need to review this if we add support for scattered caches
return Objects.equals(myAddress, objectOwnerAddress);
}
/**
* Get route to be used as the identifier for sticky session. Return null if I am not able to find the appropriate route (or in case of local mode)
*/
public String getRouteName(Cache cache, Object key) {
if (cache.getCacheConfiguration().clustering().cacheMode().isClustered() && isGeneratedNodeName) {
logger.warn("Clustered configuration used, but node name is not properly set. Make sure to start server with jboss.node.name property identifying cluster node");
}
if (isGeneratedNodeName) {
return null;
}
// Impl based on Wildfly sticky session algorithm for generating routes ( org.wildfly.clustering.web.infinispan.session.InfinispanRouteLocator )
Address address = getOwnerAddress(cache, key);
// Local mode
if (address == null || (address == LocalModeAddress.INSTANCE)) {
return myNodeName;
}
org.jgroups.Address jgroupsAddress = toJGroupsAddress(address);
String name = NameCache.get(jgroupsAddress);
// If no logical name exists, create one using physical address
if (name == null) {
Transport transport = cache.getCacheManager().getTransport();
JChannel jgroupsChannel = ((JGroupsTransport) transport).getChannel();
IpAddress ipAddress = (IpAddress) jgroupsChannel.down(new Event(Event.GET_PHYSICAL_ADDRESS, jgroupsAddress));
// Physical address might be null if node is no longer a member of the cluster
InetSocketAddress socketAddress = (ipAddress != null) ? new InetSocketAddress(ipAddress.getIpAddress(), ipAddress.getPort()) : new InetSocketAddress(0);
name = String.format("%s:%s", socketAddress.getHostString(), socketAddress.getPort());
logger.debugf("Address not found in NameCache. Fallback to %s", name);
}
return name;
}
private Address getOwnerAddress(Cache cache, Object key) {
DistributionManager dist = cache.getAdvancedCache().getDistributionManager();
Address address = (dist != null) && !cache.getCacheConfiguration().clustering().cacheMode().isScattered() ?
dist.getCacheTopology().getDistribution(key).primary() :
cache.getCacheManager().getAddress();
return address;
}
// See org.wildfly.clustering.server.group.CacheGroup
private static org.jgroups.Address toJGroupsAddress(Address address) {
if ((address == null) || (address == LocalModeAddress.INSTANCE)) return null;
if (address instanceof JGroupsAddress) {
JGroupsAddress jgroupsAddress = (JGroupsAddress) address;
return jgroupsAddress.getJGroupsAddress();
}
throw new IllegalArgumentException(address.toString());
}
}