Skip to content

Commit

Permalink
Merge pull request #18 from apache/master
Browse files Browse the repository at this point in the history
Update the latest code
  • Loading branch information
CrazyHZM committed Dec 11, 2018
2 parents f4da633 + 795a840 commit fc59784
Show file tree
Hide file tree
Showing 60 changed files with 652 additions and 172 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public interface GreetingService {
}
```

*See [api/GreetingService.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/com/alibaba/dubbo/samples/api/GreetingsService.java) on GitHub.*
*See [api/GreetingService.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/api/GreetingsService.java) on GitHub.*

### Implement service interface for the provider

Expand All @@ -73,7 +73,7 @@ public class GreetingServiceImpl implements GreetingService {
}
```

*See [provider/GreetingServiceImpl.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/com/alibaba/dubbo/samples/server/GreetingsServiceImpl.java) on GitHub.*
*See [provider/GreetingServiceImpl.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.java) on GitHub.*

### Start service provider

Expand Down Expand Up @@ -101,7 +101,7 @@ public class Application {
}
```

*See [provider/Application.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/com/alibaba/dubbo/samples/provider/Application.java) on GitHub.*
*See [provider/Application.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/provider/Application.java) on GitHub.*

### Build and run the provider

Expand Down Expand Up @@ -141,7 +141,7 @@ public class Application {

The consumer will print out `Hello world` on the screen.

*See [consumer/Application.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/com/alibaba/dubbo/samples/consumer/Application.java) on GitHub.*
*See [consumer/Application.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/org/apache/dubbo/samples/consumer/Application.java) on GitHub.*

### Next steps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,31 @@ public interface Router extends Comparable<Router> {
*/
<T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException;

/**
* priority
*
* @return
*/
int getPriority();

/**
* compare Router
*
* @param o
* @return
*/
@Override
default int compareTo(Router o) {
if (o == null) {
throw new IllegalArgumentException();
}
if (this.getPriority() == o.getPriority()) {
if (o.getUrl() == null) {
return -1;
}
return getUrl().toFullString().compareTo(o.getUrl().toFullString());
} else {
return getPriority() > o.getPriority() ? 1 : -1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
weightedRoundRobin = new WeightedRoundRobin();
weightedRoundRobin.setWeight(weight);
map.putIfAbsent(identifyString, weightedRoundRobin);
weightedRoundRobin = map.get(identifyString);
}
if (weight != weightedRoundRobin.getWeight()) {
//weight changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public <T> List<Invoker<T>> route(final List<Invoker<T>> invokers,
return invokers;
}

@Override
public int getPriority() {
return Integer.MAX_VALUE;
}

private <T> List<Invoker<T>> getMockedInvokers(final List<Invoker<T>> invokers) {
if (!hasMockProviders(invokers)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

/**
* ConditionRouter
*
*/
public class ConditionRouter implements Router {

Expand Down Expand Up @@ -178,17 +177,13 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
}

@Override
public URL getUrl() {
return url;
public int getPriority() {
return priority;
}

@Override
public int compareTo(Router o) {
if (o == null || o.getClass() != ConditionRouter.class) {
return 1;
}
ConditionRouter c = (ConditionRouter) o;
return this.priority == c.priority ? url.toFullString().compareTo(c.url.toFullString()) : (this.priority > c.priority ? 1 : -1);
public URL getUrl() {
return url;
}

boolean matchWhen(URL url, Invocation invocation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

/**
* ScriptRouter
*
*/
public class ScriptRouter implements Router {

Expand Down Expand Up @@ -114,13 +113,24 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
}
}

@Override
public int getPriority() {
return priority;
}

@Override
public int compareTo(Router o) {
if (o == null || o.getClass() != ScriptRouter.class) {
return 1;
if (o == null) {
throw new IllegalArgumentException();
}
if (this.priority == o.getPriority()) {
if (o instanceof ScriptRouter) {
ScriptRouter c = (ScriptRouter) o;
return rule.compareTo(c.rule);
}
return 0;
} else {
return this.priority > o.getPriority() ? 1 : -1;
}
ScriptRouter c = (ScriptRouter) o;
return this.priority == c.priority ? rule.compareTo(c.rule) : (this.priority > c.priority ? 1 : -1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,10 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
result.add(invoker);
}
}
// If no invoker be selected, downgrade to normal invokers
if (result.isEmpty()) {
for (Invoker<T> invoker : invokers) {
if (StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY))) {
result.add(invoker);
}
}
}
// Normal request
} else {
}
// If Constants.REQUEST_TAG_KEY unspecified or no invoker be selected, downgrade to normal invokers
if (result.isEmpty()) {
for (Invoker<T> invoker : invokers) {
// Can't access tag invoker,only normal invoker should be selected
if (StringUtils.isEmpty(invoker.getUrl().getParameter(Constants.TAG_KEY))) {
result.add(invoker);
}
Expand All @@ -98,11 +90,7 @@ public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation
}

@Override
public int compareTo(Router o) {
if (o == null || o.getClass() != TagRouter.class) {
return 1;
}
TagRouter c = (TagRouter) o;
return this.priority == c.priority ? url.toFullString().compareTo(c.url.toFullString()) : (this.priority > c.priority ? 1 : -1);
public int getPriority() {
return priority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
Expand All @@ -44,8 +45,8 @@
*/
public abstract class AbstractClusterInvoker<T> implements Invoker<T> {

private static final Logger logger = LoggerFactory
.getLogger(AbstractClusterInvoker.class);
private static final Logger logger = LoggerFactory.getLogger(AbstractClusterInvoker.class);

protected final Directory<T> directory;

protected final boolean availablecheck;
Expand Down Expand Up @@ -110,25 +111,28 @@ public void destroy() {
* @return the invoker which will final to do invoke.
* @throws RpcException
*/
protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
if (invokers == null || invokers.isEmpty()) {
protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation,
List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {

if (CollectionUtils.isEmpty(invokers)) {
return null;
}
String methodName = invocation == null ? "" : invocation.getMethodName();
String methodName = invocation == null ? StringUtils.EMPTY : invocation.getMethodName();

boolean sticky = invokers.get(0).getUrl().getMethodParameter(methodName, Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY);
{
//ignore overloaded method
if (stickyInvoker != null && !invokers.contains(stickyInvoker)) {
stickyInvoker = null;
}
//ignore concurrency problem
if (sticky && stickyInvoker != null && (selected == null || !selected.contains(stickyInvoker))) {
if (availablecheck && stickyInvoker.isAvailable()) {
return stickyInvoker;
}
boolean sticky = invokers.get(0).getUrl()
.getMethodParameter(methodName, Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY);

//ignore overloaded method
if (stickyInvoker != null && !invokers.contains(stickyInvoker)) {
stickyInvoker = null;
}
//ignore concurrency problem
if (sticky && stickyInvoker != null && (selected == null || !selected.contains(stickyInvoker))) {
if (availablecheck && stickyInvoker.isAvailable()) {
return stickyInvoker;
}
}

Invoker<T> invoker = doSelect(loadbalance, invocation, invokers, selected);

if (sticky) {
Expand All @@ -137,8 +141,10 @@ protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation, List
return invoker;
}

private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
if (invokers == null || invokers.isEmpty()) {
private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation,
List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {

if (CollectionUtils.isEmpty(invokers)) {
return null;
}
if (invokers.size() == 1) {
Expand All @@ -158,7 +164,7 @@ private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation, List
int index = invokers.indexOf(invoker);
try {
//Avoid collision
invoker = index < invokers.size() - 1 ? invokers.get(index + 1) : invokers.get(0);
invoker = invokers.get((index + 1) % invokers.size());
} catch (Exception e) {
logger.warn(e.getMessage() + " may because invokers list dynamic change, ignore.", e);
}
Expand All @@ -171,7 +177,8 @@ private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation, List
}

/**
* Reselect, use invokers not in `selected` first, if all invokers are in `selected`, just pick an available one using loadbalance policy.
* Reselect, use invokers not in `selected` first, if all invokers are in `selected`,
* just pick an available one using loadbalance policy.
*
* @param loadbalance
* @param invocation
Expand All @@ -181,48 +188,40 @@ private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation, List
* @throws RpcException
*/
private Invoker<T> reselect(LoadBalance loadbalance, Invocation invocation,
List<Invoker<T>> invokers, List<Invoker<T>> selected, boolean availablecheck)
throws RpcException {
List<Invoker<T>> invokers, List<Invoker<T>> selected, boolean availablecheck) throws RpcException {

//Allocating one in advance, this list is certain to be used.
List<Invoker<T>> reselectInvokers = new ArrayList<Invoker<T>>(invokers.size() > 1 ? (invokers.size() - 1) : invokers.size());

//First, try picking a invoker not in `selected`.
if (availablecheck) { // invoker.isAvailable() should be checked
for (Invoker<T> invoker : invokers) {
if (invoker.isAvailable()) {
if (selected == null || !selected.contains(invoker)) {
reselectInvokers.add(invoker);
}
}
}
if (!reselectInvokers.isEmpty()) {
return loadbalance.select(reselectInvokers, getUrl(), invocation);
}
} else { // do not check invoker.isAvailable()
for (Invoker<T> invoker : invokers) {
if (selected == null || !selected.contains(invoker)) {
reselectInvokers.add(invoker);
}
List<Invoker<T>> reselectInvokers = new ArrayList<>(
invokers.size() > 1 ? (invokers.size() - 1) : invokers.size());

// First, try picking a invoker not in `selected`.
for (Invoker<T> invoker : invokers) {
if (availablecheck && !invoker.isAvailable()) {
continue;
}
if (!reselectInvokers.isEmpty()) {
return loadbalance.select(reselectInvokers, getUrl(), invocation);

if (selected == null || !selected.contains(invoker)) {
reselectInvokers.add(invoker);
}
}

if (!reselectInvokers.isEmpty()) {
return loadbalance.select(reselectInvokers, getUrl(), invocation);
}

// Just pick an available invoker using loadbalance policy
{
if (selected != null) {
for (Invoker<T> invoker : selected) {
if ((invoker.isAvailable()) // available first
&& !reselectInvokers.contains(invoker)) {
reselectInvokers.add(invoker);
}
if (selected != null) {
for (Invoker<T> invoker : selected) {
if ((invoker.isAvailable()) // available first
&& !reselectInvokers.contains(invoker)) {
reselectInvokers.add(invoker);
}
}
if (!reselectInvokers.isEmpty()) {
return loadbalance.select(reselectInvokers, getUrl(), invocation);
}
}
if (!reselectInvokers.isEmpty()) {
return loadbalance.select(reselectInvokers, getUrl(), invocation);
}

return null;
}

Expand Down Expand Up @@ -257,7 +256,7 @@ public String toString() {
}

protected void checkInvokers(List<Invoker<T>> invokers, Invocation invocation) {
if (invokers == null || invokers.isEmpty()) {
if (CollectionUtils.isEmpty(invokers)) {
throw new RpcException("Failed to invoke the method "
+ invocation.getMethodName() + " in the service " + getInterface().getName()
+ ". No provider available for the service " + directory.getUrl().getServiceKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public FailbackClusterInvoker(Directory<T> directory) {
super(directory);
}

private void addFailed(Invocation invocation, AbstractClusterInvoker<?> router) {
private void addFailed(Invocation invocation, AbstractClusterInvoker<?> invoker) {
if (retryFuture == null) {
synchronized (this) {
if (retryFuture == null) {
Expand All @@ -84,11 +84,11 @@ public void run() {
}
}
}
failed.put(invocation, router);
failed.put(invocation, invoker);
}

void retryFailed() {
if (failed.size() == 0) {
if (failed.isEmpty()) {
return;
}
for (Map.Entry<Invocation, AbstractClusterInvoker<?>> entry : new HashMap<>(failed).entrySet()) {
Expand Down
Loading

0 comments on commit fc59784

Please sign in to comment.