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 8174 improve thread expire validation to read lastaccesstime #6637

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
breakponchito marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -56,6 +56,8 @@
* limitations under the License.
*/

// Portions Copyright [2024] [Payara Foundation and/or its affiliates]
breakponchito marked this conversation as resolved.
Show resolved Hide resolved

package org.apache.catalina;


Expand Down Expand Up @@ -387,5 +389,15 @@ public interface Session {
/**
* unlock the session from foreground
*/
public void unlockForeground();
public void unlockForeground();

/**
* This method is to get the last time this session was accessed by the request from the client.
*/
public long getThisAccessedTime();

/**
* This method is to set the value of the accessed time
*/
public void setThisAccessedTime(long accessedTime);
}
Expand Up @@ -55,7 +55,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//Portions Copyright [2016-2022] [Payara Foundation]
// Portions Copyright 2016-2024 Payara Foundation and/or its affiliates

package org.apache.catalina.connector;

Expand Down Expand Up @@ -2911,7 +2911,6 @@ public void setWebConnection(WebConnection wc) {
// ------------------------------------------------------ Protected Methods

protected Session doGetSession(boolean create) {

// There cannot be a session if no context has been assigned yet
if (context == null) {
return null;
Expand Down Expand Up @@ -2954,6 +2953,7 @@ protected Session doGetSession(boolean create) {
if (session != null && !session.isValid()) {
session = null;
}

if (session != null) {
session.access();
return session;
Expand Down
Expand Up @@ -55,7 +55,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Portions Copyright [2016-2019] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2024] [Payara Foundation and/or its affiliates]
breakponchito marked this conversation as resolved.
Show resolved Hide resolved

package org.apache.catalina.session;

Expand Down Expand Up @@ -600,6 +600,16 @@ protected void processExpires() {
final List<Session> sessions = findSessions();
for (final Session session1 : sessions) {
StandardSession session = (StandardSession) session1;
//verify if session also is on the store and compare lastAccessTime and thisAccessedTime
StandardSession sessionFromStore = null;
try {
sessionFromStore = (StandardSession) swapIn(session.getId());
} catch (IOException e) {
log.log(Level.INFO, "Caught exception attempting to swap in session from store", e);
}
if (sessionFromStore != null) {
compareAndUpdateAccessedTime(session, sessionFromStore);
}
if(!session.getIsValid() || session.hasExpired()) {
if(session.lockBackground()) {
try {
Expand Down Expand Up @@ -728,14 +738,35 @@ public Session findSession(String id) throws IOException {
//6406580 END

Session session = super.findSession(id);
if (session != null)

if (session != null) {
//verify if session also is on the store and compare lastAccessTime and thisAccessedTime
Session sessionFromStore = swapIn(id);
if (sessionFromStore != null) {
compareAndUpdateAccessedTime((StandardSession) session, (StandardSession) sessionFromStore);
}
return (session);
}

// See if the Session is in the Store
session = swapIn(id);
return (session);

}
}

public void compareAndUpdateAccessedTime(StandardSession currentSession, StandardSession sessionFromStore) {
breakponchito marked this conversation as resolved.
Show resolved Hide resolved
if (sessionFromStore.getLastAccessedTimeInternal() == currentSession.getLastAccessedTimeInternal()) {
return;
}
//not equal assign new value to update lastaccesstime saved on other instances from the cluster
if (sessionFromStore.getLastAccessedTimeInternal() > currentSession.getLastAccessedTimeInternal()) {
currentSession.setLastAccessedTime(sessionFromStore.getLastAccessedTimeInternal());
}
//not equal assign new value to update thisAccessTime saved on other instances from the cluster
if (sessionFromStore.getThisAccessedTime() > currentSession.getThisAccessedTime()) {
currentSession.setThisAccessedTime(sessionFromStore.getThisAccessedTime());
}
}

/**
* Return the active Session, associated with this Manager, with the
Expand Down
Expand Up @@ -256,7 +256,7 @@ public StandardSession(Manager manager) {
/**
* The last accessed time for this Session.
*/
protected long lastAccessedTime = creationTime;
protected volatile long lastAccessedTime = creationTime;

/**
* The session event listeners for this Session.
Expand Down Expand Up @@ -312,7 +312,7 @@ public StandardSession(Manager manager) {
/**
* The current accessed time for this session.
*/
protected long thisAccessedTime = creationTime;
protected volatile long thisAccessedTime = creationTime;

/**
* The session version, incremented and used by in-memory-replicating
Expand Down Expand Up @@ -581,6 +581,15 @@ public void setLastAccessedTime(long lastAcessedTime) {
}


@Override
public long getThisAccessedTime() {
return this.thisAccessedTime;
}

public void setThisAccessedTime(long accessedTime) {
this.thisAccessedTime = accessedTime;
}

/**
* Return the Manager within which this Session is valid.
*/
Expand Down Expand Up @@ -753,7 +762,6 @@ public void setValid(boolean isValid) {
public void access() {
this.lastAccessedTime = this.thisAccessedTime;
this.thisAccessedTime = System.currentTimeMillis();

evaluateIfValid();
}

Expand Down Expand Up @@ -1083,8 +1091,10 @@ public void setNote(String name, Object value) {
*/
@Override
public boolean hasExpired() {
long diff = System.currentTimeMillis() - this.thisAccessedTime;
long maxIncInterval = maxInactiveInterval * 1000L;
return maxInactiveInterval >= 0
&& (System.currentTimeMillis() - thisAccessedTime >= maxInactiveInterval * 1000L);
&& (diff >= maxIncInterval);
}
// END SJSAS 6329289

Expand Down Expand Up @@ -1908,11 +1918,25 @@ private void readRemainingObject(ObjectInputStream stream)

version = new AtomicLong();

lastAccessedTime = ((Long) stream.readObject());
//control assign of lastAccessedTime and thisAccessedTime if both on the local are more recent that the values saved
//on the store then use current values
long readlastAccessedTime = ((Long) stream.readObject());
//assign the read value from storage in case the value is greater than the current
//this means that other member from the cluster updated the value
if (readlastAccessedTime != 0 && readlastAccessedTime >= lastAccessedTime) {
lastAccessedTime = readlastAccessedTime;
}

maxInactiveInterval = ((Integer) stream.readObject());
isNew = ((Boolean) stream.readObject());
isValid = ((Boolean) stream.readObject());
thisAccessedTime = ((Long) stream.readObject());
long readThisAccessedTime = ((Long) stream.readObject());
//assign the read value from storage in case the value is greater than the current
//this means that other member from the cluster updated the value
if (readThisAccessedTime != 0 && readThisAccessedTime >= thisAccessedTime) {
thisAccessedTime = readThisAccessedTime;
}

/* SJSWS 6371339
principal = null; // Transient only
// setId((String) stream.readObject());
Expand Down