Skip to content

Commit

Permalink
imisc:add server status and fix server confilct
Browse files Browse the repository at this point in the history
  • Loading branch information
lindzh committed Dec 12, 2016
1 parent 94b2766 commit 0543b6a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
15 changes: 7 additions & 8 deletions src/main/java/com/linda/rpc/webui/service/RpcFetchService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.linda.rpc.webui.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -63,9 +59,9 @@ private void fireServerListeners(RpcConfig config,List<RpcHostAndPort> host){
}
}

private void fireServicesListeners(RpcConfig config,RpcHostAndPort host,List<RpcService> services){
private void fireServicesListeners(RpcConfig config,Map<RpcHostAndPort, List<RpcService>> hashMap){
for(RpcInfoListener listener:this.infoListeners){
listener.onServices(config, host, services);
listener.onServices(config, hashMap);
}
}

Expand All @@ -91,6 +87,7 @@ public void run() {
if(adminService==null){
RpcFetchService.this.initConfig(config);
}else{
HashMap<RpcHostAndPort, List<RpcService>> hashMap = new HashMap<RpcHostAndPort, List<RpcService>>();
List<RpcHostAndPort> servers = adminService.getRpcServers();
if(servers!=null){
logger.info("fetchServer config:"+JSONUtils.toJSON(config)+
Expand All @@ -104,8 +101,10 @@ public void run() {
" server:"+JSONUtils.toJSON(server)+
" services:"+JSONUtils.toJSON(services));
}
RpcFetchService.this.fireServicesListeners(config, server, services);
hashMap.put(server,services);
}

RpcFetchService.this.fireServicesListeners(config, hashMap);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.linda.rpc.webui.service;

import java.util.List;
import java.util.Map;

import com.linda.framework.rpc.RpcService;
import com.linda.framework.rpc.cluster.RpcHostAndPort;
Expand All @@ -9,6 +10,6 @@ public interface RpcInfoListener {

public void onServers(RpcConfig config,List<RpcHostAndPort> host);

public void onServices(RpcConfig config,RpcHostAndPort host,List<RpcService> services);
public void onServices(RpcConfig config,Map<RpcHostAndPort,List<RpcService>> hostServices);

}
50 changes: 28 additions & 22 deletions src/main/java/com/linda/rpc/webui/service/RpcWebuiServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.linda.rpc.webui.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
Expand All @@ -22,13 +17,13 @@ public class RpcWebuiServiceImpl implements RpcWebuiService,RpcInfoListener{

private ConcurrentHashMap<String, Set<RpcService>> namespaceServices = new ConcurrentHashMap<String, Set<RpcService>>();

private ConcurrentHashMap<String,Set<RpcHostAndPort>> namespaceServiceHosts = new ConcurrentHashMap<String,Set<RpcHostAndPort>>();
private HashMap<String,Set<RpcHostAndPort>> namespaceServiceHosts = new HashMap<String,Set<RpcHostAndPort>>();

private ConcurrentHashMap<String,List<RpcHostAndPort>> configProvidersCache = new ConcurrentHashMap<String,List<RpcHostAndPort>>();

private ReadWriteLock readwriteLock = new ReentrantReadWriteLock(false);

private ConcurrentHashMap<String,Set<RpcService>> namespaceHostServicesCache = new ConcurrentHashMap<String,Set<RpcService>>();
private HashMap<String,Set<RpcService>> namespaceHostServicesCache = new HashMap<String,Set<RpcService>>();

@Override
public void onServers(RpcConfig config, List<RpcHostAndPort> hosts) {
Expand All @@ -45,7 +40,7 @@ public void onServers(RpcConfig config, List<RpcHostAndPort> hosts) {
}

@Override
public void onServices(RpcConfig config, RpcHostAndPort host, List<RpcService> services) {
public void onServices(RpcConfig config,Map<RpcHostAndPort,List<RpcService>> hostServiceMap) {
Lock lock = readwriteLock.writeLock();
try{
lock.lock();
Expand All @@ -56,21 +51,32 @@ public void onServices(RpcConfig config, RpcHostAndPort host, List<RpcService> s
namespaceServices.put(md5, new HashSet<RpcService>());
set = namespaceServices.get(md5);
}
set.addAll(services);

//name space service host list
for(RpcService service:services){
String genKey = this.genKey(md5, service.getName(), service.getVersion());
Set<RpcHostAndPort> hosts = namespaceServiceHosts.get(genKey);
if(hosts==null){
hosts = new HashSet<RpcHostAndPort>();
namespaceServiceHosts.put(genKey, hosts);

HashMap<String, Set<RpcHostAndPort>> serviceHostsMap = new HashMap<String, Set<RpcHostAndPort>>();
HashMap<String, Set<RpcService>> hostServicesMap = new HashMap<String, Set<RpcService>>();

Set<RpcHostAndPort> hostAndPorts = hostServiceMap.keySet();
for(RpcHostAndPort hostAndPort: hostAndPorts){
set.addAll(hostServiceMap.get(hostAndPort));

List<RpcService> rpcServices = hostServiceMap.get(hostAndPort);

String servicesKey = this.genhostServicesKey(md5, hostAndPort.getHost()+":"+hostAndPort.getPort());
hostServicesMap.put(servicesKey,new HashSet<RpcService>(rpcServices));

for(RpcService service :rpcServices){
String genKey = this.genKey(md5, service.getName(), service.getVersion());
Set<RpcHostAndPort> hosts = serviceHostsMap.get(genKey);
if(hosts==null){
hosts = new HashSet<RpcHostAndPort>();
serviceHostsMap.put(genKey, hosts);
}
hosts.add(hostAndPort);
}
hosts.add(host);
}
//host service key
String servicesKey = this.genhostServicesKey(md5, host.getHost()+":"+host.getPort());
namespaceHostServicesCache.put(servicesKey, set);

namespaceServiceHosts = serviceHostsMap;
namespaceHostServicesCache = hostServicesMap;
}finally{
lock.unlock();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/webapp/WEB-INF/template/hosts.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<th>port</th>
<th>time</th>
<th>token</th>
<th>status</th>
<th>services</th>
</tr>
</thead>
Expand All @@ -47,6 +48,7 @@
<td>${host.port}</td>
<td>${parseDate(host.time)}</td>
<td>${host.token}</td>
<td>online</td>
<td><a href="/webui/host/services?namespace=${namespace}&hostAndPort=${host.host}:${host.port}">services</a></td>
</tr>
</#list>
Expand Down
2 changes: 2 additions & 0 deletions src/main/webapp/WEB-INF/template/service_hosts.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<th>port</th>
<th>time</th>
<th>token</th>
<th>status</th>
</tr>
</thead>
<tbody>
Expand All @@ -55,6 +56,7 @@
<td>${host.port}</td>
<td>${parseDate(host.time)}</td>
<td>${host.token}</td>
<td>online</td>
</tr>
</#list>
</tbody>
Expand Down

0 comments on commit 0543b6a

Please sign in to comment.