Skip to content

Commit e146568

Browse files
committed
nfs4: add FileTracker#getDelegations method
a method that might be used to list current delegations. Acked-by: Paul Millar Target: master
1 parent 1832061 commit e146568

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,19 @@ public Map<Inode, Collection<NFS4Client>> getOpenFiles() {
572572
e -> e.getValue().stream().map(OpenState::getClient).collect(Collectors.toSet()))
573573
);
574574
}
575+
576+
577+
/**
578+
* Get all currently issued delegations. The resulting map contains file's inodes
579+
* as key and collection of nfs clients that hold the delegation as a value.
580+
*
581+
* @return map of all currently issued delegations.
582+
*/
583+
public Map<Inode, Collection<NFS4Client>> getDelegations() {
584+
return delegations.entrySet().stream()
585+
.collect(Collectors.toMap(
586+
e -> Inode.forFile(e.getKey().getOpaque()),
587+
e -> e.getValue().stream().map(DelegationState::client).collect(Collectors.toSet()))
588+
);
589+
}
575590
}

core/src/test/java/org/dcache/nfs/v4/FileTrackerTest.java

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,35 @@
1919
*/
2020
package org.dcache.nfs.v4;
2121

22+
import java.net.UnknownHostException;
2223
import java.nio.charset.StandardCharsets;
24+
25+
import org.dcache.nfs.ChimeraNFSException;
2326
import org.dcache.nfs.status.BadStateidException;
2427
import org.dcache.nfs.status.DelayException;
2528
import org.dcache.nfs.status.InvalException;
2629
import org.dcache.nfs.status.ShareDeniedException;
27-
import org.dcache.nfs.v4.xdr.nfs4_prot;
30+
import org.hamcrest.Matchers;
2831
import org.junit.Before;
2932
import org.junit.Test;
3033

3134
import org.dcache.nfs.v4.xdr.nfs_fh4;
3235
import org.dcache.nfs.vfs.Inode;
3336

34-
import static org.junit.Assert.*;
37+
import static org.dcache.nfs.v4.xdr.nfs4_prot.OPEN4_SHARE_ACCESS_WANT_READ_DELEG;
3538
import static org.dcache.nfs.v4.NfsTestUtils.createClient;
3639
import static org.dcache.nfs.v4.NfsTestUtils.generateFileHandle;
3740

