Skip to content

Commit

Permalink
Sepearate logic to class EnableableLifeCycle
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarW committed Apr 20, 2018
1 parent 22355d4 commit bf561e7
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 141 deletions.
Expand Up @@ -40,7 +40,6 @@
import org.neo4j.helpers.AdvertisedSocketAddress; import org.neo4j.helpers.AdvertisedSocketAddress;
import org.neo4j.helpers.ListenSocketAddress; import org.neo4j.helpers.ListenSocketAddress;
import org.neo4j.logging.LogProvider; import org.neo4j.logging.LogProvider;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;


import static org.neo4j.causalclustering.protocol.Protocol.ModifierProtocols.Implementations.GZIP; import static org.neo4j.causalclustering.protocol.Protocol.ModifierProtocols.Implementations.GZIP;
import static org.neo4j.causalclustering.protocol.Protocol.ModifierProtocols.Implementations.LZ4; import static org.neo4j.causalclustering.protocol.Protocol.ModifierProtocols.Implementations.LZ4;
Expand Down
Expand Up @@ -57,8 +57,8 @@ public Role getRole()
return getDependencyResolver().resolveDependency( RaftMachine.class ).currentRole(); return getDependencyResolver().resolveDependency( RaftMachine.class ).currentRole();
} }


public void stopCatchupServer() public void disableCatchupServer()
{ {
((EnterpriseCoreEditionModule) editionModule).stopCatchupServer(); ((EnterpriseCoreEditionModule) editionModule).disableCatchupServer();
} }
} }
Expand Up @@ -501,8 +501,8 @@ public void setupSecurityModule( PlatformModule platformModule, Procedures proce
EnterpriseEditionModule.setupEnterpriseSecurityModule( platformModule, procedures ); EnterpriseEditionModule.setupEnterpriseSecurityModule( platformModule, procedures );
} }


public void stopCatchupServer() public void disableCatchupServer()
{ {
coreServerModule.catchupServer().stop(); coreServerModule.catchupServer().disable();
} }
} }
Expand Up @@ -22,6 +22,8 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;


import org.neo4j.function.ThrowingConsumer;

public class CompositeEnableable implements Enableable public class CompositeEnableable implements Enableable
{ {
private final List<Enableable> enableables = new ArrayList<>(); private final List<Enableable> enableables = new ArrayList<>();
Expand All @@ -34,12 +36,30 @@ public void add( Enableable enableable )
@Override @Override
public void enable() public void enable()
{ {
enableables.forEach( Enableable::enable ); doOperation( Enableable::enable, "Enable" );
} }


@Override @Override
public void disable() public void disable()
{ {
enableables.forEach( Enableable::disable ); doOperation( Enableable::disable, "Disable" );
}

private void doOperation( ThrowingConsumer<Enableable,Throwable> operation, String description )
{
try ( ErrorHandler errorHandler = new ErrorHandler( description ) )
{
for ( Enableable enableable : enableables )
{
try
{
operation.accept( enableable );
}
catch ( Throwable throwable )
{
errorHandler.add( throwable );
}
}
}
} }
} }
Expand Up @@ -21,7 +21,7 @@


public interface Enableable public interface Enableable
{ {
void enable(); void enable() throws Throwable;


void disable(); void disable() throws Throwable;
} }
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2002-2018 "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.causalclustering.helper;

import org.neo4j.kernel.lifecycle.Lifecycle;
import org.neo4j.logging.Log;

public abstract class EnableableLifeCycle implements Lifecycle, Enableable
{
private final Log debugLog;
private boolean stoppedByLifeCycle = true;
private boolean enabled = true;

public EnableableLifeCycle( Log debugLog )
{
this.debugLog = debugLog;
}

@Override
public void enable()
{
enabled = true;
if ( !stoppedByLifeCycle )
{
start0();
}
else
{
debugLog.info( "%s will not start. It was enabled but is stopped by lifecycle", this );
}
}

@Override
public void disable()
{
enabled = false;
stop0();
}

@Override
public void init()
{
init0();
}

@Override
public void start()
{
stoppedByLifeCycle = false;
if ( !enabled )
{
debugLog.info( "Start call from lifecycle is ignored because %s is disabled.", this );
}
else
{
start0();
}
}

@Override
public void stop()
{
stoppedByLifeCycle = true;
stop0();
}

@Override
public void shutdown()
{
stoppedByLifeCycle = true;
shutdown0();
}

public abstract void init0();

public abstract void start0();

public abstract void stop0();

public abstract void shutdown0();
}
Expand Up @@ -17,10 +17,9 @@
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.neo4j.causalclustering.discovery; package org.neo4j.causalclustering.helper;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;


public class ErrorHandler implements AutoCloseable public class ErrorHandler implements AutoCloseable
Expand All @@ -38,11 +37,6 @@ public void add( Throwable throwable )
throwables.add( throwable ); throwables.add( throwable );
} }


public List<Throwable> throwables()
{
return Collections.unmodifiableList( throwables );
}

