ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agoncha...@apache.org
Subject [08/45] ignite git commit: Stopping GridUpdateNotifier properly on Ignite stopping.
Date Tue, 08 Sep 2015 17:08:58 GMT
Stopping GridUpdateNotifier properly on Ignite stopping.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/35bc5eca
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/35bc5eca
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/35bc5eca

Branch: refs/heads/ignite-264
Commit: 35bc5ecafd669ace8321774e39e49d6c3b88efe7
Parents: b1f119f
Author: ashutak <ashutak@gridgain.com>
Authored: Mon Sep 7 23:45:01 2015 +0300
Committer: ashutak <ashutak@gridgain.com>
Committed: Mon Sep 7 23:45:01 2015 +0300

----------------------------------------------------------------------
 .../ignite/internal/GridUpdateNotifier.java     | 82 ++++++++++++++------
 .../apache/ignite/internal/IgniteKernal.java    |  5 +-
 .../internal/GridUpdateNotifierSelfTest.java    | 23 +++---
 3 files changed, 70 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/35bc5eca/modules/core/src/main/java/org/apache/ignite/internal/GridUpdateNotifier.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/GridUpdateNotifier.java
b/modules/core/src/main/java/org/apache/ignite/internal/GridUpdateNotifier.java
index 448f87b..b6162ed 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/GridUpdateNotifier.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/GridUpdateNotifier.java
@@ -26,12 +26,12 @@ import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.Collection;
-import java.util.Map;
-import java.util.concurrent.Executor;
 import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.atomic.AtomicReference;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
