Subversion Repositories DevTools

Rev

Rev 6914 | Rev 7082 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6914 Rev 7033
Line 1... Line 1...
1
package com.erggroup.buildtool.daemon;
1
package com.erggroup.buildtool.daemon;
2
 
2
 
3
import com.erggroup.buildtool.ripple.Package;
3
import com.erggroup.buildtool.ripple.Package;
4
import com.erggroup.buildtool.ripple.ReleaseConfig;
4
import com.erggroup.buildtool.ripple.ReleaseConfig;
5
import com.erggroup.buildtool.ripple.ReleaseManager;
5
import com.erggroup.buildtool.ripple.ReleaseManager;
6
import com.erggroup.buildtool.utilities.utilities;
-
 
7
import com.erggroup.buildtool.daemon.NagiosThread;
-
 
8
 
6
 
9
import java.io.File;
7
import java.io.File;
10
import java.io.IOException;
8
import java.io.IOException;
11
import java.net.ServerSocket;
9
import java.net.ServerSocket;
12
import java.sql.SQLException;
10
import java.sql.SQLException;
13
import java.util.ArrayList;
11
import java.util.ArrayList;
14
import java.util.Iterator;
12
import java.util.Iterator;
15
import java.util.Map;
13
import java.util.Map;
16
import java.util.concurrent.ConcurrentLinkedQueue;
14
import java.util.concurrent.ConcurrentLinkedQueue;
17
 
15
 
18
import org.apache.log4j.Logger;
16
import org.slf4j.Logger ;
19
import org.apache.log4j.xml.DOMConfigurator;
17
import org.slf4j.LoggerFactory;
20
 
18
 
21
/**BuildDaemon sub component and entry point (main BuildDaemon thread)
19
/**BuildDaemon sub component and entry point (main BuildDaemon thread)
22
 */
20
 */
