Changeset 0f384c8


Ignore:
Timestamp:
Dec 5, 2011 4:18:35 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
bd82a0c
Parents:
5362e7cf
Message:
  • Router:
    • More refactoring tasks to their own files
    • Adjust some thread priorities
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • apps/routerconsole/java/src/net/i2p/router/web/RouterConsoleRunner.java

    r5362e7cf r0f384c8  
    343343
    344344        Thread t = new I2PAppThread(new StatSummarizer(), "StatSummarizer", true);
     345        t.setPriority(Thread.NORM_PRIORITY - 1);
    345346        t.start();
    346347       
     
    351352            NewsFetcher fetcher = NewsFetcher.getInstance(ctx);
    352353            Thread newsThread = new I2PAppThread(fetcher, "NewsFetcher", true);
     354            newsThread.setPriority(Thread.NORM_PRIORITY - 1);
    353355            newsThread.start();
    354356       
    355357            if (PluginStarter.pluginsEnabled(ctx)) {
    356358                t = new I2PAppThread(new PluginStarter(ctx), "PluginStarter", true);
     359                t.setPriority(Thread.NORM_PRIORITY - 1);
    357360                t.start();
    358361                ctx.addShutdownTask(new PluginStopper(ctx));
  • router/java/src/net/i2p/router/Router.java

    r5362e7cf r0f384c8  
    276276        //_sessionKeyPersistenceHelper = new SessionKeyPersistenceHelper(_context);
    277277        _killVMOnEnd = true;
    278         _oomListener = new I2PThread.OOMEventListener() {
    279             public void outOfMemory(OutOfMemoryError oom) {
    280                 clearCaches();
    281                 _log.log(Log.CRIT, "Thread ran out of memory, shutting down I2P", oom);
    282                 // prevent multiple parallel shutdowns (when you OOM, you OOM a lot...)
    283                 if (_shutdownInProgress)
    284                     return;
    285                 for (int i = 0; i < 5; i++) { // try this 5 times, in case it OOMs
    286                     try {
    287                         _log.log(Log.CRIT, "free mem: " + Runtime.getRuntime().freeMemory() +
    288                                            " total mem: " + Runtime.getRuntime().totalMemory());
    289                         break; // w00t
    290                     } catch (OutOfMemoryError oome) {
    291                         // gobble
    292                     }
    293                 }
    294                 _log.log(Log.CRIT, "To prevent future shutdowns, increase wrapper.java.maxmemory in $I2P/wrapper.config");
    295                 shutdown(EXIT_OOM);
    296             }
    297         };
     278        _oomListener = new OOMListener(_context);
     279
    298280        _shutdownHook = new ShutdownHook(_context);
    299         _gracefulShutdownDetector = new I2PAppThread(new GracefulShutdown(), "Graceful shutdown hook", true);
     281        _gracefulShutdownDetector = new I2PAppThread(new GracefulShutdown(_context), "Graceful shutdown hook", true);
     282        _gracefulShutdownDetector.setPriority(Thread.NORM_PRIORITY + 1);
    300283        _gracefulShutdownDetector.start();
    301284       
    302285        _watchdog = new RouterWatchdog(_context);
    303286        _watchdogThread = new I2PAppThread(_watchdog, "RouterWatchdog", true);
     287        _watchdogThread.setPriority(Thread.NORM_PRIORITY + 1);
    304288        _watchdogThread.start();
    305289       
     
    974958******/
    975959
    976     /**
    977      *  A non-daemon thread to let
    978      *  the shutdown task get all the way to the end
    979      *  @since 0.8.8
    980      */
    981     private static class Spinner extends Thread {
    982 
    983         public Spinner() {
    984             super();
    985             setName("Shutdown Spinner");
    986             setDaemon(false);
    987         }
    988 
    989         @Override
    990         public void run() {
    991             try {
    992                 sleep(60*1000);
    993             } catch (InterruptedException ie) {}
    994         }
    995     }
    996    
    997960    public static final int EXIT_GRACEFUL = 2;
    998961    public static final int EXIT_HARD = 3;
     
    11711134        }       
    11721135    }
     1136
    11731137    /**
    11741138     * What exit code do we plan on using when we shut down (or -1, if there isn't a graceful shutdown planned)
    11751139     */
    11761140    public int scheduledGracefulExitCode() { return _gracefulExitCode; }
     1141
     1142    /**
     1143     * Is a graceful shutdown in progress? This may be cancelled.
     1144     */
    11771145    public boolean gracefulShutdownInProgress() {
    11781146        return (null != _config.get(PROP_SHUTDOWN_IN_PROGRESS));
    11791147    }
     1148
     1149    /**
     1150     * Is a final shutdown in progress? This may not be cancelled.
     1151     * @since 0.8.12
     1152     */
     1153    public boolean isFinalShutdownInProgress() {
     1154        return _shutdownInProgress;
     1155    }
     1156
    11801157    /** How long until the graceful shutdown will kill us?  */
    11811158    public long getShutdownTimeRemaining() {
     
    11881165        else
    11891166            return exp + 2*CLOCK_FUDGE_FACTOR - _context.clock().now();
    1190     }
    1191    
    1192     /**
    1193      * Simple thread that sits and waits forever, managing the
    1194      * graceful shutdown "process" (describing it would take more text
    1195      * than just reading the code...)
    1196      *
    1197      */
    1198     private class GracefulShutdown implements Runnable {
    1199         public void run() {
    1200             while (true) {
    1201                 boolean shutdown = (null != _config.get(PROP_SHUTDOWN_IN_PROGRESS));
    1202                 if (shutdown) {
    1203                     if (_gracefulExitCode == EXIT_HARD || _gracefulExitCode == EXIT_HARD_RESTART ||
    1204                         _context.tunnelManager().getParticipatingCount() <= 0) {
    1205                         if (_gracefulExitCode == EXIT_HARD)
    1206                             _log.log(Log.CRIT, "Shutting down after a brief delay");
    1207                         else if (_gracefulExitCode == EXIT_HARD_RESTART)
    1208                             _log.log(Log.CRIT, "Restarting after a brief delay");
    1209                         else
    1210                             _log.log(Log.CRIT, "Graceful shutdown progress - no more tunnels, safe to die");
    1211                         // Allow time for a UI reponse
    1212                         try {
    1213                             synchronized (Thread.currentThread()) {
    1214                                 Thread.currentThread().wait(2*1000);
    1215                             }
    1216                         } catch (InterruptedException ie) {}
    1217                         shutdown(_gracefulExitCode);
    1218                         return;
    1219                     } else {
    1220                         try {
    1221                             synchronized (Thread.currentThread()) {
    1222                                 Thread.currentThread().wait(10*1000);
    1223                             }
    1224                         } catch (InterruptedException ie) {}
    1225                     }
    1226                 } else {
    1227                     try {
    1228                         synchronized (Thread.currentThread()) {
    1229                             Thread.currentThread().wait();
    1230                         }
    1231                     } catch (InterruptedException ie) {}
    1232                 }
    1233             }
    1234         }
    12351167    }
    12361168   
     
    13091241        ((RouterClock) _context.clock()).removeShiftListener(this);
    13101242        _isAlive = false;
    1311         Thread t = new Thread(new Restarter(), "Router Restart");
     1243        _started = _context.clock().now();
     1244        Thread t = new Thread(new Restarter(_context), "Router Restart");
     1245        t.setPriority(Thread.NORM_PRIORITY + 1);
    13121246        t.start();
    13131247    }   
    13141248
    13151249    /**
    1316      *  @since 0.8.8
    1317      */
    1318     private class Restarter implements Runnable {
    1319         public void run() {
    1320             _started = _context.clock().now();
    1321             _log.error("Stopping the router for a restart...");
    1322             _log.logAlways(Log.WARN, "Stopping the client manager");
    1323             // NOTE: DisconnectMessageHandler keys off "restart"
    1324             try { _context.clientManager().shutdown("Router restart"); } catch (Throwable t) { _log.log(Log.CRIT, "Error stopping the client manager", t); }
    1325             _log.logAlways(Log.WARN, "Stopping the comm system");
    1326             _context.bandwidthLimiter().reinitialize();
    1327             try { _context.messageRegistry().restart(); } catch (Throwable t) { _log.log(Log.CRIT, "Error restarting the message registry", t); }
    1328             try { _context.commSystem().restart(); } catch (Throwable t) { _log.log(Log.CRIT, "Error restarting the comm system", t); }
    1329             _log.logAlways(Log.WARN, "Stopping the tunnel manager");
    1330             try { _context.tunnelManager().restart(); } catch (Throwable t) { _log.log(Log.CRIT, "Error restarting the tunnel manager", t); }
    1331 
    1332             //try { _context.peerManager().restart(); } catch (Throwable t) { _log.log(Log.CRIT, "Error restarting the peer manager", t); }
    1333             //try { _context.netDb().restart(); } catch (Throwable t) { _log.log(Log.CRIT, "Error restarting the networkDb", t); }
    1334             //try { _context.jobQueue().restart(); } catch (Throwable t) { _log.log(Log.CRIT, "Error restarting the job queue", t); }
    1335        
    1336             _log.logAlways(Log.WARN, "Router teardown complete, restarting the router...");
    1337             try { Thread.sleep(10*1000); } catch (InterruptedException ie) {}
    1338        
    1339             _log.logAlways(Log.WARN, "Restarting the comm system");
    1340             _log.logAlways(Log.WARN, "Restarting the tunnel manager");
    1341             _log.logAlways(Log.WARN, "Restarting the client manager");
    1342             try { _context.clientMessagePool().restart(); } catch (Throwable t) { _log.log(Log.CRIT, "Error restarting the CMP", t); }
    1343             try { _context.clientManager().startup(); } catch (Throwable t) { _log.log(Log.CRIT, "Error starting the client manager", t); }
    1344        
    1345             _isAlive = true;
    1346             rebuildRouterInfo();
    1347        
    1348             _log.logAlways(Log.WARN, "Restart complete");
    1349             ((RouterClock) _context.clock()).addShiftListener(Router.this);
    1350         }
    1351     }
    1352    
     1250     *  Only for Restarter
     1251     *  @since 0.8.12
     1252     */
     1253    public void setIsAlive() {
     1254        _isAlive = true;
     1255    }
     1256
    13531257    public static void main(String args[]) {
    13541258        System.out.println("Starting I2P " + RouterVersion.FULL_VERSION);
Note: See TracChangeset for help on using the changeset viewer.