Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Issue #210
  • Loading branch information
rsoika committed Jul 16, 2024
1 parent 3e0b6dc commit 4308cc5
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
* @version 1.0
* @author rsoika
*/

@Singleton
@Startup
public class BackupService {
Expand Down Expand Up @@ -354,6 +353,7 @@ public void restartScheduler() throws BackupException {
*/
public void startScheduler(boolean clearLog) throws BackupException {
try {
restClientHelper.reset();
if (clearLog) {
// clear log in case of a normal start
logController.reset(BackupApi.TOPIC_BACKUP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private void restoreSnapshot(DocumentClient documentClient, ItemCollection snaps
*/
public void startScheduler() throws BackupException {
try {

restClientHelper.reset();
logController.reset(BackupApi.TOPIC_RESTORE);
logController.info(BackupApi.TOPIC_RESTORE,
"Starting restore scheduler - initalDelay=0ms inverval=" + interval + "ms ....");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,12 @@ public EventLogClient createEventLogClient(DocumentClient documentClient) {
}
}

/**
* This method invalidates the rest clients
*/
public void reset() {
documentClient = null;
eventLogClient = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,22 @@

package org.imixs.archive.service;

import jakarta.annotation.PostConstruct;
import jakarta.inject.Inject;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

import org.imixs.archive.service.resync.ResyncService;
import org.imixs.archive.service.util.MessageService;

/**
* The Imixs-Archive-Service application setup
*
* @author rsoika
*
*/

@ApplicationPath("api")
public class ImixsArchiveApp extends Application {

// event log topics
public static final String EVENTLOG_TOPIC_ADD = "snapshot.add";
public static final String EVENTLOG_TOPIC_REMOVE = "snapshot.remove";
public static final String EVENTLOG_TOPIC_BACKUP = "snapshot.backup";

public static final String ITEM_BACKUPRESTORE = "$backuprestore";

// rest service endpoint
Expand All @@ -62,28 +55,8 @@ public class ImixsArchiveApp extends Application {
public static final String WORKFLOW_SYNC_DEADLOCK = "workflow.sync.deadlock";
public static final String BACKUP_SERVICE_ENDPOINT = "backup.service.endpoint";

@Inject
ResyncService syncService;

@Inject
MessageService messageService;

public ImixsArchiveApp() {
super();
}

/**
* Initialize the web application
*/
@PostConstruct
public void initialize() {
if (syncService != null) {
try {
syncService.start();
} catch (ArchiveException e) {
messageService.logMessage(ResyncService.MESSAGE_TOPIC, "Failed to start scheduler - " + e.getMessage());
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.imixs.archive.service.cassandra.ClusterService;
import org.imixs.melman.BasicAuthenticator;
import org.imixs.melman.DocumentClient;
import org.imixs.melman.FormAuthenticator;
import org.imixs.melman.RestAPIException;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.xml.XMLDataCollection;
Expand Down Expand Up @@ -83,12 +81,11 @@ public class RemoteAPIService {
* @throws ArchiveException
*
*/
public XMLDataCollection readSyncData(long syncPoint) throws ArchiveException {
public XMLDataCollection readSyncData(long syncPoint, DocumentClient documentClient) throws ArchiveException {
XMLDataCollection result = null;
// load next document
String url = "";
try {
DocumentClient documentClient = initWorkflowClient();
url = SNAPSHOT_SYNCPOINT_RESOURCE + syncPoint;
logger.finest("...... read data: " + url + "....");

Expand All @@ -115,11 +112,10 @@ public XMLDataCollection readSyncData(long syncPoint) throws ArchiveException {
* @throws ArchiveException
*
*/
public String readSnapshotIDByUniqueID(String uniqueid) throws ArchiveException {
public String readSnapshotIDByUniqueID(String uniqueid, DocumentClient documentClient) throws ArchiveException {
String result = null;
try {
// load single document
DocumentClient documentClient = initWorkflowClient();
String url = DOCUMENTS_RESOURCE + uniqueid + "?items=$snapshotid";
logger.finest("...... read snapshotid: " + url + "....");

Expand All @@ -137,9 +133,8 @@ public String readSnapshotIDByUniqueID(String uniqueid) throws ArchiveException
return result;
}

public void restoreSnapshot(ItemCollection snapshot) throws ArchiveException {
public void restoreSnapshot(ItemCollection snapshot, DocumentClient documentClient) throws ArchiveException {
try {
DocumentClient documentClient = initWorkflowClient();
String url = SNAPSHOT_RESOURCE;
logger.finest("...... post data: " + url + "....");
// documentClient.postDocument(url, snapshot);
Expand All @@ -151,9 +146,8 @@ public void restoreSnapshot(ItemCollection snapshot) throws ArchiveException {

}

public void deleteSnapshot(String id) throws ArchiveException {
public void deleteSnapshot(String id, DocumentClient documentClient) throws ArchiveException {
try {
DocumentClient documentClient = initWorkflowClient();
String url = SNAPSHOT_RESOURCE;
logger.finest("...... delete data: " + url + "....");
documentClient.deleteDocument(id);
Expand All @@ -170,27 +164,31 @@ public void deleteSnapshot(String id) throws ArchiveException {
*
* @throws RestAPIException
*/
DocumentClient initWorkflowClient() throws RestAPIException {

logger.finest("...... WORKFLOW_SERVICE_ENDPOINT = " + workflowServiceEndpoint);

DocumentClient documentClient = new DocumentClient(workflowServiceEndpoint.get());

// Test authentication method
if ("Form".equalsIgnoreCase(workflowServiceAuthMethod.get())) {
// default basic authenticator
FormAuthenticator formAuth = new FormAuthenticator(workflowServiceEndpoint.get(), workflowServiceUser.get(),
workflowServicePassword.get());
// register the authenticator
documentClient.registerClientRequestFilter(formAuth);

} else {
// default basic authenticator
BasicAuthenticator basicAuth = new BasicAuthenticator(workflowServiceUser.get(),
workflowServicePassword.get());
// register the authenticator
documentClient.registerClientRequestFilter(basicAuth);
}
return documentClient;
}
// DocumentClient initWorkflowClient() throws RestAPIException {

// logger.finest("...... WORKFLOW_SERVICE_ENDPOINT = " +
// workflowServiceEndpoint);

// DocumentClient documentClient = new
// DocumentClient(workflowServiceEndpoint.get());

// // Test authentication method
// if ("Form".equalsIgnoreCase(workflowServiceAuthMethod.get())) {
// // default basic authenticator
// FormAuthenticator formAuth = new
// FormAuthenticator(workflowServiceEndpoint.get(), workflowServiceUser.get(),
// workflowServicePassword.get());
// // register the authenticator
// documentClient.registerClientRequestFilter(formAuth);

// } else {
// // default basic authenticator
// BasicAuthenticator basicAuth = new
// BasicAuthenticator(workflowServiceUser.get(),
// workflowServicePassword.get());
// // register the authenticator
// documentClient.registerClientRequestFilter(basicAuth);
// }
// return documentClient;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public void init() {
public void run(Timer timer) {
DocumentClient documentClient = null;
EventLogClient eventLogClient = null;
// ClientRequestFilter authenticator = null;

logger.fine("--- run timeout.... timerInfo= " + timer.getInfo());
try {
Expand Down Expand Up @@ -183,6 +182,7 @@ public void run(Timer timer) {
} catch (NotFoundException | RestAPIException e) {
logger.warning("unable to process event log: " + e.getMessage());
// we need to reset the timer and discard the current JSESSIONID
restClientHelper.reset();
timer.cancel();
final TimerConfig timerConfig = new TimerConfig();
timerConfig.setInfo(""); // empty info string indicates no JSESSIONID!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void processEventLog(EventLogClient eventLogClient, DocumentClient docume

if (documentClient == null || eventLogClient == null) {
// no client object
logger.fine("...no eventLogClient available!");
logger.warning("...no eventLogClient available!");
return;
}

Expand Down Expand Up @@ -149,7 +149,7 @@ public void processEventLog(EventLogClient eventLogClient, DocumentClient docume
public void releaseDeadLocks(EventLogClient eventLogClient) throws RestAPIException {
if (eventLogClient == null) {
// no client object
logger.fine("...no eventLogClient available!");
logger.warning("...no eventLogClient available!");
return;
}
eventLogClient.releaseDeadLocks(deadLockInterval, ImixsArchiveApp.EVENTLOG_TOPIC_ADD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.logging.Logger;

import org.imixs.archive.service.ArchiveException;
import org.imixs.archive.service.resync.ResyncService;
import org.imixs.workflow.FileData;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.WorkflowKernel;
Expand Down Expand Up @@ -95,9 +94,6 @@ public class DataService {
@Inject
ClusterService clusterService;

@Inject
ResyncService schedulerService;

@Inject
protected Event<ArchiveEvent> events;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
import org.imixs.archive.service.RemoteAPIService;
import org.imixs.archive.service.cassandra.ClusterService;
import org.imixs.archive.service.cassandra.DataService;
import org.imixs.archive.service.resync.ResyncService;
import org.imixs.archive.service.util.MessageService;
import org.imixs.archive.service.util.RestClientHelper;
import org.imixs.melman.DocumentClient;
import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.exceptions.QueryException;
import org.imixs.workflow.xml.XMLDocumentAdapter;
Expand All @@ -48,7 +49,11 @@
import com.datastax.driver.core.Session;

import jakarta.annotation.Resource;
import jakarta.ejb.Stateless;
import jakarta.annotation.security.DeclareRoles;
import jakarta.annotation.security.RunAs;
import jakarta.ejb.LocalBean;
import jakarta.ejb.Singleton;
import jakarta.ejb.Startup;
import jakarta.ejb.Timeout;
import jakarta.ejb.Timer;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -80,9 +85,12 @@
* @version 1.0
* @author rsoika
*/

@Stateless
public class RestoreService {
@Startup
@Singleton
@LocalBean
@DeclareRoles({ "org.imixs.ACCESSLEVEL.MANAGERACCESS" })
@RunAs("org.imixs.ACCESSLEVEL.MANAGERACCESS")
public class RestoreScheduler {

public final static String TIMER_ID_RESTORESERVICE = "IMIXS_ARCHIVE_RESTORE_TIMER";
public final static long TIMER_INTERVAL_DURATION = 60000;
Expand All @@ -97,7 +105,7 @@ public class RestoreService {

public final static String MESSAGE_TOPIC = "restore";

private static Logger logger = Logger.getLogger(RestoreService.class.getName());
private static Logger logger = Logger.getLogger(RestoreScheduler.class.getName());

@Inject
DataService dataService;
Expand All @@ -114,6 +122,9 @@ public class RestoreService {
@Resource
jakarta.ejb.TimerService timerService;

@Inject
RestClientHelper restClientHelper;

/**
* Starts a new restore process with a EJB TimerService
* <p>
Expand Down Expand Up @@ -143,7 +154,7 @@ public void start(long restoreFrom, long restoreTo, List<Map> options) throws Ar
timer = null;
} catch (Exception e) {
messageService.logMessage(MESSAGE_TOPIC, "Failed to stop existing timer - " + e.getMessage());
throw new ArchiveException(ResyncService.class.getName(), ArchiveException.INVALID_WORKITEM,
throw new ArchiveException(RestoreScheduler.class.getName(), ArchiveException.INVALID_WORKITEM,
" failed to cancle existing timer!");
}
}
Expand Down Expand Up @@ -233,6 +244,7 @@ private void stop(Timer timer) throws ArchiveException {
*/
@Timeout
void onTimeout(jakarta.ejb.Timer timer) throws Exception {
DocumentClient documentClient = null;
Session session = null;
Cluster cluster = null;
ItemCollection metadata = null;
Expand All @@ -244,6 +256,9 @@ void onTimeout(jakarta.ejb.Timer timer) throws Exception {
long startTime = System.currentTimeMillis();

try {
// init rest clients....
documentClient = restClientHelper.createDocumentClient();

// read the metadata
metadata = dataService.loadMetadata();
// read last restore stat....
Expand Down Expand Up @@ -280,7 +295,8 @@ void onTimeout(jakarta.ejb.Timer timer) throws Exception {
// yes, lets see if this snapshot is already restored or synced?
try {
remoteSnapshotID = remoteAPIService
.readSnapshotIDByUniqueID(dataService.getUniqueID(latestSnapshot));
.readSnapshotIDByUniqueID(dataService.getUniqueID(latestSnapshot),
documentClient);
} catch (ArchiveException ae) {
// expected if not found
}
Expand All @@ -295,7 +311,7 @@ void onTimeout(jakarta.ejb.Timer timer) throws Exception {
snapshot = dataService.loadSnapshot(latestSnapshot);
_tmpSize = dataService.calculateSize(XMLDocumentAdapter.getDocument(snapshot));
logger.finest("......size=: " + _tmpSize);
remoteAPIService.restoreSnapshot(snapshot);
remoteAPIService.restoreSnapshot(snapshot, documentClient);
restoreSize = restoreSize + _tmpSize;
restoreCount++;
snapshot = null;
Expand Down Expand Up @@ -464,7 +480,7 @@ public Timer findTimer() {
public List<ItemCollection> getOptions(ItemCollection metaData) {
// convert current list of options into a list of ItemCollection elements
ArrayList<ItemCollection> result = new ArrayList<ItemCollection>();
List<Object> mapOrderItems = metaData.getItemValue(RestoreService.ITEM_RESTORE_OPTIONS);
List<Object> mapOrderItems = metaData.getItemValue(RestoreScheduler.ITEM_RESTORE_OPTIONS);
for (Object mapOderItem : mapOrderItems) {
if (mapOderItem instanceof Map) {
ItemCollection itemCol = new ItemCollection((Map) mapOderItem);
Expand All @@ -490,7 +506,7 @@ public void setOptions(List<ItemCollection> options, ItemCollection metaData) {
for (ItemCollection orderItem : options) {
mapOrderItems.add(orderItem.getAllItems());
}
metaData.replaceItemValue(RestoreService.ITEM_RESTORE_OPTIONS, mapOrderItems);
metaData.replaceItemValue(RestoreScheduler.ITEM_RESTORE_OPTIONS, mapOrderItems);
}
}

Expand Down
Loading

0 comments on commit 4308cc5

Please sign in to comment.