Search in sources :

Example 21 with RequestContext

use of org.neo4j.com.RequestContext in project neo4j by neo4j.

the class MasterImplTest method givenStartedAndAccessibleWhenNewLockSessionThenSucceeds.

@Test
public void givenStartedAndAccessibleWhenNewLockSessionThenSucceeds() throws Throwable {
    // Given
    MasterImpl.SPI spi = mockedSpi();
    Config config = config();
    when(spi.isAccessible()).thenReturn(true);
    when(spi.getTransactionChecksum(anyLong())).thenReturn(1L);
    MasterImpl instance = new MasterImpl(spi, mock(ConversationManager.class), mock(MasterImpl.Monitor.class), config);
    instance.start();
    HandshakeResult handshake = instance.handshake(1, newStoreIdForCurrentVersion()).response();
    // When
    try {
        instance.newLockSession(new RequestContext(handshake.epoch(), 1, 2, 0, 0));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : SPI(org.neo4j.kernel.ha.com.master.MasterImpl.SPI) Monitor(org.neo4j.kernel.ha.com.master.MasterImpl.Monitor) Config(org.neo4j.kernel.configuration.Config) RequestContext(org.neo4j.com.RequestContext) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) IllegalResourceException(org.neo4j.kernel.impl.transaction.IllegalResourceException) TransactionNotPresentOnMasterException(org.neo4j.com.TransactionNotPresentOnMasterException) NoSuchEntryException(org.neo4j.kernel.impl.util.collection.NoSuchEntryException) Test(org.junit.Test)

Example 22 with RequestContext

use of org.neo4j.com.RequestContext in project neo4j by neo4j.

the class MasterImplTest method shouldNotAllowCommitIfThereIsNoMatchingLockSession.

@Test
public void shouldNotAllowCommitIfThereIsNoMatchingLockSession() throws Throwable {
    // Given
    MasterImpl.SPI spi = mock(MasterImpl.SPI.class);
    DefaultConversationSPI conversationSpi = mockedConversationSpi();
    Config config = config();
    ConversationManager conversationManager = new ConversationManager(conversationSpi, config);
    when(spi.isAccessible()).thenReturn(true);
    when(spi.getTransactionChecksum(anyLong())).thenReturn(1L);
    mockEmptyResponse(spi);
    MasterImpl master = new MasterImpl(spi, conversationManager, mock(MasterImpl.Monitor.class), config);
    master.start();
    HandshakeResult handshake = master.handshake(1, newStoreIdForCurrentVersion()).response();
    RequestContext ctx = new RequestContext(handshake.epoch(), 1, 2, 0, 0);
    // When
    try {
        master.commit(ctx, mock(TransactionRepresentation.class));
        fail("Should have failed.");
    } catch (TransactionNotPresentOnMasterException e) {
        // Then
        assertThat(e.getMessage(), equalTo(new TransactionNotPresentOnMasterException(ctx).getMessage()));
    }
}
Also used : SPI(org.neo4j.kernel.ha.com.master.MasterImpl.SPI) DefaultConversationSPI(org.neo4j.kernel.ha.cluster.DefaultConversationSPI) Monitor(org.neo4j.kernel.ha.com.master.MasterImpl.Monitor) Config(org.neo4j.kernel.configuration.Config) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) TransactionNotPresentOnMasterException(org.neo4j.com.TransactionNotPresentOnMasterException) RequestContext(org.neo4j.com.RequestContext) Test(org.junit.Test)

Example 23 with RequestContext

use of org.neo4j.com.RequestContext in project neo4j by neo4j.

the class MasterImplTest method shouldAllowStartNewTransactionAfterClientSessionWasRemovedOnTimeout.

@Test
public void shouldAllowStartNewTransactionAfterClientSessionWasRemovedOnTimeout() throws Throwable {
    //Given
    MasterImpl.SPI spi = mockedSpi();
    DefaultConversationSPI conversationSpi = mockedConversationSpi();
    Monitor monitor = mock(Monitor.class);
    Config config = config();
    Client client = mock(Client.class);
    ConversationManager conversationManager = new ConversationManager(conversationSpi, config);
    int machineId = 1;
    MasterImpl master = new MasterImpl(spi, conversationManager, monitor, config);
    when(spi.isAccessible()).thenReturn(true);
    when(conversationSpi.acquireClient()).thenReturn(client);
    master.start();
    HandshakeResult handshake = master.handshake(1, newStoreIdForCurrentVersion()).response();
    RequestContext requestContext = new RequestContext(handshake.epoch(), machineId, 0, 0, 0);
    // When
    master.newLockSession(requestContext);
    master.acquireSharedLock(requestContext, ResourceTypes.NODE, 1L);
    conversationManager.stop(requestContext);
    master.newLockSession(requestContext);
    //Then
    Map<Integer, Collection<RequestContext>> transactions = master.getOngoingTransactions();
    assertEquals(1, transactions.size());
    assertThat(transactions.get(machineId), org.hamcrest.Matchers.hasItem(requestContext));
}
Also used : SPI(org.neo4j.kernel.ha.com.master.MasterImpl.SPI) DefaultConversationSPI(org.neo4j.kernel.ha.cluster.DefaultConversationSPI) Config(org.neo4j.kernel.configuration.Config) Monitor(org.neo4j.kernel.ha.com.master.MasterImpl.Monitor) Collection(java.util.Collection) RequestContext(org.neo4j.com.RequestContext) Client(org.neo4j.kernel.impl.locking.Locks.Client) Test(org.junit.Test)