23
public class BuildDaemon
21
public class BuildDaemon
24
{
22
{
Line 40... Line 38...
40
	static boolean mShutDown = false;
38
	static boolean mShutDown = false;
41
 
39
 
42
    /**Logger
40
    /**Logger
43
     * @attribute
41
     * @attribute
44
     */
42
     */
45
    private static final Logger mLogger = Logger.getLogger(BuildDaemon.class);
43
    protected static Logger mLogger =  LoggerFactory.getLogger(BuildDaemon.class);
46
 
44
 
47
    /**Collection of ThreadIdentifier objects.
45
    /**Collection of ThreadIdentifier objects.
48
     * Using a ConcurrentLinkedQueue because we add and remove items from the collection
46
     * Using a ConcurrentLinkedQueue because we add and remove items from the collection
49
     * and it is being accessed from multiple threads.
47
     * and it is being accessed from multiple threads.
50
     * @attribute
48
     * @attribute
Line 94... Line 92...
94
 
92
 
95
        /**constructor
93
        /**constructor
96
         */
94
         */
97
        ThreadIdentifier(int rconId, BuildThread thread)
95
        ThreadIdentifier(int rconId, BuildThread thread)
98
        {
96
        {
99
            mLogger.debug("ThreadIdentifier " + rconId);
97
            mLogger.debug("ThreadIdentifier {}", rconId);
100
            mRconId = rconId;
98
            mRconId = rconId;
101
            mThread = thread;
99
            mThread = thread;
102
        }
100
        }
103
 
101
 
104
        /**accessor
102
        /**accessor
105
         */
103
         */
106
        int getRconId()
104
        int getRconId()
107
        {
105
        {
108
            mLogger.info("get_rcon_id returned " + mRconId);
106
            mLogger.info("get_rcon_id returned {}", mRconId);
109
            return mRconId;
107
            return mRconId;
110
        }
108
        }
111
 
109
 
112
        /**accessor
110
        /**accessor
113
         */
111
         */
Line 122... Line 120...
122
     */
120
     */
123
    public class BuildException extends Exception
121
    public class BuildException extends Exception
124
    {
122
    {
125
        BuildException(String msg)
123
        BuildException(String msg)
126
        {
124
        {
127
            mLogger.fatal(msg);
125
            mLogger.error(msg);
128
        }
126
        }
129
 
127
 
130
      private static final long serialVersionUID = 1L;
128
      private static final long serialVersionUID = 1L;
131
    }
129
    }
132
 
130
 
133
    /**main method for the Build Daemon program
131
    /**main method for the Build Daemon program
134
     * instantiates a BuildDaemon object
132
     * instantiates a BuildDaemon object
135
     */
133
     */
136
    public static void main(String[] args)
134
    public static void main(String[] args)
137
    {
135
    {
138
        String abtdXml = utilities.catDir (System.getenv("ABT_HOME"), "abtd.xml");
-
 
139
        DOMConfigurator.configure(abtdXml);
-
 
140
        mLogger.debug("main");
136
        mLogger.debug("main");
141
 
137
 
142
        mHostname = testEnvVar("GBE_HOSTNAME");
138
        mHostname = testEnvVar("GBE_HOSTNAME");
143
        testEnvVar("ANT_HOME");
139
        testEnvVar("ANT_HOME");
144
        testEnvVar("GBE_UNC");
140
        testEnvVar("GBE_UNC");
Line 146... Line 142...
146
        mGbeLog = testEnvVar("GBE_LOG");
142
        mGbeLog = testEnvVar("GBE_LOG");
147
        
143
        
148
        File gl = new File( mGbeLog );
144
        File gl = new File( mGbeLog );
149
        if ( !gl.isDirectory() )
145
        if ( !gl.isDirectory() )
150
        {
146
        {
151
            mLogger.fatal("main GBE_LOG is not a directory");
147
            mLogger.error("main GBE_LOG is not a directory");
152
            System.exit(1);
148
            System.exit(1);
153
        }
149
        }
154
 
150
 
155
        // Connection information for the database
151
        // Connection information for the database
156
        String connectionString = System.getenv("GBE_RM_LOCATION");
152
        String connectionString = System.getenv("GBE_RM_LOCATION");
Line 190... Line 186...
190
                username == null ||
186
                username == null ||
191
                username.length() == 0 ||
187
                username.length() == 0 ||
192
                password == null ||
188
                password == null ||
193
                password.length() == 0)
189
                password.length() == 0)
194
        {
190
        {
195
            mLogger.fatal("Usage: java -jar abtdD.jar -c connectionString -u username -p password");
191
            mLogger.error("Usage: java -jar abtdD.jar -c connectionString -u username -p password");
196
            System.exit(1);
192
            System.exit(1);
197
        }
193
        }
198
 
194
 
199
        BuildDaemon buildDaemon = new BuildDaemon(connectionString, username, password);
195
        BuildDaemon buildDaemon = new BuildDaemon(connectionString, username, password);
200
        buildDaemon.cleanUp();
196
        buildDaemon.cleanUp();
Line 210... Line 206...
210
    private static String testEnvVar(String varName) {
206
    private static String testEnvVar(String varName) {
211
        String envVar = System.getenv(varName);
207
        String envVar = System.getenv(varName);
212
 
208
 
213
        if ( envVar == null )
209
        if ( envVar == null )
214
        {
210
        {
215
            mLogger.fatal("main "+ varName +" not set");
211
            mLogger.error("main {} not set", varName);
216
            System.exit(1);
212
            System.exit(1);
217
        }
213
        }
218
        return envVar;
214
        return envVar;
219
    }
215
    }
220
 
216
 
Line 285... Line 281...
285
                
281
                
286
                // Wait 3 seconds before polling again
282
                // Wait 3 seconds before polling again
287
                daemonSleepSecs(mPollTime);
283
                daemonSleepSecs(mPollTime);
288
            }
284
            }
289
            
285
            
290
            mLogger.fatal("BuildDaemon daemon spawning shutdown");
286
            mLogger.error("BuildDaemon daemon spawning shutdown");
291
            if (mShutDown)
287
            if (mShutDown)
292
            {
288
            {
293
                notifyAllBuildThreads();
289
                notifyAllBuildThreads();
294
                cleanUp();
290
                cleanUp();
295
            }
291
            }
296
        }
292
        }
297
        catch( BuildException e )
293
        catch( BuildException e )
298
        {
294
        {
299
            mLogger.fatal("BuildDaemon caught Exception");
295
            mLogger.error("BuildDaemon caught Exception");
300
        }
296
        }
301
    }
297
    }
302
    
298
    
303
    /**
299
    /**
304
     * Used by the Polling Thread to notify slaves of an active build request
300
     * Used by the Polling Thread to notify slaves of an active build request
Line 313... Line 309...
313
 
309
 
314
            if (threadIdentifier.mThread.isAlive())
310
            if (threadIdentifier.mThread.isAlive())
315
            {
311
            {
316
                BuildThread bt = threadIdentifier.mThread;
312
                BuildThread bt = threadIdentifier.mThread;
317
                if (bt.mRconId == rconId ) {
313
                if (bt.mRconId == rconId ) {
318
                    mLogger.warn("Notify RtagId:"+ bt.mRtagId +", bmlId:" + bt.mRconId);
314
                    mLogger.warn("Notify RtagId:{}, bmlId:{}", bt.mRtagId, bt.mRconId );
319
                    synchronized (bt.mActiveBuildMonitor) {
315
                    synchronized (bt.mActiveBuildMonitor) {
320
                        bt.mActiveBuildMonitor.notifyAll();
316
                        bt.mActiveBuildMonitor.notifyAll();
321
                    }
317
                    }
322
                }
318
                }
323
            }
319
            }
Line 327... Line 323...
327
    /**
323
    /**
328
     * Notify all build threads of some activity
324
     * Notify all build threads of some activity
329
     * This will release all threads waiting on the ActiveBuildMonitor
325
     * This will release all threads waiting on the ActiveBuildMonitor
330
     */
