use of org.neo4j.graphdb.DependencyResolver in project neo4j by neo4j.
the class IndexLookupTest method setUp.
@BeforeClass
public static void setUp() {
api = dbRule.getGraphDatabaseAPI();
String notUsedIndexPropKey = "notUsed";
String usedIndexPropKey = "used";
Label usedLabel = Label.label("UsedLabel");
Label notUsedLabel = Label.label("NotUsedLabel");
try (Transaction transaction = api.beginTx()) {
api.schema().indexFor(usedLabel).on(usedIndexPropKey).create();
transaction.success();
}
try (Transaction transaction = api.beginTx()) {
api.schema().awaitIndexesOnline(10, TimeUnit.SECONDS);
indexedNodePropertyValue = "value1";
notIndexedNodePropertyValue = "value2";
Node nodeA = api.createNode(usedLabel);
nodeA.setProperty(usedIndexPropKey, indexedNodePropertyValue);
nodeA.setProperty(notUsedIndexPropKey, notIndexedNodePropertyValue);
indexedNode = nodeA.getId();
Node nodeB = api.createNode(notUsedLabel);
nodeB.setProperty(usedIndexPropKey, notIndexedNodePropertyValue);
nodeB.setProperty(notUsedIndexPropKey, indexedNodePropertyValue);
notIndexedNode = nodeB.getId();
transaction.success();
}
DependencyResolver resolver = api.getDependencyResolver();
NeoStores neoStores = resolver.resolveDependency(RecordStorageEngine.class).testAccessNeoStores();
SchemaStore schemaStore = neoStores.getSchemaStore();
SchemaIndexProvider schemaIndexProvider = resolver.resolveDependency(SchemaIndexProvider.class);
indexLookup = new IndexLookup(schemaStore, schemaIndexProvider);
LabelTokenStore labelTokenStore = neoStores.getLabelTokenStore();
notUsedLabelId = findTokenFor(labelTokenStore, notUsedLabel.name()).id();
usedLabelId = findTokenFor(labelTokenStore, usedLabel.name()).id();
PropertyKeyTokenStore propertyKeyTokenStore = neoStores.getPropertyKeyTokenStore();
notUsedPropertyId = findTokenFor(propertyKeyTokenStore, notUsedIndexPropKey).id();
usedPropertyId = findTokenFor(propertyKeyTokenStore, usedIndexPropKey).id();
}
use of org.neo4j.graphdb.DependencyResolver in project neo4j by neo4j.
the class ApplyTransactionsCommand method applyTransactions.
private long applyTransactions(File fromPath, GraphDatabaseAPI toDb, long fromTxExclusive, long toTxInclusive, PrintStream out) throws IOException, TransactionFailureException {
DependencyResolver resolver = toDb.getDependencyResolver();
TransactionRepresentationCommitProcess commitProcess = new TransactionRepresentationCommitProcess(resolver.resolveDependency(TransactionAppender.class), resolver.resolveDependency(StorageEngine.class));
LifeSupport life = new LifeSupport();
try (DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
PageCache pageCache = StandalonePageCacheFactory.createPageCache(fileSystem)) {
LogicalTransactionStore source = life.add(new ReadOnlyTransactionStore(pageCache, fileSystem, fromPath, new Monitors()));
life.start();
long lastAppliedTx = fromTxExclusive;
// Some progress if there are more than a couple of transactions to apply
ProgressListener progress = toTxInclusive - fromTxExclusive >= 100 ? textual(out).singlePart("Application progress", toTxInclusive - fromTxExclusive) : ProgressListener.NONE;
try (IOCursor<CommittedTransactionRepresentation> cursor = source.getTransactions(fromTxExclusive + 1)) {
while (cursor.next()) {
CommittedTransactionRepresentation transaction = cursor.get();
TransactionRepresentation transactionRepresentation = transaction.getTransactionRepresentation();
try {
commitProcess.commit(new TransactionToApply(transactionRepresentation), NULL, EXTERNAL);
progress.add(1);
} catch (final Throwable e) {
System.err.println("ERROR applying transaction " + transaction.getCommitEntry().getTxId());
throw e;
}
lastAppliedTx = transaction.getCommitEntry().getTxId();
if (lastAppliedTx == toTxInclusive) {
break;
}
}
}
return lastAppliedTx;
} finally {
life.shutdown();
}
}
use of org.neo4j.graphdb.DependencyResolver in project neo4j by neo4j.
the class HighAvailabilityMemberStateMachineTest method whenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient.
@Test
public void whenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient() throws Throwable {
InstanceId me = new InstanceId(1);
StoreId storeId = newStoreIdForCurrentVersion();
HighAvailabilityMemberContext context = mock(HighAvailabilityMemberContext.class);
when(context.getMyId()).thenReturn(me);
AvailabilityGuard guard = mock(AvailabilityGuard.class);
ObservedClusterMembers members = mock(ObservedClusterMembers.class);
ClusterMember masterMember = mock(ClusterMember.class);
when(masterMember.getHARole()).thenReturn("master");
when(masterMember.hasRole("master")).thenReturn(true);
when(masterMember.getInstanceId()).thenReturn(new InstanceId(2));
when(masterMember.getStoreId()).thenReturn(storeId);
ClusterMember self = new ClusterMember(me);
when(members.getMembers()).thenReturn(Arrays.asList(self, masterMember));
when(members.getCurrentMember()).thenReturn(self);
DependencyResolver dependencyResolver = mock(DependencyResolver.class);
FileSystemAbstraction fs = mock(FileSystemAbstraction.class);
when(fs.fileExists(any(File.class))).thenReturn(true);
when(dependencyResolver.resolveDependency(FileSystemAbstraction.class)).thenReturn(fs);
when(dependencyResolver.resolveDependency(Monitors.class)).thenReturn(new Monitors());
NeoStoreDataSource dataSource = mock(NeoStoreDataSource.class);
when(dataSource.getDependencyResolver()).thenReturn(dependencyResolver);
when(dataSource.getStoreId()).thenReturn(storeId);
when(dependencyResolver.resolveDependency(NeoStoreDataSource.class)).thenReturn(dataSource);
when(dependencyResolver.resolveDependency(TransactionIdStore.class)).thenReturn(new DeadSimpleTransactionIdStore());
when(dependencyResolver.resolveDependency(ObservedClusterMembers.class)).thenReturn(members);
UpdatePuller updatePuller = mock(UpdatePuller.class);
when(updatePuller.tryPullUpdates()).thenReturn(true);
when(dependencyResolver.resolveDependency(UpdatePuller.class)).thenReturn(updatePuller);
ClusterMemberAvailability clusterMemberAvailability = mock(ClusterMemberAvailability.class);
final TriggerableClusterMemberEvents events = new TriggerableClusterMemberEvents();
Election election = mock(Election.class);
HighAvailabilityMemberStateMachine stateMachine = new HighAvailabilityMemberStateMachine(context, guard, members, events, election, NullLogProvider.getInstance());
ClusterMembers clusterMembers = new ClusterMembers(members, stateMachine);
when(dependencyResolver.resolveDependency(ClusterMembers.class)).thenReturn(clusterMembers);
stateMachine.init();
stateMachine.start();
final DelegateInvocationHandler<Master> handler = new DelegateInvocationHandler<>(Master.class);
MasterClientResolver masterClientResolver = mock(MasterClientResolver.class);
MasterClient masterClient = mock(MasterClient.class);
when(masterClient.getProtocolVersion()).thenReturn(MasterClient214.PROTOCOL_VERSION);
when(masterClient.handshake(anyLong(), any(StoreId.class))).thenReturn(new Response<HandshakeResult>(new HandshakeResult(0, 42), storeId, mock(ResourceReleaser.class)) {
@Override
public void accept(Handler handler) throws IOException {
}
@Override
public boolean hasTransactionsToBeApplied() {
return false;
}
});
when(masterClient.toString()).thenReturn("TheExpectedMasterClient!");
when(masterClientResolver.instantiate(anyString(), anyInt(), anyString(), any(Monitors.class), any(StoreId.class), any(LifeSupport.class))).thenReturn(masterClient);
final CountDownLatch latch = new CountDownLatch(2);
final AtomicBoolean switchedSuccessfully = new AtomicBoolean();
SwitchToSlave.Monitor monitor = new SwitchToSlave.Monitor() {
@Override
public void switchToSlaveCompleted(boolean wasSuccessful) {
switchedSuccessfully.set(wasSuccessful);
latch.countDown();
}
};
Config config = Config.embeddedDefaults(Collections.singletonMap(ClusterSettings.server_id.name(), me.toString()));
TransactionStats transactionCounters = mock(TransactionStats.class);
when(transactionCounters.getNumberOfActiveTransactions()).thenReturn(0L);
PageCache pageCacheMock = mock(PageCache.class);
PagedFile pagedFileMock = mock(PagedFile.class);
when(pagedFileMock.getLastPageId()).thenReturn(1L);
when(pageCacheMock.map(any(File.class), anyInt())).thenReturn(pagedFileMock);
TransactionIdStore transactionIdStoreMock = mock(TransactionIdStore.class);
when(transactionIdStoreMock.getLastCommittedTransaction()).thenReturn(new TransactionId(0, 0, 0));
SwitchToSlaveCopyThenBranch switchToSlave = new SwitchToSlaveCopyThenBranch(new File(""), NullLogService.getInstance(), mock(FileSystemAbstraction.class), config, dependencyResolver, mock(HaIdGeneratorFactory.class), handler, mock(ClusterMemberAvailability.class), mock(RequestContextFactory.class), mock(PullerFactory.class, RETURNS_MOCKS), Iterables.empty(), masterClientResolver, monitor, new StoreCopyClient.Monitor.Adapter(), Suppliers.singleton(dataSource), Suppliers.singleton(transactionIdStoreMock), slave -> {
SlaveServer mock = mock(SlaveServer.class);
when(mock.getSocketAddress()).thenReturn(new InetSocketAddress("localhost", 123));
return mock;
}, updatePuller, pageCacheMock, mock(Monitors.class), transactionCounters);
ComponentSwitcherContainer switcherContainer = new ComponentSwitcherContainer();
HighAvailabilityModeSwitcher haModeSwitcher = new HighAvailabilityModeSwitcher(switchToSlave, mock(SwitchToMaster.class), election, clusterMemberAvailability, mock(ClusterClient.class), storeSupplierMock(), me, switcherContainer, neoStoreDataSourceSupplierMock(), NullLogService.getInstance());
haModeSwitcher.init();
haModeSwitcher.start();
haModeSwitcher.listeningAt(URI.create("http://localhost:12345"));
stateMachine.addHighAvailabilityMemberListener(haModeSwitcher);
final AtomicReference<Master> ref = new AtomicReference<>(null);
//noinspection unchecked
AbstractComponentSwitcher<Object> otherModeSwitcher = new AbstractComponentSwitcher<Object>(mock(DelegateInvocationHandler.class)) {
@Override
protected Object getSlaveImpl() {
Master master = handler.cement();
ref.set(master);
latch.countDown();
return null;
}
@Override
protected Object getMasterImpl() {
return null;
}
};
switcherContainer.add(otherModeSwitcher);
// When
events.switchToSlave(me);
// Then
latch.await();
assertTrue("mode switch failed", switchedSuccessfully.get());
Master actual = ref.get();
// let's test the toString()s since there are too many wrappers of proxies
assertEquals(masterClient.toString(), actual.toString());
stateMachine.stop();
stateMachine.shutdown();
haModeSwitcher.stop();
haModeSwitcher.shutdown();
}
use of org.neo4j.graphdb.DependencyResolver in project neo4j by neo4j.
the class NodeIdReuseStressIT method highestNodeId.
private static long highestNodeId(GraphDatabaseService db) {
DependencyResolver resolver = dependencyResolver(db);
NeoStores neoStores = resolver.resolveDependency(RecordStorageEngine.class).testAccessNeoStores();
NodeStore nodeStore = neoStores.getNodeStore();
return nodeStore.getHighestPossibleIdInUse();
}
use of org.neo4j.graphdb.DependencyResolver in project neo4j by neo4j.
the class UniqueConstraintCompatibility method resolveInternalDependency.
private <T> T resolveInternalDependency(Class<T> type) {
@SuppressWarnings("deprecation") GraphDatabaseAPI api = (GraphDatabaseAPI) db;
DependencyResolver resolver = api.getDependencyResolver();
return resolver.resolveDependency(type);
}
Aggregations