Skip to content

Commit

Permalink
Extract HA's IdReuseEligibility in a top level class
Browse files Browse the repository at this point in the history
  • Loading branch information
lutovich authored and burqen committed Jun 30, 2016
1 parent 3e68299 commit 5d91e08
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@
import org.neo4j.kernel.ha.com.slave.MasterClientResolver;
import org.neo4j.kernel.ha.com.slave.SlaveServer;
import org.neo4j.kernel.ha.id.HaIdGeneratorFactory;
import org.neo4j.kernel.ha.id.HaIdReuseEligibility;
import org.neo4j.kernel.ha.lock.LockManagerModeSwitcher;
import org.neo4j.kernel.ha.management.ClusterDatabaseInfoProvider;
import org.neo4j.kernel.ha.management.HighlyAvailableKernelData;
import org.neo4j.kernel.ha.transaction.CommitPusher;
import org.neo4j.kernel.ha.transaction.OnDiskLastTxIdGetter;
import org.neo4j.kernel.ha.transaction.TransactionPropagator;
import org.neo4j.kernel.impl.api.KernelTransactionsSnapshot;
import org.neo4j.kernel.impl.api.SchemaWriteGuard;
import org.neo4j.kernel.impl.api.TransactionCommitProcess;
import org.neo4j.kernel.impl.api.TransactionHeaderInformation;
Expand All @@ -138,7 +138,6 @@
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.StoreId;
import org.neo4j.kernel.impl.store.id.IdReuseEligibility;
import org.neo4j.kernel.impl.storemigration.UpgradeConfiguration;
import org.neo4j.kernel.impl.storemigration.UpgradeNotAllowedByDatabaseModeException;
import org.neo4j.kernel.impl.transaction.TransactionHeaderInformationFactory;
Expand Down Expand Up @@ -556,37 +555,7 @@ public void assertSchemaWritesAllowed() throws InvalidTransactionTypeKernelExcep

constraintSemantics = new EnterpriseConstraintSemantics();

// Only buffer ids for reuse when we're the master. This is mostly an optimization since
// when in slave role the ids are thrown away anyway.
eligibleForIdReuse = new IdReuseEligibility()
{
@Override
public boolean isEligible( KernelTransactionsSnapshot snapshot )
{
switch ( members.getCurrentMemberRole() )
{
case HighAvailabilityModeSwitcher.SLAVE:
// If we're slave right now then just release them because the id generators in slave mode
// will throw them away anyway, no need to keep them in memory. The architecture around
// how buffering is done isn't a 100% fit for HA since the wrapping if IdGeneratorFactory
// where the buffering takes place is done in a place which is oblivious to HA and roles
// which means that buffering will always take place. For now we'll have to live with
// always buffering and only just release them as soon as possible when slave.
return true;
case HighAvailabilityModeSwitcher.MASTER:
// If we're master then we have to keep these ids around during the configured safe zone time
// so that slaves have a chance to read consistently as well (slaves will know and compensate
// for falling outside of safe zone). Let's keep this separate from SLAVE since they
// have different reasons for doing what they do.
return Clock.SYSTEM_CLOCK.currentTimeMillis() - snapshot.snapshotTime() >= idReuseSafeZone;
default:
// If we're anything other than slave, i.e. also pending then retain the ids since we're
// not quite sure what state we're in at the moment and we clear the id buffers anyway
// during state switch.
return false;
}
}
};
eligibleForIdReuse = new HaIdReuseEligibility( members, Clock.SYSTEM_CLOCK, idReuseSafeZone );

registerRecovery( config.get( GraphDatabaseFacadeFactory.Configuration.editionName ), dependencies, logging );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2002-2016 "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.ha.id;

import org.neo4j.helpers.Clock;
import org.neo4j.kernel.ha.cluster.HighAvailabilityModeSwitcher;
import org.neo4j.kernel.ha.cluster.member.ClusterMembers;
import org.neo4j.kernel.impl.api.KernelTransactionsSnapshot;
import org.neo4j.kernel.impl.store.id.IdReuseEligibility;

/**
* This {@link IdReuseEligibility} only buffer ids for reuse when we're the master.
* This is mostly an optimization since when in slave role the ids are thrown away anyway.
*/
public class HaIdReuseEligibility implements IdReuseEligibility
{
private final ClusterMembers members;
private final Clock clock;
private final long idReuseSafeZone;

public HaIdReuseEligibility( ClusterMembers members, Clock clock, long idReuseSafeZone )
{
this.members = members;
this.clock = clock;
this.idReuseSafeZone = idReuseSafeZone;
}

@Override
public boolean isEligible( KernelTransactionsSnapshot snapshot )
{
switch ( members.getCurrentMemberRole() )
{
case HighAvailabilityModeSwitcher.SLAVE:
// If we're slave right now then just release them because the id generators in slave mode
// will throw them away anyway, no need to keep them in memory. The architecture around
// how buffering is done isn't a 100% fit for HA since the wrapping if IdGeneratorFactory
// where the buffering takes place is done in a place which is oblivious to HA and roles
// which means that buffering will always take place. For now we'll have to live with
// always buffering and only just release them as soon as possible when slave.
return true;
case HighAvailabilityModeSwitcher.MASTER:
// If we're master then we have to keep these ids around during the configured safe zone time
// so that slaves have a chance to read consistently as well (slaves will know and compensate
// for falling outside of safe zone). Let's keep this separate from SLAVE since they
// have different reasons for doing what they do.
return clock.currentTimeMillis() - snapshot.snapshotTime() >= idReuseSafeZone;
default:
// If we're anything other than slave, i.e. also pending then retain the ids since we're
// not quite sure what state we're in at the moment and we clear the id buffers anyway
// during state switch.
return false;
}
}
}

0 comments on commit 5d91e08

Please sign in to comment.