summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/fs4/PongPacket.java
blob: 5a09c4d3d3dc2b2653b3d731ee64c8eb62c6abf5 (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
// Copyright 2017 Yahoo Holdings. 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_MONITORRESULTX
 * in the C++ implementation of the protocol.
 *
 * @author Steinar Knutsen
 */
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> activeDocuments = Optional.empty(); // how many documents are searchable (sum)

    public PongPacket() {
    }

    /** For testing */
    public PongPacket(long activeDocuments) {
        this.activeDocuments = Optional.of(activeDocuments);      
    }

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

    public void decodeBody(ByteBuffer buffer) {
        int 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) {
            activeDocuments = 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 activeDocuments;
    }

    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_MONITORRESULTX    = 221;

}