Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renaming to follow the same 'state' naming convention as the IdAlloca…
…tionState and RaftMembershipState.
- Loading branch information
Showing
24 changed files
with
518 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
enterprise/core-edge/src/main/java/org/neo4j/coreedge/raft/state/term/OnDiskTermState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.coreedge.raft.state.term; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
import org.neo4j.coreedge.raft.log.RaftStorageException; | ||
import org.neo4j.io.fs.FileSystemAbstraction; | ||
import org.neo4j.io.fs.StoreChannel; | ||
import org.neo4j.kernel.lifecycle.LifecycleAdapter; | ||
|
||
public class OnDiskTermState extends LifecycleAdapter implements TermState | ||
{ | ||
public static final int TERM_BYTES = 8; | ||
|
||
private final StoreChannel channel; | ||
private long term; | ||
|
||
public OnDiskTermState( FileSystemAbstraction fileSystem, File directory ) | ||
{ | ||
try | ||
{ | ||
channel = fileSystem.open( new File( directory, "term.state" ), "rw" ); | ||
term = readTerm(); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new RuntimeException( e ); | ||
} | ||
} | ||
|
||
@Override | ||
public void shutdown() throws Throwable | ||
{ | ||
channel.force( false ); | ||
channel.close(); | ||
} | ||
|
||
@Override | ||
public long currentTerm() | ||
{ | ||
return term; | ||
} | ||
|
||
@Override | ||
public void update( long newTerm ) throws RaftStorageException | ||
{ | ||
if ( newTerm < term ) | ||
{ | ||
throw new IllegalArgumentException( "Cannot move to a lower term" ); | ||
} | ||
|
||
try | ||
{ | ||
ByteBuffer buffer = ByteBuffer.allocate( TERM_BYTES ); | ||
buffer.putLong( newTerm ); | ||
buffer.flip(); | ||
|
||
channel.writeAll( buffer, 0 ); | ||
channel.force( false ); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new RaftStorageException( "Failed to update term", e ); | ||
} | ||
term = newTerm; | ||
} | ||
|
||
private long readTerm() throws IOException | ||
{ | ||
if ( channel.size() < TERM_BYTES ) | ||
{ | ||
return 0; | ||
} | ||
ByteBuffer buffer = ByteBuffer.allocate( TERM_BYTES ); | ||
channel.read( buffer, 0 ); | ||
buffer.flip(); | ||
return buffer.getLong(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
enterprise/core-edge/src/main/java/org/neo4j/coreedge/raft/state/vote/OnDiskVoteState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* 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.coreedge.raft.state.vote; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
|
||
import org.neo4j.coreedge.server.CoreMember; | ||
import org.neo4j.coreedge.raft.log.RaftStorageException; | ||
import org.neo4j.coreedge.raft.membership.CoreMarshal; | ||
import org.neo4j.io.fs.FileSystemAbstraction; | ||
import org.neo4j.io.fs.StoreChannel; | ||
import org.neo4j.kernel.lifecycle.LifecycleAdapter; | ||
|
||
public class OnDiskVoteState extends LifecycleAdapter implements VoteState<CoreMember> | ||
{ | ||
private final StoreChannel channel; | ||
private final CoreMarshal coreMemberMarshal = new CoreMarshal(); | ||
private CoreMember votedFor; | ||
|
||
public OnDiskVoteState( FileSystemAbstraction fileSystem, File directory ) | ||
{ | ||
try | ||
{ | ||
channel = fileSystem.open( new File( directory, "vote.state" ), "rw" ); | ||
votedFor = readVote(); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new RuntimeException( e ); | ||
} | ||
} | ||
|
||
@Override | ||
public void shutdown() throws Throwable | ||
{ | ||
channel.force( false ); | ||
channel.close(); | ||
} | ||
|
||
@Override | ||
public CoreMember votedFor() | ||
{ | ||
return votedFor; | ||
} | ||
|
||
@Override | ||
public void update( CoreMember votedFor ) throws RaftStorageException | ||
{ | ||
try | ||
{ | ||
if ( votedFor == null ) | ||
{ | ||
channel.truncate( 0 ); | ||
} | ||
else | ||
{ | ||
ByteBuf byteBuf = Unpooled.buffer(); | ||
coreMemberMarshal.marshal( votedFor, byteBuf ); | ||
ByteBuffer buffer = byteBuf.nioBuffer(); | ||
|
||
channel.writeAll( buffer, 0 ); | ||
} | ||
channel.force( false ); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new RaftStorageException( "Failed to update votedFor", e ); | ||
} | ||
this.votedFor = votedFor; | ||
} | ||
|
||
private CoreMember readVote() throws IOException | ||
{ | ||
if ( channel.size() == 0 ) | ||
{ | ||
return null; | ||
} | ||
|
||
ByteBuffer buffer = ByteBuffer.allocate( (int) channel.size() ); | ||
channel.read( buffer, 0 ); | ||
buffer.flip(); | ||
|
||
return coreMemberMarshal.unmarshal( Unpooled.wrappedBuffer( buffer ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.