Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced CoreLogPruningStrategy interface.
Introduced an interface for pruning strategies and two implementations of this interface: Size based and Entry based.
- Loading branch information
Max Sumrall
committed
May 17, 2016
1 parent
1686964
commit 0dd4b85
Showing
16 changed files
with
516 additions
and
38 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...core-edge/src/main/java/org/neo4j/coreedge/raft/log/segmented/CoreLogPruningStrategy.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,35 @@ | ||
/* | ||
* 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.log.segmented; | ||
|
||
public interface CoreLogPruningStrategy | ||
{ | ||
/** | ||
* Returns the index to keep depending on the configuration strategy. | ||
* This does not factor in the value of the safe index to prune to. | ||
* | ||
* It is worth noting that the returned value may be the first available value, | ||
* rather than the first possible value. This signifies that no pruning is needed. | ||
* @param segments | ||
* @return The lowest index the pruning configuration allows to keep. It is a value in the same range as | ||
* append indexes, starting from -1 all the way to {@link Long#MAX_VALUE}. | ||
*/ | ||
long getIndexToKeep( Segments segments ); | ||
} |
64 changes: 64 additions & 0 deletions
64
...dge/src/main/java/org/neo4j/coreedge/raft/log/segmented/EntryBasedLogPruningStrategy.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,64 @@ | ||
/* | ||
* 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.log.segmented; | ||
|
||
import java.util.ListIterator; | ||
|
||
import org.neo4j.logging.Log; | ||
import org.neo4j.logging.LogProvider; | ||
|
||
public class EntryBasedLogPruningStrategy implements CoreLogPruningStrategy | ||
{ | ||
private final long entriesToKeep; | ||
private final Log log; | ||
|
||
public EntryBasedLogPruningStrategy( long entriesToKeep, LogProvider logProvider ) | ||
{ | ||
this.entriesToKeep = entriesToKeep; | ||
this.log = logProvider.getLog( getClass() ); | ||
} | ||
|
||
@Override | ||
public long getIndexToKeep( Segments segments ) | ||
{ | ||
ListIterator<SegmentFile> iterator = segments.getSegmentFileIteratorAtEnd(); | ||
SegmentFile segmentFile = null; | ||
long nextPrevIndex = 0; | ||
long accumulated = 0; | ||
if ( !iterator.hasPrevious() ) | ||
{ | ||
log.warn( "No log files found during the prune operation. This state should resolve on its own, but" + | ||
" if this warning continues, you may want to look for other errors in the user log." ); | ||
return -1; // -1 is the lowest possible append index and so always safe to return. | ||
} | ||
segmentFile = iterator.previous(); | ||
nextPrevIndex = segmentFile.header().prevIndex(); | ||
long prevIndex; | ||
// Iterate backwards through the files, counting entries from the headers until the limit is reached. | ||
while ( accumulated < entriesToKeep && iterator.hasPrevious() ) | ||
{ | ||
segmentFile = iterator.previous(); | ||
prevIndex = segmentFile.header().prevIndex(); | ||
accumulated += (nextPrevIndex - prevIndex); | ||
nextPrevIndex = prevIndex; | ||
} | ||
return segmentFile.header().prevIndex(); | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
...core-edge/src/main/java/org/neo4j/coreedge/raft/log/segmented/SegmentedRaftLogPruner.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,62 @@ | ||
/* | ||
* 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.log.segmented; | ||
|
||
|
||
import org.neo4j.logging.LogProvider; | ||
|
||
import static org.neo4j.kernel.impl.transaction.log.pruning.ThresholdConfigParser.ThresholdConfigValue; | ||
import static org.neo4j.kernel.impl.transaction.log.pruning.ThresholdConfigParser.parse; | ||
|
||
public class SegmentedRaftLogPruner | ||
{ | ||
private final ThresholdConfigValue parsedConfigOption; | ||
private final CoreLogPruningStrategy pruneStrategy; | ||
|
||
public SegmentedRaftLogPruner( String pruningStrategyConfig, LogProvider logProvider ) | ||
{ | ||
parsedConfigOption = parse( pruningStrategyConfig ); | ||
pruneStrategy = getPruneStrategy( parsedConfigOption, logProvider ); | ||
} | ||
|
||
private CoreLogPruningStrategy getPruneStrategy( ThresholdConfigValue configValue, LogProvider logProvider ) | ||
{ | ||
switch ( configValue.type ) | ||
{ | ||
case "size": | ||
return new SizeBasedLogPruningStrategy( parsedConfigOption.value ); | ||
case "txs": | ||
case "entries": // txs and entries are synonyms | ||
return new EntryBasedLogPruningStrategy( parsedConfigOption.value, logProvider ); | ||
case "hours": | ||
return new SizeBasedLogPruningStrategy( parsedConfigOption.value ); //NOT FINISHED | ||
case "days": | ||
return new SizeBasedLogPruningStrategy( parsedConfigOption.value ); //NOT FINISHED | ||
default: | ||
throw new IllegalArgumentException( "Invalid log pruning configuration value '" + configValue.value + | ||
"'. Invalid type '" + configValue.type + "', valid are files, size, txs, entries, hours, days." ); | ||
} | ||
} | ||
|
||
public long getIndexToPruneFrom( long safeIndex, Segments segments ) | ||
{ | ||
return Math.min( safeIndex, pruneStrategy.getIndexToKeep( segments ) ); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
...edge/src/main/java/org/neo4j/coreedge/raft/log/segmented/SizeBasedLogPruningStrategy.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,51 @@ | ||
/* | ||
* 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.log.segmented; | ||
|
||
import java.util.ListIterator; | ||
|
||
public class SizeBasedLogPruningStrategy implements CoreLogPruningStrategy | ||
{ | ||
private final long bytesToKeep; | ||
|
||
public SizeBasedLogPruningStrategy( long bytesToKeep ) | ||
{ | ||
this.bytesToKeep = bytesToKeep; | ||
} | ||
|
||
public long getIndexToKeep( Segments segments ) | ||
{ | ||
long accumulatedSize = 0; | ||
ListIterator<SegmentFile> iterator = segments.getSegmentFileIteratorAtEnd(); | ||
SegmentFile segmentFile = null; | ||
while ( accumulatedSize < bytesToKeep && iterator.hasPrevious() ) | ||
{ | ||
segmentFile = iterator.previous(); | ||
accumulatedSize += sizeOf( segmentFile ); | ||
} | ||
|
||
return segmentFile != null ? segmentFile.header().prevIndex() : -1; | ||
} | ||
|
||
private long sizeOf( SegmentFile value ) | ||
{ | ||
return value.size(); | ||
} | ||
} |
Oops, something went wrong.