Browse code

[perf] ACMS-261 Fix: reduce core/locator load thanks to more caching in ysnp

Thomas Cataldo authored on 10/01/2018 10:51:53
Showing 1 changed files
... ...
@@ -37,7 +37,9 @@ import net.bluemind.config.Token;
37 37
 import net.bluemind.core.api.fault.ErrorCode;
38 38
 import net.bluemind.core.api.fault.ServerFault;
39 39
 import net.bluemind.core.container.model.ItemValue;
40
+import net.bluemind.core.rest.http.CachingLocator;
40 41
 import net.bluemind.core.rest.http.HttpClientProvider;
42
+import net.bluemind.core.rest.http.ILocator;
41 43
 import net.bluemind.core.rest.http.VertxPromiseServiceProvider;
42 44
 import net.bluemind.domain.api.IDomainsPromise;
43 45
 import net.bluemind.group.api.IGroupPromise;
... ...
@@ -47,11 +49,14 @@ import net.bluemind.user.api.User;
47 47
 
48 48
 public class GroupProtocolHandler implements Handler<Buffer> {
49 49
 	private static final Logger logger = LoggerFactory.getLogger(GroupProtocolHandler.class);
50
-	private HttpClientProvider clientProvider;
51
-	private NetSocket socket;
52
-	private Handler<Throwable> exceptionHandler;
50
+	private final HttpClientProvider clientProvider;
51
+	private final NetSocket socket;
52
+	private final Handler<Throwable> exceptionHandler;
53
+	private final ILocator cachingLocator;
53 54
 	private static Cache<String, ItemValue<User>> usersCache = CacheBuilder.newBuilder()
54 55
 			.expireAfterAccess(10, TimeUnit.MINUTES).build();
56
+	private static Cache<String, List<String>> memberOfsCache = CacheBuilder.newBuilder()
57
+			.expireAfterWrite(10, TimeUnit.MINUTES).build();
55 58
 
56 59
 	private static class UserAndGroups {
57 60
 		public UserAndGroups(ItemValue<User> user, List<String> groups) {
... ...
@@ -66,6 +71,7 @@ public class GroupProtocolHandler implements Handler<Buffer> {
66 66
 	public GroupProtocolHandler(HttpClientProvider clientProvider, NetSocket socket, Vertx vertx) {
67 67
 		this.clientProvider = clientProvider;
68 68
 		this.socket = socket;
69
+		this.cachingLocator = CachingLocator.addCache(new VertxLocatorClient(clientProvider, "admin0@global.virt"));
69 70
 		this.exceptionHandler = new Handler<Throwable>() {
70 71
 
71 72
 			@Override
... ...
@@ -98,8 +104,8 @@ public class GroupProtocolHandler implements Handler<Buffer> {
98 98
 		String login = loginAtDomain.split("@")[0];
99 99
 		String domain = loginAtDomain.split("@")[1];
100 100
 
101
-		VertxPromiseServiceProvider provider = new VertxPromiseServiceProvider(clientProvider,
102
-				new VertxLocatorClient(clientProvider, loginAtDomain), Token.admin0());
101
+		VertxPromiseServiceProvider provider = new VertxPromiseServiceProvider(clientProvider, cachingLocator,
102
+				Token.admin0());
103 103
 
104 104
 		if (login.startsWith("group:") && login.substring("group:".length()).equals(domain)) {
105 105
 
... ...
@@ -158,11 +164,17 @@ public class GroupProtocolHandler implements Handler<Buffer> {
158 158
 				if (userFromCache == null) {
159 159
 					usersCache.put(domain + "-" + login, user);
160 160
 				}
161
-				return provider.instance(IUserPromise.class, domain).memberOfGroups(user.uid)
162
-
163
-						.thenApply(g -> {
164
-							return new UserAndGroups(user, g);
165
-						});
161
+				List<String> memberOfs = memberOfsCache.getIfPresent(domain + "-" + user.uid);
162
+				if (memberOfs != null) {
163
+					return CompletableFuture.completedFuture(new UserAndGroups(user, memberOfs));
164
+				} else {
165
+					return provider.instance(IUserPromise.class, domain).memberOfGroups(user.uid)
166
+
167
+							.thenApply(g -> {
168
+								memberOfsCache.put(domain + "-" + user.uid, g);
169
+								return new UserAndGroups(user, g);
170
+							});
171
+				}
166 172
 			}).thenAccept(groupsAndUser -> {
167 173
 				logger.trace("time to found user and memberof time {}", (System.currentTimeMillis() - time));
168 174
 				List<String> ret = new ArrayList<>(groupsAndUser.groups.size());