Ejemplo n.º 1
0
void SessionManagerServer::saveSMTxnIDAndState()
{
	// caller holds the lock
	scoped_ptr<IDBDataFile> txnidfp(IDBDataFile::open(
									IDBPolicy::getType(txnidFilename.c_str(), IDBPolicy::WRITEENG),
									txnidFilename.c_str(), "wb", 0));
	if (!txnidfp) {
		perror("SessionManagerServer(): open");
		throw runtime_error("SessionManagerServer: Could not open the transaction ID file");
	}

	int filedata[2];
	filedata[0] = _verID;
	filedata[1] = _sysCatVerID;

	int err = txnidfp->write(filedata, 8);
	if (err < 0) {
		perror("SessionManagerServer::newTxnID(): write(verid)");
		throw runtime_error("SessionManagerServer::newTxnID(): write(verid) failed");
	}

	uint32_t lSystemState = systemState;
	// We don't save the pending flags, the force flag or the ready flag.
	lSystemState &= ~(SS_READY | SS_SUSPEND_PENDING | SS_SHUTDOWN_PENDING | SS_FORCE);
	err = txnidfp->write(&lSystemState, sizeof(int));
	if (err < 0) {
		perror("SessionManagerServer::saveSystemState(): write(systemState)");
		throw runtime_error("SessionManagerServer::saveSystemState(): write(systemState) failed");
	}

	txnidfp->flush();
}
void SessionManagerServer::loadState()
{
    int lastTxnID;
    int err;
    int lastSysCatVerId;

again:

    // There are now 3 pieces of info stored in the txnidfd file: last
    // transaction id, last system catalog version id, and the
    // system state flags. All these values are stored in shared, an
    // instance of struct Overlay.
    // If we fail to read a full four bytes for any value, then the
    // value isn't in the file, and we start with the default.

    if (!IDBPolicy::useHdfs())
    {
        // Last transaction id
        lseek(txnidfd, 0, SEEK_SET);
        err = read(txnidfd, &lastTxnID, 4);

        if (err < 0 && errno != EINTR)
        {
            perror("Sessionmanager::initSegment(): read");
            throw runtime_error("SessionManagerServer: read failed, aborting");
        }
        else if (err < 0)
            goto again;
        else if (err == sizeof(int))
            _verID = lastTxnID;

        // last system catalog version id
        err = read(txnidfd, &lastSysCatVerId, 4);

        if (err < 0 && errno != EINTR)
        {
            perror("Sessionmanager::initSegment(): read");
            throw runtime_error("SessionManagerServer: read failed, aborting");
        }
        else if (err < 0)
            goto again;
        else if (err == sizeof(int))
            _sysCatVerID = lastSysCatVerId;

        // System state. Contains flags regarding the suspend state of the system.
        err = read(txnidfd, &systemState, 4);

        if (err < 0 && errno == EINTR)
        {
            goto again;
        }
        else if (err == sizeof(int))
        {
            // Turn off the pending and force flags. They make no sense for a clean start.
            // Turn off the ready flag. DMLProc will set it back on when
            // initialized.
            systemState &=
                ~(SS_READY | SS_QUERY_READY | SS_SUSPEND_PENDING | SS_SHUTDOWN_PENDING | SS_ROLLBACK | SS_FORCE);
        }
        else
        {
            // else no problem. System state wasn't saved. Might be an upgraded system.
            systemState = 0;
        }
    }
    else if (IDBPolicy::exists(txnidFilename.c_str()))
    {
        scoped_ptr<IDBDataFile> txnidfp(IDBDataFile::open(
                                            IDBPolicy::getType(txnidFilename.c_str(),
                                                    IDBPolicy::WRITEENG),
                                            txnidFilename.c_str(), "rb", 0));

        if (!txnidfp)
        {
            perror("SessionManagerServer(): open");
            throw runtime_error("SessionManagerServer: Could not open the transaction ID file");
        }

        // Last transaction id
        txnidfp->seek(0, SEEK_SET);
        err = txnidfp->read(&lastTxnID, 4);

        if (err < 0 && errno != EINTR)
        {
            perror("Sessionmanager::initSegment(): read");
            throw runtime_error("SessionManagerServer: read failed, aborting");
        }
        else if (err < 0)
            goto again;
        else if (err == sizeof(int))
            _verID = lastTxnID;

        // last system catalog version id
        err = txnidfp->read(&lastSysCatVerId, 4);

        if (err < 0 && errno != EINTR)
        {
            perror("Sessionmanager::initSegment(): read");
            throw runtime_error("SessionManagerServer: read failed, aborting");
        }
        else if (err < 0)
            goto again;
        else if (err == sizeof(int))
            _sysCatVerID = lastSysCatVerId;

        // System state. Contains flags regarding the suspend state of the system.
        err = txnidfp->read(&systemState, 4);

        if (err < 0 && errno == EINTR)
        {
            goto again;
        }
        else if (err == sizeof(int))
        {
            // Turn off the pending and force flags. They make no sense for a clean start.
            // Turn off the ready flag. DMLProc will set it back on when
            // initialized.
            systemState &=
                ~(SS_READY | SS_QUERY_READY | SS_SUSPEND_PENDING | SS_SHUTDOWN_PENDING | SS_ROLLBACK | SS_FORCE);
        }
        else
        {
            // else no problem. System state wasn't saved. Might be an upgraded system.
            systemState = 0;
        }
    }
}