10
10
import org .apache .logging .log4j .LogManager ;
11
11
import org .apache .logging .log4j .Logger ;
12
12
import org .apache .logging .log4j .message .ParameterizedMessage ;
13
- import org .apache .lucene .codecs .CodecUtil ;
14
13
import org .apache .lucene .store .AlreadyClosedException ;
15
14
import org .apache .lucene .store .IOContext ;
16
15
import org .apache .lucene .store .IndexInput ;
29
28
import org .elasticsearch .index .store .BaseSearchableSnapshotIndexInput ;
30
29
import org .elasticsearch .index .store .IndexInputStats ;
31
30
import org .elasticsearch .index .store .SearchableSnapshotDirectory ;
32
- import org .elasticsearch .xpack .searchablesnapshots .SearchableSnapshotsConstants ;
33
31
import org .elasticsearch .xpack .searchablesnapshots .cache .ByteRange ;
34
32
35
33
import java .io .EOFException ;
42
40
import java .util .concurrent .atomic .AtomicLong ;
43
41
import java .util .concurrent .atomic .AtomicReference ;
44
42
import java .util .function .Consumer ;
45
- import java .util .function .Predicate ;
46
43
import java .util .stream .IntStream ;
47
44
48
- import static org .elasticsearch .index .store .checksum .ChecksumBlobContainerIndexInput .checksumToBytesArray ;
49
45
import static org .elasticsearch .xpack .searchablesnapshots .SearchableSnapshotsUtils .toIntBytes ;
50
46
51
47
public class CachedBlobContainerIndexInput extends BaseSearchableSnapshotIndexInput {
@@ -106,7 +102,7 @@ private CachedBlobContainerIndexInput(
106
102
int rangeSize ,
107
103
int recoveryRangeSize
108
104
) {
109
- super (resourceDesc , directory .blobContainer (), fileInfo , context , stats , offset , length );
105
+ super (logger , resourceDesc , directory .blobContainer (), fileInfo , context , stats , offset , length );
110
106
this .directory = directory ;
111
107
this .cacheFileReference = cacheFileReference ;
112
108
this .lastReadPosition = this .offset ;
@@ -116,19 +112,12 @@ private CachedBlobContainerIndexInput(
116
112
}
117
113
118
114
@ Override
119
- public void innerClose () {
115
+ public void doClose () {
120
116
if (isClone == false ) {
121
117
cacheFileReference .releaseOnClose ();
122
118
}
123
119
}
124
120
125
- private void ensureContext (Predicate <IOContext > predicate ) throws IOException {
126
- if (predicate .test (context ) == false ) {
127
- assert false : "this method should not be used with this context " + context ;
128
- throw new IOException ("Cannot read the index input using context [context=" + context + ", input=" + this + ']' );
129
- }
130
- }
131
-
132
121
private long getDefaultRangeSize () {
133
122
return (context != CACHE_WARMING_CONTEXT )
134
123
? (directory .isRecoveryFinalized () ? defaultRangeSize : recoveryRangeSize )
@@ -143,24 +132,12 @@ private ByteRange computeRange(long position) {
143
132
}
144
133
145
134
@ Override
146
- protected void readInternal (ByteBuffer b ) throws IOException {
135
+ protected void doReadInternal (ByteBuffer b ) throws IOException {
147
136
ensureContext (ctx -> ctx != CACHE_WARMING_CONTEXT );
148
- assert assertCurrentThreadIsNotCacheFetchAsync ();
149
137
final long position = getFilePointer () + this .offset ;
150
138
final int length = b .remaining ();
151
139
152
- // We can detect that we're going to read the last 16 bytes (that contains the footer checksum) of the file. Such reads are often
153
- // executed when opening a Directory and since we have the checksum in the snapshot metadata we can use it to fill the ByteBuffer.
154
- if (length == CodecUtil .footerLength () && isClone == false && position == fileInfo .length () - length ) {
155
- if (readChecksumFromFileInfo (b )) {
156
- logger .trace ("read footer of file [{}] at position [{}], bypassing all caches" , fileInfo .physicalName (), position );
157
- return ;
158
- }
159
- assert b .remaining () == length ;
160
- }
161
-
162
140
logger .trace ("readInternal: read [{}-{}] ([{}] bytes) from [{}]" , position , position + length , length , this );
163
-
164
141
try {
165
142
final CacheFile cacheFile = cacheFileReference .get ();
166
143
@@ -401,26 +378,6 @@ private int readDirectlyIfAlreadyClosed(long position, ByteBuffer b, Exception e
401
378
throw new IOException ("failed to read data from cache" , e );
402
379
}
403
380
404
- private boolean readChecksumFromFileInfo (ByteBuffer b ) throws IOException {
405
- assert isClone == false ;
406
- byte [] footer ;
407
- try {
408
- footer = checksumToBytesArray (fileInfo .checksum ());
409
- } catch (NumberFormatException e ) {
410
- // tests disable this optimisation by passing an invalid checksum
411
- footer = null ;
412
- }
413
- if (footer == null ) {
414
- return false ;
415
- }
416
-
417
- b .put (footer );
418
- assert b .remaining () == 0L ;
419
- return true ;
420
-
421
- // TODO we should add this to DirectBlobContainerIndexInput too.
422
- }
423
-
424
381
/**
425
382
* Prefetches a complete part and writes it in cache. This method is used to prewarm the cache.
426
383
* @return a tuple with {@code Tuple<Persistent Cache Length, Prefetched Length>} values
@@ -737,23 +694,11 @@ private static boolean assertFileChannelOpen(FileChannel fileChannel) {
737
694
return true ;
738
695
}
739
696
740
- private static boolean isCacheFetchAsyncThread (final String threadName ) {
741
- return threadName .contains ('[' + SearchableSnapshotsConstants .CACHE_FETCH_ASYNC_THREAD_POOL_NAME + ']' );
742
- }
743
-
744
697
private static boolean assertCurrentThreadMayWriteCacheFile () {
745
698
final String threadName = Thread .currentThread ().getName ();
746
699
assert isCacheFetchAsyncThread (threadName ) : "expected the current thread ["
747
700
+ threadName
748
701
+ "] to belong to the cache fetch async thread pool" ;
749
702
return true ;
750
703
}
751
-
752
- private static boolean assertCurrentThreadIsNotCacheFetchAsync () {
753
- final String threadName = Thread .currentThread ().getName ();
754
- assert false == isCacheFetchAsyncThread (threadName ) : "expected the current thread ["
755
- + threadName
756
- + "] to belong to the cache fetch async thread pool" ;
757
- return true ;
758
- }
759
704
}
0 commit comments