zstan commented on code in PR #13874:
URL: https://github.com/apache/ignite/pull/13874#discussion_r4739194233


##########
modules/core/src/main/java/org/apache/ignite/internal/processors/ssl/SslFactoryRegistry.java:
##########
@@ -128,146 +123,202 @@ private long consecutiveHotReloadFailures() {
         return sum;
     }
 
-    /** Runs the hourly check of the factories with hot reload enabled. */
-    private void checkFactories() {
-        if (log.isDebugEnabled())
-            log.debug("TLS hot reload check started");
-
-        for (RegisteredFactory registered : factories) {
-            if (registered.hotReloadFactory() != null)
-                registered.check();
-        }
-    }
-
     /** {@inheritDoc} */
     @Override public void onKernalStop(boolean cancel) {
         if (executor != null)
             executor.shutdownNow();
     }
 
     /**
-     * Reloads a registered factory. For a factory with hot reload enabled, 
waits for its running check and resets its failure
-     * count.
+     * Reloads a registered factory. If the factory has automatic checks, 
waits for its running check, resets its failure
+     * count and schedules its next check for the loaded certificates. If they 
are already due, the next check comes after
+     * {@link #CHECK_PERIOD}, because an automatic reload right away would 
load the same certificates from the source. If the
+     * factory cannot give the automatic reload time of the loaded 
certificates, the reload still succeeds, and the next check
+     * comes after {@link #CHECK_PERIOD}. The node start schedules the first 
check of each factory with automatic reload
+     * enabled, so a reload before the start only loads the factory.
      *
      * @param factory Registered factory to reload.
      */
