Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

ISPN-2140 Fault tolerant ping enabled to detect if cache name defined #1213

Closed
wants to merge 1 commit into from

1 participant

Galder Zamarreño
This page is out of date. Refresh to see the latest.
21  client/hotrod-client/src/main/java/org/infinispan/client/hotrod/RemoteCacheManager.java
@@ -515,15 +515,13 @@ private void loadFromStream(InputStream stream) {
515 515
             RemoteCacheImpl<K, V> result = new RemoteCacheImpl<K, V>(this, cacheName);
516 516
             RemoteCacheHolder rcc = new RemoteCacheHolder(result, forceReturnValueOverride == null ? forceReturnValueDefault : forceReturnValueOverride);
517 517
             startRemoteCache(rcc);
518  
-            if (config.getPingOnStartup()) {
519  
-               // If ping not successful assume that the cache does not exist
520  
-               // Default cache is always started, so don't do for it
521  
-               if (!cacheName.equals(BasicCacheContainer.DEFAULT_CACHE_NAME) &&
522  
-                     ping(result) == PingResult.CACHE_DOES_NOT_EXIST) {
523  
-                  return null;
524  
-               }
  518
+            // If ping not successful assume that the cache does not exist
  519
+            // Default cache is always started, so don't do for it
  520
+            if (!cacheName.equals(BasicCacheContainer.DEFAULT_CACHE_NAME) &&
  521
+                  ping(result) == PingResult.CACHE_DOES_NOT_EXIST) {
  522
+               return null;
525 523
             }
526  
-            // If ping on startup is disabled, or cache is defined in server
  524
+            // If cache is not defined in server
527 525
             cacheName2RemoteCache.put(cacheName, rcc);
528 526
             return result;
529 527
          } else {
@@ -537,12 +535,7 @@ private void loadFromStream(InputStream stream) {
537 535
          return PingResult.FAIL;
538 536
       }
539 537
 
540  
-      Transport transport = transportFactory.getTransport();
541  
-      try {
542  
-         return cache.ping(transport);
543  
-      } finally {
544  
-        transportFactory.releaseTransport(transport);
545  
-      }
  538
+      return cache.ping();
546 539
    }
547 540
 
548 541
    private void startRemoteCache(RemoteCacheHolder remoteCacheHolder) {
5  client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/RemoteCacheImpl.java
@@ -56,7 +56,6 @@
56 56
 import org.infinispan.client.hotrod.impl.operations.ReplaceIfUnmodifiedOperation;
57 57
 import org.infinispan.client.hotrod.impl.operations.ReplaceOperation;
58 58
 import org.infinispan.client.hotrod.impl.operations.StatsOperation;
59  
-import org.infinispan.client.hotrod.impl.transport.Transport;
60 59
 import org.infinispan.client.hotrod.logging.Log;
61 60
 import org.infinispan.client.hotrod.logging.LogFactory;
62 61
 import org.infinispan.marshall.Marshaller;
@@ -429,8 +428,8 @@ public V call() throws Exception {
429 428
       return result;
430 429
    }
431 430
 
432  
-   public PingOperation.PingResult ping(Transport transport) {
433  
-      return operationsFactory.newPingOperation(transport).execute();
  431
+   public PingOperation.PingResult ping() {
  432
+      return operationsFactory.newFaultTolerantPingOperation().execute();
434 433
    }
435 434
 
436 435
    private byte[] obj2bytes(Object o, boolean isKey) {
52  ...hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/operations/FaultTolerantPingOperation.java
... ...
@@ -0,0 +1,52 @@
  1
+/*
  2
+ * Copyright 2012 Red Hat, Inc. and/or its affiliates.
  3
+ *
  4
+ * This is free software; you can redistribute it and/or modify it
  5
+ * under the terms of the GNU Lesser General Public License as
  6
+ * published by the Free Software Foundation; either version 2.1 of
  7
+ * the License, or (at your option) any later version.
  8
+ *
  9
+ * This software is distributed in the hope that it will be useful,
  10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12
+ * Lesser General Public License for more details.
  13
+ *
  14
+ * You should have received a copy of the GNU Lesser General Public
  15
+ * License along with this library; if not, write to the Free Software
  16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17
+ * 02110-1301 USA
  18
+ */
  19
+
  20
+package org.infinispan.client.hotrod.impl.operations;
  21
+
  22
+import org.infinispan.client.hotrod.Flag;
  23
+import org.infinispan.client.hotrod.impl.protocol.Codec;
  24
+import org.infinispan.client.hotrod.impl.transport.Transport;
  25
+import org.infinispan.client.hotrod.impl.transport.TransportFactory;
  26
+
  27
+import java.util.concurrent.atomic.AtomicInteger;
  28
+
  29
+/**
  30
+ * A fault tolerant ping operation that can survive to node failures.
  31
+ *
  32
+ * @author Galder Zamarreño
  33
+ * @since 5.2
  34
+ */
  35
+public class FaultTolerantPingOperation extends RetryOnFailureOperation<PingOperation.PingResult> {
  36
+
  37
+   protected FaultTolerantPingOperation(Codec codec, TransportFactory transportFactory,
  38
+         byte[] cacheName, AtomicInteger topologyId, Flag[] flags) {
  39
+      super(codec, transportFactory, cacheName, topologyId, flags);
  40
+   }
  41
+
  42
+   @Override
  43
+   protected Transport getTransport(int retryCount) {
  44
+      return transportFactory.getTransport();
  45
+   }
  46
+
  47
+   @Override
  48
+   protected PingOperation.PingResult executeOperation(Transport transport) {
  49
+      return new PingOperation(codec, topologyId, transport, cacheName).execute();
  50
+   }
  51
+
  52
+}
2  client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/operations/HotRodOperation.java
@@ -49,7 +49,7 @@
49 49
 
50 50
    protected final AtomicInteger topologyId;
51 51
    
52  
-   private final Codec codec;
  52
+   protected final Codec codec;
53 53
 
54 54
    private static final byte NO_TX = 0;
55 55
    private static final byte XA_TX = 1;
18  client/hotrod-client/src/main/java/org/infinispan/client/hotrod/impl/operations/OperationsFactory.java
@@ -134,10 +134,28 @@ public BulkGetOperation newBulkGetOperation(int size) {
134 134
             codec, transportFactory, cacheNameBytes, topologyId, flags(), size);
135 135
    }
136 136
 
  137
+   /**
  138
+    * Construct a ping request directed to a particular node.
  139
+    *
  140
+    * @param transport represents the node to which the operation is directed
  141
+    * @return a ping operation for a particular node
  142
+    */
137 143
    public PingOperation newPingOperation(Transport transport) {
138 144
       return new PingOperation(codec, topologyId, transport, cacheNameBytes);
139 145
    }
140 146
 
  147
+   /**
  148
+    * Construct a fault tolerant ping request. This operation should be capable
  149
+    * to deal with nodes being down, so it will find the first node successful
  150
+    * node to respond to the ping.
  151
+    *
  152
+    * @return a ping operation for the cluster
  153
+    */
  154
+   public FaultTolerantPingOperation newFaultTolerantPingOperation() {
  155
+      return new FaultTolerantPingOperation(
  156
+            codec, transportFactory, cacheNameBytes, topologyId, flags());
  157
+   }
  158
+
141 159
    private Flag[] flags() {
142 160
       Flag[] flags = this.flagsMap.get();
143 161
       this.flagsMap.remove();
102  client/hotrod-client/src/test/java/org/infinispan/client/hotrod/PingOnStartupTest.java
@@ -24,6 +24,7 @@
24 24
 
25 25
 import org.infinispan.client.hotrod.impl.transport.tcp.TcpTransportFactory;
26 26
 import org.infinispan.client.hotrod.test.MultiHotRodServersTest;
  27
+import org.infinispan.client.hotrod.test.RemoteCacheManagerCallable;
27 28
 import org.infinispan.config.Configuration;
28 29
 import org.infinispan.config.Configuration.CacheMode;
29 30
 import org.infinispan.server.hotrod.HotRodServer;
@@ -32,6 +33,7 @@
32 33
 
33 34
 import java.util.Properties;
34 35
 
  36
+import static org.infinispan.client.hotrod.test.HotRodClientTestingUtil.withRemoteCacheManager;
35 37
 import static org.testng.AssertJUnit.assertEquals;
36 38
 import static org.testng.AssertJUnit.assertFalse;
37 39
 
@@ -55,55 +57,91 @@ public void testTopologyFetched() throws Exception {
55 57
             "localhost:" + hotRodServer2.getPort() + ";localhost:" + hotRodServer2.getPort());
56 58
       props.put("infinispan.client.hotrod.ping_on_startup", "true");
57 59
       props.put("timeBetweenEvictionRunsMillis", "500");
58  
-      RemoteCacheManager remoteCacheManager = new RemoteCacheManager(props);
59 60
 
60  
-      TcpTransportFactory tcpConnectionFactory = (TcpTransportFactory)
61  
-            TestingUtil.extractField(remoteCacheManager, "transportFactory");
62  
-      for (int i = 0; i < 10; i++) {
63  
-         try {
64  
-            if (tcpConnectionFactory.getServers().size() == 1) {
65  
-               Thread.sleep(1000);
66  
-            } else {
67  
-               break;
  61
+      withRemoteCacheManager(new RemoteCacheManagerCallable(
  62
+            new RemoteCacheManager(props)) {
  63
+         @Override
  64
+         public void call() throws Exception {
  65
+            TcpTransportFactory tcpConnectionFactory = (TcpTransportFactory)
  66
+                  TestingUtil.extractField(rcm, "transportFactory");
  67
+            for (int i = 0; i < 10; i++) {
  68
+               if (tcpConnectionFactory.getServers().size() == 1) {
  69
+                  Thread.sleep(1000);
  70
+               } else {
  71
+                  break;
  72
+               }
68 73
             }
69  
-         } finally {
70  
-            remoteCacheManager.stop();
  74
+            assertEquals(2, tcpConnectionFactory.getServers().size());
71 75
          }
72  
-      }
73  
-      assertEquals(2, tcpConnectionFactory.getServers().size());
  76
+      });
74 77
    }
75 78
 
76  
-   public void testTopologyNotFetched() {
  79
+   public void testTopologyNotFetched() throws Exception {
77 80
       Properties props = new Properties();
78 81
       HotRodServer hotRodServer2 = server(1);
79 82
       props.put("infinispan.client.hotrod.server_list",
80  
-            "localhost:" + hotRodServer2.getPort() + ";localhost:" + hotRodServer2.getPort());
  83
+            "localhost:" + hotRodServer2.getPort());
81 84
       props.put("infinispan.client.hotrod.ping_on_startup", "false");
82  
-      RemoteCacheManager remoteCacheManager = new RemoteCacheManager(props);
83 85
 
84  
-      TcpTransportFactory tcpConnectionFactory = (TcpTransportFactory)
85  
-            TestingUtil.extractField(remoteCacheManager, "transportFactory");
86  
-      try {
87  
-         assertEquals(1, tcpConnectionFactory.getServers().size());
88  
-      } finally {
89  
-         remoteCacheManager.stop();
90  
-      }
  86
+      withRemoteCacheManager(new RemoteCacheManagerCallable(
  87
+            new RemoteCacheManager(props)) {
  88
+         @Override
  89
+         public void call() throws Exception {
  90
+            TcpTransportFactory tcpConnectionFactory = (TcpTransportFactory)
  91
+                  TestingUtil.extractField(rcm, "transportFactory");
  92
+            assertEquals(1, tcpConnectionFactory.getServers().size());
  93
+         }
  94
+      });
91 95
    }
92 96
 
93  
-   public void testGetCacheWithPingOnStartupDisabled() {
  97
+   public void testGetCacheWithPingOnStartupDisabled() throws Exception {
94 98
       Properties props = new Properties();
95 99
       HotRodServer hotRodServer2 = server(1);
96 100
       props.put("infinispan.client.hotrod.server_list",
97  
-            "boomoo:12345;localhost:" + hotRodServer2.getPort() + ";localhost:" + hotRodServer2.getPort());
  101
+            "boomoo:12345;localhost:" + hotRodServer2.getPort());
98 102
       props.put("infinispan.client.hotrod.ping_on_startup", "false");
99  
-      RemoteCacheManager remoteCacheManager = new RemoteCacheManager(props);
100 103
 
101  
-      try {
102  
-         RemoteCache<Object, Object> cache = remoteCacheManager.getCache();
103  
-         assertFalse(cache.containsKey("k"));
104  
-      } finally {
105  
-         remoteCacheManager.stop();
106  
-      }
  104
+      withRemoteCacheManager(new RemoteCacheManagerCallable(
  105
+            new RemoteCacheManager(props)) {
  106
+         @Override
  107
+         public void call() throws Exception {
  108
+            RemoteCache<Object, Object> cache = rcm.getCache();
  109
+            assertFalse(cache.containsKey("k"));
  110
+         }
  111
+      });
  112
+   }
  113
+
  114
+   public void testGetCacheWorksIfNodeDown() throws Exception {
  115
+      Properties props = new Properties();
  116
+      HotRodServer hotRodServer2 = server(1);
  117
+      props.put("infinispan.client.hotrod.server_list",
  118
+            "boomoo:12345;localhost:" + hotRodServer2.getPort());
  119
+      props.put("infinispan.client.hotrod.ping_on_startup", "true");
  120
+      props.put("timeBetweenEvictionRunsMillis", "500");
  121
+
  122
+      withRemoteCacheManager(new RemoteCacheManagerCallable(
  123
+            new RemoteCacheManager(props)) {
  124
+         @Override
  125
+         public void call() throws Exception {
  126
+            rcm.getCache();
  127
+         }
  128
+      });
  129
+   }
  130
+
  131
+   public void testGetCacheWorksIfNodeNotDown() throws Exception {
  132
+      Properties props = new Properties();
  133
+      HotRodServer hotRodServer2 = server(1);
  134
+      props.put("infinispan.client.hotrod.server_list",
  135
+            "localhost:" + hotRodServer2.getPort());
  136
+      props.put("infinispan.client.hotrod.ping_on_startup", "true");
  137
+      props.put("timeBetweenEvictionRunsMillis", "500");
  138
+      withRemoteCacheManager(new RemoteCacheManagerCallable(
  139
+            new RemoteCacheManager(props)) {
  140
+         @Override
  141
+         public void call() throws Exception {
  142
+            rcm.getCache();
  143
+         }
  144
+      });
107 145
    }
108 146
 
109 147
 }
18  client/hotrod-client/src/test/java/org/infinispan/client/hotrod/RemoteCacheManagerTest.java
@@ -23,11 +23,14 @@
23 23
 package org.infinispan.client.hotrod;
24 24
 
25 25
 import org.infinispan.client.hotrod.impl.ConfigurationProperties;
  26
+import org.infinispan.client.hotrod.test.HotRodClientTestingUtil;
26 27
 import org.infinispan.manager.EmbeddedCacheManager;
27 28
 import org.infinispan.server.hotrod.HotRodServer;
28 29
 import org.infinispan.test.SingleCacheManagerTest;
  30
+import org.infinispan.test.TestingUtil;
29 31
 import org.infinispan.test.fwk.TestCacheManagerFactory;
30 32
 import org.testng.annotations.AfterTest;
  33
+import org.testng.annotations.BeforeClass;
31 34
 import org.testng.annotations.Test;
32 35
 
33 36
 import java.net.URL;
@@ -40,23 +43,26 @@
40 43
 @Test(testName = "client.hotrod.RemoteCacheManagerTest", groups = "functional" )
41 44
 public class RemoteCacheManagerTest extends SingleCacheManagerTest {
42 45
 
43  
-   EmbeddedCacheManager cacheManager = null;
44  
-   HotRodServer hotrodServer = null;
  46
+   HotRodServer hotrodServer;
45 47
    int port;
46 48
 
47 49
    @Override
48 50
    protected EmbeddedCacheManager createCacheManager() throws Exception {
49  
-      cacheManager = TestCacheManagerFactory.createLocalCacheManager(false);
  51
+      return TestCacheManagerFactory.createLocalCacheManager(false);
  52
+   }
  53
+
  54
+   @Override
  55
+   protected void setup() throws Exception {
  56
+      super.setup();
50 57
       hotrodServer = TestHelper.startHotRodServer(cacheManager);
51 58
       port = hotrodServer.getPort();
52  
-      return cacheManager;
53 59
    }
54 60
 
55 61
    @AfterTest(alwaysRun = true)
56 62
    public void release() {
57 63
       try {
58  
-         if (cacheManager != null) cacheManager.stop();
59  
-         if (hotrodServer != null) hotrodServer.stop();
  64
+         TestingUtil.killCacheManagers(cacheManager);
  65
+         HotRodClientTestingUtil.killServers(hotrodServer);
60 66
       } catch (Exception e) {
61 67
          e.printStackTrace();
62 68
       }
27  client/hotrod-client/src/test/java/org/infinispan/client/hotrod/test/HotRodClientTestingUtil.java
@@ -34,6 +34,11 @@
34 34
 
35 35
    private static final Log log = LogFactory.getLog(HotRodClientTestingUtil.class, Log.class);
36 36
 
  37
+   /**
  38
+    * Kills a remote cache manager.
  39
+    *
  40
+    * @param rcm the remote cache manager instance to kill
  41
+    */
37 42
    public static void killRemoteCacheManager(RemoteCacheManager rcm) {
38 43
       try {
39 44
          if (rcm != null) rcm.stop();
@@ -42,6 +47,11 @@ public static void killRemoteCacheManager(RemoteCacheManager rcm) {
42 47
       }
43 48
    }
44 49
 
  50
+   /**
  51
+    * Kills a group of Hot Rod servers.
  52
+    *
  53
+    * @param servers the group of Hot Rod servers to kill
  54
+    */
45 55
    public static void killServers(HotRodServer... servers) {
46 56
       if (servers != null) {
47 57
          for (HotRodServer server : servers) {
@@ -54,4 +64,21 @@ public static void killServers(HotRodServer... servers) {
54 64
       }
55 65
    }
56 66
 
  67
+   /**
  68
+    * Invoke a task using a remote cache manager. This method guarantees that
  69
+    * the remote manager used in the task will be cleaned up after the task has
  70
+    * completed, regardless of the task outcome.
  71
+    *
  72
+    * @param c task to execute
  73
+    * @throws Exception if the task fails somehow
  74
+    */
  75
+   public static void withRemoteCacheManager(RemoteCacheManagerCallable c)
  76
+         throws Exception {
  77
+      try {
  78
+         c.call();
  79
+      } finally {
  80
+         killRemoteCacheManager(c.rcm);
  81
+      }
  82
+   }
  83
+
57 84
 }
42  client/hotrod-client/src/test/java/org/infinispan/client/hotrod/test/RemoteCacheManagerCallable.java
... ...
@@ -0,0 +1,42 @@
  1
+/*
  2
+ * Copyright 2012 Red Hat, Inc. and/or its affiliates.
  3
+ *
  4
+ * This is free software; you can redistribute it and/or modify it
  5
+ * under the terms of the GNU Lesser General Public License as
  6
+ * published by the Free Software Foundation; either version 2.1 of
  7
+ * the License, or (at your option) any later version.
  8
+ *
  9
+ * This software is distributed in the hope that it will be useful,
  10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12
+ * Lesser General Public License for more details.
  13
+ *
  14
+ * You should have received a copy of the GNU Lesser General Public
  15
+ * License along with this library; if not, write to the Free Software
  16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17
+ * 02110-1301 USA
  18
+ */
  19
+
  20
+package org.infinispan.client.hotrod.test;
  21
+
  22
+import org.infinispan.client.hotrod.RemoteCacheManager;
  23
+
  24
+/**
  25
+ * A task that executes operations against a given remote cache manager.
  26
+ *
  27
+ * @author Galder Zamarreño
  28
+ * @since 5.2
  29
+ */
  30
+public class RemoteCacheManagerCallable {
  31
+
  32
+   protected final RemoteCacheManager rcm;
  33
+
  34
+   public RemoteCacheManagerCallable(RemoteCacheManager rcm) {
  35
+      this.rcm = rcm;
  36
+   }
  37
+
  38
+   public void call() throws Exception {
  39
+      // No-op
  40
+   }
  41
+
  42
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.