diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/BoltRaftRpcFactory.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/BoltRaftRpcFactory.java index 35cea06b8..c839a3c08 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/BoltRaftRpcFactory.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/BoltRaftRpcFactory.java @@ -23,7 +23,6 @@ import com.alipay.sofa.jraft.rpc.RaftRpcFactory; import com.alipay.sofa.jraft.rpc.RpcClient; import com.alipay.sofa.jraft.rpc.RpcServer; -import com.alipay.sofa.jraft.rpc.impl.core.JRaftRpcAddressParser; import com.alipay.sofa.jraft.util.Endpoint; import com.alipay.sofa.jraft.util.Requires; import com.alipay.sofa.jraft.util.SPI; @@ -69,7 +68,6 @@ public ConfigHelper defaultJRaftClientConfigHelper(final RpcOptions o final InvokeContext ctx = new InvokeContext(); ctx.put(InvokeContext.BOLT_CRC_SWITCH, opts.isEnableRpcChecksum()); client.setDefaultInvokeCtx(ctx); - client.setDefaultAddressParser(new JRaftRpcAddressParser()); }; } } diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/BoltRpcClient.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/BoltRpcClient.java index 68884df9c..b8c82fa95 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/BoltRpcClient.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/BoltRpcClient.java @@ -21,9 +21,7 @@ import com.alipay.remoting.ConnectionEventType; import com.alipay.remoting.RejectedExecutionPolicy; -import com.alipay.remoting.Url; import com.alipay.remoting.config.switches.GlobalSwitch; -import com.alipay.remoting.rpc.RpcAddressParser; import com.alipay.sofa.jraft.ReplicatorGroup; import com.alipay.sofa.jraft.error.InvokeTimeoutException; import com.alipay.sofa.jraft.error.RemotingException; @@ -43,7 +41,6 @@ */ public class BoltRpcClient implements RpcClient { - public static final String BOLT_ADDRESS_PARSER = "BOLT_ADDRESS_PARSER"; public static final String BOLT_CTX = "BOLT_CTX"; public static final String BOLT_REJECTED_EXECUTION_POLICY = "BOLT_REJECTED_EXECUTION_POLICY"; @@ -58,7 +55,6 @@ public class BoltRpcClient implements RpcClient { private final com.alipay.remoting.rpc.RpcClient rpcClient; private com.alipay.remoting.InvokeContext defaultInvokeCtx; - private RpcAddressParser defaultAddressParser = new RpcAddressParser(); public BoltRpcClient(com.alipay.remoting.rpc.RpcClient rpcClient) { this.rpcClient = Requires.requireNonNull(rpcClient, "rpcClient"); @@ -99,10 +95,8 @@ public void registerConnectEventListener(final ReplicatorGroup replicatorGroup) public Object invokeSync(final Endpoint endpoint, final Object request, final InvokeContext ctx, final long timeoutMs) throws InterruptedException, RemotingException { Requires.requireNonNull(endpoint, "endpoint"); - final RpcAddressParser addressParser = getAddressParser(ctx); try { - final Url url = addressParser.parse(endpoint.toString()); - return this.rpcClient.invokeSync(url, request, getBoltInvokeCtx(ctx), (int) timeoutMs); + return this.rpcClient.invokeSync(endpoint.toString(), request, getBoltInvokeCtx(ctx), (int) timeoutMs); } catch (final com.alipay.remoting.rpc.exception.InvokeTimeoutException e) { throw new InvokeTimeoutException(e); } catch (final com.alipay.remoting.exception.RemotingException e) { @@ -115,11 +109,9 @@ public void invokeAsync(final Endpoint endpoint, final Object request, final Inv final InvokeCallback callback, final long timeoutMs) throws InterruptedException, RemotingException { Requires.requireNonNull(endpoint, "endpoint"); - final RpcAddressParser addressParser = getAddressParser(ctx); try { - final Url url = addressParser.parse(endpoint.toString()); - this.rpcClient.invokeWithCallback(url, request, getBoltInvokeCtx(ctx), getBoltCallback(callback, ctx), - (int) timeoutMs); + this.rpcClient.invokeWithCallback(endpoint.toString(), request, getBoltInvokeCtx(ctx), + getBoltCallback(callback, ctx), (int) timeoutMs); } catch (final com.alipay.remoting.rpc.exception.InvokeTimeoutException e) { throw new InvokeTimeoutException(e); } catch (final com.alipay.remoting.exception.RemotingException e) { @@ -139,19 +131,6 @@ public void setDefaultInvokeCtx(com.alipay.remoting.InvokeContext defaultInvokeC this.defaultInvokeCtx = defaultInvokeCtx; } - public RpcAddressParser getDefaultAddressParser() { - return defaultAddressParser; - } - - public void setDefaultAddressParser(RpcAddressParser defaultAddressParser) { - this.defaultAddressParser = defaultAddressParser; - } - - private RpcAddressParser getAddressParser(final InvokeContext ctx) { - return ctx == null ? this.defaultAddressParser : ctx.getOrDefault(BOLT_ADDRESS_PARSER, - this.defaultAddressParser); - } - private RejectedExecutionPolicy getRejectedPolicy(final InvokeContext ctx) { return ctx == null ? RejectedExecutionPolicy.CALLER_HANDLE_EXCEPTION : ctx.getOrDefault( BOLT_REJECTED_EXECUTION_POLICY, RejectedExecutionPolicy.CALLER_HANDLE_EXCEPTION); diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/core/ClientServiceConnectionEventProcessor.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/core/ClientServiceConnectionEventProcessor.java index 173219a4a..d83a60cc1 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/core/ClientServiceConnectionEventProcessor.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/core/ClientServiceConnectionEventProcessor.java @@ -44,11 +44,11 @@ public ClientServiceConnectionEventProcessor(ReplicatorGroup rgGroup) { } @Override - public void onEvent(String remoteAddr, Connection conn) { - PeerId peer = new PeerId(); + public void onEvent(final String remoteAddr, final Connection conn) { + final PeerId peer = new PeerId(); if (peer.parse(remoteAddr)) { LOG.info("Peer {} is connected", peer); - rgGroup.checkReplicator(peer, true); + this.rgGroup.checkReplicator(peer, true); } else { LOG.error("Fail to parse peer: {}", remoteAddr); } diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/core/JRaftRpcAddressParser.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/core/JRaftRpcAddressParser.java deleted file mode 100644 index 23d100b97..000000000 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/rpc/impl/core/JRaftRpcAddressParser.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.jraft.rpc.impl.core; - -import java.lang.ref.SoftReference; -import java.util.Properties; - -import com.alipay.remoting.RemotingAddressParser; -import com.alipay.remoting.Url; -import com.alipay.remoting.rpc.RpcAddressParser; -import com.alipay.remoting.util.StringUtils; -import com.alipay.sofa.jraft.util.Requires; - -/** - * @author jiachun.fjc - */ -public class JRaftRpcAddressParser extends RpcAddressParser { - - /** - * @see com.alipay.remoting.RemotingAddressParser#parse(java.lang.String) - */ - @Override - public Url parse(final String url) { - if (StringUtils.isBlank(url)) { - throw new IllegalArgumentException("Illegal format address string [" + url + "], should not be blank! "); - } - Url parsedUrl = tryGet(url); - if (parsedUrl != null) { - return parsedUrl; - } - String ip = null; - String port = null; - Properties properties = null; - - final int size = url.length(); - int pos = 0; - for (int i = 0; i < size; ++i) { - if (COLON == url.charAt(i)) { - ip = url.substring(pos, i); - pos = i; - // should not end with COLON - if (i == size - 1) { - throw new IllegalArgumentException("Illegal format address string [" + url - + "], should not end with COLON[:]! "); - } - break; - } - // must have one COLON - if (i == size - 1) { - throw new IllegalArgumentException("Illegal format address string [" + url - + "], must have one COLON[:]! "); - } - } - - for (int i = pos; i < size; ++i) { - if (QUES == url.charAt(i)) { - port = url.substring(pos + 1, i); - pos = i; - if (i == size - 1) { - // should not end with QUES - throw new IllegalArgumentException("Illegal format address string [" + url - + "], should not end with QUES[?]! "); - } - break; - } - // end without a QUES - if (i == size - 1) { - port = url.substring(pos + 1, i + 1); - pos = size; - } - } - - if (pos < (size - 1)) { - properties = new Properties(); - while (pos < (size - 1)) { - String key = null; - String value = null; - for (int i = pos; i < size; ++i) { - if (EQUAL == url.charAt(i)) { - key = url.substring(pos + 1, i); - pos = i; - if (i == size - 1) { - // should not end with EQUAL - throw new IllegalArgumentException("Illegal format address string [" + url - + "], should not end with EQUAL[=]! "); - } - break; - } - if (i == size - 1) { - // must have one EQUAL - throw new IllegalArgumentException("Illegal format address string [" + url - + "], must have one EQUAL[=]! "); - } - } - for (int i = pos; i < size; ++i) { - if (AND == url.charAt(i)) { - value = url.substring(pos + 1, i); - pos = i; - if (i == size - 1) { - // should not end with AND - throw new IllegalArgumentException("Illegal format address string [" + url - + "], should not end with AND[&]! "); - } - break; - } - // end without more AND - if (i == size - 1) { - value = url.substring(pos + 1, i + 1); - pos = size; - } - } - if (key != null && value != null) { - properties.put(key, value); - } - } - } - Requires.requireNonNull(port, "port is null"); - // isolated the bolt connection pool - // uniqueKey = ip:port:jraft - final String uniqueKey = ip + RemotingAddressParser.COLON + port + RemotingAddressParser.COLON + "jraft"; - parsedUrl = new Url(url, ip, Integer.parseInt(port), uniqueKey, properties); - initUrlArgs(parsedUrl); - Url.parsedUrls.put(url, new SoftReference<>(parsedUrl)); - return parsedUrl; - } - - /** - * try get from cache - */ - private Url tryGet(final String url) { - final SoftReference softRef = Url.parsedUrls.get(url); - return (softRef == null) ? null : softRef.get(); - } -}