Commit 083092a
Improve data store handling with thread safety and resource management enhancements (#2954)
* Improve data store handling with thread safety and resource management enhancements
This commit addresses several improvements in the data store processing implementation:
**Thread Safety Improvements:**
- DataStoreFactory: Changed dataStoreMap from LinkedHashMap to ConcurrentHashMap for thread-safe concurrent access
- DataStoreFactory: Made dataStoreNames and lastLoadedTime volatile to ensure visibility across threads
- DataStoreFactory: Made getDataStoreNames() synchronized to prevent race conditions during cache refresh
- AbstractDataStore: Made alive field volatile to ensure proper visibility in multi-threaded scenarios
- FileListIndexUpdateCallbackImpl: Changed deleteUrlList from ArrayList to CopyOnWriteArrayList for thread-safe access from executor threads
**Resource Handling:**
- DataStoreFactory: Added null check for jarFiles array to prevent NullPointerException
- DataStoreFactory: Added existence check for XML configuration files before attempting to read them, reducing unnecessary exception handling
**Code Quality:**
- AbstractDataStore: Moved static logger declaration before instance fields following Java best practices
- FileListIndexUpdateCallbackImpl: Fixed incorrect StringUtil import (was using Apache POI's StringUtil instead of Fess's)
These improvements enhance the reliability and thread safety of the data store processing layer,
particularly important for concurrent crawling operations.
* Optimize deleteUrlList by replacing CopyOnWriteArrayList with ArrayList
Changed deleteUrlList from CopyOnWriteArrayList to ArrayList since all access
is already protected by synchronized(indexUpdateCallback) blocks. CopyOnWriteArrayList
creates a copy on every write operation which is unnecessary overhead when synchronization
is already in place.
Changes:
- Replaced CopyOnWriteArrayList with ArrayList for deleteUrlList
- Added synchronized block in commit() method for consistency
- Updated javadoc to clarify synchronization strategy
This improves performance for batch delete operations by eliminating unnecessary
array copying on each URL addition.
* Fix ProcessHelperTest timing issues by using long-running processes
Fixed two failing tests that were using 'echo' commands which complete
almost instantly, making it impossible to verify process running state:
1. test_startProcess_replaceExistingProcess:
- Changed from 'echo' to 'sleep 10' commands
- Added proper wait time before checking process state
- Added verification that only one process exists for the session
2. test_destroyProcess_withRunningProcess:
- Changed from 'echo' to 'sleep 10' command
- Simplified polling logic - just wait 100ms for process to start
- Updated exit code assertion (forcibly destroyed processes may have non-zero exit codes)
These changes ensure the tests can reliably verify that processes are running
before attempting to check their state or destroy them.
* Add comprehensive thread safety tests for data store improvements
Added extensive test coverage for the thread safety improvements made to
the data store handling layer.
DataStoreFactoryTest additions:
- test_add_concurrentAccess: Verifies ConcurrentHashMap handles concurrent additions
- test_getDataStore_concurrentWithAdd: Tests concurrent read/write operations
- test_getDataStoreNames_concurrentAccess: Verifies synchronized method correctness
- test_volatileFields_visibility: Ensures volatile fields are visible across threads
- test_cacheRefresh_withConcurrentReads: Validates cache refresh with concurrent access
- test_nullSafety_concurrentAccess: Tests null handling under concurrent load
AbstractDataStoreTest additions:
- test_aliveField_volatileVisibility: Verifies volatile alive field visibility
- test_stop_volatileVisibility: Tests stop() method sets alive and is visible
- test_stop_concurrentAccess: Validates concurrent calls to stop()
- test_aliveField_concurrentReadWrite: Tests concurrent read/write of alive field
- test_getName_concurrentAccess: Verifies getName() is thread-safe
FileListIndexUpdateCallbackImplTest additions:
- test_deleteUrlList_synchronizedAccess: Validates ArrayList with synchronized blocks
- test_deleteUrlList_concurrentReads: Tests concurrent reads from deleteUrlList
- test_deleteUrlList_clearOperation: Verifies clear() operation thread safety
- test_deleteUrlList_iteration: Tests iteration with proper synchronization
- test_deleteUrlList_isEmptyCheck: Validates isEmpty() check under concurrent access
These tests verify:
1. ConcurrentHashMap correctly handles concurrent operations in DataStoreFactory
2. Volatile fields ensure proper visibility across threads
3. Synchronized methods prevent race conditions during cache refresh
4. ArrayList works correctly when all access is properly synchronized
5. No ConcurrentModificationException occurs in any concurrent scenario
All tests use minimal or no sleep to maintain fast test execution time.
* Revert dataStoreMap from ConcurrentHashMap to LinkedHashMap
Based on feedback that dataStoreMap is not accessed by multiple threads,
reverted the ConcurrentHashMap change back to LinkedHashMap.
Changes:
- Reverted dataStoreMap from ConcurrentHashMap to LinkedHashMap
- Removed ConcurrentHashMap import
- Removed dataStoreMap-related concurrency tests:
* test_add_concurrentAccess
* test_getDataStore_concurrentWithAdd
* test_nullSafety_concurrentAccess
Kept volatile fields and synchronized getDataStoreNames():
- lastLoadedTime and dataStoreNames remain volatile (for cache visibility)
- getDataStoreNames() remains synchronized (for cache refresh safety)
- Related tests for getDataStoreNames() concurrency are maintained
The dataStoreMap is accessed in single-threaded context during application
initialization and data store registration.
---------
Co-authored-by: Claude <[email protected]>1 parent 44146b4 commit 083092a
File tree
7 files changed
+801
-504
lines changed- src
- main/java/org/codelibs/fess/ds
- callback
- test/java/org/codelibs/fess
- ds
- callback
- helper
7 files changed
+801
-504
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | 46 | | |
54 | 47 | | |
55 | 48 | | |
| |||
64 | 57 | | |
65 | 58 | | |
66 | 59 | | |
| 60 | + | |
67 | 61 | | |
68 | | - | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| 79 | + | |
79 | 80 | | |
80 | | - | |
| 81 | + | |
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| |||
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
133 | | - | |
| 134 | + | |
134 | 135 | | |
135 | 136 | | |
136 | 137 | | |
| |||
154 | 155 | | |
155 | 156 | | |
156 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
157 | 161 | | |
158 | 162 | | |
159 | 163 | | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
160 | 170 | | |
161 | 171 | | |
162 | 172 | | |
| |||
Lines changed: 10 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
93 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
94 | 97 | | |
95 | 98 | | |
96 | 99 | | |
| |||
585 | 588 | | |
586 | 589 | | |
587 | 590 | | |
588 | | - | |
589 | | - | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
590 | 595 | | |
591 | 596 | | |
592 | 597 | | |
| |||
Lines changed: 231 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
111 | 342 | | |
0 commit comments