summaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/test/java/com/yahoo/vespa/clustercontroller/core/rpc/SlimeClusterStateBundleCodecTest.java
blob: f2fe494ce61ce5b6adcb3b640389eb1295db7389 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.rpc;

import com.yahoo.vespa.clustercontroller.core.ClusterStateBundle;
import com.yahoo.vespa.clustercontroller.core.ClusterStateBundleUtil;
import com.yahoo.vespa.clustercontroller.core.StateMapping;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;

public class SlimeClusterStateBundleCodecTest {

    private static ClusterStateBundle roundtripEncode(ClusterStateBundle stateBundle) {
        SlimeClusterStateBundleCodec codec = new SlimeClusterStateBundleCodec();
        EncodedClusterStateBundle encoded = codec.encode(stateBundle);
        return codec.decode(encoded);
    }

    @Test
    public void baseline_only_bundle_can_be_round_trip_encoded() {
        ClusterStateBundle stateBundle = ClusterStateBundleUtil.makeBundle("distributor:2 storage:2");
        assertThat(roundtripEncode(stateBundle), equalTo(stateBundle));
    }

    @Test
    public void multi_space_state_bundle_can_be_round_trip_encoded() {
        ClusterStateBundle stateBundle = ClusterStateBundleUtil.makeBundle("distributor:2 storage:2",
                StateMapping.of("default", "distributor:2 storage:2 .0.s:d"),
                StateMapping.of("upsidedown", "distributor:2 .0.s:d storage:2"));
        assertThat(roundtripEncode(stateBundle), equalTo(stateBundle));
    }

    private static ClusterStateBundle makeCompressableBundle() {
        StringBuilder allDownStates = new StringBuilder(2048);
        for (int i = 0; i < 99; ++i) {
            allDownStates.append(" .").append(i).append(".s:d");
        }
        // Exact same state set string repeated twice; perfect compression fodder.
        return ClusterStateBundleUtil.makeBundle(String.format("distributor:100%s storage:100%s",
                allDownStates.toString(), allDownStates.toString()));
    }

    @Test
    public void encoded_cluster_states_can_be_compressed() {
        ClusterStateBundle stateBundle = makeCompressableBundle();

        SlimeClusterStateBundleCodec codec = new SlimeClusterStateBundleCodec();
        EncodedClusterStateBundle encoded = codec.encode(stateBundle);
        assertThat(encoded.getCompression().data().length, lessThan(stateBundle.getBaselineClusterState().toString().length()));
        ClusterStateBundle decodedBundle = codec.decode(encoded);
        assertThat(decodedBundle, equalTo(stateBundle));
    }

}