Skip to content

Commit

Permalink
Merge pull request #6198 from Pandrex247/FISH-6815-6947-6983-Release
Browse files Browse the repository at this point in the history
FISH-6815-6947-6983 Revert Job Manager Removal (Release)
  • Loading branch information
JamesHillyard authored Feb 13, 2023
2 parents 4c55de6 + 80dafd9 commit 4fb45e6
Show file tree
Hide file tree
Showing 14 changed files with 1,452 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2022] [Payara Foundation and/or its affiliates]
package org.glassfish.admin.rest.utils;

import com.sun.enterprise.admin.remote.AdminCommandStateImpl;
import com.sun.enterprise.v3.admin.JobManagerService;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.glassfish.admin.rest.RestLogging;
import org.glassfish.api.admin.AdminCommandEventBroker;
import org.glassfish.api.admin.AdminCommandState;
import org.glassfish.api.admin.CommandRunner;
import org.glassfish.internal.api.Globals;

/**
*
Expand Down Expand Up @@ -77,6 +78,8 @@ public static String invokeAsync(CommandRunner.CommandInvocation commandInvocati
CountDownLatch latch = new CountDownLatch(1);
DetachedCommandHelper helper = new DetachedCommandHelper(commandInvocation, latch);
commandInvocation.listener(".*", helper);
JobManagerService jobManagerService = Globals.getDefaultHabitat().getService(JobManagerService.class);
jobManagerService.getThreadPool().execute(helper);
try {
if (!latch.await(10, TimeUnit.SECONDS)) {
RestLogging.restLogger.log(Level.FINE, "latch.await() returned false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019-2022] [Payara Foundation and/or its affiliates]
// Portions Copyright [2019-2021] [Payara Foundation and/or its affiliates]

package org.glassfish.admin.rest.utils;

import com.sun.enterprise.admin.remote.AdminCommandStateImpl;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.enterprise.v3.admin.JobManagerService;
import com.sun.enterprise.admin.report.PropsFileActionReporter;
import java.io.IOException;
import java.util.logging.Level;
Expand All @@ -54,6 +55,7 @@
import org.glassfish.api.admin.AdminCommandState;
import org.glassfish.api.admin.CommandRunner;
import org.glassfish.api.admin.CommandRunner.CommandInvocation;
import org.glassfish.internal.api.Globals;
import org.glassfish.jersey.media.sse.EventOutput;
import org.glassfish.jersey.media.sse.OutboundEvent;

Expand Down Expand Up @@ -176,6 +178,8 @@ public static EventOutput invokeAsync(CommandInvocation commandInvocation, Actio
}
SseCommandHelper helper = new SseCommandHelper(commandInvocation, processor);
commandInvocation.listener(".*", helper);
JobManagerService jobManagerService = Globals.getDefaultHabitat().getService(JobManagerService.class);
jobManagerService.getThreadPool().execute(helper);
return helper.eventOuptut;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2022] [Payara Foundation and/or its affiliates]
package com.sun.enterprise.v3.admin;

import com.sun.enterprise.admin.event.AdminCommandEventBrokerImpl;
Expand All @@ -48,6 +47,9 @@
import org.glassfish.api.admin.Job;
import org.glassfish.api.admin.CommandProgress;
import org.glassfish.api.admin.Payload;
import org.glassfish.api.admin.progress.JobInfo;
import org.glassfish.api.admin.progress.JobPersistence;
import org.glassfish.internal.api.Globals;
import org.glassfish.security.services.common.SubjectUtil;

import javax.security.auth.Subject;
Expand Down Expand Up @@ -169,7 +171,41 @@ public void complete(ActionReport report, Payload.Outbound outbound) {
super.actionReport = report;
this.payload = outbound;
this.completionDate = System.currentTimeMillis();
setState(State.COMPLETED);
if (isManagedJob) {
if (getState().equals(State.RUNNING_RETRYABLE) && failToRetryable) {
JobManagerService jobManager = Globals.getDefaultHabitat().getService(JobManagerService.class);
jobManager.getRetryableJobsInfo().put(id, CheckpointHelper.CheckpointFilename.createBasic(this));
jobManager.purgeJob(id);
setState(State.FAILED_RETRYABLE);
} else {
JobPersistence jobPersistenceService;
if (scope != null) {
jobPersistenceService = Globals.getDefaultHabitat().getService(JobPersistence.class,scope+"job-persistence");
} else {
jobPersistenceService = Globals.getDefaultHabitat().getService(JobPersistenceService.class);
}
State finalState = State.COMPLETED;
if (getState().equals(State.REVERTING)) {
finalState = State.REVERTED;
}
String user = null;
if(subjectUsernames.size() > 0){
user = subjectUsernames.get(0);
}
jobPersistenceService.persist(new JobInfo(id,commandName,executionDate,report.getActionExitCode().name(),user,report.getMessage(),getJobsFile(),finalState.name(),completionDate));
if (getState().equals(State.RUNNING_RETRYABLE) || getState().equals(State.REVERTING)) {
JobManagerService jobManager = Globals.getDefaultHabitat().getService(JobManagerService.class);
File jobFile = getJobsFile();
if (jobFile == null) {
jobFile = jobManager.getJobsFile();
}
jobManager.deleteCheckpoint(jobFile.getParentFile(), getId());
}
setState(finalState);
}
} else {
setState(State.COMPLETED);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2017-2022] [Payara Foundation and/or its affiliates]
// Portions Copyright [2017-2021] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.v3.admin;

Expand Down Expand Up @@ -1805,6 +1805,8 @@ private void executeFromCheckpoint(JobManager.Checkpoint checkpoint, boolean rev
}
((AdminCommandInstanceImpl) job).setEventBroker(eventBroker);
((AdminCommandInstanceImpl) job).setState(revert ? AdminCommandState.State.REVERTING : AdminCommandState.State.RUNNING_RETRYABLE);
JobManager jobManager = habitat.getService(JobManagerService.class);
jobManager.registerJob(job);
//command
AdminCommand command = checkpoint.getCommand();
if (command == null) {
Expand Down Expand Up @@ -1849,23 +1851,31 @@ public Subject run() {
isManagedJob = AnnotationUtil.presentTransitive(ManagedJob.class, command.getClass());
}
JobCreator jobCreator = null;
JobManager jobManager = null;

jobCreator = habitat.getService(JobCreator.class,scope+"job-creator");
jobManager = habitat.getService(JobManagerService.class);

if (jobCreator == null ) {
jobCreator = habitat.getService(JobCreatorService.class);

}

Job job = null;
job = jobCreator.createJob(null, scope(), name(), subject, isManagedJob, parameters());

if (isManagedJob) {
job = jobCreator.createJob(jobManager.getNewId(), scope(), name(), subject, isManagedJob, parameters());
} else {
job = jobCreator.createJob(null, scope(), name(), subject, isManagedJob, parameters());
}

//Register the brokers else the detach functionality will not work
for (NameListerPair nameListerPair : nameListerPairs) {
job.getEventBroker().registerListener(nameListerPair.nameRegexp, nameListerPair.listener);
}

if (isManagedJob) {
jobManager.registerJob(job);
}
CommandRunnerImpl.this.doCommand(this, command, subject, job);
job.complete(report(), outboundPayload());
if (progressStatusChild != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012-2013 Oracle 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://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/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 packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle 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 com.sun.enterprise.v3.admin;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import javax.security.auth.Subject;
import org.glassfish.api.admin.AdminCommand;
import org.glassfish.api.admin.AuthorizationPreprocessor;
import org.glassfish.api.admin.Job;
import org.glassfish.api.admin.progress.JobInfo;
import org.jvnet.hk2.annotations.Service;

/**
* Attaches a user attribute to job resources for authorization.
*
* @author Tim Quinn
* @author Bhakti Mehta
*/
@Service
@Singleton
public class JobAuthorizationAttributeProcessor implements AuthorizationPreprocessor {

private final static String USER_ATTRIBUTE_NAME = "user";

public final static String JOB_RESOURCE_NAME_PREFIX_NO_SLASH = "jobs/job";
public final static String JOB_RESOURCE_NAME_PREFIX = JOB_RESOURCE_NAME_PREFIX_NO_SLASH + '/';

public final static Pattern JOB_PATTERN = Pattern.compile("(?:" + JOB_RESOURCE_NAME_PREFIX_NO_SLASH + "(?:/(\\d*))?)");

@Inject
private JobManagerService jobManager;

@Override
public void describeAuthorization(Subject subject, String resourceName, String action, AdminCommand command, Map<String, Object> context, Map<String, String> subjectAttributes, Map<String, String> resourceAttributes, Map<String, String> actionAttributes) {
final Matcher m = JOB_PATTERN.matcher(resourceName);
if ( ! m.matches()) {
return;
}
if (m.groupCount() == 0) {
/*
* The resource name pattern did not match for including a job ID,
* so we will not be able to attach a user attribute to the resource.
*/
return;
}
final String jobID = m.group(1);
final Job job = jobManager.get(jobID);
String userID = null;

/*
* This logic might run before any validation in the command has run,
* in which case the job ID would be invalid and the job manager and/or
* the completed jobs store might not know about the job.
*/
if (job != null && job.getSubjectUsernames().size() > 0) {
userID = job.getSubjectUsernames().get(0);
} else {
if (jobManager.getCompletedJobs(jobManager.getJobsFile()) != null) {
final JobInfo jobInfo = (JobInfo) jobManager.getCompletedJobForId(jobID);
if (jobInfo != null) {
userID = jobInfo.user;
}
}
}

if (userID != null) {
resourceAttributes.put(USER_ATTRIBUTE_NAME, userID);
}
}
}
Loading

0 comments on commit 4fb45e6

Please sign in to comment.