Search in sources :

Example 6 with RawContainerHandle

use of org.apache.derby.iapi.store.raw.data.RawContainerHandle in project derby by apache.

the class BaseDataFileFactory method addContainer.

/**
 * Add a container with a specified page size to a segment.
 *		@exception StandardException Standard Derby error policy
 */
public long addContainer(RawTransaction t, long segmentId, long input_containerid, int mode, Properties tableProperties, int temporaryFlag) throws StandardException {
    if (SanityManager.DEBUG) {
        if ((mode & ContainerHandle.MODE_CREATE_UNLOGGED) != 0)
            SanityManager.ASSERT((mode & ContainerHandle.MODE_UNLOGGED) != 0, "cannot have CREATE_UNLOGGED set but UNLOGGED not set");
    }
    // If client has provided a containerid then use it, else use the
    // internally generated one from getNextId().
    long containerId = ((input_containerid != ContainerHandle.DEFAULT_ASSIGN_ID) ? input_containerid : getNextId());
    ContainerKey identity = new ContainerKey(segmentId, containerId);
    boolean tmpContainer = (segmentId == ContainerHandle.TEMPORARY_SEGMENT);
    ContainerHandle ch = null;
    LockingPolicy cl = null;
    if (!tmpContainer) {
        if (isReadOnly()) {
            throw StandardException.newException(SQLState.DATA_CONTAINER_READ_ONLY);
        }
        cl = t.newLockingPolicy(LockingPolicy.MODE_CONTAINER, TransactionController.ISOLATION_SERIALIZABLE, true);
        if (SanityManager.DEBUG)
            SanityManager.ASSERT(cl != null);
        ch = t.openContainer(identity, cl, (ContainerHandle.MODE_FORUPDATE | ContainerHandle.MODE_OPEN_FOR_LOCK_ONLY));
    }
    FileContainer container = (FileContainer) containerCache.create(identity, tableProperties);
    // create the first alloc page and the first user page,
    // if this fails for any reason the transaction
    // will roll back and the container will be dropped (removed)
    ContainerHandle containerHdl = null;
    Page firstPage = null;
    try {
        // make sure to open it with IS_KEPT too.
        if (tmpContainer && ((temporaryFlag & TransactionController.IS_KEPT) == TransactionController.IS_KEPT)) {
            mode |= ContainerHandle.MODE_TEMP_IS_KEPT;
        }
        // open no-locking as we already have the container locked
        containerHdl = t.openContainer(identity, null, (ContainerHandle.MODE_FORUPDATE | mode));
        // we just added it, containerHdl should not be null
        if (SanityManager.DEBUG)
            SanityManager.ASSERT(containerHdl != null);
        if (!tmpContainer) {
            // make it persistent (in concept if not in reality)
            RawContainerHandle rch = (RawContainerHandle) containerHdl;
            ContainerOperation lop = new ContainerOperation(rch, ContainerOperation.CREATE);
            // mark the container as pre-dirtied so that if a checkpoint
            // happens after the log record is sent to the log stream, the
            // cache cleaning will wait for this change.
            rch.preDirty(true);
            try {
                t.logAndDo(lop);
                // flush the log to reduce the window between where
                // the container is created & synced and the log record
                // for it makes it to disk. If we fail in this
                // window we will leave a stranded container file.
                flush(t.getLastLogInstant());
            } finally {
                // in case logAndDo fail, make sure the container is not
                // stuck in preDirty state.
                rch.preDirty(false);
            }
        }
        firstPage = containerHdl.addPage();
    } finally {
        if (firstPage != null) {
            firstPage.unlatch();
            firstPage = null;
        }
        containerCache.release(container);
        if (containerHdl != null) {
            containerHdl.close();
            containerHdl = null;
        }
        if (!tmpContainer) {
            // this should do nothing, since we requested isolation 3
            // but we can't assume that, so call the policy correctly.
            cl.unlockContainer(t, ch);
        }
    }
    return containerId;
}
Also used : Page(org.apache.derby.iapi.store.raw.Page) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle) ContainerKey(org.apache.derby.iapi.store.raw.ContainerKey) LockingPolicy(org.apache.derby.iapi.store.raw.LockingPolicy) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle) ContainerHandle(org.apache.derby.iapi.store.raw.ContainerHandle) StreamContainerHandle(org.apache.derby.iapi.store.raw.StreamContainerHandle)

