Commit 58b20e02 authored by Tigran Mkrtchyan's avatar Tigran Mkrtchyan
Browse files

nfsv41: sessions: use sesionid4 class

in addition patch aswell chenges Map to Cache with
maximal idle time equals to 2xLEASE_TIME
parent 13baab12
......@@ -3,6 +3,7 @@ package org.dcache.chimera.nfs.v4;
import java.util.List;
import org.dcache.chimera.nfs.ChimeraNFSException;
import java.util.concurrent.atomic.AtomicLong;
import org.dcache.chimera.nfs.v4.xdr.sessionid4;
import org.dcache.chimera.nfs.v4.xdr.nfs_resop4;
import org.dcache.chimera.nfs.v4.xdr.nfsstat4;
......@@ -11,7 +12,8 @@ public class NFSv41Session {
private static final AtomicLong SESSIONS = new AtomicLong(0);
private final byte[] _session = (Long.toString(SESSIONS.incrementAndGet()) + "###############").getBytes();
private final sessionid4 _session =
new sessionid4((Long.toString(SESSIONS.incrementAndGet()) + "###############").getBytes());
/**
* Session reply slots.
*/
......@@ -24,11 +26,8 @@ public class NFSv41Session {
_slots = new SessionSlot[replyCacheSize];
}
public byte[] id() {
byte[] id = new byte[16];
System.arraycopy(_session, 0, id, 0, 16);
return id;
public sessionid4 id() {
return _session;
}
public NFS4Client getClient() {
......@@ -69,7 +68,7 @@ public class NFSv41Session {
@Override
public String toString() {
String s = String.format("Session: [%s]", new String(_session));
String s = String.format("Session: [%s]", _session);
return s;
}
}
......@@ -9,7 +9,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.dcache.chimera.nfs.v4.xdr.sessionid4;
import org.dcache.utils.Cache;
public class NFSv4StateHandler {
......@@ -27,7 +30,10 @@ public class NFSv4StateHandler {
// mapping between server generated clietid and nfs_client_id, not confirmed yet
private static Map<Long, NFS4Client> _clientsByServerId = new HashMap<Long, NFS4Client>();
private static Map<String, Long> _clientsByStateId = new HashMap<String, Long>();
private static Map<String, NFSv41Session> _sessionById = new HashMap<String, NFSv41Session>();
private final Cache<sessionid4, NFSv41Session> _sessionById =
new Cache<sessionid4, NFSv41Session>("NFSv41 sessions", 5000, Long.MAX_VALUE, TimeUnit.SECONDS.toMillis(NFSv4Defaults.NFS4_LEASE_TIME*2));
private static Map<String, NFS4Client> _clientByOwner = new HashMap<String, NFS4Client>();
private NFSv4StateHandler() {}
......@@ -40,7 +46,7 @@ public class NFSv4StateHandler {
public void removeClient(NFS4Client client) {
for(NFSv41Session session: client.sessions() ) {
_sessionById.remove( new String( session.id() ) );
_sessionById.remove( session.id() );
}
_clientsByServerId.remove(client.id_srv());
_clientsByServerId.remove(client.id_srv());
......@@ -86,13 +92,12 @@ public class NFSv4StateHandler {
}
public NFSv41Session sessionById( byte[] id ) {
NFSv41Session session = _sessionById.get(new String(id));
return session;
public NFSv41Session sessionById( sessionid4 id) {
return _sessionById.get(id);
}
public void sessionById( byte[] id, NFSv41Session session) {
_sessionById.put(new String(id), session);
public void sessionById( sessionid4 id, NFSv41Session session) {
_sessionById.put(id, session);
}
public NFS4Client clientByOwner( String ownerid) {
......
......@@ -2,7 +2,6 @@ package org.dcache.chimera.nfs.v4;
import org.dcache.chimera.nfs.v4.xdr.nfsstat4;
import org.dcache.chimera.nfs.v4.xdr.sequenceid4;
import org.dcache.chimera.nfs.v4.xdr.sessionid4;
import org.dcache.chimera.nfs.v4.xdr.uint32_t;
import org.dcache.chimera.nfs.v4.xdr.nfs_argop4;
import org.dcache.chimera.nfs.v4.xdr.nfs4_prot;
......@@ -115,7 +114,7 @@ public class OperationCREATE_SESSION extends AbstractNFSv4Operation {
session = new NFSv41Session(client,_args.opcreate_session.csa_fore_chan_attrs.ca_maxrequests.value.value );
client.addSession( session);
_log.log(Level.FINE, "adding new session [{0}]", new String(session.id()) );
_log.log(Level.FINE, "adding new session [{0}]", session.id() );
NFSv4StateHandler.getInstace().sessionById(session.id(), session);
client.confirmed();
client.nextSeqID();
......@@ -129,7 +128,7 @@ public class OperationCREATE_SESSION extends AbstractNFSv4Operation {
res.csr_resok4 = new CREATE_SESSION4resok();
res.csr_resok4.csr_sessionid = new sessionid4( session.id() );
res.csr_resok4.csr_sessionid = session.id();
res.csr_resok4.csr_sequence = _args.opcreate_session.csa_sequence;
/**
* from spec:
......
......@@ -23,7 +23,7 @@ public class OperationDESTROY_SESSION extends AbstractNFSv4Operation {
try {
NFSv41Session session = NFSv4StateHandler.getInstace().sessionById(_args.opdestroy_session.dsa_sessionid.value);
NFSv41Session session = NFSv4StateHandler.getInstace().sessionById(_args.opdestroy_session.dsa_sessionid);
if(session == null) {
throw new ChimeraNFSException(nfsstat4.NFS4ERR_BADSESSION, "client not found");
}
......
......@@ -57,7 +57,7 @@ public class OperationSEQUENCE extends AbstractNFSv4Operation {
res.sr_resok4.sr_target_highest_slotid = new slotid4(_args.opsequence.sa_slotid.value);
res.sr_resok4.sr_sessionid = new sessionid4(_args.opsequence.sa_sessionid.value);
NFSv41Session session = NFSv4StateHandler.getInstace().sessionById(_args.opsequence.sa_sessionid.value);
NFSv41Session session = NFSv4StateHandler.getInstace().sessionById(_args.opsequence.sa_sessionid);
if(session == null ) {
throw new ChimeraNFSException(nfsstat4.NFS4ERR_BADSESSION, "client not found");
......@@ -66,7 +66,7 @@ public class OperationSEQUENCE extends AbstractNFSv4Operation {
NFS4Client client = session.getClient();
if( client.sessionsEmpty(session)) {
_log.log(Level.FINEST, "no session for id [{0}]", new String(_args.opsequence.sa_sessionid.value) );
_log.log(Level.FINEST, "no session for id [{0}]", _args.opsequence.sa_sessionid );
throw new ChimeraNFSException(nfsstat4.NFS4ERR_BADSESSION, "client not found");
}
......
......@@ -4,7 +4,6 @@
* See http://remotetea.sourceforge.net for details
*/
package org.dcache.chimera.nfs.v4.xdr;
import org.dcache.chimera.nfs.v4.*;
import org.dcache.xdr.*;
import java.io.IOException;
......
......@@ -4,6 +4,7 @@
* See http://remotetea.sourceforge.net for details
*/
package org.dcache.chimera.nfs.v4.xdr;
import java.util.Arrays;
import org.dcache.xdr.*;
import java.io.IOException;
......@@ -33,5 +34,26 @@ public class sessionid4 implements XdrAble {
value = xdr.xdrDecodeOpaque(nfs4_prot.NFS4_SESSIONID_SIZE);
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if ( !(obj instanceof sessionid4 )) return false;
final sessionid4 other = (sessionid4) obj;
return Arrays.equals(this.value, other.value);
}
@Override
public int hashCode() {
int hash = 7;
hash = 23 * hash + Arrays.hashCode(this.value);
return hash;
}
@Override
public String toString() {
return new String(value);
}
}
// End of sessionid4.java
......@@ -37,7 +37,7 @@ public class verifier4 implements XdrAble {
@Override
public int hashCode() {
return value.length;
return Arrays.hashCode(value);
}
@Override
......
/*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program (see the file COPYING.LIB for more
* details); if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.dcache.chimera.nfs.v4.xdr;
import org.junit.Test;
import static org.junit.Assert.*;
public class sessionid4Test {
@Test
public void testEqualsTrue() {
byte[] id = "bla".getBytes();
sessionid4 session1 = new sessionid4(id);
sessionid4 session2 = new sessionid4(id);
assertTrue("equlas sessions not recognized", session1.equals(session2));
assertTrue("equlas sessions sould have the same hashCode",
session1.hashCode() == session2.hashCode());
}
@Test
public void testEqualsFalse() {
byte[] id1 = "bla".getBytes();
byte[] id2 = "blabla".getBytes();
sessionid4 session1 = new sessionid4(id1);
sessionid4 session2 = new sessionid4(id2);
assertFalse("not equal sessions not recognized", session1.equals(session2));
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment