Search in sources :

Example 1 with RaftPeer

use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer 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 2 with RaftPeer

use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer in project nacos by alibaba.

the class BaseTest method mockRaft.

protected void mockRaft() {
    RaftPeer peer = new RaftPeer();
    peer.ip = NetUtils.localServer();
    raftCore.setPeerSet(peerSet);
    Mockito.when(peerSet.local()).thenReturn(peer);
    Mockito.when(peerSet.getLeader()).thenReturn(peer);
    Mockito.when(peerSet.isLeader(NetUtils.localServer())).thenReturn(true);
}
Also used : RaftPeer(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer)

Example 3 with RaftPeer

use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer in project nacos by alibaba.

the class RaftController method beat.

/**
 * Beat api.
 *
 * @param request  http request
 * @param response http response
 * @return peer information
 * @throws Exception exception
 */
@PostMapping("/beat")
public JsonNode beat(HttpServletRequest request, HttpServletResponse response) throws Exception {
    if (versionJudgement.allMemberIsNewVersion()) {
        throw new IllegalStateException("old raft protocol already stop");
    }
    String entity = new String(IoUtils.tryDecompress(request.getInputStream()), StandardCharsets.UTF_8);
    String value = URLDecoder.decode(entity, "UTF-8");
    value = URLDecoder.decode(value, "UTF-8");
    JsonNode json = JacksonUtils.toObj(value);
    RaftPeer peer = raftCore.receivedBeat(JacksonUtils.toObj(json.get("beat").asText()));
    return JacksonUtils.transferToJsonNode(peer);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) RaftPeer(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with RaftPeer

use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer in project nacos by alibaba.

the class RaftController method getPeer.

/**
 * Get peer information.
 *
 * @param request  http request
 * @param response http response
 * @return peer information
 */
@GetMapping("/peer")
public JsonNode getPeer(HttpServletRequest request, HttpServletResponse response) {
    if (versionJudgement.allMemberIsNewVersion()) {
        throw new IllegalStateException("old raft protocol already stop");
    }
    List<RaftPeer> peers = raftCore.getPeers();
    RaftPeer peer = null;
    for (RaftPeer peer1 : peers) {
        if (StringUtils.equals(peer1.ip, NetUtils.localServer())) {
            peer = peer1;
        }
    }
    if (peer == null) {
        peer = new RaftPeer();
        peer.ip = NetUtils.localServer();
    }
    return JacksonUtils.transferToJsonNode(peer);
}
Also used : RaftPeer(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with RaftPeer

use of com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer in project nacos by alibaba.

the class RaftController method onDelete.

/**
 * Commit delete datum.
 *
 * @param request  http request
 * @param response http response
 * @return 'ok' if success
 * @throws Exception exception
 */
@DeleteMapping("/datum/commit")
public String onDelete(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");
    value = URLDecoder.decode(value, "UTF-8");
    JsonNode jsonObject = JacksonUtils.toObj(value);
    Datum datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), Datum.class);
    RaftPeer source = JacksonUtils.toObj(jsonObject.get("source").toString(), RaftPeer.class);
    raftConsistencyService.onRemove(datum, source);
    return "ok";
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) JsonNode(com.fasterxml.jackson.databind.JsonNode) RaftPeer(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Aggregations

RaftPeer (com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Datum (com.alibaba.nacos.naming.consistency.Datum)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 Instances (com.alibaba.nacos.naming.core.Instances)1 Service (com.alibaba.nacos.naming.core.Service)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1