@Override @Override
public void close() throws RuntimeException public void close() throws RuntimeException
{ {
Expand Down
Expand Up @@ -30,15 +30,14 @@
import java.net.BindException; import java.net.BindException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


import org.neo4j.causalclustering.helper.Enableable; import org.neo4j.causalclustering.helper.EnableableLifeCycle;
import org.neo4j.helpers.ListenSocketAddress; import org.neo4j.helpers.ListenSocketAddress;
import org.neo4j.helpers.NamedThreadFactory; import org.neo4j.helpers.NamedThreadFactory;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.logging.Log; import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider; import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider; import org.neo4j.logging.NullLogProvider;


public class Server extends LifecycleAdapter implements Enableable public class Server extends EnableableLifeCycle
{ {
private final Log debugLog; private final Log debugLog;
private final Log userLog; private final Log userLog;
Expand All @@ -51,8 +50,6 @@ public class Server extends LifecycleAdapter implements Enableable


private EventLoopGroup workerGroup; private EventLoopGroup workerGroup;
private Channel channel; private Channel channel;
private boolean enabled = true;
private boolean stoppedByLifeCycle = true;


public Server( ChildInitializer childInitializer, LogProvider debugLogProvider, LogProvider userLogProvider, ListenSocketAddress listenAddress, public Server( ChildInitializer childInitializer, LogProvider debugLogProvider, LogProvider userLogProvider, ListenSocketAddress listenAddress,
String serverName ) String serverName )
Expand All @@ -63,6 +60,7 @@ public Server( ChildInitializer childInitializer, LogProvider debugLogProvider,
public Server( ChildInitializer childInitializer, ChannelInboundHandler parentHandler, LogProvider debugLogProvider, LogProvider userLogProvider, public Server( ChildInitializer childInitializer, ChannelInboundHandler parentHandler, LogProvider debugLogProvider, LogProvider userLogProvider,
ListenSocketAddress listenAddress, String serverName ) ListenSocketAddress listenAddress, String serverName )
{ {
super( debugLogProvider.getLog( Server.class ) );
this.childInitializer = childInitializer; this.childInitializer = childInitializer;
this.parentHandler = parentHandler; this.parentHandler = parentHandler;
this.listenAddress = listenAddress; this.listenAddress = listenAddress;
Expand All @@ -78,33 +76,13 @@ public Server( ChildInitializer childInitializer, ListenSocketAddress listenAddr
} }


@Override @Override
public synchronized void start() public void init0()
{ {
stoppedByLifeCycle = false; // do nothing
if ( !enabled )
{
debugLog.info( "Start call from lifecycle is ignored because server is disabled." );
}
else
{
doStart();
}
} }


@Override @Override
public void stop() public void start0()
{
stoppedByLifeCycle = true;
doStop();
}

@Override
public void shutdown()
{
stoppedByLifeCycle = true;
}

private void doStart()
{ {
if ( channel != null ) if ( channel != null )
{ {
Expand Down Expand Up @@ -142,12 +120,8 @@ private void doStart()
} }
} }


public boolean isRunnig() @Override
{ public void stop0()
return channel != null;
}

private void doStop()
{ {
if ( channel == null ) if ( channel == null )
{ {
Expand All @@ -173,29 +147,20 @@ private void doStop()
workerGroup = null; workerGroup = null;
} }


public ListenSocketAddress address() @Override
public void shutdown0()
{ {
return listenAddress; // do nothing
} }


@Override public ListenSocketAddress address()
public synchronized void enable()
{ {
enabled = true; return listenAddress;
if ( !stoppedByLifeCycle )
{
doStart();
}
else
{
debugLog.info( "Server will not start. It was enabled but is stopped by lifecycle" );
}
} }


@Override @Override
public synchronized void disable() public String toString()
{ {
enabled = false; return serverName;
doStop();
} }
} }
Expand Up @@ -49,6 +49,7 @@
import org.neo4j.causalclustering.core.consensus.roles.Role; import org.neo4j.causalclustering.core.consensus.roles.Role;
import org.neo4j.causalclustering.core.state.machines.id.IdGenerationException; import org.neo4j.causalclustering.core.state.machines.id.IdGenerationException;
import org.neo4j.causalclustering.core.state.machines.locks.LeaderOnlyLockManager; import org.neo4j.causalclustering.core.state.machines.locks.LeaderOnlyLockManager;
import org.neo4j.causalclustering.helper.ErrorHandler;
import org.neo4j.causalclustering.readreplica.ReadReplicaGraphDatabase; import org.neo4j.causalclustering.readreplica.ReadReplicaGraphDatabase;
import org.neo4j.function.ThrowingSupplier; import org.neo4j.function.ThrowingSupplier;
import org.neo4j.graphdb.DatabaseShutdownException; import org.neo4j.graphdb.DatabaseShutdownException;
Expand Down
Expand Up @@ -33,7 +33,6 @@
import org.neo4j.causalclustering.core.state.ClusterStateDirectory; import org.neo4j.causalclustering.core.state.ClusterStateDirectory;
import org.neo4j.causalclustering.core.state.RaftLogPruner; import org.neo4j.causalclustering.core.state.RaftLogPruner;
import org.neo4j.causalclustering.identity.MemberId; import org.neo4j.causalclustering.identity.MemberId;
import org.neo4j.graphdb.config.Setting;
import org.neo4j.graphdb.factory.GraphDatabaseSettings; import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.AdvertisedSocketAddress; import org.neo4j.helpers.AdvertisedSocketAddress;
import org.neo4j.io.fs.DefaultFileSystemAbstraction; import org.neo4j.io.fs.DefaultFileSystemAbstraction;
Expand Down Expand Up @@ -296,9 +295,9 @@ public File raftLogDirectory()
return raftLogDir; return raftLogDir;
} }


public void stopCatchupServer() public void disableCatchupServer()
{ {
database.stopCatchupServer(); database.disableCatchupServer();
} }


int discoveryPort() int discoveryPort()
Expand Down

0 comments on commit bf561e7

Please sign in to comment.