326
     */
331
    public void notifyAllBuildThreads() {
327
    public void notifyAllBuildThreads() {
332
        mLogger.fatal("notifyAllBuildThreads");
328
        mLogger.error("notifyAllBuildThreads");
333
        for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
329
        for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
334
        {
330
        {
335
            ThreadIdentifier threadIdentifier = it.next();
331
            ThreadIdentifier threadIdentifier = it.next();
336
 
332
 
337
            if (threadIdentifier.mThread.isAlive())
333
            if (threadIdentifier.mThread.isAlive())
Line 351... Line 347...
351
     * 
347
     * 
352
     *  @param  releaseManager - The release Manager instance
348
     *  @param  releaseManager - The release Manager instance
353
     *  @param  utf            - Unit Test information 
349
     *  @param  utf            - Unit Test information 
354
     */
350
     */
355
    private void lookForNewBuildDaemons (ReleaseManager releaseManager, String utf) {
351
    private void lookForNewBuildDaemons (ReleaseManager releaseManager, String utf) {
356
        mLogger.fatal("lookForNewBuildDaemons" );
352
        mLogger.error("lookForNewBuildDaemons" );
357
        try
353
        try
358
        {
354
        {
359
            // Create a list of all machines that are configured to run on this machine
355
            // Create a list of all machines that are configured to run on this machine
360
            //
356
            //
361
            releaseManager.queryReleaseConfig(mHostname);
357
            releaseManager.queryReleaseConfig(mHostname);
Line 374... Line 370...
374
                    //  Ensure the Release is configured with exactly one master
370
                    //  Ensure the Release is configured with exactly one master
375
                    //
371
                    //
376
                    int masterCount = releaseManager.queryMasterCount( rc.getRtagId() );
372
                    int masterCount = releaseManager.queryMasterCount( rc.getRtagId() );
377
                    if ( masterCount != 1 )
373
                    if ( masterCount != 1 )
378
                    {
374
                    {
379
                        mLogger.fatal("BuildDaemon activating. Invalid Masters " + rc.getRtagId() + " MasterCount: " +  masterCount );
375
                        mLogger.error("BuildDaemon activating. Invalid Masters {} MasterCount: {}", rc.getRtagId(),  masterCount );
380
                        continue;
376
                        continue;
381
                    }
377
                    }
382
                    
378
                    
383
                    mLogger.warn("BuildDaemon activating " + rc.getRtagId() + " " + rc.getRconId() + " " + rc.getDaemonMode());
379
                    mLogger.warn("BuildDaemon activating {} {} {}",rc.getRtagId(), rc.getRconId(), rc.getDaemonMode());
384
                    
380
                    
385
                    //
381
                    //
386
                    //    Clone the BuildDaemons ReleaseManager thread
382
                    //    Clone the BuildDaemons ReleaseManager thread
387
                    //    Done so that we can perform unit testing by extending the ReleaseManager class
383
                    //    Done so that we can perform unit testing by extending the ReleaseManager class
388
                    //    Need to be able to have the Master/Slave threads process through the overridden
384
                    //    Need to be able to have the Master/Slave threads process through the overridden
Line 470... Line 466...
470
                //
466
                //
471
                //  Locate slaves that have an outstanding build request and master that have forced poll requests
467
                //  Locate slaves that have an outstanding build request and master that have forced poll requests
472
                //  Notify the threads of this condition
468
                //  Notify the threads of this condition
473
                ArrayList<Integer> rv = releaseManager.queryActivatedBuilds(mHostname, rconIdList.toString());
469
                ArrayList<Integer> rv = releaseManager.queryActivatedBuilds(mHostname, rconIdList.toString());
474
                for (Integer rconId : rv) {
470
                for (Integer rconId : rv) {
475
                    mLogger.warn("Activate: " + rconId);
471
                    mLogger.warn("Activate:{}", rconId);
476
                    notifyBuildThread(rconId);
472
                    notifyBuildThread(rconId);
477
                }
473
                }
478
            }
474
            }
479
            
475
            
480
        } catch (Exception e) {
476
        } catch (Exception e) {
481
            mLogger.fatal("lookForBuildRequests Exception:" + e.getMessage());
477
            mLogger.error("lookForBuildRequests Exception: {}", e.getMessage());
482
            mPollTime = 30;
478
            mPollTime = 30;
483
        }
479
        }
484
    
480
    
485
    }
481
    }
486
 
482
 
Line 509... Line 505...
509
            ThreadIdentifier threadIdentifier = it.next();
505
            ThreadIdentifier threadIdentifier = it.next();
510
 
506
 
511
            if (!threadIdentifier.getThread().isAlive())
507
            if (!threadIdentifier.getThread().isAlive())
512
            {
508
            {
513
                BuildThread bt = threadIdentifier.getThread();
509
                BuildThread bt = threadIdentifier.getThread();
514
                mLogger.warn("BuildDaemon removal " + bt.mRtagId + " " + bt.mRconId + " " + bt.getMode());
510
                mLogger.warn("BuildDaemon removal {} {} {}", bt.mRtagId, bt.mRconId, bt.getMode());
515
                it.remove();
511
                it.remove();
516
            }
512
            }
517
        }
513
        }