@@ -59,21 +59,24 @@ import static java.net.URLEncoder.encode;
  * gracefully ignore any errors occurred during notification and verification process.
  */
 class GridUpdateNotifier {
+    /** Default encoding. */
+    private static final String CHARSET = "UTF-8";
+
     /** Access URL to be used to access latest version data. */
     private static final String UPD_STATUS_PARAMS = IgniteProperties.get("ignite.update.status.params");
 
     /** Throttling for logging out. */
     private static final long THROTTLE_PERIOD = 24 * 60 * 60 * 1000; // 1 day.
 
+    /** Sleep milliseconds time for worker thread. */
+    public static final int WORKER_THREAD_SLEEP_TIME = 5000;
+
     /** Grid version. */
     private final String ver;
 
     /** Site. */
     private final String url;
 
-    /** Asynchronous checked. */
-    private GridWorker checker;
-
     /** Latest version. */
     private volatile String latestVer;
 
@@ -95,7 +98,8 @@ class GridUpdateNotifier {
     /** System properties */
     private final String vmProps;
 
-    private final Map<String, String> pluginVers;
+    /** Plugins information for request */
+    private final String pluginsVers;
 
     /** Kernal gateway */
     private final GridKernalGateway gw;
@@ -103,6 +107,12 @@ class GridUpdateNotifier {
     /** */
     private long lastLog = -1;
 
+    /** Command for worker thread. */
+    private final AtomicReference<Runnable> cmd = new AtomicReference<>();
+
+    /** Worker thread to process http request. */
+    private final Thread workerThread;
+
     /**
      * Creates new notifier with default values.
      *
@@ -136,18 +146,46 @@ class GridUpdateNotifier {
             this.gridName = gridName == null ? "null" : gridName;
             this.gw = gw;
 
-            pluginVers = U.newHashMap(pluginProviders.size());
+            SB pluginsBuilder = new SB();
 
             for (PluginProvider provider : pluginProviders)
-                pluginVers.put(provider.name() + "-plugin-version", provider.version());
+                pluginsBuilder.a("&").a(provider.name() + "-plugin-version").a("=").
+                    a(encode(provider.version(), CHARSET));
+
+            pluginsVers = pluginsBuilder.toString();
 
             this.reportOnlyNew = reportOnlyNew;
 
             vmProps = getSystemProperties();
+
+            workerThread = new Thread(new Runnable() {
+                @Override public void run() {
+                    try {
+                        while(!Thread.currentThread().isInterrupted()) {
+                            Runnable cmd0 = cmd.getAndSet(null);
+
+                            if (cmd0 != null)
+                                cmd0.run();
+                            else
+                                Thread.sleep(WORKER_THREAD_SLEEP_TIME);
+                        }
+                    }
+                    catch (InterruptedException ignore) {
+                        // No-op.
+                    }
+                }
+            }, "upd-ver-checker");
+
+            workerThread.setDaemon(true);
+
+            workerThread.start();
         }
         catch (ParserConfigurationException e) {
             throw new IgniteCheckedException("Failed to create xml parser.", e);
         }
+        catch (UnsupportedEncodingException e) {
+            throw new IgniteCheckedException("Failed to encode.", e);
+        }
     }
 
     /**
@@ -197,16 +235,15 @@ class GridUpdateNotifier {
     /**
      * Starts asynchronous process for retrieving latest version data.
      *
-     * @param exec Executor service.
      * @param log Logger.
      */
-    void checkForNewVersion(Executor exec, IgniteLogger log) {
+    void checkForNewVersion(IgniteLogger log) {
         assert log != null;
 
         log = log.getLogger(getClass());
 
         try {
-            exec.execute(checker = new UpdateChecker(log));
+            cmd.set(new UpdateChecker(log));
         }
         catch (RejectedExecutionException e) {
             U.error(log, "Failed to schedule a thread due to execution rejection (safely
ignoring): " +
@@ -224,10 +261,6 @@ class GridUpdateNotifier {
 
         log = log.getLogger(getClass());
 
-        // Don't join it to avoid any delays on update checker.
-        // Checker thread will eventually exit.
-        U.cancel(checker);
-
         String latestVer = this.latestVer;
         String downloadUrl = this.downloadUrl;
 
@@ -272,12 +305,16 @@ class GridUpdateNotifier {
     }
 
     /**
+     * Stops update notifier.
+     */
+    public void stop() {
+        workerThread.interrupt();
+    }
+
+    /**
      * Asynchronous checker of the latest version available.
      */
     private class UpdateChecker extends GridWorker {
-        /** Default encoding. */
-        private static final String CHARSET = "UTF-8";
-
         /** Logger. */
         private final IgniteLogger log;
 
@@ -297,18 +334,13 @@ class GridUpdateNotifier {
             try {
                 String stackTrace = gw != null ? gw.userStackTrace() : null;
 
-                SB plugins = new SB();
-
-                for (Map.Entry<String, String> p : pluginVers.entrySet())
-                    plugins.a("&").a(p.getKey()).a("=").a(encode(p.getValue(), CHARSET));
-
                 String postParams =
                     "gridName=" + encode(gridName, CHARSET) +
                     (!F.isEmpty(UPD_STATUS_PARAMS) ? "&" + UPD_STATUS_PARAMS : "") +
                     (topSize > 0 ? "&topSize=" + topSize : "") +
                     (!F.isEmpty(stackTrace) ? "&stackTrace=" + encode(stackTrace, CHARSET)
: "") +
                     (!F.isEmpty(vmProps) ? "&vmProps=" + encode(vmProps, CHARSET) : "")
+
-                    plugins.toString();
+                        pluginsVers;
 
                 URLConnection conn = new URL(url).openConnection();
 
@@ -419,4 +451,4 @@ class GridUpdateNotifier {
             return obtainMeta("downloadUrl", node);
         }
     }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/35bc5eca/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java
index 14d7c14..f160d46 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/IgniteKernal.java
@@ -1839,6 +1839,9 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable
{
             if (updateNtfTimer != null)
                 updateNtfTimer.cancel();
 
+            if (verChecker != null)
+                verChecker.stop();
+
             if (starveTask != null)
                 starveTask.close();
 
@@ -3220,7 +3223,7 @@ public class IgniteKernal implements IgniteEx, IgniteMXBean, Externalizable
{
                     verChecker.topologySize(kernal.cluster().nodes().size());
             }
 
-            verChecker.checkForNewVersion(execSvc, log);
+            verChecker.checkForNewVersion(log);
 
             // Just wait for 10 secs.
             Thread.sleep(PERIODIC_VER_CHECK_CONN_TIMEOUT);

http://git-wip-us.apache.org/repos/asf/ignite/blob/35bc5eca/modules/core/src/test/java/org/apache/ignite/internal/GridUpdateNotifierSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/GridUpdateNotifierSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/GridUpdateNotifierSelfTest.java
index b6109f7..61c2085 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/GridUpdateNotifierSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/GridUpdateNotifierSelfTest.java
@@ -19,7 +19,6 @@ package org.apache.ignite.internal;
 
 import java.util.Collections;
 import java.util.Properties;
-import java.util.concurrent.Executor;
 import org.apache.ignite.IgniteSystemProperties;
 import org.apache.ignite.internal.util.future.GridFutureAdapter;
 import org.apache.ignite.internal.util.typedef.internal.U;
@@ -27,7 +26,6 @@ import org.apache.ignite.lang.IgniteProductVersion;
 import org.apache.ignite.plugin.PluginProvider;
 import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
 import org.apache.ignite.testframework.junits.common.GridCommonTest;
-import org.jetbrains.annotations.NotNull;
 
 /**
  * Update notifier test.
@@ -74,10 +72,17 @@ public class GridUpdateNotifierSelfTest extends GridCommonAbstractTest
{
         GridUpdateNotifier ntf = new GridUpdateNotifier(null, nodeVer,
             TEST_GATEWAY, Collections.<PluginProvider>emptyList(), false);
 
-        ntf.checkForNewVersion(new SelfExecutor(), log);
+        ntf.checkForNewVersion(log);
 
         String ver = ntf.latestVersion();
 
+        // Wait 60 sec for response.
+        for (int i = 0; ver == null && i < 600; i++) {
+            Thread.sleep(100);
+
+            ver = ntf.latestVersion();
+        }
+
         info("Latest version: " + ver);
 
         assertNotNull("Ignite latest version has not been detected.", ver);
@@ -93,16 +98,6 @@ public class GridUpdateNotifierSelfTest extends GridCommonAbstractTest
{
     }
 
     /**
-     * Executor that runs task in current thread.
-     */
-    private static class SelfExecutor implements Executor {
-        /** {@inheritDoc} */
-        @Override public void execute(@NotNull Runnable r) {
-            r.run();
-        }
-    }
-
-    /**
      * Test kernal gateway that always return uninitialized user stack trace.
      */
     private static final GridKernalGateway TEST_GATEWAY = new GridKernalGateway() {
@@ -138,4 +133,4 @@ public class GridUpdateNotifierSelfTest extends GridCommonAbstractTest
{
             // No-op.
         }
     };
-}
\ No newline at end of file
+}


Mime
View raw message