Search in sources :

Example 1 with BranchedDataException

use of org.neo4j.kernel.ha.BranchedDataException in project neo4j by neo4j.

the class SwitchToSlaveBranchThenCopyTest method shouldHandleBranchedStoreWhenHandshakeFailsWithBranchedDataException.

@Test
public void shouldHandleBranchedStoreWhenHandshakeFailsWithBranchedDataException() throws Throwable {
    // Given
    SwitchToSlaveBranchThenCopy switchToSlave = newSwitchToSlaveSpy();
    URI masterUri = new URI("cluster://localhost?serverId=1");
    URI me = new URI("cluster://localhost?serverId=2");
    MasterClient masterClient = mock(MasterClient.class);
    when(masterClient.handshake(anyLong(), any(StoreId.class))).thenThrow(new BranchedDataException(""));
    TransactionIdStore transactionIdStore = mock(TransactionIdStore.class);
    when(transactionIdStore.getLastCommittedTransaction()).thenReturn(new TransactionId(42, 42, 42));
    when(transactionIdStore.getLastCommittedTransactionId()).thenReturn(TransactionIdStore.BASE_TX_ID);
    // When
    try {
        switchToSlave.checkDataConsistency(masterClient, transactionIdStore, storeId, masterUri, me, CancellationRequest.NEVER_CANCELLED);
        fail("Should have thrown " + BranchedDataException.class.getSimpleName() + " exception");
    } catch (BranchedDataException e) {
    // good we got the expected exception
    }
    // Then
    verify(switchToSlave).stopServicesAndHandleBranchedStore(any(BranchedDataPolicy.class));
}
Also used : TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) StoreId(org.neo4j.kernel.impl.store.StoreId) MasterClient(org.neo4j.kernel.ha.com.slave.MasterClient) BranchedDataPolicy(org.neo4j.kernel.ha.BranchedDataPolicy) URI(java.net.URI) BranchedDataException(org.neo4j.kernel.ha.BranchedDataException) TransactionId(org.neo4j.kernel.impl.store.TransactionId) Test(org.junit.Test)

Example 2 with BranchedDataException

use of org.neo4j.kernel.ha.BranchedDataException in project neo4j by neo4j.

the class SwitchToSlaveCopyThenBranchTest method shouldNotBranchStoreUnlessWeHaveCopiedDownAReplacement.

@Test
public void shouldNotBranchStoreUnlessWeHaveCopiedDownAReplacement() throws Throwable {
    // Given
    StoreCopyClient storeCopyClient = mock(StoreCopyClient.class);
    doAnswer(invocation -> {
        MoveAfterCopy moveAfterCopy = invocation.getArgumentAt(2, MoveAfterCopy.class);
        moveAfterCopy.move(Stream.empty(), new File(""), new File(""));
        return null;
    }).when(storeCopyClient).copyStore(any(StoreCopyClient.StoreCopyRequester.class), any(CancellationRequest.class), any(MoveAfterCopy.class));
    PageCache pageCacheMock = mock(PageCache.class);
    PagedFile pagedFileMock = mock(PagedFile.class);
    when(pagedFileMock.getLastPageId()).thenReturn(1L);
    when(pageCacheMock.map(any(File.class), anyInt())).thenReturn(pagedFileMock);
    SwitchToSlaveCopyThenBranch switchToSlave = newSwitchToSlaveSpy(pageCacheMock, storeCopyClient);
    URI masterUri = new URI("cluster://localhost?serverId=1");
    URI me = new URI("cluster://localhost?serverId=2");
    CancellationRequest cancellationRequest = CancellationRequest.NEVER_CANCELLED;
    MasterClient masterClient = mock(MasterClient.class);
    when(masterClient.handshake(anyLong(), any(StoreId.class))).thenThrow(new BranchedDataException(""));
    TransactionIdStore transactionIdStore = mock(TransactionIdStore.class);
    when(transactionIdStore.getLastCommittedTransaction()).thenReturn(new TransactionId(42, 42, 42));
    when(transactionIdStore.getLastCommittedTransactionId()).thenReturn(TransactionIdStore.BASE_TX_ID);
    // When
    BranchedDataPolicy branchPolicy = mock(BranchedDataPolicy.class);
    switchToSlave.stopServicesAndHandleBranchedStore(branchPolicy, masterUri, me, cancellationRequest);
    // Then
    InOrder inOrder = Mockito.inOrder(storeCopyClient, branchPolicy);
    inOrder.verify(storeCopyClient).copyStore(any(StoreCopyClient.StoreCopyRequester.class), any(CancellationRequest.class), any(MoveAfterCopy.class));
    inOrder.verify(branchPolicy).handle(new File(""), pageCacheMock, NullLogService.getInstance());
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) InOrder(org.mockito.InOrder) MasterClient(org.neo4j.kernel.ha.com.slave.MasterClient) URI(java.net.URI) TransactionId(org.neo4j.kernel.impl.store.TransactionId) StoreId(org.neo4j.kernel.impl.store.StoreId) StoreCopyClient(org.neo4j.com.storecopy.StoreCopyClient) BranchedDataPolicy(org.neo4j.kernel.ha.BranchedDataPolicy) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) BranchedDataException(org.neo4j.kernel.ha.BranchedDataException) MoveAfterCopy(org.neo4j.com.storecopy.MoveAfterCopy) CancellationRequest(org.neo4j.helpers.CancellationRequest) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 3 with BranchedDataException

