Search in sources :

Example 26 with Tree

use of io.datatree.Tree in project moleculer-java by moleculer-java.

the class TcpTransporterTest method createOnlineDescriptorWithoutInfo.

protected NodeDescriptor createOnlineDescriptorWithoutInfo(boolean local, String nodeID) {
    Tree info = new Tree();
    info.put("seq", 1);
    info.put("port", 1);
    info.put("hostname", nodeID);
    return new NodeDescriptor(nodeID, true, local, info);
}
Also used : NodeDescriptor(services.moleculer.transporter.tcp.NodeDescriptor) Tree(io.datatree.Tree)

Example 27 with Tree

use of io.datatree.Tree in project moleculer-java by moleculer-java.

the class TcpTransporterTest method testProcessGossipResponse.

// --- GOSSIP RESPONSE PROCESSING ---
@Test
public void testProcessGossipResponse() throws Exception {
    // Target is offline
    Tree rsp = createGossipOfflineMessage("node1", 1);
    tr.processGossipResponse(rsp);
    assertEquals(0, tr.getDescriptor().offlineSince);
    assertEquals(0, tr.nodes.size());
    assertEquals(2, tr.getDescriptor().seq);
    // Unknown node is offline
    rsp = createGossipOfflineMessage("node2", 1);
    tr.processGossipResponse(rsp);
    assertEquals(0, tr.nodes.size());
    // Add new info block to "node4"
    tr.nodes.put("node4", createOnlineDescriptorWithInfo(false, "node4"));
    Tree info = new Tree().put("x", "y");
    info.put("seq", 2);
    info.put("hostname", "aaa");
    info.put("port", 11);
    rsp = createGossipOnlineResponse("node4", info, 1, 1);
    tr.processGossipResponse(rsp);
    assertEquals(1, rsp.get("online").size());
    assertEquals("y", tr.nodes.get("node4").info.get("x", "?"));
    // Seq not changed
    info = info.clone().put("x", "z");
    rsp = createGossipOnlineResponse("node4", info, 2, 3);
    tr.processGossipResponse(rsp);
    assertEquals(1, rsp.get("online").size());
    assertEquals("y", tr.nodes.get("node4").info.get("x", "?"));
    assertEquals(3, tr.getCpuUsage("node4"));
    // Seq changed
    info.put("seq", "4");
    rsp = createGossipOnlineResponse("node4", info, 1, 1);
    tr.processGossipResponse(rsp);
    assertEquals("z", tr.nodes.get("node4").info.get("x", "?"));
    assertEquals(3, tr.getCpuUsage("node4"));
    // Target is offline
    rsp = createGossipOfflineMessage("node1", 1);
    tr.processGossipResponse(rsp);
    assertEquals(2, tr.getDescriptor().seq);
    rsp = createGossipOfflineMessage("node1", 2);
    tr.processGossipResponse(rsp);
    assertEquals(3, tr.getDescriptor().seq);
}
Also used : Tree(io.datatree.Tree) Test(org.junit.Test)

Example 28 with Tree

use of io.datatree.Tree in project moleculer-java by moleculer-java.

the class TcpTransporterTest method testProcessGossipRequest.

