Skip to content

Commit 1323c54

Browse files
committed
refactor(config): move LevelDB option reading from Storage to StorageConfig
Move default/defaultM/defaultL LevelDB option reading into StorageConfig, so Storage no longer touches Config directly. - Add DbOptionOverride with nullable boxed types for partial overrides - Fix cacheSize type from int to long to match LevelDB Options API - Remove dead externalIp(Config) bridge method - Remove setIfNeeded and Config field from Storage
1 parent d249675 commit 1323c54

4 files changed

Lines changed: 135 additions & 65 deletions

File tree

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

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.common.collect.Maps;
1919
import com.google.protobuf.ByteString;
2020
import com.typesafe.config.Config;
21-
import com.typesafe.config.ConfigObject;
2221
import java.io.File;
2322
import java.util.List;
2423
import java.util.Map;
@@ -47,7 +46,11 @@
4746
public class Storage {
4847

4948
private static final String DEFAULT_INDEX_SWITCH = "on";
50-
private Config storage;
49+
50+
// Optional per-tier LevelDB option overrides, read from StorageConfig bean
51+
private StorageConfig.DbOptionOverride defaultDbOption;
52+
private StorageConfig.DbOptionOverride defaultMDbOption;
53+
private StorageConfig.DbOptionOverride defaultLDbOption;
5154

5255
/**
5356
* Database storage directory: /path/to/{dbDirectory}
@@ -271,42 +274,6 @@ private static void applyPropertyOptions(StorageConfig.PropertyConfig pc, Option
271274
dbOptions.maxOpenFiles(pc.getMaxOpenFiles());
272275
}
273276

274-
// Keep old createProperty and setIfNeeded for setDefaultDbOptions which still
275-
// uses ConfigObject for dynamic default/defaultM/defaultL overrides
276-
private static void setIfNeeded(ConfigObject conf, Options dbOptions) {
277-
if (conf.containsKey("createIfMissing")) {
278-
dbOptions.createIfMissing(
279-
Boolean.parseBoolean(conf.get("createIfMissing").unwrapped().toString()));
280-
}
281-
if (conf.containsKey("paranoidChecks")) {
282-
dbOptions.paranoidChecks(
283-
Boolean.parseBoolean(conf.get("paranoidChecks").unwrapped().toString()));
284-
}
285-
if (conf.containsKey("verifyChecksums")) {
286-
dbOptions.verifyChecksums(
287-
Boolean.parseBoolean(conf.get("verifyChecksums").unwrapped().toString()));
288-
}
289-
if (conf.containsKey("compressionType")) {
290-
dbOptions.compressionType(CompressionType.getCompressionTypeByPersistentId(
291-
Integer.parseInt(conf.get("compressionType").unwrapped().toString())));
292-
}
293-
if (conf.containsKey("blockSize")) {
294-
dbOptions.blockSize(
295-
Integer.parseInt(conf.get("blockSize").unwrapped().toString()));
296-
}
297-
if (conf.containsKey("writeBufferSize")) {
298-
dbOptions.writeBufferSize(
299-
Integer.parseInt(conf.get("writeBufferSize").unwrapped().toString()));
300-
}
301-
if (conf.containsKey("cacheSize")) {
302-
dbOptions.cacheSize(
303-
Long.parseLong(conf.get("cacheSize").unwrapped().toString()));
304-
}
305-
if (conf.containsKey("maxOpenFiles")) {
306-
dbOptions.maxOpenFiles(
307-
Integer.parseInt(conf.get("maxOpenFiles").unwrapped().toString()));
308-
}
309-
}
310277

311278
/**
312279
* Set propertyMap of Storage object from Config via StorageConfig bean.
@@ -339,28 +306,59 @@ public void deleteAllStoragePaths() {
339306
}
340307

341308
/**
342-
* Accepts raw storage Config sub-tree because default/defaultM/defaultL are
343-
* optional nested objects with dynamic LevelDB Option fields that
344-
* ConfigBeanFactory cannot bind to fixed bean fields.
309+
* Initialize default LevelDB options and store optional per-tier overrides
310+
* from StorageConfig bean (no raw Config needed).
345311
*/
346-
public void setDefaultDbOptions(final Config storageSection) {
312+
public void setDefaultDbOptions(StorageConfig sc) {
347313
this.defaultDbOptions = DbOptionalsUtils.createDefaultDbOptions();
348-
storage = storageSection;
314+
this.defaultDbOption = sc.getDefaultDbOption();
315+
this.defaultMDbOption = sc.getDefaultMDbOption();
316+
this.defaultLDbOption = sc.getDefaultLDbOption();
349317
}
350318

351319
public Options newDefaultDbOptions(String name) {
352320
Options options = DbOptionalsUtils.newDefaultDbOptions(name, this.defaultDbOptions);
353321

354-
if (storage.hasPath("default")) {
355-
setIfNeeded(storage.getObject("default"), options);
322+
if (defaultDbOption != null) {
323+
applyDbOptionOverride(defaultDbOption, options);
356324
}
357-
if (storage.hasPath("defaultM") && DbOptionalsUtils.DB_M.contains(name)) {
358-
setIfNeeded(storage.getObject("defaultM"), options);
325+
if (defaultMDbOption != null && DbOptionalsUtils.DB_M.contains(name)) {
326+
applyDbOptionOverride(defaultMDbOption, options);
359327
}
360-
if (storage.hasPath("defaultL") && DbOptionalsUtils.DB_L.contains(name)) {
361-
setIfNeeded(storage.getObject("defaultL"), options);
328+
if (defaultLDbOption != null && DbOptionalsUtils.DB_L.contains(name)) {
329+
applyDbOptionOverride(defaultLDbOption, options);
362330
}
363331

364332
return options;
365333
}
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+
}
366364
}

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

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.typesafe.config.Config;
44
import com.typesafe.config.ConfigBeanFactory;
5+
import com.typesafe.config.ConfigObject;
56
import java.util.ArrayList;
67
import java.util.List;
78
import lombok.Getter;
@@ -36,9 +37,8 @@ public class StorageConfig {
3637
@Setter(lombok.AccessLevel.NONE)
3738
private Object merkleRoot;
3839

39-
// LevelDB per-database option overrides (default, defaultM, defaultL)
40-
// These are not bean-bound because they are optional nested objects with
41-
// dynamic usage. Read manually in fromConfig().
40+
// Raw storage config sub-tree, kept for setCacheStrategies/setDbRoots which
41+
// have dynamic keys that ConfigBeanFactory cannot bind.
4242
@Getter(lombok.AccessLevel.NONE)
4343
@Setter(lombok.AccessLevel.NONE)
4444
private Config rawStorageConfig;
@@ -47,6 +47,22 @@ public Config getRawStorageConfig() {
4747
return rawStorageConfig;
4848
}
4949

50+
// LevelDB per-database option overrides (default, defaultM, defaultL).
51+
// Excluded from auto-binding: optional partial overrides that ConfigBeanFactory cannot handle.
52+
@Getter(lombok.AccessLevel.NONE)
53+
@Setter(lombok.AccessLevel.NONE)
54+
private DbOptionOverride defaultDbOption;
55+
@Getter(lombok.AccessLevel.NONE)
56+
@Setter(lombok.AccessLevel.NONE)
57+
private DbOptionOverride defaultMDbOption;
58+
@Getter(lombok.AccessLevel.NONE)
59+
@Setter(lombok.AccessLevel.NONE)
60+
private DbOptionOverride defaultLDbOption;
61+
62+
public DbOptionOverride getDefaultDbOption() { return defaultDbOption; }
63+
public DbOptionOverride getDefaultMDbOption() { return defaultMDbOption; }
64+
public DbOptionOverride getDefaultLDbOption() { return defaultLDbOption; }
65+
5066
@Getter
5167
@Setter
5268
public static class DbConfig {
@@ -158,7 +174,7 @@ public static class PropertyConfig {
158174
private int compressionType = 1;
159175
private int blockSize = 4096;
160176
private int writeBufferSize = 10485760;
161-
private int cacheSize = 10485760;
177+
private long cacheSize = 10485760;
162178
private int maxOpenFiles = 100;
163179
}
164180

@@ -168,8 +184,71 @@ public static StorageConfig fromConfig(Config config) {
168184
Config section = config.getConfig("storage");
169185

170186
StorageConfig sc = ConfigBeanFactory.create(section, StorageConfig.class);
171-
// Keep raw config for legacy LevelDB per-database option overrides (default, defaultM, defaultL)
172187
sc.rawStorageConfig = section;
188+
189+
// Read optional LevelDB option overrides (default, defaultM, defaultL).
190+
sc.defaultDbOption = readDbOption(section, "default");
191+
sc.defaultMDbOption = readDbOption(section, "defaultM");
192+
sc.defaultLDbOption = readDbOption(section, "defaultL");
173193
return sc;
174194
}
195+
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+
211+
// Read optional LevelDB option override (default/defaultM/defaultL).
212+
// Not bean-bound: users may only set a subset of keys (e.g. just maxOpenFiles),
213+
// ConfigBeanFactory requires all fields present so partial overrides would fail.
214+
private static DbOptionOverride readDbOption(Config section, String key) {
215+
if (!section.hasPath(key)) {
216+
return null;
217+
}
218+
ConfigObject conf = section.getObject(key);
219+
DbOptionOverride o = new DbOptionOverride();
220+
if (conf.containsKey("createIfMissing")) {
221+
o.setCreateIfMissing(
222+
Boolean.parseBoolean(conf.get("createIfMissing").unwrapped().toString()));
223+
}
224+
if (conf.containsKey("paranoidChecks")) {
225+
o.setParanoidChecks(
226+
Boolean.parseBoolean(conf.get("paranoidChecks").unwrapped().toString()));
227+
}
228+
if (conf.containsKey("verifyChecksums")) {
229+
o.setVerifyChecksums(
230+
Boolean.parseBoolean(conf.get("verifyChecksums").unwrapped().toString()));
231+
}
232+
if (conf.containsKey("compressionType")) {
233+
o.setCompressionType(
234+
Integer.parseInt(conf.get("compressionType").unwrapped().toString()));
235+
}
236+
if (conf.containsKey("blockSize")) {
237+
o.setBlockSize(
238+
Integer.parseInt(conf.get("blockSize").unwrapped().toString()));
239+
}
240+
if (conf.containsKey("writeBufferSize")) {
241+
o.setWriteBufferSize(
242+
Integer.parseInt(conf.get("writeBufferSize").unwrapped().toString()));
243+
}
244+
if (conf.containsKey("cacheSize")) {
245+
o.setCacheSize(
246+
Long.parseLong(conf.get("cacheSize").unwrapped().toString()));
247+
}
248+
if (conf.containsKey("maxOpenFiles")) {
249+
o.setMaxOpenFiles(
250+
Integer.parseInt(conf.get("maxOpenFiles").unwrapped().toString()));
251+
}
252+
return o;
253+
}
175254
}

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ private static void applyStorageConfig(StorageConfig sc) {
274274
// Dynamic nested objects use StorageConfig's raw storage sub-tree
275275
// setDefaultDbOptions must be called before setPropertyMapFromBean because
276276
// createPropertyFromBean calls newDefaultDbOptions which needs defaultDbOptions initialized
277-
PARAMETER.storage.setDefaultDbOptions(sc.getRawStorageConfig());
277+
PARAMETER.storage.setDefaultDbOptions(sc);
278278
PARAMETER.storage.setPropertyMapFromBean(sc.getProperties());
279279
PARAMETER.storage.setCacheStrategies(sc.getRawStorageConfig());
280280
PARAMETER.storage.setDbRoots(sc.getRawStorageConfig());
@@ -1157,11 +1157,6 @@ private static void logEmptyError(String arg) {
11571157
// createTriggerConfig removed — logic moved to applyEventConfig()
11581158
// getEventFilter removed — logic moved to applyEventConfig()
11591159

1160-
// Kept for backward compatibility — test code calls via reflection with Config param
1161-
private static void externalIp(final com.typesafe.config.Config config) {
1162-
externalIp(NodeConfig.fromConfig(config));
1163-
}
1164-
11651160
private static void externalIp(NodeConfig nodeConfig) {
11661161
String externalIp = nodeConfig.getDiscoveryExternalIp();
11671162
if (StringUtils.isEmpty(externalIp)) {

framework/src/test/java/org/tron/core/config/args/ArgsTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,12 @@ public void testIpFromLibP2p()
143143
String configuredExternalIp = parameter.getNodeExternalIp();
144144
Assert.assertEquals("46.168.1.1", configuredExternalIp);
145145

146-
Config config = Configuration.getByFileName(TestConstants.TEST_CONF);
147-
Config config3 = config.withoutPath("node.discovery.external.ip");
148-
149146
CommonParameter.getInstance().setNodeExternalIp(null);
150147

151-
Method method2 = Args.class.getDeclaredMethod("externalIp", Config.class);
148+
NodeConfig nc = new NodeConfig();
149+
Method method2 = Args.class.getDeclaredMethod("externalIp", NodeConfig.class);
152150
method2.setAccessible(true);
153-
method2.invoke(Args.class, config3);
151+
method2.invoke(Args.class, nc);
154152

155153
Assert.assertNotEquals(configuredExternalIp, parameter.getNodeExternalIp());
156154
}

0 commit comments

Comments
 (0)