Skip to content

Commit 66f39a2

Browse files
committed
fix(config): use boxed types for partial LevelDB option overrides
- Add DbOptionOverride with nullable boxed types to distinguish "user set this" from "default value", fixing full-override bug - Fix cacheSize type from int to long to match LevelDB Options API
1 parent 6b7e46e commit 66f39a2

File tree

2 files changed

+70
-25
lines changed

2 files changed

+70
-25
lines changed

common/src/main/java/org/tron/core/config/args/Storage.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public class Storage {
4848
private static final String DEFAULT_INDEX_SWITCH = "on";
4949

5050
// Optional per-tier LevelDB option overrides, read from StorageConfig bean
51-
private StorageConfig.PropertyConfig defaultDbOption;
52-
private StorageConfig.PropertyConfig defaultMDbOption;
53-
private StorageConfig.PropertyConfig defaultLDbOption;
51+
private StorageConfig.DbOptionOverride defaultDbOption;
52+
private StorageConfig.DbOptionOverride defaultMDbOption;
53+
private StorageConfig.DbOptionOverride defaultLDbOption;
5454

5555
/**
5656
* Database storage directory: /path/to/{dbDirectory}
@@ -320,15 +320,45 @@ public Options newDefaultDbOptions(String name) {
320320
Options options = DbOptionalsUtils.newDefaultDbOptions(name, this.defaultDbOptions);
321321

322322
if (defaultDbOption != null) {
323-
applyPropertyOptions(defaultDbOption, options);
323+
applyDbOptionOverride(defaultDbOption, options);
324324
}
325325
if (defaultMDbOption != null && DbOptionalsUtils.DB_M.contains(name)) {
326-
applyPropertyOptions(defaultMDbOption, options);
326+
applyDbOptionOverride(defaultMDbOption, options);
327327
}
328328
if (defaultLDbOption != null && DbOptionalsUtils.DB_L.contains(name)) {
329-
applyPropertyOptions(defaultLDbOption, options);
329+
applyDbOptionOverride(defaultLDbOption, options);
330330
}
331331

332332
return options;
333333
}
334+
335+
// Apply only user-specified overrides (non-null fields) to LevelDB Options.
336+
private static void applyDbOptionOverride(
337+
StorageConfig.DbOptionOverride o, Options dbOptions) {
338+
if (o.getCreateIfMissing() != null) {
339+
dbOptions.createIfMissing(o.getCreateIfMissing());
340+
}
341+
if (o.getParanoidChecks() != null) {
342+
dbOptions.paranoidChecks(o.getParanoidChecks());
343+
}
344+
if (o.getVerifyChecksums() != null) {
345+
dbOptions.verifyChecksums(o.getVerifyChecksums());
346+
}
347+
if (o.getCompressionType() != null) {
348+
dbOptions.compressionType(
349+
CompressionType.getCompressionTypeByPersistentId(o.getCompressionType()));
350+
}
351+
if (o.getBlockSize() != null) {
352+
dbOptions.blockSize(o.getBlockSize());
353+
}
354+
if (o.getWriteBufferSize() != null) {
355+
dbOptions.writeBufferSize(o.getWriteBufferSize());
356+
}
357+
if (o.getCacheSize() != null) {
358+
dbOptions.cacheSize(o.getCacheSize());
359+
}
360+
if (o.getMaxOpenFiles() != null) {
361+
dbOptions.maxOpenFiles(o.getMaxOpenFiles());
362+
}
363+
}
334364
}

common/src/main/java/org/tron/core/config/args/StorageConfig.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ public Config getRawStorageConfig() {
5151
// Excluded from auto-binding: optional partial overrides that ConfigBeanFactory cannot handle.
5252
@Getter(lombok.AccessLevel.NONE)
5353
@Setter(lombok.AccessLevel.NONE)
54-
private PropertyConfig defaultDbOption;
54+
private DbOptionOverride defaultDbOption;
5555
@Getter(lombok.AccessLevel.NONE)
5656
@Setter(lombok.AccessLevel.NONE)
57-
private PropertyConfig defaultMDbOption;
57+
private DbOptionOverride defaultMDbOption;
5858
@Getter(lombok.AccessLevel.NONE)
5959
@Setter(lombok.AccessLevel.NONE)
60-
private PropertyConfig defaultLDbOption;
60+
private DbOptionOverride defaultLDbOption;
6161

62-
public PropertyConfig getDefaultDbOption() { return defaultDbOption; }
63-
public PropertyConfig getDefaultMDbOption() { return defaultMDbOption; }
64-
public PropertyConfig getDefaultLDbOption() { return defaultLDbOption; }
62+
public DbOptionOverride getDefaultDbOption() { return defaultDbOption; }
63+
public DbOptionOverride getDefaultMDbOption() { return defaultMDbOption; }
64+
public DbOptionOverride getDefaultLDbOption() { return defaultLDbOption; }
6565

6666
@Getter
6767
@Setter
@@ -174,7 +174,7 @@ public static class PropertyConfig {
174174
private int compressionType = 1;
175175
private int blockSize = 4096;
176176
private int writeBufferSize = 10485760;
177-
private int cacheSize = 10485760;
177+
private long cacheSize = 10485760;
178178
private int maxOpenFiles = 100;
179179
}
180180

@@ -193,47 +193,62 @@ public static StorageConfig fromConfig(Config config) {
193193
return sc;
194194
}
195195

196+
// Partial LevelDB option override for default/defaultM/defaultL.
197+
// Uses boxed types so null means "not set by user, keep existing value".
198+
@Getter
199+
@Setter
200+
public static class DbOptionOverride {
201+
private Boolean createIfMissing;
202+
private Boolean paranoidChecks;
203+
private Boolean verifyChecksums;
204+
private Integer compressionType;
205+
private Integer blockSize;
206+
private Integer writeBufferSize;
207+
private Long cacheSize;
208+
private Integer maxOpenFiles;
209+
}
210+
196211
// Read optional LevelDB option override (default/defaultM/defaultL).
197212
// Not bean-bound: users may only set a subset of keys (e.g. just maxOpenFiles),
198213
// ConfigBeanFactory requires all fields present so partial overrides would fail.
199-
private static PropertyConfig readDbOption(Config section, String key) {
214+
private static DbOptionOverride readDbOption(Config section, String key) {
200215
if (!section.hasPath(key)) {
201216
return null;
202217
}
203218
ConfigObject conf = section.getObject(key);
204-
PropertyConfig pc = new PropertyConfig();
219+
DbOptionOverride o = new DbOptionOverride();
205220
if (conf.containsKey("createIfMissing")) {
206-
pc.setCreateIfMissing(
221+
o.setCreateIfMissing(
207222
Boolean.parseBoolean(conf.get("createIfMissing").unwrapped().toString()));
208223
}
209224
if (conf.containsKey("paranoidChecks")) {
210-
pc.setParanoidChecks(
225+
o.setParanoidChecks(
211226
Boolean.parseBoolean(conf.get("paranoidChecks").unwrapped().toString()));
212227
}
213228
if (conf.containsKey("verifyChecksums")) {
214-
pc.setVerifyChecksums(
229+
o.setVerifyChecksums(
215230
Boolean.parseBoolean(conf.get("verifyChecksums").unwrapped().toString()));
216231
}
217232
if (conf.containsKey("compressionType")) {
218-
pc.setCompressionType(
233+
o.setCompressionType(
219234
Integer.parseInt(conf.get("compressionType").unwrapped().toString()));
220235
}
221236
if (conf.containsKey("blockSize")) {
222-
pc.setBlockSize(
237+
o.setBlockSize(
223238
Integer.parseInt(conf.get("blockSize").unwrapped().toString()));
224239
}
225240
if (conf.containsKey("writeBufferSize")) {
226-
pc.setWriteBufferSize(
241+
o.setWriteBufferSize(
227242
Integer.parseInt(conf.get("writeBufferSize").unwrapped().toString()));
228243
}
229244
if (conf.containsKey("cacheSize")) {
230-
pc.setCacheSize(
231-
Integer.parseInt(conf.get("cacheSize").unwrapped().toString()));
245+
o.setCacheSize(
246+
Long.parseLong(conf.get("cacheSize").unwrapped().toString()));
232247
}
233248
if (conf.containsKey("maxOpenFiles")) {
234-
pc.setMaxOpenFiles(
249+
o.setMaxOpenFiles(
235250
Integer.parseInt(conf.get("maxOpenFiles").unwrapped().toString()));
236251
}
237-
return pc;
252+
return o;
238253
}
239254
}

0 commit comments

Comments
 (0)