aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/persistence/ZoneRoutingPolicySerializer.java
blob: d6342bc355f0057d1dad8dc2ca74771324907f9f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.persistence;

import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.slime.Slime;
import com.yahoo.vespa.hosted.controller.routing.ZoneRoutingPolicy;

import java.util.Objects;

/**
 * Serializer for {@link ZoneRoutingPolicy}.
 *
 * @author mpolden
 */
public class ZoneRoutingPolicySerializer {

    // WARNING: Since there are multiple servers in a ZooKeeper cluster and they upgrade one by one
    //          (and rewrite all nodes on startup), changes to the serialized format must be made
    //          such that what is serialized on version N+1 can be read by version N:
    //          - ADDING FIELDS: Always ok
    //          - REMOVING FIELDS: Stop reading the field first. Stop writing it on a later version.
    //          - CHANGING THE FORMAT OF A FIELD: Don't do it bro.

    private static final String GLOBAL_ROUTING_FIELD = "globalRouting";

    private final RoutingPolicySerializer routingPolicySerializer;

    public ZoneRoutingPolicySerializer(RoutingPolicySerializer routingPolicySerializer) {
        this.routingPolicySerializer = Objects.requireNonNull(routingPolicySerializer, "routingPolicySerializer must be non-null");
    }

    public ZoneRoutingPolicy fromSlime(ZoneId zone, Slime slime) {
        var root = slime.get();
        return new ZoneRoutingPolicy(zone, routingPolicySerializer.routingStatusFromSlime(root.field(GLOBAL_ROUTING_FIELD)));
    }

    public Slime toSlime(ZoneRoutingPolicy policy) {
        var slime = new Slime();
        var root = slime.setObject();
        routingPolicySerializer.globalRoutingToSlime(policy.routingStatus(), root.setObject(GLOBAL_ROUTING_FIELD));
        return slime;
    }

}