Example 7 with RawContainerHandle

use of org.apache.derby.iapi.store.raw.data.RawContainerHandle in project derby by apache.

the class T_RawStoreFactory method P054.

/**
 *		Test internal transaction
 *
 *		@exception T_Fail Unexpected behaviour from the API
 *		@exception StandardException Unexpected exception from the implementation
 */
protected void P054() throws StandardException, T_Fail {
    if (!testRollback)
        return;
    ContextManager previousCM = contextService.getCurrentContextManager();
    ContextManager cm1 = contextService.newContextManager();
    contextService.setCurrentContextManager(cm1);
    Transaction tuser = t_util.t_startTransaction();
    Transaction tinternal = null;
    try {
        long cid1 = t_util.t_addContainer(tuser, 0);
        ContainerHandle c1 = t_util.t_openContainer(tuser, 0, cid1, true);
        Page p1 = t_util.t_addPage(c1);
        t_util.t_commit(tuser);
        // insert a row using user transaction
        T_RawStoreRow row = new T_RawStoreRow(REC_001);
        c1 = t_util.t_openContainer(tuser, 0, cid1, true);
        p1 = t_util.t_getLastPage(c1);
        RecordHandle r1 = t_util.t_insert(p1, row);
        REPORT("starting internal transaction");
        tinternal = t_util.t_startInternalTransaction();
        long cid2 = t_util.t_addContainer(tinternal, 0);
        ContainerHandle c2 = t_util.t_openContainer(tinternal, 0, cid2, true);
        Page p2 = t_util.t_addPage(c2);
        RecordHandle r2 = t_util.t_insert(p2, row);
        // commit internal transaction
        tinternal.commit();
        // this will close the container and release
        tinternal.abort();
        // the page
        tinternal.close();
        tinternal = null;
        REPORT("commit internal transaction");
        // abort user transaction
        t_util.t_abort(tuser);
        REPORT("rollback user transaction");
        c1 = t_util.t_openContainer(tuser, 0, cid1, true);
        p1 = t_util.t_getPage(c1, r1.getPageNumber());
        if (p1.recordExists(r1, false))
            throw T_Fail.testFailMsg("user transaction failed to rollback");
        c2 = t_util.t_openContainer(tuser, 0, cid2, true);
        // this should be unaffected by the
        t_util.t_checkFetch(c2, r2, REC_001);
        // user transaction rollback
        p2 = t_util.t_getLastPage(c2);
        T_RawStoreRow row2 = new T_RawStoreRow(REC_002);
        if (!p2.spaceForInsert()) {
            REPORT("P054 not run, page cannot accomodate 2 rows");
            return;
        }
        RecordHandle r21 = t_util.t_insert(p2, row2);
        // throw an exception, make sure everything is aborted.
        tinternal = t_util.t_startInternalTransaction();
        long cid3 = t_util.t_addContainer(tinternal, 0);
        ContainerHandle c3 = t_util.t_openContainer(tinternal, 0, cid3, true);
        Page p3 = t_util.t_addPage(c3);
        RecordHandle r3 = t_util.t_insert(p3, row);
        try {
            // this will throw a data statement exception
            t_util.t_insertAtSlot(p3, 100, row);
        } catch (StandardException se) {
            REPORT("cleanup on error");
            // Assume database is not active. DERBY-4856 thread dump
            cm1.cleanupOnError(se, false);
            REPORT("done cleanup on error");
        }
        tinternal = null;
        // tuser = t_util.t_startTransaction();
        c2 = t_util.t_openContainer(tuser, 0, cid2, true);
        t_util.t_checkFetch(c2, r2, REC_001);
        p2 = t_util.t_getPage(c2, r21.getPageNumber());
        if (p2.recordExists(r21, false))
            throw T_Fail.testFailMsg("expect user transaction to rollback");
        // this should fail
        ContainerKey id3 = new ContainerKey(0, cid3);
        c3 = tuser.openContainer(id3, ContainerHandle.MODE_READONLY);
        if (c3 != null)
            throw T_Fail.testFailMsg("expect internal transaction to rollback");
        LockingPolicy nolock = tuser.newLockingPolicy(LockingPolicy.MODE_NONE, 0, false);
        RawContainerHandle stub = ((RawTransaction) tuser).openDroppedContainer(id3, nolock);
        if (stub == null)
            throw T_Fail.testFailMsg("expect container to be dropped");
        if (stub.getContainerStatus() != RawContainerHandle.COMMITTED_DROP)
            throw T_Fail.testFailMsg("expect container to be committed dropped");
        // this should fail
        p3 = stub.getPage(r3.getPageNumber());
        if (p3 != null)
            throw T_Fail.testFailMsg("should not getpage with committed dropped container");
        PASS("P054");
        // cleanup
        t_util.t_dropContainer(tuser, 0, cid2);
        // cleanup
        t_util.t_dropContainer(tuser, 0, cid1);
        if (tinternal != null) {
            t_util.t_abort(tinternal);
            tinternal.close();
        }
        if (tuser != null) {
            t_util.t_commit(tuser);
            tuser.close();
        }
    } finally {
        contextService.resetCurrentContextManager(cm1);
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction) ContextManager(org.apache.derby.iapi.services.context.ContextManager) RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle)

