Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FISH-108 Add command to clean Jbatch repository #5094

Merged
merged 6 commits into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2021] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.batch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ServiceConfigurationError;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.batch.runtime.BatchRuntime;
import javax.inject.Inject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.glassfish.api.ActionReport;
import org.glassfish.api.Param;
import org.glassfish.api.admin.AdminCommand;
import org.glassfish.api.admin.AdminCommandContext;
import org.glassfish.api.admin.CommandLock;
import org.glassfish.api.admin.ExecuteOn;
import org.glassfish.api.admin.RuntimeType;
import org.glassfish.batch.spi.impl.BatchRuntimeConfiguration;
import org.glassfish.batch.spi.impl.BatchRuntimeHelper;
import org.glassfish.hk2.api.PerLookup;
import org.jvnet.hk2.annotations.Service;

/**
* Cleans the records of job executions from the repository.
*
* @author jonathan coustick
* @since 5.25.0
*/
@Service(name = "clean-jbatch-repository")
@PerLookup
@CommandLock(CommandLock.LockType.NONE)
@ExecuteOn(value = {RuntimeType.INSTANCE})
public class CleanJbatchRepository implements AdminCommand {

@Param(acceptableValues = "ALL,COMPLETED", defaultValue = "COMPLETED", optional = true)
String status;

@Param(optional = true)
int days;

@Param(name = "jobname", primary = true, optional = false)
String jobname;

@Inject
BatchRuntimeHelper batchRuntimeHelper;

@Inject
BatchRuntimeConfiguration config;

@Override
public void execute(AdminCommandContext context) {
ActionReport report = context.getActionReport();

//Initialises databases if they don't already exist, ignore result
try {
BatchRuntime.getJobOperator();
} catch (ServiceConfigurationError error) {
report.setMessage("Could not get JobOperator. Check if the Batch DataSource is configured properly and Check if the Database is up and running");
report.setFailureCause(error);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
return;
}

try {
String dataSourceName = batchRuntimeHelper.getDataSourceLookupName();
InitialContext ctx = new InitialContext();
Object object = ctx.lookup(dataSourceName);
if (object instanceof DataSource) {
DataSource datasource = (DataSource) object;
try (Connection conn = datasource.getConnection()) {
String prefix = config.getTablePrefix();
String suffix = config.getTableSuffix();

PreparedStatement deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "stepstatus" + suffix +
" WHERE id IN (SELECT seid.stepexecid "
+ "FROM jobinstancedata jid, executioninstancedata eid, stepexecutioninstancedata seid "
+ "WHERE jid.jobinstanceid = eid.jobinstanceid AND eid.jobexecid = seid.jobexecid "
+ "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND (eid.batchstatus = ? OR ? = 'ALL'))");

deleteStatement.setString(1, jobname);
deleteStatement.setInt(2, -days);
deleteStatement.setString(3, status);
deleteStatement.setString(4, status);
deleteStatement.execute();

deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "stepexecutioninstancedata" + suffix +
" WHERE jobexecid IN (SELECT eid.jobexecid "
+ "FROM jobinstancedata jid, executioninstancedata eid WHERE jid.jobinstanceid = eid.jobinstanceid "
+ "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND (eid.batchstatus = ? OR ? = 'ALL'))");
deleteStatement.setString(1, jobname);
deleteStatement.setInt(2, -days);
deleteStatement.setString(3, status);
deleteStatement.setString(4, status);
deleteStatement.execute();

deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "executioninstancedata" + suffix +
" WHERE jobinstanceid IN (SELECT jid.jobinstanceid "
+ "FROM jobinstancedata jid, executioninstancedata eid WHERE jid.jobinstanceid = eid.jobinstanceid "
+ "AND jid.name = ? AND eid.endtime < DATEADD('DAY',?, NOW()) AND (eid.batchstatus = ? OR ? = 'ALL'))");
deleteStatement.setString(1, jobname);
deleteStatement.setInt(2, -days);
deleteStatement.setString(3, status);
deleteStatement.setString(4, status);
deleteStatement.execute();

deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "jobstatus" + suffix
+ " WHERE id NOT IN (SELECT DISTINCT jobinstanceid FROM executioninstancedata)");
deleteStatement.execute();

deleteStatement = conn.prepareStatement("DELETE FROM " + prefix + "jobinstancedata " + suffix
+ " WHERE jobinstanceid NOT IN (SELECT DISTINCT jobinstanceid FROM executioninstancedata)");
deleteStatement.execute();
} catch (SQLException ex) {
Logger.getLogger("fish.payara.batch").log(Level.SEVERE, "Error cleaning repository", ex);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage("Error cleaning repository");
report.setFailureCause(ex);
}

} else {
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage("Invalid data source type for JBatch");
}

} catch (NamingException ex) {
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage("Unable to get data source for JBatch");
report.setFailureCause(ex);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
clean-jbatch-repository(1) asadmin Utility Subcommands clean-jbatch-repository(1)

NAME
clean-jbatch-repository - cleans up batch job execution records

SYNOPSIS
clean-jbatch-repository [--help]
[--target target]
[--status={COMPLETED|ALL}]
[--days days]
[job_name]

DESCRIPTION
The clean-jbatch-repository subcommand deletes records of job executions

OPTIONS
--help, -?
Displays the help text for the subcommand.

--status
The status of batch jobs executions to which delete the records of.
This value is COMPLETED by default

Possible values are as follows:

ALL
All job executions will be deleted, regardless of status.

COMPLETED
Only job executions that successfully completed will be deleted.

--days
The number of days previous that the end time of the job execution must be to
be deleted. The default value is 0.

OPERANDS
job_name
The name of the job for which to delete records of.

EXAMPLES
Example 1, Deleting job execution records
This example deletes all job execution records that are over a week old for a job named batchprocess.

asadmin> clean-jbatch-repository --status=ALL --days=7 batchprocess
Command clean-jbatch-repository executed successfully.

EXIT STATUS
0
subcommand executed successfully

1
error in executing the subcommand

SEE ALSO
list-batch-job-executions(1), list-batch-job-steps(1),
purge-jbatch-repository

asadmin(1M)

Jakarta EE 8 7 Jan 2021 clean-jbatch-repository(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
purge-jbatch-repository(1) asadmin Utility Subcommands purge-jbatch-repository(1)

NAME
purge-jbatch-repository - purges batch jobs

SYNOPSIS
purge-jbatch-repository [--help]
[apptag]

DESCRIPTION
The purge-jbatch-repository subcommand deletes all job executions of a given apptag

OPTIONS
OPERANDS
apptag
The apptag used to purge all records of from the database.

EXAMPLES
Example 1, Deleting all job executions
This example deletes all job executions for an application named jbatchapp on the default server config.

asadmin> purge-jbatch-repository server-config:jbatchapp
Command purge-jbatch-repository executed successfully.

EXIT STATUS
0
subcommand executed successfully

1
error in executing the subcommand

SEE ALSO
list-batch-job-executions(1), list-batch-job-steps(1),
clean-jbatch-repository

asadmin(1M)

Jakarta EE 8 7 Jan 2021 purge-jbatch-repository(1)