Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,17 @@ protected XrootdResponse<AuthenticationRequest> doOnAuthentication(ChannelHandle
return withOk(msg);
}

/**
* Reports the client's connection as the network flow of the transfer. This is not done when
* the pool is the destination of a third-party copy: the file then arrives over a connection
* that the third-party client opens to the source, and the connection from the client that
* requested the copy carries no file data.
*/
private static void reportDataConnection(ChannelHandlerContext ctx,
NettyTransferService<XrootdProtocolInfo>.NettyMoverChannel file) {
file.connected(ctx.channel().remoteAddress(), ctx.channel().localAddress());
}

/**
* Obtains the right mover channel using an opaque token in the request. The mover channel is
* wrapped by a file descriptor. The file descriptor is stored for subsequent access.
Expand Down Expand Up @@ -444,9 +455,11 @@ protected XrootdResponse<OpenRequest> doOnOpen(ChannelHandlerContext ctx,
tlsSessionInfo);
} else {
descriptor = new WriteDescriptor(file, posc);
reportDataConnection(ctx, file);
}
} else {
descriptor = new ReadDescriptor(file);
reportDataConnection(ctx, file);
}

FileStatus stat = msg.isRetStat() ? stat(file) : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ protected ChannelFuture doOnGet(ChannelHandlerContext context,
"Resource is not open for reading");
}

// This connection carries the file: report it as the transfer's network flow.
file.connected(context.channel().remoteAddress(), context.channel().localAddress());

fileSize = file.size();
ranges = parseHttpRange(request, 0, fileSize - 1);
} catch (Redirect e) {
Expand Down Expand Up @@ -547,6 +550,9 @@ protected ChannelFuture doOnPut(ChannelHandlerContext context, HttpRequest reque
"Resource is not open for writing");
}

// This connection carries the file: report it as the transfer's network flow.
file.connected(context.channel().remoteAddress(), context.channel().localAddress());

contentMd5Checksum(request).ifPresent(file::addChecksum);

OptionalLong contentLength = contentLength(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,18 @@ public void sendFinished(Mover<?> mover, MoverInfoMessage moverInfoMessage) {
protocolName(mover),
mover.getFileAttributes().getPnfsId(),
transferTag(mover));
} else if (mover.isPassive() && mover.getRemoteEndpoint().isEmpty()) {
// Nobody connected to the mover: there was no flow and no start marker was sent.
SCITAGS_LOGGER.debug(
"scitags lifecycle=end skip reason=no-data-connection protocol={} pnfsid={} transferTag={}",
protocolName(mover),
mover.getFileAttributes().getPnfsId(),
transferTag(mover));
} else {
InetSocketAddress remoteEndpoint = ipProtocolInfo.getSocketAddress();
// Prefer the endpoint of the connection the mover made, as reported in the start
// marker; the address in the protocol info is the one the door resolved.
InetSocketAddress remoteEndpoint = mover.getRemoteEndpoint()
.orElseGet(ipProtocolInfo::getSocketAddress);
if (remoteEndpoint == null) {
SCITAGS_LOGGER.debug(
"scitags lifecycle=end skip reason=no-remote-endpoint protocol={} pnfsid={} transferTag={}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public abstract class AbstractMover<P extends ProtocolInfo, M extends AbstractMo
private volatile Optional<RepositoryChannel> _channel = Optional.empty();

private volatile InetSocketAddress _localEndpoint;
private volatile InetSocketAddress _remoteEndpoint;

public AbstractMover(ReplicaDescriptor handle, PoolIoFileMessage message, CellPath pathToDoor,
TransferService<M> transferService) {
Expand Down Expand Up @@ -377,4 +378,13 @@ public void setLocalEndpoint(InetSocketAddress addr) {
public Optional<InetSocketAddress> getLocalEndpoint() {
return Optional.ofNullable(_localEndpoint);
}

public void setRemoteEndpoint(InetSocketAddress addr) {
_remoteEndpoint = addr;
}

@Override
public Optional<InetSocketAddress> getRemoteEndpoint() {
return Optional.ofNullable(_remoteEndpoint);
}
}
19 changes: 19 additions & 0 deletions modules/dcache/src/main/java/org/dcache/pool/movers/Mover.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ default List<InetSocketAddress> remoteConnections() {
return null;
}

/**
* Whether this mover waits for the remote party to connect to it. Such a mover has no data
* connection, and therefore no network flow, until {@link #getRemoteEndpoint()} returns a
* value.
*/
default boolean isPassive() {
return false;
}

