You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "MieAh (via GitHub)" <gi...@apache.org> on 2023/03/03 06:23:56 UTC

[GitHub] [dubbo] MieAh commented on a diff in pull request #11531: Support choose local invoke in runtime instead of startup

MieAh commented on code in PR #11531:
URL: https://github.com/apache/dubbo/pull/11531#discussion_r1124068240


##########
dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvoker.java:
##########
@@ -0,0 +1,191 @@
+/*
+ * 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 org.apache.dubbo.rpc.cluster.support.wrapper;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.url.component.ServiceConfigURL;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.rpc.Exporter;
+import org.apache.dubbo.rpc.ExporterListener;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.Protocol;
+import org.apache.dubbo.rpc.Result;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.cluster.ClusterInvoker;
+import org.apache.dubbo.rpc.cluster.Directory;
+import org.apache.dubbo.rpc.listener.ExporterChangeListener;
+import org.apache.dubbo.rpc.listener.InjvmExporterListener;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
+import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL;
+import static org.apache.dubbo.rpc.Constants.SCOPE_KEY;
+import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE;
+import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL;
+import static org.apache.dubbo.rpc.cluster.Constants.PEER_KEY;
+
+/**
+ * A ClusterInvoker that selects between local and remote invokers at runtime.
+ */
+public class ScopeClusterInvoker<T> implements ClusterInvoker<T>, ExporterChangeListener {
+
+    private Protocol protocolSPI;
+    private final Directory<T> directory;
+    private final Invoker<T> invoker;
+    private final AtomicBoolean isExported;
+    private volatile Invoker<T> injvmInvoker;
+    private volatile InjvmExporterListener injvmExporterListener;
+
+    private final Object createLock = new Object();
+
+
+    public ScopeClusterInvoker(Directory<T> directory, Invoker<T> invoker) {
+        this.directory = directory;
+        this.invoker = invoker;
+        this.isExported = new AtomicBoolean(false);
+        init();
+    }
+
+    private void init() {
+        if (injvmInvoker == null && LOCAL_PROTOCOL.equals(getRegistryUrl().getProtocol())) {
+            isExported.compareAndSet(false, true);
+            injvmInvoker = invoker;
+        }
+        protocolSPI = getUrl().getApplicationModel().getExtensionLoader(Protocol.class).getAdaptiveExtension();
+        injvmExporterListener = (InjvmExporterListener) getUrl().getApplicationModel().getExtensionLoader(ExporterListener.class).getExtension("injvm");
+        injvmExporterListener.addExporterChangeListener(this, getUrl().getServiceKey());
+    }
+
+    @Override
+    public URL getUrl() {
+        return directory.getConsumerUrl();
+    }
+
+    @Override
+    public URL getRegistryUrl() {
+        return directory.getUrl();
+    }
+
+    @Override
+    public Directory<T> getDirectory() {
+        return directory;
+    }
+
+    @Override
+    public boolean isDestroyed() {
+        return directory.isDestroyed();
+    }
+
+    @Override
+    public boolean isAvailable() {
+        if (injvmExporterListener == null) {
+            injvmExporterListener = (InjvmExporterListener) getUrl().getApplicationModel().getExtensionLoader(ExporterListener.class).getExtension(LOCAL_PROTOCOL);
+        }
+        injvmExporterListener.addExporterChangeListener(this, getUrl().getServiceKey());
+        return isExported.get() || directory.isAvailable();
+    }
+
+    @Override
+    public void destroy() {
+        if (injvmExporterListener != null) {
+            injvmExporterListener.removeExporterChangeListener(getUrl().getServiceKey());
+        }
+        this.invoker.destroy();
+    }
+
+    @Override
+    public Class<T> getInterface() {
+        return directory.getInterface();
+    }
+
+    @Override
+    public Result invoke(Invocation invocation) throws RpcException {
+        Boolean peer = (Boolean) getUrl().getAttribute(PEER_KEY);
+        if (peer != null && peer) {
+            return invoker.invoke(invocation);
+        }
+        String scope = getUrl().getParameter(SCOPE_KEY);
+        if (shouldInvokeInjvm(getUrl().getParameter(LOCAL_PROTOCOL), scope)) {

Review Comment:
   done



##########
dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/wrapper/ScopeClusterInvoker.java:
##########
@@ -0,0 +1,191 @@
+/*
+ * 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 org.apache.dubbo.rpc.cluster.support.wrapper;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.url.component.ServiceConfigURL;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.rpc.Exporter;
+import org.apache.dubbo.rpc.ExporterListener;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.Protocol;
+import org.apache.dubbo.rpc.Result;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.cluster.ClusterInvoker;
+import org.apache.dubbo.rpc.cluster.Directory;
+import org.apache.dubbo.rpc.listener.ExporterChangeListener;
+import org.apache.dubbo.rpc.listener.InjvmExporterListener;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
+import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL;
+import static org.apache.dubbo.rpc.Constants.SCOPE_KEY;
+import static org.apache.dubbo.rpc.Constants.SCOPE_REMOTE;
+import static org.apache.dubbo.rpc.Constants.SCOPE_LOCAL;
+import static org.apache.dubbo.rpc.cluster.Constants.PEER_KEY;
+
+/**
+ * A ClusterInvoker that selects between local and remote invokers at runtime.
+ */
+public class ScopeClusterInvoker<T> implements ClusterInvoker<T>, ExporterChangeListener {
+
+    private Protocol protocolSPI;
+    private final Directory<T> directory;
+    private final Invoker<T> invoker;
+    private final AtomicBoolean isExported;
+    private volatile Invoker<T> injvmInvoker;
+    private volatile InjvmExporterListener injvmExporterListener;
+
+    private final Object createLock = new Object();
+
+
+    public ScopeClusterInvoker(Directory<T> directory, Invoker<T> invoker) {
+        this.directory = directory;
+        this.invoker = invoker;
+        this.isExported = new AtomicBoolean(false);
+        init();
+    }
+
+    private void init() {
+        if (injvmInvoker == null && LOCAL_PROTOCOL.equals(getRegistryUrl().getProtocol())) {
+            isExported.compareAndSet(false, true);
+            injvmInvoker = invoker;
+        }
+        protocolSPI = getUrl().getApplicationModel().getExtensionLoader(Protocol.class).getAdaptiveExtension();
+        injvmExporterListener = (InjvmExporterListener) getUrl().getApplicationModel().getExtensionLoader(ExporterListener.class).getExtension("injvm");
+        injvmExporterListener.addExporterChangeListener(this, getUrl().getServiceKey());
+    }
+
+    @Override
+    public URL getUrl() {
+        return directory.getConsumerUrl();
+    }
+
+    @Override
+    public URL getRegistryUrl() {
+        return directory.getUrl();
+    }
+
+    @Override
+    public Directory<T> getDirectory() {
+        return directory;
+    }
+
+    @Override
+    public boolean isDestroyed() {
+        return directory.isDestroyed();
+    }
+
+    @Override
+    public boolean isAvailable() {
+        if (injvmExporterListener == null) {
+            injvmExporterListener = (InjvmExporterListener) getUrl().getApplicationModel().getExtensionLoader(ExporterListener.class).getExtension(LOCAL_PROTOCOL);
+        }
+        injvmExporterListener.addExporterChangeListener(this, getUrl().getServiceKey());
+        return isExported.get() || directory.isAvailable();
+    }
+
+    @Override
+    public void destroy() {
+        if (injvmExporterListener != null) {
+            injvmExporterListener.removeExporterChangeListener(getUrl().getServiceKey());
+        }
+        this.invoker.destroy();
+    }
+
+    @Override
+    public Class<T> getInterface() {
+        return directory.getInterface();
+    }
+
+    @Override
+    public Result invoke(Invocation invocation) throws RpcException {
+        Boolean peer = (Boolean) getUrl().getAttribute(PEER_KEY);
+        if (peer != null && peer) {
+            return invoker.invoke(invocation);
+        }
+        String scope = getUrl().getParameter(SCOPE_KEY);
+        if (shouldInvokeInjvm(getUrl().getParameter(LOCAL_PROTOCOL), scope)) {
+            return injvmInvoker.invoke(invocation);
+        }
+        return invoker.invoke(invocation);
+    }
+
+    private boolean shouldInvokeInjvm(String isInjvm, String scope) {
+        if (Boolean.TRUE.toString().equals(isInjvm)) {

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org