Skip to content

Commit d3c07cd

Browse files
authored
Remove GlobalParams (#34)
This PR removes the GlobalParams type and moves the executor ID and app version getters to the DBOS Executor type. The logic to calculate the app version from code remains in a utility class to avoid cluttering up DBOS Executor with logic that is only used in one place. fixes #33 ~Note, still two tests disabled which is why I opened this in draft~
1 parent f3f4d10 commit d3c07cd

7 files changed

Lines changed: 61 additions & 104 deletions

File tree

src/main/java/dev/dbos/transact/DBOS.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import dev.dbos.transact.queue.QueueService;
1919
import dev.dbos.transact.queue.RateLimit;
2020
import dev.dbos.transact.scheduled.SchedulerService;
21-
import dev.dbos.transact.utils.GlobalParams;
2221
import dev.dbos.transact.workflow.*;
2322

2423
import java.util.List;
@@ -208,9 +207,10 @@ public Queue build() {
208207
}
209208

210209
public void launch() {
211-
GlobalParams gp = GlobalParams.getInstance(dbosExecutor);
212-
logger.info("Executor ID: {}", gp.getExecutorId());
213-
logger.info("Application version: " + gp.getAppVersion());
210+
dbosExecutor.start();
211+
212+
logger.info("Executor ID: {}", dbosExecutor.getExecutorId());
213+
logger.info("Application version: {}", dbosExecutor.getAppVersion());
214214

215215
queueService.start();
216216

src/main/java/dev/dbos/transact/execution/DBOSExecutor.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import dev.dbos.transact.queue.QueueService;
1616
import dev.dbos.transact.tempworkflows.InternalWorkflowsService;
1717
import dev.dbos.transact.tempworkflows.InternalWorkflowsServiceImpl;
18-
import dev.dbos.transact.utils.GlobalParams;
18+
import dev.dbos.transact.utils.AppVersionComputer;
1919
import dev.dbos.transact.workflow.ForkOptions;
2020
import dev.dbos.transact.workflow.WorkflowHandle;
2121
import dev.dbos.transact.workflow.WorkflowState;
@@ -40,6 +40,9 @@
4040
public class DBOSExecutor {
4141

4242
private final DBOSConfig config;
43+
private String appVersion;
44+
private String executorId;
45+
4346
private SystemDatabase systemDatabase;
4447
private ExecutorService executorService;
4548
private final ScheduledExecutorService timeoutScheduler = Executors.newScheduledThreadPool(2);
@@ -60,17 +63,31 @@ public String getAppName() {
6063
}
6164

6265
public String getExecutorId() {
63-
return GlobalParams.getInstance().getExecutorId();
66+
return this.executorId;
6467
}
6568

6669
public String getAppVersion() {
67-
return GlobalParams.getInstance().getAppVersion();
70+
return this.appVersion;
6871
}
6972

7073
public void setQueueService(QueueService queueService) {
7174
this.queueService = queueService;
7275
}
7376

77+
public void start() {
78+
79+
this.executorId = System.getenv("DBOS__VMID");
80+
if (this.executorId == null) {
81+
this.executorId = "local";
82+
}
83+
84+
this.appVersion = System.getenv("DBOS__APPVERSION");
85+
if (this.appVersion == null) {
86+
Set<Class<?>> registeredClasses = this.getRegisteredClasses();
87+
this.appVersion = AppVersionComputer.computeAppVersion(registeredClasses);
88+
}
89+
}
90+
7491
public void shutdown() {
7592
workflowRegistry = null;
7693
executorService.shutdownNow();
@@ -111,8 +128,7 @@ public WorkflowInitResult preInvokeWorkflow(String workflowName, String classNam
111128
WorkflowStatusInternal workflowStatusInternal = new WorkflowStatusInternal(workflowId,
112129
status, workflowName, className, null, null, null, null, null, null, null, null,
113130
queueName,
114-
GlobalParams.getInstance().getExecutorId(), GlobalParams.getInstance()
115-
.getAppVersion(),
131+
this.getExecutorId(), this.getAppVersion(),
116132
null, 0,
117133
workflowTimeoutMs, workflowDeadlineEpoch, null, 1, inputString);
118134

src/main/java/dev/dbos/transact/execution/RecoveryService.java

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

33
import dev.dbos.transact.database.SystemDatabase;
44
import dev.dbos.transact.exceptions.WorkflowFunctionNotFoundException;
5-
import dev.dbos.transact.utils.GlobalParams;
65
import dev.dbos.transact.workflow.WorkflowHandle;
76
import dev.dbos.transact.workflow.internal.GetPendingWorkflowsOutput;
87

@@ -52,9 +51,8 @@ public List<WorkflowHandle> recoverWorkflows(
5251
}
5352

5453
public List<GetPendingWorkflowsOutput> getPendingWorkflows() throws SQLException {
55-
return systemDatabase.getPendingWorkflows(GlobalParams.getInstance()
56-
.getExecutorId(),
57-
GlobalParams.getInstance().getAppVersion());
54+
return systemDatabase.getPendingWorkflows(dbosExecutor.getExecutorId(),
55+
dbosExecutor.getAppVersion());
5856
}
5957

6058
/**

src/main/java/dev/dbos/transact/queue/QueueService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import dev.dbos.transact.DBOS;
88
import dev.dbos.transact.database.SystemDatabase;
99
import dev.dbos.transact.execution.DBOSExecutor;
10-
import dev.dbos.transact.utils.GlobalParams;
1110

1211
import java.util.List;
1312
import java.util.concurrent.CountDownLatch;
@@ -80,8 +79,8 @@ private void pollForWorkflows() {
8079
try {
8180

8281
List<String> workflowIds = systemDatabase.getAndStartQueuedWorkflows(queue,
83-
GlobalParams.getInstance().getExecutorId(),
84-
GlobalParams.getInstance().getAppVersion());
82+
dbosExecutor.getExecutorId(),
83+
dbosExecutor.getAppVersion());
8584

8685
for (String id : workflowIds) {
8786
dbosExecutor.executeWorkflowById(id);

src/main/java/dev/dbos/transact/utils/GlobalParams.java renamed to src/main/java/dev/dbos/transact/utils/AppVersionComputer.java

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package dev.dbos.transact.utils;
22

3-
import dev.dbos.transact.execution.DBOSExecutor;
4-
53
import java.io.InputStream;
64
import java.security.MessageDigest;
75
import java.util.*;
@@ -10,75 +8,21 @@
108
import org.slf4j.Logger;
119
import org.slf4j.LoggerFactory;
1210

13-
public class GlobalParams {
14-
15-
static Logger logger = LoggerFactory.getLogger(GlobalParams.class);
16-
17-
private String appVersion;
18-
private String executorId;
11+
public class AppVersionComputer {
1912

20-
private DBOSExecutor dbosExecutor;
21-
private static GlobalParams instance;
22-
23-
private GlobalParams(DBOSExecutor de) {
24-
this.dbosExecutor = de;
25-
appVersion = System.getenv("DBOS__APPVERSION") == null
26-
? generateAppVersion()
27-
: System.getenv("DBOS__APPVERSION");
28-
executorId = System.getenv("DBOS__VMID") == null
29-
? "local"
30-
: System.getenv("DBOS__VMID");
31-
}
32-
33-
public static synchronized GlobalParams getInstance(DBOSExecutor de) {
34-
if (instance != null) {
35-
return instance;
36-
}
37-
instance = new GlobalParams(de);
38-
return instance;
39-
}
40-
41-
public static synchronized GlobalParams getInstance() {
42-
if (instance != null) {
43-
return instance;
44-
} else {
45-
throw new RuntimeException("Run dbos launch first.");
46-
}
47-
48-
}
49-
50-
public String getAppVersion() {
51-
return appVersion;
52-
}
53-
54-
public String getExecutorId() {
55-
return executorId;
56-
}
57-
58-
private String generateAppVersion() {
59-
// return "DEFAULT_APP_VERSION";
60-
return computeAppVersionSimplified();
61-
}
62-
63-
private String computeAppVersionSimplified() {
64-
String manualVersion = System.getenv("DBOS__APPVERSION");
65-
if (manualVersion != null && !manualVersion.trim().isEmpty()) {
66-
return manualVersion.trim();
67-
}
13+
static Logger logger = LoggerFactory.getLogger(AppVersionComputer.class);
6814

15+
public static String computeAppVersion(Set<Class<?>> registeredClasses) {
6916
try {
7017
MessageDigest hasher = MessageDigest.getInstance("SHA-256");
7118

72-
// Get unique target classes
73-
Set<Class<?>> uniqueClasses = dbosExecutor.getRegisteredClasses();
74-
7519
/*
7620
* registry.values().stream() .map(wrapper -> wrapper.target.getClass())
7721
* .collect(Collectors.toSet());
7822
*/
7923

8024
// Sort by class name for deterministic ordering
81-
List<Class<?>> sortedClasses = uniqueClasses.stream()
25+
List<Class<?>> sortedClasses = registeredClasses.stream()
8226
.sorted(Comparator.comparing(Class::getName))
8327
.collect(Collectors.toList());
8428

@@ -102,7 +46,7 @@ private String computeAppVersionSimplified() {
10246
/**
10347
* Gets a hash of the class bytecode.
10448
*/
105-
private String getClassBytecodeHash(Class<?> clazz) {
49+
private static String getClassBytecodeHash(Class<?> clazz) {
10650
try {
10751
// Get the class file as a resource
10852
String className = clazz.getName().replace('.', '/') + ".class";
@@ -137,7 +81,7 @@ private String getClassBytecodeHash(Class<?> clazz) {
13781
}
13882
}
13983

140-
private String bytesToHex(byte[] bytes) {
84+
private static String bytesToHex(byte[] bytes) {
14185
StringBuilder hexString = new StringBuilder();
14286
for (byte b : bytes) {
14387
String hex = Integer.toHexString(0xff & b);
@@ -149,7 +93,7 @@ private String bytesToHex(byte[] bytes) {
14993
return hexString.toString();
15094
}
15195

152-
private String getFallbackVersion() {
96+
private static String getFallbackVersion() {
15397
return "unknown-" + System.currentTimeMillis();
15498
}
15599
}

src/test/java/dev/dbos/transact/execution/RecoveryServiceTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class RecoveryServiceTest {
3636
private static SystemDatabase systemDatabase;
3737
private DBOSExecutor dbosExecutor;
3838
private RecoveryService recoveryService;
39+
private ExecutingService executingService;
3940
Logger logger = LoggerFactory.getLogger(RecoveryServiceTest.class);
4041

4142
@BeforeAll
@@ -54,6 +55,10 @@ void setUp() throws SQLException {
5455
dbosExecutor = new DBOSExecutor(dbosConfig, systemDatabase);
5556
recoveryService = new RecoveryService(dbosExecutor, systemDatabase);
5657
dbos = DBOS.initialize(dbosConfig, systemDatabase, dbosExecutor, null, null);
58+
executingService = dbos.<ExecutingService>Workflow()
59+
.interfaceClass(ExecutingService.class).implementation(new ExecutingServiceImpl())
60+
.build();
61+
5762
dbos.launch();
5863
}
5964

@@ -65,10 +70,6 @@ void afterEachTest() throws SQLException {
6570
@Test
6671
void recoverWorkflows() throws Exception {
6772

68-
ExecutingService executingService = dbos.<ExecutingService>Workflow()
69-
.interfaceClass(ExecutingService.class).implementation(new ExecutingServiceImpl())
70-
.build();
71-
7273
String wfid = "wf-123";
7374
try (SetWorkflowID id = new SetWorkflowID(wfid)) {
7475
executingService.workflowMethod("test-item");
@@ -119,10 +120,6 @@ void recoverWorkflows() throws Exception {
119120
@Test
120121
public void recoveryThreadTest() throws SQLException {
121122

122-
ExecutingService executingService = dbos.<ExecutingService>Workflow()
123-
.interfaceClass(ExecutingService.class).implementation(new ExecutingServiceImpl())
124-
.build();
125-
126123
String wfid = "wf-123";
127124
try (SetWorkflowID id = new SetWorkflowID(wfid)) {
128125
executingService.workflowMethod("test-item");

src/test/java/dev/dbos/transact/queue/QueuesTest.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import dev.dbos.transact.database.SystemDatabase;
1313
import dev.dbos.transact.execution.DBOSExecutor;
1414
import dev.dbos.transact.utils.DBUtils;
15-
import dev.dbos.transact.utils.GlobalParams;
1615
import dev.dbos.transact.workflow.WorkflowHandle;
1716
import dev.dbos.transact.workflow.WorkflowState;
1817
import dev.dbos.transact.workflow.WorkflowStatus;
@@ -322,6 +321,8 @@ public void testLimiter() throws Exception {
322321

323322
@Test
324323
public void testWorkerConcurrency() throws Exception {
324+
String executorId = dbosExecutor.getExecutorId();
325+
String appVersion = dbosExecutor.getAppVersion();
325326

326327
queueService.stop();
327328
while (!queueService.isStopped()) {
@@ -336,9 +337,9 @@ public void testWorkerConcurrency() throws Exception {
336337
WorkflowState.SUCCESS, "OrderProcessingWorkflow",
337338
"com.example.workflows.OrderWorkflow", "prod-config", "user123@example.com",
338339
"admin", "admin,operator", "{\"result\":\"success\"}", null,
339-
System.currentTimeMillis() - 3600000, System.currentTimeMillis(), "QwithWCLimit",
340-
GlobalParams.getInstance().getExecutorId(), GlobalParams.getInstance()
341-
.getAppVersion(),
340+
System.currentTimeMillis() - 3600000, System.currentTimeMillis(),
341+
"QwithWCLimit",
342+
executorId, appVersion,
342343
"order-app-123", 0,
343344
300000l, System.currentTimeMillis() + 2400000, "dedup-112233", 1,
344345
"{\"orderId\":\"ORD-12345\"}");
@@ -357,16 +358,16 @@ public void testWorkerConcurrency() throws Exception {
357358
}
358359

359360
List<String> idsToRun = systemDatabase.getAndStartQueuedWorkflows(qwithWCLimit,
360-
GlobalParams.getInstance().getExecutorId(),
361-
GlobalParams.getInstance().getAppVersion());
361+
executorId,
362+
appVersion);
362363

363364
assertEquals(2, idsToRun.size());
364365

365366
// run the same above 2 are in Pending.
366367
// So no de queueing
367368
idsToRun = systemDatabase.getAndStartQueuedWorkflows(qwithWCLimit,
368-
GlobalParams.getInstance().getExecutorId(),
369-
GlobalParams.getInstance().getAppVersion());
369+
executorId,
370+
appVersion);
370371
assertEquals(0, idsToRun.size());
371372

372373
// mark the first 2 as success
@@ -376,8 +377,8 @@ public void testWorkerConcurrency() throws Exception {
376377

377378
// next 2 get dequeued
378379
idsToRun = systemDatabase.getAndStartQueuedWorkflows(qwithWCLimit,
379-
GlobalParams.getInstance().getExecutorId(),
380-
GlobalParams.getInstance().getAppVersion());
380+
executorId,
381+
appVersion);
381382
assertEquals(2, idsToRun.size());
382383

383384
DBUtils.updateWorkflowState(dataSource,
@@ -391,6 +392,8 @@ public void testWorkerConcurrency() throws Exception {
391392

392393
@Test
393394
public void testGlobalConcurrency() throws Exception {
395+
String executorId = dbosExecutor.getExecutorId();
396+
String appVersion = dbosExecutor.getAppVersion();
394397

395398
queueService.stop();
396399
while (!queueService.isStopped()) {
@@ -405,9 +408,9 @@ public void testGlobalConcurrency() throws Exception {
405408
WorkflowState.SUCCESS, "OrderProcessingWorkflow",
406409
"com.example.workflows.OrderWorkflow", "prod-config", "user123@example.com",
407410
"admin", "admin,operator", "{\"result\":\"success\"}", null,
408-
System.currentTimeMillis() - 3600000, System.currentTimeMillis(), "QwithWCLimit",
409-
GlobalParams.getInstance().getExecutorId(), GlobalParams.getInstance()
410-
.getAppVersion(),
411+
System.currentTimeMillis() - 3600000, System.currentTimeMillis(),
412+
"QwithWCLimit",
413+
executorId, appVersion,
411414
"order-app-123", 0,
412415
300000l, System.currentTimeMillis() + 2400000, "dedup-112233", 1,
413416
"{\"orderId\":\"ORD-12345\"}");
@@ -444,18 +447,18 @@ public void testGlobalConcurrency() throws Exception {
444447
}
445448

446449
List<String> idsToRun = systemDatabase.getAndStartQueuedWorkflows(qwithWCLimit,
447-
GlobalParams.getInstance().getExecutorId(),
448-
GlobalParams.getInstance().getAppVersion());
450+
executorId,
451+
appVersion);
449452
// 0 because global concurrency limit is reached
450453
assertEquals(0, idsToRun.size());
451454

452455
DBUtils.updateWorkflowState(dataSource,
453456
WorkflowState.PENDING.name(),
454457
WorkflowState.SUCCESS.name());
455458
idsToRun = systemDatabase.getAndStartQueuedWorkflows(qwithWCLimit,
456-
// GlobalParams.getInstance().getExecutorId(),
459+
// executorId,
457460
executor2,
458-
GlobalParams.getInstance().getAppVersion());
461+
appVersion);
459462
assertEquals(2, idsToRun.size());
460463
}
461464

0 commit comments

Comments
 (0)