use of org.neo4j.kernel.ha.BranchedDataException in project neo4j by neo4j.

the class SwitchToSlaveCopyThenBranchTest method shouldHandleBranchedStoreWhenHandshakeFailsWithBranchedDataException.

@Test
public void shouldHandleBranchedStoreWhenHandshakeFailsWithBranchedDataException() throws Throwable {
    // Given
    SwitchToSlaveCopyThenBranch switchToSlave = newSwitchToSlaveSpy();
    URI masterUri = new URI("cluster://localhost?serverId=1");
    URI me = new URI("cluster://localhost?serverId=2");
    MasterClient masterClient = mock(MasterClient.class);
    when(masterClient.handshake(anyLong(), any(StoreId.class))).thenThrow(new BranchedDataException(""));
    TransactionIdStore transactionIdStore = mock(TransactionIdStore.class);
    when(transactionIdStore.getLastCommittedTransaction()).thenReturn(new TransactionId(42, 42, 42));
    when(transactionIdStore.getLastCommittedTransactionId()).thenReturn(TransactionIdStore.BASE_TX_ID);
    // When
    try {
        switchToSlave.checkDataConsistency(masterClient, transactionIdStore, storeId, masterUri, me, CancellationRequest.NEVER_CANCELLED);
        fail("Should have thrown " + BranchedDataException.class.getSimpleName() + " exception");
    } catch (BranchedDataException e) {
    // good we got the expected exception
    }
    // Then
    verify(switchToSlave).stopServicesAndHandleBranchedStore(any(BranchedDataPolicy.class), any(URI.class), any(URI.class), any(CancellationRequest.class));
}
Also used : TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) StoreId(org.neo4j.kernel.impl.store.StoreId) MasterClient(org.neo4j.kernel.ha.com.slave.MasterClient) BranchedDataPolicy(org.neo4j.kernel.ha.BranchedDataPolicy) URI(java.net.URI) BranchedDataException(org.neo4j.kernel.ha.BranchedDataException) CancellationRequest(org.neo4j.helpers.CancellationRequest) TransactionId(org.neo4j.kernel.impl.store.TransactionId) Test(org.junit.Test)

Example 4 with BranchedDataException

use of org.neo4j.kernel.ha.BranchedDataException in project graphdb by neo4j-attic.

the class HighlyAvailableGraphDatabase method tryToEnsureIAmNotABrokenMachine.

