summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/fs4/PongPacket.java
blob: adb7931948c58f6343429cbcb6e33eeaa5a6f3a5 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fs4;

import java.nio.ByteBuffer;
import java.util.Optional;

/**
 * A pong packet for FS4. It maps to PCODE_MLD_MONITORRESULT
 * and PCODE_MONITORRESULTX in the C++ implementation of the protocol.
 *
 * @author  <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */

public class PongPacket extends BasicPacket {
    @SuppressWarnings("unused")
    private int lowPartitionId; // ignored (historical field)

    private int dispatchTimestamp;

    @SuppressWarnings("unused")
    private int totalNodes; // configured nodes
    private Optional<Integer> activeNodes = Optional.empty(); // number of nodes that are up
    @SuppressWarnings("unused")
    private int totalPartitions; // configured partitions
    private Optional<Integer> activePartitions = Optional.empty(); // number of partitions that are up

    private Optional<Long> activeDocs = Optional.empty(); // how many documents are searchable (sum)

    public PongPacket() {
    }

    private int code;
    protected void codeDecodedHook(int code) { this.code = code; }
    public int getCode() { return code; }

    public void decodeBody(ByteBuffer buffer) {
        int features = MRF_MLD;
        if (code == PCODE_MONITORRESULTX) {
            features = buffer.getInt();
        }
        lowPartitionId = buffer.getInt();
        dispatchTimestamp = buffer.getInt();
        if ((features & MRF_MLD) != 0) {
            totalNodes = buffer.getInt();
            activeNodes = Optional.of(buffer.getInt());
            totalPartitions = buffer.getInt();
            activePartitions = Optional.of(buffer.getInt());
        }
        if ((features & MRF_RFLAGS) != 0) {
            buffer.getInt(); // ignore rflags (historical field)
        }
        if ((features & MRF_ACTIVEDOCS) != 0) {
            activeDocs = Optional.of(Long.valueOf(buffer.getLong()));
        }
    }

    public static PongPacket create() {
        return new PongPacket();
    }

    /**
     * Return current docstamp for backend to make cache invalidation
     * possible.
     * */
    public int getDocstamp() {
        return dispatchTimestamp;
    }

    /**
     * retrieve the reported number of active (searchable) documents
     * in the monitored backend.
     **/
    public Optional<Long> getActiveDocuments() {
        return activeDocs;
    }

    public Optional<Integer> getActiveNodes() {
        return activeNodes;
    }

    public Optional<Integer> getActivePartitions() {
        return activePartitions;
    }

    /** feature bits, taken from searchlib/common/transport.h */
    static final int MRF_MLD        = 0x00000001;
    static final int MRF_RFLAGS     = 0x00000008;
    static final int MRF_ACTIVEDOCS = 0x00000010;

    /** packet codes, taken from searchlib/common/transport.h */
    static final int PCODE_MLD_MONITORRESULT = 210;
    static final int PCODE_MONITORRESULTX    = 221;

}