21
21
22
22
import org .apache .lucene .store .AlreadyClosedException ;
23
23
import org .elasticsearch .common .io .Channels ;
24
+ import org .elasticsearch .core .internal .io .IOUtils ;
25
+ import org .elasticsearch .index .seqno .SequenceNumbers ;
24
26
25
27
import java .io .Closeable ;
26
28
import java .io .EOFException ;
27
29
import java .io .IOException ;
28
30
import java .nio .ByteBuffer ;
29
31
import java .nio .channels .FileChannel ;
30
32
import java .nio .file .Path ;
33
+ import java .nio .file .StandardOpenOption ;
31
34
import java .util .concurrent .atomic .AtomicBoolean ;
32
35
36
+ import static org .elasticsearch .index .translog .Translog .getCommitCheckpointFileName ;
37
+
33
38
/**
34
39
* an immutable translog filereader
35
40
*/
36
41
public class TranslogReader extends BaseTranslogReader implements Closeable {
37
42
protected final long length ;
38
43
private final int totalOperations ;
39
44
private final Checkpoint checkpoint ;
40
- protected final AtomicBoolean closed ;
45
+ protected final AtomicBoolean closed = new AtomicBoolean ( false ) ;
41
46
42
47
/**
43
48
* Create a translog writer against the specified translog file channel.
@@ -48,24 +53,10 @@ public class TranslogReader extends BaseTranslogReader implements Closeable {
48
53
* @param header the header of the translog file
49
54
*/
50
55
TranslogReader (final Checkpoint checkpoint , final FileChannel channel , final Path path , final TranslogHeader header ) {
51
- this (checkpoint , channel , path , header , new AtomicBoolean (false ));
52
- }
53
-
54
- /**
55
- * Create a translog writer against the specified translog file channel.
56
- *
57
- * @param checkpoint the translog checkpoint
58
- * @param channel the translog file channel to open a translog reader against
59
- * @param path the path to the translog
60
- * @param header the header of the translog file
61
- *
62
- */
63
- TranslogReader (final Checkpoint checkpoint , final FileChannel channel , final Path path , final TranslogHeader header , AtomicBoolean closed ) {
64
56
super (checkpoint .generation , channel , path , header );
65
57
this .length = checkpoint .offset ;
66
58
this .totalOperations = checkpoint .numOps ;
67
59
this .checkpoint = checkpoint ;
68
- this .closed = closed ;
69
60
}
70
61
71
62
/**
@@ -85,10 +76,27 @@ public static TranslogReader open(
85
76
}
86
77
87
78
/**
88
- * Create a new reader with new checkoint that shares resources with current one
79
+ * Closes current reader and creates new one with new checkoint and same file channel
89
80
*/
90
- TranslogReader withNewCheckpoint (final Checkpoint newCheckpoint ){
91
- return new TranslogReader (newCheckpoint , channel , path , header , closed );
81
+ TranslogReader closeIntoTrimmedReader (long belowTerm , long aboveSeqNo , ChannelFactory channelFactory ) throws IOException {
82
+ if (!closed .get ()
83
+ && getPrimaryTerm () < belowTerm
84
+ && checkpoint .maxSeqNo != SequenceNumbers .NO_OPS_PERFORMED
85
+ && (aboveSeqNo < checkpoint .trimmedAboveSeqNo || checkpoint .trimmedAboveSeqNo == SequenceNumbers .UNASSIGNED_SEQ_NO )) {
86
+ final Path checkpointFile = path .getParent ().resolve (getCommitCheckpointFileName (checkpoint .generation ));
87
+ final Checkpoint newCheckpoint = new Checkpoint (checkpoint .offset , checkpoint .numOps ,
88
+ checkpoint .generation , checkpoint .minSeqNo , checkpoint .maxSeqNo ,
89
+ checkpoint .globalCheckpoint , checkpoint .minTranslogGeneration , aboveSeqNo );
90
+ Checkpoint .write (channelFactory , checkpointFile , newCheckpoint , StandardOpenOption .WRITE );
91
+
92
+ IOUtils .fsync (checkpointFile , false );
93
+ IOUtils .fsync (checkpointFile .getParent (), true );
94
+
95
+ closed .set (true );
96
+
97
+ return new TranslogReader (newCheckpoint , channel , path , header );
98
+ }
99
+ return this ;
92
100
}
93
101
94
102
public long sizeInBytes () {
0 commit comments