Search in sources :

Example 1 with Instances

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

the class DoubleWriteInstanceChangeToV1Task method run.

@Override
public void run() {
    try {
        ServiceManager serviceManager = ApplicationUtils.getBean(ServiceManager.class);
        com.alibaba.nacos.naming.core.Service serviceV1 = serviceManager.getService(service.getNamespace(), service.getGroupedServiceName());
        if (null == serviceV1) {
            serviceManager.createEmptyService(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
        }
        Instances newInstances = getNewInstances();
        String key = KeyBuilder.buildInstanceListKey(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
        ConsistencyService consistencyService = ApplicationUtils.getBean(NAME, ConsistencyService.class);
        consistencyService.put(key, newInstances);
    } catch (Exception e) {
        if (Loggers.SRV_LOG.isDebugEnabled()) {
            Loggers.SRV_LOG.debug("Double write task for {} instance from 2 to 1 failed", service, e);
        }
        ServiceChangeV2Task retryTask = new ServiceChangeV2Task(service, DoubleWriteContent.INSTANCE);
        retryTask.setTaskInterval(INTERVAL);
        String taskKey = ServiceChangeV2Task.getKey(service);
        ApplicationUtils.getBean(DoubleWriteDelayTaskEngine.class).addTask(taskKey, retryTask);
    }
}
Also used : Instances(com.alibaba.nacos.naming.core.Instances) ServiceManager(com.alibaba.nacos.naming.core.ServiceManager) ServiceChangeV2Task(com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV2Task) ConsistencyService(com.alibaba.nacos.naming.consistency.ConsistencyService)

Example 2 with Instances

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

the class DoubleWriteInstanceChangeToV1Task method getNewInstances.

private Instances getNewInstances() {
    Instances result = new Instances();
    ServiceStorage serviceStorage = ApplicationUtils.getBean(ServiceStorage.class);
    InstanceUpgradeHelper instanceUpgradeHelper = ApplicationUtils.getBean(InstanceUpgradeHelper.class);
    long currentTimeStamp = System.currentTimeMillis();
    for (Instance each : serviceStorage.getData(service).getHosts()) {
        com.alibaba.nacos.naming.core.Instance instance = instanceUpgradeHelper.toV1(each);
        instance.setLastBeat(currentTimeStamp);
        result.getInstanceList().add(instance);
    }
    return result;
}
Also used : Instances(com.alibaba.nacos.naming.core.Instances) ServiceStorage(com.alibaba.nacos.naming.core.v2.index.ServiceStorage) Instance(com.alibaba.nacos.api.naming.pojo.Instance)

Example 3 with Instances

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

the class RaftController method onPublish.

/**
 * Commit publish datum.
 *
 * @param request  http request
 * @param response http response
 * @return 'ok' if success
 * @throws Exception exception
 */
@PostMapping("/datum/commit")
public String onPublish(HttpServletRequest request, HttpServletResponse response) throws Exception {
    if (versionJudgement.allMemberIsNewVersion()) {
        throw new IllegalStateException("old raft protocol already stop");
    }
    response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Content-Encode", "gzip");
    String entity = IoUtils.toString(request.getInputStream(), "UTF-8");
    String value = URLDecoder.decode(entity, "UTF-8");
    JsonNode jsonObject = JacksonUtils.toObj(value);
    String key = "key";
    RaftPeer source = JacksonUtils.toObj(jsonObject.get("source").toString(), RaftPeer.class);
    JsonNode datumJson = jsonObject.get("datum");
    Datum datum = null;
    if (KeyBuilder.matchInstanceListKey(datumJson.get(key).asText())) {
        datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<Instances>>() {
        });
    } else if (KeyBuilder.matchSwitchKey(datumJson.get(key).asText())) {
        datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<SwitchDomain>>() {
        });
    } else if (KeyBuilder.matchServiceMetaKey(datumJson.get(key).asText())) {
        datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<Service>>() {
        });
    }
    raftConsistencyService.onPut(datum, source);
    return "ok";
}
Also used : Instances(com.alibaba.nacos.naming.core.Instances) Datum(com.alibaba.nacos.naming.consistency.Datum) Service(com.alibaba.nacos.naming.core.Service) JsonNode(com.fasterxml.jackson.databind.JsonNode) TypeReference(com.fasterxml.jackson.core.type.TypeReference) RaftPeer(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with Instances

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

the class RaftStore method readDatum.

private synchronized Datum readDatum(File file, String namespaceId) throws IOException {
    if (!KeyBuilder.isDatumCacheFile(file.getName())) {
        return null;
    }
    ByteBuffer buffer;
    try (FileChannel fc = new FileInputStream(file).getChannel()) {
        buffer = ByteBuffer.allocate((int) file.length());
        fc.read(buffer);
        String json = new String(buffer.array(), StandardCharsets.UTF_8);
        if (StringUtils.isBlank(json)) {
            return null;
        }
        final String fileName = file.getName();
        if (KeyBuilder.matchSwitchKey(fileName)) {
            return JacksonUtils.toObj(json, new TypeReference<Datum<SwitchDomain>>() {
            });
        }
        if (KeyBuilder.matchServiceMetaKey(fileName)) {
            Datum<Service> serviceDatum;
            try {
                serviceDatum = JacksonUtils.toObj(json.replace("\\", ""), new TypeReference<Datum<Service>>() {
                });
            } catch (Exception e) {
                JsonNode jsonObject = JacksonUtils.toObj(json);
                serviceDatum = new Datum<>();
                serviceDatum.timestamp.set(jsonObject.get("timestamp").asLong());
                serviceDatum.key = jsonObject.get("key").asText();
                serviceDatum.value = JacksonUtils.toObj(jsonObject.get("value").toString(), Service.class);
            }
            if (StringUtils.isBlank(serviceDatum.value.getGroupName())) {
                serviceDatum.value.setGroupName(Constants.DEFAULT_GROUP);
            }
            if (!serviceDatum.value.getName().contains(Constants.SERVICE_INFO_SPLITER)) {
                serviceDatum.value.setName(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceDatum.value.getName());
            }
            return serviceDatum;
        }
        if (KeyBuilder.matchInstanceListKey(fileName)) {
            Datum<Instances> instancesDatum;
            try {
                instancesDatum = JacksonUtils.toObj(json, new TypeReference<Datum<Instances>>() {
                });
            } catch (Exception e) {
                JsonNode jsonObject = JacksonUtils.toObj(json);
                instancesDatum = new Datum<>();
                instancesDatum.timestamp.set(jsonObject.get("timestamp").asLong());
                String key = jsonObject.get("key").asText();
                String serviceName = KeyBuilder.getServiceName(key);
                key = key.substring(0, key.indexOf(serviceName)) + Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName;
                instancesDatum.key = key;
                instancesDatum.value = new Instances();
                instancesDatum.value.setInstanceList(JacksonUtils.toObj(jsonObject.get("value").toString(), new TypeReference<List<Instance>>() {
                }));
                if (!instancesDatum.value.getInstanceList().isEmpty()) {
                    for (Instance instance : instancesDatum.value.getInstanceList()) {
                        instance.setEphemeral(false);
                    }
                }
            }
            return instancesDatum;
        }
        return JacksonUtils.toObj(json, Datum.class);
    } catch (Exception e) {
        Loggers.RAFT.warn("waning: failed to deserialize key: {}", file.getName());
        throw e;
    }
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) Instance(com.alibaba.nacos.naming.core.Instance) FileChannel(java.nio.channels.FileChannel) Service(com.alibaba.nacos.naming.core.Service) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) NacosException(com.alibaba.nacos.api.exception.NacosException) IOException(java.io.IOException) Instances(com.alibaba.nacos.naming.core.Instances) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 5 with Instances

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

