Skip to content

Commit

Permalink
counting provider wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
linanmiao committed Dec 4, 2018
1 parent 5fb4c89 commit a986fa6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
37 changes: 35 additions & 2 deletions cobia-core/src/main/java/lam/cobia/rpc/CountingProvider.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package lam.cobia.rpc;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import lam.cobia.core.NotNegativeLong;
import lam.cobia.core.exception.CobiaException;
import lam.cobia.core.util.concurrent.ThreadFactoryBuilder;
import lam.cobia.rpc.chain.ProviderChainWrapper;
import lam.cobia.rpc.support.Invocation;
import lam.cobia.rpc.support.Provider;
Expand All @@ -15,14 +20,37 @@
*/
public class CountingProvider<T> extends ProviderChainWrapper {

private static final Logger LOGGER = LoggerFactory.getLogger(CountingProvider.class);

private static final long ONE_MINUTE_SECOND = 60;

private static final int CORE_POOL_SIZE = 1;

private static final ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(
CORE_POOL_SIZE,
new ThreadFactoryBuilder().setThreadNamePrefix("CountingProvider").build(),
new ThreadPoolExecutor.AbortPolicy()
);

final NotNegativeLong invokedCount = new NotNegativeLong(0);

public CountingProvider(Provider<T> provider) {
super(provider, null);
start();
}

public CountingProvider(Provider<T> provider, ProviderChain next) {
super(provider, next);
start();
}

private void start() {
scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> {
LOGGER.debug("collect provider:{} invoking count:{}", getKey(), invokedCount.get());
},
ONE_MINUTE_SECOND,
ONE_MINUTE_SECOND,
TimeUnit.SECONDS);
}

@Override
Expand All @@ -35,4 +63,9 @@ public Result invoke(Invocation invocation) {
}
}

@Override
public void close() {
super.close();
scheduledThreadPoolExecutor.shutdown();
}
}
4 changes: 3 additions & 1 deletion cobia-core/src/main/java/lam/cobia/rpc/DefaultProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lam.cobia.remoting.HeaderExchangeServer;
import lam.cobia.remoting.transport.netty.NettyClient;
import lam.cobia.remoting.transport.netty.NettyServer;
import lam.cobia.rpc.chain.ProviderChainWrapper;
import lam.cobia.rpc.support.Consumer;
import lam.cobia.rpc.support.Exporter;
import lam.cobia.rpc.support.Protocol;
Expand Down Expand Up @@ -66,8 +67,9 @@ public DefaultProtocol() {

@Override
public <T> Exporter<T> export(Provider<T> provider, Map<String, Object> params) {
ProviderChainWrapper<T> providerChainWrapper = new CountingProvider<T>(provider);

DefaultExporter<T> exporter = new DefaultExporter<T>(provider);
DefaultExporter<T> exporter = new DefaultExporter<T>(providerChainWrapper);

exporterMap.put(provider.getKey(), exporter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Class<T> getInterface() {
@Override
public Result invoke(Invocation invocation) {
if (hasNext()) {
return this.next.invoke(provider, invocation);
return this.next().invoke(provider, invocation);
}
return this.provider.invoke(invocation);
}
Expand All @@ -43,6 +43,10 @@ public boolean hasNext() {
return this.next != null;
}

public ProviderChain next() {
return this.next;
}

@Override
public void close() {
this.provider.close();
Expand Down
2 changes: 1 addition & 1 deletion cobia-core/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
log4j.rootLogger = info, stdout, D
log4j.rootLogger = debug, stdout, D

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
Expand Down

0 comments on commit a986fa6

Please sign in to comment.