aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/test/java/com/yahoo/messagebus/ThrottlerTestCase.java
blob: 7e01a0f98ec5b6616aee1f06c7c465f9e0e43396 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;

import com.yahoo.concurrent.ManualTimer;
import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.slobrok.server.Slobrok;
import com.yahoo.messagebus.network.rpc.test.TestServer;
import com.yahoo.messagebus.routing.RoutingTableSpec;
import com.yahoo.messagebus.test.QueueAdapter;
import com.yahoo.messagebus.test.Receptor;
import com.yahoo.messagebus.test.SimpleMessage;
import com.yahoo.messagebus.test.SimpleProtocol;
import com.yahoo.messagebus.test.SimpleReply;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Simon Thoresen Hult
 */
public class ThrottlerTestCase {

    Slobrok slobrok;
    TestServer src, dst;

    @BeforeEach
    public void setUp() throws ListenFailedException {
        RoutingTableSpec table = new RoutingTableSpec(SimpleProtocol.NAME);
        table.addHop("dst", "test/dst/session", List.of("test/dst/session"));
        table.addRoute("test", List.of("dst"));
        slobrok = new Slobrok();
        src = new TestServer("test/src", table, slobrok, null);
        dst = new TestServer("test/dst", table, slobrok, null);
    }

    @AfterEach
    public void tearDown() {
        dst.destroy();
        src.destroy();
        slobrok.stop();
    }

    @Test
    void testMaxCount() {
        // Prepare a source session with throttle enabled.
        SourceSessionParams params = new SourceSessionParams().setTimeout(600.0);
        StaticThrottlePolicy policy = new StaticThrottlePolicy();
        policy.setMaxPendingCount(10);
        params.setThrottlePolicy(policy);

        Receptor src_rr = new Receptor();
        SourceSession src_s = src.mb.createSourceSession(src_rr, params);

        // Prepare a destination session to acknowledge messages.
        QueueAdapter dst_q = new QueueAdapter();
        DestinationSession dst_s = dst.mb.createDestinationSession("session", true, dst_q);
        src.waitSlobrok("test/dst/session", 1);

        // Send until throttler rejects a message.
        for (int i = 0; i < policy.getMaxPendingCount(); i++) {
            assertTrue(src_s.send(new SimpleMessage("msg"), "test").isAccepted());
        }
        assertFalse(src_s.send(new SimpleMessage("msg"), "test").isAccepted());

        // Acknowledge one message at a time, then attempt to send two more.
        for (int i = 0; i < 10; i++) {
            assertTrue(dst_q.waitSize(policy.getMaxPendingCount(), 60));
            dst_s.acknowledge((Message) dst_q.dequeue());

            assertNotNull(src_rr.getReply(60));
            assertTrue(src_s.send(new SimpleMessage("msg"), "test").isAccepted());
            assertFalse(src_s.send(new SimpleMessage("msg"), "test").isAccepted());
        }

        assertTrue(dst_q.waitSize(policy.getMaxPendingCount(), 60));
        while (!dst_q.isEmpty()) {
            dst_s.acknowledge((Message) dst_q.dequeue());
        }

        src_s.close();
        dst_s.destroy();
    }

    @Test
    void testMaxSize() {
        // Prepare a source session with throttle enabled.
        SourceSessionParams params = new SourceSessionParams().setTimeout(600.0);
        StaticThrottlePolicy policy = new StaticThrottlePolicy();
        policy.setMaxPendingCount(1000);
        policy.setMaxPendingSize(2);
        params.setThrottlePolicy(policy);

        Receptor src_rr = new Receptor();
        SourceSession src_s = src.mb.createSourceSession(src_rr, params);

        // Prepare a destination session to acknowledge messages.
        QueueAdapter dst_q = new QueueAdapter();
        DestinationSession dst_s = dst.mb.createDestinationSession("session", true, dst_q);
        src.waitSlobrok("test/dst/session", 1);

        assertTrue(src_s.send(new SimpleMessage("1"), "test").isAccepted());
        assertTrue(dst_q.waitSize(1, 60));
        assertTrue(src_s.send(new SimpleMessage("12"), "test").isAccepted());
        assertTrue(dst_q.waitSize(2, 60));

        assertFalse(src_s.send(new SimpleMessage("1"), "test").isAccepted());
        dst_s.acknowledge((Message) dst_q.dequeue());
        assertNotNull(src_rr.getReply(60));

        assertFalse(src_s.send(new SimpleMessage("1"), "test").isAccepted());
        dst_s.acknowledge((Message) dst_q.dequeue());
        assertNotNull(src_rr.getReply(60));

        assertTrue(src_s.send(new SimpleMessage("12"), "test").isAccepted());
        assertTrue(dst_q.waitSize(1, 60));
        assertFalse(src_s.send(new SimpleMessage("1"), "test").isAccepted());
        dst_s.acknowledge((Message) dst_q.dequeue());
        assertNotNull(src_rr.getReply(60));

        // Close sessions.
        src_s.close();
        dst_s.destroy();
    }

    @Test
    void testDynamicWindowSize() {
        ManualTimer timer = new ManualTimer();
        DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);

        policy.setWindowSizeIncrement(5)
                .setResizeRate(1);

        double windowSize = getWindowSize(policy, timer, 100);
        assertTrue(windowSize >= 90 && windowSize <= 105);

        windowSize = getWindowSize(policy, timer, 200);
        assertTrue(windowSize >= 180 && windowSize <= 205);

        windowSize = getWindowSize(policy, timer, 50);
        assertTrue(windowSize >= 45 && windowSize <= 55);

        windowSize = getWindowSize(policy, timer, 500);
        assertTrue(windowSize >= 450 && windowSize <= 505);

        windowSize = getWindowSize(policy, timer, 100);
        assertTrue(windowSize >= 90 && windowSize <= 115);
    }

    @Test
    void testIdleTimePeriod() {
        ManualTimer timer = new ManualTimer();
        DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);

        policy.setWindowSizeIncrement(5)
                .setMinWindowSize(1)
                .setResizeRate(1);

        double windowSize = getWindowSize(policy, timer, 100);
        assertTrue(windowSize >= 90 && windowSize <= 110);

        Message msg = new SimpleMessage("foo");
        timer.advance(30 * 1000);
        assertTrue(policy.canSend(msg, 0));
        assertTrue(windowSize >= 90 && windowSize <= 110);

        timer.advance(60 * 1000 + 1);
        assertTrue(policy.canSend(msg, 50));
        assertEquals(55, policy.getMaxPendingCount());

        timer.advance(60 * 1000 + 1);
        assertTrue(policy.canSend(msg, 0));
        assertEquals(5, policy.getMaxPendingCount());

    }

    @Test
    void testMinWindowSize() {
        ManualTimer timer = new ManualTimer();
        DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);

        policy.setWindowSizeIncrement(5)
                .setResizeRate(1)
                .setMinWindowSize(150);

        double windowSize = getWindowSize(policy, timer, 200);
        assertTrue(windowSize >= 150 && windowSize <= 210);
    }

    @Test
    void testMaxWindowSize() {
        ManualTimer timer = new ManualTimer();
        DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);

        policy.setWindowSizeIncrement(5);
        policy.setResizeRate(1);
        policy.setMaxWindowSize(50);

        double windowSize = getWindowSize(policy, timer, 100);
        assertTrue(windowSize >= 40 && windowSize <= 50);
    }

    private int getWindowSize(DynamicThrottlePolicy policy, ManualTimer timer, int maxPending) {
        Message msg = new SimpleMessage("foo");
        Reply reply = new SimpleReply("bar");
        reply.setContext(1);
        for (int i = 0; i < 999; ++i) {
            int numPending = 0;
            while (policy.canSend(msg, numPending)) {
                policy.processMessage(msg);
                ++numPending;
            }

            long tripTime = (numPending < maxPending) ? 1000L : 1000 + (numPending - maxPending) * 1000L;
            timer.advance(tripTime);

            while (--numPending >= 0) {
                policy.processReply(reply);
            }
        }
        int ret = policy.getMaxPendingCount();
        System.out.println("getWindowSize() = " + ret);
        return ret;
    }

}