• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/exist-core/src/main/java/org/exist/storage/sync/SyncTask.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 *
24
 * NOTE: Parts of this file contain code from 'The eXist-db Authors'.
25
 *       The original license header is included below.
26
 *
27
 * =====================================================================
28
 *
29
 * eXist-db Open Source Native XML Database
30
 * Copyright (C) 2001 The eXist-db Authors
31
 *
32
 * info@exist-db.org
33
 * http://www.exist-db.org
34
 *
35
 * This library is free software; you can redistribute it and/or
36
 * modify it under the terms of the GNU Lesser General Public
37
 * License as published by the Free Software Foundation; either
38
 * version 2.1 of the License, or (at your option) any later version.
39
 *
40
 * This library is distributed in the hope that it will be useful,
41
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43
 * Lesser General Public License for more details.
44
 *
45
 * You should have received a copy of the GNU Lesser General Public
46
 * License along with this library; if not, write to the Free Software
47
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
48
 */
49
package org.exist.storage.sync;
50

51
import java.nio.file.FileStore;
52
import java.nio.file.Path;
53
import java.util.Properties;
54

55
import com.evolvedbinary.j8fu.tuple.Tuple2;
56
import org.apache.logging.log4j.LogManager;
57
import org.apache.logging.log4j.Logger;
58
import org.exist.EXistException;
59
import org.exist.scheduler.JobDescription;
60
import org.exist.storage.BrokerPool;
61
import org.exist.storage.DBBroker;
62
import org.exist.storage.SystemTask;
63
import org.exist.storage.txn.Txn;
64
import org.exist.util.Configuration;
65
import org.exist.util.FileUtils;
66

67
import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple;
68

69
public class SyncTask implements SystemTask {
1✔
70

71
    private final static Logger LOG = LogManager.getLogger(SyncTask.class);
1✔
72

73
    private final static String JOB_NAME = "Sync";
1✔
74

75
    public static String getJobName() {
76
        return JOB_NAME;
1✔
77
    }
78

79
    public static String getJobGroup() {
80
        return JobDescription.EXIST_INTERNAL_GROUP;
×
81
    }
82

83
    private Path dataDir;
84
    private long diskSpaceMin;
85

86
    @Override
87
    public boolean afterCheckpoint() {
88
        // a checkpoint is created by the MAJOR_SYNC event
89
        return false;
1✔
90
    }
91

92
    @Override
93
    public String getName() {
94
        return getJobName();
1✔
95
    }
96

97
    @Override
98
    public void configure(final Configuration config, final Properties properties) throws EXistException {
99
        this.diskSpaceMin = 1024L * 1024L * config.getProperty(BrokerPool.DISK_SPACE_MIN_PROPERTY, BrokerPool.DEFAULT_DISK_SPACE_MIN);
1✔
100

101
        // fixme! - Shouldn't it be data dir AND journal dir we check
102
        // rather than EXIST_HOME? /ljo
103
        dataDir = (Path) config.getProperty(BrokerPool.PROPERTY_DATA_DIR);
1✔
104
        LOG.info("Using DATA_DIR: {}. Minimal disk space required for database to continue operations: {}mb", dataDir.toAbsolutePath().toString(), diskSpaceMin / 1024 / 1024);
1✔
105
        final long space = FileUtils.measureFileStore(dataDir, FileStore::getUsableSpace);
1✔
106
        LOG.info("Usable space on partition containing DATA_DIR: {}: {}mb", dataDir.toAbsolutePath().toString(), space / 1024 / 1024);
1✔
107
    }
1✔
108

109
    @Override
110
    public void execute(final DBBroker broker, final Txn transaction) throws EXistException {
111
        final BrokerPool pool = broker.getBrokerPool();
1✔
112
        final Tuple2<Boolean, Long> availableSpace = checkDiskSpace();
1✔
113
        if (!availableSpace._1) {
1!
114
            LOG.fatal("Partition containing DATA_DIR: {} is running out of disk space [minimum: {} free: {}]. Switching Elemental into read-only mode to prevent data loss!", dataDir.toAbsolutePath().toString(), diskSpaceMin, availableSpace._2);
×
115
            pool.setReadOnly();
×
116
        }
117

118
        if(System.currentTimeMillis() - pool.getLastMajorSync() >
1✔
119
                pool.getMajorSyncPeriod()) {
1!
120
            pool.sync(broker, Sync.MAJOR);
×
121
        } else {
×
122
            pool.sync(broker, Sync.MINOR);
1✔
123
        }
124
    }
1✔
125

126
    /**
127
     * @return A tuple, where the first value indicates if there is enough disk space, the
128
     * second value is the amount of usable space in bytes
129
     */
130
    private Tuple2<Boolean,Long> checkDiskSpace() {
131
        final long usableSpace = FileUtils.measureFileStore(dataDir, FileStore::getUsableSpace);
1✔
132
        //LOG.info("Usable space on partition containing DATA_DIR: " + dataDir.getAbsolutePath() + ": " + (space / 1024 / 1024) + "mb");
133
        final boolean enoughSpace = usableSpace > diskSpaceMin || usableSpace == -1;
1!
134
        return Tuple(enoughSpace, usableSpace);
1✔
135
    }
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc