summaryrefslogtreecommitdiff
path: root/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerHandlerMetricsTest.java
blob: f3ebb69366a4e47018e2287db4a97be1125c0f3a (plain)
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
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.zookeeper.server.quorum;

import org.apache.jute.BinaryOutputArchive;
import org.apache.zookeeper.metrics.MetricsUtils;
import org.apache.zookeeper.server.ServerMetrics;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.number.OrderingComparison.greaterThan;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class LearnerHandlerMetricsTest {
    private MockLearnerHandler learnerHandler;
    private long sid = 5;
    private volatile CountDownLatch allSentLatch = null;

    class MockLearnerHandler extends LearnerHandler {
        MockLearnerHandler(Socket socket, Leader leader) throws IOException {
            super(socket, null, leader);
        }
    }

    @Before
    public void setup() throws IOException {
        Leader leader = mock(Leader.class);
        when(leader.getQuorumAuthServer()).thenReturn(null);

        Socket socket = mock(Socket.class);
        when(socket.getRemoteSocketAddress()).thenReturn(new InetSocketAddress(32));

        //adding 5ms artificial delay when sending each packet
        BinaryOutputArchive oa = mock(BinaryOutputArchive.class);
        doAnswer(invocationOnMock -> {Thread.sleep(5); return null;})
                .when(oa).writeRecord(any(QuorumPacket.class), anyString());


        BufferedOutputStream bos = mock(BufferedOutputStream.class);
        // flush is called when all packets are sent and the queue is empty
        doAnswer(invocationOnMock -> {
            if (allSentLatch != null) {
                allSentLatch.countDown();
            }
            return null;
        }).when(bos).flush();

        learnerHandler = new MockLearnerHandler(socket, leader);
        learnerHandler.setOutputArchive(oa);
        learnerHandler.setBufferedOutput(bos);
        learnerHandler.sid = sid;
    }

    @Test
    public void testMetrics() throws InterruptedException {
        ServerMetrics.getMetrics().resetAll();

        //adding 1001 packets in the queue, two marker packets will be added since the interval is every 1000 packets
        for (int i=0; i<1001; i++) {
            learnerHandler.queuePacket(new QuorumPacket());
        }

        allSentLatch = new CountDownLatch(1);

        learnerHandler.startSendingPackets();

        allSentLatch.await(8, TimeUnit.SECONDS);

        Map<String, Object> values = MetricsUtils.currentServerMetrics();
        String sidStr = Long.toString(sid);

        //we record time for each marker packet and we have two marker packets
        Assert.assertEquals(2L,  values.get("cnt_" + sidStr + "_learner_handler_qp_time_ms"));

        //the second marker has 1000 packets in front of it and each takes 5 ms to send so the time in queue should be
        //longer than 5*1000
        Assert.assertThat((long)values.get("max_" + sidStr + "_learner_handler_qp_time_ms"), greaterThan(5000L));

        //we send 1001 packets + 2 marker packets so the queue size is recorded 1003 times
        Assert.assertEquals(1003L, values.get("cnt_" + sidStr + "_learner_handler_qp_size"));

        //the longest queue size is recorded when we are sending the first packet
        Assert.assertEquals(1002L, values.get("max_" + sidStr + "_learner_handler_qp_size"));

        //this is when the queue is emptied
        Assert.assertEquals(0L, values.get("min_" + sidStr + "_learner_handler_qp_size"));

    }
}