Skip to content

Commit

Permalink
make sure test waits for both subscriptions to have images. Not just …
Browse files Browse the repository at this point in the history
…one.
  • Loading branch information
tmontgomery committed Dec 8, 2016
1 parent 34dad87 commit 5773d3b
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright 2014 - 2016 Real Logic Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.aeron.driver;

import io.aeron.protocol.StatusMessageFlyweight;
import org.agrona.BitUtil;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import static io.aeron.logbuffer.LogBufferDescriptor.computePosition;
import static java.lang.System.getProperty;

/**
* Minimum multicast sender flow control strategy only for preferred members.
*
* Min of right edges where edge is to a preferred receiver
* Tracking of preferred receivers for X seconds
*/
public class PreferredMulticastFlowControl implements FlowControl
{
private static final String RECEIVER_TIMEOUT_PROP_NAME = "aeron.PreferredMulticastFlowControl.receiverTimeout";
private static final long RECEIVER_TIMEOUT_DEFAULT = TimeUnit.SECONDS.toNanos(2);

private static final long RECEIVER_TIMEOUT = Long.getLong(RECEIVER_TIMEOUT_PROP_NAME, RECEIVER_TIMEOUT_DEFAULT);

private static final String PREFERRED_ASF_PROP_NAME = "aeron.PreferredMulticastFlowControl.asf";
private static final String PREFERRED_ASF_DEFAULT = "FFFFFFFF";
public static final String PREFERRED_ASF = getProperty(PREFERRED_ASF_PROP_NAME, PREFERRED_ASF_DEFAULT);
public static final byte[] PREFERRED_ASF_BYTES = BitUtil.fromHex(PREFERRED_ASF);

private final ArrayList<Receiver> receiverList = new ArrayList<>();
private final byte[] smAsf = new byte[64];

/**
* {@inheritDoc}
*/
public long onStatusMessage(
final StatusMessageFlyweight flyweight,
final InetSocketAddress receiverAddress,
final long senderLimit,
final int initialTermId,
final int positionBitsToShift,
final long now)
{
final long position =
computePosition(
flyweight.consumptionTermId(),
flyweight.consumptionTermOffset(),
positionBitsToShift,
initialTermId);

final long windowLength = flyweight.receiverWindowLength();
final long receiverId = flyweight.receiverId();
final boolean isFromPreferred = isFromPreferred(flyweight);
boolean isExisting = false;
long minPosition = Long.MAX_VALUE;

for (final Receiver receiver : receiverList)
{
if (isFromPreferred && receiverId == receiver.receiverId)
{
receiver.lastPositionPlusWindow = position + windowLength;
receiver.timeOfLastStatusMessage = now;
isExisting = true;
}

minPosition = Math.min(minPosition, receiver.lastPositionPlusWindow);
}

if (isFromPreferred && !isExisting)
{
receiverList.add(new Receiver(position + windowLength, now, receiverId, receiverAddress));
minPosition = Math.min(minPosition, position + windowLength);
}

return Math.max(senderLimit, minPosition);
}

/**
* {@inheritDoc}
*/
public void initialize(final int initialTermId, final int termBufferCapacity)
{
}

/**
* {@inheritDoc}
*/
public long onIdle(final long now, final long senderLimit)
{
long minPosition = Long.MAX_VALUE;

for (int i = receiverList.size() - 1; i >= 0; i--)
{
final Receiver receiver = receiverList.get(i);
if (now > (receiver.timeOfLastStatusMessage + RECEIVER_TIMEOUT))
{
receiverList.remove(i);
}
else
{
minPosition = Math.min(minPosition, receiver.lastPositionPlusWindow);
}
}

return (receiverList.size() > 0) ? minPosition : senderLimit;
}

public boolean isFromPreferred(final StatusMessageFlyweight sm)
{
final int asfLength = sm.applicationSpecificFeedback(smAsf);
boolean result = false;

// default ASF is 4 bytes
if (asfLength > 0 && asfLength >= 4)
{
if (smAsf[0] == PREFERRED_ASF_BYTES[0] &&
smAsf[1] == PREFERRED_ASF_BYTES[1] &&
smAsf[2] == PREFERRED_ASF_BYTES[2] &&
smAsf[3] == PREFERRED_ASF_BYTES[3])
{
result = true;
}
}

return result;
}

private static class Receiver
{
long lastPositionPlusWindow;
long timeOfLastStatusMessage;
long receiverId;
InetSocketAddress address;

Receiver(
final long lastPositionPlusWindow,
final long now,
final long receiverId,
final InetSocketAddress receiverAddress)
{
this.lastPositionPlusWindow = lastPositionPlusWindow;
this.timeOfLastStatusMessage = now;
this.receiverId = receiverId;
this.address = receiverAddress;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.agrona.DirectBuffer;
import org.agrona.IoUtil;
import org.agrona.concurrent.UnsafeBuffer;
import org.agrona.concurrent.YieldingIdleStrategy;
import org.junit.After;
import org.junit.Test;

Expand All @@ -44,7 +45,6 @@ public class FlowControlStrategiesTest
public static final String MULTICAST_URI = "aeron:udp?endpoint=224.20.30.39:54326|interface=localhost";

private static final int STREAM_ID = 1;
private static final ThreadingMode THREADING_MODE = ThreadingMode.SHARED;

private static final int TERM_BUFFER_LENGTH = 64 * 1024;
private static final int NUM_MESSAGES_PER_TERM = 64;
Expand Down Expand Up @@ -78,13 +78,15 @@ private void launch()

driverAContext.publicationTermBufferLength(TERM_BUFFER_LENGTH)
.aeronDirectoryName(baseDirA)
.threadingMode(THREADING_MODE);
.sharedIdleStrategy(new YieldingIdleStrategy())
.threadingMode(ThreadingMode.SHARED);

aeronAContext.aeronDirectoryName(driverAContext.aeronDirectoryName());

driverBContext.publicationTermBufferLength(TERM_BUFFER_LENGTH)
.aeronDirectoryName(baseDirB)
.threadingMode(THREADING_MODE);
.sharedIdleStrategy(new YieldingIdleStrategy())
.threadingMode(ThreadingMode.SHARED);

aeronBContext.aeronDirectoryName(driverBContext.aeronDirectoryName());

Expand Down Expand Up @@ -118,7 +120,7 @@ public void shouldSpinUpAndShutdown() throws Exception
subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID);

while (subscriptionA.hasNoImages() && subscriptionB.hasNoImages())
while (subscriptionA.hasNoImages() || subscriptionB.hasNoImages())
{
Thread.sleep(1);
}
Expand All @@ -145,7 +147,7 @@ public void shouldTimeoutImageWhenBehindForTooLongWithMaxMulticastFlowControlStr
subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID);

while (!publication.isConnected() && subscriptionA.hasNoImages() && subscriptionB.hasNoImages())
while (subscriptionA.hasNoImages() || subscriptionB.hasNoImages())
{
Thread.yield();
}
Expand Down Expand Up @@ -219,12 +221,12 @@ public void shouldSlowDownWhenBehindWithMinMulticastFlowControlStrategy() throws
subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID);

