Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / eos-dom-akka / src / test / java / org / opendaylight / controller / eos / akka / EntityRpcHandlerTest.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.eos.akka;
9
10 import static org.awaitility.Awaitility.await;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import akka.actor.ActorSystem;
15 import akka.actor.testkit.typed.javadsl.ActorTestKit;
16 import akka.actor.typed.javadsl.Adapter;
17 import akka.cluster.Member;
18 import akka.cluster.MemberStatus;
19 import akka.cluster.typed.Cluster;
20 import java.time.Duration;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.ExecutionException;
24 import org.awaitility.Awaitility;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.EntityName;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.EntityType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.GetEntitiesInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.GetEntityInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.GetEntityOwnerInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.NodeName;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.entity.owners.norev.get.entities.output.EntitiesKey;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yangtools.concepts.Registration;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
44
45 public class EntityRpcHandlerTest extends AbstractNativeEosTest {
46     static final String ENTITY_TYPE = "test";
47
48     private ActorSystem system1;
49     private ActorSystem system2;
50
51     private AkkaEntityOwnershipService service1;
52     private AkkaEntityOwnershipService service2;
53
54     @Before
55     public void setUp() throws Exception {
56         system1 = startupActorSystem(2550, List.of("member-1"), TWO_NODE_SEED_NODES);
57         system2 = startupActorSystem(2551, List.of("member-2"), TWO_NODE_SEED_NODES, "dc-backup");
58
59         service1 = new AkkaEntityOwnershipService(system1, CODEC_CONTEXT);
60         service2 = new AkkaEntityOwnershipService(system2, CODEC_CONTEXT);
61
62         // need to wait until all nodes are ready
63         final var cluster = Cluster.get(Adapter.toTyped(system2));
64         Awaitility.await().atMost(Duration.ofSeconds(20)).until(() -> {
65             final List<Member> members = new ArrayList<>();
66             cluster.state().getMembers().forEach(members::add);
67             if (members.size() != 2) {
68                 return false;
69             }
70
71             for (final Member member : members) {
72                 if (!member.status().equals(MemberStatus.up())) {
73                     return false;
74                 }
75             }
76
77             return true;
78         });
79     }
80
81     @After
82     public void tearDown() throws InterruptedException, ExecutionException {
83         service1.close();
84         service2.close();
85         ActorTestKit.shutdown(Adapter.toTyped(system1), Duration.ofSeconds(20));
86         ActorTestKit.shutdown(Adapter.toTyped(system2), Duration.ofSeconds(20));
87     }
88
89     /*
90      * Tests entity rpcs handled both by the owner supervisor(service1) and with an idle supervisor(falling
91      * back to distributed-data in an inactive datacenter). This covers both the available cases, datacenters and case
92      * in which the node with active akka-singleton is shutdown and another one takes over.
93      */
94     @Test
95     public void testEntityRetrievalWithUnavailableSupervisor() throws Exception {
96         final YangInstanceIdentifier entityId = YangInstanceIdentifier.of(new NodeIdentifier(NetworkTopology.QNAME),
97                 new NodeIdentifier(Topology.QNAME),
98                 NodeIdentifierWithPredicates.of(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), "test"),
99                 new NodeIdentifier(Node.QNAME),
100                 NodeIdentifierWithPredicates.of(Node.QNAME, QName.create(Node.QNAME, "node-id"), "test://test-node"));
101
102         final DOMEntity entity = new DOMEntity(ENTITY_TYPE, entityId);
103
104         final Registration reg = service1.registerCandidate(entity);
105
106         await().untilAsserted(() -> {
107             final var getEntityResult = service1.getEntity(new GetEntityInputBuilder()
108                 .setName(new EntityName(CODEC_CONTEXT.fromYangInstanceIdentifier(entityId)))
109                 .setType(new EntityType(ENTITY_TYPE))
110                 .build()).get();
111
112             assertEquals(getEntityResult.getResult().getOwnerNode().getValue(), "member-1");
113             assertEquals(getEntityResult.getResult().getCandidateNodes().get(0).getValue(), "member-1");
114         });
115
116         // keep this under ask timeout to make sure the singleton actor in the inactive datacenter responds with failure
117         // immediately, so that the rpc actor retries with distributed-data asap
118         await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> {
119             final var getEntitiesResult = service2.getEntities(new GetEntitiesInputBuilder().build()).get().getResult();
120             final var entities = getEntitiesResult.nonnullEntities();
121             assertEquals(1, entities.size());
122             assertTrue(entities.get(new EntitiesKey(
123                 new EntityName(CODEC_CONTEXT.fromYangInstanceIdentifier(entityId)),
124                 new EntityType(ENTITY_TYPE)))
125                 .getCandidateNodes().contains(new NodeName("member-1")));
126             assertTrue(entities.get(new EntitiesKey(
127                 new EntityName(CODEC_CONTEXT.fromYangInstanceIdentifier(entityId)),
128                 new EntityType(ENTITY_TYPE)))
129                 .getOwnerNode().getValue().equals("member-1"));
130         });
131
132         await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> {
133             final var getEntityResult = service2.getEntity(new GetEntityInputBuilder()
134                 .setName(new EntityName(CODEC_CONTEXT.fromYangInstanceIdentifier(entityId)))
135                 .setType(new EntityType(ENTITY_TYPE))
136                 .build()).get().getResult();
137
138             assertEquals(getEntityResult.getOwnerNode().getValue(), "member-1");
139             assertEquals(getEntityResult.getCandidateNodes().get(0).getValue(), "member-1");
140         });
141
142         await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> {
143             final var getOwnerResult = service2.getEntityOwner(new GetEntityOwnerInputBuilder()
144                 .setName(new EntityName(CODEC_CONTEXT.fromYangInstanceIdentifier(entityId)))
145                 .setType(new EntityType(ENTITY_TYPE))
146                 .build()).get().getResult();
147
148             assertEquals(getOwnerResult.getOwnerNode().getValue(), "member-1");
149         });
150
151     }
152 }