Search in sources :

Example 1 with CandlepinModeChange

use of org.candlepin.model.CandlepinModeChange in project candlepin by candlepin.

the class StatusResourceTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    CandlepinModeChange mockModeChange = new CandlepinModeChange(new Date(), Mode.NORMAL, Reason.STARTUP);
    CandlepinQuery mockCPQuery = mock(CandlepinQuery.class);
    when(mockCPQuery.list()).thenReturn(new ArrayList<Rules>());
    when(rulesCurator.listAll()).thenReturn(mockCPQuery);
    when(rulesCurator.getRules()).thenReturn(new Rules("// Version: 2.0\nBLAH"));
    when(mockedStatusCache.getStatus()).thenReturn(null);
    when(candlepinCache.getStatusCache()).thenReturn(mockedStatusCache);
    when(modeManager.getLastCandlepinModeChange()).thenReturn(mockModeChange);
}
Also used : CandlepinQuery(org.candlepin.model.CandlepinQuery) CandlepinModeChange(org.candlepin.model.CandlepinModeChange) Rules(org.candlepin.model.Rules) Date(java.util.Date) Before(org.junit.Before)

Example 2 with CandlepinModeChange

use of org.candlepin.model.CandlepinModeChange in project candlepin by candlepin.

the class SuspendModeTransitionerTest method setUp.

@Before
public void setUp() {
    when(candlepinCache.getStatusCache()).thenReturn(cache);
    CandlepinCommonTestConfig testConfig = new CandlepinCommonTestConfig();
    transitioner = new SuspendModeTransitioner(testConfig, execService, candlepinCache);
    transitioner.setModeManager(modeManager);
    transitioner.setQmf(qmf);
    transitioner.setQpidConnection(qpidConnection);
    startupModeChange = new CandlepinModeChange(new Date(), Mode.NORMAL, Reason.STARTUP);
    downModeChange = new CandlepinModeChange(new Date(), Mode.SUSPEND, Reason.QPID_DOWN);
    normalModeChange = new CandlepinModeChange(new Date(), Mode.NORMAL, Reason.QPID_UP);
}
Also used : CandlepinCommonTestConfig(org.candlepin.config.CandlepinCommonTestConfig) CandlepinModeChange(org.candlepin.model.CandlepinModeChange) Date(java.util.Date) Before(org.junit.Before)

Example 3 with CandlepinModeChange

use of org.candlepin.model.CandlepinModeChange in project candlepin by candlepin.

the class ModeManagerImpl method enterMode.

@Override
public void enterMode(Mode m, Reason reason) {
    /**
     * When suspend mode is disabled, the Candlepin should never get into Suspend Mode.
     * Candlepin is starting always in NORMAL mode, so disalowing the transition here should
     * guarantee that.
     *
     * In fact, this method enterMode shouldn't be even called when SUSPEND mode is disabled.
     * So this check here is more of a defensive programming approach.
     */
    if (!config.getBoolean(ConfigProperties.SUSPEND_MODE_ENABLED)) {
        log.debug("Suspend mode is disabled, ignoring mode transition");
        return;
    }
    if (reason == null) {
        String noReasonErrorString = "No reason supplied when trying to change CandlepinModeChange.";
        log.error(noReasonErrorString);
        throw new IllegalArgumentException(noReasonErrorString);
    }
    log.info("Entering new mode {} for reason {}", m, reason);
    Mode previousMode = cpMode.getMode();
    if (previousMode != m) {
        fireModeChangeEvent(m);
    }
    if (m.equals(Mode.SUSPEND)) {
        log.warn("Candlepin is entering suspend mode for the following reason: {}", reason);
    }
    cpMode = new CandlepinModeChange(new Date(), m, reason);
}
Also used : Mode(org.candlepin.model.CandlepinModeChange.Mode) CandlepinModeChange(org.candlepin.model.CandlepinModeChange) Date(java.util.Date)

Example 4 with CandlepinModeChange

use of org.candlepin.model.CandlepinModeChange in project candlepin by candlepin.

the class StatusResource method status.

/**
 * Retrieves the Status of the System
 * <p>
 * <pre>
 * {
 *   "result" : true,
 *   "version" : "0.9.10",
 *   "rulesVersion" : "5.8",
 *   "release" : "1",
 *   "standalone" : true,
 *   "timeUTC" : [date],
 *   "managerCapabilities" : [ "cores", "ram", "instance_multiplier" ],
 *   "rulesSource" : "DEFAULT"
 * }
 * </pre>
 * <p>
 * Status to see if a server is up and running
 *
 * @return a Status object
 * @httpcode 200
 */