/**
* Provide the expected total number of bytes transferred for this transfer, if known. Returns
* null if this value is unknown.
Expand All @@ -219,4 +228,14 @@ default Long getBytesExpected() {
* to access the mover.
*/
Optional<InetSocketAddress> getLocalEndpoint();

/**
* Returns the {@link Optional} containing {@link InetSocketAddress} of the remote endpoint of
* the connection that carries the data, if the mover established that connection itself
* and knows it. This may differ from the address in the protocol info, for example if the
* remote server redirected the request.
*/
default Optional<InetSocketAddress> getRemoteEndpoint() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ public NettyMover(ReplicaDescriptor handle,
public UUID getUuid() {
return uuid;
}

/**
* A Netty mover listens for the client: the data connection exists once a client has
* presented the UUID of this mover.
*/
@Override
public boolean isPassive() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;
import java.util.ArrayList;
Expand All @@ -61,6 +62,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import org.dcache.cells.CellStub;
import org.dcache.pool.classic.Cancellable;
Expand Down Expand Up @@ -368,6 +370,7 @@ public void execute()
UUID uuid = mover.getUuid();
NettyMoverChannel channel =
autoclose(new NettyMoverChannel(uuid,
mover,
mover.open(),
connectTimeoutUnit.toMillis(connectTimeout), this,
mover::addChecksumType,
Expand All @@ -382,9 +385,12 @@ public void execute()

InetSocketAddress localEndpoint = new InetSocketAddress(localIP, getServerAddress().getPort());
mover.setLocalEndpoint(localEndpoint);
transferLifeCycle.onStart(((IpProtocolInfo)mover.getProtocolInfo()).getSocketAddress(),
localEndpoint, mover.getProtocolInfo(), mover.getSubject());

/* No flow marker is sent here: the client has not connected yet, so the
* endpoints of the data connection are unknown. The address in the protocol
* info is that of the client's connection to the door. The marker is sent by
* NettyMoverChannel#connected once the client has attached to the mover.
*/
sendAddressToDoor(mover, localEndpoint);
}

Expand Down Expand Up @@ -443,15 +449,19 @@ public class NettyMoverChannel extends MoverChannelDecorator<P> implements Cance
private final Consumer<ChecksumType> checksumCalculation;
private final Consumer<Checksum> integrityChecker;
private final UUID moverUuid;
private final NettyMover<P> mover;
private final AtomicBoolean isConnected = new AtomicBoolean();

public NettyMoverChannel(UUID moverUuid,
NettyMover<P> mover,
MoverChannel<P> file,
long connectTimeout,
CompletionHandler<Void, Void> completionHandler,
Consumer<ChecksumType> checksumCalculation,
Consumer<Checksum> integrityChecker) {
super(file);
this.moverUuid = moverUuid;
this.mover = mover;
this.completionHandler = completionHandler;
this.checksumCalculation = checksumCalculation;
this.integrityChecker = integrityChecker;
Expand Down Expand Up @@ -480,6 +490,31 @@ public UUID getMoverUuid() {
return moverUuid;
}

/**
* Reports the connection through which a client has attached to this mover. This is the
* connection that carries the data, so its endpoints, rather than those the door saw,
* identify the network flow. The first connection reported is recorded on the mover
* and announced with a start flow marker; the end marker reuses the same endpoints.
* Further connections to the same mover (keep-alive requests, reconnects) are ignored.
*
* @param remote the client's end of the connection
* @param local the pool's end of the connection
*/
public void connected(SocketAddress remote, SocketAddress local) {
if (remote instanceof InetSocketAddress remoteEndpoint
&& local instanceof InetSocketAddress localEndpoint
&& isConnected.compareAndSet(false, true)) {
mover.setLocalEndpoint(localEndpoint);
mover.setRemoteEndpoint(remoteEndpoint);
if (transferLifeCycle != null) {
try (CDC ignored = cdc.restore()) {
transferLifeCycle.onStart(remoteEndpoint, localEndpoint,
mover.getProtocolInfo(), mover.getSubject());
}
}
}
}

@Override
public int read(ByteBuffer dst) throws IOException {
checkState(sync.isExclusive());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package org.dcache.pool.movers;

import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import diskCacheV111.vehicles.ProtocolInfo;
import io.netty.channel.local.LocalAddress;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.nio.channels.CompletionHandler;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import javax.security.auth.Subject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* Tests that flow markers of a Netty mover describe the connection the client makes to the pool,
* not the client's connection to the door.
*/
public class NettyTransferServiceFlowMarkerTest {

private static final InetSocketAddress CLIENT_DATA_CONNECTION =
new InetSocketAddress("203.0.113.20", 47742);
private static final InetSocketAddress POOL_DATA_CONNECTION =
new InetSocketAddress("198.51.100.55", 47548);

private ScheduledExecutorService scheduler;
private TransferLifeCycle transferLifeCycle;
private NettyMover<ProtocolInfo> mover;
private ProtocolInfo protocolInfo;
private Subject subject;
private NettyTransferService<ProtocolInfo>.NettyMoverChannel channel;

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
scheduler = Executors.newSingleThreadScheduledExecutor();
transferLifeCycle = mock(TransferLifeCycle.class);
protocolInfo = mock(ProtocolInfo.class);
subject = new Subject();

mover = mock(NettyMover.class);
given(mover.getProtocolInfo()).willReturn(protocolInfo);
given(mover.getSubject()).willReturn(subject);

NettyTransferService<ProtocolInfo> service = new NettyTransferService<>("test") {
@Override
protected void sendAddressToDoor(NettyMover<ProtocolInfo> mover,
InetSocketAddress localEndpoint) {
}

@Override
protected UUID createUuid(ProtocolInfo protocolInfo) {
return UUID.randomUUID();
}
};
service.setTransferLifeCycle(transferLifeCycle);

Field timeoutScheduler = NettyTransferService.class.getDeclaredField("timeoutScheduler");
timeoutScheduler.setAccessible(true);
timeoutScheduler.set(service, scheduler);

channel = service.new NettyMoverChannel(UUID.randomUUID(), mover,
mock(MoverChannel.class), 60_000, mock(CompletionHandler.class),
type -> {}, checksum -> {});
}

@After
public void tearDown() {
scheduler.shutdownNow();
}

@Test
public void shouldSendStartMarkerForTheConnectionTheClientMadeToThePool() {
channel.connected(CLIENT_DATA_CONNECTION, POOL_DATA_CONNECTION);

verify(transferLifeCycle).onStart(CLIENT_DATA_CONNECTION, POOL_DATA_CONNECTION,
protocolInfo, subject);
}

@Test
public void shouldRecordEndpointsOfDataConnectionOnMoverForEndMarker() {
channel.connected(CLIENT_DATA_CONNECTION, POOL_DATA_CONNECTION);

verify(mover).setRemoteEndpoint(CLIENT_DATA_CONNECTION);
verify(mover).setLocalEndpoint(POOL_DATA_CONNECTION);
}

@Test
public void shouldNotSendStartMarkerBeforeClientConnects() {
verify(transferLifeCycle, never()).onStart(any(), any(), any(), any());
verify(mover, never()).setRemoteEndpoint(any());
}

@Test
public void shouldSendOnlyOneStartMarkerWhenFurtherConnectionsAttach() {
channel.connected(CLIENT_DATA_CONNECTION, POOL_DATA_CONNECTION);
channel.connected(CLIENT_DATA_CONNECTION, POOL_DATA_CONNECTION);
channel.connected(new InetSocketAddress("203.0.113.20", 50000), POOL_DATA_CONNECTION);

verify(transferLifeCycle, times(1)).onStart(any(), any(), any(), any());
verify(mover, times(1)).setRemoteEndpoint(CLIENT_DATA_CONNECTION);
}

@Test
public void shouldIgnoreConnectionsThatAreNotIpSockets() {
channel.connected(new LocalAddress("client"), new LocalAddress("pool"));

verify(transferLifeCycle, never()).onStart(any(), any(), any(), any());
verify(mover, never()).setRemoteEndpoint(any());
}

@Test
public void nettyMoverShouldBePassive() {
NettyMover<ProtocolInfo> realMover = mock(NettyMover.class,
org.mockito.Mockito.CALLS_REAL_METHODS);

assertTrue(realMover.isPassive());
}
}
Loading