Example 8 with RawContainerHandle

use of org.apache.derby.iapi.store.raw.data.RawContainerHandle in project derby by apache.

the class T_Recovery method S301.

/*
	 * incomplete transactions with create container
	 */
protected void S301() throws T_Fail, StandardException {
    T_TWC ctx = t_util.t_startTransactionWithContext();
    Transaction t = ctx.tran;
    Page page = null;
    ctx.switchTransactionContext();
    try {
        long cid1 = t_util.t_addContainer(t, 0);
        ContainerHandle c1 = t_util.t_openContainer(t, 0, cid1, true);
        page = t_util.t_addPage(c1);
        T_RawStoreRow row1 = new T_RawStoreRow(REC_001);
        RecordHandle r1 = t_util.t_insert(page, row1);
        t.abort();
        ContainerKey id1 = new ContainerKey(0, cid1);
        c1 = t.openContainer(id1, ContainerHandle.MODE_READONLY);
        if (c1 != null)
            throw T_Fail.testFailMsg("expect container to be dropped");
        LockingPolicy nolock = t.newLockingPolicy(LockingPolicy.MODE_NONE, 0, false);
        RawContainerHandle stub = ((RawTransaction) t).openDroppedContainer(new ContainerKey(0, cid1), nolock);
        /*Not true always after fix for p4#25641(fix for bug:4580)
			  Checkpoint calls cleans up the stubs that not necessary
			  for recovery.
			  if (stub == null)
			  throw T_Fail.testFailMsg("drop container should still be there");
			*/
        if (stub != null)
            if (stub.getContainerStatus() != RawContainerHandle.COMMITTED_DROP)
                throw T_Fail.testFailMsg("expect container to be committed dropped");
        long cid2 = t_util.t_addContainer(t, 0);
        ContainerHandle c2 = t_util.t_openContainer(t, 0, cid2, true);
        page = t_util.t_addPage(c2);
        T_RawStoreRow row2 = new T_RawStoreRow(REC_002);
        RecordHandle r2 = t_util.t_insert(page, row2);
        REPORT("setup S301: cid1 " + cid1 + " cid2 " + cid2);
        register(key(301, 1), cid1);
        register(key(301, 2), cid2);
    } finally {
        if (page != null && page.isLatched())
            page.unlatch();
        ctx.resetContext();
    }
// let recovery rollback incomplete transaction
}
Also used : RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction) RawTransaction(org.apache.derby.iapi.store.raw.xact.RawTransaction) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle)

Aggregations

RawContainerHandle (org.apache.derby.iapi.store.raw.data.RawContainerHandle)8 LockingPolicy (org.apache.derby.iapi.store.raw.LockingPolicy)4 RawTransaction (org.apache.derby.iapi.store.raw.xact.RawTransaction)3 StandardException (org.apache.derby.shared.common.error.StandardException)2 ContextManager (org.apache.derby.iapi.services.context.ContextManager)1 Serviceable (org.apache.derby.iapi.services.daemon.Serviceable)1 ContainerHandle (org.apache.derby.iapi.store.raw.ContainerHandle)1 ContainerKey (org.apache.derby.iapi.store.raw.ContainerKey)1 Page (org.apache.derby.iapi.store.raw.Page)1 StreamContainerHandle (org.apache.derby.iapi.store.raw.StreamContainerHandle)1 LogInstant (org.apache.derby.iapi.store.raw.log.LogInstant)1 StorageFile (org.apache.derby.io.StorageFile)1