518
    }
514
    }
519
 
515
 
520
    /**calls isAlive on the Thread object associated with the rcon_id
516
    /**calls isAlive on the Thread object associated with the rcon_id
521
     */
517
     */
522
    public boolean isActive(final int rcon_id)
518
    public boolean isActive(final int rcon_id)
523
    {
519
    {
524
        mLogger.debug("isActive " + rcon_id);
520
        mLogger.debug("isActive {}", rcon_id);
525
        boolean retVal = false;
521
        boolean retVal = false;
526
        boolean found = false;
522
        boolean found = false;
527
 
523
 
528
        for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
524
        for (Iterator<ThreadIdentifier> it = mThreadCollection.iterator(); it.hasNext(); )
529
        {
525
        {
Line 537... Line 533...
537
                    retVal = true;
533
                    retVal = true;
538
                    break;
534
                    break;
539
                }
535
                }
540
                else
536
                else
541
                {
537
                {
542
                    mLogger.warn("isActive found dead thread " + rcon_id );
538
                    mLogger.warn("isActive found dead thread {}", rcon_id );
543
                }
539
                }
544
            }
540
            }
545
        }
541
        }
546
 
542
 
547
        if ( !found )
543
        if ( !found )
548
        {
544
        {
549
            mLogger.warn("isActive thread not found " + rcon_id);
545
            mLogger.warn("isActive thread not found {}", rcon_id);
550
        }
546
        }
551
 
547
 
552
        mLogger.debug("isActive returned " + retVal);
548
        mLogger.debug("isActive returned {}", retVal);
553
        return retVal;
549
        return retVal;
554
    }
550
    }
555
 
551
 
556
    /**
552
    /**
557
     *  Nagios interface
553
     *  Nagios interface
Line 615... Line 611...
615
    {
611
    {
616
        while (sleepSecs > 0)
612
        while (sleepSecs > 0)
617
        {
613
        {
618
            if (mShutDown)
614
            if (mShutDown)
619
            {
615
            {
620
                mLogger.fatal("daemonSleepSecs detected termiantion request");
616
                mLogger.error("daemonSleepSecs detected termiantion request");
621
                return true;
617
                return true;
622
            }
618
            }
623
            
619
            
624
            int sleepTime = sleepSecs;
620
            int sleepTime = sleepSecs;
625
            if (sleepTime > 5)
621
            if (sleepTime > 5)
Line 680... Line 676...
680
    }
676
    }
681
}
677
}
682
 
678
 
683
class BuildDaemonUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler
679
class BuildDaemonUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler
684
{
680
{
685
    private static final Logger mLogger = Logger.getLogger(BuildDaemonUncaughtExceptionHandler.class);
681
    private static final Logger mLogger = LoggerFactory.getLogger(BuildDaemonUncaughtExceptionHandler.class);
686
 
682
 
687
    //Implements Thread.UncaughtExceptionHandler.uncaughtException()
683
    //Implements Thread.UncaughtExceptionHandler.uncaughtException()
688
    public void uncaughtException(Thread th, Throwable ex)
684
    public void uncaughtException(Thread th, Throwable ex)
689
    {
685
    {
690
        System.out.println("You crashed thread " + th.getName());
686
        System.out.println("You crashed thread " + th.getName());
691
        System.out.println("Exception was: " + ex.toString());
687
        System.out.println("Exception was: " + ex.toString());
692
 
688
 
693
        mLogger.fatal("UncaughtException ThreadName: " + th.getName());
689
        mLogger.error("UncaughtException ThreadName: {}", th.getName());
694
        mLogger.fatal("UncaughtException was: " + ex.toString());
690
        mLogger.error("UncaughtException was: {}", ex);
695
    }
691
    }
696
}
692
}