// --- GOSSIP REQUEST PROCESSING ---
@Test
public void testProcessGossipRequest() throws Exception {
    // Add "node2" (hidden)
    tr.nodes.put("node2", createOfflineDescriptor(false, "node2"));
    // Simple request
    Tree req = createGossipRequest("node3", 1, 2, 3);
    Tree rsp = tr.processGossipRequest(req);
    assertEquals(1, rsp.get("online").size());
    assertNull(rsp.get("offline"));
    assertEquals(1, rsp.get("online.node1").size());
    assertNotNull(rsp.get("online.node1[0].hostname"));
    assertTrue(rsp.get("online.node1[0].port", 0) > 0);
    assertTrue(rsp.get("online.node1[0].seq", 0) > 0);
    assertTrue(rsp.get("online.node1[0].services").size() > 0);
    // Update local CPU
    tr.getDescriptor().updateCpu(4);
    rsp = tr.processGossipRequest(req);
    assertEquals(3, rsp.get("online.node1").size());
    assertEquals(1, rsp.get("online.node1[1]", 0));
    assertEquals(4, rsp.get("online.node1[2]", 0));
    tr.getDescriptor().updateCpu(5);
    rsp = tr.processGossipRequest(req);
    assertEquals(2, rsp.get("online.node1[1]", 0));
    assertEquals(5, rsp.get("online.node1[2]", 0));
    tr.getDescriptor().updateCpu(0);
    rsp = tr.processGossipRequest(req);
    assertEquals(3, rsp.get("online.node1[1]", 0));
    assertEquals(0, rsp.get("online.node1[2]", 0));
    // Add offline node
    // Node4 -> offline & hidden
    tr.registerAsNewNode("node3", "host3", 1003);
    rsp = tr.processGossipRequest(req);
    assertEquals(1, rsp.get("online").size());
    assertNull(rsp.get("offline"));
    assertEquals(3, rsp.get("online.node1").size());
    // Node5 -> online
    NodeDescriptor node4 = createOnlineDescriptorWithInfo(false, "node4");
    node4.info.put("seq", "3");
    node4.seq = 3;
    tr.nodes.put("node4", node4);
    rsp = tr.processGossipRequest(req);
    assertEquals(2, rsp.get("online").size());
    assertNull(rsp.get("offline"));
    assertEquals(3, rsp.get("online.node1").size());
    assertEquals(1, rsp.get("online.node4").size());
    assertEquals(1, rsp.get("online.node4[0].port", 0));
    assertEquals(3, rsp.get("online.node4[0].seq", 0));
    assertEquals("node4", rsp.get("online.node4[0].hostname", "?"));
    // Add "node4" to request (low seq)
    req.get("online").putList("node4").add(1).add(1).add(0);
    rsp = tr.processGossipRequest(req);
    assertEquals(2, rsp.get("online").size());
    assertNull(rsp.get("offline"));
    assertEquals(3, rsp.get("online.node1").size());
    assertEquals(3, rsp.get("online.node1[1]", 0));
    assertEquals(0, rsp.get("online.node1[2]", 0));
    // Add "node4" to request (correct seq)
    req.get("online").get("node4").clear().add(3).add(1).add(0);
    rsp = tr.processGossipRequest(req);
    assertEquals(1, rsp.get("online").size());
    assertEquals(3, rsp.get("online.node1").size());
    // Add "node4" to request (high seq)
    req.get("online").get("node4").clear().add(4).add(1).add(0);
    rsp = tr.processGossipRequest(req);
    assertEquals(1, rsp.get("online").size());
    assertNull(rsp.get("offline"));
    assertEquals(3, rsp.get("online.node1").size());
    // Add "node4" to request (higher CPU seq)
    req.get("online").get("node4").clear().add(3).add(2).add(1);
    rsp = tr.processGossipRequest(req);
    assertEquals(1, rsp.get("online").size());
    assertEquals(1, tr.getCpuUsage("node4"));
    // Add "node4" to request (lower CPU seq)
    req.get("online").get("node4").clear().add(3).add(1).add(2);
    rsp = tr.processGossipRequest(req);
    assertEquals(2, rsp.get("online").size());
    assertNull(rsp.get("offline"));
    assertEquals(2, rsp.get("online.node4").size());
    assertEquals(2, rsp.get("online.node4[0]", 0));
    assertEquals(1, rsp.get("online.node4[1]", 0));
    assertEquals(1, tr.getCpuUsage("node4"));
    // Add "node4" to request (same CPU seq)
    req.get("online").get("node4").clear().add(3).add(2).add(2);
    rsp = tr.processGossipRequest(req);
    assertEquals(1, rsp.get("online").size());
    assertNull(rsp.get("offline"));
    assertNull(rsp.get("online.node4"));
    assertEquals(1, tr.getCpuUsage("node4"));
    // Add "node4" to request (higher CPU seq)
    req.get("online").get("node4").clear().add(3).add(3).add(5);
    rsp = tr.processGossipRequest(req);
    assertEquals(1, rsp.get("online").size());
    assertNull(rsp.get("offline"));
    assertNull(rsp.get("online.node4"));
    assertEquals(5, tr.getCpuUsage("node4"));
    // Add "node4" to request (lower CPU seq)
    req.get("online").get("node4").clear().add(3).add(1).add(2);
    rsp = tr.processGossipRequest(req);
    assertEquals(2, rsp.get("online").size());
    assertNull(rsp.get("offline"));
    assertEquals(2, rsp.get("online.node4").size());
    assertEquals(3, rsp.get("online.node4[0]", 0));
    assertEquals(5, rsp.get("online.node4[1]", 0));
    assertEquals(5, tr.getCpuUsage("node4"));
    assertEquals(1, tr.getDescriptor().seq);
}
Also used : NodeDescriptor(services.moleculer.transporter.tcp.NodeDescriptor) Tree(io.datatree.Tree) Test(org.junit.Test)

Example 29 with Tree

use of io.datatree.Tree in project moleculer-java by moleculer-java.

the class TcpTransporterTest method testSendGossipRequest.

