Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Some more work

  • Loading branch information...
commit 4ff7220bf72c610b6c3914d2df7a6dd7b6145564 1 parent 7b4b367
@tinwelint authored
View
6 ha/src/main/java/org/neo4j/kernel/haonefive/HaIdGeneratorFactory.java
@@ -39,10 +39,12 @@
private final Map<IdType, HaIdGenerator> generators =
new EnumMap<IdType, HaIdGenerator>( IdType.class );
private final IdGeneratorFactory localFactory = new DefaultIdGeneratorFactory();
+ private final int serverId;
- public HaIdGeneratorFactory( HaServiceSupplier stuff )
+ public HaIdGeneratorFactory( HaServiceSupplier stuff, int serverId )
{
this.stuff = stuff;
+ this.serverId = serverId;
}
@Override
@@ -95,7 +97,7 @@ public void masterChanged( Master master, int masterServerId )
public void masterChanged( Master master, int masterServerId )
{
- boolean isBecomingMaster = masterServerId == stuff.getServerId();
+ boolean isBecomingMaster = masterServerId == serverId;
if ( isBecomingMaster )
{
View
346 ha/src/main/java/org/neo4j/kernel/haonefive/HaOneFiveGraphDb.java
@@ -19,265 +19,151 @@
*/
package org.neo4j.kernel.haonefive;
-import java.util.Collection;
-import java.util.HashMap;
+import java.io.IOException;
import java.util.Map;
-import javax.transaction.TransactionManager;
+import javax.transaction.Transaction;
-import org.neo4j.backup.OnlineBackupSettings;
-import org.neo4j.graphdb.Node;
-import org.neo4j.graphdb.Relationship;
-import org.neo4j.graphdb.RelationshipType;
-import org.neo4j.graphdb.Transaction;
-import org.neo4j.graphdb.event.KernelEventHandler;
-import org.neo4j.graphdb.event.TransactionEventHandler;
-import org.neo4j.graphdb.factory.GraphDatabaseSettings;
-import org.neo4j.graphdb.index.IndexManager;
-import org.neo4j.kernel.GraphDatabaseAPI;
+import org.neo4j.com.MasterUtil;
+import org.neo4j.com.Response;
+import org.neo4j.com.SlaveContext;
+import org.neo4j.graphdb.index.IndexProvider;
+import org.neo4j.helpers.Service;
+import org.neo4j.kernel.AbstractGraphDatabase;
import org.neo4j.kernel.IdGeneratorFactory;
-import org.neo4j.kernel.KernelData;
-import org.neo4j.kernel.TransactionBuilder;
+import org.neo4j.kernel.KernelExtension;
import org.neo4j.kernel.configuration.Config;
-import org.neo4j.kernel.configuration.ConfigurationDefaults;
-import org.neo4j.kernel.guard.Guard;
import org.neo4j.kernel.ha.HaSettings;
import org.neo4j.kernel.ha.Master;
-import org.neo4j.kernel.impl.core.KernelPanicEventGenerator;
-import org.neo4j.kernel.impl.core.LockReleaser;
-import org.neo4j.kernel.impl.core.NodeManager;
-import org.neo4j.kernel.impl.core.RelationshipTypeHolder;
-import org.neo4j.kernel.impl.nioneo.store.StoreId;
-import org.neo4j.kernel.impl.persistence.PersistenceSource;
+import org.neo4j.kernel.impl.cache.CacheProvider;
+import org.neo4j.kernel.impl.core.RelationshipTypeCreator;
import org.neo4j.kernel.impl.transaction.LockManager;
-import org.neo4j.kernel.impl.transaction.XaDataSourceManager;
-import org.neo4j.kernel.impl.util.StringLogger;
-import org.neo4j.kernel.info.DiagnosticsManager;
+import org.neo4j.kernel.impl.transaction.TxHook;
+import org.neo4j.kernel.impl.transaction.xaframework.TxIdGenerator;
+import org.neo4j.kernel.impl.transaction.xaframework.XaDataSource;
-public class HaOneFiveGraphDb implements GraphDatabaseAPI, MasterChangeListener
+public class HaOneFiveGraphDb extends AbstractGraphDatabase implements MasterChangeListener
{
- private final Config config;
- private volatile InternalHaOneFiveGraphDb delegate;
- private HashMap<String, String> params;
+ private final HaServiceSupplier stuff;
+ private final int serverId;
+ private volatile long sessionTimestamp;
- public HaOneFiveGraphDb( String storeDir, Map<String, String> inputParams )
+ private volatile Master master;
+ private volatile int masterServerId;
+
+ public HaOneFiveGraphDb( String storeDir, Map<String, String> params )
{
- config = createConfig( inputParams );
+ super( storeDir, params, Service.load( IndexProvider.class ),
+ Service.load( KernelExtension.class ), Service.load( CacheProvider.class ) );
- // Here we're in a state awaiting a call from master change listener...
- }
-
- private Config createConfig( Map<String, String> inputParams )
- {
- HashMap<String, String> params = new HashMap<String, String>( inputParams );
- new ConfigurationDefaults( GraphDatabaseSettings.class, HaSettings.class, OnlineBackupSettings.class ).apply( params );
- this.params = params;
- return new Config( params );
- }
-
- private InternalHaOneFiveGraphDb delegate()
- {
- // TODO if delegate not available wait until it's available
- return delegate;
- }
-
- public void masterChanged( Master master, int masterServerId )
- {
- if ( delegate == null )
- {
- // TODO Copy store if needed
- // TODO Check consistency with master
- delegate = new InternalHaOneFiveGraphDb( getStoreDir(), params, master, masterServerId );
- }
- else
+ serverId = new Config( params ).getInteger( HaSettings.server_id );
+
+ stuff = new HaServiceSupplier()
{
- // TODO Check consistency with master
- delegate.masterChanged( master, masterServerId );
- }
- }
-
- public void shutdown()
- {
- delegate().shutdown();
- }
-
- public final String getStoreDir()
- {
- return delegate().getStoreDir();
- }
-
- public StoreId getStoreId()
- {
- return delegate().getStoreId();
- }
-
- public Transaction beginTx()
- {
- return delegate().beginTx();
- }
-
- public boolean transactionRunning()
- {
- return delegate().transactionRunning();
- }
-
- public final <T> T getManagementBean( Class<T> type )
- {
- return delegate().getManagementBean( type );
- }
-
- public final <T> T getSingleManagementBean( Class<T> type )
- {
- return delegate().getSingleManagementBean( type );
- }
-
- public String toString()
- {
- return delegate().toString();
- }
-
- public Iterable<Node> getAllNodes()
- {
- return delegate().getAllNodes();
- }
-
- public Iterable<RelationshipType> getRelationshipTypes()
- {
- return delegate().getRelationshipTypes();
- }
-
- public KernelEventHandler registerKernelEventHandler( KernelEventHandler handler )
- {
- return delegate().registerKernelEventHandler( handler );
- }
-
- public <T> TransactionEventHandler<T> registerTransactionEventHandler( TransactionEventHandler<T> handler )
- {
- return delegate().registerTransactionEventHandler( handler );
- }
-
- public KernelEventHandler unregisterKernelEventHandler( KernelEventHandler handler )
- {
- return delegate().unregisterKernelEventHandler( handler );
- }
-
- public <T> TransactionEventHandler<T> unregisterTransactionEventHandler( TransactionEventHandler<T> handler )
- {
- return delegate().unregisterTransactionEventHandler( handler );
- }
-
- public Node createNode()
- {
- return delegate().createNode();
- }
-
- public Node getNodeById( long id )
- {
- return delegate().getNodeById( id );
- }
-
- public Relationship getRelationshipById( long id )
- {
- return delegate().getRelationshipById( id );
- }
-
- public Node getReferenceNode()
- {
- return delegate().getReferenceNode();
- }
-
- public TransactionBuilder tx()
- {
- return delegate().tx();
- }
-
- public Guard getGuard()
- {
- return delegate().getGuard();
- }
-
- public <T> Collection<T> getManagementBeans( Class<T> beanClass )
- {
- return delegate().getManagementBeans( beanClass );
- }
-
- public KernelData getKernelData()
- {
- return delegate().getKernelData();
- }
-
- public IndexManager index()
- {
- return delegate().index();
- }
-
- public Config getConfig()
- {
- return delegate().getConfig();
- }
-
- public NodeManager getNodeManager()
- {
- return delegate().getNodeManager();
- }
-
- public LockReleaser getLockReleaser()
- {
- return delegate().getLockReleaser();
- }
-
- public LockManager getLockManager()
- {
- return delegate().getLockManager();
- }
-
- public XaDataSourceManager getXaDataSourceManager()
- {
- return delegate().getXaDataSourceManager();
- }
-
- public TransactionManager getTxManager()
- {
- return delegate().getTxManager();
- }
-
- public RelationshipTypeHolder getRelationshipTypeHolder()
- {
- return delegate().getRelationshipTypeHolder();
+ @Override
+ public void receive( Response<?> response )
+ {
+ try
+ {
+ MasterUtil.applyReceivedTransactions( response, HaOneFiveGraphDb.this, MasterUtil.NO_ACTION );
+ sessionTimestamp = System.currentTimeMillis();
+ }
+ catch ( IOException e )
+ {
+ throw new RuntimeException( e );
+ }
+ }
+
+ @Override
+ public boolean hasAnyLocks( Transaction tx )
+ {
+ return lockReleaser.hasLocks( tx );
+ }
+
+ @Override
+ public SlaveContext getSlaveContext()
+ {
+ return getSlaveContext( txManager.getEventIdentifier() );
+ }
+
+ @Override
+ public SlaveContext getSlaveContext( int identifier, XaDataSource dataSource )
+ {
+ return MasterUtil.getSlaveContext( dataSource, sessionTimestamp, serverId, identifier );
+ }
+
+ @Override
+ public SlaveContext getSlaveContext( int eventIdentifier )
+ {
+ return MasterUtil.getSlaveContext( xaDataSourceManager, sessionTimestamp, serverId, eventIdentifier );
+ }
+
+ @Override
+ public int getMasterServerId()
+ {
+ return masterServerId;
+ }
+
+ @Override
+ public Master getMaster()
+ {
+ if ( master == null )
+ {
+ // TODO Block in wait of new master?
+ }
+ return master;
+ }
+ };
+
+ run();
}
-
- public IdGeneratorFactory getIdGeneratorFactory()
+
+ @Override
+ protected TxHook createTxHook()
{
- return delegate().getIdGeneratorFactory();
+ return new HaTxHook( stuff );
}
-
- public DiagnosticsManager getDiagnosticsManager()
+
+ @Override
+ protected TxIdGenerator createTxIdGenerator()
{
- return delegate().getDiagnosticsManager();
+ return new HaTxIdGenerator( stuff, serverId );
}
-
- public PersistenceSource getPersistenceSource()
+
+ @Override
+ protected IdGeneratorFactory createIdGeneratorFactory()
{
- return delegate().getPersistenceSource();
+ return new HaIdGeneratorFactory( stuff, serverId );
}
-
- public final StringLogger getMessageLog()
+
+ @Override
+ protected LockManager createLockManager()
{
- return delegate().getMessageLog();
+ return new HaLockManager( stuff, ragManager );
}
-
- public KernelPanicEventGenerator getKernelPanicGenerator()
+
+ @Override
+ protected RelationshipTypeCreator createRelationshipTypeCreator()
{
- return delegate().getKernelPanicGenerator();
+ return new HaRelationshipTypeCreator( stuff );
}
- public boolean equals( Object o )
+ @Override
+ public void newMasterElected( String masterId, int masterServerId )
{
- return delegate().equals( o );
+ // TODO Block incoming transactions and rollback active ones or something.
+
+ boolean iAmToBecomeMaster = masterServerId == this.serverId;
+ this.master = iAmToBecomeMaster ?
+ new LoopbackMaster( storeId, xaDataSourceManager, relationshipTypeCreator, null, persistenceSource, persistenceManager, relationshipTypeHolder ) :
+ null; // TODO instantiate master from masterId
+ this.masterServerId = masterServerId;
+ ((HaIdGeneratorFactory) idGeneratorFactory).masterChanged( master, masterServerId );
}
- public int hashCode()
+ @Override
+ public void masterChanged( String masterId )
{
- return delegate().hashCode();
+ // TODO Remove blockade
}
}
View
6 ha/src/main/java/org/neo4j/kernel/haonefive/HaServiceSupplier.java
@@ -38,15 +38,13 @@
SlaveContext getSlaveContext( int identifier, XaDataSource dataSource );
+ SlaveContext getSlaveContext();
+
void receive( Response<?> response );
boolean hasAnyLocks( Transaction tx );
int getMasterServerId();
- int getServerId();
-
- SlaveContext getSlaveContext();
-
// ...
}
View
6 ha/src/main/java/org/neo4j/kernel/haonefive/HaTxIdGenerator.java
@@ -31,10 +31,12 @@
public class HaTxIdGenerator implements TxIdGenerator
{
private final HaServiceSupplier stuff;
+ private final int serverId;
- public HaTxIdGenerator( HaServiceSupplier stuff )
+ public HaTxIdGenerator( HaServiceSupplier stuff, int serverId )
{
this.stuff = stuff;
+ this.serverId = serverId;
}
@Override
@@ -81,6 +83,6 @@ public int getCurrentMasterId()
@Override
public int getMyId()
{
- return stuff.getServerId();
+ return serverId;
}
}
View
167 ha/src/main/java/org/neo4j/kernel/haonefive/InternalHaOneFiveGraphDb.java
@@ -1,167 +0,0 @@
-/**
- * Copyright (c) 2002-2012 "Neo Technology,"
- * Network Engine for Objects in Lund AB [http://neotechnology.com]
- *
- * This file is part of Neo4j.
- *
- * Neo4j is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.neo4j.kernel.haonefive;
-
-import java.io.IOException;
-import java.util.Map;
-
-import javax.transaction.Transaction;
-
-import org.neo4j.com.MasterUtil;
-import org.neo4j.com.Response;
-import org.neo4j.com.SlaveContext;
-import org.neo4j.graphdb.index.IndexProvider;
-import org.neo4j.helpers.Service;
-import org.neo4j.kernel.AbstractGraphDatabase;
-import org.neo4j.kernel.IdGeneratorFactory;
-import org.neo4j.kernel.KernelExtension;
-import org.neo4j.kernel.configuration.Config;
-import org.neo4j.kernel.ha.HaSettings;
-import org.neo4j.kernel.ha.Master;
-import org.neo4j.kernel.impl.cache.CacheProvider;
-import org.neo4j.kernel.impl.core.RelationshipTypeCreator;
-import org.neo4j.kernel.impl.transaction.LockManager;
-import org.neo4j.kernel.impl.transaction.TxHook;
-import org.neo4j.kernel.impl.transaction.xaframework.TxIdGenerator;
-import org.neo4j.kernel.impl.transaction.xaframework.XaDataSource;
-
-public class InternalHaOneFiveGraphDb extends AbstractGraphDatabase
-{
- private final HaServiceSupplier stuff;
- private final int serverId;
- private volatile long sessionTimestamp;
-
- private volatile Master master;
- private volatile int masterServerId;
-
- public InternalHaOneFiveGraphDb( String storeDir, Map<String, String> params, Master initialMaster, int initialMasterServerId )
- {
- super( storeDir, params, Service.load( IndexProvider.class ),
- Service.load( KernelExtension.class ), Service.load( CacheProvider.class ) );
-
- serverId = new Config( params ).getInteger( HaSettings.server_id );
-
- stuff = new HaServiceSupplier()
- {
- @Override
- public void receive( Response<?> response )
- {
- try
- {
- MasterUtil.applyReceivedTransactions( response, InternalHaOneFiveGraphDb.this, MasterUtil.NO_ACTION );
- sessionTimestamp = System.currentTimeMillis();
- }
- catch ( IOException e )
- {
- throw new RuntimeException( e );
- }
- }
-
- @Override
- public boolean hasAnyLocks( Transaction tx )
- {
- return lockReleaser.hasLocks( tx );
- }
-
- @Override
- public SlaveContext getSlaveContext()
- {
- return getSlaveContext( txManager.getEventIdentifier() );
- }
-
- @Override
- public SlaveContext getSlaveContext( int identifier, XaDataSource dataSource )
- {
- return MasterUtil.getSlaveContext( dataSource, sessionTimestamp, serverId, identifier );
- }
-
- @Override
- public SlaveContext getSlaveContext( int eventIdentifier )
- {
- return MasterUtil.getSlaveContext( xaDataSourceManager, sessionTimestamp, serverId, eventIdentifier );
- }
-
- @Override
- public int getServerId()
- {
- return serverId;
- }
-
- @Override
- public int getMasterServerId()
- {
- return masterServerId;
- }
-
- @Override
- public Master getMaster()
- {
- if ( master == null )
- {
- // TODO Block in wait of new master?
- }
- return master;
- }
- };
-
- run();
- }
-
- @Override
- protected TxHook createTxHook()
- {
- return new HaTxHook( stuff );
- }
-
- @Override
- protected TxIdGenerator createTxIdGenerator()
- {
- return new HaTxIdGenerator( stuff );
- }
-
- @Override
- protected IdGeneratorFactory createIdGeneratorFactory()
- {
- return new HaIdGeneratorFactory( stuff );
- }
-
- @Override
- protected LockManager createLockManager()
- {
- return new HaLockManager( stuff, ragManager );
- }
-
- @Override
- protected RelationshipTypeCreator createRelationshipTypeCreator()
- {
- return new HaRelationshipTypeCreator( stuff );
- }
-
- public void masterChanged( Master master, int masterServerId )
- {
- // TODO Block transactions and rollback active ones or something.
-
- ((HaIdGeneratorFactory) idGeneratorFactory).masterChanged( master, masterServerId );
- this.masterServerId = masterServerId;
- this.master = master;
-
- // TODO Remove blockade
- }
-}
View
14 ha/src/main/java/org/neo4j/kernel/haonefive/MasterChangeListener.java
@@ -19,9 +19,17 @@
*/
package org.neo4j.kernel.haonefive;
-import org.neo4j.kernel.ha.Master;
-
public interface MasterChangeListener
{
- void masterChanged( Master master, int masterServerId );
+ /**
+ * Called when new master has been elected. The new master may not be available a.t.m.
+ * A call to {@link #masterChanged(String)} will confirm that the master given in
+ * the most recent {@link #newMasterElected(String, int)} call is up and running as master.
+ *
+ * @param masterId the connection information to the master.
+ * @param masterServerId the server id of the master (TODO remove)
+ */
+ void newMasterElected( String masterId, int masterServerId /*Here because other legacy HA code relies on it*/ );
+
+ void masterChanged( String masterId );
}
View
39 ha/src/test/java/org/neo4j/kernel/haonefive/TestCreateNode.java
@@ -0,0 +1,39 @@
+package org.neo4j.kernel.haonefive;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.neo4j.graphdb.Transaction;
+import org.neo4j.helpers.collection.MapUtil;
+import org.neo4j.test.TargetDirectory;
+
+public class TestCreateNode
+{
+ private TargetDirectory PATH;
+
+ @Before
+ public void before() throws Exception
+ {
+ PATH = TargetDirectory.forTest( getClass() );
+ }
+
+ @After
+ public void after() throws Exception
+ {
+
+ }
+
+ @Test
+ public void createHighlyAvailableNode() throws Exception
+ {
+ HaOneFiveGraphDb db1 = new HaOneFiveGraphDb( PATH.directory( "0", true ).getAbsolutePath(),
+ MapUtil.stringMap( "ha.server_id", "0" ) );
+ HaOneFiveGraphDb db2 = new HaOneFiveGraphDb( PATH.directory( "1", true ).getAbsolutePath(),
+ MapUtil.stringMap( "ha.server_id", "1" ) );
+
+ Transaction tx = db1.beginTx();
+ db1.createNode();
+ tx.success();
+ tx.finish();
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.