forked from infinispan/infinispan
-
Notifications
You must be signed in to change notification settings - Fork 5
/
NettyTransportConnectionStats.java
127 lines (107 loc) · 4.48 KB
/
NettyTransportConnectionStats.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
package org.infinispan.server.core.transport;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.marshall.SerializeWith;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.server.core.ProtocolServer;
import io.netty.channel.group.ChannelGroup;
class NettyTransportConnectionStats {
private final EmbeddedCacheManager cacheManager;
private final boolean isGlobalStatsEnabled;
private final ChannelGroup acceptedChannels;
private final String threadNamePrefix;
private final AtomicLong totalBytesWritten = new AtomicLong();
private final AtomicLong totalBytesRead = new AtomicLong();
public NettyTransportConnectionStats(EmbeddedCacheManager cacheManager, ChannelGroup acceptedChannels, String threadNamePrefix) {
this.cacheManager = cacheManager;
this.acceptedChannels = acceptedChannels;
this.threadNamePrefix = threadNamePrefix;
this.isGlobalStatsEnabled = cacheManager != null && SecurityActions.getCacheManagerConfiguration(cacheManager).statistics();
}
private void increment(AtomicLong base, long bytes) {
if (isGlobalStatsEnabled)
base.addAndGet(bytes);
}
public void incrementTotalBytesWritten(long bytes) {
increment(totalBytesWritten, bytes);
}
public void incrementTotalBytesRead(long bytes) {
increment(totalBytesRead, bytes);
}
public long getTotalBytesWritten() {
return totalBytesWritten.get();
}
public long getTotalBytesRead() {
return totalBytesRead.get();
}
private boolean needDistributedCalculation() {
if (cacheManager != null) {
return cacheManager.getMembers() != null && cacheManager.getMembers().size() > 1;
}
return false;
}
private int calculateGlobalConnections() {
AtomicInteger connectionCount = new AtomicInteger();
// Submit calculation task
CompletableFuture<Void> results = SecurityActions.getClusterExecutor(cacheManager).submitConsumer(
new ConnectionAdderTask(threadNamePrefix), (a, v, t) -> {
if (t != null) {
throw new CacheException(t);
}
connectionCount.addAndGet(v);
});
// Take all results and add them up with a bit of functional programming magic :)
try {
results.get();
} catch (InterruptedException | ExecutionException e) {
throw new CacheException(e);
}
return connectionCount.get();
}
@SerializeWith(NettyTransportConnectionStats.ConnectionAdderTask.Externalizer.class)
static class ConnectionAdderTask implements Function<EmbeddedCacheManager, Integer> {
private final String serverName;
ConnectionAdderTask(String serverName) {
this.serverName = serverName;
}
@Override
public Integer apply(EmbeddedCacheManager embeddedCacheManager) {
ProtocolServer<?> protocolServer = SecurityActions.getGlobalComponentRegistry(embeddedCacheManager)
.getComponent(ProtocolServer.class, serverName);
// protocol server not registered; so no connections are open.
if (protocolServer == null) {
return 0;
}
Transport transport = protocolServer.getTransport();
// check if the transport is up; otherwise no connections are open
return transport == null ? 0 : transport.getNumberOfLocalConnections();
}
public static class Externalizer implements org.infinispan.commons.marshall.Externalizer<ConnectionAdderTask> {
@Override
public void writeObject(ObjectOutput output, ConnectionAdderTask task) throws IOException {
output.writeUTF(task.serverName);
}
@Override
public ConnectionAdderTask readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return new ConnectionAdderTask(input.readUTF());
}
}
}
public Integer getNumberOfLocalConnections() {
return acceptedChannels.size();
}
public Integer getNumberOfGlobalConnections() {
if (needDistributedCalculation()) {
return calculateGlobalConnections();
} else {
return getNumberOfLocalConnections();
}
}
}