Search in sources :

Example 6 with Instance

use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.

the class InstanceControllerTest method batchDeleteMetadata.

@Test
public void batchDeleteMetadata() throws Exception {
    Instance instance = new Instance("1.1.1.1", 8080, TEST_CLUSTER_NAME);
    instance.setServiceName(TEST_SERVICE_NAME);
    Map<String, String> metadata = new HashMap<>();
    metadata.put("key1", "value1");
    instance.setMetadata(metadata);
    Instance instance2 = new Instance("2.2.2.2", 8080, TEST_CLUSTER_NAME);
    instance2.setServiceName(TEST_SERVICE_NAME);
    List<Instance> instanceList = new LinkedList<>();
    instanceList.add(instance);
    instanceList.add(instance2);
    when(serviceManager.batchOperate(ArgumentMatchers.anyString(), ArgumentMatchers.any(InstanceOperationInfo.class), ArgumentMatchers.any(Function.class))).thenReturn(instanceList);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance/metadata/batch").param("namespace", "public").param("serviceName", TEST_SERVICE_NAME).param("instances", "[{\"ip\":\"1.1.1.1\",\"port\": \"8080\",\"ephemeral\":\"true\",\"clusterName\":\"test-cluster\"}," + "{\"ip\":\"2.2.2.2\",\"port\":\"8080\",\"ephemeral\":\"true\",\"clusterName\":\"test-cluster\"}]").param("metadata", "{\"age\":\"20\",\"name\":\"horizon\"}");
    String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
    JsonNode result = JacksonUtils.toObj(actualValue);
    JsonNode updated = result.get("updated");
    Assert.assertEquals(updated.size(), 2);
    Assert.assertTrue(updated.get(0).asText().contains("1.1.1.1"));
    Assert.assertTrue(updated.get(0).asText().contains("8080"));
    Assert.assertTrue(updated.get(0).asText().contains(TEST_CLUSTER_NAME));
    Assert.assertTrue(updated.get(0).asText().contains("ephemeral"));
    Assert.assertTrue(updated.get(1).asText().contains("2.2.2.2"));
    Assert.assertTrue(updated.get(1).asText().contains("8080"));
    Assert.assertTrue(updated.get(1).asText().contains(TEST_CLUSTER_NAME));
    Assert.assertTrue(updated.get(1).asText().contains("ephemeral"));
}
Also used : Function(java.util.function.Function) Instance(com.alibaba.nacos.naming.core.Instance) HashMap(java.util.HashMap) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) JsonNode(com.fasterxml.jackson.databind.JsonNode) LinkedList(java.util.LinkedList) Test(org.junit.Test) BaseTest(com.alibaba.nacos.naming.BaseTest)

Example 7 with Instance

use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.

the class ClientBeatCheckTaskTest method testHeartBeatNotTimeout.

@Test
public void testHeartBeatNotTimeout() {
    Instance instance = new Instance();
    instance.setLastBeat(System.currentTimeMillis());
    instance.setMarked(false);
    instance.setHealthy(true);
    Map<String, String> metadata = new HashMap<>();
    metadata.put(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, "1000000000");
    instance.setMetadata(metadata);
    Mockito.doReturn("test").when(serviceSpy).getName();
    Mockito.doReturn(true).when(distroMapperSpy).responsible(Mockito.anyString());
    clientBeatCheckTask.run();
    Assert.assertTrue(instance.isHealthy());
}
Also used : Instance(com.alibaba.nacos.naming.core.Instance) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 8 with Instance

use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.

the class ClientBeatCheckTaskTest method testIpDeleteTimeOut.

@Test
public void testIpDeleteTimeOut() {
    Instance instance = new Instance();
    instance.setLastBeat(System.currentTimeMillis());
    instance.setMarked(true);
    instance.setHealthy(true);
    Map<String, String> metadata = new HashMap<>();
    metadata.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT, "10");
    instance.setMetadata(metadata);
    Mockito.doReturn(true).when(distroMapperSpy).responsible(null);
    clientBeatCheckTask.run();
}
Also used : Instance(com.alibaba.nacos.naming.core.Instance) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 9 with Instance

