Skip to content

Commit

Permalink
Add standalone sequence reset in engine startup (#535)
Browse files Browse the repository at this point in the history
* Add standalone sequence reset in engine startup

* Add integration tests for standalone mode

* Exclude test from non-HSQL workflows
  • Loading branch information
DaSpood authored Jul 23, 2024
1 parent dd06fbd commit 687f2d9
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.enioka.jqm.engine;

import java.lang.management.ManagementFactory;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -43,6 +45,7 @@
import com.enioka.jqm.engine.api.lifecycle.JqmEngineOperations;
import com.enioka.jqm.jdbc.DatabaseException;
import com.enioka.jqm.jdbc.DbConn;
import com.enioka.jqm.jdbc.DbManager;
import com.enioka.jqm.jdbc.NoResultException;
import com.enioka.jqm.jdbc.QueryResult;
import com.enioka.jqm.model.DeploymentParameter;
Expand All @@ -62,6 +65,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.enioka.jqm.shared.misc.StandaloneHelpers.idSequenceBaseFromIp;

/**
* The engine itself. Everything starts in this class.
*/
Expand Down Expand Up @@ -245,6 +250,16 @@ public void start(String nodeName, JqmEngineHandler h)
// Pollers
syncPollers(cnx, this.node);
jqmlogger.info("All required queues are now polled");

// Standalone sequence reinit
final var standaloneMode = Boolean.parseBoolean(
GlobalParameter.getParameter(DbManager.getDb().getConn(), "wsStandaloneMode", "false"));
if (standaloneMode) {
final var localIp = Inet4Address.getLocalHost().getHostAddress();
cnx.runRawUpdate("ALTER SEQUENCE JQM_PK RESTART WITH " + idSequenceBaseFromIp(localIp));
}
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}

// Internal poller (stop notifications, keep alive)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.enioka.jqm.integration.tests;

import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.Properties;

import com.enioka.jqm.model.GlobalParameter;
import com.enioka.jqm.model.Node;
import com.enioka.jqm.test.helpers.CreationTools;
import com.enioka.jqm.test.helpers.TestHelpers;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.ops4j.pax.exam.Option;

import static com.enioka.jqm.shared.misc.StandaloneHelpers.ipFromId;

public class StandaloneTest extends JqmBaseTest {
private Node n;
private Properties p;
private String localIp;

@Override
protected Option[] moreOsgiconfig()
{
return webConfig();
}

@Before
public void before() throws UnknownHostException {
// ///// Disabled for non-HSQLDB runners

AssumeHsqldb();

// ///// Standalone setup

localIp = Inet4Address.getLocalHost().getHostAddress();
GlobalParameter.setParameter(cnx, "wsStandaloneMode", "true");
cnx.commit();
}

@Test
public void testStandaloneJobIdMatchesIp() {
addAndStartEngine();
CreationTools.createJobDef(null, true, "App", null, "jqm-tests/jqm-test-datetimemaven/target/test.jar", TestHelpers.qVip, 42,
"MarsuApplication", null, "Franquin", "ModuleMachin", "other", "other", false, cnx);
long generatedId = jqmClient.newJobRequest("MarsuApplication", "TestUser").enqueue();

Assert.assertEquals("Generated ID was " + generatedId, localIp, ipFromId(generatedId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public QueryResult runUpdate(String query_key, Object... params)
}
}

void runRawUpdate(String query_sql)
public void runRawUpdate(String query_sql)
{
transac_open = true;
String sql = null;
Expand Down
Loading

0 comments on commit 687f2d9

Please sign in to comment.