Search in sources :

Example 1 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineStateDao method pipelineStateFor.

public PipelineState pipelineStateFor(String pipelineName) {
    String cacheKey = pipelineLockStateCacheKey(pipelineName);
    PipelineState pipelineState = (PipelineState) goCache.get(cacheKey);
    if (pipelineState != null) {
        return pipelineState.equals(PipelineState.NOT_LOCKED) ? null : pipelineState;
    }
    synchronized (cacheKey) {
        pipelineState = (PipelineState) goCache.get(cacheKey);
        if (pipelineState != null) {
            return pipelineState.equals(PipelineState.NOT_LOCKED) ? null : pipelineState;
        }
        pipelineState = (PipelineState) transactionTemplate.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus transactionStatus) {
                return sessionFactory.getCurrentSession().createCriteria(PipelineState.class).add(Restrictions.eq("pipelineName", pipelineName)).setCacheable(false).uniqueResult();
            }
        });
        if (pipelineState != null && pipelineState.isLocked()) {
            StageIdentifier lockedBy = (StageIdentifier) getSqlMapClientTemplate().queryForObject("lockedPipeline", pipelineState.getLockedByPipelineId());
            pipelineState.setLockedBy(lockedBy);
        }
        goCache.put(cacheKey, pipelineState == null ? PipelineState.NOT_LOCKED : pipelineState);
        return pipelineState;
    }
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 2 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineStateDaoCachingTest method unlockPipeline_shouldSavePipelineStateAndInvalidateCache.

@Test
public void unlockPipeline_shouldSavePipelineStateAndInvalidateCache() throws Exception {
    final TransactionSynchronizationAdapter[] transactionSynchronizationAdapter = { null };
    when(transactionTemplate.execute(any(org.springframework.transaction.support.TransactionCallbackWithoutResult.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            org.springframework.transaction.support.TransactionCallbackWithoutResult callback = (org.springframework.transaction.support.TransactionCallbackWithoutResult) invocation.getArguments()[0];
            callback.doInTransaction(new SimpleTransactionStatus());
            transactionSynchronizationAdapter[0].afterCommit();
            return null;
        }
    });
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            TransactionSynchronizationAdapter adapter = (TransactionSynchronizationAdapter) invocation.getArguments()[0];
            transactionSynchronizationAdapter[0] = adapter;
            return null;
        }
    }).when(transactionSynchronizationManager).registerSynchronization(any(TransactionSynchronization.class));
    final Pipeline pipeline = PipelineMother.pipeline("mingle");
    PipelineState pipelineState = new PipelineState(pipeline.getName(), pipeline.getFirstStage().getIdentifier());
    goCache.put(pipelineStateDao.pipelineLockStateCacheKey(pipeline.getName()), pipelineState);
    when(session.load(PipelineState.class, pipeline.getId())).thenReturn(pipelineState);
    pipelineStateDao.unlockPipeline(pipeline.getName());
    assertThat(goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipeline.getName())), is(nullValue()));
    ArgumentCaptor<PipelineState> pipelineStateArgumentCaptor = ArgumentCaptor.forClass(PipelineState.class);
    verify(session).saveOrUpdate(pipelineStateArgumentCaptor.capture());
    PipelineState savedPipelineState = pipelineStateArgumentCaptor.getValue();
    assertThat(savedPipelineState.isLocked(), is(false));
    assertThat(savedPipelineState.getLockedBy(), is(nullValue()));
}
Also used : TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) Pipeline(com.thoughtworks.go.domain.Pipeline) PipelineState(com.thoughtworks.go.domain.PipelineState) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 3 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineStateDaoCachingTest method lockedPipeline_shouldCacheLockedPipelineStatus.

@Test
public void lockedPipeline_shouldCacheLockedPipelineStatus() throws Exception {
    String pipelineName = "mingle";
    when(transactionTemplate.execute(any(org.springframework.transaction.support.TransactionCallback.class))).thenReturn(new PipelineState(pipelineName, new StageIdentifier(pipelineName, 1, "1", 1L, "s1", "1")));
    PipelineState pipelineState = pipelineStateDao.pipelineStateFor(pipelineName);
    assertThat(goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipelineName)), is(pipelineState));
    PipelineState secondAttempt = pipelineStateDao.pipelineStateFor(pipelineName);
    assertSame(pipelineState, secondAttempt);
    verify(transactionTemplate, times(1)).execute(any(TransactionCallback.class));
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) TransactionCallback(org.springframework.transaction.support.TransactionCallback) Test(org.junit.Test)

Example 4 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineStateDaoTest method shouldNotCorruptCacheIfSaveFailsWhileUnLocking.

@Test
public void shouldNotCorruptCacheIfSaveFailsWhileUnLocking() {
    String pipelineName = UUID.randomUUID().toString();
    PipelineState pipelineState = new PipelineState(pipelineName);
    long lockedByPipelineId = 1;
    pipelineState.lock(lockedByPipelineId);
    goCache.put(pipelineStateDao.pipelineLockStateCacheKey(pipelineName), pipelineState);
    when(transactionTemplate.execute(any(org.springframework.transaction.support.TransactionCallbackWithoutResult.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            org.springframework.transaction.support.TransactionCallbackWithoutResult callback = (org.springframework.transaction.support.TransactionCallbackWithoutResult) invocation.getArguments()[0];
            callback.doInTransaction(new SimpleTransactionStatus());
            return null;
        }
    });
    doThrow(new RuntimeException("could not save!")).when(session).saveOrUpdate(any(PipelineState.class));
    try {
        pipelineStateDao.unlockPipeline(pipelineName);
        fail("save should have thrown an exception!");
    } catch (Exception e) {
        PipelineState stateFromCache = (PipelineState) goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipelineName));
        assertThat(stateFromCache.isLocked(), is(true));
        assertThat(stateFromCache.getLockedByPipelineId(), is(lockedByPipelineId));
    }
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) Test(org.junit.Test)

Example 5 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineLockServiceTest method shouldNotAllowStageFromLockedPipelineToBeScheduled.

@Test
public void shouldNotAllowStageFromLockedPipelineToBeScheduled() throws Exception {
    Pipeline pipeline = PipelineMother.firstStageBuildingAndSecondStageScheduled("mingle", asList("dev", "ft"), asList("test"));
    PipelineState pipelineState = new PipelineState(pipeline.getName(), new StageIdentifier(pipeline.getName(), 9999, "1.2.9999", "stage", "1"));
    pipelineState.lock(1);
    when(pipelineStateDao.pipelineStateFor("mingle")).thenReturn(pipelineState);
    when(goConfigService.isLockable(pipeline.getName())).thenReturn(true);
    pipelineLockService.lockIfNeeded(pipeline);
    assertThat(pipelineLockService.canScheduleStageInPipeline(pipeline.getIdentifier()), is(false));
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) Pipeline(com.thoughtworks.go.domain.Pipeline) Test(org.junit.Test)

Aggregations

PipelineState (com.thoughtworks.go.domain.PipelineState)11 Test (org.junit.Test)10 Pipeline (com.thoughtworks.go.domain.Pipeline)6 StageIdentifier (com.thoughtworks.go.domain.StageIdentifier)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 SimpleTransactionStatus (org.springframework.transaction.support.SimpleTransactionStatus)4 TransactionCallback (org.springframework.transaction.support.TransactionCallback)3 Answer (org.mockito.stubbing.Answer)2 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)2 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)2 Stage (com.thoughtworks.go.domain.Stage)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1