use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.

the class JacksonSerializerTest method testDeserialize.

@Test
@SuppressWarnings("checkstyle:linelength")
public void testDeserialize() {
    String example = "{\"instanceList\":[{\"ip\":\"1.1.1.1\",\"port\":1234,\"weight\":1.0,\"healthy\":true,\"enabled\":true,\"ephemeral\":true,\"clusterName\":\"cluster\",\"metadata\":{},\"lastBeat\":1590563397264,\"marked\":false,\"instanceIdGenerator\":\"simple\",\"instanceHeartBeatInterval\":5000,\"instanceHeartBeatTimeOut\":15000,\"ipDeleteTimeout\":30000}]}";
    Instances actual = serializer.deserialize(ByteUtils.toBytes(example), Instances.class);
    assertEquals(1, actual.getInstanceList().size());
    Instance actualInstance = actual.getInstanceList().get(0);
    assertEquals("1.1.1.1", actualInstance.getIp());
    assertEquals("cluster", actualInstance.getClusterName());
    assertEquals(1234, actualInstance.getPort());
}
Also used : Instances(com.alibaba.nacos.naming.core.Instances) Instance(com.alibaba.nacos.naming.core.Instance) Test(org.junit.Test)

Example 10 with Instance

use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.

the class AddressServerClusterController method deleteCluster.

/**
 * Delete cluster.
 *
 * @param product Ip list of products to be associated
 * @param cluster Ip list of product cluster to be associated
 * @param ips     will delete ips.
 * @return delete result (the cluster information is return if success, exception information is return if  fail)
 */
@RequestMapping(value = "", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteCluster(@RequestParam(required = false) String product, @RequestParam(required = false) String cluster, @RequestParam String ips) {
    // 1. prepare the storage name for product and cluster
    String productName = addressServerGeneratorManager.generateProductName(product);
    String clusterName = addressServerManager.getDefaultClusterNameIfEmpty(cluster);
    // 2. prepare the response name for product and cluster to client
    String rawProductName = addressServerManager.getRawProductName(product);
    String rawClusterName = addressServerManager.getRawClusterName(cluster);
    ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.OK).body("product=" + rawProductName + ", cluster=" + rawClusterName + " delete success.");
    try {
        String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName);
        Service service = serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName);
        if (service == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + rawProductName + " not found.");
        }
        if (StringUtils.isBlank(ips)) {
            // delete all ips from the cluster
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ips must not be empty.");
        }
        // delete specified ip list
        String[] ipArray = addressServerManager.splitIps(ips);
        String checkResult = InternetAddressUtil.checkIPs(ipArray);
        if (InternetAddressUtil.checkOK(checkResult)) {
            List<Instance> instanceList = addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
            serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray(new Instance[instanceList.size()]));
        } else {
            responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult);
        }
    } catch (Exception e) {
        responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getCause());
    }
    return responseEntity;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Instance(com.alibaba.nacos.naming.core.Instance) Service(com.alibaba.nacos.naming.core.Service) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Instance (com.alibaba.nacos.naming.core.Instance)26 Test (org.junit.Test)13 Instances (com.alibaba.nacos.naming.core.Instances)6 HashMap (java.util.HashMap)6 BaseTest (com.alibaba.nacos.naming.BaseTest)5 Cluster (com.alibaba.nacos.naming.core.Cluster)5 Service (com.alibaba.nacos.naming.core.Service)5 ArrayList (java.util.ArrayList)5 Datum (com.alibaba.nacos.naming.consistency.Datum)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)4 LinkedList (java.util.LinkedList)3 InstanceOperationInfo (com.alibaba.nacos.naming.pojo.InstanceOperationInfo)2 Function (java.util.function.Function)2 Before (org.junit.Before)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)1 AbstractHealthChecker (com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker)1 Http (com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http)1