private void tryToEnsureIAmNotABrokenMachine(Pair<Master, Machine> master) {
    if (master.other().getMachineId() == machineId) {
        return;
    } else if (master.first() == null) {
        // Temporarily disconnected from ZK
        RuntimeException cause = new RuntimeException("Unable to get master from ZK");
        shutdown(cause, false);
        throw cause;
    }
    XaDataSource nioneoDataSource = getConfig().getTxModule().getXaDataSourceManager().getXaDataSource(Config.DEFAULT_DATA_SOURCE_NAME);
    long myLastCommittedTx = nioneoDataSource.getLastCommittedTxId();
    long highestCommonTxId = Math.min(myLastCommittedTx, master.other().getLastCommittedTxId());
    int masterForMyHighestCommonTxId = -1;
    try {
        masterForMyHighestCommonTxId = nioneoDataSource.getMasterForCommittedTx(highestCommonTxId);
        ;
    } catch (IOException e) {
        // This is quite dangerous to just catch here... but the special case is
        // where this db was just now copied from the master where there's data,
        // but no logical logs were transfered and hence no masterId info is here
        msgLog.logMessage("Couldn't get master ID for txId " + highestCommonTxId + ". It may be that a log file is missing due to the db being copied from master?", e);
        return;
    }
    int masterForMastersHighestCommonTxId = master.first().getMasterIdForCommittedTx(highestCommonTxId).response();
    // Compare those two, if equal -> good
    if (masterForMyHighestCommonTxId == masterForMastersHighestCommonTxId) {
        msgLog.logMessage("Master id for last committed tx ok with highestCommonTxId=" + highestCommonTxId + " with masterId=" + masterForMyHighestCommonTxId, true);
        return;
    } else {
        String msg = "Broken store, I (machineId:" + machineId + ") think machineId for txId (" + highestCommonTxId + ") is " + masterForMyHighestCommonTxId + ", but master (machineId:" + master.other().getMachineId() + ") says that it's " + masterForMastersHighestCommonTxId;
        msgLog.logMessage(msg, true);
        RuntimeException exception = new BranchedDataException(msg);
        shutdown(exception, false);
        throw exception;
    }
}
Also used : IOException(java.io.IOException) XaDataSource(org.neo4j.kernel.impl.transaction.xaframework.XaDataSource) BranchedDataException(org.neo4j.kernel.ha.BranchedDataException)

Example 5 with BranchedDataException

use of org.neo4j.kernel.ha.BranchedDataException in project neo4j by neo4j.

the class SwitchToSlave method checkMyStoreIdAndMastersStoreId.

void checkMyStoreIdAndMastersStoreId(StoreId myStoreId, URI masterUri, DependencyResolver resolver) {
    ClusterMembers clusterMembers = resolver.resolveDependency(ClusterMembers.class);
    InstanceId serverId = HighAvailabilityModeSwitcher.getServerId(masterUri);
    Iterable<ClusterMember> members = clusterMembers.getMembers();
    ClusterMember master = firstOrNull(filter(hasInstanceId(serverId), members));
    if (master == null) {
        throw new IllegalStateException("Cannot find the master among " + members + " with master serverId=" + serverId + " and uri=" + masterUri);
    }
    StoreId masterStoreId = master.getStoreId();
    if (!myStoreId.equals(masterStoreId)) {
        throw new MismatchingStoreIdException(myStoreId, master.getStoreId());
    } else if (!myStoreId.equalsByUpgradeId(master.getStoreId())) {
        throw new BranchedDataException("My store with " + myStoreId + " was updated independently from " + "master's store " + masterStoreId);
    }
}
Also used : ClusterMember(org.neo4j.kernel.ha.cluster.member.ClusterMember) StoreId(org.neo4j.kernel.impl.store.StoreId) InstanceId(org.neo4j.cluster.InstanceId) ClusterMembers.hasInstanceId(org.neo4j.kernel.ha.cluster.member.ClusterMembers.hasInstanceId) MismatchingStoreIdException(org.neo4j.kernel.impl.store.MismatchingStoreIdException) ClusterMembers(org.neo4j.kernel.ha.cluster.member.ClusterMembers) BranchedDataException(org.neo4j.kernel.ha.BranchedDataException)

Aggregations

BranchedDataException (org.neo4j.kernel.ha.BranchedDataException)6 StoreId (org.neo4j.kernel.impl.store.StoreId)4 TransactionId (org.neo4j.kernel.impl.store.TransactionId)4 URI (java.net.URI)3 Test (org.junit.Test)3 BranchedDataPolicy (org.neo4j.kernel.ha.BranchedDataPolicy)3 MasterClient (org.neo4j.kernel.ha.com.slave.MasterClient)3 TransactionIdStore (org.neo4j.kernel.impl.transaction.log.TransactionIdStore)3 CancellationRequest (org.neo4j.helpers.CancellationRequest)2 File (java.io.File)1 IOException (java.io.IOException)1 InOrder (org.mockito.InOrder)1 InstanceId (org.neo4j.cluster.InstanceId)1 MoveAfterCopy (org.neo4j.com.storecopy.MoveAfterCopy)1 StoreCopyClient (org.neo4j.com.storecopy.StoreCopyClient)1 PageCache (org.neo4j.io.pagecache.PageCache)1 PagedFile (org.neo4j.io.pagecache.PagedFile)1 StoreOutOfDateException (org.neo4j.kernel.ha.StoreOutOfDateException)1 ClusterMember (org.neo4j.kernel.ha.cluster.member.ClusterMember)1 ClusterMembers (org.neo4j.kernel.ha.cluster.member.ClusterMembers)1