while (!publication.isConnected() && subscriptionA.hasNoImages() && subscriptionB.hasNoImages())
while (subscriptionA.hasNoImages() || subscriptionB.hasNoImages())
{
Thread.yield();
}

for (int i = 0; numFragmentsReadFromB < numMessagesToSend; i++)
for (long i = 0; numFragmentsReadFromB < numMessagesToSend; i++)
{
if (numMessagesLeftToSend > 0)
{
Expand All @@ -234,11 +236,13 @@ public void shouldSlowDownWhenBehindWithMinMulticastFlowControlStrategy() throws
}
}

Thread.yield();

// A keeps up
subscriptionA.poll(fragmentHandlerA, 10);

// B receives slowly
if ((i % 10) == 0)
if ((i % 2) == 0)
{
numFragmentsReadFromB += subscriptionB.poll(fragmentHandlerB, 1);
}
Expand Down Expand Up @@ -275,7 +279,7 @@ public void shouldRemoveDeadReceiverWithMinMulticastFlowControlStrategy() throws
subscriptionA = clientA.addSubscription(MULTICAST_URI, STREAM_ID);
subscriptionB = clientB.addSubscription(MULTICAST_URI, STREAM_ID);

while (!publication.isConnected() && subscriptionA.hasNoImages() && subscriptionB.hasNoImages())
while (subscriptionA.hasNoImages() || subscriptionB.hasNoImages())
{
Thread.yield();
}
Expand Down

0 comments on commit 5773d3b

Please sign in to comment.