@GET
@ApiOperation(value = "Status", notes = "Returns status of the server", authorizations = {})
@Produces({ MediaType.APPLICATION_JSON })
@SecurityHole(noAuth = true, anon = true)
public Status status() {
    StatusCache statusCache = candlepinCache.getStatusCache();
    Status cached = statusCache.getStatus();
    if (cached != null) {
        return cached;
    }
    /*
         * Originally this was used to indicate database connectivity being good/bad.
         * In reality it could never be false, the request would fail. This check has
         * been moved to GET /status/db.
         */
    boolean good = true;
    try {
        rulesCurator.getUpdatedFromDB();
    } catch (Exception e) {
        log.error("Error checking database connection", e);
        good = false;
    }
    CandlepinModeChange modeChange = modeManager.getLastCandlepinModeChange();
    if (modeChange.getMode() != Mode.NORMAL) {
        good = false;
    }
    Status status = new Status(good, version, release, standalone, jsProvider.getRulesVersion(), jsProvider.getRulesSource(), modeChange.getMode(), modeChange.getReason(), modeChange.getChangeTime());
    statusCache.setStatus(status);
    return status;
}
Also used : Status(org.candlepin.model.Status) StatusCache(org.candlepin.cache.StatusCache) CandlepinModeChange(org.candlepin.model.CandlepinModeChange) SecurityHole(org.candlepin.common.auth.SecurityHole) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with CandlepinModeChange

use of org.candlepin.model.CandlepinModeChange in project candlepin by candlepin.

the class SuspendModeTransitioner method transitionAppropriately.

/**
 * Attempts to transition Candlepin according to current Mode and current status of
 * the Qpid Broker. Logs and swallows possible exceptions - theoretically
 * there should be none.
 *
 * Most of the time the transition won't be required and this method will be no-op.
 * There is an edge-case when transitioning from SUSPEND to NORMAL mode.
 * During that transition, there is a small time window between checking the
 * Qpid status and attempt to reconnect. If the Qpid status is reported as
 * Qpid up, the transitioner will try to reconnect to the broker. This reconnect
 * may fail. In that case the transition to NORMAL mode shouldn't go through.
 */
public synchronized void transitionAppropriately() {
    log.debug("Attempting to transition to appropriate Mode");
    try {
        QpidStatus status = qmf.getStatus();
        CandlepinModeChange modeChange = modeManager.getLastCandlepinModeChange();
        log.debug("Qpid status is {}, the current mode is {}", status, modeChange);
        if (status != QpidStatus.CONNECTED) {
            qpidConnection.setConnectionStatus(STATUS.JMS_OBJECTS_STALE);
        }
        if (modeChange.getMode() == Mode.SUSPEND) {
            switch(status) {
                case CONNECTED:
                    log.info("Connection to qpid is restored! Reconnecting Qpid and" + " entering NORMAL mode");
                    failedAttempts = BigInteger.ZERO;
                    modeManager.enterMode(Mode.NORMAL, Reason.QPID_UP);
                    cleanStatusCache();
                    break;
                case FLOW_STOPPED:
                case DOWN:
                    failedAttempts = failedAttempts.add(BigInteger.ONE);
                    log.debug("Staying in {} mode. So far {} failed attempts", status, failedAttempts);
                    break;
                default:
                    throw new RuntimeException("Unknown status: " + status);
            }
        } else if (modeChange.getMode() == Mode.NORMAL) {
            switch(status) {
                case FLOW_STOPPED:
                    log.debug("Will need to transition Candlepin into SUSPEND Mode because " + "the Qpid connection is flow stopped");
                    modeManager.enterMode(Mode.SUSPEND, Reason.QPID_FLOW_STOPPED);
                    cleanStatusCache();
                    break;
                case DOWN:
                    log.debug("Will need to transition Candlepin into SUSPEND Mode because " + "the Qpid connection is down");
                    modeManager.enterMode(Mode.SUSPEND, Reason.QPID_DOWN);
                    cleanStatusCache();
                    break;
                case CONNECTED:
                    log.debug("Connection to Qpid is ok and current mode is NORMAL. No-op!");
                    break;
                default:
                    throw new RuntimeException("Unknown status: " + status);
            }
        }
    } catch (Throwable t) {
        log.error("Error while executing period Suspend Transitioner check", t);
    /*
             * Nothing more we can do here, since this is scheduled thread. We must
             * hope that this error won't infinitely recur with each scheduled execution
             */
    }
}
Also used : QpidStatus(org.candlepin.audit.QpidQmf.QpidStatus) CandlepinModeChange(org.candlepin.model.CandlepinModeChange)

Aggregations

CandlepinModeChange (org.candlepin.model.CandlepinModeChange)6 Date (java.util.Date)4 Before (org.junit.Before)3 ApiOperation (io.swagger.annotations.ApiOperation)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 QpidStatus (org.candlepin.audit.QpidQmf.QpidStatus)1 StatusCache (org.candlepin.cache.StatusCache)1 SecurityHole (org.candlepin.common.auth.SecurityHole)1 MapConfiguration (org.candlepin.common.config.MapConfiguration)1 CandlepinCommonTestConfig (org.candlepin.config.CandlepinCommonTestConfig)1 ModeManager (org.candlepin.controller.ModeManager)1 Mode (org.candlepin.model.CandlepinModeChange.Mode)1 CandlepinQuery (org.candlepin.model.CandlepinQuery)1 JobCurator (org.candlepin.model.JobCurator)1 Rules (org.candlepin.model.Rules)1 Status (org.candlepin.model.Status)1 ImportRecordJob (org.candlepin.pinsetter.tasks.ImportRecordJob)1 JobCleaner (org.candlepin.pinsetter.tasks.JobCleaner)1