3841
import static org.dcache.nfs.v4.xdr.nfs4_prot.OPEN4_SHARE_ACCESS_READ;
3942
import static org.dcache.nfs.v4.xdr.nfs4_prot.OPEN4_SHARE_ACCESS_WRITE;
4043
import static org.dcache.nfs.v4.xdr.nfs4_prot.OPEN4_SHARE_ACCESS_BOTH;
4144
import static org.dcache.nfs.v4.xdr.nfs4_prot.OPEN4_SHARE_ACCESS_WANT_NO_DELEG;
45+
import static org.hamcrest.MatcherAssert.assertThat;
46+
import static org.junit.Assert.assertEquals;
47+
import static org.junit.Assert.assertFalse;
48+
import static org.junit.Assert.assertNotEquals;
49+
import static org.junit.Assert.assertTrue;
50+
import static org.junit.Assert.fail;
4251
import static org.mockito.ArgumentMatchers.any;
4352
import static org.mockito.ArgumentMatchers.anyBoolean;
4453
import static org.mockito.Mockito.mock;
@@ -216,7 +225,7 @@ public void shouldGetReadDelegation() throws Exception {
216225
nfs_fh4 fh = generateFileHandle();
217226
Inode inode = Inode.forFile(fh.value);
218227

219-
var openRecord = tracker.addOpen(client, stateOwner1, inode, OPEN4_SHARE_ACCESS_READ | nfs4_prot.OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
228+
var openRecord = tracker.addOpen(client, stateOwner1, inode, OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
220229
assertTrue("Read delegation not granted", openRecord.hasDelegation());
221230
}
222231

@@ -248,7 +257,7 @@ public void shouldReCallReadDelegationOnConflict() throws Exception {
248257
nfs_fh4 fh = generateFileHandle();
249258
Inode inode = Inode.forFile(fh.value);
250259

251-
var openRecord1 = tracker.addOpen(client1, stateOwner1, inode, OPEN4_SHARE_ACCESS_READ | nfs4_prot.OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
260+
var openRecord1 = tracker.addOpen(client1, stateOwner1, inode, OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
252261
try {
253262
var openRecord2 = tracker.addOpen(client2, stateOwner2, inode, OPEN4_SHARE_ACCESS_WRITE,
254263
0);
@@ -272,8 +281,8 @@ public void shouldAllowMultipleReadDelegation() throws Exception {
272281
nfs_fh4 fh = generateFileHandle();
273282
Inode inode = Inode.forFile(fh.value);
274283

275-
var openRecord1 = tracker.addOpen(client1, stateOwner1, inode, OPEN4_SHARE_ACCESS_READ | nfs4_prot.OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
276-
var openRecord2 = tracker.addOpen(client2, stateOwner2, inode, OPEN4_SHARE_ACCESS_READ | nfs4_prot.OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
284+
var openRecord1 = tracker.addOpen(client1, stateOwner1, inode, OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
285+
var openRecord2 = tracker.addOpen(client2, stateOwner2, inode, OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
277286

278287
assertTrue("Read delegation not granted", openRecord2.hasDelegation());
279288

@@ -310,4 +319,49 @@ public void shouldNotIssueReadDelegation() throws Exception {
310319
var openRecord2 = tracker.addOpen(client, stateOwner, inode, OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WANT_NO_DELEG, 0);
311320
assertFalse("Unwanted delegation", openRecord2.hasDelegation());
312321
}
322+
323+
324+
@Test
325+
public void getOpenFiles() throws UnknownHostException, ChimeraNFSException {
326+
327+
NFS4Client client1 = createClient(sh);
328+
NFS4Client client2 = createClient(sh);
329+
StateOwner stateOwner1 = client1.getOrCreateOwner("client1".getBytes(StandardCharsets.UTF_8), new seqid4(0));
330+
StateOwner stateOwner2 = client2.getOrCreateOwner("client2".getBytes(StandardCharsets.UTF_8), new seqid4(0));
331+
332+
nfs_fh4 fh = generateFileHandle();
333+
Inode inode = Inode.forFile(fh.value);
334+
335+
tracker.addOpen(client1, stateOwner1, inode, OPEN4_SHARE_ACCESS_READ, 0);
336+
tracker.addOpen(client2, stateOwner2, inode, OPEN4_SHARE_ACCESS_READ, 0);
337+
338+
var openFiles = tracker.getOpenFiles();
339+
340+
assertEquals("Number of open files not as expected", 1, openFiles.size());
341+
342+
var clients = openFiles.get(inode);
343+
assertThat("Expected clients not found", clients, Matchers.containsInAnyOrder(client1,client2));
344+
}
345+
346+
@Test
347+
public void getDelegations() throws UnknownHostException, ChimeraNFSException {
348+
349+
NFS4Client client1 = createClient(sh);
350+
NFS4Client client2 = createClient(sh);
351+
StateOwner stateOwner1 = client1.getOrCreateOwner("client1".getBytes(StandardCharsets.UTF_8), new seqid4(0));
352+
StateOwner stateOwner2 = client2.getOrCreateOwner("client2".getBytes(StandardCharsets.UTF_8), new seqid4(0));
353+
354+
nfs_fh4 fh = generateFileHandle();
355+
Inode inode = Inode.forFile(fh.value);
356+
357+
tracker.addOpen(client1, stateOwner1, inode, OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
358+
tracker.addOpen(client2, stateOwner2, inode, OPEN4_SHARE_ACCESS_READ | OPEN4_SHARE_ACCESS_WANT_READ_DELEG, 0);
359+
360+
var delegations = tracker.getDelegations();
361+
362+
assertEquals("Number of open files not as expected", 1, delegations.size());
363+
364+
var clients = delegations.get(inode);
365+
assertThat("Expected clients not found", clients, Matchers.containsInAnyOrder(client1,client2));
366+
}
313367
}

0 commit comments

Comments
 (0)