the class NamingHttpClientProxy_ITCase method testSyncData.

@Test
public void testSyncData() throws NacosException, InterruptedException {
    // write data to DataStore
    String groupedName = NamingUtils.getGroupedName(serviceName, groupName);
    Instances instances = new Instances();
    Instance instance = new Instance();
    instance.setIp("1.2.3.4");
    instance.setPort(8888);
    instance.setEphemeral(true);
    instance.setServiceName(groupedName);
    List<Instance> instanceList = new ArrayList<Instance>(1);
    instanceList.add(instance);
    instances.setInstanceList(instanceList);
    String key = KeyBuilder.buildInstanceListKey(namespaceId, instance.getServiceName(), true);
    Datum<Instances> datum = new Datum<>();
    datum.value = instances;
    datum.key = key;
    datum.timestamp.incrementAndGet();
    this.dataStore.put(key, datum);
    // sync data to server
    Map<String, Datum> dataMap = dataStore.getDataMap();
    byte[] content = serializer.serialize(dataMap);
    boolean result = NamingProxy.syncData(content, "localhost:" + port);
    if (!result) {
        Assert.fail("NamingProxy.syncData error");
    }
    // query instance by api
    List<com.alibaba.nacos.api.naming.pojo.Instance> allInstances = namingService.getAllInstances(serviceName, false);
    for (int i = 0; i < 3 && allInstances.isEmpty(); i++) {
        // wait for async op
        TimeUnit.SECONDS.sleep(100);
        allInstances = namingService.getAllInstances(serviceName, false);
    }
    if (allInstances.isEmpty()) {
        Assert.fail("instance is empty");
    }
    com.alibaba.nacos.api.naming.pojo.Instance dst = allInstances.get(0);
    Assert.assertEquals(instance.getIp(), dst.getIp());
    Assert.assertEquals(instance.getPort(), dst.getPort());
    Assert.assertEquals(instance.getServiceName(), dst.getServiceName());
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) Instance(com.alibaba.nacos.naming.core.Instance) ArrayList(java.util.ArrayList) Instances(com.alibaba.nacos.naming.core.Instances) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Instances (com.alibaba.nacos.naming.core.Instances)10 Instance (com.alibaba.nacos.naming.core.Instance)6 Datum (com.alibaba.nacos.naming.consistency.Datum)5 Test (org.junit.Test)4 Service (com.alibaba.nacos.naming.core.Service)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Before (org.junit.Before)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 BaseTest (com.alibaba.nacos.naming.BaseTest)1 ConsistencyService (com.alibaba.nacos.naming.consistency.ConsistencyService)1 RaftPeer (com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer)1 ServiceManager (com.alibaba.nacos.naming.core.ServiceManager)1 ServiceStorage (com.alibaba.nacos.naming.core.v2.index.ServiceStorage)1 ServiceChangeV2Task (com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV2Task)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1