-    public void reload(AbstractSslContextFactory factory) {
+    public void reload(ReloadableSslFactory factory) {
         for (RegisteredFactory registered : factories) {
             if (registered.factory == factory)
                 registered.reload();
         }
     }
 
-    /** Registered factory with its transports and hot reload state. The 
hourly check and manual reload hold its monitor. */
+    /** Registered factory with its transports and automatic reload state. Its 
checks and manual reload hold its monitor. */
     private class RegisteredFactory {
         /** */
         private final Factory<SSLContext> factory;
 
+        /** The factory if the registry can reload it, {@code null} otherwise. 
*/
+        @Nullable private final ReloadableSslFactory reloadable;
+
         /** */
         private final String transports;
 
         /** */
         private final AtomicInteger failures = new AtomicInteger();
 
+        /** Next automatic check, or {@code null} if the factory has no 
automatic checks. */
+        @Nullable private ScheduledFuture<?> nextCheck;
+
+        /** Automatic reload time that the registry logged last, or {@code -1} 
before the first log. */
+        private long loggedAutoReloadTime = -1;
+
         /** */
         private RegisteredFactory(Factory<SSLContext> factory, String 
transports) {
             this.factory = factory;
             this.transports = transports;
-        }
 
-        /** @return The factory if hot reload is enabled for it, {@code null} 
otherwise. */
-        @Nullable private AbstractSslContextFactory hotReloadFactory() {
-            return factory instanceof AbstractSslContextFactory sslFactory && 
sslFactory.isHotReloadEnabled() ? sslFactory : null;
+            reloadable = factory instanceof ReloadableSslFactory sslFactory ? 
sslFactory : null;
         }
 
         /**
-         * Checks the factory unless the registry has stopped. Counts and logs 
a failed check, so it does not stop
-         * the other factories or the schedule. A check that fails while the 
registry stops is logged at debug level
-         * and not counted.
+         * Checks the factory unless the registry has stopped, and schedules 
its next check. Counts and logs a failed
+         * check and retries it after {@link #CHECK_PERIOD}, so a failure does 
not stop the checks. A check that fails
+         * while the registry stops is logged at debug level and not counted.
          */
         private synchronized void check() {
             if (executor.isShutdown())
                 return;
 
+            if (log.isDebugEnabled())
+                log.debug("TLS automatic reload check started [transports=" + 
transports + ']');
+
+            long nextCheckDelay = CHECK_PERIOD;
+
             try {
-                reloadIfDue();
+                nextCheckDelay = reloadIfDue();
             }
             catch (Throwable e) {
                 if (!executor.isShutdown()) {
                     failures.incrementAndGet();
 
-                    log.warning("TLS hot reload failed [transports=" + 
transports + ", nextAttempt="
+                    log.warning("TLS automatic reload failed [transports=" + 
transports + ", nextAttempt="
                         + new Date(System.currentTimeMillis() + CHECK_PERIOD) 
+ ']', e);
                 }
                 else if (log.isDebugEnabled())
-                    log.debug("TLS hot reload failed during node stop 
[transports=" + transports + ", err=" + e + ']');
+                    log.debug("TLS automatic reload failed during node stop 
[transports=" + transports + ", err=" + e + ']');
             }
+
+            scheduleCheck(nextCheckDelay);
         }
 
         /**
-         * Reloads the factory if its hot reload time has come, and ends the 
failure streak if it has not.
+         * Reloads the factory if its automatic reload time has come, and ends 
the failure streak if it has not.
          *
+         * @return Delay of the next check.
          * @throws IgniteException If the reload fails or leaves certificates 
that are still due.
          */
-        private void reloadIfDue() {
-            AbstractSslContextFactory sslFactory = 
(AbstractSslContextFactory)factory;
+        private long reloadIfDue() {
+            long autoReloadTime = reloadable.autoReloadTime();
+
+            logAutoReloadTime(autoReloadTime);
 
-            if (!due(sslFactory.getHotReloadTime())) {
+            long nextCheckDelay = nextCheckDelay(autoReloadTime);
+
+            if (nextCheckDelay != 0) {
                 failures.set(0);
 
-                return;
+                return nextCheckDelay;
             }
 
-            automaticHotReloadAttempts.incrementAndGet();
+            autoReloadAttempts.incrementAndGet();
 
             if (log.isInfoEnabled())
-                log.info("TLS hot reload started [transports=" + transports + 
']');
+                log.info("TLS automatic reload started [transports=" + 
transports + ']');
 
-            sslFactory.init();
+            reloadable.init();
 
-            long hotReloadTime = sslFactory.getHotReloadTime();
+            autoReloadTime = reloadable.autoReloadTime();
+            nextCheckDelay = nextCheckDelay(autoReloadTime);
 
-            if (due(hotReloadTime)) {
-                throw new IgniteException("Loaded certificates are still due 
for hot reload [hotReloadTime="
-                    + new Date(hotReloadTime) + ']');
+            if (nextCheckDelay == 0) {
+                throw new IgniteException("Loaded certificates are still due 
for automatic reload [autoReloadTime="
+                    + new Date(autoReloadTime) + ']');
             }
 
             failures.set(0);
 
             if (log.isInfoEnabled())
-                log.info("TLS hot reload succeeded [transports=" + transports 
+ ']');
+                log.info("TLS automatic reload succeeded [transports=" + 
transports + ']');
+
+            logAutoReloadTime(autoReloadTime);
 
-            logHotReloadTime(hotReloadTime);
+            return nextCheckDelay;
         }
 
-        /** @return Whether the loaded certificates have a hot reload time, 
and it has come. */
-        private static boolean due(long hotReloadTime) {
-            return hotReloadTime != 0 && hotReloadTime <= 
System.currentTimeMillis();
+        /**
+         * @param autoReloadTime Automatic reload time of the loaded 
certificates, or {@code 0} if they have none.
+         * @return Delay until the automatic reload time, but no longer than 
{@link #CHECK_PERIOD}, or {@code 0} if the time has
+         *     come. Certificates without an automatic reload time are checked 
again after the full period.
+         */
+        private static long nextCheckDelay(long autoReloadTime) {
+            if (autoReloadTime == 0)
+                return CHECK_PERIOD;
+
+            return Math.max(0, Math.min(autoReloadTime - 
System.currentTimeMillis(), CHECK_PERIOD));
+        }
+
+        /** Replaces the scheduled check of the factory with one after the 
given delay. */
+        private synchronized void scheduleCheck(long delay) {
+            if (nextCheck != null)
+                nextCheck.cancel(false);
+
+            try {
+                nextCheck = executor.schedule(new 
OperationContextAwareRunnable(this::check, null), delay, MILLISECONDS);
+            }
+            catch (RejectedExecutionException ignored) {
+                // The registry has stopped.
+            }
         }
 
         /** */
         private synchronized void reload() {
             manualReloadAttempts.incrementAndGet();
 
-            ((AbstractSslContextFactory)factory).init();
+            reloadable.init();

Review Comment:
   Do we need a defence from NPE here ?  reloadable is @Nullable 



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to