Skip to content

Commit 28fcfe4

Browse files
committed
core: Allow reuse of "Opaque" file id key
Previously, we were creating Opaque objects multiple times for the same file ID of an Inode. Let Inode return the "opaque" key: introduce a "getFileIdKey" method that always returns the same instance. Related: #149 Signed-off-by: Christian Kohlschütter <[email protected]>
1 parent 37cf57c commit 28fcfe4

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

core/src/main/java/org/dcache/nfs/v4/FileTracker.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public OpenRecord addOpen(NFS4Client client, StateOwner owner, Inode inode, int
242242
// client explicitly requested write delegation
243243
boolean wantWriteDelegation = (shareAccess & nfs4_prot.OPEN4_SHARE_ACCESS_WANT_WRITE_DELEG) != 0;
244244

245-
Opaque fileId = new Opaque(inode.getFileId());
245+
Opaque fileId = inode.getFileIdKey();
246246
Lock lock = filesLock.get(fileId);
247247
lock.lock();
248248
try {
@@ -376,7 +376,7 @@ public OpenRecord addOpen(NFS4Client client, StateOwner owner, Inode inode, int
376376
public stateid4 downgradeOpen(NFS4Client client, stateid4 stateid, Inode inode, int shareAccess, int shareDeny)
377377
throws ChimeraNFSException {
378378

379-
Opaque fileId = new Opaque(inode.getFileId());
379+
Opaque fileId = inode.getFileIdKey();
380380
Lock lock = filesLock.get(fileId);
381381
lock.lock();
382382
try {
@@ -426,7 +426,7 @@ public stateid4 downgradeOpen(NFS4Client client, stateid4 stateid, Inode inode,
426426
public void delegationReturn(NFS4Client client, stateid4 stateid, Inode inode)
427427
throws ChimeraNFSException {
428428

429-
Opaque fileId = new Opaque(inode.getFileId());
429+
Opaque fileId = inode.getFileIdKey();
430430
Lock lock = filesLock.get(fileId);
431431
lock.lock();
432432
try {
@@ -464,7 +464,7 @@ public void delegationReturn(NFS4Client client, stateid4 stateid, Inode inode)
464464
public int getShareAccess(NFS4Client client, Inode inode, stateid4 stateid)
465465
throws ChimeraNFSException {
466466

467-
Opaque fileId = new Opaque(inode.getFileId());
467+
Opaque fileId = inode.getFileIdKey();
468468
Lock lock = filesLock.get(fileId);
469469
lock.lock();
470470
try {
@@ -527,7 +527,7 @@ public int getShareAccess(NFS4Client client, Inode inode, stateid4 stateid)
527527
*/
528528
void removeOpen(Inode inode, stateid4 stateid) {
529529

530-
Opaque fileId = new Opaque(inode.getFileId());
530+
Opaque fileId = inode.getFileIdKey();
531531
Lock lock = filesLock.get(fileId);
532532
lock.lock();
533533
try {

core/src/main/java/org/dcache/nfs/vfs/Inode.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.nio.ByteOrder;
2424
import java.util.Arrays;
2525

26+
import org.dcache.nfs.util.Opaque;
27+
2628
import com.google.common.io.BaseEncoding;
2729

2830
/**
@@ -48,9 +50,10 @@ public class Inode {
4850
private final int generation;
4951
private final int exportIdx;
5052
private final int type;
51-
private final byte[] fs_opaque;
5253
private final byte[] nfsHandle;
5354

55+
private final Opaque opaqueKey;
56+
5457
@Deprecated(forRemoval = true)
5558
public Inode(FileHandle fh) {
5659
this(fh.bytes());
@@ -71,9 +74,9 @@ public Inode(int generation, int exportIdx, int type, byte[] fs_opaque) {
7174
this.generation = generation;
7275
this.exportIdx = exportIdx;
7376
this.type = type;
74-
this.fs_opaque = fs_opaque;
77+
this.opaqueKey = new Opaque(fs_opaque.clone());
7578

76-
this.nfsHandle = buildNfsHandle();
79+
this.nfsHandle = buildNfsHandle(fs_opaque);
7780
}
7881

7982
/**
@@ -106,8 +109,9 @@ public Inode(byte[] bytes) {
106109
exportIdx = b.getInt();
107110
type = (int) b.get();
108111
int olen = (int) b.get();
109-
fs_opaque = new byte[olen];
112+
byte[] fs_opaque = new byte[olen];
110113
b.get(fs_opaque);
114+
this.opaqueKey = new Opaque(fs_opaque);
111115

112116
this.nfsHandle = bytes.clone();
113117
}
@@ -141,14 +145,25 @@ public static Inode forFile(byte[] bytes) {
141145
}
142146

143147
public byte[] getFileId() {
144-
return fs_opaque;
148+
return opaqueKey.getOpaque().clone();
149+
}
150+
151+
/**
152+
* Returns a key suitable for identifying the underlying inode/file referred to by this instance, providing a
153+
* {@link Object#equals(Object)} and {@link Object#hashCode()} implementation that may or may not be different from
154+
* {@link Inode#equals(Object)} and {@link Inode#hashCode()}.
155+
*
156+
* @return The fileId key.
157+
*/
158+
public Opaque getFileIdKey() {
159+
return opaqueKey;
145160
}
146161

147162
public byte[] toNfsHandle() {
148163
return nfsHandle.clone();
149164
}
150165

151-
private byte[] buildNfsHandle() {
166+
private byte[] buildNfsHandle(byte[] fs_opaque) {
152167
int len = fs_opaque.length + MIN_LEN;
153168
byte[] bytes = new byte[len];
154169
ByteBuffer b = ByteBuffer.wrap(bytes);

core/src/main/java/org/dcache/nfs/vfs/VfsCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private void updateLookupCache(Inode parent, String path, Inode inode) {
219219
* @param inode The inode for which cached state value should be invalidated.
220220
*/
221221
public void invalidateStatCache(final Inode inode) {
222-
_statCache.invalidate(new Opaque(inode.getFileId()));
222+
_statCache.invalidate(inode.getFileIdKey());
223223
}
224224

225225
private void updateParentCache(Inode inode, Inode parent) {
@@ -246,7 +246,7 @@ private Inode lookupFromCacheOrLoad(final Inode parent, final String path) throw
246246

247247
private Stat statFromCacheOrLoad(final Inode inode) throws IOException {
248248
try {
249-
return _statCache.get(new Opaque(inode.getFileId()), () -> _inner.getattr(inode));
249+
return _statCache.get(inode.getFileIdKey(), () -> _inner.getattr(inode));
250250
} catch (ExecutionException e) {
251251
Throwable t = e.getCause();
252252
Throwables.throwIfInstanceOf(t, IOException.class);

0 commit comments

Comments
 (0)