|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.rdf4j.sail.lucene.impl; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collection; |
| 16 | +import java.util.HashSet; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.Executors; |
| 19 | +import java.util.concurrent.ScheduledExecutorService; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | +import java.util.concurrent.atomic.AtomicReference; |
| 23 | + |
| 24 | +import org.apache.lucene.store.Directory; |
| 25 | +import org.apache.lucene.store.FilterDirectory; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +/** |
| 30 | + * Wrapper around a Lucene Directory that batches sync and metadata sync calls to be executed at a fixed interval. |
| 31 | + * |
| 32 | + * @author Piotr Sowiński |
| 33 | + */ |
| 34 | +class DelayedSyncDirectoryWrapper extends FilterDirectory { |
| 35 | + |
| 36 | + final private Logger logger = LoggerFactory.getLogger(getClass()); |
| 37 | + |
| 38 | + final private ScheduledExecutorService scheduler; |
| 39 | + |
| 40 | + final private AtomicBoolean needsMetadataSync = new AtomicBoolean(false); |
| 41 | + |
| 42 | + final private AtomicReference<IOException> lastSyncException = new AtomicReference<>(null); |
| 43 | + |
| 44 | + final private HashSet<String> pendingSyncs = new HashSet<>(); |
| 45 | + |
| 46 | + final private int maxPendingSyncs; |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a new instance of LuceneDirectoryWrapper. |
| 50 | + * |
| 51 | + * @param in the underlying directory |
| 52 | + * @param fsyncInterval the interval in milliseconds writes after which a fsync is performed |
| 53 | + * @param maxPendingSyncs the maximum number of pending syncs to accumulate before forcing a sync |
| 54 | + */ |
| 55 | + DelayedSyncDirectoryWrapper(Directory in, long fsyncInterval, int maxPendingSyncs) { |
| 56 | + super(in); |
| 57 | + assert fsyncInterval > 0; |
| 58 | + assert maxPendingSyncs > 0; |
| 59 | + this.maxPendingSyncs = maxPendingSyncs; |
| 60 | + scheduler = Executors.newScheduledThreadPool(1); |
| 61 | + scheduler.scheduleAtFixedRate( |
| 62 | + this::doSync, |
| 63 | + fsyncInterval, |
| 64 | + fsyncInterval, |
| 65 | + TimeUnit.MILLISECONDS |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + private void doSync() { |
| 70 | + List<String> toSync; |
| 71 | + synchronized (pendingSyncs) { |
| 72 | + toSync = new ArrayList<>(pendingSyncs); |
| 73 | + pendingSyncs.clear(); |
| 74 | + } |
| 75 | + if (!toSync.isEmpty()) { |
| 76 | + try { |
| 77 | + super.sync(toSync); |
| 78 | + } catch (IOException e) { |
| 79 | + lastSyncException.set(e); |
| 80 | + logger.error("IO error during a periodic sync of Lucene index files", e); |
| 81 | + } |
| 82 | + } |
| 83 | + if (this.needsMetadataSync.getAndSet(false)) { |
| 84 | + try { |
| 85 | + super.syncMetaData(); |
| 86 | + } catch (IOException e) { |
| 87 | + lastSyncException.set(e); |
| 88 | + logger.error("IO error during a periodic sync of Lucene index metadata", e); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void sync(Collection<String> names) throws IOException { |
| 95 | + final IOException ex = lastSyncException.getAndSet(null); |
| 96 | + if (ex != null) { |
| 97 | + // Rethrow the last exception if there was one. |
| 98 | + // This will fail the current transaction, and not the one that caused the original exception. |
| 99 | + // But there is no other way to notify the caller of the error, as the sync is done asynchronously. |
| 100 | + throw ex; |
| 101 | + } |
| 102 | + synchronized (pendingSyncs) { |
| 103 | + pendingSyncs.addAll(names); |
| 104 | + if (pendingSyncs.size() >= maxPendingSyncs) { |
| 105 | + // If we have accumulated too many pending syncs, do a sync right away |
| 106 | + // to avoid excessive memory usage |
| 107 | + doSync(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void syncMetaData() throws IOException { |
| 114 | + needsMetadataSync.set(true); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void close() throws IOException { |
| 119 | + // Finish the current sync task, if in progress and then shut down |
| 120 | + try { |
| 121 | + scheduler.shutdown(); |
| 122 | + } finally { |
| 123 | + // Do a final sync of any remaining files |
| 124 | + try { |
| 125 | + doSync(); |
| 126 | + } finally { |
| 127 | + super.close(); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments