This is an automated email from the ASF dual-hosted git repository.

daradurvs pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new f5e5bfc  IGNITE-12894 Added waiting for service's deployment finished 
when service proxy requested. (#7771)
f5e5bfc is described below

commit f5e5bfc3663c12c322f3b514ab62e2e3c32c73a4
Author: Mikhail Petrov <[email protected]>
AuthorDate: Fri May 15 22:33:59 2020 +0300

    IGNITE-12894 Added waiting for service's deployment finished when service 
proxy requested. (#7771)
---
 .../processors/service/IgniteServiceProcessor.java |  36 ++-
 .../internal/processors/service/ServiceInfo.java   |  13 +-
 ...GridServiceProxyTopologyInitializationTest.java | 249 +++++++++++++++++++++
 .../IgniteServiceProxyTimeoutInitializedTest.java  |  33 +++
 .../testsuites/IgniteServiceGridTestSuite.java     |   2 +
 5 files changed, 310 insertions(+), 23 deletions(-)

diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/IgniteServiceProcessor.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/IgniteServiceProcessor.java
index 7a1ffd5..8fc0616 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/IgniteServiceProcessor.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/IgniteServiceProcessor.java
@@ -823,19 +823,26 @@ public class IgniteServiceProcessor extends 
ServiceProcessorAdapter implements I
 
         long startTime = U.currentTimeMillis();
 
-        Map<UUID, Integer> top;
+        ServiceInfo desc;
 
         while (true) {
-            top = serviceTopology(name);
+            synchronized (servicesTopsUpdateMux) {
+                desc = lookupInRegisteredServices(name);
+
+                if (timeout == 0 && desc == null)
+                    return null;
 
-            if (timeout == 0 || (top != null && !top.isEmpty()))
-                return top;
+                if (desc != null && desc.topologyInitialized())
+                    return desc.topologySnapshot();
 
-            synchronized (servicesTopsUpdateMux) {
-                long wait = timeout - (U.currentTimeMillis() - startTime);
+                long wait = 0;
 
-                if (wait <= 0)
-                    return top;
+                if (timeout != 0) {
+                    wait = timeout - (U.currentTimeMillis() - startTime);
+
+                    if (wait <= 0)
+                        return desc == null ? null : desc.topologySnapshot();
+                }
 
                 try {
                     servicesTopsUpdateMux.wait(wait);
@@ -847,19 +854,6 @@ public class IgniteServiceProcessor extends 
ServiceProcessorAdapter implements I
         }
     }
 
-    /**
-     * @param name Service name.
-     * @return Service topology.
-     */
-    private Map<UUID, Integer> serviceTopology(String name) {
-        for (ServiceInfo desc : registeredServices.values()) {
-            if (desc.name().equals(name))
-                return desc.topologySnapshot();
-        }
-
-        return null;
-    }
-
     /** {@inheritDoc} */
     @Override public Collection<ServiceDescriptor> serviceDescriptors() {
         return new ArrayList<>(registeredServices.values());
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/ServiceInfo.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/ServiceInfo.java
index 5a3aefc..2360eda 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/ServiceInfo.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/ServiceInfo.java
@@ -51,7 +51,7 @@ public class ServiceInfo implements ServiceDescriptor {
 
     /** Topology snapshot. */
     @GridToStringInclude
-    private volatile Map<UUID, Integer> top = Collections.emptyMap();
+    private volatile Map<UUID, Integer> top;
 
     /**
      * @param originNodeId Initiating node id.
@@ -160,7 +160,16 @@ public class ServiceInfo implements ServiceDescriptor {
 
     /** {@inheritDoc} */
     @Override public Map<UUID, Integer> topologySnapshot() {
-        return Collections.unmodifiableMap(top);
+        return top == null ? Collections.emptyMap() : 
Collections.unmodifiableMap(top);
+    }
+
+    /**
+     * Whether service topology was initialized.
+     *
+     * @return {@code True} if service topology was initialized.
+     */
+    public boolean topologyInitialized() {
+        return top != null;
     }
 
     /** {@inheritDoc} */
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/service/GridServiceProxyTopologyInitializationTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/service/GridServiceProxyTopologyInitializationTest.java
new file mode 100644
index 0000000..7e1f026
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/service/GridServiceProxyTopologyInitializationTest.java
@@ -0,0 +1,249 @@
+/*
+ * 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.ignite.internal.processors.service;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.concurrent.CountDownLatch;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.services.Service;
+import org.apache.ignite.services.ServiceConfiguration;
+import org.apache.ignite.services.ServiceContext;
+import org.apache.ignite.services.ServiceDeploymentException;
+import org.apache.ignite.spi.discovery.tcp.TestTcpDiscoverySpi;
+import org.apache.ignite.testframework.GridTestUtils.DiscoveryHook;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Assume;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static 
org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause;
+import static org.apache.ignite.testframework.GridTestUtils.runAsync;
+
+/**
+ * Tests service invocation if requested service is not deployed on the local 
node and the service topology is required.
+ */
+public class GridServiceProxyTopologyInitializationTest extends 
GridCommonAbstractTest {
+    /** Number of the test nodes. */
+    private static final int NODES_CNT = 2;
+
+    /** Name of the service that throws exception during initialization. */
+    private static final String BROKEN_SRVC = "broken-service";
+
+    /** Name of the decent service. */
+    private static final String DECENT_SRVC = "decent-service";
+
+    /** Name of the attribute that shows whether test servise deployment will 
be skipped on the node. */
+    private static final String ATTR_SKIP_DEPLOYMENT = "skip-deployment";
+
+    /** Latch that indicates whether {@link 
ServiceSingleNodeDeploymentResultBatch} execution should be proceeded. */
+    private final CountDownLatch fullMsgUnblockedLatch = new CountDownLatch(1);
+
+    /** Latch that indicated whether {@link 
ServiceSingleNodeDeploymentResultBatch} was received on the remote node. */
+    private final CountDownLatch fullMsgReceivedLatch = new CountDownLatch(1);
+
+    /** Latch that indicated whether {@link 
ServiceSingleNodeDeploymentResultBatch} was handled on the remote node. */
+    private final CountDownLatch fullMsgHandledLatch = new CountDownLatch(1);
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        if (getTestIgniteInstanceName(NODES_CNT - 
1).equals(igniteInstanceName)) {
+            ((TestTcpDiscoverySpi) cfg.getDiscoverySpi()).discoveryHook(new 
DiscoveryHook() {
+                @Override public void beforeDiscovery(DiscoveryCustomMessage 
customMsg) {
+                    if (customMsg instanceof 
ServiceClusterDeploymentResultBatch) {
+                        fullMsgReceivedLatch.countDown();
+
+                        try {
+                            fullMsgUnblockedLatch.await(getTestTimeout(), 
MILLISECONDS);
+                        }
+                        catch (InterruptedException e) {
+                            throw new RuntimeException(e);
+                        }
+                    }
+                }
+
+                @Override public void afterDiscovery(DiscoveryCustomMessage 
customMsg) {
+                    if (customMsg instanceof 
ServiceClusterDeploymentResultBatch)
+                        fullMsgHandledLatch.countDown();
+                }
+            });
+
+            
cfg.setUserAttributes(Collections.singletonMap(ATTR_SKIP_DEPLOYMENT, true));
+        }
+
+        return cfg;
+    }
+
+    /**
+     * Ignores the test in case the legacy service processor is used.
+     */
+    @BeforeClass
+    public static void checkServiceProcessorType() {
+        Assume.assumeTrue(isEventDrivenServiceProcessorEnabled());
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        stopAllGrids();
+    }
+
+    /**
+     * Tests service invocation after its topology is initialized on the local 
node.
+     *
+     * @throws Exception If fails.
+     */
+    @Test
+    public void testServiceTopologyInitialized() throws Exception {
+        IgniteEx loc = startGrids(NODES_CNT);
+
+        IgniteEx rmt = grid(NODES_CNT - 1);
+
+        assertEquals(1, fullMsgUnblockedLatch.getCount());
+
+        fullMsgUnblockedLatch.countDown();
+
+        assertEquals(1, fullMsgHandledLatch.getCount());
+
+        deployServices(loc);
+
+        assertTrue(fullMsgHandledLatch.await(getTestTimeout(), MILLISECONDS));
+
+        assertThrowsWithCause(
+            () -> rmt.services().serviceProxy(BROKEN_SRVC, Invoker.class, 
false).invoke(),
+            IgniteException.class);
+
+        assertTrue(rmt.services().serviceProxy(DECENT_SRVC, Invoker.class, 
false).invoke());
+    }
+
+    /**
+     * Tests service invocation before its topology is initialized on the 
local node.
+     *
+     * @throws Exception If fails.
+     */
+    @Test
+    @SuppressWarnings("Convert2MethodRef")
+    public void testServiceTopologyInitializationDelayed() throws Exception {
+        IgniteEx loc = startGrids(NODES_CNT);
+
+        IgniteEx rmt = grid(NODES_CNT - 1);
+
+        assertEquals(1, fullMsgReceivedLatch.getCount());
+
+        deployServices(loc);
+
+        assertTrue(fullMsgReceivedLatch.await(getTestTimeout(), MILLISECONDS));
+
+        IgniteInternalFuture<Boolean> decentSvcFut = runAsync(() ->
+            rmt.services().serviceProxy(DECENT_SRVC, Invoker.class, 
false).invoke());
+
+        IgniteInternalFuture<Boolean> brokenSvcFut = runAsync(() ->
+            rmt.services().serviceProxy(BROKEN_SRVC, Invoker.class, 
false).invoke());
+
+        U.sleep(500);
+
+        assertEquals(1, fullMsgUnblockedLatch.getCount());
+
+        assertFalse(decentSvcFut.isDone());
+        assertFalse(brokenSvcFut.isDone());
+
+        fullMsgUnblockedLatch.countDown();
+
+        assertTrue(decentSvcFut.get(getTestTimeout()));
+
+        assertThrowsWithCause(() -> brokenSvcFut.get(getTestTimeout()), 
IgniteException.class);
+    }
+
+    /**
+     * Deploys two services. The deployment of one of which fails on all nodes 
and the other proceeds smoothly.
+     *
+     * @param ignite Deployment initiator.
+     */
+    private void deployServices(Ignite ignite) {
+        assertThrowsWithCause(
+            () -> ignite.services().deployAll(Arrays.asList(
+                getServiceConfiguration(BROKEN_SRVC, new TestService(true)),
+                getServiceConfiguration(DECENT_SRVC, new TestService(false)))),
+            ServiceDeploymentException.class);
+    }
+
+    /**
+     * @param name Name of the service.
+     * @param srvc Instance of the service.
+     * @return Service configuration.
+     */
+    private ServiceConfiguration getServiceConfiguration(String name, Service 
srvc) {
+        return new ServiceConfiguration()
+            .setName(name)
+            .setMaxPerNodeCount(1)
+            .setNodeFilter(node -> node.attribute(ATTR_SKIP_DEPLOYMENT) == 
null)
+            .setService(srvc);
+    }
+
+    /**
+     * Service that can be forced to fail during initialization.
+     */
+    public static class TestService implements Invoker {
+        /** Wheter exception is thrown during initialisation. */
+        private final boolean initBroken;
+
+        /**
+         * @param initBroken Wheter exception is thrown during initialisation.
+         */
+        public TestService(boolean initBroken) {
+            this.initBroken = initBroken;
+        }
+
+        /** {@inheritDoc} */
+        @Override public void cancel(ServiceContext ctx) {
+            // No-op.
+        }
+
+        /** {@inheritDoc} */
+        @Override public void init(ServiceContext ctx) throws Exception {
+            if (initBroken)
+                throw new RuntimeException("Expected service initialization 
failure.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public void execute(ServiceContext ctx) throws Exception {
+            // No-op.
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean invoke() {
+            return true;
+        }
+    }
+
+    /** */
+    public interface Invoker extends Service {
+        /** */
+        public boolean invoke();
+    }
+}
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/service/IgniteServiceProxyTimeoutInitializedTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/service/IgniteServiceProxyTimeoutInitializedTest.java
index 48f6b18..424b0cc 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/service/IgniteServiceProxyTimeoutInitializedTest.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/service/IgniteServiceProxyTimeoutInitializedTest.java
@@ -28,6 +28,7 @@ import org.apache.ignite.compute.ComputeTaskTimeoutException;
 import org.apache.ignite.configuration.BinaryConfiguration;
 import org.apache.ignite.configuration.IgniteConfiguration;
 import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
 import org.apache.ignite.internal.IgniteInterruptedCheckedException;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.lang.IgnitePredicate;
@@ -44,6 +45,8 @@ import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import org.junit.Test;
 
+import static org.apache.ignite.testframework.GridTestUtils.runAsync;
+
 /**
  * Tests service proxy timeouts.
  */
@@ -140,6 +143,36 @@ public class IgniteServiceProxyTimeoutInitializedTest 
extends GridCommonAbstract
     }
 
     /**
+     * Checks that the service invocation is waiting for the registered 
service to be initialized.
+     *
+     * @throws Exception If fail.
+     */
+    @Test
+    public void testInitializationWaiting() throws Exception {
+        srvc = new TestWaitServiceImpl();
+
+        latch1 = new CountDownLatch(1);
+        latch2 = new CountDownLatch(1);
+
+        IgniteEx srv = startGrid(0);
+
+        assertTrue(latch1.await(getTestTimeout(), TimeUnit.MILLISECONDS));
+
+        IgniteInternalFuture<?> srvcFut = runAsync(() ->
+            srv.services().serviceProxy("testService", TestService.class, 
false).test());
+
+        U.sleep(500);
+
+        assertEquals(1, latch2.getCount());
+
+        assertFalse(srvcFut.isDone());
+
+        latch2.countDown();
+
+        srvcFut.get();
+    }
+
+    /**
      * Checks that service not hangs if timeout set. Here we get hang with 
marshalling exception.
      *
      * @throws Exception If fail.
diff --git 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteServiceGridTestSuite.java
 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteServiceGridTestSuite.java
index 02691ae..11851d1 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteServiceGridTestSuite.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteServiceGridTestSuite.java
@@ -32,6 +32,7 @@ import 
org.apache.ignite.internal.processors.service.GridServiceProcessorSingleN
 import 
org.apache.ignite.internal.processors.service.GridServiceProcessorStopSelfTest;
 import 
org.apache.ignite.internal.processors.service.GridServiceProxyClientReconnectSelfTest;
 import 
org.apache.ignite.internal.processors.service.GridServiceProxyNodeStopSelfTest;
+import 
org.apache.ignite.internal.processors.service.GridServiceProxyTopologyInitializationTest;
 import 
org.apache.ignite.internal.processors.service.GridServiceReassignmentSelfTest;
 import 
org.apache.ignite.internal.processors.service.GridServiceSerializationSelfTest;
 import 
org.apache.ignite.internal.processors.service.IgniteServiceDeployment2ClassLoadersDefaultMarshallerTest;
@@ -111,6 +112,7 @@ import org.junit.runners.Suite;
     ServiceInfoSelfTest.class,
     ServiceDeploymentProcessIdSelfTest.class,
     ServiceHotRedeploymentViaDeploymentSpiTest.class,
+    GridServiceProxyTopologyInitializationTest.class,
 })
 public class IgniteServiceGridTestSuite {
 }

Reply via email to