@@ -27,8 +27,8 @@ public class SyncedFileSystemDirectoryFactory : FileSystemDirectoryFactory
27
27
private readonly ILoggerFactory _loggerFactory ;
28
28
private readonly bool _tryFixMainIndexIfCorrupt ;
29
29
private readonly ILogger < SyncedFileSystemDirectoryFactory > _logger ;
30
- private ExamineReplicator _replicator ;
31
- private Directory _mainLuceneDir ;
30
+ private readonly ILogger < ExamineReplicator > _replicatorLogger ;
31
+ private readonly ILogger < LoggingReplicationClient > _clientLogger ;
32
32
33
33
[ Obsolete ( "Use ctor with all dependencies" ) ]
34
34
public SyncedFileSystemDirectoryFactory (
@@ -75,6 +75,8 @@ public SyncedFileSystemDirectoryFactory(
75
75
_loggerFactory = loggerFactory ;
76
76
_tryFixMainIndexIfCorrupt = tryFixMainIndexIfCorrupt ;
77
77
_logger = _loggerFactory . CreateLogger < SyncedFileSystemDirectoryFactory > ( ) ;
78
+ _replicatorLogger = _loggerFactory . CreateLogger < ExamineReplicator > ( ) ;
79
+ _clientLogger = _loggerFactory . CreateLogger < LoggingReplicationClient > ( ) ;
78
80
}
79
81
80
82
internal CreateResult TryCreateDirectory ( LuceneIndex luceneIndex , bool forceUnlock , out Directory directory )
@@ -88,24 +90,27 @@ internal CreateResult TryCreateDirectory(LuceneIndex luceneIndex, bool forceUnlo
88
90
// used by the replicator, will be a short lived directory for each synced revision and deleted when finished.
89
91
var tempDir = new DirectoryInfo ( Path . Combine ( _localDir . FullName , "Rep" , Guid . NewGuid ( ) . ToString ( "N" ) ) ) ;
90
92
91
- _mainLuceneDir = base . CreateDirectory ( luceneIndex , forceUnlock ) ;
93
+ var mainLuceneDir = base . CreateDirectory ( luceneIndex , forceUnlock ) ;
92
94
var localLuceneDir = FSDirectory . Open (
93
95
localLuceneIndexFolder ,
94
96
LockFactory . GetLockFactory ( localLuceneIndexFolder ) ) ;
95
97
96
- var mainIndexExists = DirectoryReader . IndexExists ( _mainLuceneDir ) ;
98
+ var mainIndexExists = DirectoryReader . IndexExists ( mainLuceneDir ) ;
97
99
var localIndexExists = DirectoryReader . IndexExists ( localLuceneDir ) ;
98
100
99
101
var mainResult = CreateResult . Init ;
100
102
101
103
if ( mainIndexExists )
102
104
{
103
- mainResult = CheckIndexHealthAndFix ( _mainLuceneDir , luceneIndex . Name , _tryFixMainIndexIfCorrupt ) ;
105
+ mainResult = CheckIndexHealthAndFix ( mainLuceneDir , luceneIndex . Name , _tryFixMainIndexIfCorrupt ) ;
104
106
}
105
107
106
108
// the main index is/was unhealthy or missing, lets check the local index if it exists
107
109
if ( localIndexExists && ( ! mainIndexExists || mainResult . HasFlag ( CreateResult . NotClean ) || mainResult . HasFlag ( CreateResult . MissingSegments ) ) )
108
110
{
111
+ // TODO: add details here and more below too
112
+ _logger . LogInformation ( "" ) ;
113
+
109
114
var localResult = CheckIndexHealthAndFix ( localLuceneDir , luceneIndex . Name , false ) ;
110
115
111
116
if ( localResult == CreateResult . Init )
@@ -132,7 +137,7 @@ internal CreateResult TryCreateDirectory(LuceneIndex luceneIndex, bool forceUnlo
132
137
? OpenMode . APPEND
133
138
: OpenMode . CREATE ;
134
139
135
- mainResult |= TryGetIndexWriter ( openMode , _mainLuceneDir , true , luceneIndex . Name , out var indexWriter ) ;
140
+ mainResult |= TryGetIndexWriter ( openMode , mainLuceneDir , true , luceneIndex . Name , out var indexWriter ) ;
136
141
using ( indexWriter )
137
142
{
138
143
if ( ! mainResult . HasFlag ( CreateResult . SyncedFromLocal ) )
@@ -142,27 +147,25 @@ internal CreateResult TryCreateDirectory(LuceneIndex luceneIndex, bool forceUnlo
142
147
}
143
148
}
144
149
145
- // now create the replicator that will copy from local to main on schedule
146
- _replicator = new ExamineReplicator ( _loggerFactory , luceneIndex , _mainLuceneDir , tempDir ) ;
147
-
148
150
if ( forceUnlock )
149
151
{
150
152
IndexWriter . Unlock ( localLuceneDir ) ;
151
153
}
152
154
153
- // Start replicating back to main
154
- _replicator . StartIndexReplicationOnSchedule ( 1000 ) ;
155
+ Directory luceneDir ;
155
156
156
157
var options = IndexOptions . GetNamedOptions ( luceneIndex . Name ) ;
157
158
if ( options . NrtEnabled )
158
159
{
159
- directory = new NRTCachingDirectory ( localLuceneDir , options . NrtCacheMaxMergeSizeMB , options . NrtCacheMaxCachedMB ) ;
160
+ luceneDir = new NRTCachingDirectory ( localLuceneDir , options . NrtCacheMaxMergeSizeMB , options . NrtCacheMaxCachedMB ) ;
160
161
}
161
162
else
162
163
{
163
- directory = localLuceneDir ;
164
+ luceneDir = localLuceneDir ;
164
165
}
165
166
167
+ directory = new SyncedFileSystemDirectory ( _replicatorLogger , _clientLogger , luceneDir , mainLuceneDir , luceneIndex , tempDir ) ;
168
+
166
169
return mainResult ;
167
170
}
168
171
@@ -186,16 +189,6 @@ protected override Directory CreateDirectory(LuceneIndex luceneIndex, bool force
186
189
return directory ;
187
190
}
188
191
189
- protected override void Dispose ( bool disposing )
190
- {
191
- base . Dispose ( disposing ) ;
192
- if ( disposing )
193
- {
194
- _replicator ? . Dispose ( ) ;
195
- _mainLuceneDir ? . Dispose ( ) ;
196
- }
197
- }
198
-
199
192
private CreateResult TryGetIndexWriter (
200
193
OpenMode openMode ,
201
194
Directory luceneDirectory ,
@@ -228,6 +221,8 @@ private CreateResult TryGetIndexWriter(
228
221
// Index is corrupted, typically this will be FileNotFoundException or CorruptIndexException
229
222
_logger . LogError ( ex , "{IndexName} index is corrupt, a new one will be created" , indexName ) ;
230
223
224
+ // TODO: Here I think we need to totally clear all files in the directory
225
+
231
226
indexWriter = GetIndexWriter ( luceneDirectory , OpenMode . CREATE ) ;
232
227
}
233
228
else
@@ -252,7 +247,7 @@ private void SyncIndex(IndexWriter sourceIndexWriter, bool forceUnlock, string i
252
247
253
248
using ( var sourceIndex = new LuceneIndex ( _loggerFactory , indexName , new TempOptions ( ) , sourceIndexWriter ) )
254
249
using ( var destinationLuceneDirectory = FSDirectory . Open ( destinationDirectory , LockFactory . GetLockFactory ( destinationDirectory ) ) )
255
- using ( var replicator = new ExamineReplicator ( _loggerFactory , sourceIndex , destinationLuceneDirectory , tempDir ) )
250
+ using ( var replicator = new ExamineReplicator ( _replicatorLogger , _clientLogger , sourceIndex , sourceIndexWriter . Directory , destinationLuceneDirectory , tempDir ) )
256
251
{
257
252
if ( forceUnlock )
258
253
{
@@ -328,7 +323,7 @@ private CreateResult CheckIndexHealthAndFix(
328
323
return result ;
329
324
}
330
325
331
- private IndexWriter GetIndexWriter ( Directory mainDir , OpenMode openMode )
326
+ private static IndexWriter GetIndexWriter ( Directory mainDir , OpenMode openMode )
332
327
{
333
328
var indexWriter = new IndexWriter (
334
329
mainDir ,
0 commit comments