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 5 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 @@ -387,5 +387,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]
breakponchito marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -600,6 +600,17 @@ 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 last access time
StandardSession sessionFromStore = null;
try {
sessionFromStore = (StandardSession) swapIn(session.getId());
} catch (IOException e) {
log.log(Level.WARNING, "Session from Store not available", e);
breakponchito marked this conversation as resolved.
Show resolved Hide resolved
}
if (sessionFromStore != null) {
log.log(Level.INFO, "processExpires --> Getting session from Store and compare " + sessionFromStore);
compareAndUpdateAccessedTime(session, sessionFromStore);
}
if(!session.getIsValid() || session.hasExpired()) {
if(session.lockBackground()) {
try {
Expand Down Expand Up @@ -728,14 +739,42 @@ 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 last access time
Session sessionFromStore = swapIn(id);
log.log(Level.INFO, "findSession --> Getting session from Store and compare " + sessionFromStore);
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
log.log(Level.INFO, "compareAndUpdate -->Current Session lastaccessedtime:" + currentSession.getLastAccessedTimeInternal());
log.log(Level.INFO, "compareAndUpdate -->Current Session ThisAccessedTime:" + currentSession.getThisAccessedTime());
log.log(Level.INFO, "compareAndUpdate -->Session from Store lastaccessedtime:" + sessionFromStore.getLastAccessedTimeInternal());
log.log(Level.INFO, "compareAndUpdate -->Session from Store ThisAccessedTime:" + sessionFromStore.getLastAccessedTimeInternal());
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());
}
if (sessionFromStore.getThisAccessedTime() > currentSession.getThisAccessedTime()) {
currentSession.setThisAccessedTime(sessionFromStore.getThisAccessedTime());
}

log.log(Level.INFO, "compareAndUpdate -->Saving stored LastAccessedTime to current session:" + currentSession.getLastAccessedTimeInternal());
log.log(Level.INFO, "compareAndUpdate -->Saving stored AccessedTime to current session:" + currentSession.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,8 +762,11 @@ public void setValid(boolean isValid) {
public void access() {
this.lastAccessedTime = this.thisAccessedTime;
this.thisAccessedTime = System.currentTimeMillis();

evaluateIfValid();
LOGGER.info("Access -> Thread:" + Thread.currentThread().toString());
LOGGER.info("Access -> lastAccessedTime:" + this.lastAccessedTime);
LOGGER.info("Access -> thisAccessedTime:" + this.thisAccessedTime);
LOGGER.info("Access -> thisCreationTime:" + this.creationTime);
}


Expand All @@ -764,6 +776,10 @@ public void access() {
@Override
public void endAccess() {
isNew = false;
LOGGER.info("endAccess -> Thread:" + Thread.currentThread().toString());
LOGGER.info("endAccess -> lastAccessedTime:" + this.lastAccessedTime);
LOGGER.info("endAccess -> thisAccessedTime:" + this.thisAccessedTime);
LOGGER.info("endAccess -> thisCreationTime:" + this.creationTime);
}


Expand Down Expand Up @@ -1083,8 +1099,18 @@ public void setNote(String name, Object value) {
*/
@Override
public boolean hasExpired() {
LOGGER.info("hasExpired() -> Thread:" + Thread.currentThread().toString());
LOGGER.info("hasExpired() -> lastAccessedTime:" + this.lastAccessedTime);
LOGGER.info("hasExpired() -> thisAccessedTime:" + this.thisAccessedTime);
LOGGER.info("hasExpired() -> thisCreationTime:" + this.creationTime);
LOGGER.info("hasExpired() -> currentTime:" + System.currentTimeMillis());
LOGGER.info("hasExpired() -> maxInactiveInterval:" + maxInactiveInterval);
long diff = System.currentTimeMillis() - this.thisAccessedTime;
LOGGER.info("hasExpired() -> diff:" + diff);
long maxIncInterval = maxInactiveInterval * 1000L;
LOGGER.info("hasExpired() -> maxIncInterval:" + maxIncInterval);
return maxInactiveInterval >= 0
&& (System.currentTimeMillis() - thisAccessedTime >= maxInactiveInterval * 1000L);
&& (diff >= maxIncInterval);
}
// END SJSAS 6329289

Expand Down Expand Up @@ -1908,11 +1934,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());
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());
if (readThisAccessedTime != 0 && readThisAccessedTime >= thisAccessedTime) {
thisAccessedTime = readThisAccessedTime;
}

LOGGER.info("readRemainingObject -> Thread:" + Thread.currentThread().toString());
LOGGER.info("readRemainingObject -> lastAccessedTime:" + this.lastAccessedTime);
LOGGER.info("readRemainingObject -> thisAccessedTime:" + this.thisAccessedTime);
LOGGER.info("readRemainingObject -> thisCreationTime:" + this.creationTime);
/* SJSWS 6371339
principal = null; // Transient only
// setId((String) stream.readObject());
Expand Down Expand Up @@ -2010,6 +2050,10 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
stream.writeObject(isNew);
stream.writeObject(isValid);
stream.writeObject(thisAccessedTime);
LOGGER.info("writeObject -> Thread:" + Thread.currentThread().toString());
LOGGER.info("writeObject -> lastAccessedTime:" + this.lastAccessedTime);
LOGGER.info("writeObject -> thisAccessedTime:" + this.thisAccessedTime);
LOGGER.info("writeObject -> thisCreationTime:" + this.creationTime);
// START SJSWS 6371339
// If the principal is serializable, write it out
// START PWC 6444754
Expand Down