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 @@ -129,6 +129,11 @@ public final class TpcWriteDescriptor extends WriteDescriptor
private Integer transferStatus;
private int clientChunkSize;

/*
* Whether the connection that carries the file has been reported to the mover.
*/
private volatile boolean isDataConnectionReported;

public TpcWriteDescriptor(NettyTransferService<XrootdProtocolInfo>.NettyMoverChannel channel,
boolean posc,
ChannelHandlerContext ctx,
Expand Down Expand Up @@ -328,9 +333,33 @@ public synchronized XrootdResponse<SyncRequest> sync(SyncRequest syncRequest)
@Override
public void write(InboundReadResponse inboundReadResponse)
throws IOException {
reportDataConnection();
write((ByteBuffersProvider) inboundReadResponse);
}

/**
* Reports the connection over which the file arrives as the network flow of the transfer.
* The connection from the client that requested the copy carries no file data; the file
* arrives over the connection the third-party client has made to the source. That
* connection is only known for certain once data arrives, since the source may redirect
* the client to another server first.
*/
private void reportDataConnection() {
if (isDataConnectionReported) {
return;
}
isDataConnectionReported = true;

XrootdTpcClient current = client;
ChannelFuture future = current == null ? null : current.getChannelFuture();
Channel channel = future == null ? null : future.channel();
if (channel == null) {
LOGGER.debug("Third-party client has no channel; data connection not reported.");
return;
}
getChannel().connected(channel.remoteAddress(), channel.localAddress());
}

@Override
public void close() {
if (client == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package org.dcache.xrootd.tpc;

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 io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import org.dcache.pool.movers.NettyTransferService;
import org.dcache.vehicles.XrootdProtocolInfo;
import org.dcache.xrootd.pool.XrootdTransferService;
import org.dcache.xrootd.tpc.protocol.messages.InboundReadResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* Tests that, when the pool is the destination of an xrootd third-party copy, the flow marker
* describes the connection over which the file arrives from the source.
*/
public class TpcWriteDescriptorFlowMarkerTest {

private static final InetSocketAddress SOURCE =
new InetSocketAddress("203.0.113.30", 1094);
private static final InetSocketAddress REDIRECTED_SOURCE =
new InetSocketAddress("203.0.113.31", 21468);
private static final InetSocketAddress POOL =
new InetSocketAddress("198.51.100.55", 44034);

private ScheduledExecutorService executor;
private NettyTransferService<XrootdProtocolInfo>.NettyMoverChannel moverChannel;
private TpcWriteDescriptor descriptor;

@Before
@SuppressWarnings("unchecked")
public void setup() {
executor = Executors.newSingleThreadScheduledExecutor();

moverChannel = mock(NettyTransferService.NettyMoverChannel.class);
given(moverChannel.getProtocolInfo()).willReturn(mock(XrootdProtocolInfo.class));

XrootdTransferService service = mock(XrootdTransferService.class);
given(service.getThirdPartyShutdownExecutor()).willReturn(executor);
given(service.getTpcClientChunkSize()).willReturn(1 << 20);
given(service.getTpcClientPlugins()).willReturn(Collections.emptyList());

XrootdTpcInfo info = mock(XrootdTpcInfo.class);
given(info.getSrcHost()).willReturn("127.0.0.1");
given(info.getSrcPort()).willReturn(1094);

descriptor = new TpcWriteDescriptor(moverChannel, false,
mock(ChannelHandlerContext.class), service, "user", info, null);
}

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

@Test
public void shouldNotReportAnyConnectionBeforeDataArrives() {
verify(moverChannel, never()).connected(any(), any());
}

@Test
public void shouldReportConnectionToSourceWhenFirstDataArrives() throws Exception {
givenThirdPartyClientConnectedTo(SOURCE);

descriptor.write(someData());

verify(moverChannel).connected(SOURCE, POOL);
}

@Test
public void shouldReportConnectionOnlyOnce() throws Exception {
givenThirdPartyClientConnectedTo(SOURCE);

descriptor.write(someData());
descriptor.write(someData());
descriptor.write(someData());

verify(moverChannel, times(1)).connected(any(), any());
}

@Test
public void shouldReportConnectionOfClientThatReceivesDataAfterRedirect() throws Exception {
givenThirdPartyClientConnectedTo(SOURCE);
givenThirdPartyClientConnectedTo(REDIRECTED_SOURCE);

descriptor.write(someData());

verify(moverChannel).connected(REDIRECTED_SOURCE, POOL);
verify(moverChannel, never()).connected(SOURCE, POOL);
}

@Test
public void shouldNotFailWhenClientHasNoChannel() throws Exception {
setClient(mock(XrootdTpcClient.class));

descriptor.write(someData());

verify(moverChannel, never()).connected(any(), any());
}

private void givenThirdPartyClientConnectedTo(InetSocketAddress remote) throws Exception {
Channel channel = mock(Channel.class);
given(channel.remoteAddress()).willReturn(remote);
given(channel.localAddress()).willReturn(POOL);
ChannelFuture future = mock(ChannelFuture.class);
given(future.channel()).willReturn(channel);
XrootdTpcClient client = mock(XrootdTpcClient.class);
given(client.getChannelFuture()).willReturn(future);
setClient(client);
}

private void setClient(XrootdTpcClient client) throws Exception {
Field field = TpcWriteDescriptor.class.getDeclaredField("client");
field.setAccessible(true);
field.set(descriptor, client);
}

private static InboundReadResponse someData() {
InboundReadResponse response = mock(InboundReadResponse.class);
given(response.getWriteOffset()).willReturn(0L);
given(response.toByteBuffers()).willReturn(new java.nio.ByteBuffer[0]);
return response;
}
}
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;
}
}
Loading
Loading