Subversion Repositories DevTools

Rev

Rev 7044 | Rev 7082 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6914 dpurdie 1
package com.erggroup.buildtool.ripple;
2
 
3
import java.sql.Array;
4
import java.sql.CallableStatement;
5
import java.sql.Clob;
6
import java.sql.Connection;
7
import java.sql.DriverManager;
8
import java.sql.PreparedStatement;
9
import java.sql.ResultSet;
10
import java.sql.SQLException;
11
import java.sql.Timestamp;
12
import java.sql.Types;
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.Calendar;
16
import java.util.Date;
17
import java.util.GregorianCalendar;
18
import java.util.Iterator;
19
import java.util.List;
20
import java.util.ListIterator;
21
import java.util.concurrent.locks.ReentrantLock;
22
 
23
import oracle.sql.ArrayDescriptor;
24
 
7033 dpurdie 25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
6914 dpurdie 27
 
28
import com.erggroup.buildtool.ripple.RunLevel.BuildState;
29
import com.erggroup.buildtool.utilities.MutableDate;
30
 
31
/**Release Manager schema abstraction
32
 */
33
public class ReleaseManager implements Cloneable
34
{
35
    /**Unit test hook.
36
     * Prevents Oracle interaction when false.
37
     * @attribute
38
     */
7046 dpurdie 39
    public boolean mUseDatabase = true;
6914 dpurdie 40
 
41
    /**Debug Support.
42
     * Set to false if EnvVar GBE_BUILDTOOL_DEBUG exists
43
     * Value of GBE_BUILDTOOL_DEBUG is expected to be an email if greater than
44
     * one character.
45
     *
46
     * When set to false will:
47
     *    Prevents use of Oracle Mutex and potential lockout during debug sessions.
48
     *    Prevent the initiation of an indefinite pause.
49
     *    Force abt_usetestarchive, Add -testArchive to the assemble_dpkg command
50
     * @attribute
51
     */
52
    private static boolean mUseMutex = true;
53
 
54
    /** 
55
     * Reasons for building a package
56
     * The character code is inserted directly into the Release Manager Database
57
     * 
58
     */
59
    public enum BuildReason {
60
 
61
        Ripple('R'),
62
        Test('T'),
63
        NewVersion('N'),
64
        Restore('P');
65
 
66
        private char reason;
67
        private BuildReason(char reason) { this.reason = reason; }
68
        @Override
69
        public  String toString() { return String.valueOf(reason); }
70
        public static BuildReason fromValue(String value) {  
71
            if (value != null) {  
72
              for (BuildReason vReason : values()) {  
73
                if (vReason.reason == value.charAt(0) ) {  
74
                  return vReason;  
75
                }  
76
              }  
77
            }
78
            return null;  
79
        }
80
    }
81
 
82
    /**
83
     * The build result : Error, Complete ...
84
     * The character code is inserted directly into the Release Manager Database
85
     */
86
    public enum BuildResult {
87
 
88
        Building('B'),
89
        BuildError('E'),
90
        Complete('C'),
91
        SystemError('S');
92
 
93
        private char state;
94
        private BuildResult(char state) { this.state = state; }
95
        @Override
96
        public  String toString() { return String.valueOf(state); }
97
//        public static BuildResult fromValue(String value) {  
98
//            if (value != null) {  
99
//              for (BuildResult vState : values()) {  
100
//                if (vState.state == value.charAt(0) ) {  
101
//                  return vState;  
102
//                }  
103
//              }  
104
//            }
105
//            return null;  
106
//        }
107
    }
108
 
109
    /**package object of no consequence
110
     * @attribute
111
     */
112
    public static final Package NULL_PACKAGE = new Package();
113
 
114
    /**Logger
115
     * @attribute
116
     */
7033 dpurdie 117
    private static final Logger mLogger = LoggerFactory.getLogger(ReleaseManager.class);
6914 dpurdie 118
 
119
    /** Class to contain a lock and a connection as a single entity
120
     * 
121
     */
122
    static class RmConnection {
123
    	/**
124
    	 *  Connection to the database
125
    	 */
126
    	public Connection mConnection = null;
127
 
128
    	/**
129
    	 *  Lock used to control access to the connection
130
    	 */
131
    	public final ReentrantLock mSession = new ReentrantLock();
132
 
133
    	/** 
134
    	 * Close the connection on disconnect
135
    	 */
136
    	public boolean mCloseOnDisconnect = false;
137
 
138
    	public RmConnection ( boolean closeMe)
139
    	{
140
    		mCloseOnDisconnect = closeMe;
141
    	}
142
 
143
    	/**
144
    	 *  Close the connection - do not throw an error
145
    	 */
146
    	void closeConnection()
147
    	{
148
    		closeConnection(mConnection);
149
    	}
150
 
151
    	/**
152
    	 *  Close the specified connection - do not throw an error.
153
    	 *  To be used for general error handling
154
    	 * @param connection
155
    	 */
156
    	static void closeConnection( Connection connection )
157
    	{
158
            try {
159
                if (connection != null)
160
                	connection.close();
161
            }
162
            catch (SQLException e)
163
            {
7044 dpurdie 164
                mLogger.error("SQL Exception closing connection: {}", e.getMessage());
6914 dpurdie 165
            }
166
    	}
167
    }
168
 
169
    /**database connection for use when not planning a build
170
     * @attribute
171
     */
172
    private static RmConnection mNonPlanningConnection = new RmConnection(false);
173
 
174
    /**database session handle for use when planning a build
175
     * @attribute
176
     */
177
    private static RmConnection mPlanningConnection = new RmConnection(true);
178
 
179
    /**database session handle
180
     * note this handle is only ever set to mNonPlanningConnection or mPlanningConnection
181
     * @attribute
182
     */
183
    private Connection mConnection = null;
184
    private boolean    mIsConnected = false;
185
 
186
    /**thread synchronization governing database connection request queuing
187
     * this lock is used by master threads with a low priority planning requirement
188
     * use the fairness parameter to grant access to the longest waiting thread
189
     * @attribute
190
     */
191
    private static final ReentrantLock mLowPriorityQueue = new ReentrantLock(true);
192
 
193
    /**collection of ReleaseConfig objects
194
     * @attribute
195
     */
196
    public ReleaseConfigData mReleaseConfigCollection = new ReleaseConfigData();
197
 
198
    /**database connection string
199
     * @attribute
200
     */
201
    public String mConnectionString = "";
202
 
203
    /**database username
204
     * @attribute
205
     */
206
    private String mUsername = "";
207
 
208
    /**database password
209
     * @attribute
210
     */
211
    private String mPassword = "";
212
 
213
    /**collection of RunLevel objects
214
     * @attribute
215
     */
216
    public List<RunLevelData> mRunLevelCollection = new ArrayList<RunLevelData>();
217
 
218
    /**set in claimVersion, cleared in discardVersion
219
     * @attribute
220
     */
221
    private String mPlannedPkgId = null;
222
 
223
    /**set in claimVersion, cleared in discardVersion
224
     * @attribute
225
     */
226
 
227
    private String mPlannedPkgVersion = null;
228
 
229
    /**prevents inadvertently attempting a commit which releases record locks in between claimMutex and releaseMutex
230
     * @attribute
231
     */
232
    private boolean mDoNotCommit = false;
233
 
234
     /**
235
     * Controls the data collection mode.
236
     * True : daemon Mode (default)
237
     * False: escrow Mode
238
     */
239
    public boolean mDaemon = true;
240
 
241
    /**
242
     * Indication of the state of the RM mutex
243
     * Used only for reporting
244
     */
245
    public String mMutexState = "";
246
 
247
    /**
248
     *  The number of seconds to postpone a shut down of a daemon set
249
     *  after the thread has started. 
250
     *  
251
     *  This will prevent a daemon from being automatically disabled as soon
252
     *  as it is restarted
253
     *  
254
     *  Set to four hours
255
     */
256
    private static final long mPostponeShutdown = 4L * 60L * 60L;
257
 
258
 
259
    /**constructor
260
     * @param connectionString
261
     * @param username
262
     * @param password
263
     */
264
    public ReleaseManager(final String connectionString, final String username, final String password)
265
    {
7044 dpurdie 266
        mLogger.debug("ReleaseManager {}", connectionString);
6914 dpurdie 267
        mConnectionString = connectionString;
268
        mUsername = username;
269
        mPassword = password;
270
 
271
        String gbeBtDebug = System.getenv("GBE_BUILDTOOL_DEBUG");
272
        if ( gbeBtDebug != null )
273
        {
7033 dpurdie 274
            mLogger.error("GBE_BUILDTOOL_DEBUG set - Use of database mutex supressed");
6914 dpurdie 275
            setNoMutex();
276
        }
277
    }
278
 
279
    /**
280
     * Clone an instance of this class
281
     */
282
    @Override
283
    public Object clone() throws CloneNotSupportedException {
284
 
285
        //  Clone myself - using Java's build in stuff
286
        ReleaseManager clonedObj = (ReleaseManager) super.clone();
287
 
288
        //  Fix up several members
289
        //
290
        clonedObj.mConnection = null;
291
        clonedObj.mReleaseConfigCollection = new ReleaseConfigData();
292
        clonedObj.mRunLevelCollection = new ArrayList<RunLevelData>();
293
        clonedObj.mPlannedPkgId = null;
294
        clonedObj.mDoNotCommit = false;
295
        clonedObj.mPlannedPkgVersion = null;
296
 
297
        return clonedObj;
298
    }
299
 
300
 
301
    /**constructor used when schema information is unknown eg location, username, password
302
     */
303
    public ReleaseManager()
304
    {
305
        // inherit mConnectionString, mUsername, mPassword
306
        mLogger.debug("ReleaseManager");
307
    }
308
 
309
    /**
310
     * Set the mode of operation to be either escrow mode or daemon mode
311
     * 
312
     * @param isDaemon True: Daemon Mode
313
     *               False: Escrow Mode
314
     */
315
    public void setDaemonMode(boolean isDaemon)
316
    {
317
        mDaemon = isDaemon;
7044 dpurdie 318
        mLogger.debug("DaemonMode:{}", mDaemon);
6914 dpurdie 319
    }
320
 
321
    /**
322
     * Clear mUseMutex
323
     * Used only in test mode to prevent the use of a mutex 
324
     */
325
    public static void setNoMutex()
326
    {
327
        mUseMutex = false;
328
    }
329
 
330
    /**
331
     * Return the state of mUseMutex
332
     * True indicates that the system must use a Mutex to access data
333
     */
334
    public static boolean getUseMutex()
335
    {
336
        return mUseMutex;
337
    }
338
 
339
    /** Returns the current time
340
     * 
341
     *  Overridden in ReleaseManagerUtf
342
     */
343
    public long currentTimeMillis()
344
    {
345
        return System.currentTimeMillis();
346
    }
347
 
348
    /** Returns the applications major version number
349
     *  Used to stamp the generated XML files to ensure that producer and consumer match 
350
     * 
351
     * Overridden in ReleaseManagerUtf
352
     */
353
    public String getMajorVersionNumber()
354
    {
355
        return this.getClass().getPackage().getSpecificationVersion();
356
    }
357
 
358
    /**connect to oracle - in a non planning mode 
359
     * Overridden in ReleaseManagerUtf 
360
     */
361
    public void connect() throws SQLException
362
    {
363
        mLogger.debug("connect");
364
        mNonPlanningConnection = connect( mNonPlanningConnection );
365
    }
366
 
367
    /**connect to oracle - to plan a build
368
     * @param   priority - true: Connect with a high priority (last planning session resulted in a build)
369
     */
370
    public void connectForPlanning( boolean priority ) throws SQLException
371
    {
372
        mLogger.debug("connectForPlanning");
373
 
374
        if ( !priority )
375
        {
376
            // limit only one thread with a low priority build requirement to connect
377
            if ( mLowPriorityQueue.isHeldByCurrentThread() )
378
            {
379
                // by design a thread must NOT connect multiple times
380
                // this is to ensure the lock is claimed only once
381
                mLogger.error("connectForPlanning thread already has the lock");
382
            }
383
            else
384
            {
385
                mLogger.debug("connectForPlanning calling lock");
386
                mLowPriorityQueue.lock();
387
                mLogger.debug("connectForPlanning called lock");
388
            }
389
        }
390
 
391
        // threads with a high priority build requirement are not subject to the mLowPriorityQueue
392
        mPlanningConnection = connect( mPlanningConnection );    
393
    }
394
 
395
    /**Connect to oracle - finally
396
     *  @param  session         - Lock item to use
397
     *  @param  connection      - Connection to use 
398
     */
399
    private RmConnection connect( RmConnection rmc ) throws SQLException
400
    {
401
        mLogger.debug("connect");
402
 
403
        try
404
        {
405
            if ( rmc.mSession.isHeldByCurrentThread() )
406
            {
407
                // by design a thread must NOT connect multiple times
408
                // this is to ensure the lock is claimed only once
409
                mLogger.error("connect thread already has the lock");
410
            }
411
            else
412
            {
413
                mLogger.debug("connect calling lock");
414
                rmc.mSession.lock();
415
                mLogger.debug("connect called lock");
416
            }
417
 
418
            if ( !mUseDatabase )
419
            {
420
                mLogger.info("connect !mUseDatabase");
421
            }
422
            else
423
            {
424
                // DEVI 46868
425
                // loop indefinitely until a connection attempt succeeds
426
                // unless the failure is on the first attempt
427
                boolean problemConnecting;
428
 
429
                do
430
                {
431
                    mLogger.debug("connect check connection");
432
                    problemConnecting = false;
433
 
434
                    try
435
                    {
436
                        if ( rmc.mConnection == null || ( rmc.mConnection != null && !rmc.mConnection.isValid(10) ) )
437
                        {
438
                            mLogger.warn("connect calling getConnection");
439
                            rmc.mConnection = DriverManager.getConnection(mConnectionString, mUsername, mPassword);
440
                            // when connection to the database is established, the connection, by default, is in auto-commit mode
441
                            // to adhere to the design in the use of select for update, it is crucial to turn auto-commit off
442
                            // this also improves performance
443
                            rmc.mConnection.setAutoCommit(false);
444
                        }
445
                    }
446
                    catch(SQLException e)
447
                    {
448
                        mLogger.warn("connect determined problem connecting");
449
                        problemConnecting = true;
450
                        try
451
                        {
452
                            // sleep 30 secs
453
                            mLogger.warn("connect getConnection failed. sleep 30secs");
454
                            Thread.sleep(30000);
455
                        }
456
                        catch (InterruptedException f)
457
                        {
458
                            mLogger.warn("connect caught InterruptedException");
459
                            Thread.currentThread().interrupt();
460
                        }
461
 
462
                        if ( rmc.mConnection == null )
463
                        {
464
                            // failed on first connection attempt - unlikely due to database loading - likely bad connection parameters
465
                            throw new SQLException();
466
                        }
467
                    }
468
                } while ( problemConnecting );
469
                mLogger.debug("connect checked connection");
470
            }
471
        }
472
        finally
473
        {
474
            mConnection = rmc.mConnection;
475
            mIsConnected = true;
476
            mLogger.debug("connect finally connection");
477
        }
478
        return rmc;
479
 
480
    }
481
 
482
    /**Disconnect from oracle Database 
483
     * <br>Is overridden in ReleaseManagerUtf for test purposes 
484
     */
485
    public void disconnect()
486
    {
487
        mLogger.debug("disconnect");
488
 
489
        disconnect( mNonPlanningConnection );
490
    }
491
 
492
    /**Disconnect from oracle
493
     * @param   priority    - True: High priority planning thread
494
     */
495
    public void disconnectForPlanning( boolean priority )
496
    {
497
        mLogger.debug("disconnectForPlanning");
498
 
499
        if ( !priority )
500
        {
501
            // allow another low priority thread to connect
502
            try {
503
                mLowPriorityQueue.unlock();
504
            }
505
            catch( IllegalMonitorStateException e ) {
7033 dpurdie 506
                mLogger.error("disconnectForPlanning. IllegalMonitorStateException exception");
6914 dpurdie 507
            }
508
        }
509
 
510
        disconnect( mPlanningConnection );
511
    }
512
 
513
    /**Disconnect from oracle
514
     * Internal oracle disconnection method. Wrapped by publicly available disconnection methods
515
     * @param   session. Session Lock
516
     */
517
    private void disconnect( RmConnection connection )
518
    {
519
        mLogger.debug("disconnect");
520
 
521
        // The planning connection is flagged to close on disconnect
522
        // This is an attempt to prevent database deadlocks - which have been seen
523
        // under unknown conditions.
524
        // Done before the lock is released
525
        if (connection.mCloseOnDisconnect)
526
        {
527
            mLogger.debug("disconnect close on disconnect");
528
        	connection.closeConnection();
529
        }
530
 
531
        // by design, a thread may call disconnect multiple times
532
        // this is a technique used in finally blocks
533
        // it is to ensure the lock is released in all cases
534
        // only unlock if it is held by this thread
535
        // when unlock is called on a ReentrantLock held by this thread
536
        // the hold count is decremented
537
        // connect should only let the hold count be incremented to 1
538
        // when the hold count is 0 the lock is released
539
        // and the ReentrantLock is no longer held by this thread
540
        // only call unlock when the lock is held by this thread
541
        if ( connection.mSession.isHeldByCurrentThread() )
542
        {
543
            mLogger.debug("disconnect calling unlock");
544
            try {
545
                connection.mSession.unlock();
546
            }
547
            catch (IllegalMonitorStateException e) {
7033 dpurdie 548
                mLogger.error("disconnect. IllegalMonitorStateException exception");
6914 dpurdie 549
            }
550
            mLogger.debug("disconnect called unlock");
551
        }
552
 
553
        mIsConnected = false;
554
    }
555
 
556
    /** Common processing SQL Exceptions
557
     *  If it is a connection error then a new SQLException will be thrown otherwise
558
     *  its an error with the SQL itself and a normal Exception will be thrown.
559
     *  
560
     * @param e                 - Exception being thrown
561
     * @param instanceId        - Method suffix to differentiate multiple methods of the same name
562
     * 
563
     * @throws SQLException
564
     * @throws Exception
565
     */
566
    void handleSQLException(SQLException e, String instanceId ) throws SQLException, Exception
567
    {
568
        String callingMethodName = Thread.currentThread().getStackTrace()[2].getMethodName() + instanceId ;
7044 dpurdie 569
        mLogger.error("{} sql exception:{}", callingMethodName, e.getMessage());
6914 dpurdie 570
 
571
        if ( mConnection == null || !mConnection.isValid(10) )
572
        {
7044 dpurdie 573
            mLogger.error( "{} database access error only", callingMethodName);
6914 dpurdie 574
            RmConnection.closeConnection(mConnection);
575
            throw new SQLException(e);
576
        }
577
        else
578
        {
7044 dpurdie 579
            mLogger.error( "{} show stopper", callingMethodName);
6914 dpurdie 580
            throw new Exception(callingMethodName + " show stopper");
581
        }
582
    }
583
 
584
    /** Claim the Build System Planning Lock
585
     * Essentially locks the row in the BUILD_SERVICE_CONFIG table with a service of MUTEX
586
     * for the duration of the transaction this prevents other MasterThreads from 
587
     * generating build files in parallel and hence prevents planned version numbering contention 
588
     */
589
    public void claimMutex() throws SQLException, Exception
590
    {
591
        mLogger.debug("claimMutex");
592
        mMutexState = "Claiming Mutex";
593
        if ( mUseDatabase && mUseMutex )
594
        {
595
            try
596
            {
597
                CallableStatement stmt = mConnection.prepareCall("select CONFIG from release_manager.BUILD_SERVICE_CONFIG WHERE SERVICE='MUTEX' FOR UPDATE");
7033 dpurdie 598
                mLogger.error("claimMutex calling stmt.executeUpdate");
6914 dpurdie 599
                stmt.executeUpdate();
7033 dpurdie 600
                mLogger.error("claimMutex called stmt.executeUpdate");
6914 dpurdie 601
                stmt.close();
602
                mDoNotCommit = true;
603
                mMutexState = "Holding Mutex";
604
            }
605
            catch ( SQLException e )
606
            {
607
                mMutexState = "Mutex Claim Exception";
608
                handleSQLException(e, "");
609
            }
610
 
611
            // about to start the planning process again, discard previous
612
            discardVersions();
613
        }
614
    }
615
 
616
    /** Release the Build System Planning Lock 
617
     *  Essentially unlocks the row in the BUILD_SERVICE_CONFIG table with a service of MUTEX
618
     */
619
    public void releaseMutex() throws SQLException, Exception
620
    {
621
        mLogger.debug("releaseMutex");
622
        mMutexState = "Releasing Mutex";
623
        if ( mUseDatabase )
624
        {
625
            try
626
            {
627
                mDoNotCommit = false;
7033 dpurdie 628
                mLogger.error("releaseMutex calling commit");
6914 dpurdie 629
                commit();
7033 dpurdie 630
                mLogger.error("releaseMutex called commit");
6914 dpurdie 631
                mMutexState = "Released Mutex";
632
            }
633
            catch ( SQLException e )
634
            {
635
                mMutexState = "Mutex Release Exception";
636
                handleSQLException(e, "");
637
            }
638
        }
639
    }
640
 
641
    /**central commit protection
642
     */
643
    private void commit() throws SQLException, Exception
644
    {
645
        mLogger.debug("commit");
646
        if ( mUseDatabase )
647
        {
648
            if ( mDoNotCommit )
649
            {
650
                mLogger.error("commit attempted commit with mDoNotCommit set, this is a programming error");
651
            }
652
            else
653
            {
654
                mConnection.commit();
655
            }
656
        }
657
    }
658
 
659
    /**Flag a Build System Pause in the database
660
     * This is aimed at stopping ALL daemons dead in the water
661
     * 
662
     * Used when handling an unsupported exception case in either the main or slave daemons
663
     * typically an SQLException other than a database connection related on
664
     *
665
     * @param mRecoverable  True: Recoverable pause wait until recovery is detected
666
     *                  <br>False: Unrecoverable pause. Wait for 20 minutes 
667
     */
668
    public void indefinitePause(boolean mRecoverable)
669
    {
670
        mLogger.debug("indefinitePause");
671
        if ( mUseDatabase )
672
        {
673
            String sqlStr;
674
            if ( mRecoverable) {
675
                sqlStr = "begin PK_BUILDAPI.set_infinite_pause(); end;";
676
            }
677
            else {
678
                sqlStr = "begin PK_BUILDAPI.set_finite_pause(20); end;";
679
            }
680
            try
681
            {
682
                connect();
683
                CallableStatement stmt = mConnection.prepareCall( sqlStr );
684
                stmt.executeUpdate();
685
                stmt.close();
686
                commit();
687
            }
688
            catch( SQLException e )
689
            {
690
                // do not throw Exception
691
                // this is part of Exception handling
7044 dpurdie 692
                mLogger.error( "indefinitePause caught SQLException {}", e.getMessage() );
6914 dpurdie 693
                RmConnection.closeConnection(mConnection);
694
            }
695
            catch( Exception e )
696
            {
7044 dpurdie 697
                mLogger.error( "indefinitePause caught Exception {}", e.getMessage() );
6914 dpurdie 698
            }
699
            finally
700
            {
701
                // this block is executed regardless of what happens in the try block
702
                // even if an exception is thrown
703
                // ensure disconnect
704
                try
705
                {
706
                    disconnect();
707
                }
708
                catch( Exception e )
709
                {
710
                    // do not throw Exception
711
                    // this is part of Exception handling
7044 dpurdie 712
                    mLogger.error( "indefinitePause2 caught Exception {}", e.getMessage() );
6914 dpurdie 713
                    RmConnection.closeConnection(mConnection);
714
                }
715
            }
716
        }
717
    }
718
 
719
    /**ensures a run_level_schedule row with a non null indefinite_pause column does not exist
720
     * this is aimed at resuming all daemons
721
     * 
722
     * Assumes connection to database has been established
723
     */
724
    public void resumeIndefinitePause() throws SQLException, Exception
725
    {
726
        mLogger.debug("resume");
727
        if ( mUseDatabase )
728
        {
729
            try
730
            {
731
                CallableStatement stmt = mConnection.prepareCall( "begin PK_BUILDAPI.SET_RESUME(); end;" );
732
                stmt.executeUpdate();
733
                stmt.close();
734
                commit();
735
            }
736
            catch ( SQLException e )
737
            {
738
                handleSQLException(e, "");
739
            }
740
        }
741
    }
742
 
743
 
744
    /**
745
     *  Only used in daemon mode
746
     *  Update the Database information to show the package being built
747
     * 
748
     * @param pkgId  Identify the package (name) being built
749
     * 
750
     * @param pkgVersion
751
     *                The version at which the package is being built
752
     * 
753
     * @param rtagId The release Id in which the package is being built
754
     * 
755
     * @exception SQLException
756
     * @exception Exception
757
     */
758
    void claimVersion(int pkgId, String pkgVersion, int rtagId) throws SQLException, Exception
759
    {
7044 dpurdie 760
        mLogger.debug("claimVersion {} {}", pkgId, pkgVersion);
6914 dpurdie 761
        if ( mUseDatabase )
762
        {
763
            CallableStatement stmt1 = null;
764
            try
765
            {
766
                if (isRtagIdConfigured( rtagId ))
767
                {
768
                    stmt1 = mConnection.prepareCall(
769
                            "insert into release_manager.planned_versions (pkg_id, pkg_version, planned_time) values (?,?, sysdate)");
770
                    stmt1.setInt(1, pkgId);
771
                    stmt1.setString(2, pkgVersion);
772
                    stmt1.executeUpdate();
773
 
774
                    mPlannedPkgId = String.valueOf(pkgId);
775
                    mPlannedPkgVersion = pkgVersion;
776
                }
777
            }
778
            catch ( SQLException e )
779
            {
780
                handleSQLException(e, "");
781
            }
782
            finally {
783
                if (stmt1 != null)
784
                    stmt1.close();
785
            }
786
        }
787
    }
788
 
789
    /**Only used in daemon mode
790
     * <br>Delete planned package package information from the last planned build
791
     * <p>
792
     * Note: There is an aging process to cleanup entries that are left around due to
793
     *       unforeseen errors.
794
     * <p>      
795
     * Resets mPlannedPkgId and mPlannedPkgVersion to null
796
     * 
797
     * @exception   SQLException
798
     * @exception   Exception
799
     */
800
    public void discardVersion() throws SQLException, Exception
801
    {
7033 dpurdie 802
        mLogger.error("discardVersion");
6914 dpurdie 803
 
804
        if ( mPlannedPkgId != null && mPlannedPkgVersion != null )
805
        {
806
            try
807
            {
808
                connect();
809
                CallableStatement stmt = mConnection.prepareCall(
810
                    "delete from release_manager.planned_versions" +
811
                    " where pkg_id=" + mPlannedPkgId + 
812
                    "   and pkg_version='" + mPlannedPkgVersion + "'");
813
                stmt.executeUpdate();
814
                stmt.close();
815
                commit();
816
                mPlannedPkgId = null;
817
                mPlannedPkgVersion = null;
818
            }
819
            catch ( SQLException e )
820
            {
821
                handleSQLException(e, "");
822
            }
823
            finally
824
            {
825
                disconnect();
826
            }
827
        }
828
    }
829
 
830
    /** Delete planned versions over 24 hours old (rounded to the nearest hour that is) 
831
     *  Only used in daemon mode 
832
     */
833
    private void discardVersions() throws SQLException, Exception
834
    {
835
        mLogger.debug("discardVersions");
836
        try
837
        {
838
            // housekeeping whilst the daemon has the mutex
839
            // trunc(sysdate, 'hh') returns the time now rounded to the nearest hour
840
            // trunc(sysdate, 'hh') - 1 returns the time 24 hours ago rounded to the nearest hour
841
            // this statement does not return any rows when planned_time is null, though this should never be the case
842
            CallableStatement stmt = mConnection.prepareCall("delete from release_manager.planned_versions where planned_time < trunc(sysdate, 'hh') - 1");
843
            stmt.executeUpdate();
844
            stmt.close();
845
        }
846
        catch ( SQLException e )
847
        {
848
            handleSQLException(e, "");
849
        }
850
    }
851
 
852
    /**Update the Database information to show that a package is being built 
853
     * Only used in daemon mode
854
     * 
855
     *  @param rconId - Identifies the Release Config table entry
856
     *  @param pkgId - Identifies the name of the package being built
857
     *  @param pkgPvId - Identifies the packageVersion the build is based upon
858
     */
859
    public void setCurrentPackageBeingBuilt(int rconId, int pkgId, int pkgPvId) throws SQLException, Exception
860
    {
7044 dpurdie 861
        mLogger.error("setCurrentPackageBeingBuilt {}", rconId);
6914 dpurdie 862
        if ( mUseDatabase )
863
        {
864
            CallableStatement stmt = null;
865
            try
866
            {
867
                connect();
868
 
869
                stmt = mConnection.prepareCall(
870
                        "update release_manager.run_level rl"+
871
                        " set current_pkg_id_being_built=?"+
872
                        "    ,current_pv_id=?" +
873
                        "    ,last_build=sysdate" +
874
                        " where rl.rcon_id=?" 
875
                        );
876
                stmt.setInt(1, pkgId);
877
                stmt.setInt(2, pkgPvId);
878
                stmt.setInt(3, rconId);                    
879
                stmt.executeUpdate();
880
 
881
                commit();
882
            }
883
            catch ( SQLException e )
884
            {
885
                handleSQLException(e, "");
886
            }
887
            finally
888
            {
889
                // this block is executed regardless of what happens in the try block
890
                // even if an exception is thrown
891
                // ensure disconnect
892
                if (stmt != null)
893
                    stmt.close();
894
                disconnect();
895
            }
896
        }
897
    }
898
 
899
 
900
    /**Update the Database information to show that no package is being built 
901
     * Only used in daemon mode
902
     * 
903
     *  @param rconId - Identifies the Release Config table entry
904
     */
905
    public void clearCurrentPackageBeingBuilt(int rconId) throws SQLException, Exception
906
    {
7044 dpurdie 907
        mLogger.error("clearCurrentPackageBeingBuilt {}", rconId);
6914 dpurdie 908
        if ( mUseDatabase )
909
        {
910
            try
911
            {
912
                connect();
913
 
914
                CallableStatement stmt4 = mConnection.prepareCall(
915
                        "update release_manager.run_level" +
916
                        " set current_pkg_id_being_built=NULL,"+
917
                        "     current_pv_id=NULL," +
918
                        "     current_build_files=NULL" +
919
                        " where rcon_id=" + rconId
920
                         );
921
                stmt4.executeUpdate();
922
                stmt4.close();
923
                commit();
924
            }
925
            catch ( SQLException e )
926
            {
927
                handleSQLException(e, "");
928
            }
929
            finally
930
            {
931
                // this block is executed regardless of what happens in the try block
932
                // even if an exception is thrown
933
                // ensure disconnect
934
                disconnect();
935
            }
936
        }
937
    }
938
 
939
    /**
940
     * Executes the AutoMakeRelease stored procedure with the passed parameters
941
     *          Handles database connection/disconnection
942
     * 
943
     * @param mReporting
944
     * 
945
     * @return A publishing error
946
     *         True: A publishing error that only affect the current package
947
     *         False: No publishing error
948
     * @exception SQLException
949
     * @exception Exception A publishing error that affects the entire build system.
950
     *                      This will cause an 'indefinite pause'
951
     */
952
    public boolean autoMakeRelease(ReportingData mReporting) throws SQLException, Exception
953
    {
954
 
955
        mReporting.errMsg = null;
7044 dpurdie 956
        mLogger.debug("autoMakeRelease {}", mReporting.packageName);
6914 dpurdie 957
        if ( mUseDatabase )
958
        {
959
            try
960
            {
961
                //  Create an array of dependencies
962
                //  Convert the string mReporting.packageDepends into an array
963
                //      mReporting.packageDepends is of the form 'PackageName','PackageVersion';'PackageName2','PackageVersion2'
964
                String [] depArrayData = mReporting.packageDepends.split(";"); 
965
                ArrayDescriptor desc = ArrayDescriptor.createDescriptor("RELEASE_MANAGER" + "." + "RELMGR_VARCHAR2_TAB_T", mConnection);
966
                Array depArray = new oracle.sql.ARRAY(desc, mConnection, depArrayData);                
967
 
968
                connect();
969
                CallableStatement stmt = mConnection.prepareCall( "begin ? := PK_RMAPI.AUTO_MAKE_VCSRELEASE2(?,?,?,?,?,?,?,?); end;" );
970
                stmt.registerOutParameter( 1, Types.INTEGER);
971
                stmt.setLong( 2, mReporting.rtagId );
972
                stmt.setString( 3, mReporting.packageName );
973
                stmt.setString( 4, mReporting.packageExtension );
974
                stmt.setString( 5, mReporting.packageVersion );
975
                stmt.setString( 6, mReporting.newVcsTag );
976
                stmt.setArray ( 7, depArray );
977
                stmt.setInt   ( 8, mReporting.isRipple ? 1 : 0 );
978
                stmt.setString( 9, "buildadm" );
979
                stmt.executeUpdate();
980
                int result = stmt.getInt( 1 );
981
 
982
                //
983
                //  Return values
984
                //      >0 PVID of package
985
                //      -1 Package not found in pending table
986
                //      -2 Package already exists
987
                //      -3 Not approved for auto build
988
                //      -4 Package Migrated to SVN being built from CC tag
989
                //      -5 Rippled Package: Source path changed
990
                //  Sql Application Errors cause an SQLException
991
                //         Rtagid is NULL
992
                //         No Package Name
993
                //         No Package Version
994
                //         No Package VCS
995
                //         Bad IsRipple value
996
                //         No User Name
997
                //         Malformed VCS Tag
998
                //         Database missing VCS tag
999
                //         Invalid UserName
1000
                //
1001
 
1002
                //
1003
                //  Report per-package errors directly
1004
                //  Exceptions are for errors that need to halt the entire system
1005
                //
1006
                if ( result <= 0 )
1007
                {
7044 dpurdie 1008
                    mLogger.error("autoMakeRelease PK_RMAPI.AUTO_MAKE_VCSRELEASE failed, returned {}", result);
6914 dpurdie 1009
                    if ( result == -4 ) {
1010
                        mReporting.errMsg = "Package migrated to SVN being built from CC tag";
1011
                    } else if ( result == -3 ) {
1012
                        mReporting.errMsg = "Package not approved for autobuild";
1013
                    } else if ( result == -2 ) {
1014
                        // This is OK
1015
                    }  else if ( result == -1 ) {
1016
                        mReporting.errMsg = "Package Version no longer pending";
1017
                    }  else if ( result == -5 ) {
1018
                        mReporting.errMsg = "VCS Source path changed in ripple build";
1019
                    } else {
1020
                        // Don't know this error - so its fatal
1021
                        throw new Exception("autoMakeRelease show stopper PK_RMAPI.AUTO_MAKE_VCSRELEASE failed, returned " + result);
1022
                    }
1023
                }
1024
                else
1025
                {
1026
 
1027
                    //  Now that the package-version has been created in the database, we have the pv_id of the
1028
                    //  new package. Use this in the reporting process.
1029
                    mReporting.packageVersionId = result;
1030
                }
1031
 
1032
                stmt.close();
1033
                commit();
1034
            }
1035
            catch ( SQLException e )
1036
            {
1037
                handleSQLException(e, "");
1038
            }
1039
            finally
1040
            {
1041
                // this block is executed regardless of what happens in the try block
1042
                // even if an exception is thrown
1043
                // ensure disconnect
1044
                disconnect();
1045
            }
1046
        }
1047
        return (mReporting.errMsg != null);
1048
    }
1049
 
1050
    /** Executes the insertPackageMetrics stored procedure with the passed parameters
1051
     *  Handles database connection/disconnection
1052
     */
1053
    public void insertPackageMetrics(int mRtagId, String packageName, 
1054
            String packageExtension, String metrics) throws SQLException, Exception
1055
    {
7044 dpurdie 1056
        mLogger.debug("insertPackageMetrics {}", packageName);
6914 dpurdie 1057
        if ( mUseDatabase )
1058
        {
1059
            try
1060
            {
1061
                connect();
1062
                CallableStatement stmt = mConnection.prepareCall( "begin ? := PK_RMAPI.INSERT_PACKAGE_METRICS(?,?,?,?); end;" );
1063
                stmt.registerOutParameter( 1, Types.INTEGER);
1064
                stmt.setInt   ( 2, mRtagId );
1065
                stmt.setString( 3, packageName );
1066
                stmt.setString( 4, packageExtension );
1067
                stmt.setString( 5, metrics );
1068
                stmt.executeUpdate();
1069
                int result = stmt.getInt( 1 );
1070
 
1071
                if ( result != 0 )
1072
                {
1073
                    // flag build failure
7044 dpurdie 1074
                    mLogger.error("insertPackageMetrics show stopper PK_RMAPI.INSERT_PACKAGE_METRICS failed, returned {}", result);
6914 dpurdie 1075
                    throw new Exception("insertPackageMetrics show stopper PK_RMAPI.INSERT_PACKAGE_METRICS failed, returned" + result);
1076
                }
1077
                stmt.close();
1078
                commit();
1079
            }
1080
            catch ( SQLException e )
1081
            {
1082
                handleSQLException(e, "");
1083
            }
1084
            finally
1085
            {
1086
                // this block is executed regardless of what happens in the try block
1087
                // even if an exception is thrown
1088
                // ensure disconnect
1089
                disconnect();
1090
            }
1091
        }
1092
    }
1093
 
1094
    /**
1095
     * Add entry to the Build_Instance table
1096
     * Returns the build instance number. This will be used to cross reference unit tests results
1097
     * 
1098
     * @throws throws SQLException, Exception 
1099
     * 
1100
     */
1101
    public int createBuildInstance(int rtagId, int pvId, BuildReason reason) throws SQLException, Exception
1102
    {
1103
        int buildId = 0;
1104
 
7044 dpurdie 1105
        mLogger.debug("createBuildInstance {}:{}", rtagId, pvId);
6914 dpurdie 1106
        if ( mUseDatabase )
1107
        {
1108
            try
1109
            {
1110
                connect();
1111
 
1112
                CallableStatement stmt = mConnection.prepareCall("begin ? := PK_RMAPI.new_build_instance(?,?,?); end;");
1113
                stmt.registerOutParameter( 1, Types.INTEGER);
1114
                stmt.setLong( 2,rtagId );
1115
                stmt.setLong( 3,pvId );
1116
                stmt.setString( 4,reason.toString() );
1117
                stmt.executeUpdate();
1118
                buildId = stmt.getInt( 1 );
1119
 
7044 dpurdie 1120
                mLogger.warn("createBuildInstance: Build Instance ID: {}", buildId);
6914 dpurdie 1121
 
1122
                stmt.close();
1123
                commit();
1124
            }
1125
            catch ( SQLException e )
1126
            {
1127
                handleSQLException(e, "");
1128
            }
1129
            finally
1130
            {
1131
                // this block is executed regardless of what happens in the try block
1132
                //      even if an exception is thrown
1133
                //      ensure disconnect
1134
                disconnect();
1135
            }
1136
        }
1137
        return buildId;
1138
    }
1139
 
1140
    /**
1141
     * Update the Build_Instance table.
1142
     * <br>Record the state of the current build in the database
1143
     * 
1144
     * @param buildId  - Build ID of build
1145
     * @param pvId     - PV_ID of the build
1146
     * @param result   - Outcome  of the build
1147
     * 
1148
     * Returns the internal error code. <0 == error
1149
     * 
1150
     * @throws throws SQLException, Exception 
1151
     * 
1152
     */
1153
    public int updateBuildInstance(int buildID, int pvId, BuildResult result) throws SQLException, Exception
1154
    {
1155
        int rv = 0;
1156
 
7044 dpurdie 1157
        mLogger.debug("updateBuildInstance {}:{}:{}", buildID, pvId, result);
6914 dpurdie 1158
        if ( mUseDatabase )
1159
        {
1160
            try
1161
            {
1162
                connect();
1163
 
1164
                String sql = "begin ? := PK_RMAPI.update_build_instance(?,?,?); end;";
1165
 
1166
                CallableStatement stmt = mConnection.prepareCall(sql);
1167
                stmt.registerOutParameter( 1, Types.INTEGER);
1168
                stmt.setLong( 2,buildID );
1169
                stmt.setLong( 3,pvId );
1170
                stmt.setString( 4,result.toString() );
1171
                stmt.executeUpdate();
1172
                rv = stmt.getInt( 1 );
1173
 
7044 dpurdie 1174
                mLogger.warn("updateBuildInstance: Result: {}", rv);
6914 dpurdie 1175
 
1176
                stmt.close();
1177
                commit();
1178
            }
1179
            catch ( SQLException e )
1180
            {
1181
                handleSQLException(e, "");
1182
            }
1183
            finally
1184
            {
1185
                // this block is executed regardless of what happens in the try block
1186
                //      even if an exception is thrown
1187
                //      ensure disconnect
1188
                disconnect();
1189
            }
1190
        }
1191
        return rv;
1192
    }
1193
 
1194
    /**
1195
     * Insert Test Results into Release Manager
1196
     * Manage connection and disconnection
1197
     * 
1198
     * @param buildId
1199
     * @param btr
1200
     * @throws Exception, SQLException 
1201
     */
1202
    public void insertTestResults(int buildId, BuildTestResults btr) throws Exception, SQLException
1203
    {
1204
        if (buildId <= 0)
1205
        {
1206
            mLogger.warn("insertTestResults: Invalid build Id");
1207
            return;
1208
        }
1209
 
1210
        if ( !mUseDatabase || !btr.mResultsFound )
1211
        {
1212
            return;
1213
        }
1214
 
7044 dpurdie 1215
        mLogger.warn("insertTestResults: {}, Number:{}", buildId, btr.mTestResults.size() );
6914 dpurdie 1216
        try
1217
        {
1218
            connect();
1219
            String sql = "BEGIN ? := PK_RMAPI.insert_test_run(?, ?, ?, ?, ?, ?, ?);end;";
1220
            CallableStatement stmt = mConnection.prepareCall(sql);
1221
            stmt.registerOutParameter( 1, Types.INTEGER);
1222
            stmt.setLong(2, buildId);
1223
            Clob myClob = mConnection.createClob();
1224
 
1225
            for (int ii = 0; ii < btr.mTestResults.size(); ii++)
1226
            {
1227
                //System.out.println("Data: " +data.testResults.get(ii).toString());
1228
 
1229
                BuildTestResults.testResultData td = btr.mTestResults.get(ii);
1230
                if (td.platform != null)
1231
                {
1232
                    stmt.setString(3, td.testName);
1233
                    stmt.setString(4, td.outcome);
1234
                    stmt.setString(5, td.platform);
1235
                    stmt.setString(6, td.type);
1236
                    if (td.duration ==  null) {
1237
                        stmt.setNull(7, Types.NUMERIC);
1238
                    } else {
1239
                        stmt.setLong  (7, td.duration);
1240
                    }
1241
 
1242
                    // Insert CLOB data, or a null
1243
                    if (td.message == null)
1244
                    {
1245
                        stmt.setNull(8,Types.CLOB);
1246
                    }
1247
                    else
1248
                    {
1249
                        myClob.truncate(0);
1250
                        myClob.setString(1, td.message);
1251
                        stmt.setClob(8, myClob);
1252
                    }
1253
 
1254
                    stmt.execute();
1255
                    int rv = stmt.getInt( 1 );
1256
 
1257
                    if (rv != 1)
1258
                    {
7033 dpurdie 1259
                        mLogger.error("insertTestResults show stopper. Insert error");
6914 dpurdie 1260
                        throw new Exception("insertTestResults show stopper. Insert error");
1261
                    }
1262
                }
1263
            }
1264
            stmt.close();
1265
 
1266
        }
1267
        catch ( SQLException e )
1268
        {
1269
            handleSQLException(e, "");
1270
        }
1271
        finally
1272
        {
1273
            // this block is executed regardless of what happens in the try block
1274
            // even if an exception is thrown
1275
            // ensure disconnect
1276
            disconnect();
1277
        }
1278
    }
1279
 
1280
    /**executes the get_daemon_inst function with the passed parameters
7023 dpurdie 1281
     * @param   di          - Daemon Instruction Control Data Information
1282
     * 
6914 dpurdie 1283
     * @return  true if an instruction exists - for use in a while loop
1284
     */
7023 dpurdie 1285
    public boolean getDaemonInst(DaemonInstruction di ) throws SQLException, Exception
6914 dpurdie 1286
    {
7044 dpurdie 1287
        mLogger.debug("getDaemonInst {}", di.instruction);
6914 dpurdie 1288
        boolean retVal = false;
1289
 
1290
        if ( mUseDatabase )
1291
        {
1292
            try
1293
            {
7023 dpurdie 1294
                CallableStatement stmt = mConnection.prepareCall( "begin ? := PK_BUILDAPI.GET_DAEMON_INST(?,?,?,?,?,?,?); end;" );
6914 dpurdie 1295
                stmt.registerOutParameter(1, Types.INTEGER);
1296
                stmt.registerOutParameter(3, Types.INTEGER);
1297
                stmt.registerOutParameter(4, Types.INTEGER);
1298
                stmt.registerOutParameter(5, Types.INTEGER);
1299
                stmt.registerOutParameter(6, Types.INTEGER);
1300
                stmt.registerOutParameter(7, Types.INTEGER);
7023 dpurdie 1301
                stmt.setInt(2, di.rtag_id );
1302
                stmt.setInt( 3, di.instruction );
1303
                stmt.setInt( 4, di.opCode );
1304
                stmt.setInt(8, di.expired ? 1 : 0);
6914 dpurdie 1305
                stmt.execute();
1306
                int result = stmt.getInt( 1 );
1307
 
1308
                if ( result == 1 )
1309
                {
1310
                    retVal = true;
7023 dpurdie 1311
                    di.instruction = stmt.getInt( 3 );
1312
                    di.pvId = stmt.getInt( 5 );
1313
                    di.userId = stmt.getInt( 6 );
6914 dpurdie 1314
 
1315
                    //
1316
                    //  Convert userId into an email address
1317
                    //
1318
                    CallableStatement stmt1 = mConnection.prepareCall(
7023 dpurdie 1319
                            "select user_email from release_manager.users where user_id=" + di.userId);
6914 dpurdie 1320
                    ResultSet rset1 = stmt1.executeQuery();
1321
 
1322
                    while( rset1.next() )
1323
                    {
7023 dpurdie 1324
                        di.userEmail = rset1.getString("user_email");
6914 dpurdie 1325
                        if (rset1.wasNull())
1326
                        {
7023 dpurdie 1327
                            di.userEmail = "";
6914 dpurdie 1328
                        }
1329
                    }
1330
 
1331
                    rset1.close();
1332
                    stmt1.close();
1333
 
1334
                }
1335
 
1336
                stmt.close();
1337
 
1338
            }
1339
            catch ( SQLException e )
1340
            {
1341
                handleSQLException(e, "");
1342
            }
1343
        }
1344
        return retVal;
1345
    }
7023 dpurdie 1346
 
6914 dpurdie 1347
    /** Mark a Daemon Instruction as in-progress
1348
     *  Assumes that a database connection has been established
1349
     *  Assumes that the connection is within a Mutex session and the database commit will be done elsewhere
1350
     *  
1351
     *  @param  instruction - PVID of the daemon instruction to process
1352
     */
1353
    public void markDaemonInstInProgress(final int instruction) throws SQLException, Exception
1354
    {
7044 dpurdie 1355
        mLogger.debug("markDaemonInstInProgress {}", instruction);
6914 dpurdie 1356
 
1357
        if ( mUseDatabase )
1358
        {
1359
            try
1360
            {
1361
                CallableStatement stmt = mConnection.prepareCall( "call PK_BUILDAPI.MARK_DAEMON_INST_IN_PROGRESS(?)" );
1362
                stmt.setInt( 1, instruction );
1363
                stmt.executeUpdate();
1364
                stmt.close();
1365
            }
1366
            catch ( SQLException e )
1367
            {
1368
                handleSQLException(e, "");
1369
            }
1370
        }
1371
    }
1372
 
1373
    /** Mark a Daemon Instruction as completed
1374
     *  Will establish (and release) a database connection if non is currently open
1375
     *  Will commit the change - unless commits have been disabled within a Mutex session
1376
     *  
1377
     *  @param  instruction - PVID of Daemon Instruction to process
1378
     */
1379
    public void markDaemonInstCompleted(final int instruction) throws SQLException, Exception
1380
    {
7044 dpurdie 1381
        mLogger.debug("markDaemonInstCompletedConnect {}", instruction);
6914 dpurdie 1382
        boolean connectionCreated = false;
1383
 
1384
        try
1385
        {
1386
            if (! mIsConnected)
1387
            {
1388
                connect();
1389
                connectionCreated = true;
1390
            }
1391
 
1392
            if ( mUseDatabase )
1393
            {
1394
                try
1395
                {
1396
                    CallableStatement stmt = mConnection.prepareCall( "call PK_BUILDAPI.MARK_DAEMON_INST_COMPLETED(?)" );
1397
                    stmt.setInt( 1, instruction );
1398
                    stmt.executeUpdate();
1399
                    stmt.close();
1400
                }
1401
                catch ( SQLException e )
1402
                {
1403
                    handleSQLException(e, "");
1404
                }
1405
            }
1406
 
1407
            if ( ! mDoNotCommit )
1408
            {
1409
                commit();
1410
            }
1411
        }
1412
        catch ( SQLException e )
1413
        {
1414
            handleSQLException(e, "");
1415
        }
1416
        finally
1417
        {
1418
            // this block is executed regardless of what happens in the try block
1419
            // even if an exception is thrown
1420
            // ensure disconnect
1421
            if (connectionCreated)
1422
            {
1423
                disconnect();
1424
            }
1425
        }
1426
    }
1427
 
1428
    /**
1429
     * Determine the RTAG_ID associated with a given SBOM
1430
     * Sets up mRtagId and mSbomId 
1431
     *  
1432
     * Overridden in ReleaseManagerUtf
1433
     * 
1434
     * @param sbom_id An sbom_id to process
1435
     * 
1436
     * @return The SBOM's RTAG_ID, or zero if none was found
1437
     * @exception SQLException
1438
     */
1439
    public int queryRtagIdForSbom(int sbom_id) throws SQLException
1440
    {
1441
        int rtagId = 0;
1442
 
1443
        CallableStatement stmt = mConnection.prepareCall("select b.rtag_id_fk from deployment_manager.boms b where b.bom_id=" + sbom_id);
1444
        ResultSet rset = stmt.executeQuery();
1445
        while( rset.next() )
1446
        {
1447
            rtagId = rset.getInt("rtag_id_fk");
1448
        }
1449
        rset.close();
1450
        stmt.close();
1451
 
1452
        return rtagId;
1453
     }
1454
 
1455
 
1456
    /**In daemon mode
1457
     * Returns a concatenation of the proj_name and rtag_name 
1458
     *  
1459
     * In escrow mode
1460
     * Returns a concatenation of the proj_name, branch_name, bom_version and bom_lifecycle 
1461
     *  
1462
     * Overridden in ReleaseManagerUtf 
1463
     */
1464
    public String queryBaselineName(int baseline) throws SQLException, Exception
1465
    {
7044 dpurdie 1466
        mLogger.debug("queryBaselineName {}", mDaemon);
6914 dpurdie 1467
        StringBuilder retVal = new StringBuilder();
1468
 
1469
        String sql = "";
1470
 
1471
        if ( mDaemon )
1472
        {
1473
            sql = "select p.proj_name, rt.rtag_name" +
1474
                  " from release_manager.projects p, release_manager.release_tags rt" +
1475
                  " where rt.rtag_id=" + baseline + " and p.proj_id=rt.proj_id";
1476
        }
1477
        else
1478
        {
1479
            sql = "select dp.proj_name, br.branch_name, b.bom_version, b.bom_lifecycle" +
1480
                  " from deployment_manager.dm_projects dp, deployment_manager.branches br, deployment_manager.boms b" +
1481
                  " where b.bom_id=" + baseline + " and br.branch_id=b.branch_id and dp.proj_id=br.proj_id";
1482
        }
1483
 
1484
        try
1485
        {
1486
            CallableStatement stmt = mConnection.prepareCall(sql);
1487
            ResultSet rset = stmt.executeQuery();
1488
 
1489
            while( rset.next() )
1490
            {
1491
                String proj_name = rset.getString("proj_name");
1492
 
1493
                if ( proj_name != null )
1494
                {
1495
                    retVal.append(proj_name);
1496
                }
1497
 
1498
                if ( mDaemon )
1499
                {
1500
                    String rtag_name = rset.getString("rtag_name");
1501
 
1502
                    if ( rtag_name != null )
1503
                    {
1504
                        retVal.append(" > ").append(rtag_name);
1505
                    }
1506
                }
1507
                else
1508
                {
1509
                    String branch_name = rset.getString("branch_name");
1510
 
1511
                    if ( branch_name != null )
1512
                    {
1513
                        retVal.append(" > ").append(branch_name);
1514
                    }
1515
 
1516
                    String bom_version = rset.getString("bom_version");
1517
 
1518
                    if ( bom_version != null )
1519
                    {
1520
                        retVal.append(" ").append(bom_version);
1521
                    }
1522
 
1523
                    String bom_lifecycle = rset.getString("bom_lifecycle");
1524
 
1525
                    if ( bom_lifecycle != null )
1526
                    {
1527
                        retVal.append(".").append(bom_lifecycle);
1528
                    }
1529
                }
1530
            }
1531
 
1532
            rset.close();
1533
            stmt.close();
1534
        }
1535
        catch ( SQLException e )
1536
        {
1537
            handleSQLException(e, "");
1538
        }
1539
 
7044 dpurdie 1540
        mLogger.info("queryBaselineName returned {}", retVal);
6914 dpurdie 1541
        return retVal.toString();
1542
    }
1543
 
1544
    /**only used in daemon mode
1545
     *   select config from release_manager.build_service_config where service='MAIL SERVER';
1546
     * returns the configured service 
1547
     *  
1548
     * Overridden in ReleaseManagerUtf 
1549
     *  
1550
     */
1551
    public String queryMailServer() throws SQLException, Exception
1552
    {
1553
        mLogger.debug("queryMailServer");
1554
        String retVal = "";
1555
 
1556
 
1557
        try
1558
        {
1559
            CallableStatement stmt = mConnection.prepareCall("select config from release_manager.build_service_config where service='MAIL SERVER'");
1560
            ResultSet rset = stmt.executeQuery();
1561
 
1562
            while( rset.next() )
1563
            {
1564
                String config = rset.getString("config");
1565
 
1566
                if ( config != null )
1567
                {
1568
                    retVal = config;
1569
                    break;
1570
                }
1571
            }
1572
 
1573
            rset.close();
1574
            stmt.close();
1575
        }
1576
        catch ( SQLException e )
1577
        {
1578
            handleSQLException(e, "");
1579
        }
1580
 
1581
 
7044 dpurdie 1582
        mLogger.info("queryMailServer returned {}", retVal);
6914 dpurdie 1583
        return retVal;
1584
    }
1585
 
1586
    /**only used in daemon mode
1587
     * returns the configured service 
1588
     *  
1589
     * Overridden in ReleaseManagerUtf 
1590
     *  
1591
     */
1592
    public String queryMailSender() throws SQLException, Exception
1593
    {
1594
        mLogger.debug("queryMailSender");
1595
        String retVal = "";
1596
 
1597
        try
1598
        {
1599
            CallableStatement stmt = mConnection.prepareCall("select config from release_manager.build_service_config where service='BUILD FAILURE MAIL SENDER'");
1600
            ResultSet rset = stmt.executeQuery();
1601
 
1602
            while( rset.next() )
1603
            {
1604
                String config = rset.getString("config");
1605
 
1606
                if ( config != null )
1607
                {
1608
                    retVal = config;
1609
                    break;
1610
                }
1611
            }
1612
 
1613
            rset.close();
1614
            stmt.close();
1615
        }
1616
        catch ( SQLException e )
1617
        {
1618
            handleSQLException(e, "");
1619
        }
1620
 
7044 dpurdie 1621
        mLogger.debug("queryMailSender returned {}", retVal);
6914 dpurdie 1622
        return retVal;
1623
    }
1624
 
1625
    /**only used in daemon mode
1626
     * returns the configured global email addresses 
1627
     *  
1628
     * Overridden in ReleaseManagerUtf 
1629
     */
1630
    public String queryGlobalAddresses() throws SQLException, Exception
1631
    {
1632
        mLogger.debug("queryGlobalAddresses");
1633
        String retVal = "";
1634
 
1635
        try
1636
        {
1637
            CallableStatement stmt = mConnection.prepareCall(
1638
                    "select u.user_email from release_manager.build_service_config bsc, release_manager.users u " +
1639
                            "where bsc.service='GLOBAL EMAIL ADDRESS LIST' and u.full_name=bsc.config"
1640
                    );
1641
            ResultSet rset = stmt.executeQuery();
1642
 
1643
            while( rset.next() )
1644
            {
1645
                String email = rset.getString("user_email");
1646
 
1647
                if ( email != null )
1648
                {
1649
                    retVal = email;
1650
                    break;
1651
                }
1652
            }
1653
 
1654
            rset.close();
1655
            stmt.close();
1656
        }
1657
        catch ( SQLException e )
1658
        {
1659
            handleSQLException(e, "");
1660
        }
1661
 
1662
 
7044 dpurdie 1663
        mLogger.debug("queryGlobalAddresses returned {}", retVal);
6914 dpurdie 1664
        return retVal;
1665
    }
1666
 
1667
    /**
1668
     * Determine a list of global, project wide and release email recipients
1669
     * All emails will be sent to people on this list
1670
     * @param baseline - an rtag_id
1671
     * @throws SQLException
1672
     * @returns String vector of email addresses
1673
     */
1674
    public List<String> queryProjectEmail(int baseline) throws SQLException
1675
    {
1676
        //
1677
        //  Get Global and Project Wide email information
1678
        //
1679
        mLogger.debug("queryGlobalandProjectEmail");
1680
        ArrayList<String> emailCollection = new ArrayList<String>();
1681
 
1682
        CallableStatement stmt0 = mConnection.prepareCall(
1683
                "select u.user_email " +
1684
                        "from release_manager.autobuild_failure af, " + 
1685
                        "release_manager.members_group mg, " + 
1686
                        "release_manager.users u, " + 
1687
                        "release_manager.views v, " + 
1688
                        "release_manager.release_tags rt " +
1689
                        "where rt.rtag_id=" + baseline + " " +
1690
                        "and v.view_name='PROJECT WIDE' " +
1691
                        "and af.proj_id=rt.proj_id " +
1692
                        "and af.view_id=v.view_id " +
1693
                        "and mg.group_email_id=af.group_email_id " +
1694
                        "and u.user_id=mg.user_id"
1695
                );
1696
        ResultSet rset0 = stmt0.executeQuery();
1697
        while( rset0.next() )
1698
        {
1699
            String email = rset0.getString("user_email");
1700
 
1701
            if ( email != null )
1702
            {
1703
                email = email.trim();
1704
                emailCollection.addAll( Arrays.asList(email.split("\\s*[,\\s]+\\s*")));
1705
            }
1706
        }
1707
 
1708
        rset0.close();
1709
        stmt0.close();
1710
 
1711
        //  Fetch the Release Specific email address
1712
        //  It may be comma separated
1713
        CallableStatement stmt1 = mConnection.prepareCall(
1714
                "select owner_email from release_manager.release_tags rt where rt.rtag_id = " + baseline
1715
                );
1716
        ResultSet rset1 = stmt1.executeQuery();
1717
 
1718
        while( rset1.next() )
1719
        {
1720
            String email = rset1.getString("owner_email");
1721
 
1722
            if ( email != null )
1723
            {
1724
                email = email.trim();
1725
                emailCollection.addAll( Arrays.asList(email.split("\\s*[,\\s]+\\s*")));
1726
            }
1727
        }
1728
 
1729
        rset1.close();
1730
        stmt1.close();
1731
 
1732
        return emailCollection;
1733
    }
1734
 
1735
    /**	Clears the database entry of the build file
1736
     *  <br>The assumption is that the file has been collected from the database and stored for use
1737
     *  
1738
     *  <p>sets CURRENT_BUILD_FILES to NULL for the rcon_id
1739
     */
1740
    public void clearBuildFile(int rcon_id) throws SQLException, Exception
1741
    {
1742
        mLogger.debug("clearBuildFile");
1743
 
1744
        try
1745
        {
1746
            connect();
1747
 
1748
            CallableStatement stmt = mConnection.prepareCall(
1749
                    "update release_manager.run_level" +
1750
                    " set current_build_files=NULL" +
1751
                    " where rcon_id=" + rcon_id
1752
                    );
1753
            stmt.executeUpdate();
1754
            stmt.close();
1755
            commit();
1756
        }
1757
        catch ( SQLException e )
1758
        {
1759
            handleSQLException(e, "");
1760
        }
1761
        finally
1762
        {
1763
            // this block is executed regardless of what happens in the try block
1764
            // even if an exception is thrown
1765
            // ensure disconnect
1766
            disconnect();
1767
        }
1768
    }
1769
 
1770
    /** Stores a buildfile in the database so that all daemons can collect it
1771
     * updates the CURRENT_BUILD_FILES for the rtag_id
1772
     *      This will trigger the slave build cycle
1773
     * Also sets the pkg_id and pvId of the package being built
1774
     *      This is only used for display purposes in Release manager
1775
     * 
1776
     * @param rtag_id   - Target Release
1777
     * @param buildFile - buildfile content to be saved
1778
     */
1779
    public void publishBuildFile(int rtag_id, BuildFile buildFile) throws SQLException, Exception
1780
    {
7044 dpurdie 1781
        mLogger.debug("publishBuildFile publishing a build file of length {}", buildFile.content.length());
1782
        mLogger.debug("publishBuildFile publishing pkgId: {} pvid: {}", buildFile.mPkgId, buildFile.mPvId);
6914 dpurdie 1783
 
1784
        try
1785
        {
1786
            connect();
1787
 
1788
            if ( isRtagIdConfigured( rtag_id ) )
1789
            {
1790
                PreparedStatement stmt = mConnection.prepareStatement(
1791
                        "update release_manager.run_level " +
1792
                                "    set current_build_files=?,  " +
1793
                                "        current_pkg_id_being_built=?,"+
1794
                                "        current_pv_id=?," +
1795
                                "       last_build=sysdate" +
1796
                                "    WHERE rcon_id in (  " +
1797
                                "        select rl.rcon_id from release_manager.release_config rc,  " +
1798
                                "               release_manager.run_level rl  " +
1799
                                "        where rc.rtag_id=? and rl.rcon_id=rc.rcon_id )" );
1800
                stmt.setString(1, buildFile.content);
1801
 
1802
                if (buildFile.mPkgId ==  0) {
1803
                    stmt.setNull(2, Types.NUMERIC);
1804
                } else {
1805
                    stmt.setInt  (2, buildFile.mPkgId);
1806
                }
1807
 
1808
                if (buildFile.mPvId ==  0) {
1809
                    stmt.setNull(3, Types.NUMERIC);
1810
                } else {
1811
                    stmt.setInt  (3, buildFile.mPvId);
1812
                }
1813
 
1814
                stmt.setInt(4, rtag_id);
1815
 
1816
                stmt.executeUpdate();
1817
                stmt.close();
1818
                commit();
1819
            }
1820
        }
1821
        catch ( SQLException e )
1822
        {
1823
            handleSQLException(e, "");
1824
        }
1825
        catch ( Exception e )
1826
        {
1827
            // this catch and rethrow is historical
1828
            // problems were found using CallableStatement when updating a CLOB column with data > 4000 bytes
7044 dpurdie 1829
            mLogger.error("publishBuildFile caught Exception {}", e.getMessage());
6914 dpurdie 1830
            throw new Exception("publishBuildFile caught Exception " + e.getMessage());
1831
        }
1832
        finally
1833
        {
1834
            // this block is executed regardless of what happens in the try block
1835
            // even if an exception is thrown
1836
            // ensure disconnect
1837
            disconnect();
1838
        }
1839
    }
1840
 
1841
    /**only used in daemon mode
1842
     * <br>Query the Release Sequence Number for the specified Release, while ensuing that the machine
1843
     * is still a part of the current build set
1844
     * 
1845
     * @param   rtagId      Release Identifier
1846
     * @return  <0  - No longer a part of the Build Set
1847
     * <br>     >=0 - Release Sequence Number
1848
     * 
1849
     */
1850
    public int queryReleaseSeqNum(int rtagId, int rcon_id, String machine_hostname) throws SQLException, Exception
1851
    {
1852
        mLogger.debug("queryReleaseSeqNum");
1853
        int retVal = 0;
1854
 
1855
        if ( mUseDatabase )
1856
        {
1857
            try
1858
            {
1859
                connect();
1860
                mLogger.info("queryReleaseSeqNum queryReleaseSeqNum");
1861
                CallableStatement stmt = mConnection.prepareCall(
1862
                        "SELECT NVL(rl.pause,0) AS pause, " + 
1863
                                "(select seqnum from release_modified where rtag_id = "+ rtagId +") as seqnum" +
1864
                                " FROM run_level rl," +
1865
                                "  release_config rc," +
1866
                                "  BUILD_MACHINE_CONFIG bmc" +
1867
                                " WHERE rl.rcon_id = " + rcon_id +
1868
                                " AND rl.RCON_ID   = rc.RCON_ID" +
1869
                                " AND rc.BMCON_ID IS NOT NULL" +
1870
                                " AND rc.BMCON_ID = bmc.BMCON_ID" +
1871
                                " AND rc.DAEMON_MODE = 'M'" +
1872
                                " AND UPPER(bmc.MACHINE_HOSTNAME) = UPPER('"+machine_hostname+"')"
1873
                                );
1874
 
1875
                ResultSet rset = stmt.executeQuery();
1876
                if( rset.next() )
1877
                {
1878
                    int pause = rset.getInt("pause");
1879
                    if (pause > 1)
1880
                        retVal = -2;
1881
                    else
1882
                        retVal = rset.getInt("seqnum");                        
1883
                }
1884
                else
1885
                {
1886
                    retVal = -1;
1887
                }
1888
 
1889
                rset.close();
1890
                stmt.close();
1891
            }
1892
            catch ( SQLException e )
1893
            {
1894
                handleSQLException(e, "");
1895
            }
1896
            finally
1897
            {
1898
                disconnect();
1899
            }
1900
        }
1901
 
7044 dpurdie 1902
        mLogger.warn("queryReleaseSeqNum returned {}", retVal);
6914 dpurdie 1903
        return retVal;
1904
    }
1905
 
1906
    /**
1907
     * Disable all the daemons for a specific release
1908
     *      Should only be done by the master
1909
     *      Used when master thread discovers that no build has occurred for a long time
1910
     *      
1911
     * @param rtag_id
1912
     * @throws SQLException
1913
     * @throws Exception
1914
     */
1915
    private void disableDaemons(final int rtag_id) throws SQLException, Exception
1916
    {
7044 dpurdie 1917
        mLogger.error("disableDaemons: {}", rtag_id );
6914 dpurdie 1918
 
1919
        try
1920
        {
1921
            CallableStatement stmt1 = mConnection.prepareCall( "call PK_BUILDAPI.set_daemon_states(?,2)" );
1922
            stmt1.setInt( 1, rtag_id );
1923
            stmt1.executeUpdate();
1924
            stmt1.close();
1925
            commit();
1926
        }
1927
        catch ( SQLException e )
1928
        {
1929
            handleSQLException(e, "");
1930
        }
1931
    }
1932
 
1933
    /**Checks the existence of any valid daemon configuration for the Release
1934
     * 
1935
     * A valid configuration record has a release_config entry that is linked to
1936
     * a machine_configuration entry. ie: unlinked entries are not part of a configured set.
1937
     * 
1938
     * @param   rtag_id - Release Tag to test
1939
     * @return  True if the Release still has a configuration entry
1940
     */
1941
    private boolean isRtagIdConfigured(final int rtag_id) throws SQLException, Exception
1942
    {
1943
        mLogger.debug("isRtagIdConfigured");
1944
        boolean retVal = false;
1945
 
1946
        try
1947
        {
1948
            // check if the rcon_id is still configured
1949
            CallableStatement stmt = mConnection.prepareCall(
1950
                    "select rtag_id"
1951
                    + " from release_manager.release_config"
1952
                    + " where rtag_id=" + rtag_id
1953
                    + " AND bmcon_id is not NULL");
1954
            ResultSet rset = stmt.executeQuery();
1955
 
1956
            while( rset.next() )
1957
            {
1958
                retVal = true;
1959
            }
1960
 
1961
            rset.close();
1962
            stmt.close();
1963
        }
1964
        catch ( SQLException e )
1965
        {
1966
            handleSQLException(e, "");
1967
        }
7044 dpurdie 1968
        mLogger.info("isRtagIdConfigured returning {}", retVal);
6914 dpurdie 1969
        return retVal;
1970
    }
1971
 
1972
    /**queries the RUN_LEVEL table for daemon to determine if the daemon has been paused
1973
     * or disabled.
1974
     * 
1975
     * Used as a part of allowedToProceed()
1976
     * 
1977
     * Handles the following conditions
1978
     *  Daemon has be de-configured - Returns TRUE
1979
     *  Daemon has been paused - Returns FALSE
1980
     *  Daemon has been disabled - Returns TRUE
1981
     *  Daemon has been set to run - Returns TRUE 
1982
     * 
1983
     * @param   rcon_id Connection identifier
1984
     * @returns False: Build agent has been commanded to PAUSE
1985
     *      <br>True: All other conditions
1986
     * 
1987
     * @exception SQLException
1988
     * @exception Exception
1989
     */
1990
    public boolean queryDirectedRunLevel(final int rcon_id) throws SQLException, Exception
1991
    {
7044 dpurdie 1992
        mLogger.debug("queryDirectedRunLevel {}", rcon_id);
6914 dpurdie 1993
        boolean retVal = true;
1994
 
1995
        if ( mUseDatabase )
1996
        {
1997
            try
1998
            {
1999
                CallableStatement stmt = mConnection.prepareCall(
2000
                        "select NVL(rl.pause,0) as pause" +
2001
                        " from release_manager.run_level rl, release_manager.release_config rc "+
2002
                        " where rl.rcon_id=" + rcon_id +
2003
                        " and rl.RCON_ID = rc.RCON_ID" +
2004
                        " and rc.BMCON_ID is not NULL"
2005
                        );
2006
                ResultSet rset = stmt.executeQuery();
2007
                int rsetSize = 0;
2008
 
2009
                while( rset.next() )
2010
                {
2011
                    rsetSize++;
2012
 
2013
                    //
2014
                    //  Pause: null -> 0 == Run
2015
                    //         1         == Pause
2016
                    //         2         == Disabled
2017
                    //
2018
                    int pause = rset.getInt("pause");
2019
                    if ( pause == 1) {
2020
                        retVal = false;
2021
                    }
2022
                }
2023
 
2024
                rset.close();
2025
                stmt.close();
2026
 
2027
                if ( rsetSize > 1 )
2028
                {
7033 dpurdie 2029
                    mLogger.error("queryDirectedRunLevel rsetSize > 1");
6914 dpurdie 2030
                    // show stopper
2031
                    throw new Exception("queryDirectedRunLevel rsetSize > 1");
2032
                }
2033
            }
2034
            catch ( SQLException e )
2035
            {
2036
                handleSQLException(e, "");
2037
            }
2038
        }
2039
 
7044 dpurdie 2040
        mLogger.info("queryDirectedRunLevel returning {}", retVal);
6914 dpurdie 2041
        return retVal;
2042
    }
2043
 
2044
    /**
2045
     * queries the RELEASE_CONFIG and BUILD_MACHINE_CONFIG tables using the rcon_id primary key, rtag_id, machine_hostname, daemon_mode
2046
     * <p>Return true if the query contains a result set containing one row
2047
     *  (indicating the rcon_id is still configured and its configuration is unchanged, aside from the gbe_buildfilter)
2048
     *  the gbe_buildfilter is queried prior to usage
2049
     * 
2050
     *  <p>Used to determine if this daemon should be allowed to continue running
2051
     *  or if it should be terminated
2052
     *  
2053
     * <br>Overridden in ReleaseManagerUtf 
2054
     * 
2055
     * @param rtag_id          The rtag_id of the machine to examine
2056
     * @param rcon_id          The rcond_id of the machine to examine
2057
     * @param machine_hostname The hostname of the machine to examine
2058
     * @param daemon_mode      The daemon mode of the machine to examine
2059
     * @param threadStartTime  The time that the thread started
2060
     * 
2061
     * @return True: The machine is a part of the current build set
2062
     *         False: The machine is not a part of the current build set
2063
     * @exception SQLException
2064
     * @exception Exception
2065
     */
2066
    public boolean queryReleaseConfig(final int rtag_id, final int rcon_id,final String machine_hostname, final char daemon_mode, final long threadStartTime) throws SQLException, Exception
2067
    {
2068
        mLogger.debug("queryReleaseConfig 1");
2069
        boolean retVal = false;
2070
 
2071
        try
2072
        {
2073
            String sql = 
2074
                    "select rl.pause," +
2075
                    "       rt.build_age," +
2076
                    "       TRUNC((SYSDATE-rl.last_build) ) as last_build_days" +
2077
                    "   from release_manager.release_config rc," +
2078
                    "        release_manager.release_tags rt," +
2079
                    "        release_manager.run_level rl," +
2080
                    "        release_manager.build_machine_config bc" +
2081
                    "   where rc.rtag_id=?" +
2082
                    "     and rc.rcon_id=?" +
2083
                    "     and rc.bmcon_id is not NULL" +
2084
                    "     and rc.bmcon_id = bc.bmcon_id" +
2085
                    "     and bc.machine_hostname=?" +
2086
                    "     and rc.daemon_mode=?" +
2087
                    "     and rl.rcon_id=rc.rcon_id" +
2088
                    "     and rt.rtag_id=rc.rtag_id" +
2089
                    "     and (rt.official = 'N' or rt.official='R' or rt.official='C')";
2090
 
2091
            CallableStatement stmt = mConnection.prepareCall( sql );
2092
            stmt.setFetchSize(10);
2093
            stmt.setInt(1, rtag_id);
2094
            stmt.setInt(2, rcon_id);
2095
            stmt.setString(3, machine_hostname);
2096
            stmt.setString(4, Character.toString(daemon_mode));
7023 dpurdie 2097
            RmResultSet rset = new RmResultSet(stmt.executeQuery(),"queryReleaseConfig 1");
6914 dpurdie 2098
            int rsetSize = 0;
2099
 
2100
            while( rset.next() )
2101
            {
2102
                rsetSize++;
2103
 
2104
                //
2105
                //  Pause: null -> 0 == Run
2106
                //         1         == Pause
2107
                //         2         == Disabled
2108
                //
7023 dpurdie 2109
                int pause = rset.getInt("pause",0);
2110
                int build_age = rset.getInt("build_age", 0);
2111
                int days_since_last_build = rset.getInt("last_build_days", 0);
6914 dpurdie 2112
 
2113
                //
2114
                //  Determine if we should disable the daemons on this release because they have not been
2115
                //  used in a long while.
2116
                //
2117
                //  Only Master makes the decision
2118
                //  If build_age is zero - then the daemon does not get aged out
2119
                //  If the days since last build > build_age, then we will shut down
2120
                //      Allow one days grace - will show up IN RM for a day
2121
                //  Allow the thread to run for x hours before shutting it down
2122
                //      Handles case where daemon has just been started/restarted
2123
                //
2124
                if ( daemon_mode == 'M' &&  build_age > 0 && days_since_last_build > (build_age + 1) )
2125
                {
2126
                    long upTimeSecs = (System.currentTimeMillis() - threadStartTime)/1000;
7044 dpurdie 2127
                    mLogger.error("queryReleaseConfig 1: {},{},{},{},{},{},{}",rtag_id, rcon_id, daemon_mode, pause, build_age, days_since_last_build, upTimeSecs );
6914 dpurdie 2128
                    if ( upTimeSecs > mPostponeShutdown)
2129
                    {
2130
                        disableDaemons(rtag_id);
2131
                        pause = 2;
2132
                    }
2133
                    else
2134
                    {
7033 dpurdie 2135
                        mLogger.error("queryReleaseConfig 1: Shutdown Postponed"  );    
6914 dpurdie 2136
                    }
2137
                }
2138
 
7044 dpurdie 2139
                mLogger.info("queryReleaseConfig 1: {},{},{},{},{},{}",rtag_id, rcon_id, daemon_mode, pause, build_age, days_since_last_build  );
6914 dpurdie 2140
 
2141
                if ( pause <= 1 )
2142
                {
2143
                    retVal = true;
2144
                }
2145
            }
2146
 
2147
            rset.close();
2148
            stmt.close();
2149
 
2150
            if ( rsetSize > 1 )
2151
            {
7033 dpurdie 2152
                mLogger.error("queryReleaseConfig 1 rsetSize > 1");
6914 dpurdie 2153
                // show stopper
2154
                throw new Exception("queryReleaseConfig 1 rsetSize > 1");
2155
            }
2156
        }
2157
        catch ( SQLException e )
2158
        {
2159
            handleSQLException(e, ":1");
2160
        }
2161
 
7044 dpurdie 2162
        mLogger.info("queryReleaseConfig 1 returning {}", retVal);
6914 dpurdie 2163
        return retVal;
2164
    }
2165
 
2166
    /**removes all elements from the mReleaseConfigCollection
2167
     * handles database connection and disconnection
2168
     * queries the RELEASE_CONFIG and BUILD_MACHINE_CONFIG tables using the rtag_id 
2169
     *  
2170
     * populates the mReleaseConfigCollection with the query result set
2171
     * partially implements the sequence diagrams coordinate slave threads generate build files 
2172
     *  
2173
     * Used by Master Thread to determine the build machines that will be a part 
2174
     * of the Slave-Sync process.
2175
     * 
2176
     *  Only called when a database connection has been established
2177
     */
2178
    public void queryReleaseConfig(final int rtag_id) throws SQLException, Exception
2179
    {
2180
        mLogger.debug("queryReleaseConfig 2");
2181
        mReleaseConfigCollection.resetData();
2182
 
2183
        try
2184
        {
2185
            //connect();
2186
 
2187
            CallableStatement stmt = mConnection.prepareCall(
2188
                    " select rc.rcon_id,"+
2189
                    "        rc.daemon_mode,"+
2190
                    "        rl.pause,"+
2191
                    "        bc.machine_hostname,"+
2192
                    "        rc.gbe_buildfilter,"+
2193
                    "        mt.gbe_value,"+
2194
                    "        bm.bm_name" +
2195
                    "    from release_manager.release_config rc, " +
2196
                    "         release_manager.run_level rl, " +
2197
                    "         release_manager.build_machine_config bc, " +
2198
                    "         release_manager.gbe_machtype mt," +
2199
                    "         release_manager.build_machines bm" +
2200
                    "    where   rc.rtag_id=?" +
2201
                    "        and rl.rcon_id=rc.rcon_id " +
2202
                    "        and rc.bmcon_id is not NULL" +
2203
                    "        and rc.bmcon_id = bc.bmcon_id" +
2204
                    "        and mt.gbe_id=bc.gbe_id" +
2205
                    "        and mt.bm_id=bm.bm_id"
2206
                    );
2207
            stmt.setFetchSize(10);
2208
            stmt.setInt(1, rtag_id);
7023 dpurdie 2209
            RmResultSet rset = new RmResultSet(stmt.executeQuery(),"queryReleaseConfig 2");
6914 dpurdie 2210
 
2211
            while( rset.next() )
2212
            {
7023 dpurdie 2213
                int rcon_id = rset.mustGetInt("rcon_id");
2214
 
6914 dpurdie 2215
                char dm = 'S';          
7023 dpurdie 2216
                String daemon_mode = rset.getString("daemon_mode","S");
2217
 
7044 dpurdie 2218
                mLogger.info("queryReleaseConfig 2 daemon_mode '{}'", daemon_mode);
6914 dpurdie 2219
 
7023 dpurdie 2220
                if ( daemon_mode.compareTo("M") == 0 )
6914 dpurdie 2221
                {
7023 dpurdie 2222
                    dm = 'M';
6914 dpurdie 2223
                }
2224
 
7023 dpurdie 2225
                String machine_hostname = rset.mustGetString("machine_hostname");
2226
                String gbe_buildfilter = rset.getString("gbe_buildfilter","");
2227
                String gbe_machtype = rset.mustGetString("gbe_value");
2228
                String gbe_machclass = rset.mustGetString("bm_name");
6914 dpurdie 2229
                //
2230
                //  Pause: null -> 0 == Run
2231
                //         1         == Pause
2232
                //         2         == Disabled
2233
                //
7023 dpurdie 2234
                int pause = rset.getInt("pause",0);
6914 dpurdie 2235
                mLogger.info("queryReleaseConfig 2: " + rtag_id + ", " + rcon_id + ", "+ dm + ", " + pause + 
2236
                        ", " + machine_hostname + ", " + gbe_buildfilter + ", " + gbe_machtype + ", + " + gbe_machclass );
2237
 
2238
                //
2239
                // Daemon Mode : Do not include build daemons that are disabled
2240
                // Escrow Mode : Include all machines
2241
                //
2242
                if ( pause <= 1 ||  ! mDaemon )
2243
                {
2244
                    ReleaseConfig releaseConfig = new ReleaseConfig( rtag_id, rcon_id, dm, machine_hostname, gbe_buildfilter, gbe_machtype, gbe_machclass);
2245
                    mReleaseConfigCollection.add(releaseConfig);
2246
                }
2247
            }
2248
 
2249
 
2250
            rset.close();
2251
            stmt.close();
2252
        }
2253
        catch ( SQLException e )
2254
        {
2255
            handleSQLException(e, ":2");
2256
        }
2257
        finally
2258
        {
2259
            // this block is executed regardless of what happens in the try block
2260
            // even if an exception is thrown
2261
            // ensure disconnect
2262
            //disconnect();
2263
        }
2264
    }
2265
 
2266
    /**removes all elements from the mReleaseConfigCollection
2267
     * handles database connection and disconnection
2268
     * queries the RELEASE_CONFIG and BUILD_MACHINE_CONFIG table using the machine_hostname
2269
     * populates the mReleaseConfigCollection with the query result set
2270
     * partially implements the sequence diagram spawn thread 
2271
     *  
2272
     * Used by the BuildDaemon thread to determine daemons to start and stop
2273
     */
2274
    public void queryReleaseConfig(final String hostname) throws SQLException, Exception
2275
    {
7044 dpurdie 2276
        mLogger.debug("queryReleaseConfig 3 {}", hostname);
6914 dpurdie 2277
        mReleaseConfigCollection.resetData();
2278
 
2279
        try
2280
        {
2281
            connect();
2282
            CallableStatement stmt = mConnection.prepareCall(
2283
                    "select rc.rtag_id,"+
2284
                    "       rc.rcon_id,"+
2285
                    "       rc.daemon_mode,"+
2286
                    "       rl.pause,"+
2287
                    "       bc.machine_hostname,"+
2288
                    "       rc.gbe_buildfilter,"+
2289
                    "       mt.gbe_value,"+
2290
                    "       bm.bm_name" +
2291
                    "  from release_manager.release_config rc," +
2292
                    "       release_manager.release_tags rt," +
2293
                    "       release_manager.run_level rl," +
2294
                    "       release_manager.build_machine_config bc," +
2295
                    "       release_manager.gbe_machtype mt," +
2296
                    "       release_manager.build_machines bm " +
2297
                    "    where bc.machine_hostname=?" +
2298
                    "      and rt.rtag_id=rc.rtag_id" +
2299
                    "      and rl.rcon_id=rc.rcon_id" +
2300
                    "      and rc.bmcon_id is not NULL" +
2301
                    "      and bc.bmcon_id=rc.bmcon_id" +
2302
                    "      and mt.gbe_id=bc.gbe_id" +
2303
                    "      and mt.bm_id=bm.bm_id" +
2304
                    "      and (rt.official = 'N' or rt.official='R' or rt.official='C')"
2305
                    );
2306
 
2307
            stmt.setFetchSize(20);
2308
            stmt.setString(1, hostname);
7023 dpurdie 2309
            RmResultSet rset = new RmResultSet(stmt.executeQuery(),"queryReleaseConfig 3");
6914 dpurdie 2310
 
2311
            while( rset.next() )
2312
            {
7023 dpurdie 2313
                int rtag_id = rset.mustGetKeyInt("rtag_id");
2314
                int rcon_id = rset.mustGetInt("rcon_id");
6914 dpurdie 2315
 
2316
                char dm = 'S';          
7023 dpurdie 2317
                String daemon_mode = rset.getString("daemon_mode", "S");
6914 dpurdie 2318
 
7044 dpurdie 2319
                mLogger.info("queryReleaseConfig 3 daemon_mode '{}'", daemon_mode);
6914 dpurdie 2320
 
7023 dpurdie 2321
                if ( daemon_mode.compareTo("M") == 0 )
6914 dpurdie 2322
                {
7023 dpurdie 2323
                    dm = 'M';
6914 dpurdie 2324
                }
2325
 
7023 dpurdie 2326
                String machine_hostname = rset.mustGetString("machine_hostname");
2327
                String gbe_buildfilter = rset.getString("gbe_buildfilter",null);
2328
                String gbe_machtype = rset.mustGetString("gbe_value");
2329
                String gbe_machclass = rset.mustGetString("bm_name");
2330
 
6914 dpurdie 2331
                //
2332
                //  Pause: null -> 0 == Run
2333
                //         1         == Pause
2334
                //         2         == Disabled
2335
                //
7023 dpurdie 2336
                int pause = rset.getInt("pause",0);
2337
 
6914 dpurdie 2338
                mLogger.info("queryReleaseConfig 3: " + rtag_id + ", " + rcon_id + ", "+ dm + ", " + pause + 
2339
                        ", " + machine_hostname + ", " + gbe_buildfilter + ", " + gbe_machtype + ", + " + gbe_machclass );
2340
 
2341
                //
2342
                // Do not include build daemons that are disabled
2343
                // Only those that are running or paused
2344
                //
2345
                if ( pause <= 1 )
2346
                {
2347
                    ReleaseConfig releaseConfig = new ReleaseConfig( rtag_id, rcon_id, dm, machine_hostname, gbe_buildfilter, gbe_machtype, gbe_machclass);
2348
                    mReleaseConfigCollection.add(releaseConfig);
2349
                }
2350
            }
2351
 
2352
            rset.close();
2353
            stmt.close();
2354
        }
2355
        catch ( SQLException e )
2356
        {
2357
            handleSQLException(e, ":3");
2358
        }
2359
        finally
2360
        {
2361
            // this block is executed regardless of what happens in the try block
2362
            // even if an exception is thrown
2363
            // ensure disconnect
2364
            disconnect();
2365
        }
2366
 
2367
    }
2368
 
2369
    /** Check that the build set has exactly one master
2370
     *  
2371
     *  Ignores entries that are disabled
2372
     *  Ignore entries that have been 'unlinked' - they have no active machine configuration
2373
     *
2374
     *   @param rtag_id - Release Tag
2375
     *  
2376
     *  Used by the Master and Slave Daemons 
2377
     *  Overridden in ReleaseManagerUtf 
2378
     */
2379
    public int queryMasterCount(final int rtag_id) throws SQLException, Exception
2380
    {
7044 dpurdie 2381
        mLogger.debug("queryMasterCount rtag_id {}", rtag_id);
6914 dpurdie 2382
 
2383
        int masterCount = 0;
2384
        try
2385
        {
2386
            connect();
2387
            CallableStatement stmt = mConnection.prepareCall(
2388
                            "SELECT COUNT (rc.daemon_mode) as masterCount" +
2389
                            " FROM release_manager.release_config rc, release_manager.run_level rl" +
2390
                            " WHERE rc.rtag_id =" + rtag_id +
2391
                            " AND rl.RCON_ID = rc.RCON_ID" +
2392
                            " AND   rc.bmcon_id is not NULL" +
2393
                            " AND NVL(rl.PAUSE,0) != 2" +
2394
                            " GROUP BY rc.daemon_mode" +
2395
                            " HAVING rc.daemon_mode = 'M'" );
2396
 
2397
            ResultSet rset = stmt.executeQuery();
2398
 
2399
            if ( rset.next() )
2400
            {
2401
                masterCount = rset.getInt("masterCount");
2402
            }
2403
 
2404
            rset.close();
2405
            stmt.close();
2406
        }
2407
        catch ( SQLException e )
2408
        {
2409
            handleSQLException(e, "");
2410
        }
2411
        finally
2412
        {
2413
            // this block is executed regardless of what happens in the try block
2414
            // even if an exception is thrown
2415
            // ensure disconnect
2416
            disconnect();
2417
        }
2418
 
2419
        return masterCount;
2420
    }
2421
 
2422
    /**
2423
     * Determine an array of machines for which there are active build requests
2424
     * Used to notify slave machines of a pending build request
2425
     * 
2426
     *  
2427
     * @param mHostname - Identifies the build machine
2428
     * @param rconIdList - Comma separated list of RCON_IDs that we are interested in
2429
     * @return An array of Integers, each being the rcon_id of a machine that has an active build
2430
     * @throws Exception 
2431
     */
2432
    public ArrayList<Integer> queryActivatedBuilds(String mHostname, String rconIdList) throws Exception {
2433
 
2434
        ArrayList<Integer> rv = new ArrayList<Integer>();
2435
        try
2436
        {
2437
            connect();
2438
            PreparedStatement stmt = mConnection.prepareStatement(
2439
                    "SELECT rl.rcon_id, rc.RTAG_ID" +
2440
                    " FROM run_level rl, release_config rc, release_tags rt" +
2441
                    " WHERE  rt.rtag_id = rc.RTAG_ID" +
2442
                    " AND UPPER(rc.daemon_hostname) = UPPER('"+mHostname+"')" +
2443
                    " AND rc.DAEMON_MODE != 'M'" +
2444
                    " AND rc.RCON_ID = rl.rcon_id" +
2445
                    " AND CURRENT_BUILD_FILES IS NOT NULL" +
2446
                    " AND rl.pause is null" +
2447
                    " AND rl.rcon_id in ("+rconIdList+")"
2448
                    );
2449
 
2450
            ResultSet rset = stmt.executeQuery();
2451
 
2452
            while (rset.next()) {
2453
                Integer ii = rset.getInt("rcon_id");
2454
                rv.add(ii);
2455
            }
2456
 
2457
            rset.close();
2458
            stmt.close();
2459
        }
2460
        catch ( SQLException e )
2461
        {
2462
            handleSQLException(e, "");
2463
        }
2464
        finally
2465
        {
2466
            // this block is executed regardless of what happens in the try block
2467
            // even if an exception is thrown
2468
            // ensure disconnect
2469
            disconnect();
2470
        }
2471
        return rv;
2472
    }
2473
 
7044 dpurdie 2474
    /**
2475
     * Extract all package-version information for planned, test and released packages
2476
     * In escrow mode there are no Planned or Test packages.
2477
     * 
2478
     * @param   rippleEngine        - Instance to use
2479
     * @param   packageCollection   - Package Collection structure to fill. Expected to be cleared
2480
     * @param   baseline            - RtagId of the release being processed, BomId of an Escrow being processed
6914 dpurdie 2481
     *  
2482
     * Overridden in ReleaseManagerUtf
2483
     *  
2484
     */
7023 dpurdie 2485
 
7044 dpurdie 2486
    protected void queryPackageVersions(RippleEngine rippleEngine, ArrayList<Package> packageCollection, int baseline) throws SQLException, Exception
6914 dpurdie 2487
    {
2488
        Phase phase = new Phase("rmData");
7044 dpurdie 2489
        mLogger.debug("queryPackageVersions {}", mDaemon);
6914 dpurdie 2490
 
2491
        try
2492
        {
2493
            if (mDaemon)
2494
            {
2495
                // Get planned package info
2496
                // Support multiple WIPS on the same package and build in the order they were released
2497
                // These are packages that are marked as pending build
2498
                //
2499
                mLogger.debug("queryPackageVersions: stmt1");
2500
                phase.setPhase("getPlannedPkgs1");
2501
                PreparedStatement stmt1 = mConnection.prepareStatement(
2502
                        "select pl.pv_id, p.pkg_id, p.pkg_name, pv.pkg_version, " +
2503
                                " pv.v_ext, pv.change_type, pv.ripple_field," +
2504
                                " pv.major_limit, pv.minor_limit, pv.patch_limit, pv.build_number_limit," +
2505
                                " pv.modified_stamp," +
7046 dpurdie 2506
                                " release_manager.PK_RMAPI.return_vcs_tag(pl.pv_id) AS vcsTag," +
2507
                                " pv.build_time" +
6914 dpurdie 2508
                                " from release_manager.planned pl," +
2509
                                "      release_manager.package_versions pv," +
2510
                                "      release_manager.packages p" +
2511
                                " where pl.rtag_id=" + baseline +
2512
                                "   and pv.build_type='A' and pv.dlocked='A'" +
2513
                                "   and pv.pv_id=pl.pv_id and p.pkg_id=pv.pkg_id" +
2514
                                " order by pv.modified_stamp"
2515
                        );
2516
                stmt1.setFetchSize(500);
7023 dpurdie 2517
                RmResultSet rset1 = new RmResultSet(stmt1.executeQuery(), "queryPackageVersions rset1");
6914 dpurdie 2518
 
2519
                while( rset1.next() )
2520
                {
7023 dpurdie 2521
                    int pv_id = rset1.mustGetKeyInt("pv_id");
2522
                    int pkg_id = rset1.mustGetInt("pkg_id");
2523
                    String pkg_name = rset1.mustGetString("pkg_name");
2524
                    String pkg_version = rset1.mustGetString("pkg_version");
7046 dpurdie 2525
                    int buildTime = rset1.getInt("build_time", 60);
6914 dpurdie 2526
 
2527
                    // Previous Version of this package, without the project suffix
2528
                    String pkg_prevVersion = getBaseVersionNumber(pv_id);
2529
                    if ( pkg_prevVersion == null)
2530
                    {
7044 dpurdie 2531
                        mLogger.error("queryPackageVersions. No Previous version {}", pv_id);
6914 dpurdie 2532
                        // show stopper
2533
                        throw new Exception("queryPackageVersions. No Previous version" + pv_id);
2534
                    }
2535
 
7023 dpurdie 2536
                    String v_ext = rset1.getString("v_ext","");
2537
                    String change_type = rset1.getString("change_type", "P");
6914 dpurdie 2538
 
2539
                    char ct = 'x';
2540
 
2541
                    if ( change_type.compareTo("M") == 0 )
2542
                    {
2543
                        ct = 'M';
2544
                    }
2545
                    else if ( change_type.compareTo("N") == 0 )
2546
                    {
2547
                        ct = 'N';
2548
                    }
2549
                    else if ( change_type.compareTo("P") == 0 )
2550
                    {
2551
                        ct = 'P';
2552
                    }
2553
                    else if ( change_type.compareTo("F") == 0 )
2554
                    {
2555
                        ct = 'F';
2556
                    }
2557
 
2558
                    if ( ct != 'x' )
2559
                    {
7023 dpurdie 2560
                        String ripple_field = rset1.getString("ripple_field", "b");
2561
                        int major_limit = rset1.getInt("major_limit", 0);
2562
                        int minor_limit = rset1.getInt("minor_limit",0);
2563
                        int patch_limit = rset1.getInt("patch_limit",0);
2564
                        int build_number_limit = rset1.getInt("build_number_limit",0);
2565
                        String vcs_tag = rset1.getString("vcsTag", "");
6914 dpurdie 2566
 
2567
                        Package p = new Package(pkg_id, pv_id, pkg_name, pkg_version, v_ext, pkg_name + v_ext, vcs_tag, ripple_field.charAt(0), ct);
2568
                        p.mDirectlyPlanned = true;
2569
                        p.mBuildReason = BuildReason.NewVersion;
2570
                        p.mMajorLimit = major_limit;
2571
                        p.mMinorLimit = minor_limit;
2572
                        p.mPatchLimit = patch_limit;
2573
                        p.mBuildLimit = build_number_limit;
2574
                        p.mPrevVersion = pkg_prevVersion;
7046 dpurdie 2575
                        p.mBuildTime = buildTime;
6914 dpurdie 2576
 
2577
                        Package prevPlannedPackage = findPackage(p.mAlias, packageCollection);
2578
 
2579
                        // If there are multiple packages with the same Alias, then only the first one
2580
                        // will be placed in the build set. The will be the oldest, the first one released
2581
                        // to be built
2582
                        if ( prevPlannedPackage == NULL_PACKAGE )
2583
                        {
7044 dpurdie 2584
                            mLogger.info("queryPackageVersions rset1 no previous planned package {}", pv_id);
6914 dpurdie 2585
                            packageCollection.add(p);
2586
                        }
2587
                    }
2588
                }
2589
 
2590
                rset1.close();
2591
                stmt1.close();
2592
 
2593
                // get planned package dependency info
2594
                mLogger.debug("queryPackageVersions: stmt2");
2595
                phase.setPhase("getPlannedPkgs2");
2596
                PreparedStatement stmt2 = mConnection.prepareStatement(
2597
                        "select pl.pv_id, p.pkg_name, dpv.v_ext, pv.modified_stamp " +
2598
                                "from release_manager.planned pl,"+
2599
                                "     release_manager.package_versions pv,"+
2600
                                "     release_manager.package_dependencies pd," +
2601
                                "     release_manager.package_versions dpv,"+
2602
                                "     release_manager.packages p " +
2603
                                " where pl.rtag_id=" + baseline + 
2604
                                "  and pv.build_type='A'"+
2605
                                "  and pv.dlocked='A' " +
2606
                                "  and pv.pv_id = pl.pv_id"+
2607
                                "  and pd.pv_id=pl.pv_id"+
2608
                                "  and dpv.pv_id=pd.dpv_id"+
2609
                                "  and p.pkg_id=dpv.pkg_id " +
2610
                                " order by pv.modified_stamp"
2611
                        );
2612
                stmt2.setFetchSize(500);
7023 dpurdie 2613
                RmResultSet rset2 = new RmResultSet(stmt2.executeQuery(), "queryPackageVersions rset2");
6914 dpurdie 2614
 
2615
                while( rset2.next() )
2616
                {
2617
                    boolean ignore = false;
2618
 
7023 dpurdie 2619
                    int pv_id = rset2.mustGetKeyInt("pv_id");
6914 dpurdie 2620
                    Package p = findPackage(pv_id, packageCollection);
2621
 
2622
                    if ( p == NULL_PACKAGE )
2623
                    {
7044 dpurdie 2624
                        mLogger.info("queryPackageVersions rset2 package superceded by planned {}", pv_id);
6914 dpurdie 2625
                        ignore = true;
2626
                    }
2627
 
7023 dpurdie 2628
                    String pkg_name = rset2.mustGetString("pkg_name");
2629
                    String v_ext = rset2.getString("v_ext","");
6914 dpurdie 2630
                    if ( !ignore )
2631
                    {
2632
                        p.mDependencyCollection.add(pkg_name + v_ext);
2633
                        p.mDependencyIDCollection.add(-1);
2634
                    }
2635
                }
2636
 
2637
                rset2.close();
2638
                stmt2.close();
2639
 
2640
                // get planned package build info
2641
                mLogger.debug("queryPackageVersions: stmt3");
2642
                phase.setPhase("getPlannedPkgs3");
2643
                PreparedStatement stmt3 = mConnection.prepareStatement(
2644
                        "select pl.pv_id, bm.bm_name, bsa.bsa_name, pv.modified_stamp " +
2645
                                "from release_manager.planned pl," +
2646
                                "     release_manager.package_versions pv," +
2647
                                "     release_manager.package_build_info pbi," +
2648
                                "     release_manager.build_machines bm," +
2649
                                "     release_manager.build_standards_addendum bsa " +
2650
                                "where pl.rtag_id=" + baseline + 
2651
                                "     and pv.build_type='A' and pv.dlocked='A' " +
2652
                                "     and pv.pv_id = pl.pv_id" +
2653
                                "     and pbi.pv_id=pv.pv_id" +
2654
                                "     and bm.bm_id=pbi.bm_id" +
2655
                                "     and bsa.bsa_id=pbi.bsa_id " +
2656
                                "order by pv.modified_stamp"
2657
                        );
2658
                stmt3.setFetchSize(500);
7023 dpurdie 2659
                RmResultSet rset3 = new RmResultSet(stmt3.executeQuery(), "queryPackageVersions rset3");
6914 dpurdie 2660
 
2661
                while( rset3.next() )
2662
                {
2663
                    boolean ignore = false;
7023 dpurdie 2664
                    int pv_id = rset3.mustGetKeyInt("pv_id");
6914 dpurdie 2665
 
2666
                    Package p = findPackage(pv_id, packageCollection);
2667
                    if ( p == NULL_PACKAGE )
2668
                    {
7044 dpurdie 2669
                        mLogger.info("queryPackageVersions rset3 package superceded by planned {}", pv_id);
6914 dpurdie 2670
                        ignore = true;
2671
                    }
2672
 
7023 dpurdie 2673
                    String bm_name = rset3.mustGetString("bm_name");
2674
                    String bsa_name = rset3.mustGetString("bsa_name");
6914 dpurdie 2675
 
2676
                    if ( !ignore )
2677
                    {
2678
                        BuildStandard bs = new BuildStandard(rippleEngine, bm_name, bsa_name);
2679
                        if ( bs.supportedBuildStandard() )
2680
                        {
2681
                            p.mBuildStandardCollection.add(bs);
2682
                        }
2683
                    }
2684
                }
2685
 
2686
                rset3.close();
2687
                stmt3.close();
2688
 
2689
                // get planned package unit test info
2690
                mLogger.debug("queryPackageVersions: stmt4");
2691
                phase.setPhase("getPlannedPkgs4");
2692
                PreparedStatement stmt4 = mConnection.prepareStatement(
2693
                        "select pl.pv_id, tt.test_type_name, pv.modified_stamp" +
2694
                        "  from release_manager.planned pl,"+
2695
                        "       release_manager.package_versions pv,"+
2696
                        "       release_manager.unit_tests ut," +
2697
                        "       release_manager.test_types tt" +
2698
                        " where pl.rtag_id=" + baseline + 
2699
                        "   and pv.build_type='A'" +
2700
                        "   and pv.dlocked='A'" +
2701
                        "  and pv.pv_id = pl.pv_id" +
2702
                        "  and ut.pv_id=pv.pv_id" +
2703
                        "  and tt.test_type_id=ut.test_types_fk" +
2704
                        " order by pv.modified_stamp"
2705
                        );
2706
                stmt4.setFetchSize(500);
7023 dpurdie 2707
                RmResultSet rset4 = new RmResultSet(stmt4.executeQuery(), "queryPackageVersions rset4");
6914 dpurdie 2708
 
2709
                while( rset4.next() )
2710
                {
2711
                    boolean ignore = false;
2712
 
7023 dpurdie 2713
                    int pv_id = rset4.mustGetKeyInt("pv_id");
6914 dpurdie 2714
                    Package p = findPackage(pv_id, packageCollection);
2715
                    if ( p == NULL_PACKAGE )
2716
                    {
7044 dpurdie 2717
                        mLogger.info("queryPackageVersions rset4 package superceded by planned {}", pv_id);
6914 dpurdie 2718
                        ignore = true;
2719
                    }
2720
 
7023 dpurdie 2721
                    String test_type_name = rset4.mustGetString("test_type_name");
6914 dpurdie 2722
                    if ( !ignore )
2723
                    {
2724
                        if ( test_type_name.compareTo("Autobuild UTF") == 0 )
2725
                        {
2726
                            p.mHasAutomatedUnitTests = true;
2727
                        }
2728
                    }
2729
                }
2730
 
2731
                rset4.close();
2732
                stmt4.close();
2733
 
2734
                // get planned package build failure info...
2735
                //      view based
2736
                mLogger.debug("queryPackageVersions: stmt5");
2737
                phase.setPhase("getPlannedPkgs6");
2738
                PreparedStatement stmt5 = mConnection.prepareStatement(
2739
                        "select pl.pv_id, u.user_email, pv.modified_stamp " +
2740
                                " from release_manager.planned pl, " +
2741
                                "      release_manager.release_tags rt, " +
2742
                                "      release_manager.package_versions pv, " +
2743
                                "      release_manager.autobuild_failure af, " +
2744
                                "      release_manager.members_group mg, " +
2745
                                "      release_manager.users u " +
2746
                                " where pl.rtag_id=" + baseline + 
2747
                                "      and rt.rtag_id=pl.rtag_id" +
2748
                                "      and pv.build_type='A' and pv.dlocked='A' " +
2749
                                "      and pv.pv_id = pl.pv_id " +
2750
                                "      and af.view_id=pl.view_id " +
2751
                                "      and mg.group_email_id=af.group_email_id " +
2752
                                "      and u.user_id=mg.user_id " +
2753
                                "      and af.proj_id=rt.proj_id " +
2754
                                " order by pv.modified_stamp"
2755
                        );
2756
                stmt5.setFetchSize(500);
7023 dpurdie 2757
                RmResultSet rset5 = new RmResultSet(stmt5.executeQuery(), "queryPackageVersions rset5");
6914 dpurdie 2758
 
2759
                while( rset5.next() )
2760
                {
7023 dpurdie 2761
                    int pv_id = rset5.mustGetKeyInt("pv_id");
6914 dpurdie 2762
                    Package p = findPackage(pv_id, packageCollection);
2763
                    if ( p == NULL_PACKAGE )
2764
                    {
7044 dpurdie 2765
                        mLogger.info("queryPackageVersions rset5 package superceded by planned {}", pv_id);
6914 dpurdie 2766
                    }
2767
                    else
2768
                    {
7023 dpurdie 2769
                        String user_email = rset5.getString("user_email", null);
6914 dpurdie 2770
                        if ( user_email != null )
2771
                        {
2772
                            p.addEmail(user_email);
2773
                        }
2774
                    }
2775
                }
2776
 
2777
                rset5.close();
2778
                stmt5.close();
2779
 
2780
                // get planned package build failure info...
2781
                // package version
7023 dpurdie 2782
                mLogger.debug("queryPackageVersions: stmt6");
6914 dpurdie 2783
                phase.setPhase("getPlannedPkgs7");
7023 dpurdie 2784
                PreparedStatement stmt6 = mConnection.prepareStatement(
6914 dpurdie 2785
                        "select pl.pv_id, u1.user_email, u2.user_email, u3.user_email, pv.modified_stamp" +
2786
                        " from release_manager.planned pl," +
2787
                        "      release_manager.release_tags rt," +
2788
                        "      release_manager.package_versions pv," +
2789
                        "      release_manager.users u1," +
2790
                        "      release_manager.users u2," +
2791
                        "      release_manager.users u3 " +
2792
                        " where pl.rtag_id=" + baseline + 
2793
                        "   and rt.rtag_id=pl.rtag_id" +
2794
                        "   and pv.build_type='A'" +
2795
                        "   and pv.dlocked='A' " +
2796
                        "   and pv.pv_id = pl.pv_id" +
2797
                        "   and u1.user_id=pv.creator_id" +
2798
                        "   and u2.user_id=pv.owner_id" +
2799
                        "   and u3.user_id=pv.modifier_id" +
2800
                        "   order by pv.modified_stamp"
2801
                        );
7023 dpurdie 2802
                stmt6.setFetchSize(500);
2803
                RmResultSet rset6 = new RmResultSet(stmt6.executeQuery(), "queryPackageVersions rset6");
6914 dpurdie 2804
 
7023 dpurdie 2805
                while( rset6.next() )
6914 dpurdie 2806
                {
7023 dpurdie 2807
                    int pv_id = rset6.mustGetKeyInt("pv_id");
6914 dpurdie 2808
                    Package p = findPackage(pv_id, packageCollection);
2809
                    if ( p == NULL_PACKAGE )
2810
                    {
7044 dpurdie 2811
                        mLogger.info("queryPackageVersions rset6 package superceded by planned {}", pv_id);
6914 dpurdie 2812
                    }
2813
                    else
2814
                    {
2815
 
2816
                        // walk the 3 columns of user_email in the resultset
2817
                        // columns 2, 3 and 4, all of the same name, respectively
2818
                        for(int column=2; column<5; column++ )
2819
                        {
7023 dpurdie 2820
                            String user_email = rset6.getString(column,null);
6914 dpurdie 2821
                            if ( user_email != null )
2822
                            {
2823
                                p.addEmail(user_email);
2824
                            }
2825
                        }
2826
                    }
2827
                }
2828
 
7023 dpurdie 2829
                rset6.close();
2830
                stmt6.close();
6914 dpurdie 2831
 
2832
                // get planned package advisory ripple info
2833
                mLogger.debug("queryPackageVersions: stmt7");
2834
                phase.setPhase("getPlannedPkgs8");
2835
                PreparedStatement stmt7 = mConnection.prepareStatement(
2836
                        "select pl.pv_id, pv.modified_stamp " +
2837
                        " from release_manager.planned pl," +
2838
                        "      release_manager.package_versions pv," +
2839
                        "      release_manager.advisory_ripple ar " +
2840
                        " where pl.rtag_id=" + baseline + 
2841
                        "   and pv.build_type='A'" +
2842
                        "    and pv.dlocked='A' " +
2843
                        "    and pv.pv_id = pl.pv_id" +
2844
                        "    and ar.rtag_id=pl.rtag_id" +
2845
                        "    and ar.pv_id=pl.pv_id " +
2846
                        " order by pv.modified_stamp"
2847
                        );
2848
                stmt7.setFetchSize(500);
7023 dpurdie 2849
                RmResultSet rset7 = new RmResultSet(stmt7.executeQuery(), "queryPackageVersions rset7");
6914 dpurdie 2850
 
2851
                while( rset7.next() )
2852
                {
2853
                    boolean ignore = false;
2854
 
7023 dpurdie 2855
                    int pv_id = rset7.mustGetInt("pv_id");
6914 dpurdie 2856
                    Package p = findPackage(pv_id, packageCollection);
2857
                    if ( p == NULL_PACKAGE )
2858
                    {
7044 dpurdie 2859
                        mLogger.info("queryPackageVersions rset7 package superceded by planned {}", pv_id);
6914 dpurdie 2860
                        ignore = true;
2861
                    }
2862
 
2863
                    if ( !ignore )
2864
                    {
2865
                        p.mAdvisoryRipple = true;
2866
                    }
2867
                }
2868
 
2869
                rset7.close();
2870
                stmt7.close();
7023 dpurdie 2871
 
2872
                //  Process Scheduled Build Approvals. OpCode is 2
2873
                //      These are treated as requests to not-include a 'Pending' package-version in the current build set
2874
                //      If there is an un-expired instruction, then remove the pending package-version from the build set
2875
                //
2876
                //  Process all expired requests
2877
                //      These are scheduled builds were the scheduled time has been exceeded
2878
                //      Simply discard the instruction and let the Approved build proceed
2879
                //
2880
                DaemonInstruction di = new DaemonInstruction(baseline, 2, true);
2881
 
2882
                phase.setPhase("removeOldSchedBuilds");
2883
                while ( getDaemonInst( di ) )
2884
                {
2885
                    markDaemonInstCompleted(di.instruction);
7044 dpurdie 2886
                    mLogger.info("queryPackageVersions remove Scheduled Build Approvals {}", di.pvId);
7023 dpurdie 2887
                }
2888
 
2889
                phase.setPhase("getSchedBuilds");
2890
                di = new DaemonInstruction(baseline, 2, false);
2891
                while ( getDaemonInst( di ) )
2892
                {
2893
                    Package p = findPackage(di.pvId, packageCollection);
6914 dpurdie 2894
 
7023 dpurdie 2895
                    if ( p != NULL_PACKAGE )
2896
                    {
7044 dpurdie 2897
                        mLogger.info("queryPackageVersions Scheduled Build Approvals {}", di.pvId);
7023 dpurdie 2898
                        packageCollection.remove(p);
2899
                    }
2900
                    else
2901
                    {
2902
                        // discard - the package no longer exists
2903
                        markDaemonInstCompleted( di.instruction );
7044 dpurdie 2904
                        mLogger.info("queryPackageVersions remove Scheduled Build Approvals for nonexistent package {}", di.pvId);
7023 dpurdie 2905
                    }
2906
                }                
2907
 
6914 dpurdie 2908
                // get released package info
2909
                //    Get package information on ALL released packages within the release of interest
2910
                //
2911
                mLogger.debug("queryPackageVersions: stmt8");
2912
                phase.setPhase("getAllPkgs1");
2913
                PreparedStatement stmt8 = mConnection.prepareStatement(
2914
                        "SELECT rc.pv_id," +
7046 dpurdie 2915
                                " p.pkg_id," +
2916
                                " p.pkg_name," +
2917
                                " pv.pkg_version," +
2918
                                " pv.v_ext," +
2919
                                " pv.ripple_field," +
2920
                                " pv.major_limit," +
2921
                                " pv.minor_limit," +
2922
                                " pv.patch_limit," +
2923
                                " pv.build_number_limit," +
2924
                                " pv.build_type," +
2925
                                " rc.sdktag_id," +
2926
                                " rc.ripple_stop," +
2927
                                " peg.pv_id as pegged," +
2928
                                " release_manager.PK_RMAPI.return_vcs_tag(rc.pv_id) AS vcsTag," +
2929
                                " pv.build_time" +
6914 dpurdie 2930
                                " FROM release_manager.release_content rc," +
2931
                                "  release_manager.package_versions pv," +
2932
                                "  release_manager.packages p," +
2933
                                "  release_manager.pegged_versions peg" +
2934
                                " WHERE rc.rtag_id= " + baseline +
2935
                                " AND pv.pv_id    = rc.pv_id" +
2936
                                " AND p.pkg_id    = pv.pkg_id" +
2937
                                " AND peg.rtag_id(+) = rc.rtag_id" +
2938
                                " AND peg.pv_id(+) = rc.pv_id" +
2939
                                " ORDER BY rc.pv_id"
2940
                        );
2941
                mLogger.debug("queryPackageVersions: stmt8 Prepared");
2942
                stmt8.setFetchSize(1000);
7023 dpurdie 2943
                RmResultSet rset8 = new RmResultSet(stmt8.executeQuery(), "queryPackageVersions rset8");
6914 dpurdie 2944
                mLogger.debug("queryPackageVersions: stmt8 Query Done");
2945
 
2946
                while( rset8.next() )
2947
                {
7023 dpurdie 2948
                    int pv_id = rset8.mustGetKeyInt("pv_id");
2949
                    int pkg_id = rset8.mustGetInt("pkg_id");
2950
                    String pkg_name = rset8.mustGetString("pkg_name");
2951
                    String pkg_version = rset8.mustGetString("pkg_version");
2952
                    String v_ext = rset8.getString("v_ext", "");
2953
                    String ripple_field = rset8.getString("ripple_field", "b");
2954
                    int major_limit = rset8.getInt("major_limit",0);
2955
                    int minor_limit = rset8.getInt("minor_limit", 0);
2956
                    int patch_limit = rset8.getInt("patch_limit",0);
2957
                    int build_number_limit = rset8.getInt("build_number_limit", 0);
2958
                    String vcs_tag = rset8.getString("vcsTag","");
2959
                    int isAnSdk = rset8.getInt("sdktag_id",0); 
2960
                    int isPegged = rset8.getInt("pegged",0);
2961
                    String buildType = rset8.getString("build_type", null);
6914 dpurdie 2962
                    boolean isBuildable = ( buildType != null ) && (buildType.equals("A") || buildType.equals("Y") );
7046 dpurdie 2963
                    int buildTime = rset8.getInt("build_time", 60);
6914 dpurdie 2964
 
7023 dpurdie 2965
                    String rippleStopData = rset8.getString("ripple_stop","n");
2966
                    char rippleStop = rippleStopData.charAt(0);
6914 dpurdie 2967
 
2968
                    Package p = new Package(pkg_id, pv_id, pkg_name, pkg_version, v_ext, pkg_name + v_ext, vcs_tag, ripple_field.charAt(0));
2969
                    p.mMajorLimit = major_limit;
2970
                    p.mMinorLimit = minor_limit;
2971
                    p.mPatchLimit = patch_limit;
2972
                    p.mBuildLimit = build_number_limit;
2973
                    p.mIsSdk = isAnSdk > 0;
2974
                    p.mIsPegged = isPegged > 0;
2975
                    p.mIsBuildable = isBuildable;
2976
                    p.mRippleStop = rippleStop;
7046 dpurdie 2977
                    p.mBuildTime = buildTime;
6914 dpurdie 2978
 
2979
                    Integer ipv_id = Integer.valueOf(pv_id);
2980
                    rippleEngine.mReleasedPvIDCollection.add(ipv_id);
2981
 
2982
                    //  If this package is to be replaced by a planned package then
2983
                    //  insert some of the current packages attributes
2984
                    //  Otherwise, add the the package to the package collection
2985
                    //
2986
                    Package plannedPackage = findPackage(p.mAlias, packageCollection);
2987
                    if ( plannedPackage == NULL_PACKAGE )
2988
                    {
7044 dpurdie 2989
                        mLogger.info("queryPackageVersions rset8 no planned package {}", pv_id);
6914 dpurdie 2990
                        packageCollection.add(p);
2991
                    }
2992
                    else
2993
                    {
2994
                        //  Copy these flags from the package that will be replaced
2995
                        //      Should not be able to replace a package provided by an SDK
2996
                        plannedPackage.mIsSdk = p.mIsSdk;
2997
 
2998
                        // A pegged package can be replaced with a directly built package
2999
                        // plannedPackage.mIsPegged = p.mIsPegged;
3000
                    }
3001
                }
3002
 
3003
                mLogger.debug("queryPackageVersions: stmt8 processing complete");
3004
                rset8.close();
3005
                stmt8.close();
3006
 
3007
                // get released package dependency info
3008
                // 
3009
                mLogger.debug("queryPackageVersions: stmt9");
3010
                phase.setPhase("getAllPkgs2");
3011
                PreparedStatement stmt9 = mConnection.prepareStatement(
7023 dpurdie 3012
                        "select rc.pv_id as pv_id, dpv.pv_id as dpv_id, p.pkg_name, dpv.v_ext" +
6914 dpurdie 3013
                                " from release_manager.release_content rc," +
3014
                                "      release_manager.package_versions pv," + 
3015
                                "      release_manager.package_dependencies pd,"+
3016
                                "      release_manager.package_versions dpv,"+
3017
                                "      release_manager.packages p" +
3018
                                " where rc.rtag_id=" + baseline +
3019
                                "  and pv.pv_id = rc.pv_id" +
3020
                                "  and pd.pv_id=pv.pv_id" +
3021
                                "  and dpv.pv_id=pd.dpv_id" +
3022
                                "  and p.pkg_id=dpv.pkg_id" +
3023
                                " order by rc.pv_id"
3024
                        );
3025
                mLogger.debug("queryPackageVersions: stmt9 prepared");
3026
                stmt9.setFetchSize(1000);
7023 dpurdie 3027
                RmResultSet rset9 = new RmResultSet(stmt9.executeQuery(), "queryPackageVersions rset9");
6914 dpurdie 3028
                mLogger.debug("queryPackageVersions: stmt9 query done");
3029
 
3030
                while( rset9.next() )
3031
                {
3032
                    boolean ignore = false;
3033
 
7023 dpurdie 3034
                    int pv_id = rset9.mustGetKeyInt("pv_id");
3035
                    int dpv_id = rset9.mustGetInt("dpv_id");
6914 dpurdie 3036
                    Package p = findPackage(pv_id, packageCollection);
3037
 
3038
                    if ( p == NULL_PACKAGE )
3039
                    {
7044 dpurdie 3040
                        mLogger.info("queryPackageVersions rset9 package superceded by planned {}", pv_id);
6914 dpurdie 3041
                        ignore = true;
3042
                    }
3043
 
7023 dpurdie 3044
                    String pkg_name = rset9.mustGetString("pkg_name");
3045
                    String v_ext = rset9.getString("v_ext","");
6914 dpurdie 3046
 
3047
                    if ( !ignore )
3048
                    {
3049
                        p.mDependencyCollection.add(pkg_name + v_ext);
3050
                        p.mDependencyIDCollection.add(dpv_id);
3051
                    }
3052
                }
3053
                mLogger.debug("queryPackageVersions: stmt9 processing complete");
3054
 
3055
                rset9.close();
3056
                stmt9.close();
3057
 
3058
                // get released package build info
3059
                mLogger.debug("queryPackageVersions: stmt10");
3060
                phase.setPhase("getAllPkgs3");
3061
                PreparedStatement stmt10 = mConnection.prepareStatement(
3062
                        "select rc.pv_id, bm.bm_name, bsa.bsa_name " +
3063
                                "from release_manager.release_content rc," +
3064
                                "     release_manager.package_versions pv," +
3065
                                "     release_manager.package_build_info pbi," +
3066
                                "     release_manager.build_machines bm," +
3067
                                "     release_manager.build_standards_addendum bsa " +
3068
                                "where rc.rtag_id=?" + 
3069
                                "     and pv.pv_id = rc.pv_id" +
3070
                                "     and pbi.pv_id=pv.pv_id" +
3071
                                "     and bm.bm_id=pbi.bm_id" +
3072
                                "     and bsa.bsa_id=pbi.bsa_id " +
3073
                                "order by rc.pv_id"
3074
                        );
3075
                stmt10.setFetchSize(1000);
3076
                stmt10.setInt(1, baseline);
7023 dpurdie 3077
                RmResultSet rset10 = new RmResultSet(stmt10.executeQuery(), "queryPackageVersions rset10");
6914 dpurdie 3078
 
3079
                while( rset10.next() )
3080
                {
3081
                    boolean ignore = false;
7023 dpurdie 3082
                    int pv_id = rset10.mustGetKeyInt("pv_id");
6914 dpurdie 3083
                    Package p = findPackage(pv_id, packageCollection);
3084
                    if ( p == NULL_PACKAGE )
3085
                    {
7044 dpurdie 3086
                        mLogger.info("queryPackageVersions rset10 package superceded by planned {}", pv_id);
6914 dpurdie 3087
                        ignore = true;
3088
                    }
3089
 
7023 dpurdie 3090
                    String bm_name = rset10.mustGetString("bm_name");
3091
                    String bsa_name = rset10.mustGetString("bsa_name");
6914 dpurdie 3092
 
3093
                    if ( !ignore )
3094
                    {
3095
                        BuildStandard bs = new BuildStandard(rippleEngine, bm_name, bsa_name);
3096
 
3097
                        if ( bs.supportedBuildStandard() )
3098
                        {
3099
                            p.mBuildStandardCollection.add(bs);
3100
                        }
3101
                    }
3102
                }
3103
 
3104
                rset10.close();
3105
                stmt10.close();
3106
 
3107
                // get released package unit test info
3108
                mLogger.debug("queryPackageVersions: stmt11");
3109
                phase.setPhase("getAllPkgs4");
3110
                PreparedStatement stmt11 = mConnection.prepareStatement(
3111
                        "select rc.pv_id, tt.test_type_name " +
3112
                                "from release_manager.release_content rc, release_manager.package_versions pv, release_manager.unit_tests ut, release_manager.test_types tt " +
3113
                                "where rc.rtag_id=" + baseline +
3114
                                " and pv.pv_id = rc.pv_id and ut.pv_id=pv.pv_id and tt.test_type_id=ut.test_types_fk " +
3115
                                "order by rc.pv_id"
3116
                        );
3117
                stmt11.setFetchSize(1000);
7023 dpurdie 3118
                RmResultSet rset11 = new RmResultSet(stmt11.executeQuery(), "queryPackageVersions rset11");
6914 dpurdie 3119
 
3120
                while( rset11.next() )
3121
                {
3122
                    boolean ignore = false;
3123
 
7023 dpurdie 3124
                    int pv_id = rset11.mustGetKeyInt("pv_id");
6914 dpurdie 3125
                    Package p = findPackage(pv_id, packageCollection);
3126
                    if ( p == NULL_PACKAGE )
3127
                    {
7044 dpurdie 3128
                        mLogger.info("queryPackageVersions rset11 package superceded by planned {}", pv_id);
6914 dpurdie 3129
                        ignore = true;
3130
                    }
3131
 
7023 dpurdie 3132
                    String test_type_name = rset11.mustGetString("test_type_name");
6914 dpurdie 3133
                    if ( !ignore )
3134
                    {
3135
                        if ( test_type_name.compareTo("Autobuild UTF") == 0 )
3136
                        {
3137
                            p.mHasAutomatedUnitTests = true;
3138
                        }
3139
                    }
3140
                }
3141
 
3142
                rset11.close();
3143
                stmt11.close();
3144
 
3145
                // get released package build failure info...
3146
                // view based
3147
                mLogger.debug("queryPackageVersions: stmt12");
3148
                phase.setPhase("getAllPkgs5");
3149
                PreparedStatement stmt12 = mConnection.prepareStatement(
3150
                        "SELECT rc.pv_id," +
3151
                        "  u.user_email" +
3152
                        " FROM release_manager.release_content rc," +
3153
                        "  release_manager.release_tags rt," +
3154
                        "  release_manager.package_versions pv," +
3155
                        "  release_manager.autobuild_failure af," +
3156
                        "  release_manager.members_group mg," +
3157
                        "  release_manager.users u" +
3158
                        " WHERE rc.rtag_id     = " + baseline +
3159
                        " AND rt.rtag_id       = rc.rtag_id" +
3160
                        " AND pv.pv_id         = rc.pv_id" +
3161
                        " AND af.view_id       = rc.base_view_id" +
3162
                        " AND mg.group_email_id= af.group_email_id" +
3163
                        " AND u.user_id        = mg.user_id" +
3164
                        " AND af.proj_id       = rt.proj_id" +
3165
                        " ORDER BY rc.pv_id"
3166
                        );
3167
                stmt12.setFetchSize(1000);
7023 dpurdie 3168
                RmResultSet rset12 = new RmResultSet(stmt12.executeQuery(), "queryPackageVersions rset12");
6914 dpurdie 3169
 
3170
                while( rset12.next() )
3171
                {
7023 dpurdie 3172
                    int pv_id = rset12.mustGetKeyInt("pv_id");
6914 dpurdie 3173
                    Package p = findPackage(pv_id, packageCollection);
3174
 
3175
                    if ( p == NULL_PACKAGE )
3176
                    {
7044 dpurdie 3177
                        mLogger.info("queryPackageVersions rset12 package superceded by planned {}", pv_id);
6914 dpurdie 3178
                    }
3179
                    else
3180
                    {
7023 dpurdie 3181
                        String user_email = rset12.getString("user_email",null);
6914 dpurdie 3182
 
3183
                        if ( user_email != null )
3184
                        {
3185
                            p.addEmail(user_email);
3186
                        }
3187
                    }
3188
                }
3189
 
3190
                rset12.close();
3191
                stmt12.close();
3192
 
3193
                // get released advisory ripple info
3194
                mLogger.debug("queryPackageVersions: stmt14");
3195
                phase.setPhase("getAllPkgs6");
3196
                PreparedStatement stmt14 = mConnection.prepareStatement(
3197
                        "select rc.pv_id " +
3198
                                "from release_manager.release_content rc, release_manager.package_versions pv, release_manager.advisory_ripple ar " +
3199
                                "where rc.rtag_id=" + baseline +
3200
                                " and pv.pv_id = rc.pv_id and ar.rtag_id=rc.rtag_id and ar.pv_id=rc.pv_id " +
3201
                                "order by rc.pv_id"
3202
                        );
3203
                stmt14.setFetchSize(1000);
7023 dpurdie 3204
                RmResultSet rset14 = new RmResultSet(stmt14.executeQuery(), "queryPackageVersions rset14");
6914 dpurdie 3205
 
3206
                while( rset14.next() )
3207
                {
3208
                    boolean ignore = false;
3209
 
7023 dpurdie 3210
                    int pv_id = rset14.mustGetInt("pv_id");
6914 dpurdie 3211
                    Package p = findPackage(pv_id, packageCollection);
3212
                    if ( p == NULL_PACKAGE )
3213
                    {
7044 dpurdie 3214
                        mLogger.info("queryPackageVersions rset14 package superceded by planned {}", pv_id);
6914 dpurdie 3215
                        ignore = true;
3216
                    }
3217
 
3218
                    if ( !ignore )
3219
                    {
3220
                        p.mAdvisoryRipple = true;
3221
                    }
3222
                }
3223
 
3224
                rset14.close();
3225
                stmt14.close();
3226
 
3227
                // Daemon Instruction: Force Package Ripple
3228
                //  An op code of 0 means force ripple
7023 dpurdie 3229
 
6914 dpurdie 3230
                phase.setPhase("getDaemonInstructions1");
7023 dpurdie 3231
                di = new DaemonInstruction( baseline, 0, true);
3232
                while ( getDaemonInst( di ) )
6914 dpurdie 3233
                {
7023 dpurdie 3234
                    Package p = findPackage(di.pvId, packageCollection);
6914 dpurdie 3235
 
3236
                    if ( p != NULL_PACKAGE )
3237
                    {
7044 dpurdie 3238
                        mLogger.info("queryPackageVersions forced ripple data {}", di.pvId);
7023 dpurdie 3239
                        p.mForcedRippleInstruction = di.instruction;
3240
                        p.mTestBuildEmail = di.userEmail;
6914 dpurdie 3241
                    }
3242
                    else
3243
                    {
3244
                        // discard
7023 dpurdie 3245
                        markDaemonInstCompleted( di.instruction );
6914 dpurdie 3246
                    }
3247
                }
3248
 
3249
                // Daemon Instruction: Test Build Package
3250
                //  An op code of 1 means test build
3251
                //
3252
                // bare minimal data collection - last thing needed is more data to collect
3253
                // the approach...
3254
                // query all test build instructions for this baseline
3255
                // for each, query its build information
3256
                // store them in "mTestBuild" Package attributes as follows:
3257
                // - mTestBuildInstruction (default 0)
3258
                // - mTestBuildEmail (default "null")
3259
                // - mTestBuildVcsTag (default "null")
3260
                // - mTestBuildStandardCollection
3261
                // - mTestBuildDependencyCollection
3262
                // additionally, trust nothing - these are mostly wips that at any time may have:
3263
                // - no build location
3264
                // - no build label
3265
                // - an empty build standard collection
3266
                // proceed with defaults above if necessary (the build will subsequently fail)
3267
                // in all cases, build a meaningful email body to inform the user
3268
                // of the snapshot of build information that applied to the build and store in:
3269
                // - mTestBuildEmailBody
3270
 
3271
                mLogger.debug("queryPackageVersions: stmt141");
3272
                phase.setPhase("getTestBuild1");
7023 dpurdie 3273
                di = new DaemonInstruction( baseline, 1, true);
3274
                while ( getDaemonInst( di ) )
6914 dpurdie 3275
                {
3276
                    // can only do one test build at a time - others will be discarded until future cycle
7044 dpurdie 3277
                    mLogger.info("queryPackageVersions test build data {}", di.pvId);
7023 dpurdie 3278
                    int testBuildPvId = di.pvId;
3279
                    int testBuildInstruction = di.instruction;
3280
                    String testBuildEmail = di.userEmail; 
6914 dpurdie 3281
 
3282
                    String pkg_name = "";
3283
                    String pkg_ext = "";
3284
                    String testBuildVcsTag = "null";
3285
                    int pkg_id = 0;
7046 dpurdie 3286
                    int buildTime = 60;
6914 dpurdie 3287
 
3288
                    // get package info for test build package
3289
                    mLogger.debug("queryPackageVersions: stmt15");
3290
                    PreparedStatement stmt15 = mConnection.prepareStatement(
7046 dpurdie 3291
                            "select p.pkg_name,"+
3292
                            " p.pkg_id,"+
3293
                            " pv.v_ext,"+
3294
                            " release_manager.PK_RMAPI.return_vcs_tag(pv.pv_id) AS vcsTag," +
3295
                            " pv.build_time" +
3296
                            " from release_manager.package_versions pv," +
3297
                            "      release_manager.packages p" +
3298
                            " where pv.pv_id=" + testBuildPvId +
3299
                            " and p.pkg_id=pv.pkg_id "
6914 dpurdie 3300
                            );
3301
                    stmt15.setFetchSize(10);
7023 dpurdie 3302
                    RmResultSet rset15 = new RmResultSet(stmt15.executeQuery(), "queryPackageVersions rset15");
6914 dpurdie 3303
 
3304
                    int rsetSize = 0;
3305
 
3306
                    while( rset15.next() )
3307
                    {
3308
                        rsetSize++;
7023 dpurdie 3309
                        pkg_name = rset15.mustGetKeyString("pkg_name");
3310
                        pkg_id = rset15.mustGetInt("pkg_id");
3311
                        pkg_ext = rset15.getString("v_ext","");
3312
                        testBuildVcsTag = rset15.getString("vcsTag", "null");
7046 dpurdie 3313
                        buildTime = rset15.getInt("build_time", 60);
3314
// TODO - Store the buildTime somewhere                        
6914 dpurdie 3315
                    }
3316
 
3317
                    rset15.close();
3318
                    stmt15.close();
3319
 
3320
                    mLogger.debug("queryPackageVersions: stmt16");
3321
                    if ( rsetSize == 0 )
3322
                    {
7044 dpurdie 3323
                        mLogger.error("queryPackageVersions rset15 no data found {}", di.instruction);
7023 dpurdie 3324
                        markDaemonInstCompleted( di.instruction );
6914 dpurdie 3325
                    }
3326
                    else
3327
                    {
3328
                        Package q = findPackage(pkg_name + pkg_ext, packageCollection);
3329
 
3330
                        if ( q == NULL_PACKAGE )
3331
                        {
3332
                            // Package alias does not exist in this release (planned or released packages)
3333
                            q = new Package(pkg_id, testBuildPvId, pkg_name, pkg_ext, pkg_name + pkg_ext, testBuildVcsTag, testBuildInstruction, testBuildEmail);
3334
                            packageCollection.add(q);
3335
                        }
3336
                        else
3337
                        {
3338
                            // Avoid interaction with real versions
3339
                        	// Flag as not pegged - so that we test build
3340
                            q.mVersion = "0.0.0000";
3341
                            q.mTestBuildVcsTag = testBuildVcsTag;
3342
                            q.mTestBuildInstruction = testBuildInstruction;
3343
                            q.mTestBuildEmail = testBuildEmail;
3344
                            q.mTestBuildPvId = testBuildPvId;
3345
                            q.mTestBuildHasAutomatedUnitTests = false;
3346
                            q.mIsPegged = false;
3347
                        }
3348
 
3349
                        // get test build package dependency info
3350
                        PreparedStatement stmt16 = mConnection.prepareStatement(
3351
                                "select p.pkg_name, dpv.v_ext " +
3352
                                        " from release_manager.package_versions pv," +
3353
                                        "   release_manager.package_dependencies pd,"+
3354
                                        "   release_manager.package_versions dpv,"+
3355
                                        "   release_manager.packages p" +
3356
                                        " where pv.pv_id=" + testBuildPvId +
3357
                                        "   and pd.pv_id=" + testBuildPvId +
3358
                                        "   and dpv.pv_id=pd.dpv_id"+
3359
                                        "   and p.pkg_id=dpv.pkg_id"
3360
                                );
3361
                        stmt16.setFetchSize(1000);
7023 dpurdie 3362
                        RmResultSet rset16 = new RmResultSet(stmt16.executeQuery(), "queryPackageVersions rset16");
6914 dpurdie 3363
 
3364
                        while( rset16.next() )
3365
                        {
7023 dpurdie 3366
                            String dpkg_name = rset16.mustGetString("pkg_name");
3367
                            String v_ext = rset16.getString("v_ext", "");
6914 dpurdie 3368
                            q.mTestBuildDependencyCollection.add(dpkg_name + v_ext);
3369
                        }
3370
 
3371
                        rset16.close();
3372
                        stmt16.close();
3373
 
3374
                        // get test build package build info
3375
                        mLogger.debug("queryPackageVersions: stmt17");
3376
                        PreparedStatement stmt17 = mConnection.prepareStatement(
3377
                                "select bm.bm_name, bsa.bsa_name " +
3378
                                        "from release_manager.package_versions pv," +
3379
                                        "     release_manager.package_build_info pbi," +
3380
                                        "     release_manager.build_machines bm," +
3381
                                        "     release_manager.build_standards_addendum bsa " +
3382
                                        "where pv.pv_id=" + testBuildPvId +
3383
                                        "   and pbi.pv_id=pv.pv_id" +
3384
                                        "   and bm.bm_id=pbi.bm_id" +
3385
                                        "   and bsa.bsa_id=pbi.bsa_id"
3386
                                );
7023 dpurdie 3387
                        RmResultSet rset17 = new RmResultSet(stmt17.executeQuery(), "queryPackageVersions rset17");
6914 dpurdie 3388
 
3389
                        while( rset17.next() )
3390
                        {
7023 dpurdie 3391
                            String bm_name = rset17.mustGetKeyString("bm_name");
3392
                            String bsa_name = rset17.mustGetString("bsa_name");
6914 dpurdie 3393
 
3394
                            BuildStandard bs = new BuildStandard(rippleEngine, bm_name, bsa_name);
3395
 
3396
                            if ( bs.supportedBuildStandard() )
3397
                            {
3398
                                q.mTestBuildStandardCollection.add(bs);
3399
                            }
3400
                        }
3401
 
3402
                        rset17.close();
3403
                        stmt17.close();
3404
 
3405
                        // get test build package unit test info
3406
                        mLogger.debug("queryPackageVersions: stmt4");
3407
                        PreparedStatement stmt18 = mConnection.prepareStatement(
3408
                                "select tt.test_type_name" +
3409
                                "  from release_manager.unit_tests ut," +
3410
                                "       release_manager.test_types tt" +
3411
                                " where ut.pv_id=" + testBuildPvId +
3412
                                "  and tt.test_type_id=ut.test_types_fk" 
3413
                                );
7023 dpurdie 3414
                        RmResultSet rset18 = new RmResultSet(stmt18.executeQuery(), "queryPackageVersions rset18");
6914 dpurdie 3415
 
3416
                        while( rset18.next() )
3417
                        {
3418
                            boolean ignore = false;
3419
 
7023 dpurdie 3420
                            String test_type_name = rset18.mustGetString("test_type_name");
6914 dpurdie 3421
 
3422
                            if ( !ignore )
3423
                            {
3424
                                if ( test_type_name.compareTo("Autobuild UTF") == 0 )
3425
                                {
3426
                                	q.mTestBuildHasAutomatedUnitTests = true;
3427
                                }
3428
                            }
3429
                        }
3430
 
3431
                        rset18.close();
3432
                        stmt18.close();
3433
 
3434
                    }
3435
                }
7023 dpurdie 3436
 
6914 dpurdie 3437
            }
3438
            else
3439
            {
3440
                // Escrow Mode
3441
                // get released product info
3442
                mLogger.debug("queryPackageVersions: stmt18");
3443
                PreparedStatement stmt = mConnection.prepareStatement(
3444
                        "select oc.prod_id, p.pkg_name, pv.pkg_version, pv.v_ext," +
3445
                                "release_manager.PK_RMAPI.return_vcs_tag(pv.pv_id) AS vcsTag" +
3446
                                " from deployment_manager.bom_contents bc," +
3447
                                "deployment_manager.operating_systems os," +
3448
                                "deployment_manager.os_contents oc," +
3449
                                "release_manager.package_versions pv," +
3450
                                "release_manager.packages p" +
3451
                                " where bc.bom_id=" + baseline +
3452
                                " and os.node_id=bc.node_id" +
3453
                                " and oc.os_id=os.os_id" +
3454
                                " and pv.pv_id=oc.prod_id" +
3455
                                " and p.pkg_id=pv.pkg_id" +
3456
                                " order by oc.prod_id"
3457
                        );
3458
                stmt.setFetchSize(1000);
7023 dpurdie 3459
                RmResultSet rset = new RmResultSet(stmt.executeQuery(), "queryPackageVersions rset");
6914 dpurdie 3460
 
3461
                while( rset.next() )
3462
                {
7023 dpurdie 3463
                    int pv_id = rset.mustGetKeyInt("prod_id");
3464
                    String pkg_name = rset.mustGetString("pkg_name");
3465
                    String pkg_version = rset.mustGetString("pkg_version");
3466
                    String v_ext = rset.getString("v_ext", "");
3467
                    String vcs_tag = rset.getString("vcsTag","");
3468
 
6914 dpurdie 3469
                    Package p = findPackage(pv_id, packageCollection);
3470
                    if ( p == NULL_PACKAGE )
3471
                    {
3472
                        Package q = new Package(0, pv_id, pkg_name, pkg_version, v_ext, pkg_name + "." + pkg_version, vcs_tag, 'x');
3473
                        packageCollection.add(q);
3474
                    }
3475
                }
3476
 
3477
                rset.close();
3478
                stmt.close();
7044 dpurdie 3479
 
3480
                //  Now have all the package-versions used in the SBOM need to 
3481
                //      determine all the dependent packages 
3482
                //      determine the build info for all the packages
3483
 
3484
                //  Determine all dependent packages
3485
                //  Note: use a ListIterator as it allows traverseDependencies to modify the packageCollection
3486
                for (ListIterator<Package> it = packageCollection.listIterator(); it.hasNext(); )
3487
                {
3488
                    Package p = it.next();
3489
                    traverseDependencies(packageCollection, p, false, it);
3490
                }
3491
 
3492
                //  Add required build information
3493
                for (Iterator<Package> it = packageCollection.iterator(); it.hasNext(); )
3494
                {
3495
                    Package p = it.next();
3496
                    queryBuildInfo(rippleEngine, p);
3497
                }
6914 dpurdie 3498
            }
3499
        }
3500
        catch ( SQLException e )
3501
        {
3502
            handleSQLException(e, "");
3503
        }
3504
        phase.setPhase("End");
3505
    }
3506
 
3507
 
3508
   /**	Determine the version number of the base package
3509
    *   Used in a ripple build to determine the base for calculating the next version number
3510
    *   Assumes that a database connection has been established
3511
    *   Used in Daemon Mode Only
3512
    *   
3513
    *   History: Used to determine the last non-rippled package, but this had issues [JATS-402]
3514
    *   This version will located the last release version with a check that it is not a WIP ( ie start with a '(' )
3515
    *   
3516
    *   @param pv_id	- The PVID of the package to process
3517
    *   @return         - The version number without a project suffix. Null on error. 0.0.0000 if no previous
3518
    *   @throws SQLException 
3519
    */
3520
    public String getBaseVersionNumber(int pv_id)
3521
    {
3522
    	String baseVersion = "0.0.0000";
3523
 
3524
    	try {
3525
    		CallableStatement stmt;
3526
			stmt = mConnection.prepareCall(
3527
					"SELECT pv_id,last_pv_id,pkg_version,v_ext,build_type, DLOCKED " +
3528
					" FROM " +
3529
					"  (SELECT build_type,last_pv_id,pv_id,pkg_version,v_ext, DLOCKED " +
3530
					"  FROM " +
3531
					"    (SELECT pv.build_type,pv.last_pv_id AS raw_last_pvid ,pv_id,pv.pkg_version,pv.v_ext,DECODE(pv.pv_id, pv.last_pv_id, NULL, pv.last_pv_id) AS last_pv_id, DLOCKED " +
3532
					"    FROM release_manager.package_versions pv " +
3533
					"    WHERE pv.PKG_ID IN " +
3534
					"      (SELECT pkg_id " +
3535
					"      FROM release_manager.package_versions pv " +
3536
					"      WHERE pv.pv_id = " + pv_id +
3537
					"      ) " +
3538
					"    ) " +
3539
					"    START WITH pv_id = " + pv_id +
3540
					"    CONNECT BY nocycle prior last_pv_id = pv_id " +
3541
					"  ) " +
3542
					" WHERE DLOCKED = 'Y' and pkg_version not like '(%' AND pv_id != " + pv_id
3543
	                   //" WHERE build_type != 'Y' and pv_id != " + pv_id
3544
			        );
3545
 
3546
			ResultSet rset = stmt.executeQuery();
3547
 
3548
			while( rset.next() )
3549
			{
3550
				// Previous Version of this package, without the project suffix
3551
				String pkg_prevVersion = rset.getString("pkg_version");
3552
				if (pkg_prevVersion != null) 
3553
				{
3554
					String pkg_prevVext = rset.getString("v_ext");
3555
					if (pkg_prevVext != null) 
3556
					{
3557
						int endindex = pkg_prevVersion.length() - pkg_prevVext.length();
3558
						if ( endindex > 0 )
3559
						{
3560
							baseVersion = pkg_prevVersion.substring(0, endindex);
3561
						}
3562
					}
3563
				}
3564
 
3565
				// Only want the first entry
3566
				break;
3567
			}
3568
 
3569
			rset.close();
3570
			stmt.close();
3571
 
3572
    	} catch (SQLException e) {
3573
    		baseVersion = null;
7044 dpurdie 3574
    		mLogger.error("Exception for getBaseVersionNumber {}", pv_id);
6914 dpurdie 3575
		}
3576
        return baseVersion;
3577
    }
3578
 
7044 dpurdie 3579
    /** Called only in escrow mode
3580
     * 
3581
     * Used to process each package in the escrow list and to locate all dependent packages - recursively.
3582
     * 
6914 dpurdie 3583
     * if checkCollection is true, checks the pv_id is in the packageCollection
3584
     * if checkCollection is false, or the pv_id is not in the collection
7044 dpurdie 3585
     *    Traverses the pv_id package dependencies
3586
     *    for each dpv.pv_id in the resultset
6914 dpurdie 3587
     *     call traverseDependencies( packageCollection, dpv.pv_id, true )
3588
     *     if the pv_id is not in the collection, add it
7044 dpurdie 3589
     * 
3590
     *   @param packageCollection   - Collection of packages being processed
3591
     *   @param pkg                 - Current package in the collection
3592
     *   @param checkCollection     - How to handle pkg not in the collection. False: Process all package dependencies. True: Skip if package is in collection
3593
     *   @param listIterator        - List iterator being used to iterate over packageCollection. Used to insert new packages
6914 dpurdie 3594
     *   
7044 dpurdie 3595
     *   This function is called recursively
6914 dpurdie 3596
     */
7044 dpurdie 3597
    private void traverseDependencies(ArrayList<Package> packageCollection, Package pkg, boolean checkCollection, ListIterator<Package> listIterator) throws SQLException, Exception
3598
    {
3599
        mLogger.debug("traverseDependencies {}", checkCollection);
6914 dpurdie 3600
        boolean pvIdInCollection = false;
3601
 
3602
        if ( checkCollection )
3603
        {
3604
            for (Iterator<Package> it = packageCollection.iterator(); it.hasNext(); )
3605
            {
3606
                Package p = it.next();
3607
 
3608
                if ( p.mId == pkg.mId )
3609
                {
3610
                    pvIdInCollection = true;
3611
                    break;
3612
                }
3613
            }
3614
        }
3615
 
3616
        if ( !pvIdInCollection )
3617
        {
3618
            ArrayList<Package> resultset = new ArrayList<Package>();
3619
 
3620
            try
3621
            {
3622
                PreparedStatement stmt = mConnection.prepareStatement(
3623
                        "select dpv.pv_id," +
3624
                                "p.pkg_name," + 
3625
                                "dpv.pkg_version," +
3626
                                "dpv.v_ext," + 
3627
                                "release_manager.PK_RMAPI.return_vcs_tag(dpv.pv_id) AS vcsTag" + 
3628
                                " from release_manager.package_versions pv," + 
3629
                                "release_manager.package_dependencies pd," + 
3630
                                "release_manager.package_versions dpv," + 
3631
                                "release_manager.packages p" + 
3632
                                " where pv.pv_id=" + pkg.mId + 
3633
                                "   and pd.pv_id=pv.pv_id" +
3634
                                "   and dpv.pv_id=pd.dpv_id" + 
3635
                                "   and p.pkg_id=dpv.pkg_id" + 
3636
                        " order by pv.pv_id");
3637
                stmt.setFetchSize(1000);
7023 dpurdie 3638
                RmResultSet rset = new RmResultSet(stmt.executeQuery(),"traverseDependencies");
6914 dpurdie 3639
 
3640
                while (rset.next())
3641
                {
7023 dpurdie 3642
                    int pv_id = rset.mustGetKeyInt("pv_id");
3643
                    String pkg_name = rset.mustGetString("pkg_name");
3644
                    String pkg_version = rset.mustGetString("pkg_version");
3645
                    String v_ext = rset.getString("v_ext","");
3646
                    String vcs_tag = rset.getString("vcsTag","");
6914 dpurdie 3647
 
3648
                    Package p = new Package(0, pv_id, pkg_name, pkg_version, v_ext, pkg_name + "." + pkg_version, vcs_tag, 'x');
3649
                    resultset.add(p);
3650
                    pkg.mDependencyCollection.add(p.mAlias);
3651
                }
3652
 
3653
                rset.close();
3654
                stmt.close();
3655
            }
3656
            catch ( SQLException e )
3657
            {
3658
                handleSQLException(e, "");
3659
            }
3660
 
7044 dpurdie 3661
            //  Process each dependent package
6914 dpurdie 3662
            for (Iterator<Package> it = resultset.iterator(); it.hasNext();)
3663
            {
3664
                Package r = it.next();
3665
                traverseDependencies(packageCollection, r, true, listIterator);
3666
 
3667
                pvIdInCollection = false;
3668
 
3669
                for (Iterator<Package> it2 = packageCollection.iterator(); it2.hasNext();)
3670
                {
3671
                    Package p = it2.next();
3672
 
3673
                    if (p.mId == r.mId)
3674
                    {
3675
                        pvIdInCollection = true;
3676
                        break;
3677
                    }
3678
                }
3679
 
3680
                if (!pvIdInCollection)
3681
                {
3682
                    // insert the Package immediately before the next Package returned by next
3683
                    // this does not change the next Package (if any) to be returned by next
3684
                    listIterator.add(r);
3685
                }
3686
 
3687
            }
3688
        }
7044 dpurdie 3689
    }
6914 dpurdie 3690
 
7044 dpurdie 3691
    /** Called only in escrow mode
3692
     * 
3693
     *  <p>Add build information to a Package
3694
     *  <br>Adds: bm_name and bsa_name, but only if they are supported
3695
     * 
3696
     * @param   rippleEngine    - Ripple Engine being used
3697
     * @param   p               - Package to process
3698
     * 
6914 dpurdie 3699
     */
3700
    private void queryBuildInfo(RippleEngine rippleEngine, Package p) throws SQLException, Exception
3701
    {
3702
        mLogger.debug("queryBuildInfo");
3703
 
3704
        try
3705
        {
3706
            CallableStatement stmt = mConnection.prepareCall("select bm.bm_name, bsa.bsa_name "
3707
                    + "from release_manager.package_versions pv," 
3708
                    + "     release_manager.package_build_info pbi,"
3709
                    + "     release_manager.build_machines bm," 
3710
                    + "     release_manager.build_standards_addendum bsa "
3711
                    + "where pv.pv_id=" + p.mId 
3712
                    + "   and pbi.pv_id=pv.pv_id" 
3713
                    + "   and bm.bm_id=pbi.bm_id"
3714
                    + "   and bsa.bsa_id=pbi.bsa_id " 
3715
                    + "order by pv.pv_id");
7023 dpurdie 3716
            RmResultSet rset = new RmResultSet (stmt.executeQuery(),"queryBuildInfo");
6914 dpurdie 3717
 
3718
            while (rset.next())
3719
            {
7023 dpurdie 3720
                String bm_name = rset.mustGetKeyString("bm_name");
3721
                String bsa_name = rset.mustGetString("bsa_name");
6914 dpurdie 3722
 
3723
                BuildStandard bs = new BuildStandard(rippleEngine, bm_name, bsa_name);
3724
 
3725
                if (bs.supportedBuildStandard())
3726
                {
3727
                    p.mBuildStandardCollection.add(bs);
3728
                }
3729
            }
3730
 
3731
            rset.close();
3732
            stmt.close();
3733
        }
3734
        catch ( SQLException e )
3735
        {
3736
            handleSQLException(e, "");
3737
        }
3738
 
3739
    }
3740
 
3741
    /**
3742
     * Find Package by pvid
3743
     * @param   id                  - pvid of package to locate
3744
     * @param   packageCollection   - Collection to scan
3745
     * @return  Package with the matching mID or NULL_PACKAGE if no package has the mID
3746
     */
3747
    protected Package findPackage(int id, ArrayList<Package> packageCollection)
3748
    {
7044 dpurdie 3749
        mLogger.debug("findPackage 1 id {}", id);
6914 dpurdie 3750
        Package retVal = NULL_PACKAGE;
3751
 
3752
        for (Iterator<Package> it = packageCollection.iterator(); it.hasNext(); )
3753
        {
3754
            Package p = it.next();
3755
 
3756
            if ( p.mId == id )
3757
            {
3758
                retVal = p;
3759
                break;
3760
            }
3761
        }
3762
 
7044 dpurdie 3763
        mLogger.debug("findPackage 1 returned {}", retVal.mName);
6914 dpurdie 3764
        return retVal;
3765
    }
3766
 
3767
    /**
3768
     * Find Package by package alias
3769
     * @param   alias               - alias of package to locate
3770
     * @param   packageCollection   - Collection to scan
3771
     * @return  Package with the matching mAlias or NULL_PACKAGE if no package has the mAlias
3772
     */
3773
    protected Package findPackage(String alias, ArrayList<Package> packageCollection)
3774
    {
7044 dpurdie 3775
        mLogger.debug("findPackage 2 alias {}", alias);
6914 dpurdie 3776
        Package retVal = NULL_PACKAGE;
3777
 
3778
        for (Iterator<Package> it = packageCollection.iterator(); it.hasNext(); )
3779
        {
3780
            Package p = it.next();
3781
 
3782
            if ( p.mAlias.compareTo( alias ) == 0 )
3783
            {
3784
                retVal = p;
3785
                break;
3786
            }
3787
        }
3788
 
7044 dpurdie 3789
        mLogger.info("findPackage 2 returned {}", retVal.mName);
6914 dpurdie 3790
        return retVal;
3791
    }
3792
 
3793
    /**only used in daemon mode to determine version existence in the database
3794
     *  1 select pkg_id from release_manager.package_versions where pkg_id=<pkg_id> and pkg_version=<pkg_version>;
3795
     *  2 select pkg_id from release_manager.planned_versions where pkg_id=<pkg_id> and pkg_version=<pkg_version>;
3796
     * returns true if either resultset contains one record to indicate it already exists
3797
     */
3798
    boolean queryPackageVersions(int pkg_id, String pkg_version) throws SQLException, Exception
3799
    {
3800
        mLogger.debug("queryPackageVersions");
3801
        boolean retVal = false;
3802
 
3803
        if ( mUseDatabase )
3804
        {
3805
            try
3806
            {
3807
                mLogger.info("queryPackageVersions release_manager.package_versions");
3808
                CallableStatement stmt1 = mConnection.prepareCall(
3809
                    "select pkg_id" +
3810
                    " from release_manager.package_versions" +
3811
                    " where pkg_id=" + pkg_id + 
3812
                    " and pkg_version='" + pkg_version + "'");
3813
 
3814
                ResultSet rset1 = stmt1.executeQuery();
3815
                int rsetSize = 0;
3816
 
3817
                while( rset1.next() )
3818
                {
3819
                    rsetSize++;
3820
                }
3821
 
3822
                rset1.close();
3823
                stmt1.close();
3824
 
3825
                if ( rsetSize > 1 )
3826
                {
7044 dpurdie 3827
                    String msg = "queryPackageVersions rsetSize > 1 " + pkg_id + " " + pkg_version;
3828
                    mLogger.error(msg);
6914 dpurdie 3829
                    // show stopper
7044 dpurdie 3830
                    throw new Exception(msg);
6914 dpurdie 3831
                }
3832
 
3833
                if ( rsetSize == 1 )
3834
                {
3835
                    retVal = true;
3836
                }
3837
                else
3838
                {
3839
                    mLogger.info("queryPackageVersions release_manager.planned_versions");
3840
                    CallableStatement stmt2 = mConnection.prepareCall(
3841
                        "select pkg_id" +
3842
                        " from release_manager.planned_versions" +
3843
                        " where pkg_id=" + pkg_id + 
3844
                        " and pkg_version='" + pkg_version + "'");
3845
                    ResultSet rset2 = stmt2.executeQuery();
3846
                    rsetSize = 0;
3847
 
3848
                    while( rset2.next() )
3849
                    {
3850
                        rsetSize++;
3851
                    }
3852
 
3853
                    rset2.close();
3854
                    stmt2.close();
3855
 
3856
                    if ( rsetSize > 1 )
3857
                    {
7044 dpurdie 3858
                        String msg = "queryPackageVersions rsetSize > 1 " + pkg_id + " " + pkg_version;
3859
                        mLogger.error(msg);
6914 dpurdie 3860
                        // show stopper
7044 dpurdie 3861
                        throw new Exception(msg);
6914 dpurdie 3862
                    }
3863
 
3864
                    if ( rsetSize == 1 )
3865
                    {
3866
                        retVal = true;
3867
                    }
3868
                }
3869
            }
3870
            catch ( SQLException e )
3871
            {
3872
                handleSQLException(e, "");
3873
            }
3874
        }
3875
 
7044 dpurdie 3876
        mLogger.info("queryPackageVersions returned {}", retVal);
6914 dpurdie 3877
        return retVal;
3878
    }
3879
 
3880
    /**
3881
     * Determine the set of packages that have been excluded from the build 
3882
     * These are packages that have been marked as do_not_ripple 
3883
     *  
3884
     * Overridden in ReleaseManagerUtf
3885
     * 
3886
     * @param buildExclusionCollection
3887
     * @param baseline The rtag_id of the release to examine
3888
     * 
3889
     * @exception SQLException
3890
     * @exception Exception
3891
     */
3892
    public void queryBuildExclusions(List<BuildExclusion> buildExclusionCollection, int baseline) throws SQLException, Exception
3893
    {
7044 dpurdie 3894
        mLogger.debug("queryBuildExclusions {}", baseline);
6914 dpurdie 3895
 
3896
        try
3897
        {
3898
            PreparedStatement stmt = mConnection.prepareStatement(
3899
                    "select pv_id, root_pv_id, root_cause" +
3900
                    " from release_manager.do_not_ripple" + 
3901
                    " where rtag_id=" + baseline);
3902
            stmt.setFetchSize(1000);
7023 dpurdie 3903
            RmResultSet rset = new RmResultSet(stmt.executeQuery(),"queryBuildExclusions");
6914 dpurdie 3904
 
3905
            while( rset.next() )
3906
            {
7023 dpurdie 3907
                int pvId = rset.mustGetKeyInt("pv_id");
3908
                int rootPvId = rset.getInt("root_pv_id", -1);
3909
                String rootCause = rset.getString("root_cause", null);
6914 dpurdie 3910
 
3911
                // force email notification by using a zero test build instruction
3912
                BuildExclusion buildExclusion = new BuildExclusion(pvId, rootPvId, rootCause, 0);
7032 dpurdie 3913
                buildExclusion.setImported();
6914 dpurdie 3914
                buildExclusionCollection.add(buildExclusion);
3915
            }
3916
 
3917
            rset.close();
3918
            stmt.close();
3919
        }
3920
        catch ( SQLException e )
3921
        {
3922
            handleSQLException(e, "");
3923
        }
3924
    }
3925
 
3926
    /**
7032 dpurdie 3927
     * Execute the Exclude_Indirect_From_Build stored procedure
6914 dpurdie 3928
     *  Note: Execute_Indirect_From_Build will delete matching do_not_ripple
3929
     *        rows prior to an insertion - this is crucial!!
7032 dpurdie 3930
     *        
3931
     * Note: The name 'Execute_Indirect_From_Build' is misleading as it appears to be used for both direct and indirect
3932
     *       exclusions.
3933
     * 
6914 dpurdie 3934
     * @param hasConnection        IN True. Use existing connection and will not commit the operation
3935
     * @param packageVersionId     IN passed to Exclude_Indirect_From_Build
3936
     * @param packageVersion       IN passed to Exclude_Indirect_From_Build
3937
     * @param rtagId               IN passed to Exclude_Indirect_From_Build
3938
     * @param rootPvId             IN passed to Exclude_Indirect_From_Build
3939
     * @param rootCause            IN passed to Exclude_Indirect_From_Build
3940
     * @param rootFile             IN passed to Exclude_Indirect_From_Build
3941
     * @param supercede            IN checks for a row with a matching packageVersionId and rtagId when false
3942
     *                                such a row will prevent the execution of Exclude_Indirect_From_Build
3943
     * @param testBuildInstruction IN will prevent the execution of Exclude_Indirect_From_Build true
3944
     * 
3945
     * @exception SQLException
3946
     * @exception Exception
3947
     */
3948
    public void excludeFromBuild(
3949
            boolean hasConnection, 
3950
            int packageVersionId, 
3951
            String packageVersion, 
3952
            int rtagId,
3953
            String rootPvId, 
3954
            String rootCause,
3955
            String rootFile,
3956
            boolean supercede, Boolean testBuildInstruction) throws SQLException, Exception
3957
    {
7044 dpurdie 3958
        mLogger.debug("excludeFromBuild {}", packageVersionId);
6914 dpurdie 3959
 
3960
        //  If a Test Build, then don't excluded package
3961
        if ( testBuildInstruction )
3962
        {
3963
            return;
3964
        }
3965
 
3966
        if ( mUseDatabase )
3967
        {
3968
            try
3969
            {
3970
                if (!hasConnection)
3971
                    connect();
3972
 
3973
                boolean exist = false;
7032 dpurdie 3974
                boolean dbIsIndirect = false;
3975
                boolean entryIsIndirect = (rootPvId != null);
6914 dpurdie 3976
 
3977
                if ( !supercede )
3978
                {
3979
                    // do not exclude a package already excluded ie let the first build failure count
3980
                    // there is a window of opportunity here, but it is worth doing
3981
                    // scenario 1
3982
                    //      1 this query indicates no build failure exists on this version
3983
                    //      2 another build machine reports a build failure on this version
3984
                    //      3 this is then overridden by this thread
3985
                    // does not matter
3986
                    // doing this works well for the following
3987
                    // scenario 2
3988
                    //      1 this query (run by a slave) indicates no build failure exists on this version
3989
                    //      2 build failure is reported
3990
                    //      3 master build machine detects slave in state waiting
3991
                    //      4 master daemon discovers slave did not deliver artifacts
3992
                    //      5 master daemon is prevented from overriding the build failure
7032 dpurdie 3993
                    //
3994
                    // Another complication
3995
                    //  If this is a direct exclusion (rootPvid) is null and the entry in the table is an indirect exclusion
3996
                    //  then we should delete the indirect exclusion and replace it with a direct exclusion
3997
                    //
3998
                    CallableStatement stmt = mConnection.prepareCall("select pv_id, root_pv_id from release_manager.do_not_ripple where pv_id=" + packageVersionId + "and rtag_id=" + rtagId);
6914 dpurdie 3999
                    ResultSet rset = stmt.executeQuery();
4000
 
4001
                    while( rset.next() )
4002
                    {
4003
                        exist = true;
7032 dpurdie 4004
                        rset.getInt("root_pv_id");
4005
                        dbIsIndirect = ! rset.wasNull();
4006
 
4007
                        // Override an indirect database entry with a direct exclusion
4008
                        if ( dbIsIndirect && ! entryIsIndirect ) {
4009
                            exist = false;
4010
                        }
4011
 
6914 dpurdie 4012
                        break;
4013
                    }
4014
 
4015
                    rset.close();
4016
                    stmt.close();
4017
                }
4018
 
4019
                if ( !exist )
4020
                {
4021
                    CallableStatement stmt = mConnection.prepareCall( "begin ? := PK_RMAPI.EXCLUDE_INDIRECT_FROM_BUILD(?,?,?,?,?,?,?); end;" );
4022
                    stmt.registerOutParameter( 1, Types.INTEGER);
4023
                    stmt.setInt    ( 2, packageVersionId );
4024
                    stmt.setString ( 3, packageVersion );
4025
                    stmt.setInt    ( 4, rtagId );
4026
                    stmt.setString ( 5, "buildadm" );
4027
                    stmt.setString ( 6, rootPvId);
4028
                    stmt.setString ( 7, rootCause);
4029
                    stmt.setString ( 8, rootFile);
4030
                    stmt.executeUpdate();
4031
                    int result = stmt.getInt( 1 );
4032
 
4033
                    if ( result != 0 )
4034
                    {
4035
                        // flag build failure
7044 dpurdie 4036
                        mLogger.error( "excludeFromBuild show stopper PK_RMAPI.EXCLUDE_INDIRECT_FROM_BUILD failed, returned {}",  result );
6914 dpurdie 4037
                        throw new Exception("excludeFromBuild show stopper PK_RMAPI.EXCLUDE_INDIRECT_FROM_BUILD failed, returned " + result);
4038
                    }
4039
                    stmt.close();
4040
                    if (!hasConnection )
4041
                        commit();
4042
                }
4043
            }
4044
            catch ( SQLException e )
4045
            {
4046
                handleSQLException(e, "");
4047
            }
4048
            finally
4049
            {
4050
                // this block is executed regardless of what happens in the try block
4051
                // even if an exception is thrown
4052
                // ensure disconnect
4053
                if (!hasConnection )
4054
                    disconnect();
4055
            }
4056
        }
4057
    }
4058
 
4059
    /**
4060
     * Removes an excluded package from the do_not_ripple table, thereby including the package back into the build set.
4061
     * The method does not remove packages that were manually added to the DNR table, as those
4062
     * packages are not in the domain of the buildtool.
4063
     * 
4064
     * @param packageVersionId Packages PVID
4065
     * @param rtagId           The RTAG id of the release to consider
4066
     * 
4067
     * @exception SQLException
4068
     * @exception Exception
4069
     */
4070
 
4071
    public void includeToBuild(int packageVersionId, int rtagId) throws SQLException, Exception
4072
    {
7044 dpurdie 4073
        mLogger.debug("includeToBuild {}", packageVersionId);
6914 dpurdie 4074
        if ( mUseDatabase )
4075
        {
4076
            try
4077
            {
4078
                CallableStatement stmt = mConnection.prepareCall(
4079
                    "delete from release_manager.do_not_ripple " +
4080
                    "  where rtag_id=" + rtagId + 
4081
                    "  and pv_id=" + packageVersionId +
4082
                    "  and (root_pv_id is not NULL or root_cause is not NULL or root_file is not NULL)"
4083
                    );
4084
                stmt.executeUpdate();
4085
                stmt.close();
4086
            }
4087
            catch ( SQLException e )
4088
            {
4089
                handleSQLException(e, "");
4090
            }
4091
        }
4092
    }
4093
 
4094
 
4095
 
4096
 
4097
    /**queries the RUN_LEVEL table using the rcon_id primary key
4098
     * handles database connection and disconnection
4099
     * returns the current_build_files
4100
     * implements the sequence diagram consume build files 
4101
     *  
4102
     * Used by the Slave Daemon
4103
     * 
4104
     *  @param rcon_id - My release config id
4105
     *  @return The buildFile content. Will be null if none is found
4106
     */
4107
    public String queryBuildFile(int rcon_id) throws SQLException, Exception
4108
    {
4109
        String buildFile = null;
4110
 
7044 dpurdie 4111
        mLogger.debug("queryRunLevel 1 rcon_id {}", rcon_id);
6914 dpurdie 4112
        if ( !mUseDatabase )
4113
        {
4114
            mLogger.info("queryRunLevel 1 !mUseDatabase");
4115
            buildFile = "unit test build file content";
4116
        }
4117
        else
4118
        {
4119
            try
4120
            {
4121
                connect();
4122
                CallableStatement stmt = mConnection.prepareCall("select current_build_files from release_manager.run_level where rcon_id=" + rcon_id);
4123
                ResultSet rset = stmt.executeQuery();
4124
                int rsetSize = 0;
4125
 
4126
                while( rset.next() )
4127
                {
4128
                    rsetSize++;
4129
                    buildFile = rset.getString("current_build_files");
4130
                }
4131
 
4132
                if ( rsetSize > 1 )
4133
                {
7033 dpurdie 4134
                    mLogger.error("queryRunLevel 1 rsetSize > 1");
6914 dpurdie 4135
                    // show stopper
4136
                    throw new Exception("queryRunLevel 1 rsetSize > 1");
4137
                }
4138
 
4139
                rset.close();
4140
                stmt.close();
4141
            }
4142
            catch ( SQLException e )
4143
            {
4144
                handleSQLException(e, "");
4145
            }
4146
            finally
4147
            {
4148
                // this block is executed regardless of what happens in the try block
4149
                // even if an exception is thrown
4150
                // ensure disconnect
4151
                disconnect();
4152
            }
4153
        }
4154
 
4155
        return buildFile;
4156
    }
4157
 
4158
    /** Updates mRunLevelCollection
4159
     *  Removes all elements from mRunLevelCollection, then queries the Database
4160
     *  and re-populates the structure.
4161
     *  
4162
     *  Used to determine which slave daemons the master needs to wait upon.
4163
     *  
4164
     *  Ignores entries that are disabled
4165
     *  Ignore entries that have been 'unlinked' - they have no active machine configuration
4166
     *
4167
     *   @param	rtag_id	- Release Tag
4168
     *  
4169
     *	Used by the Master Daemon 
4170
     * 	Overridden in ReleaseManagerUtf 
4171
     */
4172
    public void queryRunLevel(final int rtag_id) throws SQLException, Exception
4173
    {
7044 dpurdie 4174
        mLogger.debug("queryRunLevel 2 rtag_id {}", rtag_id);
6914 dpurdie 4175
 
4176
        mRunLevelCollection.clear();
4177
 
4178
        try
4179
        {
4180
            connect();
4181
            CallableStatement stmt = mConnection.prepareCall(
4182
                    "SELECT rl.rcon_id," +
4183
                    "  rl.current_run_level," +
4184
                    "  NVL(rl.pause,0) as pause," +
4185
                    "  rc.daemon_mode," +
4186
                    "  NVL2(rl.CURRENT_BUILD_FILES, 1 , 0) as bfPresent" +
4187
                    " FROM release_manager.release_config rc," +
4188
                    "     release_manager.run_level rl" +
4189
                    " WHERE rc.rtag_id = " + rtag_id +
4190
                    " AND   rl.rcon_id = rc.rcon_id" +
4191
                    " AND   rc.bmcon_id is not NULL");
4192
            stmt.setFetchSize(20);
7023 dpurdie 4193
            RmResultSet rset = new RmResultSet(stmt.executeQuery(),"queryRunLevel 2");
6914 dpurdie 4194
            int rcon_id = 0;
4195
            int current_run_level = 0;
4196
            String modeString;
4197
            char mode;
4198
            int pause = 0;
4199
            int buildFilePresent = 0;
4200
 
4201
            while( rset.next() )
4202
            {
7023 dpurdie 4203
                rcon_id = rset.mustGetKeyInt("rcon_id");
4204
                modeString = rset.mustGetString("daemon_mode");
6914 dpurdie 4205
                mode = modeString.charAt(0);
7023 dpurdie 4206
                buildFilePresent = rset.getInt("bfPresent",0);
4207
                current_run_level = rset.getInt("current_run_level", BuildState.DB_IDLE.toValue());
6914 dpurdie 4208
 
4209
                //
4210
                //  Pause: null -> 0 == Run
4211
                //         1         == Pause
4212
                //         2         == Disabled
4213
                //
7023 dpurdie 4214
                pause = rset.getInt("pause", 0);
6914 dpurdie 4215
 
4216
                //  Ignore disabled entries
4217
                if ( pause <= 1 )
4218
                {
4219
                    RunLevelData runLevel = new RunLevelData(rcon_id, current_run_level, mode, buildFilePresent);
4220
                    mRunLevelCollection.add(runLevel);
4221
                }
4222
            }
4223
 
4224
            rset.close();
4225
            stmt.close();
4226
        }
4227
        catch ( SQLException e )
4228
        {
4229
            handleSQLException(e, "");
4230
        }
4231
        finally
4232
        {
4233
            // this block is executed regardless of what happens in the try block
4234
            // even if an exception is thrown
4235
            // ensure disconnect
4236
            disconnect();
4237
        }
4238
    }
4239
 
4240
    /**
4241
     * Removes all elements and then re-populates mRunLevelCollection
4242
     * 
4243
     * Queries the RM database to determine the run_level of the current daemon.
4244
     * Populates the mRunLevelCollection with the query result set 
4245
     *  
4246
     * Used to determine if the (Slave) thread should still be running
4247
     *  
4248
     * Used by the Slave Daemon 
4249
     * Overridden in ReleaseManagerUtf 
4250
     * 
4251
     * @param rcon_id     Identify the Release_Config table entry for the daemon
4252
     * @param hostname    Name of the host machine
4253
     * @param daemon_mode Specifies the current daemon type
4254
     * 
4255
     * @exception SQLException
4256
     *                      Database connection errors
4257
     *                      Bad SQL statement
4258
     * @exception Exception Too many rows extracted from the database
4259
     *                      Empty(NULL) data in database
4260
     */
4261
    public void querySingleRunLevel(final int rcon_id, String hostname, char daemon_mode) throws SQLException, Exception
4262
    {
7044 dpurdie 4263
        mLogger.debug("querySingleRunLevel rcon_id {}", rcon_id);
6914 dpurdie 4264
        mRunLevelCollection.clear();
4265
 
4266
        try
4267
        {
4268
            connect();
4269
 
4270
            CallableStatement stmt = mConnection.prepareCall(
4271
                    "select rl.rcon_id, "+
4272
                    "  rl.current_run_level,"+
4273
                    "  NVL(rl.pause,0) as pause," +
4274
                    "  NVL2(rl.CURRENT_BUILD_FILES, 1 , 0) as bfPresent" +
4275
                    " from release_manager.run_level rl,"+
4276
                    "   release_manager.release_config rc" +
4277
                    " where rl.rcon_id = rc.rcon_id" +
4278
                    "   AND rc.rcon_id=" + rcon_id +
4279
                    "   AND rc.bmcon_id is not NULL" +
4280
                    "   AND rc.daemon_mode='" + daemon_mode + "'" +
4281
                    "   AND UPPER(rc.daemon_hostname)=UPPER('" + hostname + "')"
4282
                    );
7023 dpurdie 4283
            RmResultSet rset = new RmResultSet(stmt.executeQuery(),"querySingleRunLevel");
6914 dpurdie 4284
            int rsetSize = 0;
4285
            int current_run_level = 0;
4286
            int pause = 0;
4287
            int buildFilePresent = 0;
4288
 
4289
            while( rset.next() )
4290
            {
4291
                rsetSize++;
7023 dpurdie 4292
                current_run_level = rset.mustGetInt("current_run_level");
4293
                buildFilePresent = rset.getInt("bfPresent",0);
6914 dpurdie 4294
 
4295
                //
4296
                //  Pause: null -> 0 == Run
4297
                //         1         == Pause
4298
                //         2         == Disabled
4299
                //
7023 dpurdie 4300
                pause = rset.getInt("pause",0);
6914 dpurdie 4301
 
4302
                //  Ignore disabled daemons
4303
                if ( pause <= 1)
4304
                {
4305
                    RunLevelData runLevel = new RunLevelData(rcon_id, current_run_level, daemon_mode, buildFilePresent);
4306
                    mRunLevelCollection.add(runLevel);
4307
                }
4308
            }
4309
 
4310
            rset.close();
4311
            stmt.close();
4312
 
4313
            //  Must have no more than one record
4314
            //  Will have none if this daemon is no longer a part of the build set
4315
            //
4316
            if ( rsetSize > 1 )
4317
            {
4318
                //  show stopper
4319
                //  More rows than expected returned from the database
7033 dpurdie 4320
                mLogger.error("querySingleRunLevel rsetSize > 1");
6914 dpurdie 4321
                throw new Exception("querySingleRunLevel rsetSize > 1");
4322
            }
4323
        }
4324
        catch ( SQLException e )
4325
        {
4326
            handleSQLException(e, "");
4327
        }
4328
        finally
4329
        {
4330
            // this block is executed regardless of what happens in the try block
4331
            // even if an exception is thrown
4332
            // ensure disconnect
4333
            disconnect();
4334
        }
4335
    }
4336
 
4337
    /**Queries the RUN_LEVEL_SCHEDULE table to determine if we can proceed
4338
     * <br>Will not proceed if:
4339
     *      <br>- Inside a scheduled downtime
4340
     *      <br>- Indefinite pause present
4341
     * 
4342
     * <br>Should delete rows with a non NULL indefinite pause (but doesn't appear too)
4343
     *  
4344
     * <br>Assumes connection to database has been established
4345
     * 
4346
     * @param   resumeTime.value  Returns date-time when daemon can run again 
4347
     * 
4348
     * @return  False: if a row in the query result set indicates build service downtime is scheduled
4349
     *      <br>False: if a row in the query result set has a non NULL indefinite_pause
4350
     */
4351
    public boolean queryRunLevelSchedule(MutableDate resumeTime) throws SQLException, Exception
4352
    {
4353
        mLogger.debug("queryRunLevelSchedule");
4354
        boolean retVal = true;
4355
 
4356
        if ( mUseDatabase )
4357
        {
4358
            //
4359
            //  Clean up the run_level_schedule table by deleting out of date entries
4360
            //
4361
            try
4362
            {
4363
                CallableStatement stmt = mConnection.prepareCall( "BEGIN PK_BUILDAPI.DELETE_OUT_OF_DATE_SCHEDULE;   END;" );
4364
                stmt.executeUpdate();
4365
                stmt.close();
4366
                commit();
4367
            }
4368
            catch ( SQLException e )
4369
            {
4370
                handleSQLException(e, ":1");
4371
            }
4372
 
4373
            try
4374
            {
4375
                //  Extract info from the database
4376
                //
4377
                CallableStatement stmt = mConnection.prepareCall(
4378
                        "select scheduled_pause, scheduled_resume, repeat, indefinite_pause" + 
4379
                        " from release_manager.run_level_schedule");
4380
                ResultSet rset = stmt.executeQuery();
4381
                Date now = new Date();
4382
 
4383
                //
4384
                //  Scan the database information and determine if there is any reason
4385
                //  to pause. Terminate the loop on the first excuse to pause
4386
                //  as indefinite pause may have multiple (lots) of entries in the data
4387
                //  base.
4388
                //
4389
                while( retVal && rset.next() )
4390
                {
4391
                    //
4392
                    //  Examine the current row from the data base
4393
                    //  Expect one of two forms:
4394
                    //    1) scheduled_pause
4395
                    //       Must also have a scheduled_resume and a repeat
4396
                    //    2) indefinite_pause
4397
                    //
4398
 
4399
                    //  Look for scheduled_pause style of entry
4400
                    //
4401
                    Timestamp sp = rset.getTimestamp("scheduled_pause");
4402
                    if ( sp != null )
4403
                    {
4404
                        Date scheduledPause = new Date( sp.getTime() );
4405
                        Timestamp sr = rset.getTimestamp("scheduled_resume");
4406
 
4407
                        if ( sr != null )
4408
                        {
4409
                            Date scheduledResume = new Date( sr.getTime() );
4410
                            int repeat = rset.getInt("repeat");
7044 dpurdie 4411
                            mLogger.info("queryRunLevelSchedule repeat {}", repeat);
6914 dpurdie 4412
 
4413
                            //
4414
                            //  Have scheduled_pause and scheduled_resume
4415
                            //  Examine the repeat field and determine how these are used
4416
                            //  Supported repeat:
4417
                            //      0:Once Only
4418
                            //      1:Daily           Year, Month and Day information is ignored
4419
                            //      7:Weekly          Only day of week is utilized
4420
                            //
4421
                            if ( !rset.wasNull() )
4422
                            {
4423
                                GregorianCalendar startOfDowntime = new GregorianCalendar();
4424
                                GregorianCalendar endOfDowntime = new GregorianCalendar();
4425
                                GregorianCalendar clock = new GregorianCalendar();
4426
 
4427
                                switch( repeat )
4428
                                {
4429
                                    case 0:
4430
                                        // Once Only
4431
                                        // Simple check between start and end date-times
4432
                                        if ( scheduledPause.before(now) && scheduledResume.after(now) )
4433
                                        {
4434
                                            mLogger.warn("queryRunLevelSchedule one off scheduled downtime");
4435
                                            resumeTime.value = scheduledResume;
4436
                                            retVal = false;
4437
                                        }
4438
                                        break;
4439
 
4440
                                    case 1:
4441
 
4442
                                        //  Daily
4443
                                        //  Create start and end times, then massage some fields
4444
                                        //  to reflect todays date.
4445
                                        //  Use start and end times from scheduled_pause and scheduled_resume
4446
                                        //
4447
 
4448
                                        startOfDowntime.setTime(scheduledPause);
4449
                                        endOfDowntime.setTime(scheduledResume);
4450
                                        clock.setTime(now);
4451
 
4452
                                        // Force date fields to todays date
4453
                                        endOfDowntime.set  ( clock.get(Calendar.YEAR), clock.get(Calendar.MONTH), clock.get(Calendar.DAY_OF_MONTH) );
4454
                                        startOfDowntime.set( clock.get(Calendar.YEAR), clock.get(Calendar.MONTH), clock.get(Calendar.DAY_OF_MONTH) );
4455
 
4456
                                        if ( startOfDowntime.before(clock) && endOfDowntime.after(clock) )
4457
                                        {
4458
                                            mLogger.warn("queryRunLevelSchedule daily scheduled downtime");
4459
                                            resumeTime.value.setTime(endOfDowntime.getTimeInMillis());
4460
                                            retVal = false;
4461
                                        }
4462
                                        break;
4463
 
4464
                                    case 7:
4465
 
4466
                                        // Weekly
4467
                                        // Create start and end times, then massage some fields
4468
                                        // to reflect todays date.
4469
                                        // Use DayOfWeek and time from scheduled_pause
4470
                                        // Use time from scheduled_resume
4471
                                        //
4472
                                        startOfDowntime.setTime(scheduledPause);
4473
                                        endOfDowntime.setTime(scheduledResume);
4474
                                        clock.setTime(now);
4475
 
4476
                                        // Only interested in one day of the week
4477
                                        if ( startOfDowntime.get(Calendar.DAY_OF_WEEK) == clock.get(Calendar.DAY_OF_WEEK) )
4478
                                        {
4479
                                            endOfDowntime.set  ( clock.get(Calendar.YEAR), clock.get(Calendar.MONTH), clock.get(Calendar.DAY_OF_MONTH) );
4480
                                            startOfDowntime.set( clock.get(Calendar.YEAR), clock.get(Calendar.MONTH), clock.get(Calendar.DAY_OF_MONTH) );
4481
 
4482
                                            if ( startOfDowntime.before(clock) && endOfDowntime.after(clock) )
4483
                                            {
4484
                                                mLogger.warn("queryRunLevelSchedule weekly scheduled downtime");
4485
                                                resumeTime.value.setTime(endOfDowntime.getTimeInMillis());
4486
                                                retVal = false;
4487
                                            }
4488
                                        }
4489
                                        break;
4490
 
4491
                                   default:
4492
                                       //
4493
                                       //   Unexpected value
4494
                                       break;
4495
                                }
4496
                            }
4497
                        }
4498
                    }
4499
 
4500
                    //
4501
                    //  Look for indefinite_pause style of entry
4502
                    //  Note: due to an implementation error there may be many
4503
                    //        rows that match. We only need one. The scan will
4504
                    //        be terminated if we find any
4505
                    //  
4506
                    //
4507
                    String ip = rset.getString("indefinite_pause");
4508
                    if ( ip != null )
4509
                    {
4510
                        // indefinite pause is non null
4511
                        mLogger.warn("queryRunLevelSchedule indefinite pause");
4512
                        GregorianCalendar clock = new GregorianCalendar();
4513
                        clock.setTime(now);
4514
                        // wait a minute
4515
                        resumeTime.value.setTime(clock.getTimeInMillis() + 60000);
4516
                        retVal = false;
4517
                    }
4518
                }
4519
 
4520
                rset.close();
4521
                stmt.close();
4522
            }
4523
            catch ( SQLException e )
4524
            {
4525
                handleSQLException(e, ":2");
4526
            }
4527
        }
4528
 
7044 dpurdie 4529
        mLogger.info("queryRunLevelSchedule returning {}", retVal);
6914 dpurdie 4530
        return retVal;
4531
    }
4532
 
4533
    /**persists the runLevel in the RUN_LEVEL table for the rcon_id primary key
4534
     * 
4535
     * Overridden in the UTF
4536
     * 
4537
     * @param   rcon_id         Identifies the Release Connection (daemon)
4538
     * @param   runLevel        The run level to set
4539
     * 
4540
     */
4541
    public void updateCurrentRunLevel(final int rcon_id, final int runLevel) throws SQLException, Exception
4542
    {
4543
        mLogger.debug("updateCurrentRunLevel");
4544
 
4545
        try
4546
        {
4547
            connect();
4548
 
7044 dpurdie 4549
            mLogger.warn("updateCurrentRunLevel: Set Runlevel:{}, rconId:{}", runLevel, rcon_id);
6914 dpurdie 4550
            PreparedStatement stmt = mConnection.prepareCall("update release_manager.run_level set current_run_level=" + runLevel + ", keep_alive=SYSDATE where rcon_id=" + rcon_id);
4551
            stmt.executeUpdate();
4552
            stmt.close();
4553
 
4554
            mLogger.info("updateCurrentRunLevel: committing");
4555
            commit();
4556
            mLogger.info("updateCurrentRunLevel: committed");
4557
        }
4558
        catch ( SQLException e )
4559
        {
4560
            handleSQLException(e, "");
4561
        }
4562
        finally
4563
        {
4564
            // this block is executed regardless of what happens in the try block
4565
            // even if an exception is thrown
4566
            // ensure disconnect
4567
            disconnect();
4568
        }
4569
 
4570
    }
4571
 
4572
    /** Report the current build plan
4573
     *  This is only for display purposes
4574
     *  Assume a connection has been established. Will not commit if the operation is covered by the Mutex
4575
     *  
4576
     * @param mRtagId - Release we are building
4577
     * @param mBuildOrder - Ordered list of PackageVersions that we plan to build
4578
     * @throws Exception 
4579
     * @throws SQLException 
4580
     */
4581
    public void reportPlan(int mRtagId, List<Package> mBuildOrder) throws SQLException, Exception {
7044 dpurdie 4582
        mLogger.debug("reportPlan {}", mRtagId);
6914 dpurdie 4583
 
4584
        if ( mUseDatabase )
4585
        {
4586
            try
4587
            {
4588
                Iterator<Package> it = mBuildOrder.iterator();
4589
                int fragment = 0;
4590
                CallableStatement stmt = mConnection.prepareCall( "call PK_BUILDAPI.set_build_plan(?,?,?)" );
4591
 
4592
                do {
4593
                    //
4594
                    // Generate a comma separated list of PVIDs
4595
                    // Limit the length of the string since we can only pump 4000 chars into a string
4596
                    // Allow 10 chars per PV_ID
4597
                    //
4598
                    StringBuilder pvList = new StringBuilder();
4599
                    String joiner = "";
4600
 
4601
                    while ( pvList.length() < 3000  && it.hasNext() )
4602
                    {
4603
                        Package p = it.next();
4604
                        pvList.append(joiner);
4605
                        pvList.append(p.mId);
4606
                        joiner = ",";
4607
                    }
4608
 
4609
                    //  Terminate if we have nothing to add and its not the first pass
4610
                    if (pvList.length() <= 0 && fragment != 0)
4611
                    {
4612
                        break;
4613
                    }
4614
 
4615
                    //
4616
                    //  Insert data - one fragment at a time
4617
                    //
4618
                    stmt.setInt    ( 1, mRtagId );
4619
                    stmt.setInt    ( 2, fragment );
4620
                    stmt.setString ( 3, pvList.toString() );
4621
                    stmt.executeUpdate();
4622
                    fragment++;
4623
 
4624
                } while(true);
4625
 
4626
                stmt.close();
4627
 
4628
                //  Commit only if not covered by the Mutex
4629
                if ( ! mDoNotCommit )
4630
                    commit();
4631
            }
4632
            catch ( SQLException e )
4633
            {
4634
                handleSQLException(e, "");
4635
            }
4636
            finally
4637
            {
4638
                // this block is executed regardless of what happens in the try block
4639
                // even if an exception is thrown
4640
            }
4641
        }
4642
    }
4643
 
4644
    public void updateBuildDuration(int pvId, int duration) throws Exception {
7044 dpurdie 4645
        mLogger.debug("updateBuildDuration {}:{}", pvId, duration);
6914 dpurdie 4646
        if ( mUseDatabase )
4647
        {
4648
            try
4649
            {
4650
                connect();
4651
 
4652
                String sql = "update release_manager.package_versions set BUILD_TIME = ? where pv_id = ?";
4653
 
4654
                PreparedStatement stmt = mConnection.prepareStatement(sql);
4655
                stmt.setLong( 1,duration );
4656
                stmt.setLong( 2,pvId );
4657
                stmt.executeUpdate();
4658
                stmt.close();
4659
                commit();
4660
            }
4661
            catch ( SQLException e )
4662
            {
4663
                handleSQLException(e, "");
4664
            }
4665
            finally
4666
            {
4667
                disconnect();
4668
            }
4669
        }
4670
    }
4671
 
4672
    /**  Set RippleStop flag to 'w' for waiting
4673
     *  Indicated that the build system will Ripple the package, when the user confirms that it can be done
4674
     *  Assume database connection has been established
4675
     * @param mRtagId 
4676
     * 
4677
     * @param pkg - Package being processed
4678
     * @throws Exception 
4679
     */
4680
    public void setRippleStopWait(int mRtagId, Package pkg) throws Exception {
4681
 
4682
        // Use a stored procedure as it will also do logging
4683
        mLogger.debug("setRippleStopWait");
4684
        if ( mUseDatabase )
4685
        {
4686
            try
4687
            {
4688
                // "BEGIN  PK_PACKAGE.Set_Ripple_Stop( :PV_ID, :RTAG_ID, :USER_ID, :RSTATE );  END;"
4689
 
4690
                CallableStatement stmt = mConnection.prepareCall( "BEGIN  PK_PACKAGE.Set_Ripple_Stop( ?, ?, ?, 'w' );  END;" );
4691
                stmt.setInt(1, pkg.mId);        // PVID
4692
                stmt.setInt(2, mRtagId);        // RTAG ID 
4693
                stmt.setInt(3, 3768);           // USER ID - buildadm ( Yes I know its ugly )
4694
                stmt.executeUpdate();
4695
                stmt.close();
4696
            }
4697
            catch ( SQLException e )
4698
            {
4699
                handleSQLException(e, "");
4700
            }
4701
        }
4702
 
4703
    }
4704
 
4705
}
4706