Example 24 with RequestContext

use of org.neo4j.com.RequestContext in project neo4j by neo4j.

the class MasterImplTest method shouldStopLockSessionOnFailureWhereThereIsAnActiveLockAcquisition.

@Test
public void shouldStopLockSessionOnFailureWhereThereIsAnActiveLockAcquisition() throws Throwable {
    // GIVEN
    CountDownLatch latch = new CountDownLatch(1);
    try {
        Client client = newWaitingLocksClient(latch);
        MasterImpl master = newMasterWithLocksClient(client);
        HandshakeResult handshake = master.handshake(1, newStoreIdForCurrentVersion()).response();
        // WHEN
        RequestContext context = new RequestContext(handshake.epoch(), 1, 2, 0, 0);
        master.newLockSession(context);
        Future<Void> acquireFuture = otherThread.execute(state -> {
            master.acquireExclusiveLock(context, ResourceTypes.NODE, 1L);
            return null;
        });
        otherThread.get().waitUntilWaiting();
        master.endLockSession(context, false);
        verify(client).stop();
        verify(client, never()).close();
        latch.countDown();
        acquireFuture.get();
        // THEN
        verify(client).close();
    } finally {
        latch.countDown();
    }
}
Also used : RequestContext(org.neo4j.com.RequestContext) CountDownLatch(java.util.concurrent.CountDownLatch) Client(org.neo4j.kernel.impl.locking.Locks.Client) Test(org.junit.Test)

Example 25 with RequestContext

use of org.neo4j.com.RequestContext in project neo4j by neo4j.

the class MasterImplTest method shouldAllowCommitIfClientHoldsNoLocks.

@Test
public void shouldAllowCommitIfClientHoldsNoLocks() throws Throwable {
    // Given
    MasterImpl.SPI spi = mock(MasterImpl.SPI.class);
    Config config = config();
    DefaultConversationSPI conversationSpi = mockedConversationSpi();
    ConversationManager conversationManager = new ConversationManager(conversationSpi, config);
    Client locks = mock(Client.class);
    when(locks.trySharedLock(ResourceTypes.SCHEMA, ResourceTypes.schemaResource())).thenReturn(true);
    when(spi.isAccessible()).thenReturn(true);
    when(spi.getTransactionChecksum(anyLong())).thenReturn(1L);
    when(conversationSpi.acquireClient()).thenReturn(locks);
    mockEmptyResponse(spi);
    MasterImpl master = new MasterImpl(spi, conversationManager, mock(MasterImpl.Monitor.class), config);
    master.start();
    HandshakeResult handshake = master.handshake(1, newStoreIdForCurrentVersion()).response();
    int no_lock_session = -1;
    RequestContext ctx = new RequestContext(handshake.epoch(), 1, no_lock_session, 0, 0);
    TransactionRepresentation tx = mock(TransactionRepresentation.class);
    // When
    master.commit(ctx, tx);
    // Then
    verify(spi).applyPreparedTransaction(tx);
}
Also used : SPI(org.neo4j.kernel.ha.com.master.MasterImpl.SPI) DefaultConversationSPI(org.neo4j.kernel.ha.cluster.DefaultConversationSPI) Monitor(org.neo4j.kernel.ha.com.master.MasterImpl.Monitor) Config(org.neo4j.kernel.configuration.Config) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) RequestContext(org.neo4j.com.RequestContext) Client(org.neo4j.kernel.impl.locking.Locks.Client) Test(org.junit.Test)

Aggregations

RequestContext (org.neo4j.com.RequestContext)38 Test (org.junit.Test)21 Config (org.neo4j.kernel.configuration.Config)13 SPI (org.neo4j.kernel.ha.com.master.MasterImpl.SPI)12 Response (org.neo4j.com.Response)10 LockResult (org.neo4j.kernel.ha.lock.LockResult)10 StoreId (org.neo4j.kernel.impl.store.StoreId)9 IOException (java.io.IOException)8 StoreWriter (org.neo4j.com.storecopy.StoreWriter)8 DefaultConversationSPI (org.neo4j.kernel.ha.cluster.DefaultConversationSPI)8 MasterClient (org.neo4j.kernel.ha.com.slave.MasterClient)8 Client (org.neo4j.kernel.impl.locking.Locks.Client)8 TransactionRepresentation (org.neo4j.kernel.impl.transaction.TransactionRepresentation)8 ResponseUnpacker (org.neo4j.com.storecopy.ResponseUnpacker)7 HandshakeResult (org.neo4j.kernel.ha.com.master.HandshakeResult)7 Master (org.neo4j.kernel.ha.com.master.Master)7 RequestMonitor (org.neo4j.com.monitor.RequestMonitor)6 ReadableClosablePositionAwareChannel (org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel)6 String.format (java.lang.String.format)5 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)4