// --- GOSSIP REQUEST ---
@Test
public void testSendGossipRequest() throws Exception {
    // Add "node2"
    tr.nodes.put("node2", createOnlineDescriptorWithoutInfo(false, "node2"));
    Tree req = tr.sendGossipRequest();
    assertEquals(req.get("ver", "?"), ServiceBroker.PROTOCOL_VERSION);
    assertEquals(req.get("sender", "?"), "node1");
    assertEquals(req.get("online.node1[0]", -1), 1);
    assertEquals(req.get("online.node1[1]", -1), 0);
    assertEquals(req.get("online.node1[2]", -1), 0);
    assertEquals(2, req.get("online").size());
    assertEquals(req.get("online.node2[0]", -1), 1);
    assertEquals(req.get("online.node2[1]", -1), 0);
    assertEquals(req.get("online.node2[2]", -1), 0);
    // Add "node3"
    tr.nodes.put("node3", createOnlineDescriptorWithoutInfo(false, "node3"));
    req = tr.sendGossipRequest();
    assertEquals(3, req.get("online").size());
    assertEquals(req.get("online.node3[0]", -1), 1);
    assertEquals(req.get("online.node3[1]", -1), 0);
    assertEquals(req.get("online.node3[2]", -1), 0);
    // Node3 -> offline
    tr.nodes.get("node3").markAsOffline();
    req = tr.sendGossipRequest();
    assertEquals(2, req.get("online").size());
    assertNull(req.get("online.node3"));
    assertEquals(2, req.get("offline.node3", -1));
    assertEquals(1, req.get("offline").size());
    // Node4 -> offline & hidden
    tr.registerAsNewNode("node4", "host4", 1004);
    req = tr.sendGossipRequest();
    assertNull(req.get("online.node4"));
    assertNull(req.get("offline.node4"));
    assertEquals(2, req.get("online").size());
    assertEquals(1, req.get("offline").size());
    // CPU -> 2
    tr.nodes.get("node2").updateCpu(2);
    req = tr.sendGossipRequest();
    assertEquals(req.get("online.node2[1]", -1), 1);
    assertEquals(req.get("online.node2[2]", -1), 2);
    // CPU -> seq + 4
    tr.nodes.get("node2").updateCpu(5, 4);
    req = tr.sendGossipRequest();
    assertEquals(req.get("online.node2[1]", -1), 5);
    assertEquals(req.get("online.node2[2]", -1), 4);
    // CPU -> wrong seq + 4
    tr.nodes.get("node2").updateCpu(4, 3);
    req = tr.sendGossipRequest();
    assertEquals(req.get("online.node2[1]", -1), 5);
    assertEquals(req.get("online.node2[2]", -1), 4);
    // Wrong values
    assertException(() -> {
        tr.nodes.get("node2").updateCpu(-1);
    });
    assertException(() -> {
        tr.nodes.get("node2").updateCpu(101);
    });
    assertException(() -> {
        tr.nodes.get("node2").updateCpu(0, 50);
    });
    assertException(() -> {
        tr.nodes.get("node2").updateCpu(10, 200);
    });
    assertException(() -> {
        tr.nodes.get("node2").updateCpu(0, 0);
    });
    assertException(() -> {
        tr.registerAsNewNode(null, "node6", 1006);
    });
    assertException(() -> {
        tr.registerAsNewNode("node6", null, 1006);
    });
    assertException(() -> {
        tr.registerAsNewNode("node6", "", 1006);
    });
    assertException(() -> {
        tr.registerAsNewNode("", "node6", 1006);
    });
    assertException(() -> {
        tr.registerAsNewNode("node6", "node6", 0);
    });
    assertException(() -> {
        tr.udpPacketReceived("node7", "", 1007);
    });
    assertException(() -> {
        tr.udpPacketReceived(null, "node7", 1007);
    });
    assertException(() -> {
        tr.udpPacketReceived("node7", "node7", 0);
    });
    assertException(() -> {
        tr.nodes.get("node2").markAsOnline(null);
    });
    assertException(() -> {
        tr.nodes.get("node2").markAsOnline(new Tree());
    });
}
Also used : Tree(io.datatree.Tree) Test(org.junit.Test)

Example 30 with Tree

use of io.datatree.Tree in project moleculer-java by moleculer-java.

the class TcpTransporterTest method createGossipOfflineMessage.

// --- UTILITIES -- ---
protected Tree createGossipOfflineMessage(String nodeID, int seq) {
    Tree rsp = new Tree();
    rsp.put("sender", nodeID);
    rsp.put("ver", ServiceBroker.PROTOCOL_VERSION);
    Tree offline = rsp.putList("offline");
    offline.put(nodeID, seq);
    return rsp;
}
Also used : Tree(io.datatree.Tree)

Aggregations

Tree (io.datatree.Tree)60 FastBuildTree (services.moleculer.util.FastBuildTree)26 Test (org.junit.Test)12 NodeDescriptor (services.moleculer.transporter.tcp.NodeDescriptor)12 CheckedTree (services.moleculer.util.CheckedTree)9 TimeoutException (java.util.concurrent.TimeoutException)6 Promise (services.moleculer.Promise)6 CommonUtils.readTree (services.moleculer.util.CommonUtils.readTree)6 RemoteException (java.rmi.RemoteException)4 LinkedHashMap (java.util.LinkedHashMap)4 CallOptions (services.moleculer.context.CallOptions)4 Context (services.moleculer.context.Context)4 Annotation (java.lang.annotation.Annotation)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 NoSuchElementException (java.util.NoSuchElementException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ServiceBrokerConfig (services.moleculer.config.ServiceBrokerConfig)3 Action (services.moleculer.service.Action)3